Fix MIT license language to make its intent clearer.
[pub/USBasp.git] / Projects / Incomplete / StandaloneProgrammer / Lib / DataflashManager.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 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 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
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 #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.
46 *
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
50 */
51 void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, const uint32_t BlockAddress, uint16_t TotalBlocks)
52 {
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;
57
58 /* Select the correct starting Dataflash IC for the block requested */
59 Dataflash_SelectChipFromPage(CurrDFPage);
60
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();
66 #endif
67
68 /* Send the dataflash buffer write command */
69 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
70 Dataflash_SendAddressBytes(0, CurrDFPageByte);
71
72 /* Wait until endpoint is ready before continuing */
73 if (Endpoint_WaitUntilReady())
74 return;
75
76 while (TotalBlocks)
77 {
78 uint8_t BytesInBlockDiv16 = 0;
79
80 /* Write an endpoint packet sized data block to the dataflash */
81 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
82 {
83 /* Check if the endpoint is currently empty */
84 if (!(Endpoint_IsReadWriteAllowed()))
85 {
86 /* Clear the current endpoint bank */
87 Endpoint_ClearOUT();
88
89 /* Wait until the host has sent another packet */
90 if (Endpoint_WaitUntilReady())
91 return;
92 }
93
94 /* Check if end of dataflash page reached */
95 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
96 {
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);
101
102 /* Reset the dataflash buffer counter, increment the page counter */
103 CurrDFPageByteDiv16 = 0;
104 CurrDFPage++;
105
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);
109
110 /* Select the next dataflash chip based on the new dataflash page index */
111 Dataflash_SelectChipFromPage(CurrDFPage);
112
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))
116 {
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();
122 }
123 #endif
124
125 /* Send the dataflash buffer write command */
126 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE : DF_CMD_BUFF1WRITE);
127 Dataflash_SendAddressBytes(0, 0);
128 }
129
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());
147
148 /* Increment the dataflash page 16 byte block counter */
149 CurrDFPageByteDiv16++;
150
151 /* Increment the block 16 byte block counter */
152 BytesInBlockDiv16++;
153
154 /* Check if the current command is being aborted by the host */
155 if (MSInterfaceInfo->State.IsMassStoreReset)
156 return;
157 }
158
159 /* Decrement the blocks remaining counter and reset the sub block counter */
160 TotalBlocks--;
161 }
162
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();
168
169 /* If the endpoint is empty, clear it ready for the next packet from the host */
170 if (!(Endpoint_IsReadWriteAllowed()))
171 Endpoint_ClearOUT();
172
173 /* Deselect all dataflash chips */
174 Dataflash_DeselectChip();
175 }
176
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.
180 *
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
184 */
185 void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, const uint32_t BlockAddress, uint16_t TotalBlocks)
186 {
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);
190
191 /* Select the correct starting Dataflash IC for the block requested */
192 Dataflash_SelectChipFromPage(CurrDFPage);
193
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);
201
202 /* Wait until endpoint is ready before continuing */
203 if (Endpoint_WaitUntilReady())
204 return;
205
206 while (TotalBlocks)
207 {
208 uint8_t BytesInBlockDiv16 = 0;
209
210 /* Write an endpoint packet sized data block to the dataflash */
211 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
212 {
213 /* Check if the endpoint is currently full */
214 if (!(Endpoint_IsReadWriteAllowed()))
215 {
216 /* Clear the endpoint bank to send its contents to the host */
217 Endpoint_ClearIN();
218
219 /* Wait until the endpoint is ready for more data */
220 if (Endpoint_WaitUntilReady())
221 return;
222 }
223
224 /* Check if end of dataflash page reached */
225 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
226 {
227 /* Reset the dataflash buffer counter, increment the page counter */
228 CurrDFPageByteDiv16 = 0;
229 CurrDFPage++;
230
231 /* Select the next dataflash chip based on the new dataflash page index */
232 Dataflash_SelectChipFromPage(CurrDFPage);
233
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);
241 }
242
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());
260
261 /* Increment the dataflash page 16 byte block counter */
262 CurrDFPageByteDiv16++;
263
264 /* Increment the block 16 byte block counter */
265 BytesInBlockDiv16++;
266
267 /* Check if the current command is being aborted by the host */
268 if (MSInterfaceInfo->State.IsMassStoreReset)
269 return;
270 }
271
272 /* Decrement the blocks remaining counter */
273 TotalBlocks--;
274 }
275
276 /* If the endpoint is full, send its contents to the host */
277 if (!(Endpoint_IsReadWriteAllowed()))
278 Endpoint_ClearIN();
279
280 /* Deselect all dataflash chips */
281 Dataflash_DeselectChip();
282 }
283
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
287 * dataflash.
288 *
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
292 */
293 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress, uint16_t TotalBlocks, uint8_t* BufferPtr)
294 {
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;
299
300 /* Select the correct starting Dataflash IC for the block requested */
301 Dataflash_SelectChipFromPage(CurrDFPage);
302
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();
308 #endif
309
310 /* Send the dataflash buffer write command */
311 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
312 Dataflash_SendAddressBytes(0, CurrDFPageByte);
313
314 while (TotalBlocks)
315 {
316 uint8_t BytesInBlockDiv16 = 0;
317
318 /* Write an endpoint packet sized data block to the dataflash */
319 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
320 {
321 /* Check if end of dataflash page reached */
322 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
323 {
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);
328
329 /* Reset the dataflash buffer counter, increment the page counter */
330 CurrDFPageByteDiv16 = 0;
331 CurrDFPage++;
332
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);
336
337 /* Select the next dataflash chip based on the new dataflash page index */
338 Dataflash_SelectChipFromPage(CurrDFPage);
339
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))
343 {
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();
349 }
350 #endif
351
352 /* Send the dataflash buffer write command */
353 Dataflash_ToggleSelectedChipCS();
354 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
355 Dataflash_SendAddressBytes(0, 0);
356 }
357
358 /* Write one 16-byte chunk of data to the dataflash */
359 for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
360 Dataflash_SendByte(*(BufferPtr++));
361
362 /* Increment the dataflash page 16 byte block counter */
363 CurrDFPageByteDiv16++;
364
365 /* Increment the block 16 byte block counter */
366 BytesInBlockDiv16++;
367 }
368
369 /* Decrement the blocks remaining counter and reset the sub block counter */
370 TotalBlocks--;
371 }
372
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();
378
379 /* Deselect all dataflash chips */
380 Dataflash_DeselectChip();
381 }
382
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.
387 *
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
391 */
392 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress, uint16_t TotalBlocks, uint8_t* BufferPtr)
393 {
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);
397
398 /* Select the correct starting Dataflash IC for the block requested */
399 Dataflash_SelectChipFromPage(CurrDFPage);
400
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);
408
409 while (TotalBlocks)
410 {
411 uint8_t BytesInBlockDiv16 = 0;
412
413 /* Write an endpoint packet sized data block to the dataflash */
414 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
415 {
416 /* Check if end of dataflash page reached */
417 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
418 {
419 /* Reset the dataflash buffer counter, increment the page counter */
420 CurrDFPageByteDiv16 = 0;
421 CurrDFPage++;
422
423 /* Select the next dataflash chip based on the new dataflash page index */
424 Dataflash_SelectChipFromPage(CurrDFPage);
425
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);
433 }
434
435 /* Read one 16-byte chunk of data from the dataflash */
436 for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
437 *(BufferPtr++) = Dataflash_ReceiveByte();
438
439 /* Increment the dataflash page 16 byte block counter */
440 CurrDFPageByteDiv16++;
441
442 /* Increment the block 16 byte block counter */
443 BytesInBlockDiv16++;
444 }
445
446 /* Decrement the blocks remaining counter */
447 TotalBlocks--;
448 }
449
450 /* Deselect all dataflash chips */
451 Dataflash_DeselectChip();
452 }
453
454 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
455 void DataflashManager_ResetDataflashProtections(void)
456 {
457 /* Select first dataflash chip, send the read status register command */
458 Dataflash_SelectChip(DATAFLASH_CHIP1);
459 Dataflash_SendByte(DF_CMD_GETSTATUS);
460
461 /* Check if sector protection is enabled */
462 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
463 {
464 Dataflash_ToggleSelectedChipCS();
465
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]);
471 }
472
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);
477
478 /* Check if sector protection is enabled */
479 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
480 {
481 Dataflash_ToggleSelectedChipCS();
482
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]);
488 }
489 #endif
490
491 /* Deselect current dataflash chip */
492 Dataflash_DeselectChip();
493 }
494
495 /** Performs a simple test on the attached Dataflash IC(s) to ensure that they are working.
496 *
497 * \return Boolean true if all media chips are working, false otherwise
498 */
499 bool DataflashManager_CheckDataflashOperation(void)
500 {
501 uint8_t ReturnByte;
502
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();
508
509 /* If returned data is invalid, fail the command */
510 if (ReturnByte != DF_MANUFACTURER_ATMEL)
511 return false;
512
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();
519
520 /* If returned data is invalid, fail the command */
521 if (ReturnByte != DF_MANUFACTURER_ATMEL)
522 return false;
523 #endif
524
525 return true;
526 }
527 #endif