Minor documentation page updates.
[pub/USBasp.git] / Demos / Device / 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 BlockAddress Data block starting address for the write sequence
47 * \param TotalBlocks Number of blocks of data to write
48 */
49 void DataflashManager_WriteBlocks(USB_ClassInfo_MS_t* MSInterfaceInfo, 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
55 /* Copy selected dataflash's current page contents to the dataflash buffer */
56 Dataflash_SelectChipFromPage(CurrDFPage);
57 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1);
58 Dataflash_SendAddressBytes(CurrDFPage, 0);
59 Dataflash_WaitWhileBusy();
60
61 /* Send the dataflash buffer write command */
62 Dataflash_ToggleSelectedChipCS();
63 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
64 Dataflash_SendAddressBytes(0, CurrDFPageByte);
65
66 /* Wait until endpoint is ready before continuing */
67 while (!(Endpoint_IsReadWriteAllowed()));
68
69 while (TotalBlocks)
70 {
71 uint8_t BytesInBlockDiv16 = 0;
72
73 /* Write an endpoint packet sized data block to the dataflash */
74 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
75 {
76 /* Check if the endpoint is currently empty */
77 if (!(Endpoint_IsReadWriteAllowed()))
78 {
79 /* Clear the current endpoint bank */
80 Endpoint_ClearOUT();
81
82 /* Wait until the host has sent another packet */
83 while (!(Endpoint_IsReadWriteAllowed()));
84 }
85
86 /* Check if end of dataflash page reached */
87 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
88 {
89 /* Write the dataflash buffer contents back to the dataflash page */
90 Dataflash_ToggleSelectedChipCS();
91 Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE);
92 Dataflash_SendAddressBytes(CurrDFPage, 0);
93
94 /* Reset the dataflash buffer counter, increment the page counter */
95 CurrDFPageByteDiv16 = 0;
96 CurrDFPage++;
97
98 /* Select the next dataflash chip based on the new dataflash page index */
99 Dataflash_SelectChipFromPage(CurrDFPage);
100 Dataflash_WaitWhileBusy();
101
102 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
103 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */
104 if ((TotalBlocks * (VIRTUAL_MEMORY_BLOCK_SIZE >> 4)) < (DATAFLASH_PAGE_SIZE >> 4))
105 {
106 /* Copy selected dataflash's current page contents to the dataflash buffer */
107 Dataflash_ToggleSelectedChipCS();
108 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1);
109 Dataflash_SendAddressBytes(CurrDFPage, 0);
110 Dataflash_WaitWhileBusy();
111 }
112 #endif
113
114 /* Send the dataflash buffer write command */
115 Dataflash_ToggleSelectedChipCS();
116 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
117 Dataflash_SendAddressBytes(0, 0);
118 }
119
120 /* Write one 16-byte chunk of data to the dataflash */
121 Dataflash_SendByte(Endpoint_Read_Byte());
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
138 /* Increment the dataflash page 16 byte block counter */
139 CurrDFPageByteDiv16++;
140
141 /* Increment the block 16 byte block counter */
142 BytesInBlockDiv16++;
143
144 /* Check if the current command is being aborted by the host */
145 if (MSInterfaceInfo->IsMassStoreReset)
146 return;
147 }
148
149 /* Decrement the blocks remaining counter and reset the sub block counter */
150 TotalBlocks--;
151 }
152
153 /* Write the dataflash buffer contents back to the dataflash page */
154 Dataflash_ToggleSelectedChipCS();
155 Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE);
156 Dataflash_SendAddressBytes(CurrDFPage, 0x00);
157 Dataflash_WaitWhileBusy();
158
159 /* If the endpoint is empty, clear it ready for the next packet from the host */
160 if (!(Endpoint_IsReadWriteAllowed()))
161 Endpoint_ClearOUT();
162
163 /* Deselect all dataflash chips */
164 Dataflash_DeselectChip();
165 }
166
167 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into
168 * the pre-selected data IN endpoint. This routine reads in Dataflash page sized blocks from the Dataflash
169 * and writes them in OS sized blocks to the endpoint.
170 *
171 * \param BlockAddress Data block starting address for the read sequence
172 * \param TotalBlocks Number of blocks of data to read
173 */
174 void DataflashManager_ReadBlocks(USB_ClassInfo_MS_t* MSInterfaceInfo, const uint32_t BlockAddress, uint16_t TotalBlocks)
175 {
176 uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
177 uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
178 uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
179
180 /* Send the dataflash main memory page read command */
181 Dataflash_SelectChipFromPage(CurrDFPage);
182 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
183 Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte);
184 Dataflash_SendByte(0x00);
185 Dataflash_SendByte(0x00);
186 Dataflash_SendByte(0x00);
187 Dataflash_SendByte(0x00);
188
189 /* Wait until endpoint is ready before continuing */
190 while (!(Endpoint_IsReadWriteAllowed()));
191
192 while (TotalBlocks)
193 {
194 uint8_t BytesInBlockDiv16 = 0;
195
196 /* Write an endpoint packet sized data block to the dataflash */
197 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
198 {
199 /* Check if the endpoint is currently full */
200 if (!(Endpoint_IsReadWriteAllowed()))
201 {
202 /* Clear the endpoint bank to send its contents to the host */
203 Endpoint_ClearIN();
204
205 /* Wait until the endpoint is ready for more data */
206 while (!(Endpoint_IsReadWriteAllowed()));
207 }
208
209 /* Check if end of dataflash page reached */
210 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
211 {
212 /* Reset the dataflash buffer counter, increment the page counter */
213 CurrDFPageByteDiv16 = 0;
214 CurrDFPage++;
215
216 /* Select the next dataflash chip based on the new dataflash page index */
217 Dataflash_SelectChipFromPage(CurrDFPage);
218
219 /* Send the dataflash main memory page read command */
220 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
221 Dataflash_SendAddressBytes(CurrDFPage, 0);
222 Dataflash_SendByte(0x00);
223 Dataflash_SendByte(0x00);
224 Dataflash_SendByte(0x00);
225 Dataflash_SendByte(0x00);
226 }
227
228 /* Read one 16-byte chunk of data from the dataflash */
229 Endpoint_Write_Byte(Dataflash_ReceiveByte());
230 Endpoint_Write_Byte(Dataflash_ReceiveByte());
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
246 /* Increment the dataflash page 16 byte block counter */
247 CurrDFPageByteDiv16++;
248
249 /* Increment the block 16 byte block counter */
250 BytesInBlockDiv16++;
251
252 /* Check if the current command is being aborted by the host */
253 if (MSInterfaceInfo->IsMassStoreReset)
254 return;
255 }
256
257 /* Decrement the blocks remaining counter */
258 TotalBlocks--;
259 }
260
261 /* If the endpoint is full, send its contents to the host */
262 if (!(Endpoint_IsReadWriteAllowed()))
263 Endpoint_ClearIN();
264
265 /* Deselect all dataflash chips */
266 Dataflash_DeselectChip();
267 }
268
269 /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board dataflash IC(s), from
270 * the a given RAM buffer. This routine reads in OS sized blocks from the buffer and writes them to the
271 * dataflash in Dataflash page sized blocks. This can be linked to FAT libraries to write files to the
272 * dataflash.
273 *
274 * \param BlockAddress Data block starting address for the write sequence
275 * \param TotalBlocks Number of blocks of data to write
276 * \param BufferPtr Pointer to the data source RAM buffer
277 */
278 void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress, uint16_t TotalBlocks, uint8_t* BufferPtr)
279 {
280 uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
281 uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
282 uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
283
284 /* Copy selected dataflash's current page contents to the dataflash buffer */
285 Dataflash_SelectChipFromPage(CurrDFPage);
286 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1);
287 Dataflash_SendAddressBytes(CurrDFPage, 0);
288 Dataflash_WaitWhileBusy();
289
290 /* Send the dataflash buffer write command */
291 Dataflash_ToggleSelectedChipCS();
292 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
293 Dataflash_SendAddressBytes(0, CurrDFPageByte);
294
295 while (TotalBlocks)
296 {
297 uint8_t BytesInBlockDiv16 = 0;
298
299 /* Write an endpoint packet sized data block to the dataflash */
300 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
301 {
302 /* Check if end of dataflash page reached */
303 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
304 {
305 /* Write the dataflash buffer contents back to the dataflash page */
306 Dataflash_ToggleSelectedChipCS();
307 Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE);
308 Dataflash_SendAddressBytes(CurrDFPage, 0);
309
310 /* Reset the dataflash buffer counter, increment the page counter */
311 CurrDFPageByteDiv16 = 0;
312 CurrDFPage++;
313
314 /* Select the next dataflash chip based on the new dataflash page index */
315 Dataflash_SelectChipFromPage(CurrDFPage);
316 Dataflash_WaitWhileBusy();
317
318 #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
319 /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */
320 if ((TotalBlocks * (VIRTUAL_MEMORY_BLOCK_SIZE >> 4)) < (DATAFLASH_PAGE_SIZE >> 4))
321 {
322 /* Copy selected dataflash's current page contents to the dataflash buffer */
323 Dataflash_ToggleSelectedChipCS();
324 Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1);
325 Dataflash_SendAddressBytes(CurrDFPage, 0);
326 Dataflash_WaitWhileBusy();
327 }
328 #endif
329
330 /* Send the dataflash buffer write command */
331 Dataflash_ToggleSelectedChipCS();
332 Dataflash_SendByte(DF_CMD_BUFF1WRITE);
333 Dataflash_SendAddressBytes(0, 0);
334 }
335
336 /* Write one 16-byte chunk of data to the dataflash */
337 for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
338 Dataflash_SendByte(*(BufferPtr++));
339
340 /* Increment the dataflash page 16 byte block counter */
341 CurrDFPageByteDiv16++;
342
343 /* Increment the block 16 byte block counter */
344 BytesInBlockDiv16++;
345 }
346
347 /* Decrement the blocks remaining counter and reset the sub block counter */
348 TotalBlocks--;
349 }
350
351 /* Write the dataflash buffer contents back to the dataflash page */
352 Dataflash_ToggleSelectedChipCS();
353 Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE);
354 Dataflash_SendAddressBytes(CurrDFPage, 0x00);
355 Dataflash_WaitWhileBusy();
356
357 /* Deselect all dataflash chips */
358 Dataflash_DeselectChip();
359 }
360
361 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into
362 * the a preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash
363 * and writes them in OS sized blocks to the given buffer. This can be linked to FAT libraries to read
364 * the files stored on the dataflash.
365 *
366 * \param BlockAddress Data block starting address for the read sequence
367 * \param TotalBlocks Number of blocks of data to read
368 * \param BufferPtr Pointer to the data destination RAM buffer
369 */
370 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress, uint16_t TotalBlocks, uint8_t* BufferPtr)
371 {
372 uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
373 uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
374 uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
375
376 /* Send the dataflash main memory page read command */
377 Dataflash_SelectChipFromPage(CurrDFPage);
378 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
379 Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte);
380 Dataflash_SendByte(0x00);
381 Dataflash_SendByte(0x00);
382 Dataflash_SendByte(0x00);
383 Dataflash_SendByte(0x00);
384
385 while (TotalBlocks)
386 {
387 uint8_t BytesInBlockDiv16 = 0;
388
389 /* Write an endpoint packet sized data block to the dataflash */
390 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
391 {
392 /* Check if end of dataflash page reached */
393 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
394 {
395 /* Reset the dataflash buffer counter, increment the page counter */
396 CurrDFPageByteDiv16 = 0;
397 CurrDFPage++;
398
399 /* Select the next dataflash chip based on the new dataflash page index */
400 Dataflash_SelectChipFromPage(CurrDFPage);
401
402 /* Send the dataflash main memory page read command */
403 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
404 Dataflash_SendAddressBytes(CurrDFPage, 0);
405 Dataflash_SendByte(0x00);
406 Dataflash_SendByte(0x00);
407 Dataflash_SendByte(0x00);
408 Dataflash_SendByte(0x00);
409 }
410
411 /* Read one 16-byte chunk of data from the dataflash */
412 for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
413 *(BufferPtr++) = Dataflash_ReceiveByte();
414
415 /* Increment the dataflash page 16 byte block counter */
416 CurrDFPageByteDiv16++;
417
418 /* Increment the block 16 byte block counter */
419 BytesInBlockDiv16++;
420 }
421
422 /* Decrement the blocks remaining counter */
423 TotalBlocks--;
424 }
425
426 /* Deselect all dataflash chips */
427 Dataflash_DeselectChip();
428 }
429
430 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
431 void DataflashManager_ResetDataflashProtections(void)
432 {
433 /* Select first dataflash chip, send the read status register command */
434 Dataflash_SelectChip(DATAFLASH_CHIP1);
435 Dataflash_SendByte(DF_CMD_GETSTATUS);
436
437 /* Check if sector protection is enabled */
438 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
439 {
440 Dataflash_ToggleSelectedChipCS();
441
442 /* Send the commands to disable sector protection */
443 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[0]);
444 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[1]);
445 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[2]);
446 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[3]);
447 }
448
449 /* Select second dataflash chip (if present on selected board), send read status register command */
450 #if (DATAFLASH_TOTALCHIPS == 2)
451 Dataflash_SelectChip(DATAFLASH_CHIP2);
452 Dataflash_SendByte(DF_CMD_GETSTATUS);
453
454 /* Check if sector protection is enabled */
455 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
456 {
457 Dataflash_ToggleSelectedChipCS();
458
459 /* Send the commands to disable sector protection */
460 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[0]);
461 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[1]);
462 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[2]);
463 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[3]);
464 }
465 #endif
466
467 /* Deselect current dataflash chip */
468 Dataflash_DeselectChip();
469 }