Update file contributor copyrights for 2012.
[pub/lufa.git] / Projects / Incomplete / StandaloneProgrammer / Lib / DataflashManager.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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* const MSInterfaceInfo,
52 const uint32_t BlockAddress,
53 uint16_t TotalBlocks)
54 {
55 uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
56 uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
57 uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
58 bool UsingSecondBuffer = false;
59
60 /* Select the correct starting Dataflash IC for the block requested */
61 Dataflash_SelectChipFromPage(CurrDFPage);
62
63 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
64 /* Copy selected dataflash's current page contents to the Dataflash buffer */
65 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1);
66 Dataflash_SendAddressBytes(CurrDFPage, 0);
67 Dataflash_WaitWhileBusy();
68 #endif
69
70 /* Send the Dataflash buffer write command */
71 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
72 Dataflash_SendAddressBytes(0, CurrDFPageByte);
73
74 /* Wait until endpoint is ready before continuing */
75 if (Endpoint_WaitUntilReady())
76 return;
77
78 while (TotalBlocks)
79 {
80 uint8_t BytesInBlockDiv16 = 0;
81
82 /* Write an endpoint packet sized data block to the Dataflash */
83 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
84 {
85 /* Check if the endpoint is currently empty */
86 if (!(Endpoint_IsReadWriteAllowed()))
87 {
88 /* Clear the current endpoint bank */
89 Endpoint_ClearOUT();
90
91 /* Wait until the host has sent another packet */
92 if (Endpoint_WaitUntilReady())
93 return;
94 }
95
96 /* Check if end of Dataflash page reached */
97 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
98 {
99 /* Write the Dataflash buffer contents back to the Dataflash page */
100 Dataflash_WaitWhileBusy();
101 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE);
102 Dataflash_SendAddressBytes(CurrDFPage, 0);
103
104 /* Reset the Dataflash buffer counter, increment the page counter */
105 CurrDFPageByteDiv16 = 0;
106 CurrDFPage++;
107
108 /* Once all the Dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */
109 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS))
110 UsingSecondBuffer = !(UsingSecondBuffer);
111
112 /* Select the next Dataflash chip based on the new Dataflash page index */
113 Dataflash_SelectChipFromPage(CurrDFPage);
114
115 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
116 /* If less than one Dataflash page remaining, copy over the existing page to preserve trailing data */
117 if ((TotalBlocks * (VIRTUAL_MEMORY_BLOCK_SIZE >> 4)) < (DATAFLASH_PAGE_SIZE >> 4))
118 {
119 /* Copy selected dataflash's current page contents to the Dataflash buffer */
120 Dataflash_WaitWhileBusy();
121 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2 : DF_CMD_MAINMEMTOBUFF1);
122 Dataflash_SendAddressBytes(CurrDFPage, 0);
123 Dataflash_WaitWhileBusy();
124 }
125 #endif
126
127 /* Send the Dataflash buffer write command */
128 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE : DF_CMD_BUFF1WRITE);
129 Dataflash_SendAddressBytes(0, 0);
130 }
131
132 /* Write one 16-byte chunk of data to the Dataflash */
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 Dataflash_SendByte(Endpoint_Read_8());
149
150 /* Increment the Dataflash page 16 byte block counter */
151 CurrDFPageByteDiv16++;
152
153 /* Increment the block 16 byte block counter */
154 BytesInBlockDiv16++;
155
156 /* Check if the current command is being aborted by the host */
157 if (MSInterfaceInfo->State.IsMassStoreReset)
158 return;
159 }
160
161 /* Decrement the blocks remaining counter */
162 TotalBlocks--;
163 }
164
165 /* Write the Dataflash buffer contents back to the Dataflash page */
166 Dataflash_WaitWhileBusy();
167 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE);
168 Dataflash_SendAddressBytes(CurrDFPage, 0x00);
169 Dataflash_WaitWhileBusy();
170
171 /* If the endpoint is empty, clear it ready for the next packet from the host */
172 if (!(Endpoint_IsReadWriteAllowed()))
173 Endpoint_ClearOUT();
174
175 /* Deselect all Dataflash chips */
176 Dataflash_DeselectChip();
177 }
178
179 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board Dataflash IC(s), into
180 * the pre-selected data IN endpoint. This routine reads in Dataflash page sized blocks from the Dataflash
181 * and writes them in OS sized blocks to the endpoint.
182 *
183 * \param[in] MSInterfaceInfo Pointer to a structure containing a Mass Storage Class configuration and state
184 * \param[in] BlockAddress Data block starting address for the read sequence
185 * \param[in] TotalBlocks Number of blocks of data to read
186 */
187 void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
188 const uint32_t BlockAddress,
189 uint16_t TotalBlocks)
190 {
191 uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
192 uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
193 uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
194
195 /* Select the correct starting Dataflash IC for the block requested */
196 Dataflash_SelectChipFromPage(CurrDFPage);
197
198 /* Send the Dataflash main memory page read command */
199 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
200 Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte);
201 Dataflash_SendByte(0x00);
202 Dataflash_SendByte(0x00);
203 Dataflash_SendByte(0x00);
204 Dataflash_SendByte(0x00);
205
206 /* Wait until endpoint is ready before continuing */
207 if (Endpoint_WaitUntilReady())
208 return;
209
210 while (TotalBlocks)
211 {
212 uint8_t BytesInBlockDiv16 = 0;
213
214 /* Write an endpoint packet sized data block to the Dataflash */
215 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
216 {
217 /* Check if the endpoint is currently full */
218 if (!(Endpoint_IsReadWriteAllowed()))
219 {
220 /* Clear the endpoint bank to send its contents to the host */
221 Endpoint_ClearIN();
222
223 /* Wait until the endpoint is ready for more data */
224 if (Endpoint_WaitUntilReady())
225 return;
226 }
227
228 /* Check if end of Dataflash page reached */
229 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
230 {
231 /* Reset the Dataflash buffer counter, increment the page counter */
232 CurrDFPageByteDiv16 = 0;
233 CurrDFPage++;
234
235 /* Select the next Dataflash chip based on the new Dataflash page index */
236 Dataflash_SelectChipFromPage(CurrDFPage);
237
238 /* Send the Dataflash main memory page read command */
239 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
240 Dataflash_SendAddressBytes(CurrDFPage, 0);
241 Dataflash_SendByte(0x00);
242 Dataflash_SendByte(0x00);
243 Dataflash_SendByte(0x00);
244 Dataflash_SendByte(0x00);
245 }
246
247 /* Read one 16-byte chunk of data from the Dataflash */
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 Endpoint_Write_8(Dataflash_ReceiveByte());
264
265 /* Increment the Dataflash page 16 byte block counter */
266 CurrDFPageByteDiv16++;
267
268 /* Increment the block 16 byte block counter */
269 BytesInBlockDiv16++;
270
271 /* Check if the current command is being aborted by the host */
272 if (MSInterfaceInfo->State.IsMassStoreReset)
273 return;
274 }
275
276 /* Decrement the blocks remaining counter */
277 TotalBlocks--;
278 }
279
280 /* If the endpoint is full, send its contents to the host */
281 if (!(Endpoint_IsReadWriteAllowed()))
282 Endpoint_ClearIN();
283
284 /* Deselect all Dataflash chips */
285 Dataflash_DeselectChip();
286 }
287
288 /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board Dataflash IC(s), from
289 * the given RAM buffer. This routine reads in OS sized blocks from the buffer and writes them to the
290 * Dataflash in Dataflash page sized blocks. This can be linked to FAT libraries to write files to the
291 * Dataflash.
292 *
293 * \param[in] BlockAddress Data block starting address for the write sequence
294 * \param[in] TotalBlocks Number of blocks of data to write
295 * \param[in] BufferPtr Pointer to the data source RAM buffer
296 */
297 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress,
298 uint16_t TotalBlocks,
299 const uint8_t* BufferPtr)
300 {
301 uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
302 uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
303 uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
304 bool UsingSecondBuffer = false;
305
306 /* Select the correct starting Dataflash IC for the block requested */
307 Dataflash_SelectChipFromPage(CurrDFPage);
308
309 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
310 /* Copy selected dataflash's current page contents to the Dataflash buffer */
311 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1);
312 Dataflash_SendAddressBytes(CurrDFPage, 0);
313 Dataflash_WaitWhileBusy();
314 #endif
315
316 /* Send the Dataflash buffer write command */
317 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
318 Dataflash_SendAddressBytes(0, CurrDFPageByte);
319
320 while (TotalBlocks)
321 {
322 uint8_t BytesInBlockDiv16 = 0;
323
324 /* Write an endpoint packet sized data block to the Dataflash */
325 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
326 {
327 /* Check if end of Dataflash page reached */
328 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
329 {
330 /* Write the Dataflash buffer contents back to the Dataflash page */
331 Dataflash_WaitWhileBusy();
332 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE);
333 Dataflash_SendAddressBytes(CurrDFPage, 0);
334
335 /* Reset the Dataflash buffer counter, increment the page counter */
336 CurrDFPageByteDiv16 = 0;
337 CurrDFPage++;
338
339 /* Once all the Dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */
340 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS))
341 UsingSecondBuffer = !(UsingSecondBuffer);
342
343 /* Select the next Dataflash chip based on the new Dataflash page index */
344 Dataflash_SelectChipFromPage(CurrDFPage);
345
346 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
347 /* If less than one Dataflash page remaining, copy over the existing page to preserve trailing data */
348 if ((TotalBlocks * (VIRTUAL_MEMORY_BLOCK_SIZE >> 4)) < (DATAFLASH_PAGE_SIZE >> 4))
349 {
350 /* Copy selected dataflash's current page contents to the Dataflash buffer */
351 Dataflash_WaitWhileBusy();
352 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2 : DF_CMD_MAINMEMTOBUFF1);
353 Dataflash_SendAddressBytes(CurrDFPage, 0);
354 Dataflash_WaitWhileBusy();
355 }
356 #endif
357
358 /* Send the Dataflash buffer write command */
359 Dataflash_ToggleSelectedChipCS();
360 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE : DF_CMD_BUFF1WRITE);
361 Dataflash_SendAddressBytes(0, 0);
362 }
363
364 /* Write one 16-byte chunk of data to the Dataflash */
365 for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
366 Dataflash_SendByte(*(BufferPtr++));
367
368 /* Increment the Dataflash page 16 byte block counter */
369 CurrDFPageByteDiv16++;
370
371 /* Increment the block 16 byte block counter */
372 BytesInBlockDiv16++;
373 }
374
375 /* Decrement the blocks remaining counter */
376 TotalBlocks--;
377 }
378
379 /* Write the Dataflash buffer contents back to the Dataflash page */
380 Dataflash_WaitWhileBusy();
381 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE);
382 Dataflash_SendAddressBytes(CurrDFPage, 0x00);
383 Dataflash_WaitWhileBusy();
384
385 /* Deselect all Dataflash chips */
386 Dataflash_DeselectChip();
387 }
388
389 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board Dataflash IC(s), into
390 * the preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash
391 * and writes them in OS sized blocks to the given buffer. This can be linked to FAT libraries to read
392 * the files stored on the Dataflash.
393 *
394 * \param[in] BlockAddress Data block starting address for the read sequence
395 * \param[in] TotalBlocks Number of blocks of data to read
396 * \param[out] BufferPtr Pointer to the data destination RAM buffer
397 */
398 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress,
399 uint16_t TotalBlocks,
400 uint8_t* BufferPtr)
401 {
402 uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
403 uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
404 uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
405
406 /* Select the correct starting Dataflash IC for the block requested */
407 Dataflash_SelectChipFromPage(CurrDFPage);
408
409 /* Send the Dataflash main memory page read command */
410 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
411 Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte);
412 Dataflash_SendByte(0x00);
413 Dataflash_SendByte(0x00);
414 Dataflash_SendByte(0x00);
415 Dataflash_SendByte(0x00);
416
417 while (TotalBlocks)
418 {
419 uint8_t BytesInBlockDiv16 = 0;
420
421 /* Write an endpoint packet sized data block to the Dataflash */
422 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
423 {
424 /* Check if end of Dataflash page reached */
425 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
426 {
427 /* Reset the Dataflash buffer counter, increment the page counter */
428 CurrDFPageByteDiv16 = 0;
429 CurrDFPage++;
430
431 /* Select the next Dataflash chip based on the new Dataflash page index */
432 Dataflash_SelectChipFromPage(CurrDFPage);
433
434 /* Send the Dataflash main memory page read command */
435 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
436 Dataflash_SendAddressBytes(CurrDFPage, 0);
437 Dataflash_SendByte(0x00);
438 Dataflash_SendByte(0x00);
439 Dataflash_SendByte(0x00);
440 Dataflash_SendByte(0x00);
441 }
442
443 /* Read one 16-byte chunk of data from the Dataflash */
444 for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
445 *(BufferPtr++) = Dataflash_ReceiveByte();
446
447 /* Increment the Dataflash page 16 byte block counter */
448 CurrDFPageByteDiv16++;
449
450 /* Increment the block 16 byte block counter */
451 BytesInBlockDiv16++;
452 }
453
454 /* Decrement the blocks remaining counter */
455 TotalBlocks--;
456 }
457
458 /* Deselect all Dataflash chips */
459 Dataflash_DeselectChip();
460 }
461
462 /** Disables the Dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
463 void DataflashManager_ResetDataflashProtections(void)
464 {
465 /* Select first Dataflash chip, send the read status register command */
466 Dataflash_SelectChip(DATAFLASH_CHIP1);
467 Dataflash_SendByte(DF_CMD_GETSTATUS);
468
469 /* Check if sector protection is enabled */
470 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
471 {
472 Dataflash_ToggleSelectedChipCS();
473
474 /* Send the commands to disable sector protection */
475 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[0]);
476 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[1]);
477 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[2]);
478 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[3]);
479 }
480
481 /* Select second Dataflash chip (if present on selected board), send read status register command */
482 #if (DATAFLASH_TOTALCHIPS == 2)
483 Dataflash_SelectChip(DATAFLASH_CHIP2);
484 Dataflash_SendByte(DF_CMD_GETSTATUS);
485
486 /* Check if sector protection is enabled */
487 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
488 {
489 Dataflash_ToggleSelectedChipCS();
490
491 /* Send the commands to disable sector protection */
492 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[0]);
493 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[1]);
494 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[2]);
495 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[3]);
496 }
497 #endif
498
499 /* Deselect current Dataflash chip */
500 Dataflash_DeselectChip();
501 }
502
503 /** Performs a simple test on the attached Dataflash IC(s) to ensure that they are working.
504 *
505 * \return Boolean true if all media chips are working, false otherwise
506 */
507 bool DataflashManager_CheckDataflashOperation(void)
508 {
509 uint8_t ReturnByte;
510
511 /* Test first Dataflash IC is present and responding to commands */
512 Dataflash_SelectChip(DATAFLASH_CHIP1);
513 Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO);
514 ReturnByte = Dataflash_ReceiveByte();
515 Dataflash_DeselectChip();
516
517 /* If returned data is invalid, fail the command */
518 if (ReturnByte != DF_MANUFACTURER_ATMEL)
519 return false;
520
521 #if (DATAFLASH_TOTALCHIPS == 2)
522 /* Test second Dataflash IC is present and responding to commands */
523 Dataflash_SelectChip(DATAFLASH_CHIP2);
524 Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO);
525 ReturnByte = Dataflash_ReceiveByte();
526 Dataflash_DeselectChip();
527
528 /* If returned data is invalid, fail the command */
529 if (ReturnByte != DF_MANUFACTURER_ATMEL)
530 return false;
531 #endif
532
533 return true;
534 }
535 #endif
536