w25qxx.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. #include "w25qxx.h"
  2. #include "w25qxxConf.h"
  3. #include "w25qxxPort.h"
  4. #include "string.h"
  5. #if (_W25QXX_DEBUG == 1)
  6. #include "stdio.h"
  7. #define W25QXX_DEBUG_PRINTF(...) \
  8. do{\
  9. printf(__VA_ARGS__);\
  10. printf("\r\n");\
  11. }while(0)
  12. #endif
  13. #define W25QXX_DUMMY_BYTE 0xA5
  14. w25qxx_t w25qxx;
  15. // ###################################################################################################################
  16. uint32_t W25qxx_ReadID(void)
  17. {
  18. uint32_t Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;
  19. W25qxx_Select();
  20. W25qxx_Spi(0x9F);
  21. Temp0 = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  22. Temp1 = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  23. Temp2 = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  24. W25qxx_Deselect();
  25. Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;
  26. return Temp;
  27. }
  28. // ###################################################################################################################
  29. void W25qxx_ReadUniqID(void)
  30. {
  31. W25qxx_Select();
  32. W25qxx_Spi(0x4B);
  33. for (uint8_t i = 0; i < 4; i++)
  34. W25qxx_Spi(W25QXX_DUMMY_BYTE);
  35. for (uint8_t i = 0; i < 8; i++)
  36. w25qxx.UniqID[i] = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  37. W25qxx_Deselect();
  38. }
  39. // ###################################################################################################################
  40. void W25qxx_WriteEnable(void)
  41. {
  42. W25qxx_Select();
  43. W25qxx_Spi(0x06);
  44. W25qxx_Deselect();
  45. W25qxx_Delay(1);
  46. }
  47. // ###################################################################################################################
  48. void W25qxx_WriteDisable(void)
  49. {
  50. W25qxx_Select();
  51. W25qxx_Spi(0x04);
  52. W25qxx_Deselect();
  53. W25qxx_Delay(1);
  54. }
  55. // ###################################################################################################################
  56. uint8_t W25qxx_ReadStatusRegister(uint8_t SelectStatusRegister_1_2_3)
  57. {
  58. uint8_t status = 0;
  59. W25qxx_Select();
  60. if (SelectStatusRegister_1_2_3 == 1)
  61. {
  62. W25qxx_Spi(0x05);
  63. status = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  64. w25qxx.StatusRegister1 = status;
  65. }
  66. else if (SelectStatusRegister_1_2_3 == 2)
  67. {
  68. W25qxx_Spi(0x35);
  69. status = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  70. w25qxx.StatusRegister2 = status;
  71. }
  72. else
  73. {
  74. W25qxx_Spi(0x15);
  75. status = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  76. w25qxx.StatusRegister3 = status;
  77. }
  78. W25qxx_Deselect();
  79. return status;
  80. }
  81. // ###################################################################################################################
  82. void W25qxx_WriteStatusRegister(uint8_t SelectStatusRegister_1_2_3, uint8_t Data)
  83. {
  84. W25qxx_Select();
  85. if (SelectStatusRegister_1_2_3 == 1)
  86. {
  87. W25qxx_Spi(0x01);
  88. w25qxx.StatusRegister1 = Data;
  89. }
  90. else if (SelectStatusRegister_1_2_3 == 2)
  91. {
  92. W25qxx_Spi(0x31);
  93. w25qxx.StatusRegister2 = Data;
  94. }
  95. else
  96. {
  97. W25qxx_Spi(0x11);
  98. w25qxx.StatusRegister3 = Data;
  99. }
  100. W25qxx_Spi(Data);
  101. W25qxx_Deselect();
  102. }
  103. // ###################################################################################################################
  104. void W25qxx_WaitForWriteEnd(void)
  105. {
  106. W25qxx_Delay(1);
  107. W25qxx_Select();
  108. W25qxx_Spi(0x05);
  109. do
  110. {
  111. w25qxx.StatusRegister1 = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  112. W25qxx_Delay(1);
  113. } while ((w25qxx.StatusRegister1 & 0x01) == 0x01);
  114. W25qxx_Deselect();
  115. }
  116. // ###################################################################################################################
  117. bool W25qxx_Init(void)
  118. {
  119. W25qxx_SPI_Init();
  120. w25qxx.Lock = 1;
  121. W25qxx_Delay(100);
  122. W25qxx_Deselect();
  123. W25qxx_Delay(100);
  124. uint32_t id;
  125. #if (_W25QXX_DEBUG == 1)
  126. W25QXX_DEBUG_PRINTF("w25qxx Init Begin...");
  127. #endif
  128. id = W25qxx_ReadID();
  129. #if (_W25QXX_DEBUG == 1)
  130. W25QXX_DEBUG_PRINTF("w25qxx ID:0x%06lx", id);
  131. #endif
  132. switch (id & 0x0000FFFF)
  133. {
  134. case 0x401A: // w25q512
  135. w25qxx.ID = W25Q512;
  136. w25qxx.BlockCount = 1024;
  137. #if (_W25QXX_DEBUG == 1)
  138. W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q512");
  139. #endif
  140. break;
  141. case 0x4019: // w25q256
  142. w25qxx.ID = W25Q256;
  143. w25qxx.BlockCount = 512;
  144. #if (_W25QXX_DEBUG == 1)
  145. W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q256");
  146. #endif
  147. break;
  148. case 0x4018: // w25q128
  149. w25qxx.ID = W25Q128;
  150. w25qxx.BlockCount = 256;
  151. #if (_W25QXX_DEBUG == 1)
  152. W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q128");
  153. #endif
  154. break;
  155. case 0x4017: // w25q64
  156. w25qxx.ID = W25Q64;
  157. w25qxx.BlockCount = 128;
  158. #if (_W25QXX_DEBUG == 1)
  159. W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q64");
  160. #endif
  161. break;
  162. case 0x4016: // w25q32
  163. w25qxx.ID = W25Q32;
  164. w25qxx.BlockCount = 64;
  165. #if (_W25QXX_DEBUG == 1)
  166. W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q32");
  167. #endif
  168. break;
  169. case 0x4015: // w25q16
  170. w25qxx.ID = W25Q16;
  171. w25qxx.BlockCount = 32;
  172. #if (_W25QXX_DEBUG == 1)
  173. W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q16");
  174. #endif
  175. break;
  176. case 0x4014: // w25q80
  177. w25qxx.ID = W25Q80;
  178. w25qxx.BlockCount = 16;
  179. #if (_W25QXX_DEBUG == 1)
  180. W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q80");
  181. #endif
  182. break;
  183. case 0x4013: // w25q40
  184. w25qxx.ID = W25Q40;
  185. w25qxx.BlockCount = 8;
  186. #if (_W25QXX_DEBUG == 1)
  187. W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q40");
  188. #endif
  189. break;
  190. case 0x4012: // w25q20
  191. w25qxx.ID = W25Q20;
  192. w25qxx.BlockCount = 4;
  193. #if (_W25QXX_DEBUG == 1)
  194. W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q20");
  195. #endif
  196. break;
  197. case 0x4011: // w25q10
  198. w25qxx.ID = W25Q10;
  199. w25qxx.BlockCount = 2;
  200. #if (_W25QXX_DEBUG == 1)
  201. W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q10");
  202. #endif
  203. break;
  204. default:
  205. #if (_W25QXX_DEBUG == 1)
  206. W25QXX_DEBUG_PRINTF("w25qxx Unknown ID");
  207. #endif
  208. w25qxx.Lock = 0;
  209. return false;
  210. }
  211. w25qxx.PageSize = 256;
  212. w25qxx.SectorSize = 0x1000;
  213. w25qxx.SectorCount = w25qxx.BlockCount * 16;
  214. w25qxx.PageCount = (w25qxx.SectorCount * w25qxx.SectorSize) / w25qxx.PageSize;
  215. w25qxx.BlockSize = w25qxx.SectorSize * 16;
  216. w25qxx.CapacityInKiloByte = (w25qxx.SectorCount * w25qxx.SectorSize) / 1024;
  217. W25qxx_ReadUniqID();
  218. W25qxx_ReadStatusRegister(1);
  219. W25qxx_ReadStatusRegister(2);
  220. W25qxx_ReadStatusRegister(3);
  221. #if (_W25QXX_DEBUG == 1)
  222. W25QXX_DEBUG_PRINTF("w25qxx Page Size: %d Bytes", w25qxx.PageSize);
  223. W25QXX_DEBUG_PRINTF("w25qxx Page Count: %ld", w25qxx.PageCount);
  224. W25QXX_DEBUG_PRINTF("w25qxx Sector Size: %ld Bytes", w25qxx.SectorSize);
  225. W25QXX_DEBUG_PRINTF("w25qxx Sector Count: %ld", w25qxx.SectorCount);
  226. W25QXX_DEBUG_PRINTF("w25qxx Block Size: %ld Bytes", w25qxx.BlockSize);
  227. W25QXX_DEBUG_PRINTF("w25qxx Block Count: %ld", w25qxx.BlockCount);
  228. W25QXX_DEBUG_PRINTF("w25qxx Capacity: %ld KiloBytes", w25qxx.CapacityInKiloByte);
  229. W25QXX_DEBUG_PRINTF("w25qxx Init Done");
  230. #endif
  231. w25qxx.Lock = 0;
  232. return true;
  233. }
  234. // ###################################################################################################################
  235. void W25qxx_EraseChip(void)
  236. {
  237. while (w25qxx.Lock == 1)
  238. W25qxx_Delay(1);
  239. w25qxx.Lock = 1;
  240. #if (_W25QXX_DEBUG == 1)
  241. uint32_t StartTime = W25qxx_Ticks();
  242. W25QXX_DEBUG_PRINTF("w25qxx EraseChip Begin...");
  243. #endif
  244. W25qxx_WriteEnable();
  245. W25qxx_Select();
  246. W25qxx_Spi(0xC7);
  247. W25qxx_Deselect();
  248. W25qxx_WaitForWriteEnd();
  249. #if (_W25QXX_DEBUG == 1)
  250. W25QXX_DEBUG_PRINTF("w25qxx EraseBlock done after %ld ms!", W25qxx_Ticks() - StartTime);
  251. #endif
  252. W25qxx_Delay(10);
  253. w25qxx.Lock = 0;
  254. }
  255. // ###################################################################################################################
  256. void W25qxx_EraseSector(uint32_t SectorAddr)
  257. {
  258. while (w25qxx.Lock == 1)
  259. W25qxx_Delay(1);
  260. w25qxx.Lock = 1;
  261. #if (_W25QXX_DEBUG == 1)
  262. uint32_t StartTime = W25qxx_Ticks();
  263. W25QXX_DEBUG_PRINTF("w25qxx EraseSector %ld Begin...", SectorAddr);
  264. #endif
  265. W25qxx_WaitForWriteEnd();
  266. SectorAddr = SectorAddr * w25qxx.SectorSize;
  267. W25qxx_WriteEnable();
  268. W25qxx_Select();
  269. W25qxx_Spi(0x20);
  270. if (w25qxx.ID >= W25Q256)
  271. W25qxx_Spi((SectorAddr & 0xFF000000) >> 24);
  272. W25qxx_Spi((SectorAddr & 0xFF0000) >> 16);
  273. W25qxx_Spi((SectorAddr & 0xFF00) >> 8);
  274. W25qxx_Spi(SectorAddr & 0xFF);
  275. W25qxx_Deselect();
  276. W25qxx_WaitForWriteEnd();
  277. #if (_W25QXX_DEBUG == 1)
  278. W25QXX_DEBUG_PRINTF("w25qxx EraseSector done after %ld ms", W25qxx_Ticks() - StartTime);
  279. #endif
  280. W25qxx_Delay(1);
  281. w25qxx.Lock = 0;
  282. }
  283. // ###################################################################################################################
  284. void W25qxx_EraseBlock(uint32_t BlockAddr)
  285. {
  286. while (w25qxx.Lock == 1)
  287. W25qxx_Delay(1);
  288. w25qxx.Lock = 1;
  289. #if (_W25QXX_DEBUG == 1)
  290. W25QXX_DEBUG_PRINTF("w25qxx EraseBlock %ld Begin...", BlockAddr);
  291. W25qxx_Delay(100);
  292. uint32_t StartTime = W25qxx_Ticks();
  293. #endif
  294. W25qxx_WaitForWriteEnd();
  295. BlockAddr = BlockAddr * w25qxx.SectorSize * 16;
  296. W25qxx_WriteEnable();
  297. W25qxx_Select();
  298. W25qxx_Spi(0xD8);
  299. if (w25qxx.ID >= W25Q256)
  300. W25qxx_Spi((BlockAddr & 0xFF000000) >> 24);
  301. W25qxx_Spi((BlockAddr & 0xFF0000) >> 16);
  302. W25qxx_Spi((BlockAddr & 0xFF00) >> 8);
  303. W25qxx_Spi(BlockAddr & 0xFF);
  304. W25qxx_Deselect();
  305. W25qxx_WaitForWriteEnd();
  306. #if (_W25QXX_DEBUG == 1)
  307. W25QXX_DEBUG_PRINTF("w25qxx EraseBlock done after %ld ms", W25qxx_Ticks() - StartTime);
  308. W25qxx_Delay(100);
  309. #endif
  310. W25qxx_Delay(1);
  311. w25qxx.Lock = 0;
  312. }
  313. // ###################################################################################################################
  314. uint32_t W25qxx_PageToSector(uint32_t PageAddress)
  315. {
  316. return ((PageAddress * w25qxx.PageSize) / w25qxx.SectorSize);
  317. }
  318. // ###################################################################################################################
  319. uint32_t W25qxx_PageToBlock(uint32_t PageAddress)
  320. {
  321. return ((PageAddress * w25qxx.PageSize) / w25qxx.BlockSize);
  322. }
  323. // ###################################################################################################################
  324. uint32_t W25qxx_SectorToBlock(uint32_t SectorAddress)
  325. {
  326. return ((SectorAddress * w25qxx.SectorSize) / w25qxx.BlockSize);
  327. }
  328. // ###################################################################################################################
  329. uint32_t W25qxx_SectorToPage(uint32_t SectorAddress)
  330. {
  331. return (SectorAddress * w25qxx.SectorSize) / w25qxx.PageSize;
  332. }
  333. // ###################################################################################################################
  334. uint32_t W25qxx_BlockToPage(uint32_t BlockAddress)
  335. {
  336. return (BlockAddress * w25qxx.BlockSize) / w25qxx.PageSize;
  337. }
  338. // ###################################################################################################################
  339. bool W25qxx_IsEmptyPage(uint32_t Page_Address, uint32_t OffsetInByte, uint32_t NumByteToCheck_up_to_PageSize)
  340. {
  341. while (w25qxx.Lock == 1)
  342. W25qxx_Delay(1);
  343. w25qxx.Lock = 1;
  344. if (((NumByteToCheck_up_to_PageSize + OffsetInByte) > w25qxx.PageSize) || (NumByteToCheck_up_to_PageSize == 0))
  345. NumByteToCheck_up_to_PageSize = w25qxx.PageSize - OffsetInByte;
  346. #if (_W25QXX_DEBUG == 1)
  347. W25QXX_DEBUG_PRINTF("w25qxx CheckPage:%ld, Offset:%ld, Bytes:%ld begin...", Page_Address, OffsetInByte, NumByteToCheck_up_to_PageSize);
  348. W25qxx_Delay(100);
  349. uint32_t StartTime = W25qxx_Ticks();
  350. #endif
  351. uint8_t pBuffer[32];
  352. uint32_t WorkAddress;
  353. uint32_t i;
  354. for (i = OffsetInByte; i < w25qxx.PageSize; i += sizeof(pBuffer))
  355. {
  356. W25qxx_Select();
  357. WorkAddress = (i + Page_Address * w25qxx.PageSize);
  358. W25qxx_Spi(0x0B);
  359. if (w25qxx.ID >= W25Q256)
  360. W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
  361. W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
  362. W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
  363. W25qxx_Spi(WorkAddress & 0xFF);
  364. W25qxx_Spi(W25QXX_DUMMY_BYTE);
  365. W25qxx_Spi_Receive(pBuffer, sizeof(pBuffer), 100);
  366. W25qxx_Deselect();
  367. for (uint8_t x = 0; x < sizeof(pBuffer); x++)
  368. {
  369. if (pBuffer[x] != 0xFF)
  370. goto NOT_EMPTY;
  371. }
  372. }
  373. if ((w25qxx.PageSize + OffsetInByte) % sizeof(pBuffer) != 0)
  374. {
  375. i -= sizeof(pBuffer);
  376. for (; i < w25qxx.PageSize; i++)
  377. {
  378. W25qxx_Select();
  379. WorkAddress = (i + Page_Address * w25qxx.PageSize);
  380. W25qxx_Spi(0x0B);
  381. if (w25qxx.ID >= W25Q256)
  382. W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
  383. W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
  384. W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
  385. W25qxx_Spi(WorkAddress & 0xFF);
  386. W25qxx_Spi(W25QXX_DUMMY_BYTE);
  387. W25qxx_Spi_Receive(pBuffer, 1, 100);
  388. W25qxx_Deselect();
  389. if (pBuffer[0] != 0xFF)
  390. goto NOT_EMPTY;
  391. }
  392. }
  393. #if (_W25QXX_DEBUG == 1)
  394. W25QXX_DEBUG_PRINTF("w25qxx CheckPage is Empty in %ld ms", W25qxx_Ticks() - StartTime);
  395. W25qxx_Delay(100);
  396. #endif
  397. w25qxx.Lock = 0;
  398. return true;
  399. NOT_EMPTY:
  400. #if (_W25QXX_DEBUG == 1)
  401. W25QXX_DEBUG_PRINTF("w25qxx CheckPage is Not Empty in %ld ms", W25qxx_Ticks() - StartTime);
  402. W25qxx_Delay(100);
  403. #endif
  404. w25qxx.Lock = 0;
  405. return false;
  406. }
  407. // ###################################################################################################################
  408. bool W25qxx_IsEmptySector(uint32_t Sector_Address, uint32_t OffsetInByte, uint32_t NumByteToCheck_up_to_SectorSize)
  409. {
  410. while (w25qxx.Lock == 1)
  411. W25qxx_Delay(1);
  412. w25qxx.Lock = 1;
  413. if ((NumByteToCheck_up_to_SectorSize > w25qxx.SectorSize) || (NumByteToCheck_up_to_SectorSize == 0))
  414. NumByteToCheck_up_to_SectorSize = w25qxx.SectorSize;
  415. #if (_W25QXX_DEBUG == 1)
  416. W25QXX_DEBUG_PRINTF("w25qxx CheckSector:%ld, Offset:%ld, Bytes:%ld begin...", Sector_Address, OffsetInByte, NumByteToCheck_up_to_SectorSize);
  417. W25qxx_Delay(100);
  418. uint32_t StartTime = W25qxx_Ticks();
  419. #endif
  420. uint8_t pBuffer[32];
  421. uint32_t WorkAddress;
  422. uint32_t i;
  423. for (i = OffsetInByte; i < w25qxx.SectorSize; i += sizeof(pBuffer))
  424. {
  425. W25qxx_Select();
  426. WorkAddress = (i + Sector_Address * w25qxx.SectorSize);
  427. W25qxx_Spi(0x0B);
  428. if (w25qxx.ID >= W25Q256)
  429. W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
  430. W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
  431. W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
  432. W25qxx_Spi(WorkAddress & 0xFF);
  433. W25qxx_Spi(W25QXX_DUMMY_BYTE);
  434. W25qxx_Spi_Receive(pBuffer, sizeof(pBuffer), 100);
  435. W25qxx_Deselect();
  436. for (uint8_t x = 0; x < sizeof(pBuffer); x++)
  437. {
  438. if (pBuffer[x] != 0xFF)
  439. goto NOT_EMPTY;
  440. }
  441. }
  442. if ((w25qxx.SectorSize + OffsetInByte) % sizeof(pBuffer) != 0)
  443. {
  444. i -= sizeof(pBuffer);
  445. for (; i < w25qxx.SectorSize; i++)
  446. {
  447. W25qxx_Select();
  448. WorkAddress = (i + Sector_Address * w25qxx.SectorSize);
  449. W25qxx_Spi(0x0B);
  450. if (w25qxx.ID >= W25Q256)
  451. W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
  452. W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
  453. W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
  454. W25qxx_Spi(WorkAddress & 0xFF);
  455. W25qxx_Spi(W25QXX_DUMMY_BYTE);
  456. W25qxx_Spi_Receive(pBuffer, 1, 100);
  457. W25qxx_Deselect();
  458. if (pBuffer[0] != 0xFF)
  459. goto NOT_EMPTY;
  460. }
  461. }
  462. #if (_W25QXX_DEBUG == 1)
  463. W25QXX_DEBUG_PRINTF("w25qxx CheckSector is Empty in %ld ms", W25qxx_Ticks() - StartTime);
  464. W25qxx_Delay(100);
  465. #endif
  466. w25qxx.Lock = 0;
  467. return true;
  468. NOT_EMPTY:
  469. #if (_W25QXX_DEBUG == 1)
  470. W25QXX_DEBUG_PRINTF("w25qxx CheckSector is Not Empty in %ld ms", W25qxx_Ticks() - StartTime);
  471. W25qxx_Delay(100);
  472. #endif
  473. w25qxx.Lock = 0;
  474. return false;
  475. }
  476. // ###################################################################################################################
  477. bool W25qxx_IsEmptyBlock(uint32_t Block_Address, uint32_t OffsetInByte, uint32_t NumByteToCheck_up_to_BlockSize)
  478. {
  479. while (w25qxx.Lock == 1)
  480. W25qxx_Delay(1);
  481. w25qxx.Lock = 1;
  482. if ((NumByteToCheck_up_to_BlockSize > w25qxx.BlockSize) || (NumByteToCheck_up_to_BlockSize == 0))
  483. NumByteToCheck_up_to_BlockSize = w25qxx.BlockSize;
  484. #if (_W25QXX_DEBUG == 1)
  485. W25QXX_DEBUG_PRINTF("w25qxx CheckBlock:%ld, Offset:%ld, Bytes:%ld begin...", Block_Address, OffsetInByte, NumByteToCheck_up_to_BlockSize);
  486. W25qxx_Delay(100);
  487. uint32_t StartTime = W25qxx_Ticks();
  488. #endif
  489. uint8_t pBuffer[32];
  490. uint32_t WorkAddress;
  491. uint32_t i;
  492. for (i = OffsetInByte; i < w25qxx.BlockSize; i += sizeof(pBuffer))
  493. {
  494. W25qxx_Select();
  495. WorkAddress = (i + Block_Address * w25qxx.BlockSize);
  496. W25qxx_Spi(0x0B);
  497. if (w25qxx.ID >= W25Q256)
  498. W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
  499. W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
  500. W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
  501. W25qxx_Spi(WorkAddress & 0xFF);
  502. W25qxx_Spi(W25QXX_DUMMY_BYTE);
  503. W25qxx_Spi_Receive(pBuffer, sizeof(pBuffer), 100);
  504. W25qxx_Deselect();
  505. for (uint8_t x = 0; x < sizeof(pBuffer); x++)
  506. {
  507. if (pBuffer[x] != 0xFF)
  508. goto NOT_EMPTY;
  509. }
  510. }
  511. if ((w25qxx.BlockSize + OffsetInByte) % sizeof(pBuffer) != 0)
  512. {
  513. i -= sizeof(pBuffer);
  514. for (; i < w25qxx.BlockSize; i++)
  515. {
  516. W25qxx_Select();
  517. WorkAddress = (i + Block_Address * w25qxx.BlockSize);
  518. W25qxx_Spi(0x0B);
  519. if (w25qxx.ID >= W25Q256)
  520. W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
  521. W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
  522. W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
  523. W25qxx_Spi(WorkAddress & 0xFF);
  524. W25qxx_Spi(W25QXX_DUMMY_BYTE);
  525. W25qxx_Spi_Receive(pBuffer, 1, 100);
  526. W25qxx_Deselect();
  527. if (pBuffer[0] != 0xFF)
  528. goto NOT_EMPTY;
  529. }
  530. }
  531. #if (_W25QXX_DEBUG == 1)
  532. W25QXX_DEBUG_PRINTF("w25qxx CheckBlock is Empty in %ld ms", W25qxx_Ticks() - StartTime);
  533. W25qxx_Delay(100);
  534. #endif
  535. w25qxx.Lock = 0;
  536. return true;
  537. NOT_EMPTY:
  538. #if (_W25QXX_DEBUG == 1)
  539. W25QXX_DEBUG_PRINTF("w25qxx CheckBlock is Not Empty in %ld ms", W25qxx_Ticks() - StartTime);
  540. W25qxx_Delay(100);
  541. #endif
  542. w25qxx.Lock = 0;
  543. return false;
  544. }
  545. // ###################################################################################################################
  546. void W25qxx_WriteByte(uint8_t pBuffer, uint32_t WriteAddr_inBytes)
  547. {
  548. while (w25qxx.Lock == 1)
  549. W25qxx_Delay(1);
  550. w25qxx.Lock = 1;
  551. #if (_W25QXX_DEBUG == 1)
  552. uint32_t StartTime = W25qxx_Ticks();
  553. W25QXX_DEBUG_PRINTF("w25qxx WriteByte 0x%02X at address %ld begin...", pBuffer, WriteAddr_inBytes);
  554. #endif
  555. W25qxx_WaitForWriteEnd();
  556. W25qxx_WriteEnable();
  557. W25qxx_Select();
  558. W25qxx_Spi(0x02);
  559. if (w25qxx.ID >= W25Q256)
  560. W25qxx_Spi((WriteAddr_inBytes & 0xFF000000) >> 24);
  561. W25qxx_Spi((WriteAddr_inBytes & 0xFF0000) >> 16);
  562. W25qxx_Spi((WriteAddr_inBytes & 0xFF00) >> 8);
  563. W25qxx_Spi(WriteAddr_inBytes & 0xFF);
  564. W25qxx_Spi(pBuffer);
  565. W25qxx_Deselect();
  566. W25qxx_WaitForWriteEnd();
  567. #if (_W25QXX_DEBUG == 1)
  568. W25QXX_DEBUG_PRINTF("w25qxx WriteByte done after %ld ms", W25qxx_Ticks() - StartTime);
  569. #endif
  570. w25qxx.Lock = 0;
  571. }
  572. // ###################################################################################################################
  573. void W25qxx_WritePage(uint8_t *pBuffer, uint32_t Page_Address, uint32_t OffsetInByte, uint32_t NumByteToWrite_up_to_PageSize)
  574. {
  575. while (w25qxx.Lock == 1)
  576. W25qxx_Delay(1);
  577. w25qxx.Lock = 1;
  578. if (((NumByteToWrite_up_to_PageSize + OffsetInByte) > w25qxx.PageSize) || (NumByteToWrite_up_to_PageSize == 0))
  579. NumByteToWrite_up_to_PageSize = w25qxx.PageSize - OffsetInByte;
  580. if ((OffsetInByte + NumByteToWrite_up_to_PageSize) > w25qxx.PageSize)
  581. NumByteToWrite_up_to_PageSize = w25qxx.PageSize - OffsetInByte;
  582. #if (_W25QXX_DEBUG == 1)
  583. W25QXX_DEBUG_PRINTF("w25qxx WritePage:%ld, Offset:%ld ,Writes %ld Bytes, begin...", Page_Address, OffsetInByte, NumByteToWrite_up_to_PageSize);
  584. W25qxx_Delay(100);
  585. uint32_t StartTime = W25qxx_Ticks();
  586. #endif
  587. W25qxx_WaitForWriteEnd();
  588. W25qxx_WriteEnable();
  589. W25qxx_Select();
  590. W25qxx_Spi(0x02);
  591. Page_Address = (Page_Address * w25qxx.PageSize) + OffsetInByte;
  592. if (w25qxx.ID >= W25Q256)
  593. W25qxx_Spi((Page_Address & 0xFF000000) >> 24);
  594. W25qxx_Spi((Page_Address & 0xFF0000) >> 16);
  595. W25qxx_Spi((Page_Address & 0xFF00) >> 8);
  596. W25qxx_Spi(Page_Address & 0xFF);
  597. W25qxx_Spi_Transmit(pBuffer, NumByteToWrite_up_to_PageSize, 100);
  598. W25qxx_Deselect();
  599. W25qxx_WaitForWriteEnd();
  600. #if (_W25QXX_DEBUG == 1)
  601. StartTime = W25qxx_Ticks() - StartTime;
  602. W25QXX_DEBUG_PRINTF("w25qxx WritePage done after %ld ms", StartTime);
  603. W25qxx_Delay(100);
  604. #endif
  605. W25qxx_Delay(1);
  606. w25qxx.Lock = 0;
  607. }
  608. // ###################################################################################################################
  609. void W25qxx_WriteSector(uint8_t *pBuffer, uint32_t Sector_Address, uint32_t OffsetInByte, uint32_t NumByteToWrite_up_to_SectorSize)
  610. {
  611. if ((NumByteToWrite_up_to_SectorSize > w25qxx.SectorSize) || (NumByteToWrite_up_to_SectorSize == 0))
  612. NumByteToWrite_up_to_SectorSize = w25qxx.SectorSize;
  613. #if (_W25QXX_DEBUG == 1)
  614. W25QXX_DEBUG_PRINTF("+++w25qxx WriteSector:%ld, Offset:%ld ,Write %ld Bytes, begin...", Sector_Address, OffsetInByte, NumByteToWrite_up_to_SectorSize);
  615. W25qxx_Delay(100);
  616. #endif
  617. if (OffsetInByte >= w25qxx.SectorSize)
  618. {
  619. #if (_W25QXX_DEBUG == 1)
  620. W25QXX_DEBUG_PRINTF("---w25qxx WriteSector Faild!");
  621. W25qxx_Delay(100);
  622. #endif
  623. return;
  624. }
  625. uint32_t StartPage;
  626. int32_t BytesToWrite;
  627. uint32_t LocalOffset;
  628. if ((OffsetInByte + NumByteToWrite_up_to_SectorSize) > w25qxx.SectorSize)
  629. BytesToWrite = w25qxx.SectorSize - OffsetInByte;
  630. else
  631. BytesToWrite = NumByteToWrite_up_to_SectorSize;
  632. StartPage = W25qxx_SectorToPage(Sector_Address) + (OffsetInByte / w25qxx.PageSize);
  633. LocalOffset = OffsetInByte % w25qxx.PageSize;
  634. do
  635. {
  636. W25qxx_WritePage(pBuffer, StartPage, LocalOffset, BytesToWrite);
  637. StartPage++;
  638. BytesToWrite -= w25qxx.PageSize - LocalOffset;
  639. pBuffer += w25qxx.PageSize - LocalOffset;
  640. LocalOffset = 0;
  641. } while (BytesToWrite > 0);
  642. #if (_W25QXX_DEBUG == 1)
  643. W25QXX_DEBUG_PRINTF("---w25qxx WriteSector Done");
  644. W25qxx_Delay(100);
  645. #endif
  646. }
  647. // ###################################################################################################################
  648. void W25qxx_WriteBlock(uint8_t *pBuffer, uint32_t Block_Address, uint32_t OffsetInByte, uint32_t NumByteToWrite_up_to_BlockSize)
  649. {
  650. if ((NumByteToWrite_up_to_BlockSize > w25qxx.BlockSize) || (NumByteToWrite_up_to_BlockSize == 0))
  651. NumByteToWrite_up_to_BlockSize = w25qxx.BlockSize;
  652. #if (_W25QXX_DEBUG == 1)
  653. W25QXX_DEBUG_PRINTF("+++w25qxx WriteBlock:%ld, Offset:%ld ,Write %ld Bytes, begin...", Block_Address, OffsetInByte, NumByteToWrite_up_to_BlockSize);
  654. W25qxx_Delay(100);
  655. #endif
  656. if (OffsetInByte >= w25qxx.BlockSize)
  657. {
  658. #if (_W25QXX_DEBUG == 1)
  659. W25QXX_DEBUG_PRINTF("---w25qxx WriteBlock Faild!");
  660. W25qxx_Delay(100);
  661. #endif
  662. return;
  663. }
  664. uint32_t StartPage;
  665. int32_t BytesToWrite;
  666. uint32_t LocalOffset;
  667. if ((OffsetInByte + NumByteToWrite_up_to_BlockSize) > w25qxx.BlockSize)
  668. BytesToWrite = w25qxx.BlockSize - OffsetInByte;
  669. else
  670. BytesToWrite = NumByteToWrite_up_to_BlockSize;
  671. StartPage = W25qxx_BlockToPage(Block_Address) + (OffsetInByte / w25qxx.PageSize);
  672. LocalOffset = OffsetInByte % w25qxx.PageSize;
  673. do
  674. {
  675. W25qxx_WritePage(pBuffer, StartPage, LocalOffset, BytesToWrite);
  676. StartPage++;
  677. BytesToWrite -= w25qxx.PageSize - LocalOffset;
  678. pBuffer += w25qxx.PageSize - LocalOffset;
  679. LocalOffset = 0;
  680. } while (BytesToWrite > 0);
  681. #if (_W25QXX_DEBUG == 1)
  682. W25QXX_DEBUG_PRINTF("---w25qxx WriteBlock Done");
  683. W25qxx_Delay(100);
  684. #endif
  685. }
  686. // ###################################################################################################################
  687. void W25qxx_ReadByte(uint8_t *pBuffer, uint32_t Bytes_Address)
  688. {
  689. while (w25qxx.Lock == 1)
  690. W25qxx_Delay(1);
  691. w25qxx.Lock = 1;
  692. #if (_W25QXX_DEBUG == 1)
  693. uint32_t StartTime = W25qxx_Ticks();
  694. W25QXX_DEBUG_PRINTF("w25qxx ReadByte at address %ld begin...", Bytes_Address);
  695. #endif
  696. W25qxx_Select();
  697. W25qxx_Spi(0x0B);
  698. if (w25qxx.ID >= W25Q256)
  699. W25qxx_Spi((Bytes_Address & 0xFF000000) >> 24);
  700. W25qxx_Spi((Bytes_Address & 0xFF0000) >> 16);
  701. W25qxx_Spi((Bytes_Address & 0xFF00) >> 8);
  702. W25qxx_Spi(Bytes_Address & 0xFF);
  703. W25qxx_Spi(W25QXX_DUMMY_BYTE);
  704. *pBuffer = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  705. W25qxx_Deselect();
  706. #if (_W25QXX_DEBUG == 1)
  707. W25QXX_DEBUG_PRINTF("w25qxx ReadByte 0x%02X done after %ld ms", *pBuffer, W25qxx_Ticks() - StartTime);
  708. #endif
  709. w25qxx.Lock = 0;
  710. }
  711. // ###################################################################################################################
  712. void W25qxx_ReadBytes(uint8_t *pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead)
  713. {
  714. while (w25qxx.Lock == 1)
  715. W25qxx_Delay(1);
  716. w25qxx.Lock = 1;
  717. #if (_W25QXX_DEBUG == 1)
  718. uint32_t StartTime = W25qxx_Ticks();
  719. W25QXX_DEBUG_PRINTF("w25qxx ReadBytes at Address:%ld, %ld Bytes begin...", ReadAddr, NumByteToRead);
  720. #endif
  721. W25qxx_Select();
  722. W25qxx_Spi(0x0B);
  723. if (w25qxx.ID >= W25Q256)
  724. W25qxx_Spi((ReadAddr & 0xFF000000) >> 24);
  725. W25qxx_Spi((ReadAddr & 0xFF0000) >> 16);
  726. W25qxx_Spi((ReadAddr & 0xFF00) >> 8);
  727. W25qxx_Spi(ReadAddr & 0xFF);
  728. W25qxx_Spi(W25QXX_DUMMY_BYTE);
  729. W25qxx_Spi_Receive(pBuffer, NumByteToRead, 2000);
  730. W25qxx_Deselect();
  731. #if (_W25QXX_DEBUG == 1)
  732. StartTime = W25qxx_Ticks() - StartTime;
  733. W25QXX_DEBUG_PRINTF("w25qxx ReadBytes done after %ld ms", StartTime);
  734. W25qxx_Delay(100);
  735. #endif
  736. W25qxx_Delay(1);
  737. w25qxx.Lock = 0;
  738. }
  739. // ###################################################################################################################
  740. void W25qxx_ReadPage(uint8_t *pBuffer, uint32_t Page_Address, uint32_t OffsetInByte, uint32_t NumByteToRead_up_to_PageSize)
  741. {
  742. while (w25qxx.Lock == 1)
  743. W25qxx_Delay(1);
  744. w25qxx.Lock = 1;
  745. if ((NumByteToRead_up_to_PageSize > w25qxx.PageSize) || (NumByteToRead_up_to_PageSize == 0))
  746. NumByteToRead_up_to_PageSize = w25qxx.PageSize;
  747. if ((OffsetInByte + NumByteToRead_up_to_PageSize) > w25qxx.PageSize)
  748. NumByteToRead_up_to_PageSize = w25qxx.PageSize - OffsetInByte;
  749. #if (_W25QXX_DEBUG == 1)
  750. W25QXX_DEBUG_PRINTF("w25qxx ReadPage:%ld, Offset:%ld ,Read %ld Bytes, begin...", Page_Address, OffsetInByte, NumByteToRead_up_to_PageSize);
  751. W25qxx_Delay(100);
  752. uint32_t StartTime = W25qxx_Ticks();
  753. #endif
  754. Page_Address = Page_Address * w25qxx.PageSize + OffsetInByte;
  755. W25qxx_Select();
  756. W25qxx_Spi(0x0B);
  757. if (w25qxx.ID >= W25Q256)
  758. W25qxx_Spi((Page_Address & 0xFF000000) >> 24);
  759. W25qxx_Spi((Page_Address & 0xFF0000) >> 16);
  760. W25qxx_Spi((Page_Address & 0xFF00) >> 8);
  761. W25qxx_Spi(Page_Address & 0xFF);
  762. W25qxx_Spi(W25QXX_DUMMY_BYTE);
  763. W25qxx_Spi_Receive(pBuffer, NumByteToRead_up_to_PageSize, 100);
  764. W25qxx_Deselect();
  765. #if (_W25QXX_DEBUG == 1)
  766. StartTime = W25qxx_Ticks() - StartTime;
  767. W25QXX_DEBUG_PRINTF("w25qxx ReadPage done after %ld ms", StartTime);
  768. W25qxx_Delay(100);
  769. #endif
  770. W25qxx_Delay(1);
  771. w25qxx.Lock = 0;
  772. }
  773. // ###################################################################################################################
  774. void W25qxx_ReadSector(uint8_t *pBuffer, uint32_t Sector_Address, uint32_t OffsetInByte, uint32_t NumByteToRead_up_to_SectorSize)
  775. {
  776. if ((NumByteToRead_up_to_SectorSize > w25qxx.SectorSize) || (NumByteToRead_up_to_SectorSize == 0))
  777. NumByteToRead_up_to_SectorSize = w25qxx.SectorSize;
  778. #if (_W25QXX_DEBUG == 1)
  779. W25QXX_DEBUG_PRINTF("+++w25qxx ReadSector:%ld, Offset:%ld ,Read %ld Bytes, begin...", Sector_Address, OffsetInByte, NumByteToRead_up_to_SectorSize);
  780. W25qxx_Delay(100);
  781. #endif
  782. if (OffsetInByte >= w25qxx.SectorSize)
  783. {
  784. #if (_W25QXX_DEBUG == 1)
  785. W25QXX_DEBUG_PRINTF("---w25qxx ReadSector Faild!");
  786. W25qxx_Delay(100);
  787. #endif
  788. return;
  789. }
  790. uint32_t StartPage;
  791. int32_t BytesToRead;
  792. uint32_t LocalOffset;
  793. if ((OffsetInByte + NumByteToRead_up_to_SectorSize) > w25qxx.SectorSize)
  794. BytesToRead = w25qxx.SectorSize - OffsetInByte;
  795. else
  796. BytesToRead = NumByteToRead_up_to_SectorSize;
  797. StartPage = W25qxx_SectorToPage(Sector_Address) + (OffsetInByte / w25qxx.PageSize);
  798. LocalOffset = OffsetInByte % w25qxx.PageSize;
  799. do
  800. {
  801. W25qxx_ReadPage(pBuffer, StartPage, LocalOffset, BytesToRead);
  802. StartPage++;
  803. BytesToRead -= w25qxx.PageSize - LocalOffset;
  804. pBuffer += w25qxx.PageSize - LocalOffset;
  805. LocalOffset = 0;
  806. } while (BytesToRead > 0);
  807. #if (_W25QXX_DEBUG == 1)
  808. W25QXX_DEBUG_PRINTF("---w25qxx ReadSector Done");
  809. W25qxx_Delay(100);
  810. #endif
  811. }
  812. // ###################################################################################################################
  813. void W25qxx_ReadBlock(uint8_t *pBuffer, uint32_t Block_Address, uint32_t OffsetInByte, uint32_t NumByteToRead_up_to_BlockSize)
  814. {
  815. if ((NumByteToRead_up_to_BlockSize > w25qxx.BlockSize) || (NumByteToRead_up_to_BlockSize == 0))
  816. NumByteToRead_up_to_BlockSize = w25qxx.BlockSize;
  817. #if (_W25QXX_DEBUG == 1)
  818. W25QXX_DEBUG_PRINTF("+++w25qxx ReadBlock:%ld, Offset:%ld ,Read %ld Bytes, begin...", Block_Address, OffsetInByte, NumByteToRead_up_to_BlockSize);
  819. W25qxx_Delay(100);
  820. #endif
  821. if (OffsetInByte >= w25qxx.BlockSize)
  822. {
  823. #if (_W25QXX_DEBUG == 1)
  824. W25QXX_DEBUG_PRINTF("w25qxx ReadBlock Faild!");
  825. W25qxx_Delay(100);
  826. #endif
  827. return;
  828. }
  829. uint32_t StartPage;
  830. int32_t BytesToRead;
  831. uint32_t LocalOffset;
  832. if ((OffsetInByte + NumByteToRead_up_to_BlockSize) > w25qxx.BlockSize)
  833. BytesToRead = w25qxx.BlockSize - OffsetInByte;
  834. else
  835. BytesToRead = NumByteToRead_up_to_BlockSize;
  836. StartPage = W25qxx_BlockToPage(Block_Address) + (OffsetInByte / w25qxx.PageSize);
  837. LocalOffset = OffsetInByte % w25qxx.PageSize;
  838. do
  839. {
  840. W25qxx_ReadPage(pBuffer, StartPage, LocalOffset, BytesToRead);
  841. StartPage++;
  842. BytesToRead -= w25qxx.PageSize - LocalOffset;
  843. pBuffer += w25qxx.PageSize - LocalOffset;
  844. LocalOffset = 0;
  845. } while (BytesToRead > 0);
  846. #if (_W25QXX_DEBUG == 1)
  847. W25QXX_DEBUG_PRINTF("---w25qxx ReadBlock Done");
  848. W25qxx_Delay(100);
  849. #endif
  850. }
  851. // ###################################################################################################################
  852. void W25qxx_SelfTest(void)
  853. {
  854. uint8_t write_data[256];
  855. uint8_t read_data[sizeof(write_data)];
  856. uint32_t page_address = 0; // 使用第一页进行测试
  857. int passed = 0;
  858. // 初始化
  859. W25qxx_Init();
  860. for (int i = 0; i < 16; i++)
  861. {
  862. #if _W25QXX_DEBUG
  863. W25QXX_DEBUG_PRINTF("flash test, cycle:%d", i);
  864. #endif
  865. memset(write_data, i, sizeof(write_data));
  866. memset(read_data, i + 1, sizeof(read_data));
  867. // 擦除0扇区
  868. #if _W25QXX_DEBUG
  869. W25QXX_DEBUG_PRINTF("Erasing Flash...");
  870. #endif
  871. W25qxx_EraseSector(0);
  872. // 写入数据到 Flash
  873. #if _W25QXX_DEBUG
  874. W25QXX_DEBUG_PRINTF("Writing data to Flash...");
  875. #endif
  876. W25qxx_WritePage(write_data, page_address, 0, sizeof(write_data));
  877. // 读取数据
  878. #if _W25QXX_DEBUG
  879. W25QXX_DEBUG_PRINTF("Reading data from Flash...");
  880. #endif
  881. W25qxx_ReadPage(read_data, page_address, 0, sizeof(read_data));
  882. // 验证数据是否一致
  883. bool success = true;
  884. for (int i = 0; i < sizeof(read_data); i++)
  885. {
  886. if (read_data[i] != write_data[i])
  887. {
  888. success = false;
  889. break;
  890. }
  891. }
  892. if (success)
  893. {
  894. #if _W25QXX_DEBUG
  895. W25QXX_DEBUG_PRINTF("Flash test passed! Data matches.");
  896. #endif
  897. passed++;
  898. }
  899. #if _W25QXX_DEBUG
  900. else
  901. {
  902. W25QXX_DEBUG_PRINTF("Flash test failed! Data mismatch.");
  903. }
  904. W25QXX_DEBUG_PRINTF("###################################################");
  905. #endif
  906. }
  907. #if _W25QXX_DEBUG
  908. W25QXX_DEBUG_PRINTF("Flash test finished. %d/256 passed.", passed);
  909. #endif
  910. }
  911. // ###################################################################################################################