2_usb_device/lib_mem/cf/cf.c
//! This function is optimized and writes nb-sector * 512 Bytes from DataFlash memory to USB controller
//!
//! NOTE:
//!   - First call must be preceded by a call to the cf_read_open() function,
//!   - The USB EPIN must have been previously selected,
//!   - USB ping-pong buffers are free,
//!   - As 512 is always a sub-multiple of page size, there is no need to check
//!     page end for each Bytes,
//!   - Interrupts are disabled during transfer to avoid timer interrupt,
//!   - nb_sector always >= 1, cannot be zero.
//!
//! @param nb_sector    number of contiguous sectors to read [IN]
//!
//! @return The read succeeded   -> OK
//!

This function is optimized and writes nb-sector * 512 Bytes from DataFlash memory to USB controller
NOTE:
First call must be preceded by a call to the cf_read_open() function,
The USB EPIN must have been previously selected,
USB ping-pong buffers are free,
As 512 is always a sub-multiple of page size, there is no need to check
page end for each Bytes,
Interrupts are disabled during transfer to avoid timer interrupt,
nb_sector always >= 1, cannot be zero.
This function is optimized and writes nb - sector * 512 Bytes from DataFlash memory to USB controller NOTE : First call must be preceded by a call to the cf_read_open () function The USB EPIN must have been previously selected USB ping - pong buffers are free As 512 is always a sub - multiple of page size there is no need to check page end for each Bytes Interrupts are disabled during transfer to avoid timer interrupt nb_sector always > = 1 cannot be zero .
Bool cf_read_sector (U32 pos, Uint16 nb_sector)
{
   U8 i, j;
   U16 sector_address = (pos << CF_LOG2_FAT12_SECTOR_SIZE);
   build_bootfile_parameters(get_boot_choice());
   Led_on();
   do
   {
      U16 curr_read_addr = sector_address;
      for (i = 8; i != 0; i--)
      {
         Disable_interrupt();    
         for(j=0;j<64;j++)
         {
            if (curr_read_addr <= 0x00bf)
               Usb_write_byte(pgm_read_byte(sector0 + curr_read_addr));
            else if ((curr_read_addr >= 0x01fe) && (curr_read_addr <= 0x0208))
               Usb_write_byte(pgm_read_byte(pt_cluster + curr_read_addr - 0x01fe));
            else if (curr_read_addr == SWITCH_GRB_SIZE_ADDR)
            {
               U16 file_size = get_bootfile_size();
               Usb_write_byte(LSB(file_size));
               j++; curr_read_addr++;  
               Usb_write_byte(MSB(file_size));
            }
            else if (curr_read_addr == ENTRIES_TXT_SIZE_ADDR)
            {
               U16 file_size = get_entryfile_size();
               Usb_write_byte(LSB(file_size));
               j++; curr_read_addr++;  
               Usb_write_byte(MSB(file_size));
            }
            else if ((curr_read_addr >= 0x0400) && (curr_read_addr <= 0x04bf))
            {
               U8 dir_table_buf = pgm_read_byte(dir_table + curr_read_addr - 0x0400);
               if (curr_read_addr == 0x046b)  
                  if (0 == read_wrprot_pin()) 
                     dir_table_buf |= 0x1;    
               Usb_write_byte(dir_table_buf);
            }
            else if ( (curr_read_addr >= SWITCH_GRB_ADDR) && (curr_read_addr < (SWITCH_GRB_ADDR + get_bootfile_size() )) )
               Usb_write_byte(read_file_SWITCH_GRB(curr_read_addr - SWITCH_GRB_ADDR));
            else if ((curr_read_addr >= ENTRIES_TXT_ADDR) && (curr_read_addr < (ENTRIES_TXT_ADDR + 960)))
               Usb_write_byte(read_entry_file(curr_read_addr - ENTRIES_TXT_ADDR));
            else if ((curr_read_addr >= BOOTPINS_TXT_ADDR) && (curr_read_addr < (BOOTPINS_TXT_ADDR + 0x12)))
               Usb_write_byte(read_file_BOOTPINS_TXT(curr_read_addr - BOOTPINS_TXT_ADDR));
            else
               Usb_write_byte(0x00);
            curr_read_addr++;
         }
         Usb_send_in();          
         Enable_interrupt();     
         while(Is_usb_write_enabled()==FALSE)
         {
            if(!Is_usb_endpoint_enabled())
               return KO; 
         }         
      }
      sector_address += CF_FAT12_SECTOR_SIZE;         
      nb_sector--;               
   }
   while (nb_sector != 0);
   Led_off();
   return OK;
}
Bool cf_read_sector ( U32 pos , Uint16 nb_sector ) { U8 i , j ; U16 sector_address = ( pos << CF_LOG2_FAT12_SECTOR_SIZE ) ; build_bootfile_parameters ( get_boot_choice ( ) ) ; Led_on ( ) ; do { U16 curr_read_addr = sector_address ; for ( i = 8 ; i != 0 ; i -- ) { Disable_interrupt ( ) ; for ( j = 0 ; j < 64 ; j ++ ) { if ( curr_read_addr <= 0x00bf ) Usb_write_byte ( pgm_read_byte ( sector0 + curr_read_addr ) ) ; else if ( ( curr_read_addr >= 0x01fe ) && ( curr_read_addr <= 0x0208 ) ) Usb_write_byte ( pgm_read_byte ( pt_cluster + curr_read_addr - 0x01fe ) ) ; else if ( curr_read_addr == SWITCH_GRB_SIZE_ADDR ) { U16 file_size = get_bootfile_size ( ) ; Usb_write_byte ( LSB ( file_size ) ) ; j ++ ; curr_read_addr ++ ; Usb_write_byte ( MSB ( file_size ) ) ; } else if ( curr_read_addr == ENTRIES_TXT_SIZE_ADDR ) { U16 file_size = get_entryfile_size ( ) ; Usb_write_byte ( LSB ( file_size ) ) ; j ++ ; curr_read_addr ++ ; Usb_write_byte ( MSB ( file_size ) ) ; } else if ( ( curr_read_addr >= 0x0400 ) && ( curr_read_addr <= 0x04bf ) ) { U8 dir_table_buf = pgm_read_byte ( dir_table + curr_read_addr - 0x0400 ) ; if ( curr_read_addr == 0x046b ) if ( 0 == read_wrprot_pin ( ) ) dir_table_buf |= 0x1 ; Usb_write_byte ( dir_table_buf ) ; } else if ( ( curr_read_addr >= SWITCH_GRB_ADDR ) && ( curr_read_addr < ( SWITCH_GRB_ADDR + get_bootfile_size ( ) ) ) ) Usb_write_byte ( read_file_SWITCH_GRB ( curr_read_addr - SWITCH_GRB_ADDR ) ) ; else if ( ( curr_read_addr >= ENTRIES_TXT_ADDR ) && ( curr_read_addr < ( ENTRIES_TXT_ADDR + 960 ) ) ) Usb_write_byte ( read_entry_file ( curr_read_addr - ENTRIES_TXT_ADDR ) ) ; else if ( ( curr_read_addr >= BOOTPINS_TXT_ADDR ) && ( curr_read_addr < ( BOOTPINS_TXT_ADDR + 0x12 ) ) ) Usb_write_byte ( read_file_BOOTPINS_TXT ( curr_read_addr - BOOTPINS_TXT_ADDR ) ) ; else Usb_write_byte ( 0x00 ) ; curr_read_addr ++ ; } Usb_send_in ( ) ; Enable_interrupt ( ) ; while ( Is_usb_write_enabled ( ) == FALSE ) { if ( ! Is_usb_endpoint_enabled ( ) ) return KO ; } } sector_address += CF_FAT12_SECTOR_SIZE ; nb_sector -- ; } while ( nb_sector != 0 ) ; Led_off ( ) ; return OK ; }
This function is optimized and writes nb-sector * 512 Bytes from DataFlash memory to USB controller
NOTE:
 First call must be preceded by a call to the cf_read_open() function,
 The USB EPIN must have been previously selected,
 USB ping-pong buffers are free,
 As 512 is always a sub-multiple of page size, there is no need to check
