3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 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 #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.
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
51 void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
,
52 const uint32_t BlockAddress
,
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;
60 /* Select the correct starting Dataflash IC for the block requested */
61 Dataflash_SelectChipFromPage(CurrDFPage
);
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();
70 /* Send the Dataflash buffer write command */
71 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
72 Dataflash_SendAddressBytes(0, CurrDFPageByte
);
74 /* Wait until endpoint is ready before continuing */
75 if (Endpoint_WaitUntilReady())
80 uint8_t BytesInBlockDiv16
= 0;
82 /* Write an endpoint packet sized data block to the Dataflash */
83 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
85 /* Check if the endpoint is currently empty */
86 if (!(Endpoint_IsReadWriteAllowed()))
88 /* Clear the current endpoint bank */
91 /* Wait until the host has sent another packet */
92 if (Endpoint_WaitUntilReady())
96 /* Check if end of Dataflash page reached */
97 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
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);
104 /* Reset the Dataflash buffer counter, increment the page counter */
105 CurrDFPageByteDiv16
= 0;
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
);
112 /* Select the next Dataflash chip based on the new Dataflash page index */
113 Dataflash_SelectChipFromPage(CurrDFPage
);
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))
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();
127 /* Send the Dataflash buffer write command */
128 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE
: DF_CMD_BUFF1WRITE
);
129 Dataflash_SendAddressBytes(0, 0);
132 /* Write one 16-byte chunk of data to the Dataflash */
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 Dataflash_SendByte(Endpoint_Read_Byte());
148 Dataflash_SendByte(Endpoint_Read_Byte());
150 /* Increment the Dataflash page 16 byte block counter */
151 CurrDFPageByteDiv16
++;
153 /* Increment the block 16 byte block counter */
156 /* Check if the current command is being aborted by the host */
157 if (MSInterfaceInfo
->State
.IsMassStoreReset
)
161 /* Decrement the blocks remaining counter and reset the sub block counter */
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();
171 /* If the endpoint is empty, clear it ready for the next packet from the host */
172 if (!(Endpoint_IsReadWriteAllowed()))
175 /* Deselect all Dataflash chips */
176 Dataflash_DeselectChip();
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.
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
187 void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
,
188 const uint32_t BlockAddress
,
189 uint16_t TotalBlocks
)
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);
195 /* Select the correct starting Dataflash IC for the block requested */
196 Dataflash_SelectChipFromPage(CurrDFPage
);
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);
206 /* Wait until endpoint is ready before continuing */
207 if (Endpoint_WaitUntilReady())
212 uint8_t BytesInBlockDiv16
= 0;
214 /* Write an endpoint packet sized data block to the Dataflash */
215 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
217 /* Check if the endpoint is currently full */
218 if (!(Endpoint_IsReadWriteAllowed()))
220 /* Clear the endpoint bank to send its contents to the host */
223 /* Wait until the endpoint is ready for more data */
224 if (Endpoint_WaitUntilReady())
228 /* Check if end of Dataflash page reached */
229 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
231 /* Reset the Dataflash buffer counter, increment the page counter */
232 CurrDFPageByteDiv16
= 0;
235 /* Select the next Dataflash chip based on the new Dataflash page index */
236 Dataflash_SelectChipFromPage(CurrDFPage
);
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);
247 /* Read one 16-byte chunk of data from the Dataflash */
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 Endpoint_Write_Byte(Dataflash_ReceiveByte());
261 Endpoint_Write_Byte(Dataflash_ReceiveByte());
262 Endpoint_Write_Byte(Dataflash_ReceiveByte());
263 Endpoint_Write_Byte(Dataflash_ReceiveByte());
265 /* Increment the Dataflash page 16 byte block counter */
266 CurrDFPageByteDiv16
++;
268 /* Increment the block 16 byte block counter */
271 /* Check if the current command is being aborted by the host */
272 if (MSInterfaceInfo
->State
.IsMassStoreReset
)
276 /* Decrement the blocks remaining counter */
280 /* If the endpoint is full, send its contents to the host */
281 if (!(Endpoint_IsReadWriteAllowed()))
284 /* Deselect all Dataflash chips */
285 Dataflash_DeselectChip();
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
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
297 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress
,
298 uint16_t TotalBlocks
,
299 const uint8_t* BufferPtr
)
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;
306 /* Select the correct starting Dataflash IC for the block requested */
307 Dataflash_SelectChipFromPage(CurrDFPage
);
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();
316 /* Send the Dataflash buffer write command */
317 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
318 Dataflash_SendAddressBytes(0, CurrDFPageByte
);
322 uint8_t BytesInBlockDiv16
= 0;
324 /* Write an endpoint packet sized data block to the Dataflash */
325 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
327 /* Check if end of Dataflash page reached */
328 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
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);
335 /* Reset the Dataflash buffer counter, increment the page counter */
336 CurrDFPageByteDiv16
= 0;
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
);
343 /* Select the next Dataflash chip based on the new Dataflash page index */
344 Dataflash_SelectChipFromPage(CurrDFPage
);
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))
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();
358 /* Send the Dataflash buffer write command */
359 Dataflash_ToggleSelectedChipCS();
360 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
361 Dataflash_SendAddressBytes(0, 0);
364 /* Write one 16-byte chunk of data to the Dataflash */
365 for (uint8_t ByteNum
= 0; ByteNum
< 16; ByteNum
++)
366 Dataflash_SendByte(*(BufferPtr
++));
368 /* Increment the Dataflash page 16 byte block counter */
369 CurrDFPageByteDiv16
++;
371 /* Increment the block 16 byte block counter */
375 /* Decrement the blocks remaining counter and reset the sub block counter */
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();
385 /* Deselect all Dataflash chips */
386 Dataflash_DeselectChip();
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.
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
398 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress
,
399 uint16_t TotalBlocks
,
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);
406 /* Select the correct starting Dataflash IC for the block requested */
407 Dataflash_SelectChipFromPage(CurrDFPage
);
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);
419 uint8_t BytesInBlockDiv16
= 0;
421 /* Write an endpoint packet sized data block to the Dataflash */
422 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
424 /* Check if end of Dataflash page reached */
425 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
427 /* Reset the Dataflash buffer counter, increment the page counter */
428 CurrDFPageByteDiv16
= 0;
431 /* Select the next Dataflash chip based on the new Dataflash page index */
432 Dataflash_SelectChipFromPage(CurrDFPage
);
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);
443 /* Read one 16-byte chunk of data from the Dataflash */
444 for (uint8_t ByteNum
= 0; ByteNum
< 16; ByteNum
++)
445 *(BufferPtr
++) = Dataflash_ReceiveByte();
447 /* Increment the Dataflash page 16 byte block counter */
448 CurrDFPageByteDiv16
++;
450 /* Increment the block 16 byte block counter */
454 /* Decrement the blocks remaining counter */
458 /* Deselect all Dataflash chips */
459 Dataflash_DeselectChip();
462 /** Disables the Dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
463 void DataflashManager_ResetDataflashProtections(void)
465 /* Select first Dataflash chip, send the read status register command */
466 Dataflash_SelectChip(DATAFLASH_CHIP1
);
467 Dataflash_SendByte(DF_CMD_GETSTATUS
);
469 /* Check if sector protection is enabled */
470 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
)
472 Dataflash_ToggleSelectedChipCS();
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]);
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
);
486 /* Check if sector protection is enabled */
487 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
)
489 Dataflash_ToggleSelectedChipCS();
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]);
499 /* Deselect current Dataflash chip */
500 Dataflash_DeselectChip();
503 /** Performs a simple test on the attached Dataflash IC(s) to ensure that they are working.
505 * \return Boolean true if all media chips are working, false otherwise
507 bool DataflashManager_CheckDataflashOperation(void)
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();
517 /* If returned data is invalid, fail the command */
518 if (ReturnByte
!= DF_MANUFACTURER_ATMEL
)
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();
528 /* If returned data is invalid, fail the command */
529 if (ReturnByte
!= DF_MANUFACTURER_ATMEL
)