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 MSInterfaceInfo Pointer to a Mass Storage class state structure for the Mass Storage interface being used
47 * \param BlockAddress Data block starting address for the write sequence
48 * \param 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);
56 /* Copy selected dataflash's current page contents to the dataflash buffer */
57 Dataflash_SelectChipFromPage(CurrDFPage
);
58 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
);
59 Dataflash_SendAddressBytes(CurrDFPage
, 0);
60 Dataflash_WaitWhileBusy();
62 /* Send the dataflash buffer write command */
63 Dataflash_ToggleSelectedChipCS();
64 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
65 Dataflash_SendAddressBytes(0, CurrDFPageByte
);
67 /* Wait until endpoint is ready before continuing */
68 while (!(Endpoint_IsReadWriteAllowed()));
72 uint8_t BytesInBlockDiv16
= 0;
74 /* Write an endpoint packet sized data block to the dataflash */
75 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
77 /* Check if the endpoint is currently empty */
78 if (!(Endpoint_IsReadWriteAllowed()))
80 /* Clear the current endpoint bank */
83 /* Wait until the host has sent another packet */
84 while (!(Endpoint_IsReadWriteAllowed()));
87 /* Check if end of dataflash page reached */
88 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
90 /* Write the dataflash buffer contents back to the dataflash page */
91 Dataflash_ToggleSelectedChipCS();
92 Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE
);
93 Dataflash_SendAddressBytes(CurrDFPage
, 0);
95 /* Reset the dataflash buffer counter, increment the page counter */
96 CurrDFPageByteDiv16
= 0;
99 /* Select the next dataflash chip based on the new dataflash page index */
100 Dataflash_SelectChipFromPage(CurrDFPage
);
101 Dataflash_WaitWhileBusy();
103 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
104 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */
105 if ((TotalBlocks
* (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4)) < (DATAFLASH_PAGE_SIZE
>> 4))
107 /* Copy selected dataflash's current page contents to the dataflash buffer */
108 Dataflash_ToggleSelectedChipCS();
109 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
);
110 Dataflash_SendAddressBytes(CurrDFPage
, 0);
111 Dataflash_WaitWhileBusy();
115 /* Send the dataflash buffer write command */
116 Dataflash_ToggleSelectedChipCS();
117 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
118 Dataflash_SendAddressBytes(0, 0);
121 /* Write one 16-byte chunk of data to the dataflash */
122 Dataflash_SendByte(Endpoint_Read_Byte());
123 Dataflash_SendByte(Endpoint_Read_Byte());
124 Dataflash_SendByte(Endpoint_Read_Byte());
125 Dataflash_SendByte(Endpoint_Read_Byte());
126 Dataflash_SendByte(Endpoint_Read_Byte());
127 Dataflash_SendByte(Endpoint_Read_Byte());
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());
139 /* Increment the dataflash page 16 byte block counter */
140 CurrDFPageByteDiv16
++;
142 /* Increment the block 16 byte block counter */
145 /* Check if the current command is being aborted by the host */
146 if (MSInterfaceInfo
->State
.IsMassStoreReset
)
150 /* Decrement the blocks remaining counter and reset the sub block counter */
154 /* Write the dataflash buffer contents back to the dataflash page */
155 Dataflash_ToggleSelectedChipCS();
156 Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE
);
157 Dataflash_SendAddressBytes(CurrDFPage
, 0x00);
158 Dataflash_WaitWhileBusy();
160 /* If the endpoint is empty, clear it ready for the next packet from the host */
161 if (!(Endpoint_IsReadWriteAllowed()))
164 /* Deselect all dataflash chips */
165 Dataflash_DeselectChip();
168 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into
169 * the pre-selected data IN endpoint. This routine reads in Dataflash page sized blocks from the Dataflash
170 * and writes them in OS sized blocks to the endpoint.
172 * \param MSInterfaceInfo Pointer to a Mass Storage class state structure for the Mass Storage interface being used
173 * \param BlockAddress Data block starting address for the read sequence
174 * \param TotalBlocks Number of blocks of data to read
176 void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t
* MSInterfaceInfo
, const uint32_t BlockAddress
, uint16_t TotalBlocks
)
178 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
179 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
180 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
182 /* Send the dataflash main memory page read command */
183 Dataflash_SelectChipFromPage(CurrDFPage
);
184 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
185 Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
);
186 Dataflash_SendByte(0x00);
187 Dataflash_SendByte(0x00);
188 Dataflash_SendByte(0x00);
189 Dataflash_SendByte(0x00);
191 /* Wait until endpoint is ready before continuing */
192 while (!(Endpoint_IsReadWriteAllowed()));
196 uint8_t BytesInBlockDiv16
= 0;
198 /* Write an endpoint packet sized data block to the dataflash */
199 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
201 /* Check if the endpoint is currently full */
202 if (!(Endpoint_IsReadWriteAllowed()))
204 /* Clear the endpoint bank to send its contents to the host */
207 /* Wait until the endpoint is ready for more data */
208 while (!(Endpoint_IsReadWriteAllowed()));
211 /* Check if end of dataflash page reached */
212 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
214 /* Reset the dataflash buffer counter, increment the page counter */
215 CurrDFPageByteDiv16
= 0;
218 /* Select the next dataflash chip based on the new dataflash page index */
219 Dataflash_SelectChipFromPage(CurrDFPage
);
221 /* Send the dataflash main memory page read command */
222 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
223 Dataflash_SendAddressBytes(CurrDFPage
, 0);
224 Dataflash_SendByte(0x00);
225 Dataflash_SendByte(0x00);
226 Dataflash_SendByte(0x00);
227 Dataflash_SendByte(0x00);
230 /* Read one 16-byte chunk of data from the dataflash */
231 Endpoint_Write_Byte(Dataflash_ReceiveByte());
232 Endpoint_Write_Byte(Dataflash_ReceiveByte());
233 Endpoint_Write_Byte(Dataflash_ReceiveByte());
234 Endpoint_Write_Byte(Dataflash_ReceiveByte());
235 Endpoint_Write_Byte(Dataflash_ReceiveByte());
236 Endpoint_Write_Byte(Dataflash_ReceiveByte());
237 Endpoint_Write_Byte(Dataflash_ReceiveByte());
238 Endpoint_Write_Byte(Dataflash_ReceiveByte());
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());
248 /* Increment the dataflash page 16 byte block counter */
249 CurrDFPageByteDiv16
++;
251 /* Increment the block 16 byte block counter */
254 /* Check if the current command is being aborted by the host */
255 if (MSInterfaceInfo
->State
.IsMassStoreReset
)
259 /* Decrement the blocks remaining counter */
263 /* If the endpoint is full, send its contents to the host */
264 if (!(Endpoint_IsReadWriteAllowed()))
267 /* Deselect all dataflash chips */
268 Dataflash_DeselectChip();
271 /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board dataflash IC(s), from
272 * the a given RAM buffer. This routine reads in OS sized blocks from the buffer and writes them to the
273 * dataflash in Dataflash page sized blocks. This can be linked to FAT libraries to write files to the
276 * \param BlockAddress Data block starting address for the write sequence
277 * \param TotalBlocks Number of blocks of data to write
278 * \param BufferPtr Pointer to the data source RAM buffer
280 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
)
282 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
283 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
284 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
286 /* Copy selected dataflash's current page contents to the dataflash buffer */
287 Dataflash_SelectChipFromPage(CurrDFPage
);
288 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
);
289 Dataflash_SendAddressBytes(CurrDFPage
, 0);
290 Dataflash_WaitWhileBusy();
292 /* Send the dataflash buffer write command */
293 Dataflash_ToggleSelectedChipCS();
294 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
295 Dataflash_SendAddressBytes(0, CurrDFPageByte
);
299 uint8_t BytesInBlockDiv16
= 0;
301 /* Write an endpoint packet sized data block to the dataflash */
302 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
304 /* Check if end of dataflash page reached */
305 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
307 /* Write the dataflash buffer contents back to the dataflash page */
308 Dataflash_ToggleSelectedChipCS();
309 Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE
);
310 Dataflash_SendAddressBytes(CurrDFPage
, 0);
312 /* Reset the dataflash buffer counter, increment the page counter */
313 CurrDFPageByteDiv16
= 0;
316 /* Select the next dataflash chip based on the new dataflash page index */
317 Dataflash_SelectChipFromPage(CurrDFPage
);
318 Dataflash_WaitWhileBusy();
320 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
321 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */
322 if ((TotalBlocks
* (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4)) < (DATAFLASH_PAGE_SIZE
>> 4))
324 /* Copy selected dataflash's current page contents to the dataflash buffer */
325 Dataflash_ToggleSelectedChipCS();
326 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1
);
327 Dataflash_SendAddressBytes(CurrDFPage
, 0);
328 Dataflash_WaitWhileBusy();
332 /* Send the dataflash buffer write command */
333 Dataflash_ToggleSelectedChipCS();
334 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
335 Dataflash_SendAddressBytes(0, 0);
338 /* Write one 16-byte chunk of data to the dataflash */
339 for (uint8_t ByteNum
= 0; ByteNum
< 16; ByteNum
++)
340 Dataflash_SendByte(*(BufferPtr
++));
342 /* Increment the dataflash page 16 byte block counter */
343 CurrDFPageByteDiv16
++;
345 /* Increment the block 16 byte block counter */
349 /* Decrement the blocks remaining counter and reset the sub block counter */
353 /* Write the dataflash buffer contents back to the dataflash page */
354 Dataflash_ToggleSelectedChipCS();
355 Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE
);
356 Dataflash_SendAddressBytes(CurrDFPage
, 0x00);
357 Dataflash_WaitWhileBusy();
359 /* Deselect all dataflash chips */
360 Dataflash_DeselectChip();
363 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into
364 * the a preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash
365 * and writes them in OS sized blocks to the given buffer. This can be linked to FAT libraries to read
366 * the files stored on the dataflash.
368 * \param BlockAddress Data block starting address for the read sequence
369 * \param TotalBlocks Number of blocks of data to read
370 * \param BufferPtr Pointer to the data destination RAM buffer
372 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress
, uint16_t TotalBlocks
, uint8_t* BufferPtr
)
374 uint16_t CurrDFPage
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) / DATAFLASH_PAGE_SIZE
);
375 uint16_t CurrDFPageByte
= ((BlockAddress
* VIRTUAL_MEMORY_BLOCK_SIZE
) % DATAFLASH_PAGE_SIZE
);
376 uint8_t CurrDFPageByteDiv16
= (CurrDFPageByte
>> 4);
378 /* Send the dataflash main memory page read command */
379 Dataflash_SelectChipFromPage(CurrDFPage
);
380 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
381 Dataflash_SendAddressBytes(CurrDFPage
, CurrDFPageByte
);
382 Dataflash_SendByte(0x00);
383 Dataflash_SendByte(0x00);
384 Dataflash_SendByte(0x00);
385 Dataflash_SendByte(0x00);
389 uint8_t BytesInBlockDiv16
= 0;
391 /* Write an endpoint packet sized data block to the dataflash */
392 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
394 /* Check if end of dataflash page reached */
395 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
397 /* Reset the dataflash buffer counter, increment the page counter */
398 CurrDFPageByteDiv16
= 0;
401 /* Select the next dataflash chip based on the new dataflash page index */
402 Dataflash_SelectChipFromPage(CurrDFPage
);
404 /* Send the dataflash main memory page read command */
405 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD
);
406 Dataflash_SendAddressBytes(CurrDFPage
, 0);
407 Dataflash_SendByte(0x00);
408 Dataflash_SendByte(0x00);
409 Dataflash_SendByte(0x00);
410 Dataflash_SendByte(0x00);
413 /* Read one 16-byte chunk of data from the dataflash */
414 for (uint8_t ByteNum
= 0; ByteNum
< 16; ByteNum
++)
415 *(BufferPtr
++) = Dataflash_ReceiveByte();
417 /* Increment the dataflash page 16 byte block counter */
418 CurrDFPageByteDiv16
++;
420 /* Increment the block 16 byte block counter */
424 /* Decrement the blocks remaining counter */
428 /* Deselect all dataflash chips */
429 Dataflash_DeselectChip();
432 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
433 void DataflashManager_ResetDataflashProtections(void)
435 /* Select first dataflash chip, send the read status register command */
436 Dataflash_SelectChip(DATAFLASH_CHIP1
);
437 Dataflash_SendByte(DF_CMD_GETSTATUS
);
439 /* Check if sector protection is enabled */
440 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
)
442 Dataflash_ToggleSelectedChipCS();
444 /* Send the commands to disable sector protection */
445 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[0]);
446 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[1]);
447 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[2]);
448 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF
[3]);
451 /* Select second dataflash chip (if present on selected board), send read status register command */
452 #if (DATAFLASH_TOTALCHIPS == 2)
453 Dataflash_SelectChip(DATAFLASH_CHIP2
);
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]);
469 /* Deselect current dataflash chip */
470 Dataflash_DeselectChip();