3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  33  *  Functions to manage the physical dataflash media, including reading and writing of 
  34  *  blocks of data. These functions are called by the SCSI layer when data must be stored 
  35  *  or retrieved to/from the physical storage media. If a different media is used (such 
  36  *  as a SD card or EEPROM), functions similar to these will need to be generated. 
  39 #define  INCLUDE_FROM_DATAFLASHMANAGER_C 
  40 #include "DataflashManager.h" 
  42 /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board dataflash IC(s), from 
  43  *  the pre-selected data OUT endpoint. This routine reads in OS sized blocks from the endpoint and writes 
  44  *  them to the dataflash in Dataflash page sized blocks. 
  46  *  \param BlockAddress  Data block starting address for the write sequence 
  47  *  \param TotalBlocks   Number of blocks of data to write 
  49 void DataflashManager_WriteBlocks(USB_ClassInfo_MS_t
* MSInterfaceInfo
, const uint32_t BlockAddress
, uint16_t TotalBlocks
) 
  51         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
  52         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
  53         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
  55         /* Copy selected dataflash's current page contents to the dataflash buffer */ 
  56         Dataflash_SelectChipFromPage(CurrDFPage
); 
  57         Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
); 
  58         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
  59         Dataflash_WaitWhileBusy(); 
  61         /* Send the dataflash buffer write command */ 
  62         Dataflash_ToggleSelectedChipCS(); 
  63         Dataflash_SendByte(DF_CMD_BUFF1WRITE
); 
  64         Dataflash_SendAddressBytes(0, CurrDFPageByte
); 
  66         /* Wait until endpoint is ready before continuing */ 
  67         while (!(Endpoint_IsReadWriteAllowed())); 
  71                 uint8_t BytesInBlockDiv16 
= 0; 
  73                 /* Write an endpoint packet sized data block to the dataflash */ 
  74                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
  76                         /* Check if the endpoint is currently empty */ 
  77                         if (!(Endpoint_IsReadWriteAllowed())) 
  79                                 /* Clear the current endpoint bank */ 
  82                                 /* Wait until the host has sent another packet */ 
  83                                 while (!(Endpoint_IsReadWriteAllowed())); 
  86                         /* Check if end of dataflash page reached */ 
  87                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
  89                                 /* Write the dataflash buffer contents back to the dataflash page */ 
  90                                 Dataflash_ToggleSelectedChipCS(); 
  91                                 Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
  92                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
  94                                 /* Reset the dataflash buffer counter, increment the page counter */ 
  95                                 CurrDFPageByteDiv16 
= 0; 
  98                                 /* Select the next dataflash chip based on the new dataflash page index */ 
  99                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 100                                 Dataflash_WaitWhileBusy(); 
 102 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) 
 103                                 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */ 
 104                                 if ((TotalBlocks 
* (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) < (DATAFLASH_PAGE_SIZE 
>> 4)) 
 106                                         /* Copy selected dataflash's current page contents to the dataflash buffer */ 
 107                                         Dataflash_ToggleSelectedChipCS(); 
 108                                         Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
); 
 109                                         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 110                                         Dataflash_WaitWhileBusy(); 
 114                                 /* Send the dataflash buffer write command */ 
 115                                 Dataflash_ToggleSelectedChipCS(); 
 116                                 Dataflash_SendByte(DF_CMD_BUFF1WRITE
); 
 117                                 Dataflash_SendAddressBytes(0, 0); 
 120                         /* Write one 16-byte chunk of data to the dataflash */ 
 121                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 122                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 123                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 124                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 125                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 126                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 127                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 128                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 129                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 130                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 131                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 132                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 133                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 134                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 135                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 136                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 138                         /* Increment the dataflash page 16 byte block counter */ 
 139                         CurrDFPageByteDiv16
++; 
 141                         /* Increment the block 16 byte block counter */ 
 144                         /* Check if the current command is being aborted by the host */ 
 145                         if (MSInterfaceInfo
->IsMassStoreReset
) 
 149                 /* Decrement the blocks remaining counter and reset the sub block counter */ 
 153         /* Write the dataflash buffer contents back to the dataflash page */ 
 154         Dataflash_ToggleSelectedChipCS(); 
 155         Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
 156         Dataflash_SendAddressBytes(CurrDFPage
, 0x00); 
 157         Dataflash_WaitWhileBusy(); 
 159         /* If the endpoint is empty, clear it ready for the next packet from the host */ 
 160         if (!(Endpoint_IsReadWriteAllowed())) 
 163         /* Deselect all dataflash chips */ 
 164         Dataflash_DeselectChip(); 
 167 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into 
 168  *  the pre-selected data IN endpoint. This routine reads in Dataflash page sized blocks from the Dataflash 
 169  *  and writes them in OS sized blocks to the endpoint. 
 171  *  \param BlockAddress  Data block starting address for the read sequence 
 172  *  \param TotalBlocks   Number of blocks of data to read 
 174 void DataflashManager_ReadBlocks(USB_ClassInfo_MS_t
* MSInterfaceInfo
, const uint32_t BlockAddress
, uint16_t TotalBlocks
) 
 176         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
 177         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
 178         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
 180         /* Send the dataflash main memory page read command */ 
 181         Dataflash_SelectChipFromPage(CurrDFPage
); 
 182         Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 183         Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
); 
 184         Dataflash_SendByte(0x00); 
 185         Dataflash_SendByte(0x00); 
 186         Dataflash_SendByte(0x00); 
 187         Dataflash_SendByte(0x00); 
 189         /* Wait until endpoint is ready before continuing */ 
 190         while (!(Endpoint_IsReadWriteAllowed())); 
 194                 uint8_t BytesInBlockDiv16 
