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 #if defined(USB_CAN_BE_DEVICE) 
  43 /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board dataflash IC(s), from 
  44  *  the pre-selected data OUT endpoint. This routine reads in OS sized blocks from the endpoint and writes 
  45  *  them to the dataflash in Dataflash page sized blocks. 
  47  *  \param[in] MSInterfaceInfo  Pointer to a structure containing a Mass Storage Class configuration and state 
  48  *  \param[in] BlockAddress  Data block starting address for the write sequence 
  49  *  \param[in] TotalBlocks   Number of blocks of data to write 
  51 void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t
* MSInterfaceInfo
, const uint32_t BlockAddress
, uint16_t TotalBlocks
) 
  53         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
  54         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
  55         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
  56         bool     UsingSecondBuffer   
= false; 
  58         /* Select the correct starting Dataflash IC for the block requested */ 
  59         Dataflash_SelectChipFromPage(CurrDFPage
); 
  61 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) 
  62         /* Copy selected dataflash's current page contents to the dataflash buffer */ 
  63         Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
); 
  64         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
  65         Dataflash_WaitWhileBusy(); 
  68         /* Send the dataflash buffer write command */ 
  69         Dataflash_SendByte(DF_CMD_BUFF1WRITE
); 
  70         Dataflash_SendAddressBytes(0, CurrDFPageByte
); 
  72         /* Wait until endpoint is ready before continuing */ 
  73         if (Endpoint_WaitUntilReady()) 
  78                 uint8_t BytesInBlockDiv16 
= 0; 
  80                 /* Write an endpoint packet sized data block to the dataflash */ 
  81                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
  83                         /* Check if the endpoint is currently empty */ 
  84                         if (!(Endpoint_IsReadWriteAllowed())) 
  86                                 /* Clear the current endpoint bank */ 
  89                                 /* Wait until the host has sent another packet */ 
  90                                 if (Endpoint_WaitUntilReady()) 
  94                         /* Check if end of dataflash page reached */ 
  95                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
  97                                 /* Write the dataflash buffer contents back to the dataflash page */ 
  98                                 Dataflash_WaitWhileBusy(); 
  99                                 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE 
: DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
 100                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 102                                 /* Reset the dataflash buffer counter, increment the page counter */ 
 103                                 CurrDFPageByteDiv16 
= 0; 
 106                                 /* Once all the dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */ 
 107                                 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS
)) 
 108                                   UsingSecondBuffer 
= !(UsingSecondBuffer
); 
 110                                 /* Select the next dataflash chip based on the new dataflash page index */ 
 111                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 113 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) 
 114                                 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */ 
 115                                 if ((TotalBlocks 
* (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) < (DATAFLASH_PAGE_SIZE 
>> 4)) 
 117                                         /* Copy selected dataflash's current page contents to the dataflash buffer */ 
 118                                         Dataflash_WaitWhileBusy(); 
 119                                         Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2 
: DF_CMD_MAINMEMTOBUFF1
); 
 120                                         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 121                                         Dataflash_WaitWhileBusy(); 
 125                                 /* Send the dataflash buffer write command */ 
 126                                 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE 
: DF_CMD_BUFF1WRITE
); 
 127                                 Dataflash_SendAddressBytes(0, 0);                                
 130                         /* Write one 16-byte chunk of data to the dataflash */ 
 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()); 
 137                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 138                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 139                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 140                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 141                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 142                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 143                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 144                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 145                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 146                         Dataflash_SendByte(Endpoint_Read_Byte()); 
 148                         /* Increment the dataflash page 16 byte block counter */ 
 149                         CurrDFPageByteDiv16
++; 
 151                         /* Increment the block 16 byte block counter */ 
 154                         /* Check if the current command is being aborted by the host */ 
 155                         if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 159                 /* Decrement the blocks remaining counter and reset the sub block counter */ 
 163         /* Write the dataflash buffer contents back to the dataflash page */ 
 164         Dataflash_WaitWhileBusy(); 
 165         Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE 
: DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
 166         Dataflash_SendAddressBytes(CurrDFPage
, 0x00); 
 167         Dataflash_WaitWhileBusy(); 
 169         /* If the endpoint is empty, clear it ready for the next packet from the host */ 
 170         if (!(Endpoint_IsReadWriteAllowed())) 
 173         /* Deselect all dataflash chips */ 
 174         Dataflash_DeselectChip(); 
 177 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into 
 178  *  the pre-selected data IN endpoint. This routine reads in Dataflash page sized blocks from the Dataflash 
 179  *  and writes them in OS sized blocks to the endpoint. 
 181  *  \param[in] MSInterfaceInfo  Pointer to a structure containing a Mass Storage Class configuration and state 
 182  *  \param[in] BlockAddress  Data block starting address for the read sequence 
 183  *  \param[in] TotalBlocks   Number of blocks of data to read 
 185 void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t
* MSInterfaceInfo
, const uint32_t BlockAddress
, uint16_t TotalBlocks
) 
 187         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
 188         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
 189         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
 191         /* Select the correct starting Dataflash IC for the block requested */ 
 192         Dataflash_SelectChipFromPage(CurrDFPage
); 
 194         /* Send the dataflash main memory page read command */ 
 195         Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 196         Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
); 
 197         Dataflash_SendByte(0x00); 
 198         Dataflash_SendByte(0x00); 
 199         Dataflash_SendByte(0x00); 
 200         Dataflash_SendByte(0x00); 
 202         /* Wait until endpoint is ready before continuing */ 
 203         if (Endpoint_WaitUntilReady()) 
 208                 uint8_t BytesInBlockDiv16 
= 0; 
 210                 /* Write an endpoint packet sized data block to the dataflash */ 
 211                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
 213                         /* Check if the endpoint is currently full */ 
 214                         if (!(Endpoint_IsReadWriteAllowed())) 
 216                                 /* Clear the endpoint bank to send its contents to the host */ 
 219                                 /* Wait until the endpoint is ready for more data */ 
 220                                 if (Endpoint_WaitUntilReady()) 
 224                         /* Check if end of dataflash page reached */ 
 225                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
 227                                 /* Reset the dataflash buffer counter, increment the page counter */ 
 228                                 CurrDFPageByteDiv16 
= 0; 
 231                                 /* Select the next dataflash chip based on the new dataflash page index */ 
 232                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 234                                 /* Send the dataflash main memory page read command */ 
 235                                 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 236                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 237                                 Dataflash_SendByte(0x00); 
 238                                 Dataflash_SendByte(0x00); 
 239                                 Dataflash_SendByte(0x00); 
 240                                 Dataflash_SendByte(0x00); 
 243                         /* Read one 16-byte chunk of data from the dataflash */ 
 244                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 245                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 246                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 247                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 248                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 249                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 250                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 251                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 252                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 253                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 254                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 255                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 256                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 257                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 258                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 259                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 261                         /* Increment the dataflash page 16 byte block counter */ 
 262                         CurrDFPageByteDiv16
++; 
 264                         /* Increment the block 16 byte block counter */ 
 267                         /* Check if the current command is being aborted by the host */ 
 268                         if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 272                 /* Decrement the blocks remaining counter */ 
 276         /* If the endpoint is full, send its contents to the host */ 
 277         if (!(Endpoint_IsReadWriteAllowed())) 
 280         /* Deselect all dataflash chips */ 
 281         Dataflash_DeselectChip(); 
 284 /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board dataflash IC(s), from 
 285  *  the a given RAM buffer. This routine reads in OS sized blocks from the buffer and writes them to the 
 286  *  dataflash in Dataflash page sized blocks. This can be linked to FAT libraries to write files to the 
 289  *  \param[in] BlockAddress  Data block starting address for the write sequence 
 290  *  \param[in] TotalBlocks   Number of blocks of data to write 
 291  *  \param[in] BufferPtr     Pointer to the data source RAM buffer 
 293 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
) 
 295         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
 296         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
 297         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
 298         bool     UsingSecondBuffer   
