hal_flash.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #include "hal_flash.h"
  2. //flash 1.0V
  3. /*********************************************************************
  4. * LOCAL VARIABLES
  5. */
  6. Flash_t mFlash;
  7. FlashBackup_t mBackup;
  8. /*********************************************************************
  9. * PUBLIC FUNCTIONS
  10. */
  11. /****************************************************接口****************************************************/
  12. void Flash_Initialize(void)
  13. {
  14. uint8_t ret = 0;
  15. uint32_t* addr_R = (uint32_t*)START_FSTORAGE_ADDR;
  16. Fstorage_FlashInit();
  17. SEGGER_RTT_printf(0,"Flash head read(%04X).\n",*addr_R);
  18. if((*addr_R)!=FLASH_FLAG){ SEGGER_RTT_printf(0,"Flash first init(%04X),write infomation to flash.\n",*addr_R);
  19. memset((uint8_t*)(&mFlash),0,sizeof(Flash_t));
  20. mFlash.flag = FLASH_FLAG;
  21. //重置之后将备份区域的地址拷贝
  22. // Read_N_Byte_flash(FLASH_ADDR_BACKUP,(uint32_t*)(&mBackup),sizeof(FlashBackup_t));
  23. Flash_GetBackup(&mBackup);
  24. for(int i=0;i<6;i++){
  25. mFlash.mClient.macAddr[i] = mBackup.macAddr_R[i];
  26. mFlash.macHost[i] = mBackup.macAddr_L[i];
  27. }
  28. mFlash.mClient.isConfig = mBackup.isConfig;
  29. mFlash.mClient.hardVersion = mBackup.hardVersion;
  30. mFlash.mClient.sotfVersion = mBackup.sotfVersion;
  31. SEGGER_RTT_printf(0,"backup config:%d,hardVersion:%d,sotfVersion:%d\n",mBackup.isConfig,mBackup.hardVersion,mBackup.sotfVersion);
  32. Flash_SaveInfomation();
  33. SEGGER_RTT_printf(0,"Flash head second read(%04X).\n",*addr_R);
  34. if((*addr_R)!=FLASH_FLAG){
  35. SEGGER_RTT_printf(0,"Flash write head fail.\n");
  36. app_err_Set(ERR_NUM_FLASH,1);
  37. ret = 1;
  38. // nrf_delay_ms(30000);
  39. // NVIC_SystemReset();
  40. return;
  41. }
  42. SEGGER_RTT_printf(0,"System reset...\n",*addr_R);
  43. nrf_delay_ms(3000);
  44. NVIC_SystemReset();
  45. // for(uint32_t i=0;i<FLASH_STEP_LEN;i++){ mFlash.mStep.stepPerHour[i] = i; } //测试
  46. // Flash_WriteHalfWordBuff(FLASH_ADDR_STEP,&mFlash.mStep.stepPerHour[0],FLASH_STEP_LEN);
  47. // while (nrf_fstorage_is_busy(&fstorage));
  48. // Flash_ReadHalfWordBuff(FLASH_ADDR_STEP,mFlash.mStep.stepPerHour,FLASH_STEP_LEN);
  49. }
  50. if(ret==0){
  51. // Read_N_Byte_flash(FLASH_ADDR_INFO,(uint32_t*)(&mFlash),sizeof(Flash_t));
  52. // Read_N_Byte_flash(FLASH_ADDR_BACKUP,(uint32_t*)(&mBackup),sizeof(FlashBackup_t));
  53. Flash_GetInfomation(&mFlash);
  54. Flash_GetBackup(&mBackup);
  55. }
  56. if(mFlash.basePress==0) mFlash.basePress = 200;
  57. SEGGER_RTT_printf(0,"mFlash.mStep.num=%d\n",mFlash.mStep.num);
  58. SEGGER_RTT_printf(0,"mFlash.mStep.stepCur[0]=%d,mFlash.mStep.stepCur[1]=%d\n",mFlash.mStep.stepCur[0],mFlash.mStep.stepCur[1]);
  59. SEGGER_RTT_printf(0,"mFlash.basePress=%d\n",mFlash.basePress);
  60. SEGGER_RTT_printf(0,"err code :%s\n",mFlash.mFlashLog.logData);
  61. SEGGER_RTT_printf(0,"Flash init ok.\n");
  62. mFlash.isHost = _IS_HOST;
  63. #ifdef PIN_SEL
  64. nrf_gpio_cfg_input(PIN_SEL,NRF_GPIO_PIN_PULLUP);
  65. nrf_delay_ms(100);
  66. mFlash.isHost = (uint8_t)nrf_gpio_pin_read(PIN_SEL);
  67. nrf_gpio_cfg_input(PIN_SEL,NRF_GPIO_PIN_NOPULL);
  68. #endif
  69. if(mFlash.isHost){
  70. SEGGER_RTT_printf(0,"======= Left shooe ======= \n");
  71. }else{
  72. SEGGER_RTT_printf(0,"======= Right shooe ======= \n");
  73. }
  74. TestHalFlashInterface();
  75. }
  76. /**
  77. @brief 存储步数
  78. @param 无
  79. @return 错误代码
  80. */
  81. uint32_t Flash_SaveStep(void)
  82. {
  83. uint32_t err_code;
  84. uint32_t flash_data;
  85. uint32_t addr;
  86. if(mFlash.mStep.num<1500)
  87. {
  88. //SEGGER_RTT_printf(0,"app_step_GetStep_L()=%d,app_step_GetStep_R()=%d\n",app_step_GetStep_L(),app_step_GetStep_R());
  89. uint32_t step = app_step_GetStep_L() + app_step_GetStep_R(); //获取左右鞋步数
  90. //获取地址
  91. //if(mFlash.mStep.num%2 == 0)addr = FLASH_ADDR_STEP+mFlash.mStep.num*2;
  92. //else addr = FLASH_ADDR_STEP+(mFlash.mStep.num-1)*2;
  93. addr = FLASH_ADDR_STEP+mFlash.mStep.num*4;
  94. //读取内容
  95. err_code = Read_N_Byte_flash(addr , &flash_data, sizeof(flash_data));
  96. if(err_code != FLASH_OP_SUCCESS)return err_code;
  97. //修改内容
  98. //if(mFlash.mStep.num%2 == 0)flash_data &= (0xFFFF0000 | (step>>8 | step<<8));
  99. //else flash_data &= ((step>>8 | step<<8)<<16 | 0x0000FFFF);
  100. flash_data = ((step<<24 & 0xff000000) | (step<<8 & 0x00ff0000) | (step>>8 & 0x0000ff00) | (step>>24 & 0x000000ff));
  101. //写回内容
  102. err_code = Only_Write_N_4Byte_flash(addr , &flash_data, sizeof(flash_data),false);
  103. if(err_code != FLASH_OP_SUCCESS)return err_code;
  104. mFlash.mStep.num++;
  105. }
  106. return FLASH_OP_SUCCESS;
  107. }
  108. /**
  109. @brief 删除所有步数
  110. @param 无
  111. @return 错误代码
  112. */
  113. uint32_t Flash_DeleteAllStep(void)
  114. {
  115. uint32_t err_code;
  116. uint16_t PageNum;
  117. //获取页号
  118. err_code = GetPageNum(FLASH_ADDR_STEP, &PageNum);
  119. if(err_code != FLASH_OP_SUCCESS)return err_code;
  120. //擦除一页
  121. return Erase_OnePage(PageNum);
  122. }
  123. /**
  124. @brief 存储基本信息
  125. @param 无
  126. @return 错误代码
  127. */
  128. uint32_t Flash_SaveInfomation(void)
  129. {
  130. uint32_t err_code;
  131. uint16_t PageNum;
  132. uint32_t write_addr,len;
  133. //获取页号
  134. err_code = GetPageNum(FLASH_ADDR_INFO, &PageNum);
  135. if(err_code != FLASH_OP_SUCCESS)return err_code;
  136. //查找空闲空间
  137. Find_FreeSpace_In_Page(PageNum, &write_addr, &len);
  138. //更新保存基本信息
  139. if(len < sizeof(mFlash))
  140. {
  141. Erase_OnePage(PageNum);//擦除一页
  142. write_addr = FLASH_ADDR_INFO;
  143. }
  144. return Only_Write_N_4Byte_flash(write_addr, (uint32_t*)&mFlash, sizeof(mFlash), false);//直接写
  145. }
  146. /**
  147. @brief 存储备份信息
  148. @param 无
  149. @return 错误代码
  150. */
  151. uint32_t Flash_SaveBackup(void)
  152. {
  153. uint32_t err_code;
  154. uint16_t PageNum;
  155. uint32_t write_addr,len;
  156. //获取页号
  157. err_code = GetPageNum(FLASH_ADDR_BACKUP, &PageNum);
  158. if(err_code != FLASH_OP_SUCCESS)return err_code;
  159. //查找空闲空间
  160. Find_FreeSpace_In_Page(PageNum, &write_addr, &len);
  161. //更新保存备份信息
  162. if(len < sizeof(mBackup))
  163. {
  164. Erase_OnePage(PageNum);//擦除一页
  165. write_addr = FLASH_ADDR_BACKUP;
  166. }
  167. return Only_Write_N_4Byte_flash(write_addr, (uint32_t*)&mBackup, sizeof(mBackup), false);//直接写
  168. }
  169. /**
  170. @brief 获取基本信息
  171. @param 无
  172. @return 错误代码
  173. */
  174. uint32_t Flash_GetInfomation(Flash_t *pflash)
  175. {
  176. uint32_t err_code;
  177. uint16_t PageNum;
  178. uint32_t read_addr,len;
  179. //获取页号
  180. err_code = GetPageNum(FLASH_ADDR_INFO, &PageNum);
  181. if(err_code != FLASH_OP_SUCCESS)return err_code;
  182. //查找空闲空间
  183. Find_FreeSpace_In_Page(PageNum, &read_addr, &len);
  184. //获取最新的基本信息的地址
  185. read_addr = (read_addr != FLASH_ADDR_INFO)?read_addr - sizeof(Flash_t):FLASH_ADDR_INFO;//read_addr = FLASH_ADDR_INFO的情况属于第一次烧写
  186. return Read_N_Byte_flash(read_addr , (uint32_t*)pflash, sizeof(Flash_t));
  187. }
  188. /**
  189. @brief 获取备份信息
  190. @param 无
  191. @return 错误代码
  192. */
  193. uint32_t Flash_GetBackup(FlashBackup_t *pbackup)
  194. {
  195. uint32_t err_code;
  196. uint16_t PageNum;
  197. uint32_t read_addr,len;
  198. //获取页号
  199. err_code = GetPageNum(FLASH_ADDR_BACKUP, &PageNum);
  200. if(err_code != FLASH_OP_SUCCESS)return err_code;
  201. //查找空闲空间
  202. Find_FreeSpace_In_Page(PageNum, &read_addr, &len);
  203. //获取最新的基本信息的地址
  204. read_addr = (read_addr != FLASH_ADDR_BACKUP)?read_addr - sizeof(FlashBackup_t):FLASH_ADDR_BACKUP;//read_addr = FLASH_ADDR_INFO的情况属于第一次烧写
  205. return Read_N_Byte_flash(read_addr , (uint32_t*)pbackup, sizeof(FlashBackup_t));
  206. }
  207. /**
  208. @brief 保存日志信息
  209. @param[in] id Fault identifier. See @ref NRF_FAULT_IDS.
  210. @param[in] pc The program counter of the instruction that triggered the fault, or 0 if
  211. unavailable.
  212. @param[in] info Optional additional information regarding the fault. The value of the @p id
  213. parameter dictates how to interpret this parameter. Refer to the documentation
  214. for each fault identifier (@ref NRF_FAULT_IDS and @ref APP_ERROR_FAULT_IDS) for
  215. details about interpreting @p info.
  216. @return 错误代码
  217. */
  218. uint32_t Flash_SaveLog(uint32_t id, uint32_t pc, uint32_t info)
  219. {
  220. memset((uint8_t*)(&mFlash.mFlashLog),0,sizeof(FlashLog));
  221. mFlash.mFlashLog.Errorflag =1;
  222. switch (id)
  223. {
  224. #if defined(SOFTDEVICE_PRESENT) && SOFTDEVICE_PRESENT
  225. case NRF_FAULT_ID_SD_ASSERT:
  226. memcpy(mFlash.mFlashLog.logData,"SD: ASSERTION FAILED",MaxLength(sizeof("SD: ASSERTION FAILED"),sizeof(mFlash.mFlashLog.logData)));
  227. break;
  228. case NRF_FAULT_ID_APP_MEMACC:
  229. memcpy(mFlash.mFlashLog.logData,"SD: INVALID MEMORY ACCESS",MaxLength(sizeof("SD: INVALID MEMORY ACCESS"),sizeof(mFlash.mFlashLog.logData)));
  230. break;
  231. #endif
  232. case NRF_FAULT_ID_SDK_ASSERT:
  233. {
  234. assert_info_t * p_info = (assert_info_t *)info;
  235. sprintf((char *)mFlash.mFlashLog.logData,"ASSERTION FAILED %s:%u",
  236. p_info->p_file_name,
  237. p_info->line_num);
  238. break;
  239. }
  240. case NRF_FAULT_ID_SDK_ERROR:
  241. {
  242. error_info_t * p_info = (error_info_t *)info;
  243. sprintf((char *)mFlash.mFlashLog.logData,"error:%u,%s:%u",
  244. p_info->err_code,
  245. p_info->p_file_name,
  246. p_info->line_num);
  247. SEGGER_RTT_printf(0,">>>>>err code :%d,%s",p_info->err_code,mFlash.mFlashLog.logData);
  248. break;
  249. }
  250. default:
  251. sprintf((char *)mFlash.mFlashLog.logData,"UNKNOWN FAULT 0x%08X\n", pc);
  252. break;
  253. }
  254. return Flash_SaveInfomation();
  255. }
  256. /**
  257. @brief 返回主机标志位
  258. @param 无
  259. @return 主机标志位
  260. */
  261. uint8_t Get_isHost(void)
  262. {
  263. return mFlash.isHost;
  264. }
  265. /**
  266. @brief 测试halflash接口
  267. @param 无
  268. @return 无
  269. */
  270. void TestHalFlashInterface(void)
  271. {
  272. //uint32_t Flash_SaveStep(void);
  273. ////存储基本信息
  274. //uint32_t Flash_SaveInfomation(void);
  275. ////存储备份信息
  276. //uint32_t Flash_SaveBackup(void);
  277. ////获取基本信息
  278. //uint32_t Flash_GetInfomation(Flash_t *pflash);
  279. ////获取备份信息
  280. //uint32_t Flash_GetBackup(FlashBackup_t *pbackup);
  281. // uint32_t i;
  282. // Flash_t m_testflash;
  283. //
  284. //// Fstorage_FlashInit();
  285. //
  286. // //测试步数信息的写入和读取
  287. // for(i=0;i<1500;i++)
  288. // {
  289. // mFlash.mStep.num = i;
  290. // mFlash.mStep.stepCur[0] = 0;
  291. // mFlash.mStep.stepCur[1] = i;
  292. //
  293. // SEGGER_RTT_printf(0,"Flash_SaveStep[%d]:%d \n\n",i,Flash_SaveStep());
  294. // }
  295. // uint8_t checkflash[4] = {0};
  296. // for(i=0;i<1500;i++)
  297. // {
  298. // Read_N_Byte_flash(FLASH_ADDR_STEP+i*4, (uint32_t*)&checkflash[0], 4);
  299. // SEGGER_RTT_printf(0,"checkflash[%d]:%02x,%02x,%02x,%02x ",i,checkflash[0],checkflash[1],checkflash[2],checkflash[3]);
  300. // if(i%5 == 0)SEGGER_RTT_printf(0,"\n");
  301. // nrf_delay_ms(5);
  302. // }
  303. }