= 0; 
 196                 /* Write an endpoint packet sized data block to the dataflash */ 
 197                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
 199                         /* Check if the endpoint is currently full */ 
 200                         if (!(Endpoint_IsReadWriteAllowed())) 
 202                                 /* Clear the endpoint bank to send its contents to the host */ 
 205                                 /* Wait until the endpoint is ready for more data */ 
 206                                 while (!(Endpoint_IsReadWriteAllowed())); 
 209                         /* Check if end of dataflash page reached */ 
 210                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
 212                                 /* Reset the dataflash buffer counter, increment the page counter */ 
 213                                 CurrDFPageByteDiv16 
= 0; 
 216                                 /* Select the next dataflash chip based on the new dataflash page index */ 
 217                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 219                                 /* Send the dataflash main memory page read command */ 
 220                                 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 221                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 222                                 Dataflash_SendByte(0x00); 
 223                                 Dataflash_SendByte(0x00); 
 224                                 Dataflash_SendByte(0x00); 
 225                                 Dataflash_SendByte(0x00); 
 228                         /* Read one 16-byte chunk of data from the dataflash */ 
 229                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 230                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 231                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 232                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 233                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 234                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 235                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 236                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 237                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 238                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 239                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 240                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 241                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 242                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 243                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 244                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 246                         /* Increment the dataflash page 16 byte block counter */ 
 247                         CurrDFPageByteDiv16
++; 
 249                         /* Increment the block 16 byte block counter */ 
 252                         /* Check if the current command is being aborted by the host */ 
 253                         if (MSInterfaceInfo
->IsMassStoreReset
) 
 257                 /* Decrement the blocks remaining counter */ 
 261         /* If the endpoint is full, send its contents to the host */ 
 262         if (!(Endpoint_IsReadWriteAllowed())) 
 265         /* Deselect all dataflash chips */ 
 266         Dataflash_DeselectChip(); 
 269 /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board dataflash IC(s), from 
 270  *  the a given RAM buffer. This routine reads in OS sized blocks from the buffer and writes them to the 
 271  *  dataflash in Dataflash page sized blocks. This can be linked to FAT libraries to write files to the 
 274  *  \param BlockAddress  Data block starting address for the write sequence 
 275  *  \param TotalBlocks   Number of blocks of data to write 
 276  *  \param BufferPtr     Pointer to the data source RAM buffer 
 278 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
) 
 280         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
 281         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
 282         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
 284         /* Copy selected dataflash's current page contents to the dataflash buffer */ 
 285         Dataflash_SelectChipFromPage(CurrDFPage
); 
 286         Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
); 
 287         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 288         Dataflash_WaitWhileBusy(); 
 290         /* Send the dataflash buffer write command */ 
 291         Dataflash_ToggleSelectedChipCS(); 
 292         Dataflash_SendByte(DF_CMD_BUFF1WRITE
); 
 293         Dataflash_SendAddressBytes(0, CurrDFPageByte
); 
 297                 uint8_t BytesInBlockDiv16 
= 0; 
 299                 /* Write an endpoint packet sized data block to the dataflash */ 
 300                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
 302                         /* Check if end of dataflash page reached */ 
 303                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
 305                                 /* Write the dataflash buffer contents back to the dataflash page */ 
 306                                 Dataflash_ToggleSelectedChipCS(); 
 307                                 Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
 308                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 310                                 /* Reset the dataflash buffer counter, increment the page counter */ 
 311                                 CurrDFPageByteDiv16 
