flash.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include "flash.h"
  2. #include "nrf_delay.h"
  3. #include "nrf_log.h"
  4. #include "type.h"
  5. #include "md5.h"
  6. static bool store_flag=false;
  7. static bool update_flag=false;
  8. static bool load_flag=false;
  9. static bool clear_flag=false;
  10. pstorage_handle_t m_storage_handle_app;
  11. static pstorage_handle_t m_storage_handle_appdata;
  12. static pstorage_handle_t block_hander_appdata;
  13. static pstorage_handle_t block_hander_reset_data;
  14. static void upgrade_pstorage_cb_handler(pstorage_handle_t * p_handle,
  15. uint8_t op_code,
  16. uint32_t result,
  17. uint8_t * p_data,
  18. uint32_t data_len)
  19. {
  20. switch(op_code)
  21. {
  22. case PSTORAGE_LOAD_OP_CODE:
  23. if (result == NRF_SUCCESS)
  24. {
  25. load_flag=true;
  26. // NRF_LOG_PRINTF("PSTORAGE_LOAD_OP_CODE success \r\n");
  27. }
  28. else
  29. {
  30. // NRF_LOG_PRINTF("PSTORAGE_LOAD_OP_CODE fail \r\n");
  31. }
  32. break;
  33. case PSTORAGE_STORE_OP_CODE:
  34. if (result == NRF_SUCCESS)
  35. {
  36. store_flag=true;
  37. // NRF_LOG_PRINTF("PSTORAGE_STORE_OP_CODE success \r\n");
  38. }
  39. else
  40. {
  41. // NRF_LOG_PRINTF("PSTORAGE_STORE_OP_CODE fail \r\n");
  42. }
  43. break;
  44. case PSTORAGE_CLEAR_OP_CODE:
  45. if (result == NRF_SUCCESS)
  46. {
  47. clear_flag=true;
  48. // NRF_LOG_PRINTF("PSTORAGE_CLEAR_OP_CODE success \r\n");
  49. }
  50. else
  51. {
  52. // NRF_LOG_PRINTF("PSTORAGE_CLEAR_OP_CODE fail \r\n");
  53. }
  54. break;
  55. case PSTORAGE_UPDATE_OP_CODE:
  56. if (result == NRF_SUCCESS)
  57. {
  58. update_flag=true;
  59. // NRF_LOG_PRINTF("PSTORAGE_UPDATE_OP_CODE success \r\n");
  60. }
  61. else
  62. {
  63. // NRF_LOG_PRINTF("PSTORAGE_UPDATE_OP_CODE fail \r\n");
  64. }
  65. break;
  66. default:break;
  67. }
  68. }
  69. uint16_t flash_upgrade_init(void)
  70. {
  71. pstorage_module_param_t param;
  72. uint16_t err_code;
  73. param.cb = upgrade_pstorage_cb_handler;
  74. m_storage_handle_app.block_id = DFU_BANK_1_REGION_START;
  75. // NRF_LOG_PRINTF("flash bank1 start is[%02X],size is[%d k]\r\n",m_storage_handle_app.block_id,DFU_IMAGE_MAX_SIZE_BANKED/1024);
  76. err_code = pstorage_raw_register(&param,&m_storage_handle_app);
  77. if(err_code == NRF_SUCCESS)
  78. {
  79. // NRF_LOG_PRINTF("flash pstorage register success!\r\n");
  80. }
  81. else
  82. {
  83. // NRF_LOG_PRINTF("flash pstorage register fail!err_code is %d\r\n",err_code);
  84. return err_code;
  85. }
  86. clear_flag = false;
  87. err_code = pstorage_raw_clear(&m_storage_handle_app,DFU_IMAGE_MAX_SIZE_BANKED);
  88. if(err_code == NRF_SUCCESS)
  89. {
  90. // NRF_LOG_PRINTF("flash pstorage clear success!\r\n");
  91. }
  92. else
  93. {
  94. // NRF_LOG_PRINTF("flash pstorage clear fail!err_code is %d\r\n",err_code);
  95. return err_code;
  96. }
  97. while(!clear_flag)
  98. {
  99. // NRF_LOG_PRINTF("wating clear...\r\n");
  100. //nrf_delay_ms(1);
  101. nrf_delay_ms(50);
  102. }
  103. return err_code;
  104. }
  105. uint16_t flash_upgrade_store(uint8_t* input,uint16_t input_len,uint16_t ota_bin_offset)
  106. {
  107. uint16_t err_code;
  108. // NRF_LOG_PRINTF("flash pstorage start store,input_len is %d,offset is %d\r\n",input_len,ota_bin_offset);
  109. store_flag = false;
  110. err_code = pstorage_raw_store(&m_storage_handle_app,input,input_len,ota_bin_offset);
  111. if(err_code == NRF_SUCCESS)
  112. {
  113. // NRF_LOG_PRINTF("flash pstorage store success!\r\n");
  114. }
  115. else
  116. {
  117. // NRF_LOG_PRINTF("flash pstorage store fail!err_code is %d\r\n",err_code);
  118. return err_code;
  119. }
  120. while(!store_flag)
  121. {
  122. // NRF_LOG_PRINTF("wating store...\r\n");
  123. nrf_delay_ms(5);
  124. //nrf_delay_ms(1);
  125. }
  126. return err_code;
  127. }
  128. uint16_t appdata_flash_init(void)
  129. {
  130. pstorage_module_param_t param;
  131. uint16_t err_code;
  132. err_code = pstorage_init();
  133. if(err_code == NRF_SUCCESS)
  134. {
  135. // NRF_LOG_PRINTF("appdata flash pstorage init success!\r\n");
  136. }
  137. else
  138. {
  139. // NRF_LOG_PRINTF("appdata flash pstorage init fail!err_code is %d\r\n",err_code);
  140. return err_code;
  141. }
  142. param.block_size = PSTORAGE_MAX_BLOCK_SIZE;
  143. param.block_count = 2;
  144. param.cb = upgrade_pstorage_cb_handler;
  145. m_storage_handle_appdata.block_id = DFU_APP_DATA_START;
  146. // NRF_LOG_PRINTF("appdata flash start is %02X\r\n",m_storage_handle_appdata.block_id);
  147. err_code = pstorage_register(&param,&m_storage_handle_appdata);
  148. if(err_code == NRF_SUCCESS)
  149. {
  150. // NRF_LOG_PRINTF("appdata flash pstorage register success!\r\n");
  151. }
  152. else
  153. {
  154. // NRF_LOG_PRINTF("appdata flash pstorage register fail!err_code is %d\r\n",err_code);
  155. return err_code;
  156. }
  157. return err_code;
  158. }
  159. uint16_t flash_upgrade_clear(void)
  160. {
  161. uint16_t err_code;
  162. clear_flag = false;
  163. err_code = pstorage_raw_clear(&m_storage_handle_app,DFU_IMAGE_MAX_SIZE_BANKED);
  164. if(err_code == NRF_SUCCESS)
  165. {
  166. // NRF_LOG_PRINTF("flash pstorage clear success!\r\n");
  167. }
  168. else
  169. {
  170. // NRF_LOG_PRINTF("flash pstorage clear fail!err_code is %d\r\n",err_code);
  171. return err_code;
  172. }
  173. while(!clear_flag)
  174. {
  175. // NRF_LOG_PRINTF("wating clear...\r\n");
  176. //nrf_delay_ms(1);
  177. nrf_delay_ms(50);
  178. }
  179. return err_code;
  180. }
  181. uint16_t appdata_flash_update(uint8_t *src,uint16_t size,uint16_t offset)
  182. {
  183. uint16_t err_code;
  184. err_code = pstorage_block_identifier_get(&m_storage_handle_appdata,1,&block_hander_appdata);
  185. if(err_code == NRF_SUCCESS)
  186. {
  187. // NRF_LOG_PRINTF("appdata flash pstorage block id get success!\r\n");
  188. }
  189. else
  190. {
  191. // NRF_LOG_PRINTF("appdata flash pstorage block id get fail,err_code is %d!\r\n",err_code);
  192. return err_code;
  193. }
  194. update_flag = false;
  195. err_code = pstorage_update(&block_hander_appdata,src,size,offset);
  196. if(err_code == NRF_SUCCESS)
  197. {
  198. // NRF_LOG_PRINTF("appdata flash pstorage update success!\r\n");
  199. }
  200. else
  201. {
  202. // NRF_LOG_PRINTF("appdata flash pstorage update fail,err_code is %d!\r\n",err_code);
  203. return err_code;
  204. }
  205. while(!update_flag)
  206. {
  207. // NRF_LOG_PRINTF("appdata wating update...\r\n");
  208. nrf_delay_ms(5);
  209. }
  210. return err_code;
  211. }
  212. uint16_t appdata_flash_load(uint8_t *dest,uint16_t size,uint16_t offset)
  213. {
  214. uint16_t err_code;
  215. err_code = pstorage_block_identifier_get(&m_storage_handle_appdata,1,&block_hander_appdata);
  216. if(err_code == NRF_SUCCESS)
  217. {
  218. // NRF_LOG_PRINTF("appdata flash pstorage block id get success!\r\n");
  219. }
  220. else
  221. {
  222. // NRF_LOG_PRINTF("appdata flash pstorage block id get fail,err_code is %d!\r\n",err_code);
  223. return err_code;
  224. }
  225. load_flag = false;
  226. err_code = pstorage_load(dest,&block_hander_appdata,size,offset);
  227. if(err_code == NRF_SUCCESS)
  228. {
  229. // NRF_LOG_PRINTF("appdata flash pstorage load success!\r\n");
  230. }
  231. else
  232. {
  233. // NRF_LOG_PRINTF("appdata flash pstorage load fail,err_code is %d!\r\n",err_code);
  234. return err_code;
  235. }
  236. while(!load_flag)
  237. {
  238. // NRF_LOG_PRINTF("appdata wating load...\r\n");
  239. nrf_delay_ms(5);
  240. }
  241. return err_code;
  242. }
  243. uint16_t reset_data_flash_update(uint8_t *src,uint16_t size,uint16_t offset)
  244. {
  245. uint16_t err_code;
  246. err_code = pstorage_block_identifier_get(&m_storage_handle_appdata,0,&block_hander_reset_data);
  247. if(err_code == NRF_SUCCESS)
  248. {
  249. // NRF_LOG_PRINTF("appdata flash pstorage block id get success!\r\n");
  250. }
  251. else
  252. {
  253. // NRF_LOG_PRINTF("appdata flash pstorage block id get fail,err_code is %d!\r\n",err_code);
  254. return err_code;
  255. }
  256. update_flag = false;
  257. err_code = pstorage_update(&block_hander_reset_data,src,size,offset);
  258. if(err_code == NRF_SUCCESS)
  259. {
  260. // NRF_LOG_PRINTF("appdata flash pstorage update success!\r\n");
  261. }
  262. else
  263. {
  264. // NRF_LOG_PRINTF("appdata flash pstorage update fail,err_code is %d!\r\n",err_code);
  265. return err_code;
  266. }
  267. while(!update_flag)
  268. {
  269. // NRF_LOG_PRINTF("appdata wating update...\r\n");
  270. nrf_delay_ms(5);
  271. }
  272. return err_code;
  273. }
  274. uint16_t reset_data_flash_load(uint8_t *dest,uint16_t size,uint16_t offset)
  275. {
  276. uint16_t err_code;
  277. err_code = pstorage_block_identifier_get(&m_storage_handle_appdata,0,&block_hander_reset_data);
  278. if(err_code == NRF_SUCCESS)
  279. {
  280. // NRF_LOG_PRINTF("appdata flash pstorage block id get success!\r\n");
  281. }
  282. else
  283. {
  284. // NRF_LOG_PRINTF("appdata flash pstorage block id get fail,err_code is %d!\r\n",err_code);
  285. return err_code;
  286. }
  287. load_flag = false;
  288. err_code = pstorage_load(dest,&block_hander_reset_data,size,offset);
  289. if(err_code == NRF_SUCCESS)
  290. {
  291. // NRF_LOG_PRINTF("appdata flash pstorage load success!\r\n");
  292. }
  293. else
  294. {
  295. // NRF_LOG_PRINTF("appdata flash pstorage load fail,err_code is %d!\r\n",err_code);
  296. return err_code;
  297. }
  298. while(!load_flag)
  299. {
  300. // NRF_LOG_PRINTF("appdata wating load...\r\n");
  301. nrf_delay_ms(5);
  302. }
  303. return err_code;
  304. }