3      Copyright (C) Dean Camera, 2016. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2016  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, distribute, and sell this 
  13   software and its documentation for any purpose is hereby granted 
  14   without fee, provided that the above copyright notice appear in 
  15   all 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 disclaims 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[in] MSInterfaceInfo  Pointer to a structure containing a Mass Storage Class configuration and state 
  47  *  \param[in] BlockAddress  Data block starting address for the write sequence 
  48  *  \param[in] TotalBlocks   Number of blocks of data to write 
  50 void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
, 
  51                                   const uint32_t BlockAddress
, 
  54         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
  55         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
  56         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
  57         bool     UsingSecondBuffer   
= false; 
  59         /* Select the correct starting Dataflash IC for the block requested */ 
  60         Dataflash_SelectChipFromPage(CurrDFPage
); 
  62 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) 
  63         /* Copy selected dataflash's current page contents to the Dataflash buffer */ 
  64         Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
); 
  65         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
  66         Dataflash_WaitWhileBusy(); 
  69         /* Send the Dataflash buffer write command */ 
  70         Dataflash_SendByte(DF_CMD_BUFF1WRITE
); 
  71         Dataflash_SendAddressBytes(0, CurrDFPageByte
); 
  73         /* Wait until endpoint is ready before continuing */ 
  74         if (Endpoint_WaitUntilReady()) 
  79                 uint8_t BytesInBlockDiv16 
= 0; 
  81                 /* Write an endpoint packet sized data block to the Dataflash */ 
  82                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
  84                         /* Check if the endpoint is currently empty */ 
  85                         if (!(Endpoint_IsReadWriteAllowed())) 
  87                                 /* Clear the current endpoint bank */ 
  90                                 /* Wait until the host has sent another packet */ 
  91                                 if (Endpoint_WaitUntilReady()) 
  95                         /* Check if end of Dataflash page reached */ 
  96                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
  98                                 /* Write the Dataflash buffer contents back to the Dataflash page */ 
  99                                 Dataflash_WaitWhileBusy(); 
 100                                 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE 
: DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
 101                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 103                                 /* Reset the Dataflash buffer counter, increment the page counter */ 
 104                                 CurrDFPageByteDiv16 
= 0; 
 107                                 /* Once all the Dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */ 
 108                                 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS
)) 
 109                                   UsingSecondBuffer 
= !(UsingSecondBuffer
); 
 111                                 /* Select the next Dataflash chip based on the new Dataflash page index */ 
 112                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 114 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) 
 115                                 /* If less than one Dataflash page remaining, copy over the existing page to preserve trailing data */ 
 116                                 if ((TotalBlocks 
* (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) < (DATAFLASH_PAGE_SIZE 
>> 4)) 
 118                                         /* Copy selected dataflash's current page contents to the Dataflash buffer */ 
 119                                         Dataflash_WaitWhileBusy(); 
 120                                         Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2 
: DF_CMD_MAINMEMTOBUFF1
); 
 121                                         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 122                                         Dataflash_WaitWhileBusy(); 
 126                                 /* Send the Dataflash buffer write command */ 
 127                                 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE 
: DF_CMD_BUFF1WRITE
); 
 128                                 Dataflash_SendAddressBytes(0, 0); 
 131                         /* Write one 16-byte chunk of data to the Dataflash */ 
 132                         Dataflash_SendByte(Endpoint_Read_8()); 
 133                         Dataflash_SendByte(Endpoint_Read_8()); 
 134                         Dataflash_SendByte(Endpoint_Read_8()); 
 135                         Dataflash_SendByte(Endpoint_Read_8()); 
 136                         Dataflash_SendByte(Endpoint_Read_8()); 
 137                         Dataflash_SendByte(Endpoint_Read_8()); 
 138                         Dataflash_SendByte(Endpoint_Read_8()); 
 139                         Dataflash_SendByte(Endpoint_Read_8()); 
 140                         Dataflash_SendByte(Endpoint_Read_8()); 
 141                         Dataflash_SendByte(Endpoint_Read_8()); 
 142                         Dataflash_SendByte(Endpoint_Read_8()); 
 143                         Dataflash_SendByte(Endpoint_Read_8()); 
 144                         Dataflash_SendByte(Endpoint_Read_8()); 
 145                         Dataflash_SendByte(Endpoint_Read_8()); 
 146                         Dataflash_SendByte(Endpoint_Read_8()); 
 147                         Dataflash_SendByte(Endpoint_Read_8()); 
 149                         /* Increment the Dataflash page 16 byte block counter */ 
 150                         CurrDFPageByteDiv16
