Changed stream wait timeout counter to be 16-bit, so that very long timeout periods...
[pub/USBasp.git] / Demos / MassStorage / 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_ReadWriteAllowed()));
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_ReadWriteAllowed()))
78 {
79 /* Clear the current endpoint bank */
80 Endpoint_ClearCurrentBank();
81
82 /* Wait until the host has sent another packet */
83 while (!(Endpoint_ReadWriteAllowed()));
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_ReadWriteAllowed()))
161 Endpoint_ClearCurrentBank();
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_ReadWriteAllowed()));
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_ReadWriteAllowed()))
201 {
202 /* Clear the endpoint bank to send its contents to the host */
203 Endpoint_ClearCurrentBank();
204
205 /* Wait until the endpoint is ready for more data */
206 while (!(Endpoint_ReadWriteAllowed()));
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_ReadWriteAllowed()))
263 Endpoint_ClearCurrentBank();
264
265 /* Deselect all dataflash chips */
266 Dataflash_DeselectChip();
267 }
268
269 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
270 void DataflashManager_ResetDataflashProtections(void)
271 {
272 /* Select first dataflash chip, send the read status register command */
273 Dataflash_SelectChip(DATAFLASH_CHIP1);
274 Dataflash_SendByte(DF_CMD_GETSTATUS);
275
276 /* Check if sector protection is enabled */
277 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
278 {
279 Dataflash_ToggleSelectedChipCS();
280
281 /* Send the commands to disable sector protection */
282 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[0]);
283 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[1]);
284 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[2]);
285 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[3]);
286 }
287
288 /* Select second dataflash chip (if present on selected board), send read status register command */
289 #if (DATAFLASH_TOTALCHIPS == 2)
290 Dataflash_SelectChip(DATAFLASH_CHIP2);
291 Dataflash_SendByte(DF_CMD_GETSTATUS);
292
293 /* Check if sector protection is enabled */
294 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
295 {
296 Dataflash_ToggleSelectedChipCS();
297
298 /* Send the commands to disable sector protection */
299 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[0]);
300 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[1]);
301 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[2]);
302 Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[3]);
303 }
304 #endif
305
306 /* Deselect current dataflash chip */
307 Dataflash_DeselectChip();
308 }