1c584e71c3bec831c345a15aa7f2331121f92c64
[pub/USBasp.git] / Demos / Device / ClassDriver / MassStorage / Lib / DataflashManager.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
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.
37 */
38
39 #define INCLUDE_FROM_DATAFLASHMANAGER_C
40 #include "DataflashManager.h"
41
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.
45 *
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
49 */
50 void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, const uint32_t BlockAddress, uint16_t TotalBlocks)
51 {
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;
56
57 /* Select the correct starting Dataflash IC for the block requested */
58 Dataflash_SelectChipFromPage(CurrDFPage);
59
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();
65 #endif
66
67 /* Send the dataflash buffer write command */
68 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
69 Dataflash_SendAddressBytes(0, CurrDFPageByte);
70
71 /* Wait until endpoint is ready before continuing */
72 while (!(Endpoint_IsReadWriteAllowed()));
73
74 while (TotalBlocks)
75 {
76 uint8_t BytesInBlockDiv16 = 0;
77
78 /* Write an endpoint packet sized data block to the dataflash */
79 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
80 {
81 /* Check if the endpoint is currently empty */
82 if (!(Endpoint_IsReadWriteAllowed()))
83 {
84 /* Clear the current endpoint bank */
85 Endpoint_ClearOUT();
86
87 /* Wait until the host has sent another packet */
88 while (!(Endpoint_IsReadWriteAllowed()));
89 }
90
91 /* Check if end of dataflash page reached */
92 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
93 {
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);
98
99 /* Reset the dataflash buffer counter, increment the page counter */
100 CurrDFPageByteDiv16 = 0;
101 CurrDFPage++;
102
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);
106
107 /* Select the next dataflash chip based on the new dataflash page index */
108 Dataflash_SelectChipFromPage(CurrDFPage);
109
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))
113 {
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();
119 }
120 #endif
121
122 /* Send the dataflash buffer write command */
123 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE : DF_CMD_BUFF1WRITE);
124 Dataflash_SendAddressBytes(0, 0);
125 }
126
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());
144
145 /* Increment the dataflash page 16 byte block counter */
146 CurrDFPageByteDiv16++;
147
148 /* Increment the block 16 byte block counter */
149 BytesInBlockDiv16++;
150
151 /* Check if the current command is being aborted by the host */
152 if (MSInterfaceInfo->State.IsMassStoreReset)
153 return;
154 }
155
156 /* Decrement the blocks remaining counter and reset the sub block counter */
157 TotalBlocks--;
158 }
159
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();
165
166 /* If the endpoint is empty, clear it ready for the next packet from the host */
167 if (!(Endpoint_IsReadWriteAllowed()))
168 Endpoint_ClearOUT();
169
170 /* Deselect all dataflash chips */
171 Dataflash_DeselectChip();
172 }
173
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.
177 *
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
181 */
182 void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, const uint32_t BlockAddress, uint16_t TotalBlocks)
183 {
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);
187
188 /* Select the correct starting Dataflash IC for the block requested */
189 Dataflash_SelectChipFromPage(CurrDFPage);
190
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);
198
199 /* Wait until endpoint is ready before continuing */
200 while (!(Endpoint_IsReadWriteAllowed()));
201
202 while (TotalBlocks)
203 {
204 uint8_t BytesInBlockDiv16 = 0;
205
206 /* Write an endpoint packet sized data block to the dataflash */
207 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
208 {
209 /* Check if the endpoint is currently full */
210 if (!(Endpoint_IsReadWriteAllowed()))
211 {
212 /* Clear the endpoint bank to send its contents to the host */
213 Endpoint_ClearIN();
214
215 /* Wait until the endpoint is ready for more data */
216 while (!(Endpoint_IsReadWriteAllowed()));
217 }
218
219 /* Check if end of dataflash page reached */
220 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
221 {
222 /* Reset the dataflash buffer counter, increment the page counter */
223 CurrDFPageByteDiv16 = 0;
224 CurrDFPage++;
225
226 /* Select the next dataflash chip based on the new dataflash page index */
227 Dataflash_SelectChipFromPage(CurrDFPage);
228
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);
236 }
237
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());
255
256 /* Increment the dataflash page 16 byte block counter */
257 CurrDFPageByteDiv16++;
258
259 /* Increment the block 16 byte block counter */
260 BytesInBlockDiv16++;
261
262 /* Check if the current command is being aborted by the host */
263 if (MSInterfaceInfo->State.IsMassStoreReset)
264 return;
265 }
266
267 /* Decrement the blocks remaining counter */
268 TotalBlocks--;
269 }
270
271 /* If the endpoint is full, send its contents to the host */
272 if (!(Endpoint_IsReadWriteAllowed()))
273 Endpoint_ClearIN();
274
275 /* Deselect all dataflash chips */
276 Dataflash_DeselectChip();
277 }
278
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
282 * dataflash.
283 *
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
287 */
288 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress, uint16_t TotalBlocks, uint8_t* BufferPtr)
289 {
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;
294
295 /* Select the correct starting Dataflash IC for the block requested */
296 Dataflash_SelectChipFromPage(CurrDFPage);
297
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();
303 #endif
304
305 /* Send the dataflash buffer write command */
306 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
307 Dataflash_SendAddressBytes(0, CurrDFPageByte);
308
309 while (TotalBlocks)
310 {
311 uint8_t BytesInBlockDiv16 = 0;
312
313 /* Write an endpoint packet sized data block to the dataflash */
314 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
315 {
316 /* Check if end of dataflash page reached */
317 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
318 {
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);
323
324 /* Reset the dataflash buffer counter, increment the page counter */
325 CurrDFPageByteDiv16 = 0;
326 CurrDFPage++;
327
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);
331
332 /* Select the next dataflash chip based on the new dataflash page index */
333 Dataflash_SelectChipFromPage(CurrDFPage);
334
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))
338 {
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();
344 }
345 #endif
346
347 /* Send the dataflash buffer write command */
348 Dataflash_ToggleSelectedChipCS();
349 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
350 Dataflash_SendAddressBytes(0, 0);
351 }
352
353 /* Write one 16-byte chunk of data to the dataflash */
354 for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
355 Dataflash_SendByte(*(BufferPtr++));
356
357 /* Increment the dataflash page 16 byte block counter */
358 CurrDFPageByteDiv16++;
359
360 /* Increment the block 16 byte block counter */
361 BytesInBlockDiv16++;
362 }
363
364 /* Decrement the blocks remaining counter and reset the sub block counter */
365 TotalBlocks--;
366 }
367
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();
373
374 /* Deselect all dataflash chips */
375 Dataflash_DeselectChip();
376 }
377
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.
382 *
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
386 */
387 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress, uint16_t TotalBlocks, uint8_t* BufferPtr)
388 {
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);
392
393 /* Select the correct starting Dataflash IC for the block requested */
394 Dataflash_SelectChipFromPage(CurrDFPage);
395
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);
403
404 while (TotalBlocks)
405 {
406 uint8_t BytesInBlockDiv16 = 0;
407
408 /* Write an endpoint packet sized data block to the dataflash */
409 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
410 {
411 /* Check if end of dataflash page reached */
412 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
413 {
414 /* Reset the dataflash buffer counter, increment the page counter */
415 CurrDFPageByteDiv16 = 0;
416 CurrDFPage++;
417
418 /* Select the next dataflash chip based on the new dataflash page index */
419 Dataflash_SelectChipFromPage(CurrDFPage);
420
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);
428 }
429
430 /* Read one 16-byte chunk of data from the dataflash */
431 for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
432 *(BufferPtr++) = Dataflash_ReceiveByte();
433
434 /* Increment the dataflash page 16 byte block counter */
435 CurrDFPageByteDiv16++;
436
437 /* Increment the block 16 byte block counter */
438 BytesInBlockDiv16++;
439 }
440
441 /* Decrement the blocks remaining counter */
442 TotalBlocks--;
443 }
444
445 /* Deselect all dataflash chips */
446 Dataflash_DeselectChip();
447 }
448
449 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
450 void DataflashManager_ResetDataflashProtections(void)
451 {
452 /* Select first dataflash chip, send the read status register command */
453 Dataflash_SelectChip(DATAFLASH_CHIP1);
454 Dataflash_SendByte(DF_CMD_GETSTATUS);
455
456 /* Check if sector protection is enabled */
457 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
458 {
459 Dataflash_ToggleSelectedChipCS();
460
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]);
466 }
467
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);
472
473 /* Check if sector protection is enabled */
474 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
475 {
476 Dataflash_ToggleSelectedChipCS();
477
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]);
483 }
484 #endif
485
486 /* Deselect current dataflash chip */
487 Dataflash_DeselectChip();
488 }