++; 
 152                         /* Increment the block 16 byte block counter */ 
 155                         /* Check if the current command is being aborted by the host */ 
 156                         if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 160                 /* Decrement the blocks remaining counter */ 
 164         /* Write the Dataflash buffer contents back to the Dataflash page */ 
 165         Dataflash_WaitWhileBusy(); 
 166         Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE 
: DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
 167         Dataflash_SendAddressBytes(CurrDFPage
, 0x00); 
 168         Dataflash_WaitWhileBusy(); 
 170         /* If the endpoint is empty, clear it ready for the next packet from the host */ 
 171         if (!(Endpoint_IsReadWriteAllowed())) 
 174         /* Deselect all Dataflash chips */ 
 175         Dataflash_DeselectChip(); 
 178 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board Dataflash IC(s), into 
 179  *  the pre-selected data IN endpoint. This routine reads in Dataflash page sized blocks from the Dataflash 
 180  *  and writes them in OS sized blocks to the endpoint. 
 182  *  \param[in] MSInterfaceInfo  Pointer to a structure containing a Mass Storage Class configuration and state 
 183  *  \param[in] BlockAddress  Data block starting address for the read sequence 
 184  *  \param[in] TotalBlocks   Number of blocks of data to read 
 186 void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
, 
 187                                  const uint32_t BlockAddress
, 
 188                                  uint16_t TotalBlocks
) 
 190         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
 191         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
 192         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
 194         /* Select the correct starting Dataflash IC for the block requested */ 
 195         Dataflash_SelectChipFromPage(CurrDFPage
); 
 197         /* Send the Dataflash main memory page read command */ 
 198         Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 199         Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
); 
 200         Dataflash_SendByte(0x00); 
 201         Dataflash_SendByte(0x00); 
 202         Dataflash_SendByte(0x00); 
 203         Dataflash_SendByte(0x00); 
 205         /* Wait until endpoint is ready before continuing */ 
 206         if (Endpoint_WaitUntilReady()) 
 211                 uint8_t BytesInBlockDiv16 
= 0; 
 213                 /* Read an endpoint packet sized data block from the Dataflash */ 
 214                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
 216                         /* Check if the endpoint is currently full */ 
 217                         if (!(Endpoint_IsReadWriteAllowed())) 
 219                                 /* Clear the endpoint bank to send its contents to the host */ 
 222                                 /* Wait until the endpoint is ready for more data */ 
 223                                 if (Endpoint_WaitUntilReady()) 
 227                         /* Check if end of Dataflash page reached */ 
 228                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
 230                                 /* Reset the Dataflash buffer counter, increment the page counter */ 
 231                                 CurrDFPageByteDiv16 
= 0; 
 234                                 /* Select the next Dataflash chip based on the new Dataflash page index */ 
 235                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 237                                 /* Send the Dataflash main memory page read command */ 
 238                                 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 239                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 240                                 Dataflash_SendByte(0x00); 
 241                                 Dataflash_SendByte(0x00); 
 242                                 Dataflash_SendByte(0x00); 
 243                                 Dataflash_SendByte(0x00); 
 246                         /* Read one 16-byte chunk of data from the Dataflash */ 
 247                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 248                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 249                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 250                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 251                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 252                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 253                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 254                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 255                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 256                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 257                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 258                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 259                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 260                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 261                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 262                         Endpoint_Write_8(Dataflash_ReceiveByte()); 
 264                         /* Increment the Dataflash page 16 byte block counter */ 
 265                         CurrDFPageByteDiv16
++; 
 267                         /* Increment the block 16 byte block counter */ 
 270                         /* Check if the current command is being aborted by the host */ 
 271                         if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 275                 /* Decrement the blocks remaining counter */ 
 279         /* If the endpoint is full, send its contents to the host */ 
 280         if (!(Endpoint_IsReadWriteAllowed())) 
 283         /* Deselect all Dataflash chips */ 
 284         Dataflash_DeselectChip(); 
 287 /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board Dataflash IC(s), from 
 288  *  the given RAM buffer. This routine reads in OS sized blocks from the buffer and writes them to the 
 289  *  Dataflash in Dataflash page sized blocks. This can be linked to FAT libraries to write files to the 
 292  *  \param[in] BlockAddress  Data block starting address for the write sequence 
 293  *  \param[in] TotalBlocks   Number of blocks of data to write 
 294  *  \param[in] BufferPtr     Pointer to the data source RAM buffer 
 296 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress
