app_flash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. #include "app_flash.h"
  2. #include "bsp_wdt.h"
  3. #include "exception.h"
  4. #include "system.h"
  5. #include "hal_minifds.h"
  6. #include "nrf_sdh_soc.h"
  7. #include "app_step.h"
  8. #include "nrf_pwr_mgmt.h"
  9. #include "bsp_flash.h"
  10. /*************************************
  11. *DEFINITION
  12. */
  13. #define FLASH_BACKUP_START_ADDR 0x00073000UL
  14. #define FLASH_BACKUP_END_ADDR_INFOR 0x00073FFFUL
  15. #define FLASH_BACKUP_ADDR_PAGENUM 1
  16. #define FLASH_INFOR_START_ADDR 0x00074000UL
  17. #define FLASH_INFOR_END_ADDR 0x00076000UL
  18. #define FLASH_INFOR_ADDR_PAGENUM 2
  19. #define FLASH_START_ADDR_STEP 0x00076000UL
  20. #define FLASH_END_ADDR_STEP 0x00077FFFUL
  21. #define FLASH_ADDR_STEP_PAGENUM 2
  22. #if FLASH_BACKUP_END_ADDR_INFOR>=FLASH_BACKUP_START_ADDR?0:1
  23. #error BACKUP ADDER FAIL
  24. #endif
  25. #if FLASH_INFOR_END_ADDR>=FLASH_INFOR_START_ADDR?0:1
  26. #error INFOR ADDER FAIL
  27. #endif
  28. #define MaxLength(a,b) a>b?a:b
  29. typedef struct t_mflash_param_type{
  30. const char *name; //参数名字
  31. void *value; //数据指针
  32. const uint8_t length; //数据长度
  33. }mflash_param_type;
  34. const mflash_param_type All_Falsh_value[]={
  35. {"inforhead",&mFlash.head,4},//不可调整
  36. {"macHost",mFlash.macHost,RecordMacAddrL},
  37. {"IsHost",&mFlash.isHost,1},
  38. {"LR_FLAG",&mFlash.LR_FLAG,1},
  39. {"startTime",mFlash.mStep.startTime,8},
  40. {"stepCur",mFlash.mStep.stepCur,8},
  41. {"step",mFlash.mStep.step,8},
  42. {"step_num",&mFlash.mStep.step_num,4},
  43. {"C_isConfig",&mFlash.mClient.isConfig,1},
  44. {"C_hard",&mFlash.mClient.hardVersion,4},
  45. {"C_sotf",&mFlash.mClient.sotfVersion,2},
  46. {"C_macAddr",&mFlash.mClient.macAddr,RecordMacAddrL},
  47. {"Errorflag",&mFlash.mFlashLog.Errorflag,1},
  48. {"logData",mFlash.mFlashLog.logData,50},
  49. {"PB_BR_V2E",&mFlash.mbattercb_t.preBestResult_Voltage2power,4},
  50. {"PB_BR_C2P",&mFlash.mbattercb_t.preBestResult_chargeV2P_f,4},
  51. {"PB_P_MAH",&mFlash.mbattercb_t.P_mAh,4},
  52. {"PB_KG", &mFlash.mbattercb_t.kg,4},
  53. {"PP2", &mFlash.mbattercb_t.P2,4},
  54. {"PP1", &mFlash.mbattercb_t.P1,4},
  55. {"PCAP_MAH",&mFlash.mbattercb_t.Battery_capacity_mAh,4},
  56. {"PTP4056",&mFlash.mbattercb_t.adc_tp4056_power,4},
  57. {"PINIT", &mFlash.mbattercb_t.init,1},
  58. {"PSTA", &mFlash.mbattercb_t.sta,1},
  59. {"PCV2P_INIT",&mFlash.mbattercb_t.chargeV2P_f_init,1},
  60. {"PV2P_INIT",&mFlash.mbattercb_t.Voltage2power_init,1},
  61. };
  62. static const uint8_t MAX_mflash_param_LENGTH =sizeof(All_Falsh_value)/sizeof(All_Falsh_value[0]);
  63. DEFINE_MINIINF(information, FLASH_INFOR_START_ADDR, FLASH_INFOR_END_ADDR, sdflash_write, sdflash_page_erase);
  64. Flash_t mFlash={0};
  65. FlashBackup_t mBackup={0};
  66. /****************************************************接口****************************************************/
  67. void Flash_Initialize(void)
  68. {
  69. uint16_t i = 0;
  70. uint8_t length =0;
  71. uint32_t sucess = Flash_GetBackup(&mBackup);
  72. DEBUG_LOG("Flash_GetBackup (&mBackup):%d,MAX_mflash_param_LENGTH:%d\n",sucess,MAX_mflash_param_LENGTH);
  73. //查看是否有连续重启的标志位
  74. if(mBackup.ErrStartCnt >= 5){
  75. Flash_DeleteAllInfor();
  76. Flash_DeleteAllStep();
  77. mBackup.ErrStartCnt =0;
  78. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_FLASH,"save backup fail");
  79. DEBUG_LOG("mBackup.ErrorStartFlag >= 5!!!!!!!!!!!\n");
  80. NVIC_SystemReset();
  81. }
  82. else{
  83. mBackup.ErrStartCnt++;
  84. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_FLASH,"save backup fail");
  85. }
  86. minfds_init(&information);
  87. //读取记录
  88. for(i =0;i<MAX_mflash_param_LENGTH;i++){
  89. if(MINIFDS_SUCCESS == pm_get_length(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),&length)){
  90. if(length == All_Falsh_value[i].length){
  91. pm_find(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),(uint8_t *)All_Falsh_value[i].value,&length);
  92. }
  93. }
  94. }
  95. DEBUG_LOG("Flash head read(%04X).\n",mFlash.head);
  96. //查看是否第一次写入
  97. if(mFlash.head !=FLASH_HEAD){ DEBUG_LOG("Flash first init(%04X),write infomation to flash.\n",mFlash.head);
  98. Flash_DeleteAllInfor();
  99. Flash_DeleteAllStep();
  100. memset((uint8_t*)(&mFlash),0,sizeof(Flash_t));
  101. mFlash.head = FLASH_HEAD;
  102. mFlash.isHost = _IS_HOST;
  103. //从备份预期中拷贝数据
  104. if(mBackup.head == FLASH_HEAD){
  105. uint8_t i =0;
  106. for(i=0;i<RecordMacAddrL;i++){
  107. mFlash.macHost[i] = mBackup.macAddr_L[i]; //主机地址
  108. mFlash.mClient.macAddr[i] = mBackup.macAddr_R[i];//从机地址
  109. }
  110. DEBUG_LOG("Back macAddr_L:");for(i=0;i<RecordMacAddrL;i++){DEBUG_LOG("%02X:",mBackup.macAddr_L[i]);}DEBUG_LOG("\r\n");
  111. DEBUG_LOG("Back macAddr_R:");for(i=0;i<RecordMacAddrL;i++){DEBUG_LOG("%02X:",mBackup.macAddr_R[i]);}DEBUG_LOG("\r\n");
  112. mFlash.mClient.hardVersion = mBackup.hardVersion;
  113. mFlash.mClient.sotfVersion = mBackup.sotfVersion;
  114. mFlash.mClient.isConfig = mBackup.isConfig;
  115. mFlash.isHost = mBackup.isHost;
  116. mFlash.LR_FLAG = mBackup.LR_FLAG;
  117. mFlash.selfcheck_flow = mBackup.selfcheck_flow;
  118. DEBUG_LOG("Back Data:ifconfig:%d,hardVer:%d,backup.softversion:%d\n",mBackup.isConfig,mBackup.hardVersion,mBackup.sotfVersion);
  119. }else{
  120. memset((uint8_t*)(&mBackup),0,sizeof(FlashBackup_t));
  121. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_FLASH,"save backup fail");
  122. }
  123. for(i =0;i<MAX_mflash_param_LENGTH;i++){
  124. pm_add(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),(uint8_t *)All_Falsh_value[i].value,All_Falsh_value[i].length);
  125. }
  126. mFlash.head =0;
  127. //查看是否写入成功
  128. for(i =0;i<MAX_mflash_param_LENGTH;i++){
  129. if(MINIFDS_SUCCESS == pm_get_length(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),&length)){
  130. if(length == All_Falsh_value[i].length)
  131. {
  132. pm_find(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),(uint8_t *)All_Falsh_value[i].value,&length);
  133. }
  134. }
  135. }
  136. DEBUG_LOG("Flash head second read(%04X).\n",mFlash.head);
  137. if(mFlash.head != FLASH_HEAD){
  138. DEBUG_LOG("Flash write head fail.\n");
  139. Except_TxError(EXCEPT_FLASH,"Flash write head fail");
  140. return;
  141. }
  142. DEBUG_LOG("System reset...\n",mFlash.head);
  143. for(uint8_t i =0;i<6;i++){
  144. nrf_delay_ms(500);
  145. feed_watchdog();
  146. }
  147. NVIC_SystemReset();
  148. }
  149. else{
  150. for(i =0;i<MAX_mflash_param_LENGTH;i++){
  151. if(MINIFDS_SUCCESS == pm_get_length(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),&length)){
  152. if(length == All_Falsh_value[i].length){
  153. if(memcmp(&All_Falsh_value[i].name,"selfcheck_flow",strlen(All_Falsh_value[i].name)) == 0)
  154. {
  155. mFlash.selfcheck_flow = mBackup.selfcheck_flow;
  156. }
  157. pm_find(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),(uint8_t *)All_Falsh_value[i].value,&length);
  158. }
  159. else{
  160. pm_delete(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name));
  161. pm_add(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),(uint8_t *)All_Falsh_value[i].value,All_Falsh_value[i].length);
  162. }
  163. }
  164. else{
  165. if(memcmp(&All_Falsh_value[i].name,"selfcheck_flow",strlen(All_Falsh_value[i].name)) == 0)
  166. {
  167. mFlash.selfcheck_flow = mBackup.selfcheck_flow;
  168. }
  169. pm_add(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),(uint8_t *)All_Falsh_value[i].value,All_Falsh_value[i].length);
  170. }
  171. }
  172. }
  173. DEBUG_LOG("err code :%s\n",mFlash.mFlashLog.logData);
  174. DEBUG_LOG("Flash init ok.\n");
  175. //TestHalFlashInterface();
  176. }
  177. /**
  178. @brief 设置清空脚步区域flash的标志
  179. @param 无
  180. @return 无
  181. */
  182. static uint8_t FlashStep_ClearFlag =0;
  183. void flash_SetClearStepFlag(void){
  184. FlashStep_ClearFlag =1;
  185. }
  186. uint32_t flash_GetStep_StartAddress(void){
  187. return FLASH_START_ADDR_STEP;
  188. }
  189. /**
  190. @brief 存储步数
  191. @param 无
  192. @return 错误代码
  193. */
  194. uint32_t Flash_SaveStep(void)
  195. {
  196. flash_OPER_Result err_code;
  197. uint32_t flash_data;
  198. #define Max_Hour PAGE_INT_SIZE * FLASH_ADDR_STEP_PAGENUM
  199. if(mFlash.mStep.step_num < Max_Hour)
  200. {
  201. // mFlash.mStep.stepCur[0] +=1 ;
  202. uint32_t step = app_step_GetStep_L() + app_step_GetStep_R(); //获取左右鞋步数
  203. flash_data = ((step<<24 & 0xff000000) | (step<<8 & 0x00ff0000) | (step>>8 & 0x0000ff00) | (step>>24 & 0x000000ff));
  204. //清空步数的flash step内容
  205. if(1 == FlashStep_ClearFlag){
  206. if(Flash_DeleteAllStep() != ZONE_OP_SUCCESS){
  207. Except_TxError(EXCEPT_DATEStep,"clear step fail");
  208. return ZONE_ERROR_WRITE_FAIL;
  209. }
  210. mFlash.mStep.step_num = 0;
  211. FlashStep_ClearFlag = 0;
  212. }
  213. DEBUG_LOG("step %d,flash_data:%d,step_num:%d\n",step, flash_data,mFlash.mStep.step_num);
  214. err_code = sdflash_write((uint32_t*)(FLASH_START_ADDR_STEP + (mFlash.mStep.step_num * 4)), &flash_data, 1);
  215. if(err_code != FLASH_OP_SUCCESS)return ZONE_ERROR_WRITE_FAIL;
  216. mFlash.mStep.step_num++;
  217. mFlash.mStep.step[0] = mFlash.mStep.stepCur[0];
  218. mFlash.mStep.step[1] = mFlash.mStep.stepCur[1];
  219. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS){
  220. Except_TxError(EXCEPT_FLASH,"save information fail");
  221. }
  222. uint8_t i=4;
  223. uint8_t length =0;
  224. for(;i<5;i++){
  225. if(MINIFDS_SUCCESS == pm_get_length(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),&length)){
  226. if(length == All_Falsh_value[i].length){
  227. if(MINIFDS_SUCCESS == pm_find(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),(uint8_t *)All_Falsh_value[i].value,&length))
  228. {
  229. DEBUG_LOG(">>>>>>>>%s value:%d\n",All_Falsh_value[i].name,mFlash.mStep.stepCur[0]);
  230. }
  231. }
  232. }
  233. }
  234. }
  235. else return ZONE_ERROR_WRITE_FAIL;
  236. return ZONE_OP_SUCCESS;
  237. }
  238. /**
  239. @brief 删除所有步数
  240. @param 无
  241. @return 错误代码
  242. */
  243. uint32_t Flash_DeleteAllStep(void)
  244. {
  245. uint32_t errcode = ZONE_OP_SUCCESS;
  246. uint32_t pag_addr = 0;
  247. for(uint8_t i=0;i<FLASH_ADDR_STEP_PAGENUM;i++){
  248. pag_addr = (FLASH_START_ADDR_STEP +i*FLASH_PAGE_SIZE);
  249. errcode = sdflash_page_erase(pag_addr);
  250. if(errcode != NRF_SUCCESS)
  251. return ZONE_ERROR_ERASE_FAIL;
  252. }
  253. return ZONE_OP_SUCCESS;
  254. }
  255. /**
  256. @brief 删除infromation的数据
  257. @param 无
  258. @return 错误代码
  259. */
  260. uint32_t Flash_DeleteAllInfor(void)
  261. {
  262. uint32_t errcode = ZONE_OP_SUCCESS;
  263. uint32_t pag_addr = 0;
  264. for(uint8_t i=0;i<FLASH_INFOR_ADDR_PAGENUM;i++){
  265. pag_addr = (FLASH_INFOR_START_ADDR +i*FLASH_PAGE_SIZE);
  266. errcode = sdflash_page_erase(pag_addr);
  267. if(errcode != NRF_SUCCESS)
  268. return ZONE_ERROR_ERASE_FAIL;
  269. }
  270. return ZONE_OP_SUCCESS;
  271. }
  272. /**
  273. @brief 删除Backup的数据
  274. @param 无
  275. @return 错误代码
  276. */
  277. uint32_t Flash_DeleteAllBackup(void)
  278. {
  279. uint32_t errcode = ZONE_OP_SUCCESS;
  280. uint32_t pag_addr = 0;
  281. for(uint8_t i=0;i<FLASH_BACKUP_ADDR_PAGENUM;i++){
  282. pag_addr = (FLASH_BACKUP_START_ADDR +i*FLASH_PAGE_SIZE);
  283. errcode = sdflash_page_erase(pag_addr);
  284. if(errcode != NRF_SUCCESS)
  285. return ZONE_ERROR_ERASE_FAIL;
  286. }
  287. return ZONE_OP_SUCCESS;
  288. }
  289. /**
  290. @brief 读取一个区域中指定位置的数据
  291. @param destination_addr[in] - 读取区域的地址
  292. @param pData[in] - 指向存储的buff
  293. @param dataLen[in] - 读取的长度,单位字节
  294. @return 错误代码
  295. */
  296. uint32_t flash_Data_Read(uint32_t addr, uint32_t *pData, uint32_t dataLen){
  297. if(pData != NULL && dataLen != 0){
  298. if(addr < START_FSTORAGE_ADDR || addr >= END_FSTORAGE_ADDR){
  299. return ZONE_ERROR_ADDRESS_FAIL;
  300. }
  301. else memcpy(pData, (uint32_t*)addr, dataLen);
  302. }
  303. return ZONE_OP_SUCCESS;
  304. }
  305. /**
  306. @brief 存储基本信息
  307. @param 无
  308. @return 错误代码
  309. */
  310. uint32_t Flash_SaveInfomation(void)
  311. {
  312. char result =0;
  313. for(uint8_t i =0;i<MAX_mflash_param_LENGTH;i++){
  314. result = pm_update(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),(uint8_t *)All_Falsh_value[i].value,All_Falsh_value[i].length);
  315. if(result != MINIFDS_SUCCESS){
  316. DEBUG_LOG("Flash_SaveInfomation fail name %s,result %d\r\n",All_Falsh_value[i].name,result);
  317. return ZONE_ERROR_WRITE_FAIL;
  318. }
  319. }
  320. return ZONE_OP_SUCCESS;
  321. }
  322. /**
  323. @brief 存储备份信息
  324. @param 无
  325. @return 错误代码
  326. */
  327. uint32_t Flash_SaveBackup(void)
  328. {
  329. uint32_t errcode = ZONE_OP_SUCCESS;
  330. uint32_t pag_addr = 0;
  331. mBackup.head = FLASH_HEAD;
  332. for(uint8_t i=0;i<FLASH_BACKUP_ADDR_PAGENUM;i++){
  333. pag_addr = (FLASH_BACKUP_START_ADDR +i*FLASH_PAGE_SIZE);
  334. errcode = sdflash_page_erase(pag_addr);
  335. if(errcode != NRF_SUCCESS)
  336. return ZONE_ERROR_ERASE_FAIL;
  337. }
  338. errcode = sdflash_write((uint32_t*)FLASH_BACKUP_START_ADDR, (uint32_t*)&mBackup, sizeof(mBackup)/4);
  339. if(errcode != FLASH_OP_SUCCESS)return ZONE_ERROR_WRITE_FAIL;
  340. return ZONE_OP_SUCCESS;
  341. }
  342. /**
  343. @brief 获取备份信息
  344. @param 无
  345. @return 错误代码
  346. */
  347. uint32_t Flash_GetBackup(FlashBackup_t *pbackup)
  348. {
  349. return flash_Data_Read(FLASH_BACKUP_START_ADDR,(uint32_t*)pbackup, sizeof(FlashBackup_t));
  350. }
  351. /**
  352. @brief 保存日志信息
  353. @param[in] id Fault identifier. See @ref NRF_FAULT_IDS.
  354. @param[in] pc The program counter of the instruction that triggered the fault, or 0 if
  355. unavailable.
  356. @param[in] info Optional additional information regarding the fault. The value of the @p id
  357. parameter dictates how to interpret this parameter. Refer to the documentation
  358. for each fault identifier (@ref NRF_FAULT_IDS and @ref APP_ERROR_FAULT_IDS) for
  359. details about interpreting @p info.
  360. @return 错误代码
  361. */
  362. uint32_t Flash_SaveLog(uint32_t id, uint32_t pc, uint32_t info)
  363. {
  364. memset((uint8_t*)(&mFlash.mFlashLog),0,sizeof(FlashLog));
  365. mFlash.mFlashLog.Errorflag =1;
  366. switch (id)
  367. {
  368. #if defined(SOFTDEVICE_PRESENT) && SOFTDEVICE_PRESENT
  369. case NRF_FAULT_ID_SD_ASSERT:
  370. memcpy(mFlash.mFlashLog.logData,"SD: ASSERTION FAILED\r\n",MaxLength(sizeof("SD: ASSERTION FAILED\r\n"),sizeof(mFlash.mFlashLog.logData)));
  371. break;
  372. case NRF_FAULT_ID_APP_MEMACC:
  373. memcpy(mFlash.mFlashLog.logData,"SD: INVALID MEMORY ACCESS\r\n",MaxLength(sizeof("SD: INVALID MEMORY ACCESS\r\n"),sizeof(mFlash.mFlashLog.logData)));
  374. break;
  375. #endif
  376. case NRF_FAULT_ID_SDK_ASSERT:
  377. {
  378. assert_info_t * p_info = (assert_info_t *)info;
  379. sprintf((char *)mFlash.mFlashLog.logData,"ASSERTION FAILED %s:%u\r\n",
  380. p_info->p_file_name,
  381. p_info->line_num);
  382. break;
  383. }
  384. case NRF_FAULT_ID_SDK_ERROR:
  385. {
  386. error_info_t * p_info = (error_info_t *)info;
  387. sprintf((char *)mFlash.mFlashLog.logData,"error:%u,%s:%u\r\n",
  388. p_info->err_code,
  389. p_info->p_file_name,
  390. p_info->line_num);
  391. DEBUG_LOG(">>>>>err code :%d,%s",p_info->err_code,mFlash.mFlashLog.logData);
  392. break;
  393. }
  394. default:
  395. sprintf((char *)mFlash.mFlashLog.logData,"UNKNOWN FAULT 0x%08X\n", pc);
  396. break;
  397. }
  398. return Flash_SaveInfomation();
  399. }
  400. /**
  401. @brief 测试halflash接口
  402. @param 无
  403. @return 无
  404. */
  405. void TestHalFlashInterface(void)
  406. {
  407. // uint2_t i;
  408. // Flash_t m_testflash;
  409. // FlashBackup_t m_testbackup;
  410. // //测试基本信息和备份信息的写入和读取
  411. // for(i=0;i<10000;i++)
  412. // {
  413. // mFlash.mStep.step_num = i;
  414. // DEBUG_LOG("Flash_SaveInfomation[%d]:%d \n",i,Flash_SaveInfomation());
  415. //
  416. // DEBUG_LOG("Flash_SaveBackup[%d]:%d \n",i,Flash_SaveBackup());
  417. // }
  418. //
  419. // DEBUG_LOG("Flash_GetInfomation[%d]:%d \n",i,Flash_GetInfomation(&m_testflash));
  420. // DEBUG_LOG("m_testflash:%d \n",m_testflash.mStep.num);
  421. //
  422. // DEBUG_LOG("Flash_GetBackup[%d]:%d \n",i,Flash_GetBackup(&m_testbackup));
  423. // DEBUG_LOG("m_testbackup:%d \n",m_testbackup.hardVersion);
  424. }