= 0; 
 314                                 /* Select the next dataflash chip based on the new dataflash page index */ 
 315                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 316                                 Dataflash_WaitWhileBusy(); 
 318 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) 
 319                                 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */ 
 320                                 if ((TotalBlocks 
* (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) < (DATAFLASH_PAGE_SIZE 
>> 4)) 
 322                                         /* Copy selected dataflash's current page contents to the dataflash buffer */ 
 323                                         Dataflash_ToggleSelectedChipCS(); 
 324                                         Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
); 
 325                                         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 326                                         Dataflash_WaitWhileBusy(); 
 330                                 /* Send the dataflash buffer write command */ 
 331                                 Dataflash_ToggleSelectedChipCS(); 
 332                                 Dataflash_SendByte(DF_CMD_BUFF1WRITE
); 
 333                                 Dataflash_SendAddressBytes(0, 0); 
 336                         /* Write one 16-byte chunk of data to the dataflash */ 
 337                         for (uint8_t ByteNum 
= 0; ByteNum 
< 16; ByteNum
++) 
 338                           Dataflash_SendByte(*(BufferPtr
++)); 
 340                         /* Increment the dataflash page 16 byte block counter */ 
 341                         CurrDFPageByteDiv16
++; 
 343                         /* Increment the block 16 byte block counter */ 
 347                 /* Decrement the blocks remaining counter and reset the sub block counter */ 
 351         /* Write the dataflash buffer contents back to the dataflash page */ 
 352         Dataflash_ToggleSelectedChipCS(); 
 353         Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
 354         Dataflash_SendAddressBytes(CurrDFPage
, 0x00); 
 355         Dataflash_WaitWhileBusy(); 
 357         /* Deselect all dataflash chips */ 
 358         Dataflash_DeselectChip(); 
 361 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into 
 362  *  the a preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash 
 363  *  and writes them in OS sized blocks to the given buffer. This can be linked to FAT libraries to read 
 364  *  the files stored on the dataflash. 
 366  *  \param BlockAddress  Data block starting address for the read sequence 
 367  *  \param TotalBlocks   Number of blocks of data to read 
 368  *  \param BufferPtr     Pointer to the data destination RAM buffer 
 370 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
) 
 372         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
 373         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
 374         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
 376         /* Send the dataflash main memory page read command */ 
 377         Dataflash_SelectChipFromPage(CurrDFPage
); 
 378         Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 379         Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
); 
 380         Dataflash_SendByte(0x00); 
 381         Dataflash_SendByte(0x00); 
 382         Dataflash_SendByte(0x00); 
 383         Dataflash_SendByte(0x00); 
 387                 uint8_t BytesInBlockDiv16 
= 0; 
 389                 /* Write an endpoint packet sized data block to the dataflash */ 
 390                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
 392                         /* Check if end of dataflash page reached */ 
 393                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
 395                                 /* Reset the dataflash buffer counter, increment the page counter */ 
 396                                 CurrDFPageByteDiv16 
= 0; 
 399                                 /* Select the next dataflash chip based on the new dataflash page index */ 
 400                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 402                                 /* Send the dataflash main memory page read command */ 
 403                                 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 404                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 405                                 Dataflash_SendByte(0x00); 
 406                                 Dataflash_SendByte(0x00); 
 407                                 Dataflash_SendByte(0x00); 
 408                                 Dataflash_SendByte(0x00); 
 411                         /* Read one 16-byte chunk of data from the dataflash */ 
 412                         for (uint8_t ByteNum 
= 0; ByteNum 
< 16; ByteNum
++) 
 413                           *(BufferPtr
++) = Dataflash_ReceiveByte(); 
 415                         /* Increment the dataflash page 16 byte block counter */ 
 416                         CurrDFPageByteDiv16
++; 
 418                         /* Increment the block 16 byte block counter */ 
 422                 /* Decrement the blocks remaining counter */ 
 426         /* Deselect all dataflash chips */ 
 427         Dataflash_DeselectChip(); 
 430 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */ 
 431 void DataflashManager_ResetDataflashProtections(void) 
 433         /* Select first dataflash chip, send the read status register command */ 
 434         Dataflash_SelectChip(DATAFLASH_CHIP1
); 
 435         Dataflash_SendByte(DF_CMD_GETSTATUS
); 
 437         /* Check if sector protection is enabled */ 
 438         if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
) 
 440                 Dataflash_ToggleSelectedChipCS(); 
 442                 /* Send the commands to disable sector protection */ 
 443                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]); 
 444                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]); 
 445                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]); 
 446                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]); 
 449         /* Select second dataflash chip (if present on selected board), send read status register command */ 
 450         #if (DATAFLASH_TOTALCHIPS == 2) 
 451         Dataflash_SelectChip(DATAFLASH_CHIP2
); 
 452         Dataflash_SendByte(DF_CMD_GETSTATUS
); 
 454         /* Check if sector protection is enabled */ 
 455         if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
) 
 457                 Dataflash_ToggleSelectedChipCS(); 
 459                 /* Send the commands to disable sector protection */ 
 460                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]); 
 461                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]); 
 462                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]); 
 463                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]); 
 467         /* Deselect current dataflash chip */ 
 468         Dataflash_DeselectChip();