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[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
* MSInterfaceInfo
, const uint32_t BlockAddress
, uint16_t TotalBlocks
) 
  52         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
  53         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
  54         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
  55         bool     UsingSecondBuffer   
= false; 
  57         /* Select the correct starting Dataflash IC for the block requested */ 
  58         Dataflash_SelectChipFromPage(CurrDFPage
); 
  60 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) 
  61         /* Copy selected dataflash's current page contents to the dataflash buffer */ 
  62         Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
); 
  63         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
  64         Dataflash_WaitWhileBusy(); 
  67         /* Send the dataflash buffer write command */ 
  68         Dataflash_SendByte(DF_CMD_BUFF1WRITE
); 
  69         Dataflash_SendAddressBytes(0, CurrDFPageByte
); 
  71         /* Wait until endpoint is ready before continuing */ 
  72         if (Endpoint_WaitUntilReady()) 
  77                 uint8_t BytesInBlockDiv16 
= 0; 
  79                 /* Write an endpoint packet sized data block to the dataflash */ 
  80                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
  82                         /* Check if the endpoint is currently empty */ 
  83                         if (!(Endpoint_IsReadWriteAllowed())) 
  85                                 /* Clear the current endpoint bank */ 
  88                                 /* Wait until the host has sent another packet */ 
  89                                 if (Endpoint_WaitUntilReady()) 
  93                         /* Check if end of dataflash page reached */ 
  94                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
  96                                 /* Write the dataflash buffer contents back to the dataflash page */ 
  97                                 Dataflash_WaitWhileBusy(); 
  98                                 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE 
: DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
  99                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 101                                 /* Reset the dataflash buffer counter, increment the page counter */ 
 102                                 CurrDFPageByteDiv16 
= 0; 
 105                                 /* Once all the dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */ 
 106                                 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS
)) 
 107                                   UsingSecondBuffer 
= !(UsingSecondBuffer
); 
 109                                 /* Select the next dataflash chip based on the new dataflash page index */ 
 110                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 112 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) 
 113                                 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */ 
 114                                 if ((TotalBlocks 
* (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) < (DATAFLASH_PAGE_SIZE 
>> 4)) 
 116                                         /* Copy selected dataflash's current page contents to the dataflash buffer */ 
 117                                         Dataflash_WaitWhileBusy(); 
 118                                         Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2 
: DF_CMD_MAINMEMTOBUFF1
); 
 119                                         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 120                                         Dataflash_WaitWhileBusy(); 
 124                                 /* Send the dataflash buffer write command */ 
 125                                 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE 
: DF_CMD_BUFF1WRITE
); 
 126                                 Dataflash_SendAddressBytes(0, 0);                                
 129                         /* Write one 16-byte chunk of data to the dataflash */ 
 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()); 
 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()); 
 147                         /* Increment the dataflash page 16 byte block counter */ 
 148                         CurrDFPageByteDiv16
++; 
 150                         /* Increment the block 16 byte block counter */ 
 153                         /* Check if the current command is being aborted by the host */ 
 154                         if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 158                 /* Decrement the blocks remaining counter and reset the sub block counter */ 
 162         /* Write the dataflash buffer contents back to the dataflash page */ 
 163         Dataflash_WaitWhileBusy(); 
 164         Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE 
: DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
 165         Dataflash_SendAddressBytes(CurrDFPage
, 0x00); 
 166         Dataflash_WaitWhileBusy(); 
 168         /* If the endpoint is empty, clear it ready for the next packet from the host */ 
 169         if (!(Endpoint_IsReadWriteAllowed())) 
 172         /* Deselect all dataflash chips */ 
 173         Dataflash_DeselectChip(); 
 176 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into 
 177  *  the pre-selected data IN endpoint. This routine reads in Dataflash page sized blocks from the Dataflash 
 178  *  and writes them in OS sized blocks to the endpoint. 
 180  *  \param[in] MSInterfaceInfo  Pointer to a structure containing a Mass Storage Class configuration and state. 
 181  *  \param[in] BlockAddress  Data block starting address for the read sequence 
 182  *  \param[in] TotalBlocks   Number of blocks of data to read 
 184 void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t
* MSInterfaceInfo
, const uint32_t BlockAddress
, uint16_t TotalBlocks
) 
 186         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
 187         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
 188         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
 190         /* Select the correct starting Dataflash IC for the block requested */ 
 191         Dataflash_SelectChipFromPage(CurrDFPage
); 
 193         /* Send the dataflash main memory page read command */ 
 194         Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 195         Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
); 
 196         Dataflash_SendByte(0x00); 
 197         Dataflash_SendByte(0x00); 
 198         Dataflash_SendByte(0x00); 
 199         Dataflash_SendByte(0x00); 
 201         /* Wait until endpoint is ready before continuing */ 
 202         if (Endpoint_WaitUntilReady()) 
 207                 uint8_t BytesInBlockDiv16 
