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] MSInterfaceInfo Pointer to a structure containing a Mass Storage Class configuration and state.
47 * \param[in] BlockAddress Data block starting address for the write sequence
48 * \param[in] TotalBlocks Number of blocks of data to write
50 void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t
* MSInterfaceInfo
, const uint32_t BlockAddress
, uint16_t TotalBlocks
)
52 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
53 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
54 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
55 bool UsingSecondBuffer
= false;
57 /* Select the correct starting Dataflash IC for the block requested */
58 Dataflash_SelectChipFromPage(CurrDFPage
);
60 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
61 /* Copy selected dataflash's current page contents to the dataflash buffer */
62 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
);
63 Dataflash_SendAddressBytes(CurrDFPage
, 0);
64 Dataflash_WaitWhileBusy();
67 /* Send the dataflash buffer write command */
68 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
69 Dataflash_SendAddressBytes(0, CurrDFPageByte
);
71 /* Wait until endpoint is ready before continuing */
72 while (!(Endpoint_IsReadWriteAllowed()))
74 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
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 while (!(Endpoint_IsReadWriteAllowed()))
94 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
99 /* Check if end of dataflash page reached */
100 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
102 /* Write the dataflash buffer contents back to the dataflash page */
103 Dataflash_WaitWhileBusy();
104 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
105 Dataflash_SendAddressBytes(CurrDFPage
, 0);
107 /* Reset the dataflash buffer counter, increment the page counter */
108 CurrDFPageByteDiv16
= 0;
111 /* Once all the dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */
112 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS
))
113 UsingSecondBuffer
= !(UsingSecondBuffer
);
115 /* Select the next dataflash chip based on the new dataflash page index */
116 Dataflash_SelectChipFromPage(CurrDFPage
);
118 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
119 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */
120 if ((TotalBlocks
* (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4)) < (DATAFLASH_PAGE_SIZE
>> 4))
122 /* Copy selected dataflash's current page contents to the dataflash buffer */
123 Dataflash_WaitWhileBusy();
124 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2
: DF_CMD_MAINMEMTOBUFF1
);
125 Dataflash_SendAddressBytes(CurrDFPage
, 0);
126 Dataflash_WaitWhileBusy();
130 /* Send the dataflash buffer write command */
131 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE
: DF_CMD_BUFF1WRITE
);
132 Dataflash_SendAddressBytes(0, 0);
135 /* Write one 16-byte chunk of data to the dataflash */
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());
151 Dataflash_SendByte(Endpoint_Read_Byte());
153 /* Increment the dataflash page 16 byte block counter */
154 CurrDFPageByteDiv16
++;
156 /* Increment the block 16 byte block counter */
159 /* Check if the current command is being aborted by the host */
160 if (MSInterfaceInfo
->State
.IsMassStoreReset
)
164 /* Decrement the blocks remaining counter and reset the sub block counter */
168 /* Write the dataflash buffer contents back to the dataflash page */
169 Dataflash_WaitWhileBusy();
170 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
171 Dataflash_SendAddressBytes(CurrDFPage
, 0x00);
172 Dataflash_WaitWhileBusy();
174 /* If the endpoint is empty, clear it ready for the next packet from the host */
175 if (!(Endpoint_IsReadWriteAllowed()))
178 /* Deselect all dataflash chips */
179 Dataflash_DeselectChip();
182 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into
183 * the pre-selected data IN endpoint. This routine reads in Dataflash page sized blocks from the Dataflash
184 * and writes them in OS sized blocks to the endpoint.
186 * \param[in] MSInterfaceInfo Pointer to a structure containing a Mass Storage Class configuration and state.
187 * \param[in] BlockAddress Data block starting address for the read sequence
188 * \param[in] TotalBlocks Number of blocks of data to read
190 void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t
* MSInterfaceInfo
, const uint32_t BlockAddress
, uint16_t TotalBlocks
)
192 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
193 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
194 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
196 /* Select the correct starting Dataflash IC for the block requested */
197 Dataflash_SelectChipFromPage(CurrDFPage
);
199 /* Send the dataflash main memory page read command */
200 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
201 Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
);
202 Dataflash_SendByte(0x00);
203 Dataflash_SendByte(0x00);
204 Dataflash_SendByte(0x00);
205 Dataflash_SendByte(0x00);
207 /* Wait until endpoint is ready before continuing */
208 while (!(Endpoint_IsReadWriteAllowed()))
210 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
216 uint8_t BytesInBlockDiv16
= 0;
218 /* Write an endpoint packet sized data block to the dataflash */
219 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
221 /* Check if the endpoint is currently full */
222 if (!(Endpoint_IsReadWriteAllowed()))
224 /* Clear the endpoint bank to send its contents to the host */
227 /* Wait until the endpoint is ready for more data */
228 while (!(Endpoint_IsReadWriteAllowed()))
230 if (USB_DeviceState
== DEVICE_STATE_Unattached
)
235 /* Check if end of dataflash page reached */
236 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
238 /* Reset the dataflash buffer counter, increment the page counter */
239 CurrDFPageByteDiv16
= 0;
242 /* Select the next dataflash chip based on the new dataflash page index */
243 Dataflash_SelectChipFromPage(CurrDFPage
);
245 /* Send the dataflash main memory page read command */
246 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
247 Dataflash_SendAddressBytes(CurrDFPage
, 0);
248 Dataflash_SendByte(0x00);
249 Dataflash_SendByte(0x00);
250 Dataflash_SendByte(0x00);
251 Dataflash_SendByte(0x00);
254 /* Read one 16-byte chunk of data from the dataflash */
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());
269 Endpoint_Write_Byte(Dataflash_ReceiveByte());
270 Endpoint_Write_Byte(Dataflash_ReceiveByte());
272 /* Increment the dataflash page 16 byte block counter */
273 CurrDFPageByteDiv16
++;
275 /* Increment the block 16 byte block counter */
278 /* Check if the current command is being aborted by the host */
279 if (MSInterfaceInfo
->State
.IsMassStoreReset
)
283 /* Decrement the blocks remaining counter */
287 /* If the endpoint is full, send its contents to the host */
288 if (!(Endpoint_IsReadWriteAllowed()))
291 /* Deselect all dataflash chips */
292 Dataflash_DeselectChip();
295 /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board dataflash IC(s), from
296 * the a given RAM buffer. This routine reads in OS sized blocks from the buffer and writes them to the
297 * dataflash in Dataflash page sized blocks. This can be linked to FAT libraries to write files to the
300 * \param[in] BlockAddress Data block starting address for the write sequence
301 * \param[in] TotalBlocks Number of blocks of data to write
302 * \param[in] BufferPtr Pointer to the data source RAM buffer
304 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
)
306 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
307 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
308 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
309 bool UsingSecondBuffer
= false;
311 /* Select the correct starting Dataflash IC for the block requested */
312 Dataflash_SelectChipFromPage(CurrDFPage
);
314 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
315 /* Copy selected dataflash's current page contents to the dataflash buffer */
316 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
);
317 Dataflash_SendAddressBytes(CurrDFPage
, 0);
318 Dataflash_WaitWhileBusy();
321 /* Send the dataflash buffer write command */
322 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
323 Dataflash_SendAddressBytes(0, CurrDFPageByte
);
327 uint8_t BytesInBlockDiv16
= 0;
329 /* Write an endpoint packet sized data block to the dataflash */
330 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
332 /* Check if end of dataflash page reached */
333 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
335 /* Write the dataflash buffer contents back to the dataflash page */
336 Dataflash_WaitWhileBusy();
337 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
338 Dataflash_SendAddressBytes(CurrDFPage
, 0);
340 /* Reset the dataflash buffer counter, increment the page counter */
341 CurrDFPageByteDiv16
= 0;
344 /* Once all the dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */
345 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS
))
346 UsingSecondBuffer
= !(UsingSecondBuffer
);
348 /* Select the next dataflash chip based on the new dataflash page index */
349 Dataflash_SelectChipFromPage(CurrDFPage
);
351 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
352 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */
353 if ((TotalBlocks
* (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4)) < (DATAFLASH_PAGE_SIZE
>> 4))
355 /* Copy selected dataflash's current page contents to the dataflash buffer */
356 Dataflash_WaitWhileBusy();
357 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2
: DF_CMD_MAINMEMTOBUFF1
);
358 Dataflash_SendAddressBytes(CurrDFPage
, 0);
359 Dataflash_WaitWhileBusy();
363 /* Send the dataflash buffer write command */
364 Dataflash_ToggleSelectedChipCS();
365 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
366 Dataflash_SendAddressBytes(0, 0);
369 /* Write one 16-byte chunk of data to the dataflash */
370 for (uint8_t ByteNum
= 0; ByteNum
< 16; ByteNum
++)
371 Dataflash_SendByte(*(BufferPtr
++));
373 /* Increment the dataflash page 16 byte block counter */
374 CurrDFPageByteDiv16
++;
376 /* Increment the block 16 byte block counter */
380 /* Decrement the blocks remaining counter and reset the sub block counter */
384 /* Write the dataflash buffer contents back to the dataflash page */
385 Dataflash_WaitWhileBusy();
386 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
387 Dataflash_SendAddressBytes(CurrDFPage
, 0x00);
388 Dataflash_WaitWhileBusy();
390 /* Deselect all dataflash chips */
391 Dataflash_DeselectChip();
394 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into
395 * the a preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash
396 * and writes them in OS sized blocks to the given buffer. This can be linked to FAT libraries to read
397 * the files stored on the dataflash.
399 * \param[in] BlockAddress Data block starting address for the read sequence
400 * \param[in] TotalBlocks Number of blocks of data to read
401 * \param[out] BufferPtr Pointer to the data destination RAM buffer
403 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
)
405 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
406 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
407 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
409 /* Select the correct starting Dataflash IC for the block requested */
410 Dataflash_SelectChipFromPage(CurrDFPage
);
412 /* Send the dataflash main memory page read command */
413 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
414 Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
);
415 Dataflash_SendByte(0x00);
416 Dataflash_SendByte(0x00);
417 Dataflash_SendByte(0x00);
418 Dataflash_SendByte(0x00);
422 uint8_t BytesInBlockDiv16
= 0;
424 /* Write an endpoint packet sized data block to the dataflash */
425 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
427 /* Check if end of dataflash page reached */
428 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
430 /* Reset the dataflash buffer counter, increment the page counter */
431 CurrDFPageByteDiv16
= 0;
434 /* Select the next dataflash chip based on the new dataflash page index */
435 Dataflash_SelectChipFromPage(CurrDFPage
);
437 /* Send the dataflash main memory page read command */
438 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
439 Dataflash_SendAddressBytes(CurrDFPage
, 0);
440 Dataflash_SendByte(0x00);
441 Dataflash_SendByte(0x00);
442 Dataflash_SendByte(0x00);
443 Dataflash_SendByte(0x00);
446 /* Read one 16-byte chunk of data from the dataflash */
447 for (uint8_t ByteNum
= 0; ByteNum
< 16; ByteNum
++)
448 *(BufferPtr
++) = Dataflash_ReceiveByte();
450 /* Increment the dataflash page 16 byte block counter */
451 CurrDFPageByteDiv16
++;
453 /* Increment the block 16 byte block counter */
457 /* Decrement the blocks remaining counter */
461 /* Deselect all dataflash chips */
462 Dataflash_DeselectChip();
465 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
466 void DataflashManager_ResetDataflashProtections(void)
468 /* Select first dataflash chip, send the read status register command */
469 Dataflash_SelectChip(DATAFLASH_CHIP1
);
470 Dataflash_SendByte(DF_CMD_GETSTATUS
);
472 /* Check if sector protection is enabled */
473 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
)
475 Dataflash_ToggleSelectedChipCS();
477 /* Send the commands to disable sector protection */
478 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]);
479 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]);
480 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]);
481 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]);
484 /* Select second dataflash chip (if present on selected board), send read status register command */
485 #if (DATAFLASH_TOTALCHIPS == 2)
486 Dataflash_SelectChip(DATAFLASH_CHIP2
);
487 Dataflash_SendByte(DF_CMD_GETSTATUS
);
489 /* Check if sector protection is enabled */
490 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
)
492 Dataflash_ToggleSelectedChipCS();
494 /* Send the commands to disable sector protection */
495 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]);
496 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]);
497 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]);
498 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]);
502 /* Deselect current dataflash chip */
503 Dataflash_DeselectChip();