, 
 297                                       uint16_t TotalBlocks
, 
 300         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
 301         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
 302         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
 303         bool     UsingSecondBuffer   
= false; 
 305         /* Select the correct starting Dataflash IC for the block requested */ 
 306         Dataflash_SelectChipFromPage(CurrDFPage
); 
 308 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) 
 309         /* Copy selected dataflash's current page contents to the Dataflash buffer */ 
 310         Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
); 
 311         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 312         Dataflash_WaitWhileBusy(); 
 315         /* Send the Dataflash buffer write command */ 
 316         Dataflash_SendByte(DF_CMD_BUFF1WRITE
); 
 317         Dataflash_SendAddressBytes(0, CurrDFPageByte
); 
 321                 uint8_t BytesInBlockDiv16 
= 0; 
 323                 /* Write an endpoint packet sized data block to the Dataflash */ 
 324                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
 326                         /* Check if end of Dataflash page reached */ 
 327                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
 329                                 /* Write the Dataflash buffer contents back to the Dataflash page */ 
 330                                 Dataflash_WaitWhileBusy(); 
 331                                 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE 
: DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
 332                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 334                                 /* Reset the Dataflash buffer counter, increment the page counter */ 
 335                                 CurrDFPageByteDiv16 
= 0; 
 338                                 /* Once all the Dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */ 
 339                                 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS
)) 
 340                                   UsingSecondBuffer 
= !(UsingSecondBuffer
); 
 342                                 /* Select the next Dataflash chip based on the new Dataflash page index */ 
 343                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 345 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) 
 346                                 /* If less than one Dataflash page remaining, copy over the existing page to preserve trailing data */ 
 347                                 if ((TotalBlocks 
* (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) < (DATAFLASH_PAGE_SIZE 
>> 4)) 
 349                                         /* Copy selected dataflash's current page contents to the Dataflash buffer */ 
 350                                         Dataflash_WaitWhileBusy(); 
 351                                         Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2 
: DF_CMD_MAINMEMTOBUFF1
); 
 352                                         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 353                                         Dataflash_WaitWhileBusy(); 
 357                                 /* Send the Dataflash buffer write command */ 
 358                                 Dataflash_ToggleSelectedChipCS(); 
 359                                 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE 
: DF_CMD_BUFF1WRITE
); 
 360                                 Dataflash_SendAddressBytes(0, 0); 
 363                         /* Write one 16-byte chunk of data to the Dataflash */ 
 364                         for (uint8_t ByteNum 
= 0; ByteNum 
< 16; ByteNum
++) 
 365                           Dataflash_SendByte(*(BufferPtr
++)); 
 367                         /* Increment the Dataflash page 16 byte block counter */ 
 368                         CurrDFPageByteDiv16
++; 
 370                         /* Increment the block 16 byte block counter */ 
 374                 /* Decrement the blocks remaining counter */ 
 378         /* Write the Dataflash buffer contents back to the Dataflash page */ 
 379         Dataflash_WaitWhileBusy(); 
 380         Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE 
: DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
 381         Dataflash_SendAddressBytes(CurrDFPage
, 0x00); 
 382         Dataflash_WaitWhileBusy(); 
 384         /* Deselect all Dataflash chips */ 
 385         Dataflash_DeselectChip(); 
 388 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board Dataflash IC(s), into 
 389  *  the preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash 
 390  *  and writes them in OS sized blocks to the given buffer. This can be linked to FAT libraries to read 
 391  *  the files stored on the Dataflash. 
 393  *  \param[in] BlockAddress  Data block starting address for the read sequence 
 394  *  \param[in] TotalBlocks   Number of blocks of data to read 
 395  *  \param[out] BufferPtr    Pointer to the data destination RAM buffer 
 397 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress
, 
 398                                      uint16_t TotalBlocks
