More Doxygen fixes - ensure no undocumented function parameters.
[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 BlockAddress Data block starting address for the write sequence
47 * \param 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
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 (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(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 (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 /* Check if the current command is being aborted by the host */
347 if (IsMassStoreReset)
348 return;
349 }
350
351 /* Decrement the blocks remaining counter and reset the sub block counter */
352 TotalBlocks--;
353 }
354
355 /* Write the dataflash buffer contents back to the dataflash page */
356 Dataflash_ToggleSelectedChipCS();
357 Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE);
358 Dataflash_SendAddressBytes(CurrDFPage, 0x00);
359 Dataflash_WaitWhileBusy();
360
361 /* Deselect all dataflash chips */
362 Dataflash_DeselectChip();
363 }
364
365 /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board dataflash IC(s), into
366 * the a preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash
367 * and writes them in OS sized blocks to the given buffer. This can be linked to FAT libraries to read
368 * the files stored on the dataflash.
369 *
370 * \param BlockAddress Data block starting address for the read sequence
371 * \param TotalBlocks Number of blocks of data to read
372 * \param BufferPtr Pointer to the data destination RAM buffer
373 */
374 void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress, uint16_t TotalBlocks, uint8_t* BufferPtr)
375 {
376 uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
377 uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
378 uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
379
380 /* Send the dataflash main memory page read command */
381 Dataflash_SelectChipFromPage(CurrDFPage);
382 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
383 Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte);
384 Dataflash_SendByte(0x00);
385 Dataflash_SendByte(0x00);
386 Dataflash_SendByte(0x00);
387 Dataflash_SendByte(0x00);
388
389 while (TotalBlocks)
390 {
391 uint8_t BytesInBlockDiv16 = 0;
392
393 /* Write an endpoint packet sized data block to the dataflash */
394 while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
395 {
396 /* Check if end of dataflash page reached */
397 if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
398 {
399 /* Reset the dataflash buffer counter, increment the page counter */
400 CurrDFPageByteDiv16 = 0;
401 CurrDFPage++;
402
403 /* Select the next dataflash chip based on the new dataflash page index */
404 Dataflash_SelectChipFromPage(CurrDFPage);
405
406 /* Send the dataflash main memory page read command */
407 Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
408 Dataflash_SendAddressBytes(CurrDFPage, 0);
409 Dataflash_SendByte(0x00);
410 Dataflash_SendByte(0x00);
411 Dataflash_SendByte(0x00);
412 Dataflash_SendByte(0x00);
413 }
414
415 /* Read one 16-byte chunk of data from the dataflash */
416 for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
417 *(BufferPtr++) = Dataflash_ReceiveByte();
418
419 /* Increment the dataflash page 16 byte block counter */
420 CurrDFPageByteDiv16++;
421
422 /* Increment the block 16 byte block counter */
423 BytesInBlockDiv16++;
424
425 /* Check if the current command is being aborted by the host */
426 if (IsMassStoreReset)
427 return;
428 }
429
430 /* Decrement the blocks remaining counter */
431 TotalBlocks--;
432 }
433
434 /* Deselect all dataflash chips */
435 Dataflash_DeselectChip();
436 }
437
438 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
439 void DataflashManager_ResetDataflashProtections(void)
440 {
441 /* Select first dataflash chip, send the read status register command */
442 Dataflash_SelectChip(DATAFLASH_CHIP1);
443 Dataflash_SendByte(DF_CMD_GETSTATUS);
444
445 /* Check if sector protection is enabled */
446 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
447 {
448 Dataflash_ToggleSelectedChipCS();
449
450 /* Send the commands to disable sector protection */
451 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[0]);
452 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[1]);
453 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[2]);
454 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[3]);
455 }
456
457 /* Select second dataflash chip (if present on selected board), send read status register command */
458 #if (DATAFLASH_TOTALCHIPS == 2)
459 Dataflash_SelectChip(DATAFLASH_CHIP2);
460 Dataflash_SendByte(DF_CMD_GETSTATUS);
461
462 /* Check if sector protection is enabled */
463 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
464 {
465 Dataflash_ToggleSelectedChipCS();
466
467 /* Send the commands to disable sector protection */
468 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[0]);
469 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[1]);
470 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[2]);
471 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[3]);
472 }
473 #endif
474
475 /* Deselect current dataflash chip */
476 Dataflash_DeselectChip();
477 }