spiflash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. #include "spi.h"
  29. #include "error.h"
  30. #include "spiflash.h"
  31. #include "hal_mcu.h"
  32. #include "log.h"
  33. /*
  34. hal_spi_t spi ={
  35. .spi_index = SPI0,
  36. };
  37. */
  38. extern hal_spi_t spi;
  39. #define spiflash_cmd_tx_and_rx(tx_buf,rx_buf,len) hal_spi_transmit(&spi,tx_buf,rx_buf,len)
  40. //gd25q16 driver
  41. uint32_t spiflash_read_identification(void)//check
  42. {
  43. uint8_t buf_send[4] = {FLASH_RDID,0x00,0x00,0x00};
  44. uint8_t buf_rece[4] = {0x00,0x00,0x00,0x00};
  45. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,4))
  46. {
  47. return (buf_rece[1] << 16)|(buf_rece[2] << 8) | (buf_rece[3]);
  48. }
  49. else
  50. {
  51. return 0xFFFFFF;
  52. }
  53. }
  54. uint16_t spiflash_read_status_register(uint8_t bitsSel)//0~low other~high
  55. {
  56. uint8_t buf_send[4] = {0x00,0x00,0x00,0x00};
  57. uint8_t buf_rece[4] = {0x00,0x00,0x00,0x00};
  58. if(bitsSel == 0)
  59. {
  60. buf_send[0] = FLASH_RDSR_LOW;
  61. }
  62. else
  63. {
  64. buf_send[0] = FLASH_RDSR_HIGH;
  65. }
  66. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,3))
  67. {
  68. return (buf_rece[1] << 8) | (buf_rece[2]);
  69. }
  70. else
  71. {
  72. return 0xFFFF;
  73. }
  74. }
  75. bool spiflash_bus_busy(void)
  76. {
  77. return (spiflash_read_status_register(0) & 0x01);
  78. }
  79. void spiflash_program_erase_suspend(void)
  80. {
  81. uint8_t buf_send[1] = {FLASH_PES};
  82. uint8_t buf_rece[1] = {0x00};
  83. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,1))
  84. {
  85. ;
  86. }
  87. }
  88. void spiflash_program_erase_resume(void)
  89. {
  90. uint8_t buf_send[1] = {FLASH_PER};
  91. uint8_t buf_rece[1] = {0x00};
  92. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,1))
  93. {
  94. ;
  95. }
  96. }
  97. void spiflash_deep_powerdown(void)
  98. {
  99. uint8_t buf_send[1] = {FLASH_DP};
  100. uint8_t buf_rece[1] = {0x00};
  101. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,1))
  102. {
  103. ;
  104. }
  105. }
  106. void spiflash_release_from_powerdown(void)
  107. {
  108. uint8_t buf_send[1] = {FLASH_RDI};
  109. uint8_t buf_rece[1] = {0x00};
  110. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,1))
  111. {
  112. ;
  113. }
  114. }
  115. void spiflash_write_enable(void)
  116. {
  117. uint8_t buf_send[1] = {FLASH_WREN};
  118. uint8_t buf_rece[1] = {0x00};
  119. while(spiflash_bus_busy());
  120. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,1))
  121. {
  122. ;
  123. }
  124. }
  125. void spiflash_write_disable(void)
  126. {
  127. uint8_t buf_send[1] = {FLASH_WRDIS};
  128. uint8_t buf_rece[1] = {0x00};
  129. //while(spiflash_bus_busy());
  130. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,1))
  131. {
  132. ;
  133. }
  134. }
  135. void spiflash_chip_erase(void)
  136. {
  137. uint8_t buf_send[1] = {FLASH_CE};
  138. uint8_t buf_rece[1] = {0x00};
  139. buf_send[0] = FLASH_CE;
  140. spiflash_write_enable();
  141. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,1))
  142. {
  143. ;
  144. }
  145. }
  146. void spiflash_sector_erase(uint32_t addr)
  147. {
  148. uint8_t buf_send[4] = {FLASH_SE,0x00,0x00,0x00};
  149. uint8_t buf_rece[4] = {0x00,0x00,0x00,0x00};
  150. buf_send[1] = (addr>>16) & 0xff;
  151. buf_send[2] = (addr>>8) & 0xff;
  152. buf_send[3] = addr & 0xff;
  153. spiflash_write_enable();
  154. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,4))
  155. {
  156. ;
  157. }
  158. }
  159. void spiflash_block_erase_32KB(uint32_t addr)
  160. {
  161. uint8_t buf_send[4] = {FLASH_BE_32KB,0x00,0x00,0x00};
  162. uint8_t buf_rece[4] = {0x00,0x00,0x00,0x00};
  163. buf_send[1] = (addr>>16) & 0xff;
  164. buf_send[2] = (addr>>8) & 0xff;
  165. buf_send[3] = addr & 0xff;
  166. spiflash_write_enable();
  167. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,4))
  168. {
  169. ;
  170. }
  171. }
  172. void spiflash_block_erase_64KB(uint32_t addr)
  173. {
  174. uint8_t buf_send[4] = {FLASH_BE_64KB,0x00,0x00,0x00};
  175. uint8_t buf_rece[4] = {0x00,0x00,0x00,0x00};
  176. buf_send[1] = (addr>>16) & 0xff;
  177. buf_send[2] = (addr>>8) & 0xff;
  178. buf_send[3] = addr & 0xff;
  179. spiflash_write_enable();
  180. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,4))
  181. {
  182. ;
  183. }
  184. }
  185. void spiflash_write_status_register(uint8_t data)
  186. {
  187. uint8_t buf_send[2] = {FLASH_WRSR,0x00};
  188. uint8_t buf_rece[2] = {0x00,0x00};
  189. buf_send[1] = data;
  190. while(spiflash_bus_busy());
  191. spiflash_write_enable();
  192. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,2))
  193. {
  194. ;
  195. }
  196. }
  197. static void spiflash_write_unit(uint32_t addr,uint8_t* tx_buf,uint8_t tx_len)//tx_len in [1,4]
  198. {
  199. uint8_t buf_send[8] = {FLASH_PP,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  200. uint8_t buf_rece[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  201. buf_send[1] = (addr>>16)&0xff;
  202. buf_send[2] = (addr>>8)&0xff;
  203. buf_send[3] = addr & 0xff;
  204. switch(tx_len)
  205. {
  206. case 1:
  207. buf_send[4] = *tx_buf;
  208. break;
  209. case 2:
  210. buf_send[4] = *tx_buf;
  211. buf_send[5] = *(tx_buf+1);
  212. break;
  213. case 3:
  214. buf_send[4] = *tx_buf;
  215. buf_send[5] = *(tx_buf+1);
  216. buf_send[6] = *(tx_buf+2);
  217. break;
  218. case 4:
  219. buf_send[4] = *tx_buf;
  220. buf_send[5] = *(tx_buf+1);
  221. buf_send[6] = *(tx_buf+2);
  222. buf_send[7] = *(tx_buf+3);
  223. break;
  224. default:
  225. break;
  226. }
  227. spiflash_write_enable();
  228. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,(tx_len + 4)))
  229. {
  230. ;
  231. }
  232. }
  233. void spiflash_write(uint32_t addr,uint8_t* tx_buf,uint16_t tx_len)
  234. {
  235. //polling mode=polling
  236. //force_cs=0
  237. uint16_t offset = 0,ret16;
  238. ret16 = spiflash_read_status_register(0);
  239. if(ret16 != 0)
  240. {
  241. spiflash_write_status_register(0x00);
  242. while(spiflash_bus_busy() == TRUE);
  243. }
  244. while(tx_len > 0)
  245. {
  246. if(tx_len >= 4)
  247. {
  248. spiflash_write_unit((addr + offset),(tx_buf + offset),4);
  249. offset += 4;
  250. tx_len -= 4;
  251. }
  252. else
  253. {
  254. spiflash_write_unit((addr + offset),(tx_buf + offset),tx_len);
  255. tx_len = 0;
  256. }
  257. }
  258. //you can process the protect with your requirenment
  259. // if(ret16 != 0)
  260. // {
  261. // spiflash_write_status_register(ret16);
  262. // }
  263. }
  264. static void spiflash_read_unit(uint32_t addr,uint8_t* rx_buf,uint8_t rx_len)//rx_len in [1,4]
  265. {
  266. uint8_t buf_send[8] = {FLASH_READ,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  267. uint8_t buf_rece[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  268. buf_send[1] = (addr>>16)&0xff;
  269. buf_send[2] = (addr>>8)&0xff;
  270. buf_send[3] = addr & 0xff;
  271. if(PPlus_SUCCESS == spiflash_cmd_tx_and_rx(buf_send,buf_rece,(rx_len + 4)))
  272. {
  273. switch(rx_len)
  274. {
  275. case 1:
  276. *rx_buf = buf_rece[4];
  277. break;
  278. case 2:
  279. *rx_buf = buf_rece[4];
  280. *(rx_buf+1) = buf_rece[5];
  281. break;
  282. case 3:
  283. *rx_buf = buf_rece[4];
  284. *(rx_buf+1) = buf_rece[5];
  285. *(rx_buf+2) = buf_rece[6];
  286. break;
  287. case 4:
  288. *rx_buf = buf_rece[4];
  289. *(rx_buf+1) = buf_rece[5];
  290. *(rx_buf+2) = buf_rece[6];
  291. *(rx_buf+3) = buf_rece[7];
  292. break;
  293. default:
  294. break;
  295. }
  296. }
  297. }
  298. void spiflash_read(uint32_t addr,uint8_t* rx_buf,uint16_t rx_len)
  299. {
  300. //polling mode=polling
  301. //force_cs=0
  302. uint16_t offset = 0;
  303. while(rx_len > 0)
  304. {
  305. if(rx_len >= 4)
  306. {
  307. spiflash_read_unit((addr + offset),(rx_buf + offset),4);
  308. offset += 4;
  309. rx_len -= 4;
  310. }
  311. else
  312. {
  313. spiflash_read_unit((addr + offset),(rx_buf + offset),rx_len);
  314. rx_len = 0;
  315. }
  316. }
  317. }
  318. //gd25q16
  319. int GD25_init(void)
  320. {
  321. //if(hal_spi_bus_init(&spi,cfg) == PPlus_SUCCESS)//config and init spi first
  322. //LOG("spi init success!\n");
  323. uint32_t flash_id = spiflash_read_identification();
  324. if((flash_id == 0x00)||flash_id == 0xffffff){
  325. LOG("flash id error\n");
  326. return false;
  327. }
  328. else{
  329. LOG("flash id:%x\n",flash_id);
  330. return true;
  331. }
  332. }
  333. int GD25_read(uint32_t addr,uint8_t *data,uint8_t len)
  334. {
  335. if((addr < 0x200000) && (data != NULL) && (len > 0)){
  336. spiflash_read(addr,data,len);
  337. return true;
  338. }
  339. return false;
  340. }
  341. int GD25_erase(uint32_t addr,uint32_t len)
  342. {
  343. uint8_t lockinfo = 0;
  344. uint32_t remainder = 0;
  345. if((addr >= 0x200000) || (len == 0))
  346. return false;
  347. lockinfo = spiflash_read_status_register(0);
  348. spiflash_write_status_register(0x00);
  349. if((addr == 0) && (len == 0x200000))
  350. {
  351. spiflash_chip_erase();
  352. }
  353. else
  354. {
  355. remainder = addr%0x1000;//4KB
  356. if(remainder){
  357. addr -= remainder;
  358. len += remainder;
  359. }
  360. remainder = len%0x1000;//4KB
  361. if(remainder){
  362. len = len + 0x1000 - remainder;
  363. }
  364. addr = addr/0x1000;
  365. len = len/0x1000;
  366. while(len > 0){
  367. //LOG("addr:%d len:%d\n",addr,len);
  368. if(((addr %16) == 0) && (len >= 16)){
  369. while(spiflash_bus_busy());
  370. spiflash_block_erase_64KB(addr*0x1000);
  371. addr += 16;
  372. len -= 16;
  373. continue;
  374. }
  375. if(((addr %8) == 0) && (len >= 8)){
  376. while(spiflash_bus_busy());
  377. spiflash_block_erase_32KB(addr*0x1000);
  378. addr += 8;
  379. len -= 8;
  380. continue;
  381. }
  382. if(len >= 1){
  383. while(spiflash_bus_busy());
  384. spiflash_sector_erase(addr*0x1000);
  385. addr += 1;
  386. len -= 1;
  387. continue;
  388. }
  389. }
  390. }
  391. spiflash_write_status_register(lockinfo);
  392. while(spiflash_bus_busy());
  393. return true;
  394. }
  395. int GD25_write(uint32_t addr,const uint8_t *data,uint8_t len)
  396. {
  397. if((addr < 0x200000) && (data != NULL) && (len > 0)){
  398. spiflash_write(addr,(uint8_t *)data,len);
  399. return true;
  400. }
  401. return false;
  402. }