, 
 401         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
 402         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
 403         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
 405         /* Select the correct starting Dataflash IC for the block requested */ 
 406         Dataflash_SelectChipFromPage(CurrDFPage
); 
 408         /* Send the Dataflash main memory page read command */ 
 409         Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 410         Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
); 
 411         Dataflash_SendByte(0x00); 
 412         Dataflash_SendByte(0x00); 
 413         Dataflash_SendByte(0x00); 
 414         Dataflash_SendByte(0x00); 
 418                 uint8_t BytesInBlockDiv16 
= 0; 
 420                 /* Read an endpoint packet sized data block from the Dataflash */ 
 421                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
 423                         /* Check if end of Dataflash page reached */ 
 424                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
 426                                 /* Reset the Dataflash buffer counter, increment the page counter */ 
 427                                 CurrDFPageByteDiv16 
= 0; 
 430                                 /* Select the next Dataflash chip based on the new Dataflash page index */ 
 431                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 433                                 /* Send the Dataflash main memory page read command */ 
 434                                 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 435                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 436                                 Dataflash_SendByte(0x00); 
 437                                 Dataflash_SendByte(0x00); 
 438                                 Dataflash_SendByte(0x00); 
 439                                 Dataflash_SendByte(0x00); 
 442                         /* Read one 16-byte chunk of data from the Dataflash */ 
 443                         for (uint8_t ByteNum 
= 0; ByteNum 
< 16; ByteNum
++) 
 444                           *(BufferPtr
++) = Dataflash_ReceiveByte(); 
 446                         /* Increment the Dataflash page 16 byte block counter */ 
 447                         CurrDFPageByteDiv16
++; 
 449                         /* Increment the block 16 byte block counter */ 
 453                 /* Decrement the blocks remaining counter */ 
 457         /* Deselect all Dataflash chips */ 
 458         Dataflash_DeselectChip(); 
 461 /** Disables the Dataflash memory write protection bits on the board Dataflash ICs, if enabled. */ 
 462 void DataflashManager_ResetDataflashProtections(void) 
 464         /* Select first Dataflash chip, send the read status register command */ 
 465         Dataflash_SelectChip(DATAFLASH_CHIP1
); 
 466         Dataflash_SendByte(DF_CMD_GETSTATUS
); 
 468         /* Check if sector protection is enabled */ 
 469         if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
) 
 471                 Dataflash_ToggleSelectedChipCS(); 
 473                 /* Send the commands to disable sector protection */ 
 474                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]); 
 475                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]); 
 476                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]); 
 477                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]); 
 480         /* Select second Dataflash chip (if present on selected board), send read status register command */ 
 481         #if (DATAFLASH_TOTALCHIPS == 2) 
 482         Dataflash_SelectChip(DATAFLASH_CHIP2
); 
 483         Dataflash_SendByte(DF_CMD_GETSTATUS
); 
 485         /* Check if sector protection is enabled */ 
 486         if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
) 
 488                 Dataflash_ToggleSelectedChipCS(); 
 490                 /* Send the commands to disable sector protection */ 
 491                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]); 
 492                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]); 
 493                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]); 
 494                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]); 
 498         /* Deselect current Dataflash chip */ 
 499         Dataflash_DeselectChip(); 
 502 /** Performs a simple test on the attached Dataflash IC(s) to ensure that they are working. 
 504  *  \return Boolean \c true if all media chips are working, \c false otherwise 
 506 bool DataflashManager_CheckDataflashOperation(void) 
 510         /* Test first Dataflash IC is present and responding to commands */ 
 511         Dataflash_SelectChip(DATAFLASH_CHIP1
); 
 512         Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO
); 
 513         ReturnByte 
= Dataflash_ReceiveByte(); 
 514         Dataflash_DeselectChip(); 
 516         /* If returned data is invalid, fail the command */ 
 517         if (ReturnByte 
!= DF_MANUFACTURER_ATMEL
) 
 520         #if (DATAFLASH_TOTALCHIPS == 2) 
 521         /* Test second Dataflash IC is present and responding to commands */ 
 522         Dataflash_SelectChip(DATAFLASH_CHIP2
); 
 523         Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO
); 
 524         ReturnByte 
= Dataflash_ReceiveByte(); 
 525         Dataflash_DeselectChip(); 
 527         /* If returned data is invalid, fail the command */ 
 528         if (ReturnByte 
!= DF_MANUFACTURER_ATMEL
)