Renamed all library events to properly seperate out Device and Host mode events....
[pub/USBasp.git] / Demos / Device / LowLevel / 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] BlockAddress Data block starting address for the write sequence
47 * \param[in] TotalBlocks Number of blocks of data to write
48 */
49 void DataflashManager_WriteBlocks(const uint32_t BlockAddress, uint16_t TotalBlocks)
50 {
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;
55
56 /* Select the correct starting Dataflash IC for the block requested */
57 Dataflash_SelectChipFromPage(CurrDFPage);
58
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();
64 #endif
65
66 /* Send the dataflash buffer write command */
67 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
68 Dataflash_SendAddressBytes(0, CurrDFPageByte);
69
70 /* Wait until endpoint is ready before continuing */
71 while (!(Endpoint_IsReadWriteAllowed()))
72 {
73 if (USB_DeviceState == DEVICE_STATE_Unattached)
74 return;
75 }
76
77 while (TotalBlocks)
78 {
79 uint8_t BytesInBlockDiv16 = 0;
80
81 /* Write an endpoint packet sized data block to the dataflash */
82 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
83 {
84 /* Check if the endpoint is currently empty */
85 if (!(Endpoint_IsReadWriteAllowed()))
86 {
87 /* Clear the current endpoint bank */
88 Endpoint_ClearOUT();
89
90 /* Wait until the host has sent another packet */
91 while (!(Endpoint_IsReadWriteAllowed()))
92 {
93 if (USB_DeviceState == DEVICE_STATE_Unattached)
94 return;
95 }
96 }
97
98 /* Check if end of dataflash page reached */
99 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
100 {
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);
105
106 /* Reset the dataflash buffer counter, increment the page counter */
107 CurrDFPageByteDiv16 = 0;
108 CurrDFPage++;
109
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);
113
114 /* Select the next dataflash chip based on the new dataflash page index */
115 Dataflash_SelectChipFromPage(CurrDFPage);
116
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))
120 {
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();
126 }
127 #endif
128
129 /* Send the dataflash buffer write command */
130 Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE : DF_CMD_BUFF1WRITE);
131 Dataflash_SendAddressBytes(0, 0);
132 }
133
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());
151
152 /* Increment the dataflash page 16 byte block counter */
153 CurrDFPageByteDiv16++;
154
155 /* Increment the block 16 byte block counter */
156 BytesInBlockDiv16++;
157
158 /* Check if the current command is being aborted by the host */
159 if (IsMassStoreReset)
160 return;
161 }
162
163 /* Decrement the blocks remaining counter and reset the sub block counter */
164 TotalBlocks--;
165 }
166
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();
172
173 /* If the endpoint is empty, clear it ready for the next packet from the host */
174 if (!(Endpoint_IsReadWriteAllowed()))
175 Endpoint_ClearOUT();
176
177 /* Deselect all dataflash chips */
178 Dataflash_DeselectChip();
179 }
180
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.
184 *
185 * \param[in] BlockAddress Data block starting address for the read sequence
186 * \param[in] TotalBlocks Number of blocks of data to read
187 */
188 void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBlocks)
189 {
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);
193
194 /* Select the correct starting Dataflash IC for the block requested */
195 Dataflash_SelectChipFromPage(CurrDFPage);
196
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);
204
205 /* Wait until endpoint is ready before continuing */
206 while (!(Endpoint_IsReadWriteAllowed()))
207 {
208 if (USB_DeviceState == DEVICE_STATE_Unattached)
209 return;
210 }
211
212 while (TotalBlocks)
213 {
214 uint8_t BytesInBlockDiv16 = 0;
215
216 /* Write an endpoint packet sized data block to the dataflash */
217 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
218 {
219 /* Check if the endpoint is currently full */
220 if (!(Endpoint_IsReadWriteAllowed()))
221 {
222 /* Clear the endpoint bank to send its contents to the host */
223 Endpoint_ClearIN();
224
225 /* Wait until the endpoint is ready for more data */
226 while (!(Endpoint_IsReadWriteAllowed()))
227 {
228 if (USB_DeviceState == DEVICE_STATE_Unattached)
229 return;
230 }
231 }
232
233 /* Check if end of dataflash page reached */
234 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
235 {
236 /* Reset the dataflash buffer counter, increment the page counter */
237 CurrDFPageByteDiv16 = 0;
238 CurrDFPage++;
239
240 /* Select the next dataflash chip based on the new dataflash page index */
241 Dataflash_SelectChipFromPage(CurrDFPage);
242
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);
250 }
251
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());
269
270 /* Increment the dataflash page 16 byte block counter */
271 CurrDFPageByteDiv16++;
272
273 /* Increment the block 16 byte block counter */
274 BytesInBlockDiv16++;
275
276 /* Check if the current command is being aborted by the host */
277 if (IsMassStoreReset)
278 return;
279 }
280
281 /* Decrement the blocks remaining counter */
282 TotalBlocks--;
283 }
284
285 /* If the endpoint is full, send its contents to the host */
286 if (!(Endpoint_IsReadWriteAllowed()))
287 Endpoint_ClearIN();
288
289 /* Deselect all dataflash chips */
290 Dataflash_DeselectChip();
291 }
292
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
296 * dataflash.
297 *
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
301 */
302 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress, uint16_t TotalBlocks, uint8_t* BufferPtr)
303 {
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;
308
309 /* Select the correct starting Dataflash IC for the block requested */
310 Dataflash_SelectChipFromPage(CurrDFPage);
311
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();
317 #endif
318
319 /* Send the dataflash buffer write command */
320 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
321 Dataflash_SendAddressBytes(0, CurrDFPageByte);
322
323 while (TotalBlocks)
324 {
325 uint8_t BytesInBlockDiv16 = 0;
326
327 /* Write an endpoint packet sized data block to the dataflash */
328 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
329 {
330 /* Check if end of dataflash page reached */
331 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
332 {
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);
337
338 /* Reset the dataflash buffer counter, increment the page counter */
339 CurrDFPageByteDiv16 = 0;
340 CurrDFPage++;
341
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);
345
346 /* Select the next dataflash chip based on the new dataflash page index */
347 Dataflash_SelectChipFromPage(CurrDFPage);
348
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))
352 {
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();
358 }
359 #endif
360
361 /* Send the dataflash buffer write command */
362 Dataflash_ToggleSelectedChipCS();
363 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
364 Dataflash_SendAddressBytes(0, 0);
365 }
366
367 /* Write one 16-byte chunk of data to the dataflash */
368 for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
369 Dataflash_SendByte(*(BufferPtr++));
370
371 /* Increment the dataflash page 16 byte block counter */
372 CurrDFPageByteDiv16++;
373
374 /* Increment the block 16 byte block counter */
375 BytesInBlockDiv16++;
376 }
377
378 /* Decrement the blocks remaining counter and reset the sub block counter */
379 TotalBlocks--;
380 }
381
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();
387
388 /* Deselect all dataflash chips */
389 Dataflash_DeselectChip();
390 }
391
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.
396 *
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
400 */
401 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress, uint16_t TotalBlocks, uint8_t* BufferPtr)
402 {
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);
406
407 /* Select the correct starting Dataflash IC for the block requested */
408 Dataflash_SelectChipFromPage(CurrDFPage);
409
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);
417
418 while (TotalBlocks)
419 {
420 uint8_t BytesInBlockDiv16 = 0;
421
422 /* Write an endpoint packet sized data block to the dataflash */
423 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
424 {
425 /* Check if end of dataflash page reached */
426 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
427 {
428 /* Reset the dataflash buffer counter, increment the page counter */
429 CurrDFPageByteDiv16 = 0;
430 CurrDFPage++;
431
432 /* Select the next dataflash chip based on the new dataflash page index */
433 Dataflash_SelectChipFromPage(CurrDFPage);
434
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);
442 }
443
444 /* Read one 16-byte chunk of data from the dataflash */
445 for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
446 *(BufferPtr++) = Dataflash_ReceiveByte();
447
448 /* Increment the dataflash page 16 byte block counter */
449 CurrDFPageByteDiv16++;
450
451 /* Increment the block 16 byte block counter */
452 BytesInBlockDiv16++;
453 }
454
455 /* Decrement the blocks remaining counter */
456 TotalBlocks--;
457 }
458
459 /* Deselect all dataflash chips */
460 Dataflash_DeselectChip();
461 }
462
463 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
464 void DataflashManager_ResetDataflashProtections(void)
465 {
466 /* Select first dataflash chip, send the read status register command */
467 Dataflash_SelectChip(DATAFLASH_CHIP1);
468 Dataflash_SendByte(DF_CMD_GETSTATUS);
469
470 /* Check if sector protection is enabled */
471 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
472 {
473 Dataflash_ToggleSelectedChipCS();
474
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]);
480 }
481
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);
486
487 /* Check if sector protection is enabled */
488 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
489 {
490 Dataflash_ToggleSelectedChipCS();
491
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]);
497 }
498 #endif
499
500 /* Deselect current dataflash chip */
501 Dataflash_DeselectChip();
502 }