Break device mode class driver interfaces into seperate config and state structs...
[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 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
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
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();
61
62 /* Send the dataflash buffer write command */
63 Dataflash_ToggleSelectedChipCS();
64 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
65 Dataflash_SendAddressBytes(0, CurrDFPageByte);
66
67 /* Wait until endpoint is ready before continuing */
68 while (!(Endpoint_IsReadWriteAllowed()));
69
70 while (TotalBlocks)
71 {
72 uint8_t BytesInBlockDiv16 = 0;
73
74 /* Write an endpoint packet sized data block to the dataflash */
75 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
76 {
77 /* Check if the endpoint is currently empty */
78 if (!(Endpoint_IsReadWriteAllowed()))
79 {
80 /* Clear the current endpoint bank */
81 Endpoint_ClearOUT();
82
83 /* Wait until the host has sent another packet */
84 while (!(Endpoint_IsReadWriteAllowed()));
85 }
86
87 /* Check if end of dataflash page reached */
88 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
89 {
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);
94
95 /* Reset the dataflash buffer counter, increment the page counter */
96 CurrDFPageByteDiv16 = 0;
97 CurrDFPage++;
98
99 /* Select the next dataflash chip based on the new dataflash page index */
100 Dataflash_SelectChipFromPage(CurrDFPage);
101 Dataflash_WaitWhileBusy();
102
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))
106 {
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();
112 }
113 #endif
114
115 /* Send the dataflash buffer write command */
116 Dataflash_ToggleSelectedChipCS();
117 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
118 Dataflash_SendAddressBytes(0, 0);
119 }
120
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());
138
139 /* Increment the dataflash page 16 byte block counter */
140 CurrDFPageByteDiv16++;
141
142 /* Increment the block 16 byte block counter */
143 BytesInBlockDiv16++;
144
145 /* Check if the current command is being aborted by the host */
146 if (MSInterfaceInfo->State.IsMassStoreReset)
147 return;
148 }
149
150 /* Decrement the blocks remaining counter and reset the sub block counter */
151 TotalBlocks--;
152 }
153
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();
159
160 /* If the endpoint is empty, clear it ready for the next packet from the host */
161 if (!(Endpoint_IsReadWriteAllowed()))
162 Endpoint_ClearOUT();
163
164 /* Deselect all dataflash chips */
165 Dataflash_DeselectChip();
166 }
167
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.
171 *
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
175 */
176 void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, const uint32_t BlockAddress, uint16_t TotalBlocks)
177 {
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);
181
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);
190
191 /* Wait until endpoint is ready before continuing */
192 while (!(Endpoint_IsReadWriteAllowed()));
193
194 while (TotalBlocks)
195 {
196 uint8_t BytesInBlockDiv16 = 0;
197
198 /* Write an endpoint packet sized data block to the dataflash */
199 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
200 {
201 /* Check if the endpoint is currently full */
202 if (!(Endpoint_IsReadWriteAllowed()))
203 {
204 /* Clear the endpoint bank to send its contents to the host */
205 Endpoint_ClearIN();
206
207 /* Wait until the endpoint is ready for more data */
208 while (!(Endpoint_IsReadWriteAllowed()));
209 }
210
211 /* Check if end of dataflash page reached */
212 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
213 {
214 /* Reset the dataflash buffer counter, increment the page counter */
215 CurrDFPageByteDiv16 = 0;
216 CurrDFPage++;
217
218 /* Select the next dataflash chip based on the new dataflash page index */
219 Dataflash_SelectChipFromPage(CurrDFPage);
220
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);
228 }
229
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());
247
248 /* Increment the dataflash page 16 byte block counter */
249 CurrDFPageByteDiv16++;
250
251 /* Increment the block 16 byte block counter */
252 BytesInBlockDiv16++;
253
254 /* Check if the current command is being aborted by the host */
255 if (MSInterfaceInfo->State.IsMassStoreReset)
256 return;
257 }
258
259 /* Decrement the blocks remaining counter */
260 TotalBlocks--;
261 }
262
263 /* If the endpoint is full, send its contents to the host */
264 if (!(Endpoint_IsReadWriteAllowed()))
265 Endpoint_ClearIN();
266
267 /* Deselect all dataflash chips */
268 Dataflash_DeselectChip();
269 }
270
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
274 * dataflash.
275 *
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
279 */
280 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress, uint16_t TotalBlocks, uint8_t* BufferPtr)
281 {
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);
285
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();
291
292 /* Send the dataflash buffer write command */
293 Dataflash_ToggleSelectedChipCS();
294 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
295 Dataflash_SendAddressBytes(0, CurrDFPageByte);
296
297 while (TotalBlocks)
298 {
299 uint8_t BytesInBlockDiv16 = 0;
300
301 /* Write an endpoint packet sized data block to the dataflash */
302 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
303 {
304 /* Check if end of dataflash page reached */
305 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
306 {
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);
311
312 /* Reset the dataflash buffer counter, increment the page counter */
313 CurrDFPageByteDiv16 = 0;
314 CurrDFPage++;
315
316 /* Select the next dataflash chip based on the new dataflash page index */
317 Dataflash_SelectChipFromPage(CurrDFPage);
318 Dataflash_WaitWhileBusy();
319
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))
323 {
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();
329 }
330 #endif
331
332 /* Send the dataflash buffer write command */
333 Dataflash_ToggleSelectedChipCS();
334 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
335 Dataflash_SendAddressBytes(0, 0);
336 }
337
338 /* Write one 16-byte chunk of data to the dataflash */
339 for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
340 Dataflash_SendByte(*(BufferPtr++));
341
342 /* Increment the dataflash page 16 byte block counter */
343 CurrDFPageByteDiv16++;
344
345 /* Increment the block 16 byte block counter */
346 BytesInBlockDiv16++;
347 }
348
349 /* Decrement the blocks remaining counter and reset the sub block counter */
350 TotalBlocks--;
351 }
352
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();
358
359 /* Deselect all dataflash chips */
360 Dataflash_DeselectChip();
361 }
362
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.
367 *
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
371 */
372 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress, uint16_t TotalBlocks, uint8_t* BufferPtr)
373 {
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);
377
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);
386
387 while (TotalBlocks)
388 {
389 uint8_t BytesInBlockDiv16 = 0;
390
391 /* Write an endpoint packet sized data block to the dataflash */
392 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
393 {
394 /* Check if end of dataflash page reached */
395 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
396 {
397 /* Reset the dataflash buffer counter, increment the page counter */
398 CurrDFPageByteDiv16 = 0;
399 CurrDFPage++;
400
401 /* Select the next dataflash chip based on the new dataflash page index */
402 Dataflash_SelectChipFromPage(CurrDFPage);
403
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);
411 }
412
413 /* Read one 16-byte chunk of data from the dataflash */
414 for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
415 *(BufferPtr++) = Dataflash_ReceiveByte();
416
417 /* Increment the dataflash page 16 byte block counter */
418 CurrDFPageByteDiv16++;
419
420 /* Increment the block 16 byte block counter */
421 BytesInBlockDiv16++;
422 }
423
424 /* Decrement the blocks remaining counter */
425 TotalBlocks--;
426 }
427
428 /* Deselect all dataflash chips */
429 Dataflash_DeselectChip();
430 }
431
432 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
433 void DataflashManager_ResetDataflashProtections(void)
434 {
435 /* Select first dataflash chip, send the read status register command */
436 Dataflash_SelectChip(DATAFLASH_CHIP1);
437 Dataflash_SendByte(DF_CMD_GETSTATUS);
438
439 /* Check if sector protection is enabled */
440 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
441 {
442 Dataflash_ToggleSelectedChipCS();
443
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]);
449 }
450
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);
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 #endif
468
469 /* Deselect current dataflash chip */
470 Dataflash_DeselectChip();
471 }