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()));
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 while (!(Endpoint_IsReadWriteAllowed()));
91 /* Check if end of dataflash page reached */
92 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
94 /* Write the dataflash buffer contents back to the dataflash page */
95 Dataflash_WaitWhileBusy();
96 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
97 Dataflash_SendAddressBytes(CurrDFPage
, 0);
99 /* Reset the dataflash buffer counter, increment the page counter */
100 CurrDFPageByteDiv16
= 0;
103 /* Once all the dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */
104 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS
))
105 UsingSecondBuffer
= !(UsingSecondBuffer
);
107 /* Select the next dataflash chip based on the new dataflash page index */
108 Dataflash_SelectChipFromPage(CurrDFPage
);
110 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
111 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */
112 if ((TotalBlocks
* (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4)) < (DATAFLASH_PAGE_SIZE
>> 4))
114 /* Copy selected dataflash's current page contents to the dataflash buffer */
115 Dataflash_WaitWhileBusy();
116 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2
: DF_CMD_MAINMEMTOBUFF1
);
117 Dataflash_SendAddressBytes(CurrDFPage
, 0);
118 Dataflash_WaitWhileBusy();
122 /* Send the dataflash buffer write command */
123 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE
: DF_CMD_BUFF1WRITE
);
124 Dataflash_SendAddressBytes(0, 0);
127 /* Write one 16-byte chunk of data to the dataflash */
128 Dataflash_SendByte(Endpoint_Read_Byte());
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());
145 /* Increment the dataflash page 16 byte block counter */
146 CurrDFPageByteDiv16
++;
148 /* Increment the block 16 byte block counter */
151 /* Check if the current command is being aborted by the host */
152 if (MSInterfaceInfo
->State
.IsMassStoreReset
)
156 /* Decrement the blocks remaining counter and reset the sub block counter */
160 /* Write the dataflash buffer contents back to the dataflash page */
161 Dataflash_WaitWhileBusy();
162 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
163 Dataflash_SendAddressBytes(CurrDFPage
, 0x00);
164 Dataflash_WaitWhileBusy();
166 /* If the endpoint is empty, clear it ready for the next packet from the host */
167 if (!(Endpoint_IsReadWriteAllowed()))
170 /* Deselect all dataflash chips */
171 Dataflash_DeselectChip();
174 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into
175 * the pre-selected data IN endpoint. This routine reads in Dataflash page sized blocks from the Dataflash
176 * and writes them in OS sized blocks to the endpoint.
178 * \param[in] MSInterfaceInfo Pointer to a structure containing a Mass Storage Class configuration and state.
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(USB_ClassInfo_MS_Device_t
* MSInterfaceInfo
, 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 while (!(Endpoint_IsReadWriteAllowed()));
204 uint8_t BytesInBlockDiv16
= 0;
206 /* Write an endpoint packet sized data block to the dataflash */
207 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
209 /* Check if the endpoint is currently full */
210 if (!(Endpoint_IsReadWriteAllowed()))
212 /* Clear the endpoint bank to send its contents to the host */
215 /* Wait until the endpoint is ready for more data */
216 while (!(Endpoint_IsReadWriteAllowed()));
219 /* Check if end of dataflash page reached */
220 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
222 /* Reset the dataflash buffer counter, increment the page counter */
223 CurrDFPageByteDiv16
= 0;
226 /* Select the next dataflash chip based on the new dataflash page index */
227 Dataflash_SelectChipFromPage(CurrDFPage
);
229 /* Send the dataflash main memory page read command */
230 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
231 Dataflash_SendAddressBytes(CurrDFPage
, 0);
232 Dataflash_SendByte(0x00);
233 Dataflash_SendByte(0x00);
234 Dataflash_SendByte(0x00);
235 Dataflash_SendByte(0x00);
238 /* Read one 16-byte chunk of data from the dataflash */
239 Endpoint_Write_Byte(Dataflash_ReceiveByte());
240 Endpoint_Write_Byte(Dataflash_ReceiveByte());
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());
256 /* Increment the dataflash page 16 byte block counter */
257 CurrDFPageByteDiv16
++;
259 /* Increment the block 16 byte block counter */
262 /* Check if the current command is being aborted by the host */
263 if (MSInterfaceInfo
->State
.IsMassStoreReset
)
267 /* Decrement the blocks remaining counter */
271 /* If the endpoint is full, send its contents to the host */
272 if (!(Endpoint_IsReadWriteAllowed()))
275 /* Deselect all dataflash chips */
276 Dataflash_DeselectChip();
279 /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board dataflash IC(s), from
280 * the a given RAM buffer. This routine reads in OS sized blocks from the buffer and writes them to the
281 * dataflash in Dataflash page sized blocks. This can be linked to FAT libraries to write files to the
284 * \param[in] BlockAddress Data block starting address for the write sequence
285 * \param[in] TotalBlocks Number of blocks of data to write
286 * \param[in] BufferPtr Pointer to the data source RAM buffer
288 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
)
290 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
291 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
292 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
293 bool UsingSecondBuffer
= false;
295 /* Select the correct starting Dataflash IC for the block requested */
296 Dataflash_SelectChipFromPage(CurrDFPage
);
298 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
299 /* Copy selected dataflash's current page contents to the dataflash buffer */
300 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
);
301 Dataflash_SendAddressBytes(CurrDFPage
, 0);
302 Dataflash_WaitWhileBusy();
305 /* Send the dataflash buffer write command */
306 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
307 Dataflash_SendAddressBytes(0, CurrDFPageByte
);
311 uint8_t BytesInBlockDiv16
= 0;
313 /* Write an endpoint packet sized data block to the dataflash */
314 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
316 /* Check if end of dataflash page reached */
317 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
319 /* Write the dataflash buffer contents back to the dataflash page */
320 Dataflash_WaitWhileBusy();
321 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
322 Dataflash_SendAddressBytes(CurrDFPage
, 0);
324 /* Reset the dataflash buffer counter, increment the page counter */
325 CurrDFPageByteDiv16
= 0;
328 /* Once all the dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */
329 if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS
))
330 UsingSecondBuffer
= !(UsingSecondBuffer
);
332 /* Select the next dataflash chip based on the new dataflash page index */
333 Dataflash_SelectChipFromPage(CurrDFPage
);
335 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
336 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */
337 if ((TotalBlocks
* (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4)) < (DATAFLASH_PAGE_SIZE
>> 4))
339 /* Copy selected dataflash's current page contents to the dataflash buffer */
340 Dataflash_WaitWhileBusy();
341 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2
: DF_CMD_MAINMEMTOBUFF1
);
342 Dataflash_SendAddressBytes(CurrDFPage
, 0);
343 Dataflash_WaitWhileBusy();
347 /* Send the dataflash buffer write command */
348 Dataflash_ToggleSelectedChipCS();
349 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
350 Dataflash_SendAddressBytes(0, 0);
353 /* Write one 16-byte chunk of data to the dataflash */
354 for (uint8_t ByteNum
= 0; ByteNum
< 16; ByteNum
++)
355 Dataflash_SendByte(*(BufferPtr
++));
357 /* Increment the dataflash page 16 byte block counter */
358 CurrDFPageByteDiv16
++;
360 /* Increment the block 16 byte block counter */
364 /* Decrement the blocks remaining counter and reset the sub block counter */
368 /* Write the dataflash buffer contents back to the dataflash page */
369 Dataflash_WaitWhileBusy();
370 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE
: DF_CMD_BUFF1TOMAINMEMWITHERASE
);
371 Dataflash_SendAddressBytes(CurrDFPage
, 0x00);
372 Dataflash_WaitWhileBusy();
374 /* Deselect all dataflash chips */
375 Dataflash_DeselectChip();
378 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into
379 * the a preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash
380 * and writes them in OS sized blocks to the given buffer. This can be linked to FAT libraries to read
381 * the files stored on the dataflash.
383 * \param[in] BlockAddress Data block starting address for the read sequence
384 * \param[in] TotalBlocks Number of blocks of data to read
385 * \param[out] BufferPtr Pointer to the data destination RAM buffer
387 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
)
389 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
390 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
391 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
393 /* Select the correct starting Dataflash IC for the block requested */
394 Dataflash_SelectChipFromPage(CurrDFPage
);
396 /* Send the dataflash main memory page read command */
397 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
398 Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
);
399 Dataflash_SendByte(0x00);
400 Dataflash_SendByte(0x00);
401 Dataflash_SendByte(0x00);
402 Dataflash_SendByte(0x00);
406 uint8_t BytesInBlockDiv16
= 0;
408 /* Write an endpoint packet sized data block to the dataflash */
409 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
411 /* Check if end of dataflash page reached */
412 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
414 /* Reset the dataflash buffer counter, increment the page counter */
415 CurrDFPageByteDiv16
= 0;
418 /* Select the next dataflash chip based on the new dataflash page index */
419 Dataflash_SelectChipFromPage(CurrDFPage
);
421 /* Send the dataflash main memory page read command */
422 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
423 Dataflash_SendAddressBytes(CurrDFPage
, 0);
424 Dataflash_SendByte(0x00);
425 Dataflash_SendByte(0x00);
426 Dataflash_SendByte(0x00);
427 Dataflash_SendByte(0x00);
430 /* Read one 16-byte chunk of data from the dataflash */
431 for (uint8_t ByteNum
= 0; ByteNum
< 16; ByteNum
++)
432 *(BufferPtr
++) = Dataflash_ReceiveByte();
434 /* Increment the dataflash page 16 byte block counter */
435 CurrDFPageByteDiv16
++;
437 /* Increment the block 16 byte block counter */
441 /* Decrement the blocks remaining counter */
445 /* Deselect all dataflash chips */
446 Dataflash_DeselectChip();
449 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
450 void DataflashManager_ResetDataflashProtections(void)
452 /* Select first dataflash chip, send the read status register command */
453 Dataflash_SelectChip(DATAFLASH_CHIP1
);
454 Dataflash_SendByte(DF_CMD_GETSTATUS
);
456 /* Check if sector protection is enabled */
457 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
)
459 Dataflash_ToggleSelectedChipCS();
461 /* Send the commands to disable sector protection */
462 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]);
463 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]);
464 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]);
465 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]);
468 /* Select second dataflash chip (if present on selected board), send read status register command */
469 #if (DATAFLASH_TOTALCHIPS == 2)
470 Dataflash_SelectChip(DATAFLASH_CHIP2
);
471 Dataflash_SendByte(DF_CMD_GETSTATUS
);
473 /* Check if sector protection is enabled */
474 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
)
476 Dataflash_ToggleSelectedChipCS();
478 /* Send the commands to disable sector protection */
479 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]);
480 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]);
481 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]);
482 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]);
486 /* Deselect current dataflash chip */
487 Dataflash_DeselectChip();