obj_spi_w25qxx.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. #ifdef USE_W25QXX_SPI
  2. #if (_W25QXX_DEBUG==1)
  3. #include <stdio.h>
  4. #endif
  5. #include "sys_delay.h"
  6. #include "dev_spi_sdcard.h"
  7. #include "obj_spi_w25qxx.h"
  8. #define W25QXX_DUMMY_BYTE 0xA5
  9. SPI_HandleTypeDef hspi_w25q = {0};
  10. w25qxx_t w25qxx;
  11. #if (_W25QXX_USE_FREERTOS==1)
  12. #define W25qxx_Delay(delay) osDelay(delay)
  13. #include "cmsis_os.h"
  14. #else
  15. #define W25qxx_Delay(delay) delay_us(delay)//sys_delay_ms(delay)//
  16. #endif
  17. void W25qxx_Cs_Init()
  18. {
  19. GPIO_InitTypeDef GPIO_InitStruct;
  20. SPIx_CS_GPIO_CLK_ENABLE();
  21. GPIO_InitStruct.Pin = _W25QXX_CS_PIN ;
  22. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  23. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  24. HAL_GPIO_Init(_W25QXX_CS_GPIO, &GPIO_InitStruct);
  25. W25QXX_CS_HIGH();
  26. }
  27. /**
  28. * @brief SPI MSP Initialization
  29. * This function configures the hardware resources used in this example:
  30. * - Peripheral's clock enable
  31. * - Peripheral's GPIO Configuration
  32. * @param hspi: SPI handle pointer
  33. * @retval None
  34. */
  35. void HAL_SPI_MspInit_flash(void)
  36. {
  37. GPIO_InitTypeDef GPIO_InitStruct;
  38. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  39. /* Enable GPIO TX/RX clock */
  40. SPIx_SCK_GPIO_CLK_ENABLE();
  41. SPIx_MISO_GPIO_CLK_ENABLE();
  42. SPIx_MOSI_GPIO_CLK_ENABLE();
  43. /*##-2- Configure peripheral GPIO ##########################################*/
  44. /* SPI SCK GPIO pin configuration */
  45. GPIO_InitStruct.Pin = SPIx_SCK_PIN;
  46. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  47. GPIO_InitStruct.Pull = GPIO_PULLUP;
  48. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  49. GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
  50. HAL_GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
  51. /* SPI MISO GPIO pin configuration */
  52. GPIO_InitStruct.Pin = SPIx_MISO_PIN;
  53. HAL_GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStruct);
  54. /* SPI MOSI GPIO pin configuration */
  55. GPIO_InitStruct.Pin = SPIx_MOSI_PIN;
  56. HAL_GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct);
  57. }
  58. void W25qxx_Spi_Init(void)
  59. {
  60. /* Enable SPI clock */
  61. SPIx_CLK_ENABLE();
  62. hspi_w25q.Instance = SPIx; //SP2
  63. hspi_w25q.Init.Mode = SPI_MODE_MASTER; //设置SPI工作模式,设置为主模式
  64. hspi_w25q.Init.Direction = SPI_DIRECTION_2LINES; //设置SPI单向或者双向的数据模式:SPI设置为双线模式
  65. hspi_w25q.Init.DataSize = SPI_DATASIZE_8BIT; //设置SPI的数据大小:SPI发送接收8位帧结构
  66. hspi_w25q.Init.CLKPolarity = SPI_POLARITY_HIGH; //串行同步时钟的空闲状态为高电平
  67. hspi_w25q.Init.CLKPhase = SPI_PHASE_2EDGE; //串行同步时钟的第二个跳变沿(上升或下降)数据被采样
  68. hspi_w25q.Init.NSS = SPI_NSS_SOFT; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
  69. hspi_w25q.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; //定义波特率预分频的值:波特率预分频值为256
  70. hspi_w25q.Init.FirstBit = SPI_FIRSTBIT_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
  71. hspi_w25q.Init.TIMode = SPI_TIMODE_DISABLE; //关闭TI模式
  72. hspi_w25q.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; //关闭硬件CRC校验
  73. hspi_w25q.Init.CRCPolynomial = 7; //CRC值计算的多项式
  74. HAL_SPI_Init(&hspi_w25q);//初始化
  75. __HAL_SPI_ENABLE(&hspi_w25q); //使能SPI2
  76. }
  77. //###################################################################################################################
  78. uint8_t W25qxx_Spi(uint8_t Data)
  79. {
  80. uint8_t ret;
  81. HAL_SPI_TransmitReceive(&_W25QXX_SPI,&Data,&ret,1,10);
  82. return ret;
  83. }
  84. //###################################################################################################################
  85. uint32_t W25qxx_ReadID(void)
  86. {
  87. uint32_t Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;
  88. W25QXX_CS_LOW();
  89. W25qxx_Spi(0x9F);
  90. Temp0 = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  91. Temp1 = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  92. Temp2 = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  93. W25QXX_CS_HIGH();
  94. Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;
  95. return Temp;
  96. }
  97. //###################################################################################################################
  98. void W25qxx_ReadUniqID(void)
  99. {
  100. W25QXX_CS_LOW();
  101. W25qxx_Spi(0x4B);
  102. for(uint8_t i=0;i<4;i++)
  103. W25qxx_Spi(W25QXX_DUMMY_BYTE);
  104. for(uint8_t i=0;i<8;i++)
  105. w25qxx.UniqID[i] = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  106. W25QXX_CS_HIGH();
  107. }
  108. //###################################################################################################################
  109. void W25qxx_WriteEnable(void)
  110. {
  111. W25QXX_CS_LOW();
  112. W25qxx_Spi(0x06);
  113. W25QXX_CS_HIGH();
  114. W25qxx_Delay(1);
  115. }
  116. //###################################################################################################################
  117. void W25qxx_WriteDisable(void)
  118. {
  119. W25QXX_CS_LOW();
  120. W25qxx_Spi(0x04);
  121. W25QXX_CS_HIGH();
  122. W25qxx_Delay(1);
  123. }
  124. //###################################################################################################################
  125. uint8_t W25qxx_ReadStatusRegister(uint8_t SelectStatusRegister_1_2_3)
  126. {
  127. uint8_t status=0;
  128. W25QXX_CS_LOW();
  129. if(SelectStatusRegister_1_2_3==1)
  130. {
  131. W25qxx_Spi(0x05);
  132. status=W25qxx_Spi(W25QXX_DUMMY_BYTE);
  133. w25qxx.StatusRegister1 = status;
  134. }
  135. else if(SelectStatusRegister_1_2_3==2)
  136. {
  137. W25qxx_Spi(0x35);
  138. status=W25qxx_Spi(W25QXX_DUMMY_BYTE);
  139. w25qxx.StatusRegister2 = status;
  140. }
  141. else
  142. {
  143. W25qxx_Spi(0x15);
  144. status=W25qxx_Spi(W25QXX_DUMMY_BYTE);
  145. w25qxx.StatusRegister3 = status;
  146. }
  147. W25QXX_CS_HIGH();
  148. return status;
  149. }
  150. //###################################################################################################################
  151. void W25qxx_WriteStatusRegister(uint8_t SelectStatusRegister_1_2_3,uint8_t Data)
  152. {
  153. W25QXX_CS_LOW();
  154. if(SelectStatusRegister_1_2_3==1)
  155. {
  156. W25qxx_Spi(0x01);
  157. w25qxx.StatusRegister1 = Data;
  158. }
  159. else if(SelectStatusRegister_1_2_3==2)
  160. {
  161. W25qxx_Spi(0x31);
  162. w25qxx.StatusRegister2 = Data;
  163. }
  164. else
  165. {
  166. W25qxx_Spi(0x11);
  167. w25qxx.StatusRegister3 = Data;
  168. }
  169. W25qxx_Spi(Data);
  170. W25QXX_CS_HIGH();
  171. }
  172. //###################################################################################################################
  173. void W25qxx_WaitForWriteEnd(void)
  174. {
  175. W25qxx_Delay(1);
  176. W25QXX_CS_LOW();
  177. W25qxx_Spi(0x05);
  178. do
  179. {
  180. w25qxx.StatusRegister1 = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  181. W25qxx_Delay(1);
  182. }
  183. while ((w25qxx.StatusRegister1 & 0x01) == 0x01);
  184. W25QXX_CS_HIGH();
  185. }
  186. //###################################################################################################################
  187. bool W25qxx_Init(void)
  188. {
  189. w25qxx.Lock=1;
  190. while(HAL_GetTick()<100)
  191. W25qxx_Delay(1);
  192. uint32_t id;
  193. #if (_W25QXX_DEBUG==1)
  194. printf("w25qxx Init Begin...\r\n");
  195. #endif
  196. id=W25qxx_ReadID();
  197. #if (_W25QXX_DEBUG==1)
  198. printf("w25qxx ID:0x%X\r\n",id);
  199. #endif
  200. switch(id&0x0000FFFF)
  201. {
  202. case 0x401A: // w25q512
  203. w25qxx.ID=ID_W25Q512;
  204. w25qxx.BlockCount=1024;
  205. #if (_W25QXX_DEBUG==1)
  206. printf("w25qxx Chip: w25q512\r\n");
  207. #endif
  208. break;
  209. case 0x4019: // w25q256
  210. w25qxx.ID=ID_W25Q256;
  211. w25qxx.BlockCount=512;
  212. #if (_W25QXX_DEBUG==1)
  213. printf("w25qxx Chip: w25q256\r\n");
  214. #endif
  215. break;
  216. case 0x4018: // w25q128
  217. w25qxx.ID=ID_W25Q128;
  218. w25qxx.BlockCount=256;
  219. #if (_W25QXX_DEBUG==1)
  220. printf("w25qxx Chip: w25q128\r\n");
  221. #endif
  222. break;
  223. case 0x4017: // w25q64
  224. w25qxx.ID=ID_W25Q64;
  225. w25qxx.BlockCount=128;
  226. #if (_W25QXX_DEBUG==1)
  227. printf("w25qxx Chip: w25q64\r\n");
  228. #endif
  229. break;
  230. case 0x4016: // w25q32
  231. w25qxx.ID=ID_W25Q32;
  232. w25qxx.BlockCount=64;
  233. #if (_W25QXX_DEBUG==1)
  234. printf("w25qxx Chip: w25q32\r\n");
  235. #endif
  236. break;
  237. case 0x4015: // w25q16
  238. w25qxx.ID=ID_W25Q16;
  239. w25qxx.BlockCount=32;
  240. #if (_W25QXX_DEBUG==1)
  241. printf("w25qxx Chip: w25q16\r\n");
  242. #endif
  243. break;
  244. case 0x4014: // w25q80
  245. w25qxx.ID=ID_W25Q80;
  246. w25qxx.BlockCount=16;
  247. #if (_W25QXX_DEBUG==1)
  248. printf("w25qxx Chip: w25q80\r\n");
  249. #endif
  250. break;
  251. case 0x4013: // w25q40
  252. w25qxx.ID=ID_W25Q40;
  253. w25qxx.BlockCount=8;
  254. #if (_W25QXX_DEBUG==1)
  255. printf("w25qxx Chip: w25q40\r\n");
  256. #endif
  257. break;
  258. case 0x4012: // w25q20
  259. w25qxx.ID=ID_W25Q20;
  260. w25qxx.BlockCount=4;
  261. #if (_W25QXX_DEBUG==1)
  262. printf("w25qxx Chip: w25q20\r\n");
  263. #endif
  264. break;
  265. case 0x4011: // w25q10
  266. w25qxx.ID=ID_W25Q10;
  267. w25qxx.BlockCount=2;
  268. #if (_W25QXX_DEBUG==1)
  269. printf("w25qxx Chip: w25q10\r\n");
  270. #endif
  271. break;
  272. default:
  273. #if (_W25QXX_DEBUG==1)
  274. printf("w25qxx Unknown ID\r\n");
  275. #endif
  276. w25qxx.Lock=0;
  277. return false;
  278. }
  279. w25qxx.PageSize=256;
  280. w25qxx.SectorSize=0x1000;
  281. w25qxx.SectorCount=w25qxx.BlockCount*16;
  282. w25qxx.PageCount=(w25qxx.SectorCount*w25qxx.SectorSize)/w25qxx.PageSize;
  283. w25qxx.BlockSize=w25qxx.SectorSize*16;
  284. w25qxx.CapacityInKiloByte=(w25qxx.SectorCount*w25qxx.SectorSize)/1024;
  285. W25qxx_ReadUniqID();
  286. W25qxx_ReadStatusRegister(1);
  287. W25qxx_ReadStatusRegister(2);
  288. W25qxx_ReadStatusRegister(3);
  289. #if (_W25QXX_DEBUG==1)
  290. printf("w25qxx Page Size: %d Bytes\r\n",w25qxx.PageSize);
  291. printf("w25qxx Page Count: %d\r\n",w25qxx.PageCount);
  292. printf("w25qxx Sector Size: %d Bytes\r\n",w25qxx.SectorSize);
  293. printf("w25qxx Sector Count: %d\r\n",w25qxx.SectorCount);
  294. printf("w25qxx Block Size: %d Bytes\r\n",w25qxx.BlockSize);
  295. printf("w25qxx Block Count: %d\r\n",w25qxx.BlockCount);
  296. printf("w25qxx Capacity: %d KiloBytes\r\n",w25qxx.CapacityInKiloByte);
  297. printf("w25qxx Init Done\r\n");
  298. #endif
  299. w25qxx.Lock=0;
  300. return true;
  301. }
  302. //###################################################################################################################
  303. void W25qxx_EraseChip(void)
  304. {
  305. while(w25qxx.Lock==1)
  306. W25qxx_Delay(1);
  307. w25qxx.Lock=1;
  308. #if (_W25QXX_DEBUG==1)
  309. uint32_t StartTime=HAL_GetTick();
  310. printf("w25qxx EraseChip Begin...\r\n");
  311. #endif
  312. W25qxx_WriteEnable();
  313. W25QXX_CS_LOW();
  314. W25qxx_Spi(0xC7);
  315. W25QXX_CS_HIGH();
  316. W25qxx_WaitForWriteEnd();
  317. #if (_W25QXX_DEBUG==1)
  318. printf("w25qxx EraseBlock done after %d ms!\r\n",HAL_GetTick()-StartTime);
  319. #endif
  320. W25qxx_Delay(10);
  321. w25qxx.Lock=0;
  322. }
  323. //###################################################################################################################
  324. void W25qxx_EraseSector(uint32_t SectorAddr)
  325. {
  326. while(w25qxx.Lock==1)
  327. W25qxx_Delay(1);
  328. w25qxx.Lock=1;
  329. #if (_W25QXX_DEBUG==1)
  330. uint32_t StartTime=HAL_GetTick();
  331. printf("w25qxx EraseSector %d Begin...\r\n",SectorAddr);
  332. #endif
  333. W25qxx_WaitForWriteEnd();
  334. SectorAddr = SectorAddr * w25qxx.SectorSize;
  335. W25qxx_WriteEnable();
  336. W25QXX_CS_LOW();
  337. W25qxx_Spi(0x20);
  338. if(w25qxx.ID>=ID_W25Q256)
  339. W25qxx_Spi((SectorAddr & 0xFF000000) >> 24);
  340. W25qxx_Spi((SectorAddr & 0xFF0000) >> 16);
  341. W25qxx_Spi((SectorAddr & 0xFF00) >> 8);
  342. W25qxx_Spi(SectorAddr & 0xFF);
  343. W25QXX_CS_HIGH();
  344. W25qxx_WaitForWriteEnd();
  345. #if (_W25QXX_DEBUG==1)
  346. printf("w25qxx EraseSector done after %d ms\r\n",HAL_GetTick()-StartTime);
  347. #endif
  348. W25qxx_Delay(1);
  349. w25qxx.Lock=0;
  350. }
  351. //###################################################################################################################
  352. void W25qxx_EraseBlock(uint32_t BlockAddr)
  353. {
  354. while(w25qxx.Lock==1)
  355. W25qxx_Delay(1);
  356. w25qxx.Lock=1;
  357. #if (_W25QXX_DEBUG==1)
  358. printf("w25qxx EraseBlock %d Begin...\r\n",BlockAddr);
  359. W25qxx_Delay(100);
  360. uint32_t StartTime=HAL_GetTick();
  361. #endif
  362. W25qxx_WaitForWriteEnd();
  363. BlockAddr = BlockAddr * w25qxx.SectorSize*16;
  364. W25qxx_WriteEnable();
  365. W25QXX_CS_LOW();
  366. W25qxx_Spi(0xD8);
  367. if(w25qxx.ID>=ID_W25Q256)
  368. W25qxx_Spi((BlockAddr & 0xFF000000) >> 24);
  369. W25qxx_Spi((BlockAddr & 0xFF0000) >> 16);
  370. W25qxx_Spi((BlockAddr & 0xFF00) >> 8);
  371. W25qxx_Spi(BlockAddr & 0xFF);
  372. W25QXX_CS_HIGH();
  373. W25qxx_WaitForWriteEnd();
  374. #if (_W25QXX_DEBUG==1)
  375. printf("w25qxx EraseBlock done after %d ms\r\n",HAL_GetTick()-StartTime);
  376. W25qxx_Delay(100);
  377. #endif
  378. W25qxx_Delay(1);
  379. w25qxx.Lock=0;
  380. }
  381. //###################################################################################################################
  382. uint32_t W25qxx_PageToSector(uint32_t PageAddress)
  383. {
  384. return ((PageAddress*w25qxx.PageSize)/w25qxx.SectorSize);
  385. }
  386. //###################################################################################################################
  387. uint32_t W25qxx_PageToBlock(uint32_t PageAddress)
  388. {
  389. return ((PageAddress*w25qxx.PageSize)/w25qxx.BlockSize);
  390. }
  391. //###################################################################################################################
  392. uint32_t W25qxx_SectorToBlock(uint32_t SectorAddress)
  393. {
  394. return ((SectorAddress*w25qxx.SectorSize)/w25qxx.BlockSize);
  395. }
  396. //###################################################################################################################
  397. uint32_t W25qxx_SectorToPage(uint32_t SectorAddress)
  398. {
  399. return (SectorAddress*w25qxx.SectorSize)/w25qxx.PageSize;
  400. }
  401. //###################################################################################################################
  402. uint32_t W25qxx_BlockToPage(uint32_t BlockAddress)
  403. {
  404. return (BlockAddress*w25qxx.BlockSize)/w25qxx.PageSize;
  405. }
  406. //###################################################################################################################
  407. bool W25qxx_IsEmptyPage(uint32_t Page_Address,uint32_t OffsetInByte,uint32_t NumByteToCheck_up_to_PageSize)
  408. {
  409. while(w25qxx.Lock==1)
  410. W25qxx_Delay(1);
  411. w25qxx.Lock=1;
  412. if(((NumByteToCheck_up_to_PageSize+OffsetInByte)>w25qxx.PageSize)||(NumByteToCheck_up_to_PageSize==0))
  413. NumByteToCheck_up_to_PageSize=w25qxx.PageSize-OffsetInByte;
  414. #if (_W25QXX_DEBUG==1)
  415. printf("w25qxx CheckPage:%d, Offset:%d, Bytes:%d begin...\r\n",Page_Address,OffsetInByte,NumByteToCheck_up_to_PageSize);
  416. W25qxx_Delay(100);
  417. uint32_t StartTime=HAL_GetTick();
  418. #endif
  419. uint8_t pBuffer[32];
  420. uint32_t WorkAddress;
  421. uint32_t i;
  422. for(i=OffsetInByte; i<w25qxx.PageSize; i+=sizeof(pBuffer))
  423. {
  424. W25QXX_CS_LOW();
  425. WorkAddress=(i+Page_Address*w25qxx.PageSize);
  426. W25qxx_Spi(0x0B);
  427. if(w25qxx.ID>=ID_W25Q256)
  428. W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
  429. W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
  430. W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
  431. W25qxx_Spi(WorkAddress & 0xFF);
  432. W25qxx_Spi(0);
  433. HAL_SPI_Receive(&_W25QXX_SPI,pBuffer,sizeof(pBuffer),100);
  434. W25QXX_CS_HIGH();
  435. for(uint8_t x=0;x<sizeof(pBuffer);x++)
  436. {
  437. if(pBuffer[x]!=0xFF)
  438. goto NOT_EMPTY;
  439. }
  440. }
  441. if((w25qxx.PageSize+OffsetInByte)%sizeof(pBuffer)!=0)
  442. {
  443. i-=sizeof(pBuffer);
  444. for( ; i<w25qxx.PageSize; i++)
  445. {
  446. W25QXX_CS_LOW();
  447. WorkAddress=(i+Page_Address*w25qxx.PageSize);
  448. W25qxx_Spi(0x0B);
  449. if(w25qxx.ID>=ID_W25Q256)
  450. W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
  451. W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
  452. W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
  453. W25qxx_Spi(WorkAddress & 0xFF);
  454. W25qxx_Spi(0);
  455. HAL_SPI_Receive(&_W25QXX_SPI,pBuffer,1,100);
  456. W25QXX_CS_HIGH();
  457. if(pBuffer[0]!=0xFF)
  458. goto NOT_EMPTY;
  459. }
  460. }
  461. #if (_W25QXX_DEBUG==1)
  462. printf("w25qxx CheckPage is Empty in %d ms\r\n",HAL_GetTick()-StartTime);
  463. W25qxx_Delay(100);
  464. #endif
  465. w25qxx.Lock=0;
  466. return true;
  467. NOT_EMPTY:
  468. #if (_W25QXX_DEBUG==1)
  469. printf("w25qxx CheckPage is Not Empty in %d ms\r\n",HAL_GetTick()-StartTime);
  470. W25qxx_Delay(100);
  471. #endif
  472. w25qxx.Lock=0;
  473. return false;
  474. }
  475. //###################################################################################################################
  476. bool W25qxx_IsEmptySector(uint32_t Sector_Address,uint32_t OffsetInByte,uint32_t NumByteToCheck_up_to_SectorSize)
  477. {
  478. while(w25qxx.Lock==1)
  479. W25qxx_Delay(1);
  480. w25qxx.Lock=1;
  481. if((NumByteToCheck_up_to_SectorSize>w25qxx.SectorSize)||(NumByteToCheck_up_to_SectorSize==0))
  482. NumByteToCheck_up_to_SectorSize=w25qxx.SectorSize;
  483. #if (_W25QXX_DEBUG==1)
  484. printf("w25qxx CheckSector:%d, Offset:%d, Bytes:%d begin...\r\n",Sector_Address,OffsetInByte,NumByteToCheck_up_to_SectorSize);
  485. W25qxx_Delay(100);
  486. uint32_t StartTime=HAL_GetTick();
  487. #endif
  488. uint8_t pBuffer[32];
  489. uint32_t WorkAddress;
  490. uint32_t i;
  491. for(i=OffsetInByte; i<w25qxx.SectorSize; i+=sizeof(pBuffer))
  492. {
  493. W25QXX_CS_LOW();
  494. WorkAddress=(i+Sector_Address*w25qxx.SectorSize);
  495. W25qxx_Spi(0x0B);
  496. if(w25qxx.ID>=ID_W25Q256)
  497. W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
  498. W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
  499. W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
  500. W25qxx_Spi(WorkAddress & 0xFF);
  501. W25qxx_Spi(0);
  502. HAL_SPI_Receive(&_W25QXX_SPI,pBuffer,sizeof(pBuffer),100);
  503. W25QXX_CS_HIGH();
  504. for(uint8_t x=0;x<sizeof(pBuffer);x++)
  505. {
  506. if(pBuffer[x]!=0xFF)
  507. goto NOT_EMPTY;
  508. }
  509. }
  510. if((w25qxx.SectorSize+OffsetInByte)%sizeof(pBuffer)!=0)
  511. {
  512. i-=sizeof(pBuffer);
  513. for( ; i<w25qxx.SectorSize; i++)
  514. {
  515. W25QXX_CS_LOW();
  516. WorkAddress=(i+Sector_Address*w25qxx.SectorSize);
  517. W25qxx_Spi(0x0B);
  518. if(w25qxx.ID>=ID_W25Q256)
  519. W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
  520. W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
  521. W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
  522. W25qxx_Spi(WorkAddress & 0xFF);
  523. W25qxx_Spi(0);
  524. HAL_SPI_Receive(&_W25QXX_SPI,pBuffer,1,100);
  525. W25QXX_CS_HIGH();
  526. if(pBuffer[0]!=0xFF)
  527. goto NOT_EMPTY;
  528. }
  529. }
  530. #if (_W25QXX_DEBUG==1)
  531. printf("w25qxx CheckSector is Empty in %d ms\r\n",HAL_GetTick()-StartTime);
  532. W25qxx_Delay(100);
  533. #endif
  534. w25qxx.Lock=0;
  535. return true;
  536. NOT_EMPTY:
  537. #if (_W25QXX_DEBUG==1)
  538. printf("w25qxx CheckSector is Not Empty in %d ms\r\n",HAL_GetTick()-StartTime);
  539. W25qxx_Delay(100);
  540. #endif
  541. w25qxx.Lock=0;
  542. return false;
  543. }
  544. //###################################################################################################################
  545. bool W25qxx_IsEmptyBlock(uint32_t Block_Address,uint32_t OffsetInByte,uint32_t NumByteToCheck_up_to_BlockSize)
  546. {
  547. while(w25qxx.Lock==1)
  548. W25qxx_Delay(1);
  549. w25qxx.Lock=1;
  550. if((NumByteToCheck_up_to_BlockSize>w25qxx.BlockSize)||(NumByteToCheck_up_to_BlockSize==0))
  551. NumByteToCheck_up_to_BlockSize=w25qxx.BlockSize;
  552. #if (_W25QXX_DEBUG==1)
  553. printf("w25qxx CheckBlock:%d, Offset:%d, Bytes:%d begin...\r\n",Block_Address,OffsetInByte,NumByteToCheck_up_to_BlockSize);
  554. W25qxx_Delay(100);
  555. uint32_t StartTime=HAL_GetTick();
  556. #endif
  557. uint8_t pBuffer[32];
  558. uint32_t WorkAddress;
  559. uint32_t i;
  560. for(i=OffsetInByte; i<w25qxx.BlockSize; i+=sizeof(pBuffer))
  561. {
  562. W25QXX_CS_LOW();
  563. WorkAddress=(i+Block_Address*w25qxx.BlockSize);
  564. W25qxx_Spi(0x0B);
  565. if(w25qxx.ID>=ID_W25Q256)
  566. W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
  567. W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
  568. W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
  569. W25qxx_Spi(WorkAddress & 0xFF);
  570. W25qxx_Spi(0);
  571. HAL_SPI_Receive(&_W25QXX_SPI,pBuffer,sizeof(pBuffer),100);
  572. W25QXX_CS_HIGH();
  573. for(uint8_t x=0;x<sizeof(pBuffer);x++)
  574. {
  575. if(pBuffer[x]!=0xFF)
  576. goto NOT_EMPTY;
  577. }
  578. }
  579. if((w25qxx.BlockSize+OffsetInByte)%sizeof(pBuffer)!=0)
  580. {
  581. i-=sizeof(pBuffer);
  582. for( ; i<w25qxx.BlockSize; i++)
  583. {
  584. W25QXX_CS_LOW();
  585. WorkAddress=(i+Block_Address*w25qxx.BlockSize);
  586. W25qxx_Spi(0x0B);
  587. if(w25qxx.ID>=ID_W25Q256)
  588. W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
  589. W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
  590. W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
  591. W25qxx_Spi(WorkAddress & 0xFF);
  592. W25qxx_Spi(0);
  593. HAL_SPI_Receive(&_W25QXX_SPI,pBuffer,1,100);
  594. W25QXX_CS_HIGH();
  595. if(pBuffer[0]!=0xFF)
  596. goto NOT_EMPTY;
  597. }
  598. }
  599. #if (_W25QXX_DEBUG==1)
  600. printf("w25qxx CheckBlock is Empty in %d ms\r\n",HAL_GetTick()-StartTime);
  601. W25qxx_Delay(100);
  602. #endif
  603. w25qxx.Lock=0;
  604. return true;
  605. NOT_EMPTY:
  606. #if (_W25QXX_DEBUG==1)
  607. printf("w25qxx CheckBlock is Not Empty in %d ms\r\n",HAL_GetTick()-StartTime);
  608. W25qxx_Delay(100);
  609. #endif
  610. w25qxx.Lock=0;
  611. return false;
  612. }
  613. //###################################################################################################################
  614. void W25qxx_WriteByte(uint8_t pBuffer, uint32_t WriteAddr_inBytes)
  615. {
  616. while(w25qxx.Lock==1)
  617. W25qxx_Delay(1);
  618. w25qxx.Lock=1;
  619. #if (_W25QXX_DEBUG==1)
  620. uint32_t StartTime=HAL_GetTick();
  621. printf("w25qxx WriteByte 0x%02X at address %d begin...",pBuffer,WriteAddr_inBytes);
  622. #endif
  623. W25qxx_WaitForWriteEnd();
  624. W25qxx_WriteEnable();
  625. W25QXX_CS_LOW();
  626. W25qxx_Spi(0x02);
  627. if(w25qxx.ID>=ID_W25Q256)
  628. W25qxx_Spi((WriteAddr_inBytes & 0xFF000000) >> 24);
  629. W25qxx_Spi((WriteAddr_inBytes & 0xFF0000) >> 16);
  630. W25qxx_Spi((WriteAddr_inBytes & 0xFF00) >> 8);
  631. W25qxx_Spi(WriteAddr_inBytes & 0xFF);
  632. W25qxx_Spi(pBuffer);
  633. W25QXX_CS_HIGH();
  634. W25qxx_WaitForWriteEnd();
  635. #if (_W25QXX_DEBUG==1)
  636. printf("w25qxx WriteByte done after %d ms\r\n",HAL_GetTick()-StartTime);
  637. #endif
  638. w25qxx.Lock=0;
  639. }
  640. //###################################################################################################################
  641. void W25qxx_WritePage(uint8_t *pBuffer ,uint32_t Page_Address,uint32_t OffsetInByte,uint32_t NumByteToWrite_up_to_PageSize)
  642. {
  643. while(w25qxx.Lock==1)
  644. W25qxx_Delay(1);
  645. w25qxx.Lock=1;
  646. if(((NumByteToWrite_up_to_PageSize+OffsetInByte)>w25qxx.PageSize)||(NumByteToWrite_up_to_PageSize==0))
  647. NumByteToWrite_up_to_PageSize=w25qxx.PageSize-OffsetInByte;
  648. if((OffsetInByte+NumByteToWrite_up_to_PageSize) > w25qxx.PageSize)
  649. NumByteToWrite_up_to_PageSize = w25qxx.PageSize-OffsetInByte;
  650. #if (_W25QXX_DEBUG==1)
  651. printf("w25qxx WritePage:%d, Offset:%d ,Writes %d Bytes, begin...\r\n",Page_Address,OffsetInByte,NumByteToWrite_up_to_PageSize);
  652. W25qxx_Delay(100);
  653. uint32_t StartTime=HAL_GetTick();
  654. #endif
  655. W25qxx_WaitForWriteEnd();
  656. W25qxx_WriteEnable();
  657. W25QXX_CS_LOW();
  658. W25qxx_Spi(0x02);
  659. Page_Address = (Page_Address*w25qxx.PageSize)+OffsetInByte;
  660. if(w25qxx.ID>=ID_W25Q256)
  661. W25qxx_Spi((Page_Address & 0xFF000000) >> 24);
  662. W25qxx_Spi((Page_Address & 0xFF0000) >> 16);
  663. W25qxx_Spi((Page_Address & 0xFF00) >> 8);
  664. W25qxx_Spi(Page_Address&0xFF);
  665. HAL_SPI_Transmit(&_W25QXX_SPI,pBuffer,NumByteToWrite_up_to_PageSize,100);
  666. W25QXX_CS_HIGH();
  667. W25qxx_WaitForWriteEnd();
  668. #if (_W25QXX_DEBUG==1)
  669. StartTime = HAL_GetTick()-StartTime;
  670. for(uint32_t i=0;i<NumByteToWrite_up_to_PageSize ; i++)
  671. {
  672. if((i%8==0)&&(i>2))
  673. {
  674. printf("\r\n");
  675. W25qxx_Delay(10);
  676. }
  677. printf("0x%02X,",pBuffer[i]);
  678. }
  679. printf("\r\n");
  680. printf("w25qxx WritePage done after %d ms\r\n",StartTime);
  681. W25qxx_Delay(100);
  682. #endif
  683. W25qxx_Delay(1);
  684. w25qxx.Lock=0;
  685. }
  686. //###################################################################################################################
  687. void W25qxx_WriteSector(uint8_t *pBuffer ,uint32_t Sector_Address,uint32_t OffsetInByte ,uint32_t NumByteToWrite_up_to_SectorSize)
  688. {
  689. if((NumByteToWrite_up_to_SectorSize>w25qxx.SectorSize)||(NumByteToWrite_up_to_SectorSize==0))
  690. NumByteToWrite_up_to_SectorSize=w25qxx.SectorSize;
  691. #if (_W25QXX_DEBUG==1)
  692. printf("+++w25qxx WriteSector:%d, Offset:%d ,Write %d Bytes, begin...\r\n",Sector_Address,OffsetInByte,NumByteToWrite_up_to_SectorSize);
  693. W25qxx_Delay(100);
  694. #endif
  695. if(OffsetInByte>=w25qxx.SectorSize)
  696. {
  697. #if (_W25QXX_DEBUG==1)
  698. printf("---w25qxx WriteSector Faild!\r\n");
  699. W25qxx_Delay(100);
  700. #endif
  701. return;
  702. }
  703. uint32_t StartPage;
  704. int32_t BytesToWrite;
  705. uint32_t LocalOffset;
  706. if((OffsetInByte+NumByteToWrite_up_to_SectorSize) > w25qxx.SectorSize)
  707. BytesToWrite = w25qxx.SectorSize-OffsetInByte;
  708. else
  709. BytesToWrite = NumByteToWrite_up_to_SectorSize;
  710. StartPage = W25qxx_SectorToPage(Sector_Address)+(OffsetInByte/w25qxx.PageSize);
  711. LocalOffset = OffsetInByte%w25qxx.PageSize;
  712. do
  713. {
  714. W25qxx_WritePage(pBuffer,StartPage,LocalOffset,BytesToWrite);
  715. StartPage++;
  716. BytesToWrite-=w25qxx.PageSize-LocalOffset;
  717. pBuffer+=w25qxx.PageSize;
  718. LocalOffset=0;
  719. }while(BytesToWrite>0);
  720. #if (_W25QXX_DEBUG==1)
  721. printf("---w25qxx WriteSector Done\r\n");
  722. W25qxx_Delay(100);
  723. #endif
  724. }
  725. //###################################################################################################################
  726. void W25qxx_WriteBlock (uint8_t* pBuffer ,uint32_t Block_Address ,uint32_t OffsetInByte ,uint32_t NumByteToWrite_up_to_BlockSize)
  727. {
  728. if((NumByteToWrite_up_to_BlockSize>w25qxx.BlockSize)||(NumByteToWrite_up_to_BlockSize==0))
  729. NumByteToWrite_up_to_BlockSize=w25qxx.BlockSize;
  730. #if (_W25QXX_DEBUG==1)
  731. printf("+++w25qxx WriteBlock:%d, Offset:%d ,Write %d Bytes, begin...\r\n",Block_Address,OffsetInByte,NumByteToWrite_up_to_BlockSize);
  732. W25qxx_Delay(100);
  733. #endif
  734. if(OffsetInByte>=w25qxx.BlockSize)
  735. {
  736. #if (_W25QXX_DEBUG==1)
  737. printf("---w25qxx WriteBlock Faild!\r\n");
  738. W25qxx_Delay(100);
  739. #endif
  740. return;
  741. }
  742. uint32_t StartPage;
  743. int32_t BytesToWrite;
  744. uint32_t LocalOffset;
  745. if((OffsetInByte+NumByteToWrite_up_to_BlockSize) > w25qxx.BlockSize)
  746. BytesToWrite = w25qxx.BlockSize-OffsetInByte;
  747. else
  748. BytesToWrite = NumByteToWrite_up_to_BlockSize;
  749. StartPage = W25qxx_BlockToPage(Block_Address)+(OffsetInByte/w25qxx.PageSize);
  750. LocalOffset = OffsetInByte%w25qxx.PageSize;
  751. do
  752. {
  753. W25qxx_WritePage(pBuffer,StartPage,LocalOffset,BytesToWrite);
  754. StartPage++;
  755. BytesToWrite-=w25qxx.PageSize-LocalOffset;
  756. pBuffer+=w25qxx.PageSize;
  757. LocalOffset=0;
  758. }while(BytesToWrite>0);
  759. #if (_W25QXX_DEBUG==1)
  760. printf("---w25qxx WriteBlock Done\r\n");
  761. W25qxx_Delay(100);
  762. #endif
  763. }
  764. //###################################################################################################################
  765. void W25qxx_ReadByte(uint8_t *pBuffer,uint32_t Bytes_Address)
  766. {
  767. while(w25qxx.Lock==1)
  768. W25qxx_Delay(1);
  769. w25qxx.Lock=1;
  770. #if (_W25QXX_DEBUG==1)
  771. uint32_t StartTime=HAL_GetTick();
  772. printf("w25qxx ReadByte at address %d begin...\r\n",Bytes_Address);
  773. #endif
  774. W25QXX_CS_LOW();
  775. W25qxx_Spi(0x0B);
  776. if(w25qxx.ID>=ID_W25Q256)
  777. W25qxx_Spi((Bytes_Address & 0xFF000000) >> 24);
  778. W25qxx_Spi((Bytes_Address & 0xFF0000) >> 16);
  779. W25qxx_Spi((Bytes_Address& 0xFF00) >> 8);
  780. W25qxx_Spi(Bytes_Address & 0xFF);
  781. W25qxx_Spi(0);
  782. *pBuffer = W25qxx_Spi(W25QXX_DUMMY_BYTE);
  783. W25QXX_CS_HIGH();
  784. #if (_W25QXX_DEBUG==1)
  785. printf("w25qxx ReadByte 0x%02X done after %d ms\r\n",*pBuffer,HAL_GetTick()-StartTime);
  786. #endif
  787. w25qxx.Lock=0;
  788. }
  789. //###################################################################################################################
  790. void W25qxx_ReadBytes(uint8_t* pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead)
  791. {
  792. while(w25qxx.Lock==1)
  793. W25qxx_Delay(1);
  794. w25qxx.Lock=1;
  795. #if (_W25QXX_DEBUG==1)
  796. uint32_t StartTime=HAL_GetTick();
  797. printf("w25qxx ReadBytes at Address:%d, %d Bytes begin...\r\n",ReadAddr,NumByteToRead);
  798. #endif
  799. W25QXX_CS_LOW();
  800. W25qxx_Spi(0x0B);
  801. if(w25qxx.ID>=ID_W25Q256)
  802. W25qxx_Spi((ReadAddr & 0xFF000000) >> 24);
  803. W25qxx_Spi((ReadAddr & 0xFF0000) >> 16);
  804. W25qxx_Spi((ReadAddr& 0xFF00) >> 8);
  805. W25qxx_Spi(ReadAddr & 0xFF);
  806. W25qxx_Spi(0);
  807. HAL_SPI_Receive(&_W25QXX_SPI,pBuffer,NumByteToRead,2000);
  808. W25QXX_CS_HIGH();
  809. #if (_W25QXX_DEBUG==1)
  810. StartTime = HAL_GetTick()-StartTime;
  811. for(uint32_t i=0;i<NumByteToRead ; i++)
  812. {
  813. if((i%8==0)&&(i>2))
  814. {
  815. printf("\r\n");
  816. W25qxx_Delay(10);
  817. }
  818. printf("0x%02X,",pBuffer[i]);
  819. }
  820. printf("\r\n");
  821. printf("w25qxx ReadBytes done after %d ms\r\n",StartTime);
  822. W25qxx_Delay(100);
  823. #endif
  824. W25qxx_Delay(1);
  825. w25qxx.Lock=0;
  826. }
  827. //###################################################################################################################
  828. void W25qxx_ReadPage(uint8_t *pBuffer,uint32_t Page_Address,uint32_t OffsetInByte,uint32_t NumByteToRead_up_to_PageSize)
  829. {
  830. while(w25qxx.Lock==1)
  831. W25qxx_Delay(1);
  832. w25qxx.Lock=1;
  833. if((NumByteToRead_up_to_PageSize>w25qxx.PageSize)||(NumByteToRead_up_to_PageSize==0))
  834. NumByteToRead_up_to_PageSize=w25qxx.PageSize;
  835. if((OffsetInByte+NumByteToRead_up_to_PageSize) > w25qxx.PageSize)
  836. NumByteToRead_up_to_PageSize = w25qxx.PageSize-OffsetInByte;
  837. #if (_W25QXX_DEBUG==1)
  838. printf("w25qxx ReadPage:%d, Offset:%d ,Read %d Bytes, begin...\r\n",Page_Address,OffsetInByte,NumByteToRead_up_to_PageSize);
  839. W25qxx_Delay(100);
  840. uint32_t StartTime=HAL_GetTick();
  841. #endif
  842. Page_Address = Page_Address*w25qxx.PageSize+OffsetInByte;
  843. W25QXX_CS_LOW();
  844. W25qxx_Spi(0x0B);
  845. if(w25qxx.ID>=ID_W25Q256)
  846. W25qxx_Spi((Page_Address & 0xFF000000) >> 24);
  847. W25qxx_Spi((Page_Address & 0xFF0000) >> 16);
  848. W25qxx_Spi((Page_Address& 0xFF00) >> 8);
  849. W25qxx_Spi(Page_Address & 0xFF);
  850. W25qxx_Spi(0);
  851. HAL_SPI_Receive(&_W25QXX_SPI,pBuffer,NumByteToRead_up_to_PageSize,100);
  852. W25QXX_CS_HIGH();
  853. #if (_W25QXX_DEBUG==1)
  854. StartTime = HAL_GetTick()-StartTime;
  855. for(uint32_t i=0;i<NumByteToRead_up_to_PageSize ; i++)
  856. {
  857. if((i%8==0)&&(i>2))
  858. {
  859. printf("\r\n");
  860. W25qxx_Delay(10);
  861. }
  862. printf("0x%02X,",pBuffer[i]);
  863. }
  864. printf("\r\n");
  865. printf("w25qxx ReadPage done after %d ms\r\n",StartTime);
  866. W25qxx_Delay(100);
  867. #endif
  868. W25qxx_Delay(1);
  869. w25qxx.Lock=0;
  870. }
  871. //###################################################################################################################
  872. void W25qxx_ReadSector(uint8_t *pBuffer,uint32_t Sector_Address,uint32_t OffsetInByte,uint32_t NumByteToRead_up_to_SectorSize)
  873. {
  874. if((NumByteToRead_up_to_SectorSize>w25qxx.SectorSize)||(NumByteToRead_up_to_SectorSize==0))
  875. NumByteToRead_up_to_SectorSize=w25qxx.SectorSize;
  876. #if (_W25QXX_DEBUG==1)
  877. printf("+++w25qxx ReadSector:%d, Offset:%d ,Read %d Bytes, begin...\r\n",Sector_Address,OffsetInByte,NumByteToRead_up_to_SectorSize);
  878. W25qxx_Delay(100);
  879. #endif
  880. if(OffsetInByte>=w25qxx.SectorSize)
  881. {
  882. #if (_W25QXX_DEBUG==1)
  883. printf("---w25qxx ReadSector Faild!\r\n");
  884. W25qxx_Delay(100);
  885. #endif
  886. return;
  887. }
  888. uint32_t StartPage;
  889. int32_t BytesToRead;
  890. uint32_t LocalOffset;
  891. if((OffsetInByte+NumByteToRead_up_to_SectorSize) > w25qxx.SectorSize)
  892. BytesToRead = w25qxx.SectorSize-OffsetInByte;
  893. else
  894. BytesToRead = NumByteToRead_up_to_SectorSize;
  895. StartPage = W25qxx_SectorToPage(Sector_Address)+(OffsetInByte/w25qxx.PageSize);
  896. LocalOffset = OffsetInByte%w25qxx.PageSize;
  897. do
  898. {
  899. W25qxx_ReadPage(pBuffer,StartPage,LocalOffset,BytesToRead);
  900. StartPage++;
  901. BytesToRead-=w25qxx.PageSize-LocalOffset;
  902. pBuffer+=w25qxx.PageSize;
  903. LocalOffset=0;
  904. }while(BytesToRead>0);
  905. #if (_W25QXX_DEBUG==1)
  906. printf("---w25qxx ReadSector Done\r\n");
  907. W25qxx_Delay(100);
  908. #endif
  909. }
  910. //###################################################################################################################
  911. void W25qxx_ReadBlock(uint8_t* pBuffer,uint32_t Block_Address,uint32_t OffsetInByte,uint32_t NumByteToRead_up_to_BlockSize)
  912. {
  913. if((NumByteToRead_up_to_BlockSize>w25qxx.BlockSize)||(NumByteToRead_up_to_BlockSize==0))
  914. NumByteToRead_up_to_BlockSize=w25qxx.BlockSize;
  915. #if (_W25QXX_DEBUG==1)
  916. printf("+++w25qxx ReadBlock:%d, Offset:%d ,Read %d Bytes, begin...\r\n",Block_Address,OffsetInByte,NumByteToRead_up_to_BlockSize);
  917. W25qxx_Delay(100);
  918. #endif
  919. if(OffsetInByte>=w25qxx.BlockSize)
  920. {
  921. #if (_W25QXX_DEBUG==1)
  922. printf("w25qxx ReadBlock Faild!\r\n");
  923. W25qxx_Delay(100);
  924. #endif
  925. return;
  926. }
  927. uint32_t StartPage;
  928. int32_t BytesToRead;
  929. uint32_t LocalOffset;
  930. if((OffsetInByte+NumByteToRead_up_to_BlockSize) > w25qxx.BlockSize)
  931. BytesToRead = w25qxx.BlockSize-OffsetInByte;
  932. else
  933. BytesToRead = NumByteToRead_up_to_BlockSize;
  934. StartPage = W25qxx_BlockToPage(Block_Address)+(OffsetInByte/w25qxx.PageSize);
  935. LocalOffset = OffsetInByte%w25qxx.PageSize;
  936. do
  937. {
  938. W25qxx_ReadPage(pBuffer,StartPage,LocalOffset,BytesToRead);
  939. StartPage++;
  940. BytesToRead-=w25qxx.PageSize-LocalOffset;
  941. pBuffer+=w25qxx.PageSize;
  942. LocalOffset=0;
  943. }while(BytesToRead>0);
  944. #if (_W25QXX_DEBUG==1)
  945. printf("---w25qxx ReadBlock Done\r\n");
  946. W25qxx_Delay(100);
  947. #endif
  948. }
  949. #endif //-----------------------------------USE_W25QXX_SPI------------------------------------------//
  950. //###################################################################################################################