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

This function is optimized and writes nb-sector * 512 Bytes from USB controller to DataFlash memory
NOTE:
First call must be preceded by a call to the cf_write_open() function,
As 512 is always a sub-multiple of page size, there is no need to check
page end for each Bytes,
The USB EPOUT must have been previously selected,
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 USB controller to DataFlash memory NOTE : First call must be preceded by a call to the cf_write_open () function As 512 is always a sub - multiple of page size there is no need to check page end for each Bytes The USB EPOUT must have been previously selected Interrupts are disabled during transfer to avoid timer interrupt nb_sector always > = 1 cannot be zero .
Bool cf_write_sector (U32 pos, Uint16 nb_sector)
{
   U8 i, j;
   U16 sector_address = (pos << CF_LOG2_FAT12_SECTOR_SIZE);
   Bool write_protected = (0 == read_wrprot_pin());
   Led_on();
   do
   {
      U16 curr_write_addr = sector_address;
      for (i = 8; i != 0; i--)
      {
         while(!Is_usb_read_enabled())
         {
            if(!Is_usb_endpoint_enabled())
              return KO; 
         }
         Disable_interrupt();    
         U8 usb_rx_buffer[64];
         if (write_protected)
         {
            for(j=0;j<64;j++)
               usb_rx_buffer[j] = Usb_read_byte();
         }
         else
         {
            for(j=0;j<64;j++)
            {
               usb_rx_buffer[j] = Usb_read_byte();
               if (curr_write_addr == ENTRIES_TXT_SIZE_ADDR)
               {
                  U16 file_size;
                  LSB(file_size) = usb_rx_buffer[j];
                  j++; curr_write_addr++; 
                  usb_rx_buffer[j] = Usb_read_byte();
                  MSB(file_size) = usb_rx_buffer[j];
                  if (file_size > 960)
                     file_size = 960;
                  set_entryfile_size(file_size);
                  eeprom_write_word((uint16_t*)1022, file_size);
                  parse_entry_file();
                  build_bootfile_parameters(get_boot_choice());
               }
               curr_write_addr++;
            }
            U16 last_block_address = curr_write_addr - 64;
            if ( (last_block_address >= ENTRIES_TXT_ADDR) && (last_block_address < (ENTRIES_TXT_ADDR + 960) ))
               eeprom_write_block((U8*)usb_rx_buffer, (U8*)(last_block_address-ENTRIES_TXT_ADDR), 64);
         }
         Usb_ack_receive_out();  
         Enable_interrupt();     
      } 
      sector_address += 512;         
      nb_sector--;               
   }
   while (nb_sector != 0);
   Led_off();
   return OK;                  
}
Bool cf_write_sector ( U32 pos , Uint16 nb_sector ) { U8 i , j ; U16 sector_address = ( pos << CF_LOG2_FAT12_SECTOR_SIZE ) ; Bool write_protected = ( 0 == read_wrprot_pin ( ) ) ; Led_on ( ) ; do { U16 curr_write_addr = sector_address ; for ( i = 8 ; i != 0 ; i -- ) { while ( ! Is_usb_read_enabled ( ) ) { if ( ! Is_usb_endpoint_enabled ( ) ) return KO ; } Disable_interrupt ( ) ; U8 usb_rx_buffer [ 64 ] ; if ( write_protected ) { for ( j = 0 ; j < 64 ; j ++ ) usb_rx_buffer [ j ] = Usb_read_byte ( ) ; } else { for ( j = 0 ; j < 64 ; j ++ ) { usb_rx_buffer [ j ] = Usb_read_byte ( ) ; if ( curr_write_addr == ENTRIES_TXT_SIZE_ADDR ) { U16 file_size ; LSB ( file_size ) = usb_rx_buffer [ j ] ; j ++ ; curr_write_addr ++ ; usb_rx_buffer [ j ] = Usb_read_byte ( ) ; MSB ( file_size ) = usb_rx_buffer [ j ] ; if ( file_size > 960 ) file_size = 960 ; set_entryfile_size ( file_size ) ; eeprom_write_word ( ( uint16_t * ) 1022 , file_size ) ; parse_entry_file ( ) ; build_bootfile_parameters ( get_boot_choice ( ) ) ; } curr_write_addr ++ ; } U16 last_block_address = curr_write_addr - 64 ; if ( ( last_block_address >= ENTRIES_TXT_ADDR ) && ( last_block_address < ( ENTRIES_TXT_ADDR + 960 ) ) ) eeprom_write_block ( ( U8 * ) usb_rx_buffer , ( U8 * ) ( last_block_address - ENTRIES_TXT_ADDR ) , 64 ) ; } Usb_ack_receive_out ( ) ; Enable_interrupt ( ) ; } sector_address += 512 ; nb_sector -- ; } while ( nb_sector != 0 ) ; Led_off ( ) ; return OK ; }
This function is optimized and writes nb-sector * 512 Bytes from USB controller to DataFlash memory
NOTE:
 First call must be preceded by a call to the cf_write_open() function,
 As 512 is always a sub-multiple of page size, there is no need to check
page end for each Bytes,
 The USB EPOUT must have been previously selected,
 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 USB controller to DataFlash memory NOTE : First call must be preceded by a call to the cf_write_open () function As 512 is always a sub - multiple of page size there is no need to check page end for each Bytes The USB EPOUT must have been previously selected 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
 //# Write 8x64b = 512b from the USB FIFO OUT.
 // Wait end of rx in USB EPOUT.
 // USB Reset
 // Global disable.
 // empty buffer, don't use data though
 // file size of '.entries.txt' in root directory
 // double-byte access, skip one loop iteration
 // limit file size to maximum if stored too big
 // copy 64 bytes to EEPROM if it was file .entries.txt
 // USB EPOUT read acknowledgement.
 // Global re-enable.
 // for (i = 8; i != 0; i--)
 // Update the memory pointer.
 // 1 more sector written
 // Write done