= 0; 
 209                 /* Write an endpoint packet sized data block to the dataflash */ 
 210                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
 212                         /* Check if the endpoint is currently full */ 
 213                         if (!(Endpoint_IsReadWriteAllowed())) 
 215                                 /* Clear the endpoint bank to send its contents to the host */ 
 218                                 /* Wait until the endpoint is ready for more data */ 
 219                                 if (Endpoint_WaitUntilReady()) 
 223                         /* Check if end of dataflash page reached */ 
 224                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
 226                                 /* Reset the dataflash buffer counter, increment the page counter */ 
 227                                 CurrDFPageByteDiv16 
= 0; 
 230                                 /* Select the next dataflash chip based on the new dataflash page index */ 
 231                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 233                                 /* Send the dataflash main memory page read command */ 
 234                                 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 235                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 236                                 Dataflash_SendByte(0x00); 
 237                                 Dataflash_SendByte(0x00); 
 238                                 Dataflash_SendByte(0x00); 
 239                                 Dataflash_SendByte(0x00); 
 242                         /* Read one 16-byte chunk of data from the dataflash */ 
 243                         Endpoint_Write_Byte(Dataflash_ReceiveByte()); 
 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()); 
 260                         /* Increment the dataflash page 16 byte block counter */ 
 261                         CurrDFPageByteDiv16
++; 
 263                         /* Increment the block 16 byte block counter */ 
 266                         /* Check if the current command is being aborted by the host */ 
 267                         if (MSInterfaceInfo
->State
.IsMassStoreReset
) 
 271                 /* Decrement the blocks remaining counter */ 
 275         /* If the endpoint is full, send its contents to the host */ 
 276         if (!(Endpoint_IsReadWriteAllowed())) 
 279         /* Deselect all dataflash chips */ 
 280         Dataflash_DeselectChip(); 
 283 /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board dataflash IC(s), from 
 284  *  the a given RAM buffer. This routine reads in OS sized blocks from the buffer and writes them to the 
 285  *  dataflash in Dataflash page sized blocks. This can be linked to FAT libraries to write files to the 
 288  *  \param[in] BlockAddress  Data block starting address for the write sequence 
 289  *  \param[in] TotalBlocks   Number of blocks of data to write 
 290  *  \param[in] BufferPtr     Pointer to the data source RAM buffer 
 292 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
) 
 294         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
 295         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
 296         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
 297         bool     UsingSecondBuffer   
= false; 
 299         /* Select the correct starting Dataflash IC for the block requested */ 
 300         Dataflash_SelectChipFromPage(CurrDFPage
); 
 302 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) 
 303         /* Copy selected dataflash's current page contents to the dataflash buffer */ 
 304         Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
); 
 305         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 306         Dataflash_WaitWhileBusy(); 
 309         /* Send the dataflash buffer write command */ 
 310         Dataflash_SendByte(DF_CMD_BUFF1WRITE
); 
 311         Dataflash_SendAddressBytes(0, CurrDFPageByte
); 
 315                 uint8_t BytesInBlockDiv16 
= 0; 
 317                 /* Write an endpoint packet sized data block to the dataflash */ 
 318                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
 320                         /* Check if end of dataflash page reached */ 
 321                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
 323                                 /* Write the dataflash buffer contents back to the dataflash page */ 
 324                                 Dataflash_WaitWhileBusy(); 
 325                                 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE 
: DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
 326                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 328                                 /* Reset the dataflash buffer counter, increment the page counter */ 
 329                                 CurrDFPageByteDiv16 
= 0; 
 332                                 /* Once all the dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */ 
 333                                 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS
)) 
 334                                   UsingSecondBuffer 
= !(UsingSecondBuffer
); 
 336                                 /* Select the next dataflash chip based on the new dataflash page index */ 
 337                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 339 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) 
 340                                 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */ 
 341                                 if ((TotalBlocks 
* (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) < (DATAFLASH_PAGE_SIZE 
>> 4)) 
 343                                         /* Copy selected dataflash's current page contents to the dataflash buffer */ 
 344                                         Dataflash_WaitWhileBusy(); 
 345                                         Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2 
: DF_CMD_MAINMEMTOBUFF1
); 
 346                                         Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 347                                         Dataflash_WaitWhileBusy(); 
 351                                 /* Send the dataflash buffer write command */ 
 352                                 Dataflash_ToggleSelectedChipCS(); 
 353                                 Dataflash_SendByte(DF_CMD_BUFF1WRITE
); 
 354                                 Dataflash_SendAddressBytes(0, 0); 
 357                         /* Write one 16-byte chunk of data to the dataflash */ 
 358                         for (uint8_t ByteNum 
= 0; ByteNum 
< 16; ByteNum
++) 
 359                           Dataflash_SendByte(*(BufferPtr
++)); 
 361                         /* Increment the dataflash page 16 byte block counter */ 
 362                         CurrDFPageByteDiv16
