]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-git/Demos/Device/ClassDriver/MassStorage/Lib/DataflashManager.c
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[qmk_firmware.git] / protocol / lufa / LUFA-git / Demos / Device / ClassDriver / MassStorage / Lib / DataflashManager.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28   this software.
29 */
30
31 /** \file
32  *
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.
37  */
38
39 #define  INCLUDE_FROM_DATAFLASHMANAGER_C
40 #include "DataflashManager.h"
41
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.
45  *
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
49  */
50 void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
51                                   const uint32_t BlockAddress,
52                                   uint16_t TotalBlocks)
53 {
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;
58
59         /* Select the correct starting Dataflash IC for the block requested */
60         Dataflash_SelectChipFromPage(CurrDFPage);
61
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();
67 #endif
68
69         /* Send the Dataflash buffer write command */
70         Dataflash_SendByte(DF_CMD_BUFF1WRITE);
71         Dataflash_SendAddressBytes(0, CurrDFPageByte);
72
73         /* Wait until endpoint is ready before continuing */
74         if (Endpoint_WaitUntilReady())
75           return;
76
77         while (TotalBlocks)
78         {
79                 uint8_t BytesInBlockDiv16 = 0;
80
81                 /* Write an endpoint packet sized data block to the Dataflash */
82                 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
83                 {
84                         /* Check if the endpoint is currently empty */
85                         if (!(Endpoint_IsReadWriteAllowed()))
86                         {
87                                 /* Clear the current endpoint bank */
88                                 Endpoint_ClearOUT();
89
90                                 /* Wait until the host has sent another packet */
91                                 if (Endpoint_WaitUntilReady())
92                                   return;
93                         }
94
95                         /* Check if end of Dataflash page reached */
96                         if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
97                         {
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);
102
103                                 /* Reset the Dataflash buffer counter, increment the page counter */
104                                 CurrDFPageByteDiv16 = 0;
105                                 CurrDFPage++;
106
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);
110
111                                 /* Select the next Dataflash chip based on the new Dataflash page index */
112                                 Dataflash_SelectChipFromPage(CurrDFPage);
113
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))
117                                 {
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();
123                                 }
124 #endif
125
126                                 /* Send the Dataflash buffer write command */
127                                 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE : DF_CMD_BUFF1WRITE);
128                                 Dataflash_SendAddressBytes(0, 0);
129                         }
130
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());
148
149                         /* Increment the Dataflash page 16 byte block counter */
150                         CurrDFPageByteDiv16++;
151
152                         /* Increment the block 16 byte block counter */
153                         BytesInBlockDiv16++;
154
155                         /* Check if the current command is being aborted by the host */
156                         if (MSInterfaceInfo->State.IsMassStoreReset)
157                           return;
158                 }
159
160                 /* Decrement the blocks remaining counter */
161                 TotalBlocks--;
162         }
163
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();
169
170         /* If the endpoint is empty, clear it ready for the next packet from the host */
171         if (!(Endpoint_IsReadWriteAllowed()))
172           Endpoint_ClearOUT();
173
174         /* Deselect all Dataflash chips */
175         Dataflash_DeselectChip();
176 }
177
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.
181  *
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
185  */
186 void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
187                                  const uint32_t BlockAddress,
188                                  uint16_t TotalBlocks)
189 {
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);
193
194         /* Select the correct starting Dataflash IC for the block requested */
195         Dataflash_SelectChipFromPage(CurrDFPage);
196
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);
204
205         /* Wait until endpoint is ready before continuing */
206         if (Endpoint_WaitUntilReady())
207           return;
208
209         while (TotalBlocks)
210         {
211                 uint8_t BytesInBlockDiv16 = 0;
212
213                 /* Write an endpoint packet sized data block to the Dataflash */
214                 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
215                 {
216                         /* Check if the endpoint is currently full */
217                         if (!(Endpoint_IsReadWriteAllowed()))
218                         {
219                                 /* Clear the endpoint bank to send its contents to the host */
220                                 Endpoint_ClearIN();
221
222                                 /* Wait until the endpoint is ready for more data */
223                                 if (Endpoint_WaitUntilReady())
224                                   return;
225                         }
226
227                         /* Check if end of Dataflash page reached */
228                         if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
229                         {
230                                 /* Reset the Dataflash buffer counter, increment the page counter */
231                                 CurrDFPageByteDiv16 = 0;
232                                 CurrDFPage++;
233
234                                 /* Select the next Dataflash chip based on the new Dataflash page index */
235                                 Dataflash_SelectChipFromPage(CurrDFPage);
236
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);
244                         }
245
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());
263
264                         /* Increment the Dataflash page 16 byte block counter */
265                         CurrDFPageByteDiv16++;
266
267                         /* Increment the block 16 byte block counter */
268                         BytesInBlockDiv16++;
269
270                         /* Check if the current command is being aborted by the host */
271                         if (MSInterfaceInfo->State.IsMassStoreReset)
272                           return;
273                 }
274
275                 /* Decrement the blocks remaining counter */
276                 TotalBlocks--;
277         }
278
279         /* If the endpoint is full, send its contents to the host */
280         if (!(Endpoint_IsReadWriteAllowed()))
281           Endpoint_ClearIN();
282
283         /* Deselect all Dataflash chips */
284         Dataflash_DeselectChip();
285 }
286
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
290  *  Dataflash.
291  *
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
295  */
296 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress,
297                                       uint16_t TotalBlocks,
298                                       uint8_t* BufferPtr)
299 {
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;
304
305         /* Select the correct starting Dataflash IC for the block requested */
306         Dataflash_SelectChipFromPage(CurrDFPage);
307
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();
313 #endif
314
315         /* Send the Dataflash buffer write command */
316         Dataflash_SendByte(DF_CMD_BUFF1WRITE);
317         Dataflash_SendAddressBytes(0, CurrDFPageByte);
318
319         while (TotalBlocks)
320         {
321                 uint8_t BytesInBlockDiv16 = 0;
322
323                 /* Write an endpoint packet sized data block to the Dataflash */
324                 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
325                 {
326                         /* Check if end of Dataflash page reached */
327                         if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
328                         {
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);
333
334                                 /* Reset the Dataflash buffer counter, increment the page counter */
335                                 CurrDFPageByteDiv16 = 0;
336                                 CurrDFPage++;
337
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);
341
342                                 /* Select the next Dataflash chip based on the new Dataflash page index */
343                                 Dataflash_SelectChipFromPage(CurrDFPage);
344
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))
348                                 {
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();
354                                 }
355 #endif
356
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);
361                         }
362
363                         /* Write one 16-byte chunk of data to the Dataflash */
364                         for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
365                           Dataflash_SendByte(*(BufferPtr++));
366
367                         /* Increment the Dataflash page 16 byte block counter */
368                         CurrDFPageByteDiv16++;
369
370                         /* Increment the block 16 byte block counter */
371                         BytesInBlockDiv16++;
372                 }
373
374                 /* Decrement the blocks remaining counter */
375                 TotalBlocks--;
376         }
377
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();
383
384         /* Deselect all Dataflash chips */
385         Dataflash_DeselectChip();
386 }
387
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.
392  *
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
396  */
397 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress,
398                                      uint16_t TotalBlocks,
399                                      uint8_t* BufferPtr)
400 {
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);
404
405         /* Select the correct starting Dataflash IC for the block requested */
406         Dataflash_SelectChipFromPage(CurrDFPage);
407
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);
415
416         while (TotalBlocks)
417         {
418                 uint8_t BytesInBlockDiv16 = 0;
419
420                 /* Write an endpoint packet sized data block to the Dataflash */
421                 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
422                 {
423                         /* Check if end of Dataflash page reached */
424                         if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
425                         {
426                                 /* Reset the Dataflash buffer counter, increment the page counter */
427                                 CurrDFPageByteDiv16 = 0;
428                                 CurrDFPage++;
429
430                                 /* Select the next Dataflash chip based on the new Dataflash page index */
431                                 Dataflash_SelectChipFromPage(CurrDFPage);
432
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);
440                         }
441
442                         /* Read one 16-byte chunk of data from the Dataflash */
443                         for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
444                           *(BufferPtr++) = Dataflash_ReceiveByte();
445
446                         /* Increment the Dataflash page 16 byte block counter */
447                         CurrDFPageByteDiv16++;
448
449                         /* Increment the block 16 byte block counter */
450                         BytesInBlockDiv16++;
451                 }
452
453                 /* Decrement the blocks remaining counter */
454                 TotalBlocks--;
455         }
456
457         /* Deselect all Dataflash chips */
458         Dataflash_DeselectChip();
459 }
460
461 /** Disables the Dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
462 void DataflashManager_ResetDataflashProtections(void)
463 {
464         /* Select first Dataflash chip, send the read status register command */
465         Dataflash_SelectChip(DATAFLASH_CHIP1);
466         Dataflash_SendByte(DF_CMD_GETSTATUS);
467
468         /* Check if sector protection is enabled */
469         if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
470         {
471                 Dataflash_ToggleSelectedChipCS();
472
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]);
478         }
479
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);
484
485         /* Check if sector protection is enabled */
486         if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
487         {
488                 Dataflash_ToggleSelectedChipCS();
489
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]);
495         }
496         #endif
497
498         /* Deselect current Dataflash chip */
499         Dataflash_DeselectChip();
500 }
501
502 /** Performs a simple test on the attached Dataflash IC(s) to ensure that they are working.
503  *
504  *  \return Boolean \c true if all media chips are working, \c false otherwise
505  */
506 bool DataflashManager_CheckDataflashOperation(void)
507 {
508         uint8_t ReturnByte;
509
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();
515
516         /* If returned data is invalid, fail the command */
517         if (ReturnByte != DF_MANUFACTURER_ATMEL)
518           return false;
519
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();
526
527         /* If returned data is invalid, fail the command */
528         if (ReturnByte != DF_MANUFACTURER_ATMEL)
529           return false;
530         #endif
531
532         return true;
533 }
534