Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataflashManager.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * Functions to manage the physical Dataflash media, including reading and writing of
  29. * blocks of data. These functions are called by the SCSI layer when data must be stored
  30. * or retrieved to/from the physical storage media. If a different media is used (such
  31. * as a SD card or EEPROM), functions similar to these will need to be generated.
  32. */
  33. #define INCLUDE_FROM_DATAFLASHMANAGER_C
  34. #include "DataflashManager.h"
  35. /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board Dataflash IC(s), from
  36. * the pre-selected data OUT endpoint. This routine reads in OS sized blocks from the endpoint and writes
  37. * them to the Dataflash in Dataflash page sized blocks.
  38. *
  39. * \param[in] MSInterfaceInfo Pointer to a structure containing a Mass Storage Class configuration and state
  40. * \param[in] BlockAddress Data block starting address for the write sequence
  41. * \param[in] TotalBlocks Number of blocks of data to write
  42. */
  43. void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
  44. const uint32_t BlockAddress,
  45. uint16_t TotalBlocks)
  46. {
  47. uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
  48. uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
  49. uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
  50. bool UsingSecondBuffer = false;
  51. /* Select the correct starting Dataflash IC for the block requested */
  52. Dataflash_SelectChipFromPage(CurrDFPage);
  53. #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
  54. /* Copy selected dataflash's current page contents to the Dataflash buffer */
  55. Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1);
  56. Dataflash_SendAddressBytes(CurrDFPage, 0);
  57. Dataflash_WaitWhileBusy();
  58. #endif
  59. /* Send the Dataflash buffer write command */
  60. Dataflash_SendByte(DF_CMD_BUFF1WRITE);
  61. Dataflash_SendAddressBytes(0, CurrDFPageByte);
  62. /* Wait until endpoint is ready before continuing */
  63. if (Endpoint_WaitUntilReady())
  64. return;
  65. while (TotalBlocks)
  66. {
  67. uint8_t BytesInBlockDiv16 = 0;
  68. /* Write an endpoint packet sized data block to the Dataflash */
  69. while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
  70. {
  71. /* Check if the endpoint is currently empty */
  72. if (!(Endpoint_IsReadWriteAllowed()))
  73. {
  74. /* Clear the current endpoint bank */
  75. Endpoint_ClearOUT();
  76. /* Wait until the host has sent another packet */
  77. if (Endpoint_WaitUntilReady())
  78. return;
  79. }
  80. /* Check if end of Dataflash page reached */
  81. if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
  82. {
  83. /* Write the Dataflash buffer contents back to the Dataflash page */
  84. Dataflash_WaitWhileBusy();
  85. Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE);
  86. Dataflash_SendAddressBytes(CurrDFPage, 0);
  87. /* Reset the Dataflash buffer counter, increment the page counter */
  88. CurrDFPageByteDiv16 = 0;
  89. CurrDFPage++;
  90. /* Once all the Dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */
  91. if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS))
  92. UsingSecondBuffer = !(UsingSecondBuffer);
  93. /* Select the next Dataflash chip based on the new Dataflash page index */
  94. Dataflash_SelectChipFromPage(CurrDFPage);
  95. #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
  96. /* If less than one Dataflash page remaining, copy over the existing page to preserve trailing data */
  97. if ((TotalBlocks * (VIRTUAL_MEMORY_BLOCK_SIZE >> 4)) < (DATAFLASH_PAGE_SIZE >> 4))
  98. {
  99. /* Copy selected dataflash's current page contents to the Dataflash buffer */
  100. Dataflash_WaitWhileBusy();
  101. Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2 : DF_CMD_MAINMEMTOBUFF1);
  102. Dataflash_SendAddressBytes(CurrDFPage, 0);
  103. Dataflash_WaitWhileBusy();
  104. }
  105. #endif
  106. /* Send the Dataflash buffer write command */
  107. Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE : DF_CMD_BUFF1WRITE);
  108. Dataflash_SendAddressBytes(0, 0);
  109. }
  110. /* Write one 16-byte chunk of data to the Dataflash */
  111. Dataflash_SendByte(Endpoint_Read_8());
  112. Dataflash_SendByte(Endpoint_Read_8());
  113. Dataflash_SendByte(Endpoint_Read_8());
  114. Dataflash_SendByte(Endpoint_Read_8());
  115. Dataflash_SendByte(Endpoint_Read_8());
  116. Dataflash_SendByte(Endpoint_Read_8());
  117. Dataflash_SendByte(Endpoint_Read_8());
  118. Dataflash_SendByte(Endpoint_Read_8());
  119. Dataflash_SendByte(Endpoint_Read_8());
  120. Dataflash_SendByte(Endpoint_Read_8());
  121. Dataflash_SendByte(Endpoint_Read_8());
  122. Dataflash_SendByte(Endpoint_Read_8());
  123. Dataflash_SendByte(Endpoint_Read_8());
  124. Dataflash_SendByte(Endpoint_Read_8());
  125. Dataflash_SendByte(Endpoint_Read_8());
  126. Dataflash_SendByte(Endpoint_Read_8());
  127. /* Increment the Dataflash page 16 byte block counter */
  128. CurrDFPageByteDiv16++;
  129. /* Increment the block 16 byte block counter */
  130. BytesInBlockDiv16++;
  131. /* Check if the current command is being aborted by the host */
  132. if (MSInterfaceInfo->State.IsMassStoreReset)
  133. return;
  134. }
  135. /* Decrement the blocks remaining counter */
  136. TotalBlocks--;
  137. }
  138. /* Write the Dataflash buffer contents back to the Dataflash page */
  139. Dataflash_WaitWhileBusy();
  140. Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE);
  141. Dataflash_SendAddressBytes(CurrDFPage, 0x00);
  142. Dataflash_WaitWhileBusy();
  143. /* If the endpoint is empty, clear it ready for the next packet from the host */
  144. if (!(Endpoint_IsReadWriteAllowed()))
  145. Endpoint_ClearOUT();
  146. /* Deselect all Dataflash chips */
  147. Dataflash_DeselectChip();
  148. }
  149. /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board Dataflash IC(s), into
  150. * the pre-selected data IN endpoint. This routine reads in Dataflash page sized blocks from the Dataflash
  151. * and writes them in OS sized blocks to the endpoint.
  152. *
  153. * \param[in] MSInterfaceInfo Pointer to a structure containing a Mass Storage Class configuration and state
  154. * \param[in] BlockAddress Data block starting address for the read sequence
  155. * \param[in] TotalBlocks Number of blocks of data to read
  156. */
  157. void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
  158. const uint32_t BlockAddress,
  159. uint16_t TotalBlocks)
  160. {
  161. uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
  162. uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
  163. uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
  164. /* Select the correct starting Dataflash IC for the block requested */
  165. Dataflash_SelectChipFromPage(CurrDFPage);
  166. /* Send the Dataflash main memory page read command */
  167. Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
  168. Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte);
  169. Dataflash_SendByte(0x00);
  170. Dataflash_SendByte(0x00);
  171. Dataflash_SendByte(0x00);
  172. Dataflash_SendByte(0x00);
  173. /* Wait until endpoint is ready before continuing */
  174. if (Endpoint_WaitUntilReady())
  175. return;
  176. while (TotalBlocks)
  177. {
  178. uint8_t BytesInBlockDiv16 = 0;
  179. /* Write an endpoint packet sized data block to the Dataflash */
  180. while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
  181. {
  182. /* Check if the endpoint is currently full */
  183. if (!(Endpoint_IsReadWriteAllowed()))
  184. {
  185. /* Clear the endpoint bank to send its contents to the host */
  186. Endpoint_ClearIN();
  187. /* Wait until the endpoint is ready for more data */
  188. if (Endpoint_WaitUntilReady())
  189. return;
  190. }
  191. /* Check if end of Dataflash page reached */
  192. if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
  193. {
  194. /* Reset the Dataflash buffer counter, increment the page counter */
  195. CurrDFPageByteDiv16 = 0;
  196. CurrDFPage++;
  197. /* Select the next Dataflash chip based on the new Dataflash page index */
  198. Dataflash_SelectChipFromPage(CurrDFPage);
  199. /* Send the Dataflash main memory page read command */
  200. Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
  201. Dataflash_SendAddressBytes(CurrDFPage, 0);
  202. Dataflash_SendByte(0x00);
  203. Dataflash_SendByte(0x00);
  204. Dataflash_SendByte(0x00);
  205. Dataflash_SendByte(0x00);
  206. }
  207. /* Read one 16-byte chunk of data from the Dataflash */
  208. Endpoint_Write_8(Dataflash_ReceiveByte());
  209. Endpoint_Write_8(Dataflash_ReceiveByte());
  210. Endpoint_Write_8(Dataflash_ReceiveByte());
  211. Endpoint_Write_8(Dataflash_ReceiveByte());
  212. Endpoint_Write_8(Dataflash_ReceiveByte());
  213. Endpoint_Write_8(Dataflash_ReceiveByte());
  214. Endpoint_Write_8(Dataflash_ReceiveByte());
  215. Endpoint_Write_8(Dataflash_ReceiveByte());
  216. Endpoint_Write_8(Dataflash_ReceiveByte());
  217. Endpoint_Write_8(Dataflash_ReceiveByte());
  218. Endpoint_Write_8(Dataflash_ReceiveByte());
  219. Endpoint_Write_8(Dataflash_ReceiveByte());
  220. Endpoint_Write_8(Dataflash_ReceiveByte());
  221. Endpoint_Write_8(Dataflash_ReceiveByte());
  222. Endpoint_Write_8(Dataflash_ReceiveByte());
  223. Endpoint_Write_8(Dataflash_ReceiveByte());
  224. /* Increment the Dataflash page 16 byte block counter */
  225. CurrDFPageByteDiv16++;
  226. /* Increment the block 16 byte block counter */
  227. BytesInBlockDiv16++;
  228. /* Check if the current command is being aborted by the host */
  229. if (MSInterfaceInfo->State.IsMassStoreReset)
  230. return;
  231. }
  232. /* Decrement the blocks remaining counter */
  233. TotalBlocks--;
  234. }
  235. /* If the endpoint is full, send its contents to the host */
  236. if (!(Endpoint_IsReadWriteAllowed()))
  237. Endpoint_ClearIN();
  238. /* Deselect all Dataflash chips */
  239. Dataflash_DeselectChip();
  240. }
  241. /** Writes blocks (OS blocks, not Dataflash pages) to the storage medium, the board Dataflash IC(s), from
  242. * the given RAM buffer. This routine reads in OS sized blocks from the buffer and writes them to the
  243. * Dataflash in Dataflash page sized blocks. This can be linked to FAT libraries to write files to the
  244. * Dataflash.
  245. *
  246. * \param[in] BlockAddress Data block starting address for the write sequence
  247. * \param[in] TotalBlocks Number of blocks of data to write
  248. * \param[in] BufferPtr Pointer to the data source RAM buffer
  249. */
  250. void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress,
  251. uint16_t TotalBlocks,
  252. const uint8_t* BufferPtr)
  253. {
  254. uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
  255. uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
  256. uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
  257. bool UsingSecondBuffer = false;
  258. /* Select the correct starting Dataflash IC for the block requested */
  259. Dataflash_SelectChipFromPage(CurrDFPage);
  260. #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
  261. /* Copy selected dataflash's current page contents to the Dataflash buffer */
  262. Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1);
  263. Dataflash_SendAddressBytes(CurrDFPage, 0);
  264. Dataflash_WaitWhileBusy();
  265. #endif
  266. /* Send the Dataflash buffer write command */
  267. Dataflash_SendByte(DF_CMD_BUFF1WRITE);
  268. Dataflash_SendAddressBytes(0, CurrDFPageByte);
  269. while (TotalBlocks)
  270. {
  271. uint8_t BytesInBlockDiv16 = 0;
  272. /* Write an endpoint packet sized data block to the Dataflash */
  273. while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
  274. {
  275. /* Check if end of Dataflash page reached */
  276. if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
  277. {
  278. /* Write the Dataflash buffer contents back to the Dataflash page */
  279. Dataflash_WaitWhileBusy();
  280. Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE);
  281. Dataflash_SendAddressBytes(CurrDFPage, 0);
  282. /* Reset the Dataflash buffer counter, increment the page counter */
  283. CurrDFPageByteDiv16 = 0;
  284. CurrDFPage++;
  285. /* Once all the Dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */
  286. if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS))
  287. UsingSecondBuffer = !(UsingSecondBuffer);
  288. /* Select the next Dataflash chip based on the new Dataflash page index */
  289. Dataflash_SelectChipFromPage(CurrDFPage);
  290. #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
  291. /* If less than one Dataflash page remaining, copy over the existing page to preserve trailing data */
  292. if ((TotalBlocks * (VIRTUAL_MEMORY_BLOCK_SIZE >> 4)) < (DATAFLASH_PAGE_SIZE >> 4))
  293. {
  294. /* Copy selected dataflash's current page contents to the Dataflash buffer */
  295. Dataflash_WaitWhileBusy();
  296. Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2 : DF_CMD_MAINMEMTOBUFF1);
  297. Dataflash_SendAddressBytes(CurrDFPage, 0);
  298. Dataflash_WaitWhileBusy();
  299. }
  300. #endif
  301. /* Send the Dataflash buffer write command */
  302. Dataflash_ToggleSelectedChipCS();
  303. Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE : DF_CMD_BUFF1WRITE);
  304. Dataflash_SendAddressBytes(0, 0);
  305. }
  306. /* Write one 16-byte chunk of data to the Dataflash */
  307. for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
  308. Dataflash_SendByte(*(BufferPtr++));
  309. /* Increment the Dataflash page 16 byte block counter */
  310. CurrDFPageByteDiv16++;
  311. /* Increment the block 16 byte block counter */
  312. BytesInBlockDiv16++;
  313. }
  314. /* Decrement the blocks remaining counter */
  315. TotalBlocks--;
  316. }
  317. /* Write the Dataflash buffer contents back to the Dataflash page */
  318. Dataflash_WaitWhileBusy();
  319. Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE);
  320. Dataflash_SendAddressBytes(CurrDFPage, 0x00);
  321. Dataflash_WaitWhileBusy();
  322. /* Deselect all Dataflash chips */
  323. Dataflash_DeselectChip();
  324. }
  325. /** Reads blocks (OS blocks, not Dataflash pages) from the storage medium, the board Dataflash IC(s), into
  326. * the preallocated RAM buffer. This routine reads in Dataflash page sized blocks from the Dataflash
  327. * and writes them in OS sized blocks to the given buffer. This can be linked to FAT libraries to read
  328. * the files stored on the Dataflash.
  329. *
  330. * \param[in] BlockAddress Data block starting address for the read sequence
  331. * \param[in] TotalBlocks Number of blocks of data to read
  332. * \param[out] BufferPtr Pointer to the data destination RAM buffer
  333. */
  334. void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress,
  335. uint16_t TotalBlocks,
  336. uint8_t* BufferPtr)
  337. {
  338. uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
  339. uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
  340. uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
  341. /* Select the correct starting Dataflash IC for the block requested */
  342. Dataflash_SelectChipFromPage(CurrDFPage);
  343. /* Send the Dataflash main memory page read command */
  344. Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
  345. Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte);
  346. Dataflash_SendByte(0x00);
  347. Dataflash_SendByte(0x00);
  348. Dataflash_SendByte(0x00);
  349. Dataflash_SendByte(0x00);
  350. while (TotalBlocks)
  351. {
  352. uint8_t BytesInBlockDiv16 = 0;
  353. /* Write an endpoint packet sized data block to the Dataflash */
  354. while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
  355. {
  356. /* Check if end of Dataflash page reached */
  357. if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
  358. {
  359. /* Reset the Dataflash buffer counter, increment the page counter */
  360. CurrDFPageByteDiv16 = 0;
  361. CurrDFPage++;
  362. /* Select the next Dataflash chip based on the new Dataflash page index */
  363. Dataflash_SelectChipFromPage(CurrDFPage);
  364. /* Send the Dataflash main memory page read command */
  365. Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
  366. Dataflash_SendAddressBytes(CurrDFPage, 0);
  367. Dataflash_SendByte(0x00);
  368. Dataflash_SendByte(0x00);
  369. Dataflash_SendByte(0x00);
  370. Dataflash_SendByte(0x00);
  371. }
  372. /* Read one 16-byte chunk of data from the Dataflash */
  373. for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
  374. *(BufferPtr++) = Dataflash_ReceiveByte();
  375. /* Increment the Dataflash page 16 byte block counter */
  376. CurrDFPageByteDiv16++;
  377. /* Increment the block 16 byte block counter */
  378. BytesInBlockDiv16++;
  379. }
  380. /* Decrement the blocks remaining counter */
  381. TotalBlocks--;
  382. }
  383. /* Deselect all Dataflash chips */
  384. Dataflash_DeselectChip();
  385. }
  386. /** Disables the Dataflash memory write protection bits on the board Dataflash ICs, if enabled. */
  387. void DataflashManager_ResetDataflashProtections(void)
  388. {
  389. /* Select first Dataflash chip, send the read status register command */
  390. Dataflash_SelectChip(DATAFLASH_CHIP1);
  391. Dataflash_SendByte(DF_CMD_GETSTATUS);
  392. /* Check if sector protection is enabled */
  393. if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
  394. {
  395. Dataflash_ToggleSelectedChipCS();
  396. /* Send the commands to disable sector protection */
  397. Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[0]);
  398. Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[1]);
  399. Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[2]);
  400. Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[3]);
  401. }
  402. /* Select second Dataflash chip (if present on selected board), send read status register command */
  403. #if (DATAFLASH_TOTALCHIPS == 2)
  404. Dataflash_SelectChip(DATAFLASH_CHIP2);
  405. Dataflash_SendByte(DF_CMD_GETSTATUS);
  406. /* Check if sector protection is enabled */
  407. if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
  408. {
  409. Dataflash_ToggleSelectedChipCS();
  410. /* Send the commands to disable sector protection */
  411. Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[0]);
  412. Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[1]);
  413. Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[2]);
  414. Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[3]);
  415. }
  416. #endif
  417. /* Deselect current Dataflash chip */
  418. Dataflash_DeselectChip();
  419. }
  420. /** Performs a simple test on the attached Dataflash IC(s) to ensure that they are working.
  421. *
  422. * \return Boolean \c true if all media chips are working, \c false otherwise
  423. */
  424. bool DataflashManager_CheckDataflashOperation(void)
  425. {
  426. uint8_t ReturnByte;
  427. /* Test first Dataflash IC is present and responding to commands */
  428. Dataflash_SelectChip(DATAFLASH_CHIP1);
  429. Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO);
  430. ReturnByte = Dataflash_ReceiveByte();
  431. Dataflash_DeselectChip();
  432. /* If returned data is invalid, fail the command */
  433. if (ReturnByte != DF_MANUFACTURER_ATMEL)
  434. return false;
  435. #if (DATAFLASH_TOTALCHIPS == 2)
  436. /* Test second Dataflash IC is present and responding to commands */
  437. Dataflash_SelectChip(DATAFLASH_CHIP2);
  438. Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO);
  439. ReturnByte = Dataflash_ReceiveByte();
  440. Dataflash_DeselectChip();
  441. /* If returned data is invalid, fail the command */
  442. if (ReturnByte != DF_MANUFACTURER_ATMEL)
  443. return false;
  444. #endif
  445. return true;
  446. }