keil_examples/USBHostLite/UsbHost/Fat/usbhost_fat.c
/*
**************************************************************************************************************
*                                                 READ FROM ONE CLUSTER
*
* Description: This function reads the data from a single cluster.
*
* Arguments  : curr_clus         Current cluster from which the data has to read
*              clus_offset       Position in the current cluster from which the data has to read
*              buffer            Buffer into which the data has to read
*              num_bytes         Number of bytes to read
*
* Returns    : tot_bytes_read    Total bytes read from the current cluster
*
**************************************************************************************************************
*/
READ FROM ONE CLUSTER
Description: This function reads the data from a single cluster.

Arguments  : curr_clus         Current cluster from which the data has to read
clus_offset       Position in the current cluster from which the data has to read
buffer            Buffer into which the data has to read
num_bytes         Number of bytes to read

Returns    : tot_bytes_read    Total bytes read from the current cluster
READ FROM ONE CLUSTER Description : This function reads the data from a single cluster . Arguments : curr_clus Current cluster from which the data has to read clus_offset Position in the current cluster from which the data has to read buffer Buffer into which the data has to read num_bytes Number of bytes to read Returns : tot_bytes_read Total bytes read from the current cluster
uint32_t  FAT_ClusRead (          uint16_t   curr_clus,
                                    uint32_t   clus_offset,
                          volatile  uint8_t  *buffer,
                                    uint32_t   num_bytes)
{
    uint32_t  tot_bytes_read;                              
    uint32_t  n_bytes;                                     
    uint32_t  start_sec;                                   
    uint32_t  sec_num;                                     
    uint16_t  num_sec;                                     
    uint32_t  sec_offset;                                  
    uint32_t  cnt;
    tot_bytes_read = 0;
    start_sec  = ((curr_clus - 2) * FAT_BootSec.SecPerClus) + FAT_BootSec.FirstDataSec;
    sec_num    = start_sec + (clus_offset / FAT_BootSec.BytsPerSec);
    num_sec    = num_bytes / FAT_BootSec.BytsPerSec;
    sec_offset = clus_offset % FAT_BootSec.BytsPerSec;
    if (sec_offset) {                                 
        MS_BulkRecv(sec_num, 1, FATBuffer);           
        n_bytes = (FAT_BootSec.BytsPerSec - sec_offset <= num_bytes) ?
                  (FAT_BootSec.BytsPerSec - sec_offset) : num_bytes;
        for (cnt = sec_offset; cnt < sec_offset + n_bytes; cnt++) {
            *buffer = FATBuffer[cnt];                 
             buffer++;
        }
        tot_bytes_read += n_bytes;
        clus_offset    += n_bytes;
        num_bytes      -= n_bytes;
        sec_num++;
    }
    if (num_bytes / FAT_BootSec.BytsPerSec) {         
        num_sec = num_bytes / FAT_BootSec.BytsPerSec;
        MS_BulkRecv(sec_num, num_sec, buffer);
        buffer         += (num_sec * FAT_BootSec.BytsPerSec);
        tot_bytes_read += (num_sec * FAT_BootSec.BytsPerSec);
        clus_offset    += (num_sec * FAT_BootSec.BytsPerSec);
        num_bytes      -= (num_sec * FAT_BootSec.BytsPerSec);
        sec_num        += num_sec;
    }
    if (num_bytes) {                                  
        MS_BulkRecv(sec_num, 1, FATBuffer);
        for (cnt = 0; cnt < num_bytes; cnt++) {
            *buffer = FATBuffer[cnt];                 
             buffer++;
        }
        tot_bytes_read += num_bytes;
    }
    return (tot_bytes_read);
}
uint32_t FAT_ClusRead ( uint16_t curr_clus , uint32_t clus_offset , volatile uint8_t * buffer , uint32_t num_bytes ) { uint32_t tot_bytes_read ; uint32_t n_bytes ; uint32_t start_sec ; uint32_t sec_num ; uint16_t num_sec ; uint32_t sec_offset ; uint32_t cnt ; tot_bytes_read = 0 ; start_sec = ( ( curr_clus - 2 ) * FAT_BootSec . SecPerClus ) + FAT_BootSec . FirstDataSec ; sec_num = start_sec + ( clus_offset / FAT_BootSec . BytsPerSec ) ; num_sec = num_bytes / FAT_BootSec . BytsPerSec ; sec_offset = clus_offset % FAT_BootSec . BytsPerSec ; if ( sec_offset ) { MS_BulkRecv ( sec_num , 1 , FATBuffer ) ; n_bytes = ( FAT_BootSec . BytsPerSec - sec_offset <= num_bytes ) ? ( FAT_BootSec . BytsPerSec - sec_offset ) : num_bytes ; for ( cnt = sec_offset ; cnt < sec_offset + n_bytes ; cnt ++ ) { * buffer = FATBuffer [ cnt ] ; buffer ++ ; } tot_bytes_read += n_bytes ; clus_offset += n_bytes ; num_bytes -= n_bytes ; sec_num ++ ; } if ( num_bytes / FAT_BootSec . BytsPerSec ) { num_sec = num_bytes / FAT_BootSec . BytsPerSec ; MS_BulkRecv ( sec_num , num_sec , buffer ) ; buffer += ( num_sec * FAT_BootSec . BytsPerSec ) ; tot_bytes_read += ( num_sec * FAT_BootSec . BytsPerSec ) ; clus_offset += ( num_sec * FAT_BootSec . BytsPerSec ) ; num_bytes -= ( num_sec * FAT_BootSec . BytsPerSec ) ; sec_num += num_sec ; } if ( num_bytes ) { MS_BulkRecv ( sec_num , 1 , FATBuffer ) ; for ( cnt = 0 ; cnt < num_bytes ; cnt ++ ) { * buffer = FATBuffer [ cnt ] ; buffer ++ ; } tot_bytes_read += num_bytes ; } return ( tot_bytes_read ) ; }
READ FROM ONE CLUSTER
Description: This function reads the data from a single cluster.
READ FROM ONE CLUSTER Description : This function reads the data from a single cluster .
/* total bytes read in the current cluster     */ /* Bytes to read in the current sector         */ /* Starting sector of the current cluster      */ /*Current sector number                        */ /* Number of sectors to be read                */ /* Offset in the current sector                */ /* If the sector offset is at the middle of a sector  */ /* Read the first sector                              */ /* Copy the required bytes to user buffer             */ /* Read all the remaining full sectors                */ /* Read the last sector for the remaining bytes       */ /* Copy the required bytes to user buffer             */