keil_examples/USBHostLite/UsbHost/Fat/usbhost_fat.c
/*
**************************************************************************************************************
*                                       WRITE THE DATA REQUESTED BY THE USER
*
* Description: This function writes data requested by the application to the file pointed by file descriptor
*
* Arguments  : fd                     file descriptor that points to a file
*              buffer                 buffer from which the data is to be written
*              num_bytes              number of bytes requested by the application
*
* Returns    : total_bytes_written    Total bytes actually written
*
**************************************************************************************************************
*/
WRITE THE DATA REQUESTED BY THE USER
Description: This function writes data requested by the application to the file pointed by file descriptor

Arguments  : fd                     file descriptor that points to a file
buffer                 buffer from which the data is to be written
num_bytes              number of bytes requested by the application

Returns    : total_bytes_written    Total bytes actually written
WRITE THE DATA REQUESTED BY THE USER Description : This function writes data requested by the application to the file pointed by file descriptor Arguments : fd file descriptor that points to a file buffer buffer from which the data is to be written num_bytes number of bytes requested by the application Returns : total_bytes_written Total bytes actually written
uint32_t  FILE_Write (          int32_t  fd,
                        volatile  uint8_t  *buffer,
                                  uint32_t   num_bytes)
{
    uint32_t   total_bytes_to_write;                      
    uint32_t   total_bytes_written;                       
    uint32_t   bytes_written;                             
    uint32_t   bytes_to_write;                            
    FILE_ENTRY  *entry;                               
    uint16_t   free_clus;                                 
    entry = &FAT_FileEntry[fd-1];                           
    total_bytes_written  = 0;
    total_bytes_to_write = num_bytes;
    if (num_bytes) {
        if (entry->FileSize == 0) {
            free_clus = FAT_GetFreeClus();
            FAT_UpdateFAT(free_clus, 0xFFFF);
            entry->CurrClus  = free_clus;
            MS_BulkRecv(entry->EntrySec, 1, FATBuffer);
            WriteLE16U(&FATBuffer[(entry->EntrySecOffset) + 26], free_clus);
            MS_BulkSend(entry->EntrySec, 1, FATBuffer);
        }
    } else {
        return (0);
    }
    entry->CurrClus = FAT_GetEndClus(entry->CurrClus);           
    entry->CurrClusOffset = entry->FileSize % FAT_BootSec.BytsPerClus;   
    do {
        if (total_bytes_to_write > FAT_BootSec.BytsPerClus - entry->CurrClusOffset) {
            bytes_to_write = FAT_BootSec.BytsPerClus - entry->CurrClusOffset;
        } else {
            bytes_to_write = total_bytes_to_write;
        }
        bytes_written = FAT_ClusWrite(entry->CurrClus,
                                      entry->CurrClusOffset,
                                      buffer,
                                      bytes_to_write);
        buffer               += bytes_written;
        total_bytes_written  += bytes_written;
        total_bytes_to_write -= bytes_written;
        entry->FileSize      += bytes_written;
        if (entry->CurrClusOffset + bytes_written == FAT_BootSec.BytsPerClus) {
            entry->CurrClusOffset = 0;
        } else {
            entry->CurrClusOffset += bytes_written;
        }
        if (entry->CurrClusOffset == 0) {
            free_clus = FAT_GetFreeClus();
            if (free_clus == 0) {
                return (total_bytes_written);
            }
            FAT_UpdateFAT(entry->CurrClus, free_clus);
            FAT_UpdateFAT(free_clus, 0xFFFF);
            entry->CurrClus = free_clus;
        }
    } while (total_bytes_to_write);
    return (total_bytes_written);
}
uint32_t FILE_Write ( int32_t fd , volatile uint8_t * buffer , uint32_t num_bytes ) { uint32_t total_bytes_to_write ; uint32_t total_bytes_written ; uint32_t bytes_written ; uint32_t bytes_to_write ; FILE_ENTRY * entry ; uint16_t free_clus ; entry = & FAT_FileEntry [ fd - 1 ] ; total_bytes_written = 0 ; total_bytes_to_write = num_bytes ; if ( num_bytes ) { if ( entry -> FileSize == 0 ) { free_clus = FAT_GetFreeClus ( ) ; FAT_UpdateFAT ( free_clus , 0xFFFF ) ; entry -> CurrClus = free_clus ; MS_BulkRecv ( entry -> EntrySec , 1 , FATBuffer ) ; WriteLE16U ( & FATBuffer [ ( entry -> EntrySecOffset ) + 26 ] , free_clus ) ; MS_BulkSend ( entry -> EntrySec , 1 , FATBuffer ) ; } } else { return ( 0 ) ; } entry -> CurrClus = FAT_GetEndClus ( entry -> CurrClus ) ; entry -> CurrClusOffset = entry -> FileSize % FAT_BootSec . BytsPerClus ; do { if ( total_bytes_to_write > FAT_BootSec . BytsPerClus - entry -> CurrClusOffset ) { bytes_to_write = FAT_BootSec . BytsPerClus - entry -> CurrClusOffset ; } else { bytes_to_write = total_bytes_to_write ; } bytes_written = FAT_ClusWrite ( entry -> CurrClus , entry -> CurrClusOffset , buffer , bytes_to_write ) ; buffer += bytes_written ; total_bytes_written += bytes_written ; total_bytes_to_write -= bytes_written ; entry -> FileSize += bytes_written ; if ( entry -> CurrClusOffset + bytes_written == FAT_BootSec . BytsPerClus ) { entry -> CurrClusOffset = 0 ; } else { entry -> CurrClusOffset += bytes_written ; } if ( entry -> CurrClusOffset == 0 ) { free_clus = FAT_GetFreeClus ( ) ; if ( free_clus == 0 ) { return ( total_bytes_written ) ; } FAT_UpdateFAT ( entry -> CurrClus , free_clus ) ; FAT_UpdateFAT ( free_clus , 0xFFFF ) ; entry -> CurrClus = free_clus ; } } while ( total_bytes_to_write ) ; return ( total_bytes_written ) ; }
WRITE THE DATA REQUESTED BY THE USER
Description: This function writes data requested by the application to the file pointed by file descriptor
WRITE THE DATA REQUESTED BY THE USER Description : This function writes data requested by the application to the file pointed by file descriptor
/* Total bytes requested by application         */ /* Total bytes written                          */ /* Bytes written in a single cluster            */ /* Bytes to write in a single cluster           */ /* Entry that contains the file attribute information */ /* Free cluster available in the disk           */ /* Get file entry from file descriptor                     */ /* Make the current cluster as end cluster */ /* Move cluster offset to file end */