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

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

Returns    : tot_bytes_read    Total bytes written into the current cluster
WRITE TO ONE CLUSTER Description : This function writes the data to a single cluster . Arguments : curr_clus Current cluster into which the data has to write clus_offset Position in the current cluster from which the data has to write buffer Buffer from which the data has to write num_bytes Number of bytes to write Returns : tot_bytes_read Total bytes written into the current cluster
uint32_t  FAT_ClusWrite (          uint16_t   curr_clus,
                                     uint32_t   clus_offset,
                           volatile  uint8_t  *buffer,
                                     uint32_t   num_bytes)
{
    uint32_t  tot_bytes_written;
	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_written = 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++) {
            FATBuffer[cnt] = *buffer;
			buffer++;
		}
        MS_BulkSend(sec_num, 1, FATBuffer);
		tot_bytes_written += 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_BulkSend(sec_num, num_sec, buffer);
		buffer            += (num_sec * FAT_BootSec.BytsPerSec);
		tot_bytes_written += (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++) {
			FATBuffer[cnt] = *buffer;
			buffer++;
		}
        MS_BulkSend(sec_num, 1, FATBuffer);
		tot_bytes_written += num_bytes;
	}
	return (tot_bytes_written);
}
uint32_t FAT_ClusWrite ( uint16_t curr_clus , uint32_t clus_offset , volatile uint8_t * buffer , uint32_t num_bytes ) { uint32_t tot_bytes_written ; 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_written = 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 ++ ) { FATBuffer [ cnt ] = * buffer ; buffer ++ ; } MS_BulkSend ( sec_num , 1 , FATBuffer ) ; tot_bytes_written += 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_BulkSend ( sec_num , num_sec , buffer ) ; buffer += ( num_sec * FAT_BootSec . BytsPerSec ) ; tot_bytes_written += ( 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 ++ ) { FATBuffer [ cnt ] = * buffer ; buffer ++ ; } MS_BulkSend ( sec_num , 1 , FATBuffer ) ; tot_bytes_written += num_bytes ; } return ( tot_bytes_written ) ; }
WRITE TO ONE CLUSTER
Description: This function writes the data to a single cluster.
WRITE TO ONE CLUSTER Description : This function writes the data to a single cluster .