hal_flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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.mStep.stepCur[0]<mFlash.mStep.step[0])
  57. mFlash.mStep.step[0] = mFlash.mStep.stepCur[0];
  58. if(mFlash.basePress==0) mFlash.basePress = 40000;
  59. SEGGER_RTT_printf(0,"mFlash.mStep.num=%d\n",mFlash.mStep.num);
  60. SEGGER_RTT_printf(0,"mFlash.mStep.stepCur[0]=%d,mFlash.mStep.stepCur[1]=%d\n",mFlash.mStep.stepCur[0],mFlash.mStep.stepCur[1]);
  61. SEGGER_RTT_printf(0,"mFlash.basePress=%d\n",mFlash.basePress);
  62. SEGGER_RTT_printf(0,"err code :%s\n",mFlash.mFlashLog.logData);
  63. SEGGER_RTT_printf(0,"Flash init ok.\n");
  64. mFlash.isHost = _IS_HOST;
  65. #ifdef PIN_SEL
  66. nrf_gpio_cfg_input(PIN_SEL,NRF_GPIO_PIN_PULLUP);
  67. nrf_delay_ms(100);
  68. mFlash.isHost = (uint8_t)nrf_gpio_pin_read(PIN_SEL);
  69. nrf_gpio_cfg_input(PIN_SEL,NRF_GPIO_PIN_NOPULL);
  70. #endif
  71. if(mFlash.isHost){
  72. SEGGER_RTT_printf(0,"======= Left shooe ======= \n");
  73. }else{
  74. SEGGER_RTT_printf(0,"======= Right shooe ======= \n");
  75. }
  76. }
  77. /**
  78. @brief 存储步数
  79. @param 无
  80. @return 错误代码
  81. */
  82. uint32_t Flash_SaveStep(void)
  83. {
  84. uint32_t err_code;
  85. uint32_t flash_data;
  86. uint32_t addr;
  87. if(mFlash.mStep.num<1500)
  88. {
  89. //SEGGER_RTT_printf(0,"app_step_GetStep_L()=%d,app_step_GetStep_R()=%d\n",app_step_GetStep_L(),app_step_GetStep_R());
  90. uint16_t step = app_step_GetStep_L() + app_step_GetStep_R(); //获取左右鞋步数
  91. //获取地址
  92. if(mFlash.mStep.num%2 == 0)addr = FLASH_ADDR_STEP+mFlash.mStep.num*2;
  93. else addr = FLASH_ADDR_STEP+(mFlash.mStep.num-1)*2;
  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. //写回内容
  101. err_code = Only_Write_N_4Byte_flash(addr , &flash_data, sizeof(flash_data),false);
  102. if(err_code != FLASH_OP_SUCCESS)return err_code;
  103. mFlash.mStep.num++;
  104. mFlash.mStep.step[0] = mFlash.mStep.stepCur[0];
  105. mFlash.mStep.step[1] = mFlash.mStep.stepCur[1];
  106. }
  107. return FLASH_OP_SUCCESS;
  108. }
  109. /**
  110. @brief 删除所有步数
  111. @param 无
  112. @return 错误代码
  113. */
  114. uint32_t Flash_DeleteAllStep(void)
  115. {
  116. uint32_t err_code;
  117. uint16_t PageNum;
  118. //获取页号
  119. err_code = GetPageNum(FLASH_ADDR_STEP, &PageNum);
  120. if(err_code != FLASH_OP_SUCCESS)return err_code;
  121. //擦除一页
  122. return Erase_OnePage(PageNum);
  123. }
  124. /**
  125. @brief 存储基本信息
  126. @param 无
  127. @return 错误代码
  128. */
  129. uint32_t Flash_SaveInfomation(void)
  130. {
  131. uint32_t err_code;
  132. uint16_t PageNum;
  133. uint32_t write_addr,len;
  134. //获取页号
  135. err_code = GetPageNum(FLASH_ADDR_INFO, &PageNum);
  136. if(err_code != FLASH_OP_SUCCESS)return err_code;
  137. //查找空闲空间
  138. Find_FreeSpace_In_Page(PageNum, &write_addr, &len);
  139. //更新保存基本信息
  140. if(len < sizeof(mFlash))
  141. {
  142. Erase_OnePage(PageNum);//擦除一页
  143. write_addr = FLASH_ADDR_INFO;
  144. }
  145. return Only_Write_N_4Byte_flash(write_addr, (uint32_t*)&mFlash, sizeof(mFlash), false);//直接写
  146. }
  147. /**
  148. @brief 存储备份信息
  149. @param 无
  150. @return 错误代码
  151. */
  152. uint32_t Flash_SaveBackup(void)
  153. {
  154. uint32_t err_code;
  155. uint16_t PageNum;
  156. uint32_t write_addr,len;
  157. //获取页号
  158. err_code = GetPageNum(FLASH_ADDR_BACKUP, &PageNum);
  159. if(err_code != FLASH_OP_SUCCESS)return err_code;
  160. //查找空闲空间
  161. Find_FreeSpace_In_Page(PageNum, &write_addr, &len);
  162. //更新保存备份信息
  163. if(len < sizeof(mBackup))
  164. {
  165. Erase_OnePage(PageNum);//擦除一页
  166. write_addr = FLASH_ADDR_BACKUP;
  167. }
  168. return Only_Write_N_4Byte_flash(write_addr, (uint32_t*)&mBackup, sizeof(mBackup), false);//直接写
  169. }
  170. /**
  171. @brief 获取基本信息
  172. @param 无
  173. @return 错误代码
  174. */
  175. uint32_t Flash_GetInfomation(Flash_t *pflash)
  176. {
  177. uint32_t err_code;
  178. uint16_t PageNum;
  179. uint32_t read_addr,len;
  180. //获取页号
  181. err_code = GetPageNum(FLASH_ADDR_INFO, &PageNum);
  182. if(err_code != FLASH_OP_SUCCESS)return err_code;
  183. //查找空闲空间
  184. Find_FreeSpace_In_Page(PageNum, &read_addr, &len);
  185. //获取最新的基本信息的地址
  186. read_addr = (read_addr != FLASH_ADDR_INFO)?read_addr - sizeof(Flash_t):FLASH_ADDR_INFO;//read_addr = FLASH_ADDR_INFO的情况属于第一次烧写
  187. return Read_N_Byte_flash(read_addr , (uint32_t*)pflash, sizeof(Flash_t));
  188. }
  189. /**
  190. @brief 获取备份信息
  191. @param 无
  192. @return 错误代码
  193. */
  194. uint32_t Flash_GetBackup(FlashBackup_t *pbackup)
  195. {
  196. uint32_t err_code;
  197. uint16_t PageNum;
  198. uint32_t read_addr,len;
  199. //获取页号
  200. err_code = GetPageNum(FLASH_ADDR_BACKUP, &PageNum);
  201. if(err_code != FLASH_OP_SUCCESS)return err_code;
  202. //查找空闲空间
  203. Find_FreeSpace_In_Page(PageNum, &read_addr, &len);
  204. //获取最新的基本信息的地址
  205. read_addr = (read_addr != FLASH_ADDR_BACKUP)?read_addr - sizeof(FlashBackup_t):FLASH_ADDR_BACKUP;//read_addr = FLASH_ADDR_INFO的情况属于第一次烧写
  206. return Read_N_Byte_flash(read_addr , (uint32_t*)pbackup, sizeof(FlashBackup_t));
  207. }
  208. /**
  209. @brief 保存日志信息
  210. @param[in] id Fault identifier. See @ref NRF_FAULT_IDS.
  211. @param[in] pc The program counter of the instruction that triggered the fault, or 0 if
  212. unavailable.
  213. @param[in] info Optional additional information regarding the fault. The value of the @p id
  214. parameter dictates how to interpret this parameter. Refer to the documentation
  215. for each fault identifier (@ref NRF_FAULT_IDS and @ref APP_ERROR_FAULT_IDS) for
  216. details about interpreting @p info.
  217. @return 错误代码
  218. */
  219. uint32_t Flash_SaveLog(uint32_t id, uint32_t pc, uint32_t info)
  220. {
  221. memset((uint8_t*)(&mFlash.mFlashLog),0,sizeof(FlashLog));
  222. mFlash.mFlashLog.Errorflag =1;
  223. switch (id)
  224. {
  225. #if defined(SOFTDEVICE_PRESENT) && SOFTDEVICE_PRESENT
  226. case NRF_FAULT_ID_SD_ASSERT:
  227. memcpy(mFlash.mFlashLog.logData,"SD: ASSERTION FAILED",MaxLength(sizeof("SD: ASSERTION FAILED"),sizeof(mFlash.mFlashLog.logData)));
  228. break;
  229. case NRF_FAULT_ID_APP_MEMACC:
  230. memcpy(mFlash.mFlashLog.logData,"SD: INVALID MEMORY ACCESS",MaxLength(sizeof("SD: INVALID MEMORY ACCESS"),sizeof(mFlash.mFlashLog.logData)));
  231. break;
  232. #endif
  233. case NRF_FAULT_ID_SDK_ASSERT:
  234. {
  235. assert_info_t * p_info = (assert_info_t *)info;
  236. sprintf((char *)mFlash.mFlashLog.logData,"ASSERTION FAILED %s:%u",
  237. p_info->p_file_name,
  238. p_info->line_num);
  239. break;
  240. }
  241. case NRF_FAULT_ID_SDK_ERROR:
  242. {
  243. error_info_t * p_info = (error_info_t *)info;
  244. sprintf((char *)mFlash.mFlashLog.logData,"error:%u,%s:%u",
  245. p_info->err_code,
  246. p_info->p_file_name,
  247. p_info->line_num);
  248. SEGGER_RTT_printf(0,">>>>>err code :%d,%s",p_info->err_code,mFlash.mFlashLog.logData);
  249. break;
  250. }
  251. default:
  252. sprintf((char *)mFlash.mFlashLog.logData,"UNKNOWN FAULT 0x%08X\n", pc);
  253. break;
  254. }
  255. return Flash_SaveInfomation();
  256. }
  257. /**
  258. @brief 返回主机标志位
  259. @param 无
  260. @return 主机标志位
  261. */
  262. uint8_t Get_isHost(void)
  263. {
  264. return mFlash.isHost;
  265. }
  266. /**
  267. @brief 测试halflash接口
  268. @param 无
  269. @return 无
  270. */
  271. void TestHalFlashInterface(void)
  272. {
  273. //uint32_t Flash_SaveStep(void);
  274. ////存储基本信息
  275. //uint32_t Flash_SaveInfomation(void);
  276. ////存储备份信息
  277. //uint32_t Flash_SaveBackup(void);
  278. ////获取基本信息
  279. //uint32_t Flash_GetInfomation(Flash_t *pflash);
  280. ////获取备份信息
  281. //uint32_t Flash_GetBackup(FlashBackup_t *pbackup);
  282. uint32_t i;
  283. Flash_t m_testflash;
  284. FlashBackup_t m_testbackup;
  285. Fstorage_FlashInit();
  286. //测试基本信息和备份信息的写入和读取
  287. for(i=0;i<10000;i++)
  288. {
  289. mFlash.mStep.num = i;
  290. SEGGER_RTT_printf(0,"Flash_SaveInfomation[%d]:%d \n",i,Flash_SaveInfomation());
  291. mBackup.hardVersion = i;
  292. SEGGER_RTT_printf(0,"Flash_SaveBackup[%d]:%d \n",i,Flash_SaveBackup());
  293. }
  294. SEGGER_RTT_printf(0,"Flash_GetInfomation[%d]:%d \n",i,Flash_GetInfomation(&m_testflash));
  295. SEGGER_RTT_printf(0,"m_testflash:%d \n",m_testflash.mStep.num);
  296. SEGGER_RTT_printf(0,"Flash_GetInfomation[%d]:%d \n",i,Flash_GetBackup(&m_testbackup));
  297. SEGGER_RTT_printf(0,"m_testbackup:%d \n",m_testbackup.hardVersion);
  298. //测试步数信息的写入和读取
  299. for(i=0;i<1500;i++)
  300. {
  301. mFlash.mStep.num = i;
  302. mFlash.mStep.stepCur[0] = i;
  303. mFlash.mStep.stepCur[1] = i+i;
  304. SEGGER_RTT_printf(0,"Flash_SaveStep[%d]:%d \n\n",i,Flash_SaveStep());
  305. }
  306. uint32_t checkflash[750];
  307. Read_N_Byte_flash(FLASH_ADDR_STEP , (uint32_t*)checkflash, sizeof(checkflash));
  308. for(i=0;i<750;i++)
  309. {
  310. SEGGER_RTT_printf(0,"checkflash[%d]:0x%x ",i,checkflash[i]);
  311. if(i%5 == 0)SEGGER_RTT_printf(0,"\n");
  312. nrf_delay_ms(5);
  313. }
  314. SEGGER_RTT_printf(0,"TestHalFlashInterface done !!!!!!!\n");
  315. }