++; 
 364                         /* Increment the block 16 byte block counter */ 
 368                 /* Decrement the blocks remaining counter and reset the sub block counter */ 
 372         /* Write the dataflash buffer contents back to the dataflash page */ 
 373         Dataflash_WaitWhileBusy(); 
 374         Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE 
: DF_CMD_BUFF1TOMAINMEMWITHERASE
); 
 375         Dataflash_SendAddressBytes(CurrDFPage
, 0x00); 
 376         Dataflash_WaitWhileBusy(); 
 378         /* Deselect all dataflash chips */ 
 379         Dataflash_DeselectChip(); 
 382 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into 
 383  *  the a preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash 
 384  *  and writes them in OS sized blocks to the given buffer. This can be linked to FAT libraries to read 
 385  *  the files stored on the dataflash. 
 387  *  \param[in] BlockAddress  Data block starting address for the read sequence 
 388  *  \param[in] TotalBlocks   Number of blocks of data to read 
 389  *  \param[out] BufferPtr    Pointer to the data destination RAM buffer 
 391 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
) 
 393         uint16_t CurrDFPage          
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
); 
 394         uint16_t CurrDFPageByte      
= ((BlockAddress 
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
); 
 395         uint8_t  CurrDFPageByteDiv16 
= (CurrDFPageByte 
>> 4); 
 397         /* Select the correct starting Dataflash IC for the block requested */ 
 398         Dataflash_SelectChipFromPage(CurrDFPage
); 
 400         /* Send the dataflash main memory page read command */ 
 401         Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 402         Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
); 
 403         Dataflash_SendByte(0x00); 
 404         Dataflash_SendByte(0x00); 
 405         Dataflash_SendByte(0x00); 
 406         Dataflash_SendByte(0x00); 
 410                 uint8_t BytesInBlockDiv16 
= 0; 
 412                 /* Write an endpoint packet sized data block to the dataflash */ 
 413                 while (BytesInBlockDiv16 
< (VIRTUAL_MEMORY_BLOCK_SIZE 
>> 4)) 
 415                         /* Check if end of dataflash page reached */ 
 416                         if (CurrDFPageByteDiv16 
== (DATAFLASH_PAGE_SIZE 
>> 4)) 
 418                                 /* Reset the dataflash buffer counter, increment the page counter */ 
 419                                 CurrDFPageByteDiv16 
= 0; 
 422                                 /* Select the next dataflash chip based on the new dataflash page index */ 
 423                                 Dataflash_SelectChipFromPage(CurrDFPage
); 
 425                                 /* Send the dataflash main memory page read command */ 
 426                                 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
); 
 427                                 Dataflash_SendAddressBytes(CurrDFPage
, 0); 
 428                                 Dataflash_SendByte(0x00); 
 429                                 Dataflash_SendByte(0x00); 
 430                                 Dataflash_SendByte(0x00); 
 431                                 Dataflash_SendByte(0x00); 
 434                         /* Read one 16-byte chunk of data from the dataflash */ 
 435                         for (uint8_t ByteNum 
= 0; ByteNum 
< 16; ByteNum
++) 
 436                           *(BufferPtr
++) = Dataflash_ReceiveByte(); 
 438                         /* Increment the dataflash page 16 byte block counter */ 
 439                         CurrDFPageByteDiv16
++; 
 441                         /* Increment the block 16 byte block counter */ 
 445                 /* Decrement the blocks remaining counter */ 
 449         /* Deselect all dataflash chips */ 
 450         Dataflash_DeselectChip(); 
 453 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */ 
 454 void DataflashManager_ResetDataflashProtections(void) 
 456         /* Select first dataflash chip, send the read status register command */ 
 457         Dataflash_SelectChip(DATAFLASH_CHIP1
); 
 458         Dataflash_SendByte(DF_CMD_GETSTATUS
); 
 460         /* Check if sector protection is enabled */ 
 461         if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
) 
 463                 Dataflash_ToggleSelectedChipCS(); 
 465                 /* Send the commands to disable sector protection */ 
 466                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]); 
 467                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]); 
 468                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]); 
 469                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]); 
 472         /* Select second dataflash chip (if present on selected board), send read status register command */ 
 473         #if (DATAFLASH_TOTALCHIPS == 2) 
 474         Dataflash_SelectChip(DATAFLASH_CHIP2
); 
 475         Dataflash_SendByte(DF_CMD_GETSTATUS
); 
 477         /* Check if sector protection is enabled */ 
 478         if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
) 
 480                 Dataflash_ToggleSelectedChipCS(); 
 482                 /* Send the commands to disable sector protection */ 
 483                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]); 
 484                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]); 
 485                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]); 
 486                 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]); 
 490         /* Deselect current dataflash chip */ 
 491         Dataflash_DeselectChip();