= false; 
 300         /* Select the correct starting Dataflash IC for the block requested */ 
 301         Dataflash_SelectChipFromPage(CurrDFPage
); 
 303 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) 
 304         /* Copy selected dataflash's current page contents to the dataflash buffer */ 
 305         Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
); 
 306         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 307         Dataflash_WaitWhileBusy(); 
 310         /* Send the dataflash buffer write command */ 
 311         Dataflash_SendByte(DF_CMD_BUFF1WRITE
); 
 312         Dataflash_SendAddressBytes(0, CurrDFPageByte
); 
 316                 uint8_t BytesInBlockDiv16 
= 0; 
 318                 /* Write an endpoint packet sized data block to the dataflash */ 
 319                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
 321                         /* Check if end of dataflash page reached */ 
 322                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
 324                                 /* Write the dataflash buffer contents back to the dataflash page */ 
 325                                 Dataflash_WaitWhileBusy(); 
 326                                 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE 
: DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
 327                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 329                                 /* Reset the dataflash buffer counter, increment the page counter */ 
 330                                 CurrDFPageByteDiv16 
= 0; 
 333                                 /* Once all the dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */ 
 334                                 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS
)) 
 335                                   UsingSecondBuffer 
= !(UsingSecondBuffer
); 
 337                                 /* Select the next dataflash chip based on the new dataflash page index */ 
 338                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 340 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) 
 341                                 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */ 
 342                                 if ((TotalBlocks 
* (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) < (DATAFLASH_PAGE_SIZE 
>> 4)) 
 344                                         /* Copy selected dataflash's current page contents to the dataflash buffer */ 
 345                                         Dataflash_WaitWhileBusy(); 
 346                                         Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2 
: DF_CMD_MAINMEMTOBUFF1
); 
 347                                         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 348                                         Dataflash_WaitWhileBusy(); 
 352                                 /* Send the dataflash buffer write command */ 
 353                                 Dataflash_ToggleSelectedChipCS(); 
 354                                 Dataflash_SendByte(DF_CMD_BUFF1WRITE
); 
 355                                 Dataflash_SendAddressBytes(0, 0); 
 358                         /* Write one 16-byte chunk of data to the dataflash */ 
 359                         for (uint8_t ByteNum 
= 0; ByteNum 
< 16; ByteNum
++) 
 360                           Dataflash_SendByte(*(BufferPtr
++)); 
 362                         /* Increment the dataflash page 16 byte block counter */ 
 363                         CurrDFPageByteDiv16
++; 
 365                         /* Increment the block 16 byte block counter */ 
 369                 /* Decrement the blocks remaining counter and reset the sub block counter */ 
 373         /* Write the dataflash buffer contents back to the dataflash page */ 
 374         Dataflash_WaitWhileBusy(); 
 375         Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE 
: DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
 376         Dataflash_SendAddressBytes(CurrDFPage
, 0x00); 
 377         Dataflash_WaitWhileBusy(); 
 379         /* Deselect all dataflash chips */ 
 380         Dataflash_DeselectChip(); 
 383 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into 
 384  *  the a preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash 
 385  *  and writes them in OS sized blocks to the given buffer. This can be linked to FAT libraries to read 
 386  *  the files stored on the dataflash. 
 388  *  \param[in] BlockAddress  Data block starting address for the read sequence 
 389  *  \param[in] TotalBlocks   Number of blocks of data to read 
 390  *  \param[out] BufferPtr    Pointer to the data destination RAM buffer 
 392 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
) 
 394         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
 395         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
 396         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
 398         /* Select the correct starting Dataflash IC for the block requested */ 
 399         Dataflash_SelectChipFromPage(CurrDFPage
); 
 401         /* Send the dataflash main memory page read command */ 
 402         Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 403         Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
); 
 404         Dataflash_SendByte(0x00); 
 405         Dataflash_SendByte(0x00); 
 406         Dataflash_SendByte(0x00); 
 407         Dataflash_SendByte(0x00); 
 411                 uint8_t BytesInBlockDiv16 
