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, 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.
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] BlockAddress Data block starting address for the write sequence
47 * \param[in] TotalBlocks Number of blocks of data to write
49 void DataflashManager_WriteBlocks(const uint32_t BlockAddress
, uint16_t TotalBlocks
)
51 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
52 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
53 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
54 bool UsingSecondBuffer
= false;
56 /* Select the correct starting Dataflash IC for the block requested */
57 Dataflash_SelectChipFromPage(CurrDFPage
);
59 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
60 /* Copy selected dataflash's current page contents to the dataflash buffer */
61 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
);
62 Dataflash_SendAddressBytes(CurrDFPage
, 0);
63 Dataflash_WaitWhileBusy();
66 /* Send the dataflash buffer write command */
67 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
68 Dataflash_SendAddressBytes(0, CurrDFPageByte
);
70 /* Wait until endpoint is ready before continuing */
71 if (Endpoint_WaitUntilReady())
76 uint8_t BytesInBlockDiv16
= 0;
78 /* Write an endpoint packet sized data block to the dataflash */
79 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
81 /* Check if the endpoint is currently empty */
82 if (!(Endpoint_IsReadWriteAllowed()))
84 /* Clear the current endpoint bank */
87 /* Wait until the host has sent another packet */
88 if (Endpoint_WaitUntilReady())
92 /* Check if end of dataflash page reached */
93 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
95 /* Write the dataflash buffer contents back to the dataflash page */
96 Dataflash_WaitWhileBusy();
97 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
98 Dataflash_SendAddressBytes(CurrDFPage
, 0);
100 /* Reset the dataflash buffer counter, increment the page counter */
101 CurrDFPageByteDiv16
= 0;
104 /* Once all the dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */
105 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS
))
106 UsingSecondBuffer
= !(UsingSecondBuffer
);
108 /* Select the next dataflash chip based on the new dataflash page index */
109 Dataflash_SelectChipFromPage(CurrDFPage
);
111 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
112 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */
113 if ((TotalBlocks
* (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4)) < (DATAFLASH_PAGE_SIZE
>> 4))
115 /* Copy selected dataflash's current page contents to the dataflash buffer */
116 Dataflash_WaitWhileBusy();
117 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2
: DF_CMD_MAINMEMTOBUFF1
);
118 Dataflash_SendAddressBytes(CurrDFPage
, 0);
119 Dataflash_WaitWhileBusy();
123 /* Send the dataflash buffer write command */
124 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE
: DF_CMD_BUFF1WRITE
);
125 Dataflash_SendAddressBytes(0, 0);
128 /* Write one 16-byte chunk of data to the dataflash */
129 Dataflash_SendByte(Endpoint_Read_Byte());
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());
146 /* Increment the dataflash page 16 byte block counter */
147 CurrDFPageByteDiv16
++;
149 /* Increment the block 16 byte block counter */
152 /* Check if the current command is being aborted by the host */
153 if (IsMassStoreReset
)
157 /* Decrement the blocks remaining counter and reset the sub block counter */
161 /* Write the dataflash buffer contents back to the dataflash page */
162 Dataflash_WaitWhileBusy();
163 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
164 Dataflash_SendAddressBytes(CurrDFPage
, 0x00);
165 Dataflash_WaitWhileBusy();
167 /* If the endpoint is empty, clear it ready for the next packet from the host */
168 if (!(Endpoint_IsReadWriteAllowed()))
171 /* Deselect all dataflash chips */
172 Dataflash_DeselectChip();
175 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into
176 * the pre-selected data IN endpoint. This routine reads in Dataflash page sized blocks from the Dataflash
177 * and writes them in OS sized blocks to the endpoint.
179 * \param[in] BlockAddress Data block starting address for the read sequence
180 * \param[in] TotalBlocks Number of blocks of data to read
182 void DataflashManager_ReadBlocks(const uint32_t BlockAddress
, uint16_t TotalBlocks
)
184 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
185 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
186 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
188 /* Select the correct starting Dataflash IC for the block requested */
189 Dataflash_SelectChipFromPage(CurrDFPage
);
191 /* Send the dataflash main memory page read command */
192 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
193 Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
);
194 Dataflash_SendByte(0x00);
195 Dataflash_SendByte(0x00);
196 Dataflash_SendByte(0x00);
197 Dataflash_SendByte(0x00);
199 /* Wait until endpoint is ready before continuing */
200 if (Endpoint_WaitUntilReady())
205 uint8_t BytesInBlockDiv16
= 0;
207 /* Write an endpoint packet sized data block to the dataflash */
208 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
210 /* Check if the endpoint is currently full */
211 if (!(Endpoint_IsReadWriteAllowed()))
213 /* Clear the endpoint bank to send its contents to the host */
216 /* Wait until the endpoint is ready for more data */
217 if (Endpoint_WaitUntilReady())
221 /* Check if end of dataflash page reached */
222 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
224 /* Reset the dataflash buffer counter, increment the page counter */
225 CurrDFPageByteDiv16
= 0;
228 /* Select the next dataflash chip based on the new dataflash page index */
229 Dataflash_SelectChipFromPage(CurrDFPage
);
231 /* Send the dataflash main memory page read command */
232 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
233 Dataflash_SendAddressBytes(CurrDFPage
, 0);
234 Dataflash_SendByte(0x00);
235 Dataflash_SendByte(0x00);
236 Dataflash_SendByte(0x00);
237 Dataflash_SendByte(0x00);
240 /* Read one 16-byte chunk of data from the dataflash */
241 Endpoint_Write_Byte(Dataflash_ReceiveByte());
242 Endpoint_Write_Byte(Dataflash_ReceiveByte());
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());
258 /* Increment the dataflash page 16 byte block counter */
259 CurrDFPageByteDiv16
++;
261 /* Increment the block 16 byte block counter */
264 /* Check if the current command is being aborted by the host */
265 if (IsMassStoreReset
)
269 /* Decrement the blocks remaining counter */
273 /* If the endpoint is full, send its contents to the host */
274 if (!(Endpoint_IsReadWriteAllowed()))
277 /* Deselect all dataflash chips */
278 Dataflash_DeselectChip();
281 /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board dataflash IC(s), from
282 * the a given RAM buffer. This routine reads in OS sized blocks from the buffer and writes them to the
283 * dataflash in Dataflash page sized blocks. This can be linked to FAT libraries to write files to the
286 * \param[in] BlockAddress Data block starting address for the write sequence
287 * \param[in] TotalBlocks Number of blocks of data to write
288 * \param[in] BufferPtr Pointer to the data source RAM buffer
290 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
)
292 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
293 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
294 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
295 bool UsingSecondBuffer
= false;
297 /* Select the correct starting Dataflash IC for the block requested */
298 Dataflash_SelectChipFromPage(CurrDFPage
);
300 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
301 /* Copy selected dataflash's current page contents to the dataflash buffer */
302 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
);
303 Dataflash_SendAddressBytes(CurrDFPage
, 0);
304 Dataflash_WaitWhileBusy();
307 /* Send the dataflash buffer write command */
308 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
309 Dataflash_SendAddressBytes(0, CurrDFPageByte
);
313 uint8_t BytesInBlockDiv16
= 0;
315 /* Write an endpoint packet sized data block to the dataflash */
316 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
318 /* Check if end of dataflash page reached */
319 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
321 /* Write the dataflash buffer contents back to the dataflash page */
322 Dataflash_WaitWhileBusy();
323 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
324 Dataflash_SendAddressBytes(CurrDFPage
, 0);
326 /* Reset the dataflash buffer counter, increment the page counter */
327 CurrDFPageByteDiv16
= 0;
330 /* Once all the dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */
331 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS
))
332 UsingSecondBuffer
= !(UsingSecondBuffer
);
334 /* Select the next dataflash chip based on the new dataflash page index */
335 Dataflash_SelectChipFromPage(CurrDFPage
);
337 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
338 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */
339 if ((TotalBlocks
* (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4)) < (DATAFLASH_PAGE_SIZE
>> 4))
341 /* Copy selected dataflash's current page contents to the dataflash buffer */
342 Dataflash_WaitWhileBusy();
343 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2
: DF_CMD_MAINMEMTOBUFF1
);
344 Dataflash_SendAddressBytes(CurrDFPage
, 0);
345 Dataflash_WaitWhileBusy();
349 /* Send the dataflash buffer write command */
350 Dataflash_ToggleSelectedChipCS();
351 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
352 Dataflash_SendAddressBytes(0, 0);
355 /* Write one 16-byte chunk of data to the dataflash */
356 for (uint8_t ByteNum
= 0; ByteNum
< 16; ByteNum
++)
357 Dataflash_SendByte(*(BufferPtr
++));
359 /* Increment the dataflash page 16 byte block counter */
360 CurrDFPageByteDiv16
++;
362 /* Increment the block 16 byte block counter */
366 /* Decrement the blocks remaining counter and reset the sub block counter */
370 /* Write the dataflash buffer contents back to the dataflash page */
371 Dataflash_WaitWhileBusy();
372 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
373 Dataflash_SendAddressBytes(CurrDFPage
, 0x00);
374 Dataflash_WaitWhileBusy();
376 /* Deselect all dataflash chips */
377 Dataflash_DeselectChip();
380 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into
381 * the a preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash
382 * and writes them in OS sized blocks to the given buffer. This can be linked to FAT libraries to read
383 * the files stored on the dataflash.
385 * \param[in] BlockAddress Data block starting address for the read sequence
386 * \param[in] TotalBlocks Number of blocks of data to read
387 * \param[out] BufferPtr Pointer to the data destination RAM buffer
389 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
)
391 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
392 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
393 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
395 /* Select the correct starting Dataflash IC for the block requested */
396 Dataflash_SelectChipFromPage(CurrDFPage
);
398 /* Send the dataflash main memory page read command */
399 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
400 Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
);
401 Dataflash_SendByte(0x00);
402 Dataflash_SendByte(0x00);
403 Dataflash_SendByte(0x00);
404 Dataflash_SendByte(0x00);
408 uint8_t BytesInBlockDiv16
= 0;
410 /* Write an endpoint packet sized data block to the dataflash */
411 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
413 /* Check if end of dataflash page reached */
414 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
416 /* Reset the dataflash buffer counter, increment the page counter */
417 CurrDFPageByteDiv16
= 0;
420 /* Select the next dataflash chip based on the new dataflash page index */
421 Dataflash_SelectChipFromPage(CurrDFPage
);
423 /* Send the dataflash main memory page read command */
424 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
425 Dataflash_SendAddressBytes(CurrDFPage
, 0);
426 Dataflash_SendByte(0x00);
427 Dataflash_SendByte(0x00);
428 Dataflash_SendByte(0x00);
429 Dataflash_SendByte(0x00);
432 /* Read one 16-byte chunk of data from the dataflash */
433 for (uint8_t ByteNum
= 0; ByteNum
< 16; ByteNum
++)
434 *(BufferPtr
++) = Dataflash_ReceiveByte();
436 /* Increment the dataflash page 16 byte block counter */
437 CurrDFPageByteDiv16
++;
439 /* Increment the block 16 byte block counter */
443 /* Decrement the blocks remaining counter */
447 /* Deselect all dataflash chips */
448 Dataflash_DeselectChip();
451 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
452 void DataflashManager_ResetDataflashProtections(void)
454 /* Select first dataflash chip, send the read status register command */
455 Dataflash_SelectChip(DATAFLASH_CHIP1
);
456 Dataflash_SendByte(DF_CMD_GETSTATUS
);
458 /* Check if sector protection is enabled */
459 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
)
461 Dataflash_ToggleSelectedChipCS();
463 /* Send the commands to disable sector protection */
464 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]);
465 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]);
466 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]);
467 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]);
470 /* Select second dataflash chip (if present on selected board), send read status register command */
471 #if (DATAFLASH_TOTALCHIPS == 2)
472 Dataflash_SelectChip(DATAFLASH_CHIP2
);
473 Dataflash_SendByte(DF_CMD_GETSTATUS
);
475 /* Check if sector protection is enabled */
476 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
)
478 Dataflash_ToggleSelectedChipCS();
480 /* Send the commands to disable sector protection */
481 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]);
482 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]);
483 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]);
484 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]);
488 /* Deselect current dataflash chip */
489 Dataflash_DeselectChip();
492 /** Performs a simple test on the attached Dataflash IC(s) to ensure that they are working.
494 * \return Boolean true if all media chips are working, false otherwise
496 bool DataflashManager_CheckDataflashOperation(void)
500 /* Test first Dataflash IC is present and responding to commands */
501 Dataflash_SelectChip(DATAFLASH_CHIP1
);
502 Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO
);
503 ReturnByte
= Dataflash_ReceiveByte();
504 Dataflash_DeselectChip();
506 /* If returned data is invalid, fail the command */
507 if (ReturnByte
!= DF_MANUFACTURER_ATMEL
)
510 #if (DATAFLASH_TOTALCHIPS == 2)
511 /* Test second Dataflash IC is present and responding to commands */
512 Dataflash_SelectChip(DATAFLASH_CHIP2
);
513 Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO
);
514 ReturnByte
= Dataflash_ReceiveByte();
515 Dataflash_DeselectChip();
517 /* If returned data is invalid, fail the command */
518 if (ReturnByte
!= DF_MANUFACTURER_ATMEL
)