app_flash.c 14 KB

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