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, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 while (!(Endpoint_IsReadWriteAllowed()))
73 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
79 uint8_t BytesInBlockDiv16
= 0;
81 /* Write an endpoint packet sized data block to the dataflash */
82 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
84 /* Check if the endpoint is currently empty */
85 if (!(Endpoint_IsReadWriteAllowed()))
87 /* Clear the current endpoint bank */
90 /* Wait until the host has sent another packet */
91 while (!(Endpoint_IsReadWriteAllowed()))
93 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
98 /* Check if end of dataflash page reached */
99 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
101 /* Write the dataflash buffer contents back to the dataflash page */
102 Dataflash_WaitWhileBusy();
103 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
104 Dataflash_SendAddressBytes(CurrDFPage
, 0);
106 /* Reset the dataflash buffer counter, increment the page counter */
107 CurrDFPageByteDiv16
= 0;
110 /* Once all the dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */
111 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS
))
112 UsingSecondBuffer
= !(UsingSecondBuffer
);
114 /* Select the next dataflash chip based on the new dataflash page index */
115 Dataflash_SelectChipFromPage(CurrDFPage
);
117 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
118 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */
119 if ((TotalBlocks
* (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4)) < (DATAFLASH_PAGE_SIZE
>> 4))
121 /* Copy selected dataflash's current page contents to the dataflash buffer */
122 Dataflash_WaitWhileBusy();
123 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2
: DF_CMD_MAINMEMTOBUFF1
);
124 Dataflash_SendAddressBytes(CurrDFPage
, 0);
125 Dataflash_WaitWhileBusy();
129 /* Send the dataflash buffer write command */
130 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE
: DF_CMD_BUFF1WRITE
);
131 Dataflash_SendAddressBytes(0, 0);
134 /* Write one 16-byte chunk of data to the dataflash */
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());
149 Dataflash_SendByte(Endpoint_Read_Byte());
150 Dataflash_SendByte(Endpoint_Read_Byte());
152 /* Increment the dataflash page 16 byte block counter */
153 CurrDFPageByteDiv16
++;
155 /* Increment the block 16 byte block counter */
158 /* Check if the current command is being aborted by the host */
159 if (IsMassStoreReset
)
163 /* Decrement the blocks remaining counter and reset the sub block counter */
167 /* Write the dataflash buffer contents back to the dataflash page */
168 Dataflash_WaitWhileBusy();
169 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
170 Dataflash_SendAddressBytes(CurrDFPage
, 0x00);
171 Dataflash_WaitWhileBusy();
173 /* If the endpoint is empty, clear it ready for the next packet from the host */
174 if (!(Endpoint_IsReadWriteAllowed()))
177 /* Deselect all dataflash chips */
178 Dataflash_DeselectChip();
181 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into
182 * the pre-selected data IN endpoint. This routine reads in Dataflash page sized blocks from the Dataflash
183 * and writes them in OS sized blocks to the endpoint.
185 * \param[in] BlockAddress Data block starting address for the read sequence
186 * \param[in] TotalBlocks Number of blocks of data to read
188 void DataflashManager_ReadBlocks(const uint32_t BlockAddress
, uint16_t TotalBlocks
)
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);
194 /* Select the correct starting Dataflash IC for the block requested */
195 Dataflash_SelectChipFromPage(CurrDFPage
);
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);
205 /* Wait until endpoint is ready before continuing */
206 while (!(Endpoint_IsReadWriteAllowed()))
208 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
214 uint8_t BytesInBlockDiv16
= 0;
216 /* Write an endpoint packet sized data block to the dataflash */
217 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
219 /* Check if the endpoint is currently full */
220 if (!(Endpoint_IsReadWriteAllowed()))
222 /* Clear the endpoint bank to send its contents to the host */
225 /* Wait until the endpoint is ready for more data */
226 while (!(Endpoint_IsReadWriteAllowed()))
228 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
233 /* Check if end of dataflash page reached */
234 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
236 /* Reset the dataflash buffer counter, increment the page counter */
237 CurrDFPageByteDiv16
= 0;
240 /* Select the next dataflash chip based on the new dataflash page index */
241 Dataflash_SelectChipFromPage(CurrDFPage
);
243 /* Send the dataflash main memory page read command */
244 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
245 Dataflash_SendAddressBytes(CurrDFPage
, 0);
246 Dataflash_SendByte(0x00);
247 Dataflash_SendByte(0x00);
248 Dataflash_SendByte(0x00);
249 Dataflash_SendByte(0x00);
252 /* Read one 16-byte chunk of data from the dataflash */
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());
264 Endpoint_Write_Byte(Dataflash_ReceiveByte());
265 Endpoint_Write_Byte(Dataflash_ReceiveByte());
266 Endpoint_Write_Byte(Dataflash_ReceiveByte());
267 Endpoint_Write_Byte(Dataflash_ReceiveByte());
268 Endpoint_Write_Byte(Dataflash_ReceiveByte());
270 /* Increment the dataflash page 16 byte block counter */
271 CurrDFPageByteDiv16
++;
273 /* Increment the block 16 byte block counter */
276 /* Check if the current command is being aborted by the host */
277 if (IsMassStoreReset
)
281 /* Decrement the blocks remaining counter */
285 /* If the endpoint is full, send its contents to the host */
286 if (!(Endpoint_IsReadWriteAllowed()))
289 /* Deselect all dataflash chips */
290 Dataflash_DeselectChip();
293 /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board dataflash IC(s), from
294 * the a given RAM buffer. This routine reads in OS sized blocks from the buffer and writes them to the
295 * dataflash in Dataflash page sized blocks. This can be linked to FAT libraries to write files to the
298 * \param[in] BlockAddress Data block starting address for the write sequence
299 * \param[in] TotalBlocks Number of blocks of data to write
300 * \param[in] BufferPtr Pointer to the data source RAM buffer
302 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
)
304 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
305 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
306 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
307 bool UsingSecondBuffer
= false;
309 /* Select the correct starting Dataflash IC for the block requested */
310 Dataflash_SelectChipFromPage(CurrDFPage
);
312 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
313 /* Copy selected dataflash's current page contents to the dataflash buffer */
314 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
);
315 Dataflash_SendAddressBytes(CurrDFPage
, 0);
316 Dataflash_WaitWhileBusy();
319 /* Send the dataflash buffer write command */
320 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
321 Dataflash_SendAddressBytes(0, CurrDFPageByte
);
325 uint8_t BytesInBlockDiv16
= 0;
327 /* Write an endpoint packet sized data block to the dataflash */
328 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
330 /* Check if end of dataflash page reached */
331 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
333 /* Write the dataflash buffer contents back to the dataflash page */
334 Dataflash_WaitWhileBusy();
335 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
336 Dataflash_SendAddressBytes(CurrDFPage
, 0);
338 /* Reset the dataflash buffer counter, increment the page counter */
339 CurrDFPageByteDiv16
= 0;
342 /* Once all the dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */
343 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS
))
344 UsingSecondBuffer
= !(UsingSecondBuffer
);
346 /* Select the next dataflash chip based on the new dataflash page index */
347 Dataflash_SelectChipFromPage(CurrDFPage
);
349 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
350 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */
351 if ((TotalBlocks
* (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4)) < (DATAFLASH_PAGE_SIZE
>> 4))
353 /* Copy selected dataflash's current page contents to the dataflash buffer */
354 Dataflash_WaitWhileBusy();
355 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2
: DF_CMD_MAINMEMTOBUFF1
);
356 Dataflash_SendAddressBytes(CurrDFPage
, 0);
357 Dataflash_WaitWhileBusy();
361 /* Send the dataflash buffer write command */
362 Dataflash_ToggleSelectedChipCS();
363 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
364 Dataflash_SendAddressBytes(0, 0);
367 /* Write one 16-byte chunk of data to the dataflash */
368 for (uint8_t ByteNum
= 0; ByteNum
< 16; ByteNum
++)
369 Dataflash_SendByte(*(BufferPtr
++));
371 /* Increment the dataflash page 16 byte block counter */
372 CurrDFPageByteDiv16
++;
374 /* Increment the block 16 byte block counter */
378 /* Decrement the blocks remaining counter and reset the sub block counter */
382 /* Write the dataflash buffer contents back to the dataflash page */
383 Dataflash_WaitWhileBusy();
384 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
385 Dataflash_SendAddressBytes(CurrDFPage
, 0x00);
386 Dataflash_WaitWhileBusy();
388 /* Deselect all dataflash chips */
389 Dataflash_DeselectChip();
392 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into
393 * the a preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash
394 * and writes them in OS sized blocks to the given buffer. This can be linked to FAT libraries to read
395 * the files stored on the dataflash.
397 * \param[in] BlockAddress Data block starting address for the read sequence
398 * \param[in] TotalBlocks Number of blocks of data to read
399 * \param[out] BufferPtr Pointer to the data destination RAM buffer
401 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
)
403 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
404 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
405 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
407 /* Select the correct starting Dataflash IC for the block requested */
408 Dataflash_SelectChipFromPage(CurrDFPage
);
410 /* Send the dataflash main memory page read command */
411 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
412 Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
);
413 Dataflash_SendByte(0x00);
414 Dataflash_SendByte(0x00);
415 Dataflash_SendByte(0x00);
416 Dataflash_SendByte(0x00);
420 uint8_t BytesInBlockDiv16
= 0;
422 /* Write an endpoint packet sized data block to the dataflash */
423 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
425 /* Check if end of dataflash page reached */
426 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
428 /* Reset the dataflash buffer counter, increment the page counter */
429 CurrDFPageByteDiv16
= 0;
432 /* Select the next dataflash chip based on the new dataflash page index */
433 Dataflash_SelectChipFromPage(CurrDFPage
);
435 /* Send the dataflash main memory page read command */
436 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
437 Dataflash_SendAddressBytes(CurrDFPage
, 0);
438 Dataflash_SendByte(0x00);
439 Dataflash_SendByte(0x00);
440 Dataflash_SendByte(0x00);
441 Dataflash_SendByte(0x00);
444 /* Read one 16-byte chunk of data from the dataflash */
445 for (uint8_t ByteNum
= 0; ByteNum
< 16; ByteNum
++)
446 *(BufferPtr
++) = Dataflash_ReceiveByte();
448 /* Increment the dataflash page 16 byte block counter */
449 CurrDFPageByteDiv16
++;
451 /* Increment the block 16 byte block counter */
455 /* Decrement the blocks remaining counter */
459 /* Deselect all dataflash chips */
460 Dataflash_DeselectChip();
463 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
464 void DataflashManager_ResetDataflashProtections(void)
466 /* Select first dataflash chip, send the read status register command */
467 Dataflash_SelectChip(DATAFLASH_CHIP1
);
468 Dataflash_SendByte(DF_CMD_GETSTATUS
);
470 /* Check if sector protection is enabled */
471 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
)
473 Dataflash_ToggleSelectedChipCS();
475 /* Send the commands to disable sector protection */
476 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]);
477 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]);
478 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]);
479 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]);
482 /* Select second dataflash chip (if present on selected board), send read status register command */
483 #if (DATAFLASH_TOTALCHIPS == 2)
484 Dataflash_SelectChip(DATAFLASH_CHIP2
);
485 Dataflash_SendByte(DF_CMD_GETSTATUS
);
487 /* Check if sector protection is enabled */
488 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
)
490 Dataflash_ToggleSelectedChipCS();
492 /* Send the commands to disable sector protection */
493 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]);
494 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]);
495 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]);
496 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]);
500 /* Deselect current dataflash chip */
501 Dataflash_DeselectChip();