app_flash - 副本.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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_HEAD 0xAA5555AA
  14. #define FLASH_START_ADDR_INFOR 0x00074000UL
  15. #define FLASH_END_ADDR_INFOR 0x00076000UL
  16. #define FLASH_START_ADDR_STEP 0x00076000UL
  17. #define FLASH_END_ADDR_STEP 0x00077FFFUL
  18. #define FLASH_ADDR_STEP_PAGENUM 2
  19. #define MaxLength(a,b) a>b?a:b
  20. typedef struct t_mflash_param_type{
  21. const char *name; //参数名字
  22. void *value; //数据指针
  23. const uint8_t length; //数据长度
  24. // const myvalue_type mtype;//数据类型
  25. }mflash_param_type;
  26. #define MAX_mflash_param_LENGTH 27
  27. const mflash_param_type All_Falsh_value[MAX_mflash_param_LENGTH]={
  28. {"inforhead",&mFlash.head,4},
  29. {"macHost",mFlash.macHost,6},
  30. {"IsHost",&mFlash.isHost,1},
  31. {"startTime",mFlash.mStep.startTime,8},
  32. {"stepCur",mFlash.mStep.stepCur,8},
  33. {"step",mFlash.mStep.step,8},
  34. {"step_num",&mFlash.mStep.step_num,4},
  35. {"C_isConfig",&mFlash.mClient.isConfig,1},
  36. {"C_hard",&mFlash.mClient.hardVersion,4},
  37. {"C_sotf",&mFlash.mClient.sotfVersion,2},
  38. {"C_macAddr",&mFlash.mClient.macAddr,6},
  39. {"Errorflag",&mFlash.mFlashLog.Errorflag,1},
  40. {"logData",mFlash.mFlashLog.logData,50},
  41. {"PB_BR_V2E",&mFlash.mbattercb_t.preBestResult_Voltage2power,4},
  42. {"PB_BR_C2P",&mFlash.mbattercb_t.preBestResult_chargeV2P_f,4},
  43. {"PB_P_MAH",&mFlash.mbattercb_t.P_mAh,4},
  44. {"PB_KG", &mFlash.mbattercb_t.kg,4},
  45. {"PP2", &mFlash.mbattercb_t.P2,4},
  46. {"PP1", &mFlash.mbattercb_t.P1,4},
  47. {"PCAP_MAH",&mFlash.mbattercb_t.Battery_capacity_mAh,4},
  48. {"PTP4056",&mFlash.mbattercb_t.adc_tp4056_power,4},
  49. {"PINIT", &mFlash.mbattercb_t.init,1},
  50. {"PSTA", &mFlash.mbattercb_t.sta,1},
  51. {"PCV2P_INIT",&mFlash.mbattercb_t.chargeV2P_f_init,1},
  52. {"PV2P_INIT",&mFlash.mbattercb_t.Voltage2power_init},
  53. {"Safe_cnt",&mFlash.Safe_cnt,1},
  54. {"RestartCnt",&mFlash.RestartCnt,1},
  55. };
  56. /*
  57. typedef enum myvalue_type{
  58. Value_CHAR =0,
  59. Value_UINT8_T,
  60. Value_UINT16_T,
  61. Value_UINT32_T,
  62. Value_FLOAT,
  63. Value_BUFF
  64. }myvalue_type;
  65. const mflash_param_type All_Falsh_value[MAX_mflash_param_LENGTH]={
  66. {"inforhead",&mFlash.head,4,Value_UINT32_T},
  67. {"macHost",mFlash.macHost,6,Value_BUFF},
  68. {"IsHost",&mFlash.isHost,1,Value_UINT8_T},
  69. {"startTime",mFlash.mStep.startTime,8,Value_BUFF},
  70. {"stepCur",mFlash.mStep.stepCur,8,Value_BUFF},
  71. {"step",mFlash.mStep.step,8,Value_BUFF},
  72. {"step_num",&mFlash.mStep.step_num,4,Value_UINT32_T},
  73. {"C_isConfig",&mFlash.mClient.isConfig,1,Value_UINT8_T},
  74. {"C_hard",&mFlash.mClient.hardVersion,4,Value_UINT32_T},
  75. {"C_sotf",&mFlash.mClient.sotfVersion,2,Value_UINT16_T},
  76. {"C_macAddr",&mFlash.mClient.macAddr,6,Value_BUFF},
  77. {"Errorflag",&mFlash.mFlashLog.Errorflag,1,Value_UINT8_T},
  78. {"logData",mFlash.mFlashLog.logData,50,Value_UINT8_T},
  79. {"PB_BR_V2E",&mFlash.mbattercb_t.preBestResult_Voltage2power,4,Value_FLOAT},
  80. {"PB_BR_C2P",&mFlash.mbattercb_t.preBestResult_chargeV2P_f,4,Value_FLOAT},
  81. {"PB_P_MAH",&mFlash.mbattercb_t.P_mAh,4,Value_FLOAT},
  82. {"PB_KG", &mFlash.mbattercb_t.kg,4,Value_FLOAT},
  83. {"PP2", &mFlash.mbattercb_t.P2,4,Value_FLOAT},
  84. {"PP1", &mFlash.mbattercb_t.P1,4,Value_FLOAT},
  85. {"PCAP_MAH",&mFlash.mbattercb_t.Battery_capacity_mAh,4,Value_FLOAT},
  86. {"PTP4056",&mFlash.mbattercb_t.adc_tp4056_power,4,Value_FLOAT},
  87. {"PINIT", &mFlash.mbattercb_t.init,1,Value_CHAR},
  88. {"PSTA", &mFlash.mbattercb_t.sta,1,Value_CHAR},
  89. {"PCV2P_INIT",&mFlash.mbattercb_t.chargeV2P_f_init,1,Value_CHAR},
  90. {"PV2P_INIT",&mFlash.mbattercb_t.Voltage2power_init,1,Value_CHAR},
  91. {"Safe_cnt",&mFlash.Safe_cnt,1,Value_UINT8_T},
  92. {"RestartCnt",&mFlash.RestartCnt,1,Value_UINT8_T},
  93. };*/
  94. DEFINE_MINIINF(information, FLASH_START_ADDR_INFOR, FLASH_END_ADDR_INFOR, sdflash_write, sdflash_page_erase);
  95. Flash_t mFlash={0};
  96. /****************************************************接口****************************************************/
  97. void Flash_Initialize(void)
  98. {
  99. uint8_t i = 0;
  100. uint8_t length =0;
  101. minfds_init(&information);
  102. for(i =0;i<MAX_mflash_param_LENGTH;i++){
  103. if(MINIFDS_SUCCESS == pm_get_length(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),&length)){
  104. if(length == All_Falsh_value[i].length){
  105. pm_find(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),(uint8_t *)All_Falsh_value[i].value,&length);
  106. }
  107. else{
  108. pm_delete(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name));
  109. 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);
  110. }
  111. }
  112. }
  113. DEBUG_LOG(">>>>>>>>1111111 mFlash.mStep.stepCur[0]:%d, %d\n",mFlash.mStep.stepCur[0],mFlash.mStep.step_num);
  114. DEBUG_LOG("Flash head read(%04X).\n",mFlash.head);
  115. if(mFlash.head !=FLASH_HEAD){ DEBUG_LOG("Flash first init(%04X),write infomation to flash.\n",mFlash.head);
  116. memset((uint8_t*)(&mFlash),0,sizeof(Flash_t));
  117. mFlash.head = FLASH_HEAD;
  118. for(i =0;i<MAX_mflash_param_LENGTH;i++){
  119. 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);
  120. }
  121. if(MINIFDS_SUCCESS == pm_get_length(&information,All_Falsh_value[0].name,strlen(All_Falsh_value[0].name),&length)){
  122. if(length == All_Falsh_value[0].length)
  123. {
  124. pm_find(&information,All_Falsh_value[0].name,strlen(All_Falsh_value[0].name),(uint8_t *)All_Falsh_value[0].value,&length);
  125. }
  126. }
  127. //
  128. DEBUG_LOG("Flash head second read(%04X).\n",mFlash.head);
  129. if(mFlash.head != FLASH_HEAD){
  130. DEBUG_LOG("Flash write head fail.\n");
  131. Except_TxError(EXCEPT_FLASH,"Flash write head fail");
  132. return;
  133. }
  134. DEBUG_LOG("System reset...\n",mFlash.head);
  135. for(uint8_t i =0;i<6;i++){
  136. nrf_delay_ms(500);
  137. feed_watchdog();
  138. }
  139. NVIC_SystemReset();
  140. }
  141. DEBUG_LOG("err code :%s\n",mFlash.mFlashLog.logData);
  142. DEBUG_LOG("Flash init ok.\n");
  143. mFlash.isHost = _IS_HOST;
  144. #ifdef PIN_SEL
  145. nrf_gpio_cfg_input(PIN_SEL,NRF_GPIO_PIN_PULLUP);
  146. nrf_delay_ms(100);
  147. mFlash.isHost = (uint8_t)nrf_gpio_pin_read(PIN_SEL);
  148. if(1 == mFlash.isHost)nrf_gpio_cfg_input(PIN_SEL,NRF_GPIO_PIN_PULLDOWN);
  149. else nrf_gpio_cfg_input(PIN_SEL,NRF_GPIO_PIN_NOPULL);
  150. #endif
  151. if(mFlash.isHost){
  152. DEBUG_LOG("======= Left shooe ======= \n");
  153. }else{
  154. DEBUG_LOG("======= Right shooe ======= \n");
  155. }
  156. //TestHalFlashInterface();
  157. }
  158. /**
  159. @brief 设置清空flash的标志
  160. @param 无
  161. @return 无
  162. */
  163. static uint8_t FlashStep_ClearFlag =0;
  164. void flash_SetClearStepFlag(void){
  165. FlashStep_ClearFlag =1;
  166. }
  167. uint32_t flash_GetStep_StartAddress(void){
  168. return FLASH_START_ADDR_STEP;
  169. }
  170. /**
  171. @brief 存储步数
  172. @param 无
  173. @return 错误代码
  174. */
  175. uint32_t Flash_SaveStep(void)
  176. {
  177. flash_OPER_Result err_code;
  178. uint32_t flash_data;
  179. #define Max_Hour PAGE_INT_SIZE * FLASH_ADDR_STEP_PAGENUM
  180. DEBUG_LOG("==============\n");
  181. if(mFlash.mStep.step_num < Max_Hour)
  182. {
  183. // mFlash.mStep.stepCur[0] +=1 ;
  184. uint32_t step = app_step_GetStep_L() + app_step_GetStep_R(); //获取左右鞋步数
  185. flash_data = ((step<<24 & 0xff000000) | (step<<8 & 0x00ff0000) | (step>>8 & 0x0000ff00) | (step>>24 & 0x000000ff));
  186. //清空步数的flash step内容
  187. if(1 == FlashStep_ClearFlag){
  188. if(Flash_DeleteAllStep() != ZONE_OP_SUCCESS){
  189. Except_TxError(EXCEPT_DATEStep,"clear step fail");
  190. return ZONE_ERROR_WRITE_FAIL;
  191. }
  192. mFlash.mStep.step_num = 0;
  193. FlashStep_ClearFlag = 0;
  194. }
  195. DEBUG_LOG("step %d,flash_data:%d,step_num:%d\n",step, flash_data,mFlash.mStep.step_num);
  196. err_code = sdflash_write((uint32_t*)(FLASH_START_ADDR_STEP + (mFlash.mStep.step_num * 4)), &flash_data, 1);
  197. if(err_code != FLASH_OP_SUCCESS)return ZONE_ERROR_WRITE_FAIL;
  198. mFlash.mStep.step_num++;
  199. mFlash.mStep.step[0] = mFlash.mStep.stepCur[0];
  200. mFlash.mStep.step[1] = mFlash.mStep.stepCur[1];
  201. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS){
  202. Except_TxError(EXCEPT_FLASH,"save information fail");
  203. }
  204. uint8_t i=4;
  205. uint8_t length =0;
  206. for(;i<5;i++){
  207. if(MINIFDS_SUCCESS == pm_get_length(&information,All_Falsh_value[i].name,strlen(All_Falsh_value[i].name),&length)){
  208. if(length == All_Falsh_value[i].length){
  209. 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))
  210. {
  211. DEBUG_LOG(">>>>>>>>%s value:%d\n",All_Falsh_value[i].name,mFlash.mStep.stepCur[0]);
  212. }
  213. }
  214. }
  215. }
  216. }
  217. else return ZONE_ERROR_WRITE_FAIL;
  218. return ZONE_OP_SUCCESS;
  219. }
  220. /**
  221. @brief 删除所有步数
  222. @param 无
  223. @return 错误代码
  224. */
  225. uint32_t Flash_DeleteAllStep(void)
  226. {
  227. uint32_t errcode = ZONE_OP_SUCCESS;
  228. uint32_t pag_addr = 0;
  229. for(uint8_t i=0;i<FLASH_ADDR_STEP_PAGENUM;i++){
  230. pag_addr = (FLASH_START_ADDR_STEP +i*FLASH_PAGE_SIZE);
  231. errcode = sdflash_page_erase(pag_addr);
  232. if(errcode != NRF_SUCCESS)
  233. return ZONE_ERROR_ERASE_FAIL;
  234. }
  235. return ZONE_OP_SUCCESS;
  236. }
  237. /**
  238. @brief 读取一个区域中指定位置的数据
  239. @param destination_addr[in] - 读取区域的地址
  240. @param pData[in] - 指向存储的buff
  241. @param dataLen[in] - 读取的长度,单位字节
  242. @return 错误代码
  243. */
  244. uint32_t flash_Data_Read(uint32_t addr, uint32_t *pData, uint32_t dataLen){
  245. if(pData != NULL && dataLen != 0){
  246. if(addr < START_FSTORAGE_ADDR || addr >= END_FSTORAGE_ADDR){
  247. return ZONE_ERROR_ADDRESS_FAIL;
  248. }
  249. else
  250. memcpy(pData, (uint32_t*)addr, dataLen);
  251. }
  252. return ZONE_OP_SUCCESS;
  253. }
  254. /**
  255. @brief 存储基本信息
  256. @param 无
  257. @return 错误代码
  258. */
  259. uint32_t Flash_SaveInfomation(void)
  260. {
  261. char result =0;
  262. for(uint8_t i =0;i<MAX_mflash_param_LENGTH;i++){
  263. 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);
  264. if(result != MINIFDS_SUCCESS){
  265. return ZONE_ERROR_WRITE_FAIL;
  266. }
  267. }
  268. return ZONE_OP_SUCCESS;
  269. }
  270. /**
  271. @brief 保存日志信息
  272. @param[in] id Fault identifier. See @ref NRF_FAULT_IDS.
  273. @param[in] pc The program counter of the instruction that triggered the fault, or 0 if
  274. unavailable.
  275. @param[in] info Optional additional information regarding the fault. The value of the @p id
  276. parameter dictates how to interpret this parameter. Refer to the documentation
  277. for each fault identifier (@ref NRF_FAULT_IDS and @ref APP_ERROR_FAULT_IDS) for
  278. details about interpreting @p info.
  279. @return 错误代码
  280. */
  281. uint32_t Flash_SaveLog(uint32_t id, uint32_t pc, uint32_t info)
  282. {
  283. memset((uint8_t*)(&mFlash.mFlashLog),0,sizeof(FlashLog));
  284. mFlash.mFlashLog.Errorflag =1;
  285. switch (id)
  286. {
  287. #if defined(SOFTDEVICE_PRESENT) && SOFTDEVICE_PRESENT
  288. case NRF_FAULT_ID_SD_ASSERT:
  289. memcpy(mFlash.mFlashLog.logData,"SD: ASSERTION FAILED\r\n",MaxLength(sizeof("SD: ASSERTION FAILED\r\n"),sizeof(mFlash.mFlashLog.logData)));
  290. break;
  291. case NRF_FAULT_ID_APP_MEMACC:
  292. memcpy(mFlash.mFlashLog.logData,"SD: INVALID MEMORY ACCESS\r\n",MaxLength(sizeof("SD: INVALID MEMORY ACCESS\r\n"),sizeof(mFlash.mFlashLog.logData)));
  293. break;
  294. #endif
  295. case NRF_FAULT_ID_SDK_ASSERT:
  296. {
  297. assert_info_t * p_info = (assert_info_t *)info;
  298. sprintf((char *)mFlash.mFlashLog.logData,"ASSERTION FAILED %s:%u\r\n",
  299. p_info->p_file_name,
  300. p_info->line_num);
  301. break;
  302. }
  303. case NRF_FAULT_ID_SDK_ERROR:
  304. {
  305. error_info_t * p_info = (error_info_t *)info;
  306. sprintf((char *)mFlash.mFlashLog.logData,"error:%u,%s:%u\r\n",
  307. p_info->err_code,
  308. p_info->p_file_name,
  309. p_info->line_num);
  310. DEBUG_LOG(">>>>>err code :%d,%s",p_info->err_code,mFlash.mFlashLog.logData);
  311. break;
  312. }
  313. default:
  314. sprintf((char *)mFlash.mFlashLog.logData,"UNKNOWN FAULT 0x%08X\n", pc);
  315. break;
  316. }
  317. return Flash_SaveInfomation();
  318. }
  319. /**
  320. @brief 返回主机标志位
  321. @param 无
  322. @return 主机标志位
  323. */
  324. uint8_t Get_isHost(void)
  325. {
  326. return mFlash.isHost;
  327. }
  328. /**
  329. @brief 测试halflash接口
  330. @param 无
  331. @return 无
  332. */
  333. void TestHalFlashInterface(void)
  334. {
  335. // uint2_t i;
  336. // Flash_t m_testflash;
  337. // FlashBackup_t m_testbackup;
  338. // //测试基本信息和备份信息的写入和读取
  339. // for(i=0;i<10000;i++)
  340. // {
  341. // mFlash.mStep.step_num = i;
  342. // DEBUG_LOG("Flash_SaveInfomation[%d]:%d \n",i,Flash_SaveInfomation());
  343. //
  344. // DEBUG_LOG("Flash_SaveBackup[%d]:%d \n",i,Flash_SaveBackup());
  345. // }
  346. //
  347. // DEBUG_LOG("Flash_GetInfomation[%d]:%d \n",i,Flash_GetInfomation(&m_testflash));
  348. // DEBUG_LOG("m_testflash:%d \n",m_testflash.mStep.num);
  349. //
  350. // DEBUG_LOG("Flash_GetBackup[%d]:%d \n",i,Flash_GetBackup(&m_testbackup));
  351. // DEBUG_LOG("m_testbackup:%d \n",m_testbackup.hardVersion);
  352. }