3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
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.
39 #define INCLUDE_FROM_DATAFLASHMANAGER_C
40 #include "DataflashManager.h"
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.
46 * \param BlockAddress Data block starting address for the write sequence
47 * \param TotalBlocks Number of blocks of data to write
49 void DataflashManager_WriteBlocks(const uint32_t BlockAddress
, uint16_t TotalBlocks
)
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);
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();
61 /* Send the dataflash buffer write command */
62 Dataflash_ToggleSelectedChipCS();
63 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
64 Dataflash_SendAddressBytes(0, CurrDFPageByte
);
66 /* Wait until endpoint is ready before continuing */
67 while (!(Endpoint_ReadWriteAllowed()));
71 uint8_t BytesInBlockDiv16
= 0;
73 /* Write an endpoint packet sized data block to the dataflash */
74 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
76 /* Check if the endpoint is currently empty */
77 if (!(Endpoint_ReadWriteAllowed()))
79 /* Clear the current endpoint bank */
80 Endpoint_ClearCurrentBank();
82 /* Wait until the host has sent another packet */
83 while (!(Endpoint_ReadWriteAllowed()));
86 /* Check if end of dataflash page reached */
87 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
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);
94 /* Reset the dataflash buffer counter, increment the page counter */
95 CurrDFPageByteDiv16
= 0;
98 /* Select the next dataflash chip based on the new dataflash page index */
99 Dataflash_SelectChipFromPage(CurrDFPage
);
100 Dataflash_WaitWhileBusy();
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))
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();
114 /* Send the dataflash buffer write command */
115 Dataflash_ToggleSelectedChipCS();
116 Dataflash_SendByte(DF_CMD_BUFF1WRITE
);
117 Dataflash_SendAddressBytes(0, 0);
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());
138 /* Increment the dataflash page 16 byte block counter */
139 CurrDFPageByteDiv16
++;
141 /* Increment the block 16 byte block counter */
144 /* Check if the current command is being aborted by the host */
145 if (IsMassStoreReset
)
149 /* Decrement the blocks remaining counter and reset the sub block counter */
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();
159 /* If the endpoint is empty, clear it ready for the next packet from the host */
160 if (!(Endpoint_ReadWriteAllowed()))
161 Endpoint_ClearCurrentBank();
163 /* Deselect all dataflash chips */
164 Dataflash_DeselectChip();
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.
171 * \param BlockAddress Data block starting address for the read sequence
172 * \param TotalBlocks Number of blocks of data to read
174 void DataflashManager_ReadBlocks(const uint32_t BlockAddress
, uint16_t TotalBlocks
)
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);
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);
189 /* Wait until endpoint is ready before continuing */
190 while (!(Endpoint_ReadWriteAllowed()));
194 uint8_t BytesInBlockDiv16
= 0;
196 /* Write an endpoint packet sized data block to the dataflash */
197 while (BytesInBlockDiv16
< (VIRTUAL_MEMORY_BLOCK_SIZE
>> 4))
199 /* Check if the endpoint is currently full */
200 if (!(Endpoint_ReadWriteAllowed()))
202 /* Clear the endpoint bank to send its contents to the host */
203 Endpoint_ClearCurrentBank();
205 /* Wait until the endpoint is ready for more data */
206 while (!(Endpoint_ReadWriteAllowed()));
209 /* Check if end of dataflash page reached */
210 if (CurrDFPageByteDiv16
== (DATAFLASH_PAGE_SIZE
>> 4))
212 /* Reset the dataflash buffer counter, increment the page counter */
213 CurrDFPageByteDiv16
= 0;
216 /* Select the next dataflash chip based on the new dataflash page index */
217 Dataflash_SelectChipFromPage(CurrDFPage
);
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);
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());
246 /* Increment the dataflash page 16 byte block counter */
247 CurrDFPageByteDiv16
++;
249 /* Increment the block 16 byte block counter */
252 /* Check if the current command is being aborted by the host */
253 if (IsMassStoreReset
)
257 /* Decrement the blocks remaining counter */
261 /* If the endpoint is full, send its contents to the host */
262 if (!(Endpoint_ReadWriteAllowed()))
263 Endpoint_ClearCurrentBank();
265 /* Deselect all dataflash chips */
266 Dataflash_DeselectChip();
269 /** Disables the dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
270 void DataflashManager_ResetDataflashProtections(void)
272 /* Select first dataflash chip, send the read status register command */
273 Dataflash_SelectChip(DATAFLASH_CHIP1
);
274 Dataflash_SendByte(DF_CMD_GETSTATUS
);
276 /* Check if sector protection is enabled */
277 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
)
279 Dataflash_ToggleSelectedChipCS();
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]);
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
);
293 /* Check if sector protection is enabled */
294 if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON
)
296 Dataflash_ToggleSelectedChipCS();
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]);
306 /* Deselect current dataflash chip */
307 Dataflash_DeselectChip();