= 0; 
 413                 /* Write an endpoint packet sized data block to the dataflash */ 
 414                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
 416                         /* Check if end of dataflash page reached */ 
 417                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
 419                                 /* Reset the dataflash buffer counter, increment the page counter */ 
 420                                 CurrDFPageByteDiv16 
= 0; 
 423                                 /* Select the next dataflash chip based on the new dataflash page index */ 
 424                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 426                                 /* Send the dataflash main memory page read command */ 
 427                                 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 428                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 429                                 Dataflash_SendByte(0x00); 
 430                                 Dataflash_SendByte(0x00); 
 431                                 Dataflash_SendByte(0x00); 
 432                                 Dataflash_SendByte(0x00); 
 435                         /* Read one 16-byte chunk of data from the dataflash */ 
 436                         for (uint8_t ByteNum 
= 0; ByteNum 
< 16; ByteNum
++) 
 437                           *(BufferPtr
++) = Dataflash_ReceiveByte(); 
 439                         /* Increment the dataflash page 16 byte block counter */ 
 440                         CurrDFPageByteDiv16
++; 
 442                         /* Increment the block 16 byte block counter */ 
 446                 /* Decrement the blocks remaining counter */ 
 450         /* Deselect all dataflash chips */ 
 451         Dataflash_DeselectChip(); 
 454 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */ 
 455 void DataflashManager_ResetDataflashProtections(void) 
 457         /* Select first dataflash chip, send the read status register command */ 
 458         Dataflash_SelectChip(DATAFLASH_CHIP1
); 
 459         Dataflash_SendByte(DF_CMD_GETSTATUS
); 
 461         /* Check if sector protection is enabled */ 
 462         if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
) 
 464                 Dataflash_ToggleSelectedChipCS(); 
 466                 /* Send the commands to disable sector protection */ 
 467                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]); 
 468                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]); 
 469                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]); 
 470                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]); 
 473         /* Select second dataflash chip (if present on selected board), send read status register command */ 
 474         #if (DATAFLASH_TOTALCHIPS == 2) 
 475         Dataflash_SelectChip(DATAFLASH_CHIP2
); 
 476         Dataflash_SendByte(DF_CMD_GETSTATUS
); 
 478         /* Check if sector protection is enabled */ 
 479         if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
) 
 481                 Dataflash_ToggleSelectedChipCS(); 
 483                 /* Send the commands to disable sector protection */ 
 484                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]); 
 485                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]); 
 486                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]); 
 487                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]); 
 491         /* Deselect current dataflash chip */ 
 492         Dataflash_DeselectChip(); 
 495 /** Performs a simple test on the attached Dataflash IC(s) to ensure that they are working. 
 497  *  \return Boolean true if all media chips are working, false otherwise 
 499 bool DataflashManager_CheckDataflashOperation(void) 
 503         /* Test first Dataflash IC is present and responding to commands */ 
 504         Dataflash_SelectChip(DATAFLASH_CHIP1
); 
 505         Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO
); 
 506         ReturnByte 
= Dataflash_ReceiveByte(); 
 507         Dataflash_DeselectChip(); 
 509         /* If returned data is invalid, fail the command */ 
 510         if (ReturnByte 
!= DF_MANUFACTURER_ATMEL
) 
 513         #if (DATAFLASH_TOTALCHIPS == 2) 
 514         /* Test second Dataflash IC is present and responding to commands */ 
 515         Dataflash_SelectChip(DATAFLASH_CHIP2
); 
 516         Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO
); 
 517         ReturnByte 
= Dataflash_ReceiveByte(); 
 518         Dataflash_DeselectChip(); 
 520         /* If returned data is invalid, fail the command */ 
 521         if (ReturnByte 
!= DF_MANUFACTURER_ATMEL
)