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

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

Returns    : total_bytes_read    Total bytes actually read.
READ DATA REQUESTED BY THE USER Description : This function reads data requested by the application from the file pointed by file descriptor . Arguments : fd file descriptor that points to a file buffer buffer into which the data is to be read num_bytes number of bytes requested by the application Returns : total_bytes_read Total bytes actually read .
uint32_t  FILE_Read (          int32_t  fd,
                       volatile  uint8_t  *buffer,
                                 uint32_t   num_bytes)
{
    uint32_t   total_bytes_to_read;            
    uint32_t   total_bytes_read;               
    uint32_t   bytes_read;                     
    uint32_t   bytes_to_read;                  
    FILE_ENTRY  *entry;                          
    uint16_t   next_clus;                     
    entry = &FAT_FileEntry[fd-1];                
    total_bytes_read = 0;
    if (entry->FileSize == 0) {
        return (0);
    }
    if (num_bytes < entry->FileSize) {
        total_bytes_to_read = num_bytes;
    } else {
        total_bytes_to_read = entry->FileSize;
    }
    do {
        next_clus = FAT_GetNextClus(entry->CurrClus);     
        if (next_clus == 0) {                             
            if (entry->CurrClusOffset == (entry->FileSize % FAT_BootSec.BytsPerClus)) {
                return (0);                               
            }                               
            if (total_bytes_to_read > ((entry->FileSize % FAT_BootSec.BytsPerClus) - entry->CurrClusOffset)) {
                total_bytes_to_read = (entry->FileSize % FAT_BootSec.BytsPerClus) - entry->CurrClusOffset;
            }
            bytes_to_read = total_bytes_to_read;
        } else if (total_bytes_to_read > (FAT_BootSec.BytsPerClus - entry->CurrClusOffset)) {
            bytes_to_read = FAT_BootSec.BytsPerClus - entry->CurrClusOffset;
        } else {
            bytes_to_read = total_bytes_to_read;
        }
        bytes_read = FAT_ClusRead(entry->CurrClus,       
                                  entry->CurrClusOffset,
                                  buffer,
                                  bytes_to_read);
        buffer              += bytes_read;
        total_bytes_read    += bytes_read;
        total_bytes_to_read -= bytes_read;
        if (entry->CurrClusOffset + bytes_read == FAT_BootSec.BytsPerClus) {
            entry->CurrClusOffset = 0;
        } else {
            entry->CurrClusOffset += bytes_read;        
        }
        if (entry->CurrClusOffset == 0) {
            entry->CurrClus = (next_clus > 0) ? next_clus : entry->CurrClus;
        }
    } while (total_bytes_to_read);
    return (total_bytes_read);
}
uint32_t FILE_Read ( int32_t fd , volatile uint8_t * buffer , uint32_t num_bytes ) { uint32_t total_bytes_to_read ; uint32_t total_bytes_read ; uint32_t bytes_read ; uint32_t bytes_to_read ; FILE_ENTRY * entry ; uint16_t next_clus ; entry = & FAT_FileEntry [ fd - 1 ] ; total_bytes_read = 0 ; if ( entry -> FileSize == 0 ) { return ( 0 ) ; } if ( num_bytes < entry -> FileSize ) { total_bytes_to_read = num_bytes ; } else { total_bytes_to_read = entry -> FileSize ; } do { next_clus = FAT_GetNextClus ( entry -> CurrClus ) ; if ( next_clus == 0 ) { if ( entry -> CurrClusOffset == ( entry -> FileSize % FAT_BootSec . BytsPerClus ) ) { return ( 0 ) ; } if ( total_bytes_to_read > ( ( entry -> FileSize % FAT_BootSec . BytsPerClus ) - entry -> CurrClusOffset ) ) { total_bytes_to_read = ( entry -> FileSize % FAT_BootSec . BytsPerClus ) - entry -> CurrClusOffset ; } bytes_to_read = total_bytes_to_read ; } else if ( total_bytes_to_read > ( FAT_BootSec . BytsPerClus - entry -> CurrClusOffset ) ) { bytes_to_read = FAT_BootSec . BytsPerClus - entry -> CurrClusOffset ; } else { bytes_to_read = total_bytes_to_read ; } bytes_read = FAT_ClusRead ( entry -> CurrClus , entry -> CurrClusOffset , buffer , bytes_to_read ) ; buffer += bytes_read ; total_bytes_read += bytes_read ; total_bytes_to_read -= bytes_read ; if ( entry -> CurrClusOffset + bytes_read == FAT_BootSec . BytsPerClus ) { entry -> CurrClusOffset = 0 ; } else { entry -> CurrClusOffset += bytes_read ; } if ( entry -> CurrClusOffset == 0 ) { entry -> CurrClus = ( next_clus > 0 ) ? next_clus : entry -> CurrClus ; } } while ( total_bytes_to_read ) ; return ( total_bytes_read ) ; }
READ DATA REQUESTED BY THE USER
Description: This function reads data requested by the application from the file pointed by file descriptor.
READ DATA REQUESTED BY THE USER Description : This function reads data requested by the application from the file pointed by file descriptor .
/* Total bytes requested by the application                */ /* Total bytes read                                        */ /* Bytes read from one cluster                             */ /* Bytes to be read in one cluster                         */ /* Entry that contains the file attribute information      */ /* Next cluster of the current cluster in the cluster chain */ /* Get file entry from file descriptor                     */ /* Get next cluster                               */ /* If the current cluster is the last cluster     */ /* If the offset is at the end of the file        */ /* No more bytes to read                          */ /* If requested number is > remaining bytes in the last cluster */ /* If requested number is > remaining bytes in the current cluster */ /* Read bytes from a single cluster                */ /* If the cluster offset reaches end of the cluster, make it 0 */ /* Else increment the cluster offset                */