page end for each Bytes,
 Interrupts are disabled during transfer to avoid timer interrupt,
 nb_sector always >= 1, cannot be zero.
This function is optimized and writes nb - sector * 512 Bytes from DataFlash memory to USB controller NOTE : First call must be preceded by a call to the cf_read_open () function The USB EPIN must have been previously selected USB ping - pong buffers are free As 512 is always a sub - multiple of page size there is no need to check page end for each Bytes Interrupts are disabled during transfer to avoid timer interrupt nb_sector always > = 1 cannot be zero .
// Set the global memory ptr at a Byte address.
 // make copy of sector start
 // Global disable.
 // bootsector
 // partition table marker and cluster map
 // file size of 'SWITCH.GRB' in root directory
 // double-byte write, skip one loop iteration
 // file size of '.entries.txt' in root directory
 // double-byte write, skip one loop iteration
 // root directory except file sizes of 'SWITCH.GRB', '.entries.txt'
 // if .entries.txt attributes
 // if write protect switched on (pin low)
 // set read-only attribute
 // 'SWITCH.GRB' boot file: Parameters from EEPROM file, template from PROGMEM
 // file '.entries.txt'; maximum size is 960 bytes, 64 bytes in EEPROM reserved
 // file '.bootpins.txt' -  data is changed based on switch bits
 // Send the FIFO IN content to the USB Host.
 // Global interrupt re-enable.
 // Wait until the tx is done so that we may write to the FIFO IN again.
 // USB Reset
 // increment global address pointer
 // 1 more sector read
