app_flash.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. #include "app_flash.h"
  2. #include "bsp_wdt.h"
  3. /*************************************
  4. *DEFINITION
  5. */
  6. #define FLASH_HEAD 0xAA5555AA
  7. #define FLASH_TAIL_H 0xA5 //高位字节不能为0,因为结构体强制4字节对齐缘故,会导致解析错误。
  8. #define FLASH_TAIL_L 0x55 //低字节随意,最好是0101组合
  9. #define FLASH_ADDR_INFO_PAGENUM 1
  10. #define FLASH_ADDR_STEP_PAGENUM 2
  11. #define FLASH_ADDR_BACKUP_PAGENUM 1
  12. #define MaxLength(a,b) a>b?a:b
  13. /*********************************************************************
  14. * LOCAL VARIABLES
  15. */
  16. static uint32_t info_zone;
  17. static uint32_t step_zone;
  18. static uint32_t backup_zone;
  19. Flash_t mFlash;
  20. FlashBackup_t mBackup;
  21. /*********************************************************************
  22. * LOCAL FUCTIONS
  23. */
  24. /**
  25. @brief crc16校验码
  26. @param crc - [in] 默认0
  27. @param buf - [in] 指向需要校验的数据
  28. @param len - [in] 校验数据的长度
  29. @return 返回crc16校验码
  30. */
  31. static int CalCrc(int crc, const char *buf, int len)
  32. {
  33. unsigned int byte;
  34. unsigned char k;
  35. unsigned short ACC,TOPBIT;
  36. unsigned short remainder = crc;
  37. TOPBIT = 0x8000;
  38. for (byte = 0; byte < len; ++byte)
  39. {
  40. ACC = buf[byte];
  41. remainder ^= (ACC <<8);
  42. for (k = 8; k > 0; --k)
  43. {
  44. if (remainder & TOPBIT)
  45. {
  46. remainder = (remainder << 1) ^0x8005;
  47. }
  48. else
  49. {
  50. remainder = (remainder << 1);
  51. }
  52. }
  53. }
  54. remainder=remainder^0x0000;
  55. return remainder;
  56. }
  57. /****************************************************接口****************************************************/
  58. void Flash_Initialize(void)
  59. {
  60. uint8_t ret = 0;
  61. uint32_t* addr_R;
  62. Fstorage_FlashInit();
  63. SEGGER_RTT_printf(0,"Zone_Alloca(FLASH_ADDR_INFO_PAGENUM, &info_zone):%d\n",Zone_Alloca(FLASH_ADDR_INFO_PAGENUM, &info_zone));
  64. SEGGER_RTT_printf(0,"Zone_Alloca(FLASH_ADDR_STEP_PAGENUM, &step_zone):%d\n",Zone_Alloca(FLASH_ADDR_STEP_PAGENUM, &step_zone));
  65. SEGGER_RTT_printf(0,"Zone_Alloca(FLASH_ADDR_BACKUP_PAGENUM, &backup_zone):%d\n",Zone_Alloca(FLASH_ADDR_BACKUP_PAGENUM, &backup_zone));
  66. addr_R = (uint32_t*)info_zone;
  67. SEGGER_RTT_printf(0,"Flash head read(%04X).\n",*addr_R);
  68. if((*addr_R)!=FLASH_HEAD){ SEGGER_RTT_printf(0,"Flash first init(%04X),write infomation to flash.\n",*addr_R);
  69. memset((uint8_t*)(&mFlash),0,sizeof(Flash_t));
  70. mFlash.head = FLASH_HEAD;
  71. mFlash.m_struct_size = sizeof(mFlash);
  72. Flash_SaveInfomation();
  73. SEGGER_RTT_printf(0,"Flash head second read(%04X).\n",*addr_R);
  74. if((*addr_R)!=FLASH_HEAD){
  75. SEGGER_RTT_printf(0,"Flash write head fail.\n");
  76. app_err_Set(ERR_NUM_FLASH,1);
  77. ret = 1;
  78. return;
  79. }
  80. SEGGER_RTT_printf(0,"System reset...\n",*addr_R);
  81. #if WATCHDOG_ENANBLE
  82. for(uint8_t i =0;i<6;i++){
  83. nrf_delay_ms(500);
  84. feed_watchdog();
  85. }
  86. #else
  87. nrf_delay_ms(3000);
  88. #endif
  89. NVIC_SystemReset();
  90. }
  91. if(ret==0){
  92. SEGGER_RTT_printf(0,"Flash_GetInfomation(&mFlash)\n");
  93. SEGGER_RTT_printf(0,"Flash_GetInfomation(&mFlash):%d \n",Flash_GetInfomation(&mFlash));
  94. SEGGER_RTT_printf(0,"Flash_GetInfomation(&mBackup):%d \n",Flash_GetBackup(&mBackup));
  95. // SEGGER_RTT_printf(0,">>>>>>>>>add:%02x,add1:%02x,add2:%02x,add3:%02x,add4:%02x,add5:%02x\r\n",mFlash.add,mFlash.add2,mFlash.add3,mFlash.add4,mFlash.add5);
  96. // mFlash.add = 0x11;
  97. // mFlash.add2 = 0x22;
  98. // mFlash.add3 = 0x33;
  99. // mFlash.add4 = 0x44;
  100. // SEGGER_RTT_printf(0,"Flash_SaveInfomation():%d\n",Flash_SaveInfomation());
  101. }
  102. if(mFlash.mStep.stepCur[0]<mFlash.mStep.step[0])
  103. mFlash.mStep.step[0] = mFlash.mStep.stepCur[0];
  104. SEGGER_RTT_printf(0,"mFlash.mStep.num=%d\n",mFlash.mStep.num);
  105. SEGGER_RTT_printf(0,"mFlash.mStep.stepCur[0]=%d,mFlash.mStep.stepCur[1]=%d\n",mFlash.mStep.stepCur[0],mFlash.mStep.stepCur[1]);
  106. SEGGER_RTT_printf(0,"err code :%s\n",mFlash.mFlashLog.logData);
  107. SEGGER_RTT_printf(0,"Flash init ok.\n");
  108. mFlash.isHost = _IS_HOST;
  109. #ifdef PIN_SEL
  110. nrf_gpio_cfg_input(PIN_SEL,NRF_GPIO_PIN_PULLUP);
  111. nrf_delay_ms(100);
  112. mFlash.isHost = (uint8_t)nrf_gpio_pin_read(PIN_SEL);
  113. if(1 == mFlash.isHost)nrf_gpio_cfg_input(PIN_SEL,NRF_GPIO_PIN_PULLDOWN);
  114. else nrf_gpio_cfg_input(PIN_SEL,NRF_GPIO_PIN_NOPULL);
  115. #endif
  116. if(mFlash.isHost){
  117. SEGGER_RTT_printf(0,"======= Left shooe ======= \n");
  118. }else{
  119. SEGGER_RTT_printf(0,"======= Right shooe ======= \n");
  120. }
  121. //TestHalFlashInterface();
  122. }
  123. /**
  124. @brief 存储步数
  125. @param 无
  126. @return 错误代码
  127. */
  128. uint32_t Flash_SaveStep(void)
  129. {
  130. uint32_t err_code;
  131. uint32_t flash_data;
  132. static uint32_t Max_Hour = PAGE_INT_SIZE * FLASH_ADDR_STEP_PAGENUM;
  133. if(mFlash.mStep.num < Max_Hour)
  134. {
  135. uint32_t step = app_step_GetStep_L() + app_step_GetStep_R(); //获取左右鞋步数
  136. flash_data = ((step<<24 & 0xff000000) | (step<<8 & 0x00ff0000) | (step>>8 & 0x0000ff00) | (step>>24 & 0x000000ff));
  137. err_code = Zone_Write(step_zone + (mFlash.mStep.num * 4), &flash_data, 4);
  138. if(err_code != ZONE_OP_SUCCESS)return err_code;
  139. mFlash.mStep.num++;
  140. mFlash.mStep.step[0] = mFlash.mStep.stepCur[0];
  141. mFlash.mStep.step[1] = mFlash.mStep.stepCur[1];
  142. }
  143. else return ZONE_ERROR_WRITE_FAIL;
  144. return ZONE_OP_SUCCESS;
  145. }
  146. /**
  147. @brief 获取步数区域首地址
  148. @param 无
  149. @return 错误代码
  150. */
  151. uint32_t Flash_GetStepZoneStartAddr(void)
  152. {
  153. return step_zone;
  154. }
  155. /**
  156. @brief 获取步数区域数据
  157. @param 无
  158. @return 错误代码
  159. */
  160. uint32_t Flash_GetStep(uint32_t destination_addr, uint32_t *pData, uint32_t dataLen)
  161. {
  162. return Zone_Read(destination_addr,pData,dataLen);
  163. }
  164. /**
  165. @brief 删除所有步数
  166. @param 无
  167. @return 错误代码
  168. */
  169. uint32_t Flash_DeleteAllStep(void)
  170. {
  171. mFlash.mStep.num = 0;
  172. return Zone_Erase(step_zone);
  173. }
  174. /**
  175. @brief 存储基本信息
  176. @param 无
  177. @return 错误代码
  178. */
  179. uint32_t Flash_SaveInfomation(void)
  180. {
  181. uint32_t err_code;
  182. uint32_t zone_bytes;
  183. Flash_t temp_flash;
  184. int32_t offset;
  185. uint16_t crc;
  186. uint8_t tail_h;
  187. memset(&temp_flash,0,sizeof(temp_flash));
  188. err_code = Zone_GetByteSize(info_zone, &zone_bytes);
  189. if(err_code != ZONE_OP_SUCCESS)return err_code;
  190. //从区域尾开始找起,找到最新的帧尾
  191. for(offset = zone_bytes - 1; offset >= 0; offset--)
  192. {
  193. err_code = Zone_Read(info_zone + offset, (uint32_t*)&tail_h, 1);
  194. if(err_code != ZONE_OP_SUCCESS)return err_code;
  195. if(tail_h == FLASH_TAIL_H)break;
  196. }
  197. //重新获取crc16校验码
  198. mFlash.tail_crc16 = 0;
  199. mFlash.m_struct_size = sizeof(mFlash);
  200. mFlash.head = FLASH_HEAD;
  201. crc = CalCrc(0, (char*)&mFlash, sizeof(mFlash));//计算得到的16位CRC校验码
  202. mFlash.tail_crc16 = (uint32_t)((uint32_t)(FLASH_TAIL_H << 8 | FLASH_TAIL_L) << 16) | crc;
  203. //写入
  204. if(offset < 0){ //意味着整个区域是干净的,直接写开头。
  205. err_code = Zone_Write(info_zone, (uint32_t*)&mFlash, sizeof(mFlash));
  206. if(err_code != ZONE_OP_SUCCESS)return err_code;
  207. SEGGER_RTT_printf(0,"write zone all clear,mFlash.tail_crc16:0x%x\r\n",mFlash.tail_crc16);
  208. }else{
  209. //这里要偏移才能对齐成员
  210. offset++;
  211. //获取最新的帧尾
  212. err_code = Zone_Read(info_zone + offset - sizeof(temp_flash.tail_crc16), (uint32_t*)&temp_flash.tail_crc16, sizeof(temp_flash.tail_crc16));
  213. if(err_code != ZONE_OP_SUCCESS)return err_code;
  214. //获取最新的帧长度
  215. err_code = Zone_Read(info_zone + offset - sizeof(temp_flash.tail_crc16) - sizeof(temp_flash.m_struct_size) , (uint32_t*)&temp_flash.m_struct_size, sizeof(temp_flash.m_struct_size));
  216. if(err_code != ZONE_OP_SUCCESS)return err_code;
  217. //因为结构体强制4字节对齐缘故,所以tail_crc16未必是真实存储的帧尾,后面可能带一些数据为0的填充字节。所以要做校准。
  218. // uint32_t pad_len = temp_flash.m_struct_size - ((uint32_t)&temp_flash.tail_crc16 + sizeof(temp_flash.tail_crc16) - (uint32_t)&temp_flash.head);
  219. uint32_t pad_len = 0;
  220. SEGGER_RTT_printf(0,"save info_zone:0x%x offset:%d pad_len:%d temp_flash.m_struct_size:%d\n",info_zone,offset,pad_len,temp_flash.m_struct_size);
  221. if(offset + pad_len < temp_flash.m_struct_size)pad_len = temp_flash.m_struct_size - offset;
  222. err_code = Zone_Read(info_zone + offset + pad_len - temp_flash.m_struct_size, (uint32_t*)&temp_flash.head, sizeof(temp_flash.head));
  223. if(err_code != ZONE_OP_SUCCESS)return err_code;
  224. // SEGGER_RTT_printf(0,"save >>>>>>p_head:0x%x,addr:0x%x\n",temp_flash.head,info_zone + offset + pad_len - temp_flash.m_struct_size);
  225. while(temp_flash.head != FLASH_HEAD){
  226. pad_len++;
  227. err_code = Zone_Read(info_zone + offset + pad_len - temp_flash.m_struct_size, (uint32_t*)&temp_flash.head, sizeof(temp_flash.head));
  228. if(err_code != ZONE_OP_SUCCESS)return err_code;
  229. // SEGGER_RTT_printf(0,"save >>>>>>p_head:0x%x,addr:0x%x\n",temp_flash.head,info_zone + offset + pad_len - temp_flash.m_struct_size);
  230. // nrf_delay_ms(5);
  231. }
  232. // while(1)
  233. // {
  234. // SEGGER_RTT_printf(0,"save info_zone:0x%x offset:%d pad_len:%d temp_flash.m_struct_size:%d\n",info_zone,offset,pad_len,temp_flash.m_struct_size);
  235. // nrf_delay_ms(1000);
  236. // feed_watchdog();
  237. // }
  238. err_code = Zone_Write(info_zone + offset + pad_len, (uint32_t*)&mFlash, sizeof(mFlash));
  239. // if(err_code == ZONE_OP_SUCCESS)SEGGER_RTT_printf(0,"write zone forward ,mFlash.tail_crc16:0x%x\r\n",mFlash.tail_crc16);
  240. if(err_code != ZONE_OP_SUCCESS){ //如果写失败了
  241. //擦除该区域
  242. err_code = Zone_Erase(info_zone);
  243. if(err_code != ZONE_OP_SUCCESS)return err_code;
  244. //写在开头
  245. err_code = Zone_Write(info_zone, (uint32_t*)&mFlash, sizeof(mFlash));
  246. if(err_code != ZONE_OP_SUCCESS)return err_code;
  247. // SEGGER_RTT_printf(0,"write zone all erase ,mFlash.tail_crc16:0x%x\r\n",mFlash.tail_crc16);
  248. }
  249. }
  250. return ZONE_OP_SUCCESS;
  251. }
  252. /**
  253. @brief 获取基本信息
  254. @param 无
  255. @return 错误代码
  256. */
  257. uint32_t Flash_GetInfomation(Flash_t *pflash)
  258. {
  259. uint32_t err_code;
  260. uint32_t zone_bytes;
  261. Flash_t temp_flash;
  262. int32_t offset;
  263. uint16_t crc;
  264. uint8_t tail_h;
  265. memset(&temp_flash,0,sizeof(temp_flash));
  266. SEGGER_RTT_printf(0,"Zone_GetByteSize(info_zone, &zone_bytes):%d\n",zone_bytes);
  267. err_code = Zone_GetByteSize(info_zone, &zone_bytes);
  268. if(err_code != ZONE_OP_SUCCESS)return err_code;
  269. SEGGER_RTT_printf(0,"Zone_GetByteSize(info_zone, &zone_bytes):%d\n",zone_bytes);
  270. //从区域尾开始找起,找到最新的帧尾
  271. for(offset = zone_bytes - 1; offset >= 0; offset--)
  272. {
  273. err_code = Zone_Read(info_zone + offset, (uint32_t*)&tail_h, 1);
  274. if(err_code != ZONE_OP_SUCCESS)return err_code;
  275. if(tail_h == FLASH_TAIL_H)break;
  276. }
  277. //意味着整个区域是干净的
  278. if(offset < 0){
  279. memset(pflash,0,sizeof(Flash_t));
  280. pflash->head = FLASH_HEAD;
  281. pflash->m_struct_size = sizeof(Flash_t); //这里是包括填充字节的
  282. crc = CalCrc(0, (char*)pflash, sizeof(Flash_t));//计算得到的16位CRC校验码,这里注意,有可能会将填充的字节也拿来做运算
  283. pflash->tail_crc16 = ((uint32_t)(FLASH_TAIL_H << 8 | FLASH_TAIL_L) << 16) | crc; //这里最后才加,是因为不知道填充字节的个数
  284. SEGGER_RTT_printf(0,"read zone all clear,pflash->tail_crc16:0x%x\r\n",pflash->tail_crc16);
  285. return ZONE_OP_SUCCESS;
  286. }
  287. //这里要偏移才能对齐成员
  288. offset++;
  289. //获取最新的帧尾
  290. err_code = Zone_Read(info_zone + offset - sizeof(temp_flash.tail_crc16), (uint32_t*)&temp_flash.tail_crc16, sizeof(temp_flash.tail_crc16));
  291. if(err_code != ZONE_OP_SUCCESS)return err_code;
  292. //获取最新的帧长度
  293. err_code = Zone_Read(info_zone + offset - sizeof(temp_flash.tail_crc16) - sizeof(temp_flash.m_struct_size) , (uint32_t*)&temp_flash.m_struct_size, sizeof(temp_flash.m_struct_size));
  294. if(err_code != ZONE_OP_SUCCESS)return err_code;
  295. //因为结构体强制4字节对齐缘故,所以tail_crc16未必是真实存储的帧尾,后面可能带一些数据为0的填充字节。所以要做校准。
  296. // uint32_t pad_len = temp_flash.m_struct_size - ((uint32_t)&temp_flash.tail_crc16 + sizeof(temp_flash.tail_crc16) - (uint32_t)&temp_flash.head);
  297. uint32_t pad_len = 0;
  298. SEGGER_RTT_printf(0,"get info_zone:0x%x offset:%d pad_len:%d temp_flash.m_struct_size:%d\n",info_zone,offset,pad_len,temp_flash.m_struct_size);
  299. if(offset + pad_len < temp_flash.m_struct_size)pad_len = temp_flash.m_struct_size - offset;
  300. err_code = Zone_Read(info_zone + offset + pad_len - temp_flash.m_struct_size, (uint32_t*)&temp_flash.head, sizeof(temp_flash.head));
  301. if(err_code != ZONE_OP_SUCCESS)return err_code;
  302. // SEGGER_RTT_printf(0,">>>>>>p_head:0x%x,addr:0x%x\n",temp_flash.head,info_zone + offset + pad_len - temp_flash.m_struct_size);
  303. while(temp_flash.head != FLASH_HEAD){
  304. pad_len++;
  305. err_code = Zone_Read(info_zone + offset + pad_len - temp_flash.m_struct_size, (uint32_t*)&temp_flash.head, sizeof(temp_flash.head));
  306. if(err_code != ZONE_OP_SUCCESS)return err_code;
  307. // SEGGER_RTT_printf(0,">>>>>>p_head:0x%x,addr:0x%x\n",temp_flash.head,info_zone + offset + pad_len - temp_flash.m_struct_size);
  308. // nrf_delay_ms(5);
  309. }
  310. // SEGGER_RTT_printf(0,"read pad_len 0x%x info_zone + offset + pad_len - temp_flash.m_struct_size:0x%x\r\n",pad_len,info_zone + offset + pad_len - temp_flash.m_struct_size);
  311. //获取最新的帧
  312. err_code = Zone_Read(info_zone + offset + pad_len - temp_flash.m_struct_size , (uint32_t*)&temp_flash, temp_flash.m_struct_size - sizeof(temp_flash.tail_crc16)-sizeof(temp_flash.m_struct_size)-pad_len);
  313. if(err_code != ZONE_OP_SUCCESS)return err_code;
  314. //校验数据
  315. Flash_t crc_buf;//单纯做一个缓冲区
  316. memset(&crc_buf,0,sizeof(crc_buf));
  317. char *buf = (char*)&crc_buf;
  318. //获取成员head ~ m_struct_size
  319. // SEGGER_RTT_printf(0,"1:sizeof(crc_buf):%d temp_flash.m_struct_size - sizeof(temp_flash.tail_crc16)-sizeof(temp_flash.m_struct_size)-pad_len:%d\n",sizeof(crc_buf),temp_flash.m_struct_size - sizeof(temp_flash.tail_crc16)-sizeof(temp_flash.m_struct_size)-pad_len);
  320. memcpy(buf,&temp_flash,temp_flash.m_struct_size - sizeof(temp_flash.tail_crc16)-sizeof(temp_flash.m_struct_size)-pad_len);
  321. // SEGGER_RTT_printf(0,"read pad_len:%d,info_zone:0x%x,offset:%d,pad_len:%d,temp_flash.m_struct_size:%d\r\n",pad_len,info_zone,offset,pad_len,temp_flash.m_struct_size);
  322. // SEGGER_RTT_printf(0,"2:buf:0x%x sizeof(temp_flash.m_struct_size):%d\n",buf + temp_flash.m_struct_size - sizeof(temp_flash.tail_crc16)-sizeof(temp_flash.m_struct_size)-pad_len,sizeof(temp_flash.m_struct_size));
  323. memcpy(buf + temp_flash.m_struct_size - sizeof(temp_flash.tail_crc16)-sizeof(temp_flash.m_struct_size)-pad_len,(uint32_t*)&temp_flash.m_struct_size,sizeof(temp_flash.m_struct_size));
  324. //计算得到的16位CRC校验码
  325. crc = CalCrc(0, buf, temp_flash.m_struct_size);
  326. if(crc == (temp_flash.tail_crc16 & 0xFFFF)){
  327. SEGGER_RTT_printf(0,"read crc success :0x%x 0x%x \r\n",crc,temp_flash.tail_crc16 & 0xFFFF);
  328. temp_flash.head = FLASH_HEAD;
  329. temp_flash.m_struct_size = sizeof(temp_flash);
  330. temp_flash.tail_crc16 = 0;
  331. crc = CalCrc(0, (char*)&temp_flash, sizeof(temp_flash));//计算得到的16位CRC校验码
  332. temp_flash.tail_crc16 = ((uint32_t)(FLASH_TAIL_H << 8 | FLASH_TAIL_L) << 16) | crc;
  333. *pflash = temp_flash;//校验通过
  334. }
  335. else{
  336. //校验没通过
  337. SEGGER_RTT_printf(0,"read crc fail :0x%x 0x%x \r\n",crc,temp_flash.tail_crc16 & 0xFFFF);
  338. memset(pflash,0,sizeof(Flash_t));
  339. pflash->head = FLASH_HEAD;
  340. pflash->m_struct_size = sizeof(Flash_t);
  341. //将备份区域的数据拷贝
  342. FlashBackup_t temp_backup;
  343. Flash_GetBackup(&temp_backup);
  344. for(int i=0;i<6;i++){
  345. pflash->mClient.macAddr[i] = temp_backup.macAddr_R[i];
  346. pflash->macHost[i] = temp_backup.macAddr_L[i];
  347. }
  348. pflash->mClient.isConfig = temp_backup.isConfig;
  349. pflash->mClient.hardVersion = temp_backup.hardVersion;
  350. pflash->mClient.sotfVersion = temp_backup.sotfVersion;
  351. crc = CalCrc(0, (char*)pflash, sizeof(Flash_t));//计算得到的16位CRC校验码
  352. pflash->tail_crc16 = ((uint32_t)(FLASH_TAIL_H << 8 | FLASH_TAIL_L) << 16) | crc;
  353. err_code = Zone_Erase(info_zone);
  354. if(err_code != ZONE_OP_SUCCESS)return err_code;
  355. err_code = Flash_SaveInfomation();
  356. if(err_code != ZONE_OP_SUCCESS)return err_code;
  357. }
  358. return ZONE_OP_SUCCESS;
  359. }
  360. /**
  361. @brief 存储备份信息
  362. @param 无
  363. @return 错误代码
  364. */
  365. uint32_t Flash_SaveBackup(void)
  366. {
  367. uint32_t err_code;
  368. uint32_t zone_bytes;
  369. FlashBackup_t temp_backup;
  370. int32_t offset;
  371. uint16_t crc;
  372. uint8_t tail_h;
  373. memset(&temp_backup,0,sizeof(temp_backup));
  374. err_code = Zone_GetByteSize(backup_zone, &zone_bytes);
  375. if(err_code != ZONE_OP_SUCCESS)return err_code;
  376. //从区域尾开始找起,找到最新的帧尾
  377. for(offset = zone_bytes - 1; offset >= 0; offset--)
  378. {
  379. err_code = Zone_Read(backup_zone + offset, (uint32_t*)&tail_h, 1);
  380. if(err_code != ZONE_OP_SUCCESS)return err_code;
  381. if(tail_h == FLASH_TAIL_H)break;
  382. }
  383. //重新获取crc16校验码
  384. mBackup.tail_crc16 = 0;
  385. mBackup.m_struct_size = sizeof(mBackup);
  386. mBackup.head = FLASH_HEAD;
  387. crc = CalCrc(0, (char*)&mBackup, sizeof(mBackup));//计算得到的16位CRC校验码
  388. mBackup.tail_crc16 = ((uint32_t)(FLASH_TAIL_H << 8 | FLASH_TAIL_L) << 16) | crc;
  389. //写入
  390. if(offset < 0){ //意味着整个区域是干净的,直接写开头。
  391. err_code = Zone_Write(backup_zone, (uint32_t*)&mBackup, sizeof(mBackup));
  392. if(err_code != ZONE_OP_SUCCESS)return err_code;
  393. // SEGGER_RTT_printf(0,"write zone all clear,mBackup.tail_crc16:0x%x\r\n",mBackup.tail_crc16);
  394. }else{
  395. //这里要偏移才能对齐成员
  396. offset++;
  397. //获取最新的帧尾
  398. err_code = Zone_Read(backup_zone + offset - sizeof(temp_backup.tail_crc16), (uint32_t*)&temp_backup.tail_crc16, sizeof(temp_backup.tail_crc16));
  399. if(err_code != ZONE_OP_SUCCESS)return err_code;
  400. //获取最新的帧长度
  401. err_code = Zone_Read(backup_zone + offset - sizeof(temp_backup.tail_crc16) - sizeof(temp_backup.m_struct_size) , (uint32_t*)&temp_backup.m_struct_size, sizeof(temp_backup.m_struct_size));
  402. if(err_code != ZONE_OP_SUCCESS)return err_code;
  403. //因为结构体强制4字节对齐缘故,所以tail_crc16未必是真实存储的帧尾,后面可能带一些数据为0的填充字节。所以要做校准。
  404. // uint32_t pad_len = temp_backup.m_struct_size - ((uint32_t)&temp_backup.tail_crc16 + sizeof(temp_backup.tail_crc16) - (uint32_t)&temp_backup.head);
  405. uint32_t pad_len = 0;
  406. SEGGER_RTT_printf(0,"save back up info_zone:0x%x offset:%d pad_len:%d temp_backup.m_struct_size:%d\n",backup_zone,offset,pad_len,temp_backup.m_struct_size);
  407. if(offset + pad_len < temp_backup.m_struct_size)pad_len = temp_backup.m_struct_size - offset;
  408. err_code = Zone_Read(backup_zone + offset + pad_len - temp_backup.m_struct_size, (uint32_t*)&temp_backup.head, sizeof(temp_backup.head));
  409. if(err_code != ZONE_OP_SUCCESS)return err_code;
  410. // SEGGER_RTT_printf(0,">>>>>>p_head:0x%x,addr:0x%x\n",temp_backup.head,info_zone + offset + pad_len - temp_backup.m_struct_size);
  411. while(temp_backup.head != FLASH_HEAD){
  412. pad_len++;
  413. err_code = Zone_Read(backup_zone + offset + pad_len - temp_backup.m_struct_size, (uint32_t*)&temp_backup.head, sizeof(temp_backup.head));
  414. if(err_code != ZONE_OP_SUCCESS)return err_code;
  415. // SEGGER_RTT_printf(0,">>>>>>p_head:0x%x,addr:0x%x\n",temp_backup.head,info_zone + offset + pad_len - temp_backup.m_struct_size);
  416. // nrf_delay_ms(5);
  417. }
  418. err_code = Zone_Write(backup_zone + offset + pad_len, (uint32_t*)&mBackup, sizeof(mBackup));
  419. // if(err_code == ZONE_OP_SUCCESS)SEGGER_RTT_printf(0,"write zone forward ,mBackup.tail_crc16:0x%x\r\n",mBackup.tail_crc16);
  420. if(err_code != ZONE_OP_SUCCESS){ //如果写失败了
  421. //擦除该区域
  422. err_code = Zone_Erase(backup_zone);
  423. if(err_code != ZONE_OP_SUCCESS)return err_code;
  424. //写在开头
  425. err_code = Zone_Write(backup_zone, (uint32_t*)&mBackup, sizeof(mBackup));
  426. if(err_code != ZONE_OP_SUCCESS)return err_code;
  427. // SEGGER_RTT_printf(0,"write zone all erase ,mBackup.tail_crc16:0x%x\r\n",mBackup.tail_crc16);
  428. }
  429. }
  430. return ZONE_OP_SUCCESS;
  431. }
  432. /**
  433. @brief 获取备份信息
  434. @param 无
  435. @return 错误代码
  436. */
  437. uint32_t Flash_GetBackup(FlashBackup_t *pbackup)
  438. {
  439. uint32_t err_code;
  440. uint32_t zone_bytes;
  441. FlashBackup_t temp_backup;
  442. int32_t offset;
  443. uint16_t crc;
  444. uint8_t tail_h;
  445. memset(&temp_backup,0,sizeof(temp_backup));
  446. err_code = Zone_GetByteSize(backup_zone, &zone_bytes);
  447. if(err_code != ZONE_OP_SUCCESS)return err_code;
  448. //从区域尾开始找起,找到最新的帧尾
  449. for(offset = zone_bytes - 1; offset >= 0; offset--)
  450. {
  451. err_code = Zone_Read(backup_zone + offset, (uint32_t*)&tail_h, 1);
  452. if(err_code != ZONE_OP_SUCCESS)return err_code;
  453. if(tail_h == FLASH_TAIL_H)break;
  454. }
  455. //意味着整个区域是干净的
  456. if(offset < 0){
  457. memset(pbackup,0,sizeof(FlashBackup_t));
  458. pbackup->head = FLASH_HEAD;
  459. pbackup->m_struct_size = sizeof(FlashBackup_t); //这里是包括填充字节的
  460. crc = CalCrc(0, (char*)pbackup, sizeof(FlashBackup_t));//计算得到的16位CRC校验码,这里注意,有可能会将填充的字节也拿来做运算
  461. pbackup->tail_crc16 = ((uint32_t)(FLASH_TAIL_H << 8 | FLASH_TAIL_L) << 16) | crc; //这里最后才加,是因为不知道填充字节的个数
  462. // SEGGER_RTT_printf(0,"read zone all clear,pbackup->tail_crc16:0x%x\r\n",pbackup->tail_crc16);
  463. return ZONE_OP_SUCCESS;
  464. }
  465. //这里要偏移才能对齐成员
  466. offset++;
  467. //获取最新的帧尾
  468. err_code = Zone_Read(backup_zone + offset - sizeof(temp_backup.tail_crc16), (uint32_t*)&temp_backup.tail_crc16, sizeof(temp_backup.tail_crc16));
  469. if(err_code != ZONE_OP_SUCCESS)return err_code;
  470. //获取最新的帧长度
  471. err_code = Zone_Read(backup_zone + offset - sizeof(temp_backup.tail_crc16) - sizeof(temp_backup.m_struct_size) , (uint32_t*)&temp_backup.m_struct_size, sizeof(temp_backup.m_struct_size));
  472. if(err_code != ZONE_OP_SUCCESS)return err_code;
  473. //因为结构体强制4字节对齐缘故,所以tail_crc16未必是真实存储的帧尾,后面可能带一些数据为0的填充字节。所以要做校准。
  474. // uint32_t pad_len = temp_backup.m_struct_size - ((uint32_t)&temp_backup.tail_crc16 + sizeof(temp_backup.tail_crc16) - (uint32_t)&temp_backup.head);
  475. uint32_t pad_len = 0;
  476. SEGGER_RTT_printf(0,"back up info_zone:0x%x offset:%d pad_len:%d temp_backup.m_struct_size:%d\n",backup_zone,offset,pad_len,temp_backup.m_struct_size);
  477. if(offset + pad_len < temp_backup.m_struct_size)pad_len = temp_backup.m_struct_size - offset;
  478. err_code = Zone_Read(backup_zone + offset + pad_len - temp_backup.m_struct_size, (uint32_t*)&temp_backup.head, sizeof(temp_backup.head));
  479. if(err_code != ZONE_OP_SUCCESS)return err_code;
  480. // SEGGER_RTT_printf(0,">>>>>>p_head:0x%x,addr:0x%x\n",temp_backup.head,info_zone + offset + pad_len - temp_backup.m_struct_size);
  481. while(temp_backup.head != FLASH_HEAD){
  482. pad_len++;
  483. err_code = Zone_Read(backup_zone + offset + pad_len - temp_backup.m_struct_size, (uint32_t*)&temp_backup.head, sizeof(temp_backup.head));
  484. if(err_code != ZONE_OP_SUCCESS)return err_code;
  485. // SEGGER_RTT_printf(0,">>>>>>p_head:0x%x,addr:0x%x\n",temp_backup.head,info_zone + offset + pad_len - temp_backup.m_struct_size);
  486. // nrf_delay_ms(5);
  487. }
  488. // SEGGER_RTT_printf(0,"read pad_len 0x%x\r\n",pad_len);
  489. //获取最新的帧
  490. err_code = Zone_Read(backup_zone + offset + pad_len - temp_backup.m_struct_size , (uint32_t*)&temp_backup, temp_backup.m_struct_size - sizeof(temp_backup.tail_crc16)-sizeof(temp_backup.m_struct_size)-pad_len);
  491. if(err_code != ZONE_OP_SUCCESS)return err_code;
  492. //校验数据
  493. FlashBackup_t crc_buf;//单纯做一个缓冲区
  494. memset(&crc_buf,0,sizeof(crc_buf));
  495. char *buf = (char*)&crc_buf;
  496. //获取成员head ~ m_struct_size
  497. memcpy(buf,&temp_backup,temp_backup.m_struct_size - sizeof(temp_backup.tail_crc16)-sizeof(temp_backup.m_struct_size)-pad_len);
  498. memcpy(buf + temp_backup.m_struct_size - sizeof(temp_backup.tail_crc16)-sizeof(temp_backup.m_struct_size)-pad_len,(uint32_t*)&temp_backup.m_struct_size,sizeof(temp_backup.m_struct_size));
  499. //计算得到的16位CRC校验码
  500. crc = CalCrc(0, buf, temp_backup.m_struct_size);
  501. if(crc == (temp_backup.tail_crc16 & 0xFFFF)){
  502. // SEGGER_RTT_printf(0,"read crc success :0x%x 0x%x \r\n",crc,temp_backup.tail_crc16 & 0xFFFF);
  503. temp_backup.head = FLASH_HEAD;
  504. temp_backup.m_struct_size = sizeof(temp_backup);
  505. temp_backup.tail_crc16 = 0;
  506. crc = CalCrc(0, (char*)&temp_backup, sizeof(temp_backup));//计算得到的16位CRC校验码
  507. temp_backup.tail_crc16 = ((uint32_t)(FLASH_TAIL_H << 8 | FLASH_TAIL_L) << 16) | crc;
  508. *pbackup = temp_backup;//校验通过
  509. }
  510. else{
  511. //校验没通过
  512. // SEGGER_RTT_printf(0,"read crc fail :0x%x 0x%x \r\n",crc,temp_backup.tail_crc16 & 0xFFFF);
  513. memset(pbackup,0,sizeof(FlashBackup_t));
  514. pbackup->head = FLASH_HEAD;
  515. pbackup->m_struct_size = sizeof(FlashBackup_t);
  516. crc = CalCrc(0, (char*)pbackup, sizeof(FlashBackup_t));//计算得到的16位CRC校验码
  517. pbackup->tail_crc16 = ((uint32_t)(FLASH_TAIL_H << 8 | FLASH_TAIL_L) << 16) | crc;
  518. err_code = Zone_Erase(backup_zone);
  519. if(err_code != ZONE_OP_SUCCESS)return err_code;
  520. }
  521. return ZONE_OP_SUCCESS;
  522. }
  523. /**
  524. @brief 保存日志信息
  525. @param[in] id Fault identifier. See @ref NRF_FAULT_IDS.
  526. @param[in] pc The program counter of the instruction that triggered the fault, or 0 if
  527. unavailable.
  528. @param[in] info Optional additional information regarding the fault. The value of the @p id
  529. parameter dictates how to interpret this parameter. Refer to the documentation
  530. for each fault identifier (@ref NRF_FAULT_IDS and @ref APP_ERROR_FAULT_IDS) for
  531. details about interpreting @p info.
  532. @return 错误代码
  533. */
  534. uint32_t Flash_SaveLog(uint32_t id, uint32_t pc, uint32_t info)
  535. {
  536. memset((uint8_t*)(&mFlash.mFlashLog),0,sizeof(FlashLog));
  537. mFlash.mFlashLog.Errorflag =1;
  538. switch (id)
  539. {
  540. #if defined(SOFTDEVICE_PRESENT) && SOFTDEVICE_PRESENT
  541. case NRF_FAULT_ID_SD_ASSERT:
  542. memcpy(mFlash.mFlashLog.logData,"SD: ASSERTION FAILED\r\n",MaxLength(sizeof("SD: ASSERTION FAILED\r\n"),sizeof(mFlash.mFlashLog.logData)));
  543. break;
  544. case NRF_FAULT_ID_APP_MEMACC:
  545. memcpy(mFlash.mFlashLog.logData,"SD: INVALID MEMORY ACCESS\r\n",MaxLength(sizeof("SD: INVALID MEMORY ACCESS\r\n"),sizeof(mFlash.mFlashLog.logData)));
  546. break;
  547. #endif
  548. case NRF_FAULT_ID_SDK_ASSERT:
  549. {
  550. assert_info_t * p_info = (assert_info_t *)info;
  551. sprintf((char *)mFlash.mFlashLog.logData,"ASSERTION FAILED %s:%u\r\n",
  552. p_info->p_file_name,
  553. p_info->line_num);
  554. break;
  555. }
  556. case NRF_FAULT_ID_SDK_ERROR:
  557. {
  558. error_info_t * p_info = (error_info_t *)info;
  559. sprintf((char *)mFlash.mFlashLog.logData,"error:%u,%s:%u\r\n",
  560. p_info->err_code,
  561. p_info->p_file_name,
  562. p_info->line_num);
  563. SEGGER_RTT_printf(0,">>>>>err code :%d,%s",p_info->err_code,mFlash.mFlashLog.logData);
  564. break;
  565. }
  566. default:
  567. sprintf((char *)mFlash.mFlashLog.logData,"UNKNOWN FAULT 0x%08X\n", pc);
  568. break;
  569. }
  570. return Flash_SaveInfomation();
  571. }
  572. /**
  573. @brief 返回主机标志位
  574. @param 无
  575. @return 主机标志位
  576. */
  577. uint8_t Get_isHost(void)
  578. {
  579. return mFlash.isHost;
  580. }
  581. /**
  582. @brief 测试halflash接口
  583. @param 无
  584. @return 无
  585. */
  586. void TestHalFlashInterface(void)
  587. {
  588. Fstorage_FlashInit();
  589. SEGGER_RTT_printf(0,"Zone_Alloca(FLASH_ADDR_INFO_PAGENUM, &info_zone):%d\n",Zone_Alloca(FLASH_ADDR_INFO_PAGENUM, &info_zone));
  590. SEGGER_RTT_printf(0,"Zone_Alloca(FLASH_ADDR_STEP_PAGENUM, &step_zone):%d\n",Zone_Alloca(FLASH_ADDR_STEP_PAGENUM, &step_zone));
  591. SEGGER_RTT_printf(0,"Zone_Alloca(FLASH_ADDR_BACKUP_PAGENUM, &backup_zone):%d\n",Zone_Alloca(FLASH_ADDR_BACKUP_PAGENUM, &backup_zone));
  592. SEGGER_RTT_printf(0,"Flash_GetInfomation(&mFlash):%d \n",Flash_GetInfomation(&mFlash));
  593. SEGGER_RTT_printf(0,"Flash_GetInfomation(&mBackup):%d \n",Flash_GetBackup(&mBackup));
  594. uint32_t i;
  595. // Flash_t m_testflash;
  596. // FlashBackup_t m_testbackup;
  597. // //测试基本信息和备份信息的写入和读取
  598. // for(i=0;i<10000;i++)
  599. // {
  600. // mFlash.mStep.num = i;
  601. // SEGGER_RTT_printf(0,"Flash_SaveInfomation[%d]:%d \n",i,Flash_SaveInfomation());
  602. //
  603. // mBackup.hardVersion = i;
  604. // SEGGER_RTT_printf(0,"Flash_SaveBackup[%d]:%d \n",i,Flash_SaveBackup());
  605. // }
  606. //
  607. // SEGGER_RTT_printf(0,"Flash_GetInfomation[%d]:%d \n",i,Flash_GetInfomation(&m_testflash));
  608. // SEGGER_RTT_printf(0,"m_testflash:%d \n",m_testflash.mStep.num);
  609. //
  610. // SEGGER_RTT_printf(0,"Flash_GetBackup[%d]:%d \n",i,Flash_GetBackup(&m_testbackup));
  611. // SEGGER_RTT_printf(0,"m_testbackup:%d \n",m_testbackup.hardVersion);
  612. //测试步数信息的写入和读取
  613. for(i=0;i<(PAGE_INT_SIZE * FLASH_ADDR_STEP_PAGENUM+100);i++)
  614. {
  615. mFlash.mStep.stepCur[0] = 0;
  616. mFlash.mStep.stepCur[1] = i;
  617. SEGGER_RTT_printf(0,"Flash_SaveStep[%d]:%d \n\n",i,Flash_SaveStep());
  618. }
  619. uint8_t checkflash[4] = {0};
  620. for(i=0;i<(PAGE_INT_SIZE * FLASH_ADDR_STEP_PAGENUM);i+=4)
  621. {
  622. // Zone_Read(step_zone + i, (uint32_t*)&step, 4);
  623. // SEGGER_RTT_printf(0,"step[%d]:0x%x ",i,step);
  624. Zone_Read(step_zone + i,(uint32_t*)&checkflash[0], 4);
  625. SEGGER_RTT_printf(0,"checkflash[%d]:%02x,%02x,%02x,%02x ",i,checkflash[0],checkflash[1],checkflash[2],checkflash[3]);
  626. if(i%5 == 0)SEGGER_RTT_printf(0,"\n");
  627. nrf_delay_ms(5);
  628. }
  629. ////uint32_t Flash_SaveStep(void);
  630. //////存储基本信息
  631. ////uint32_t Flash_SaveInfomation(void);
  632. //////存储备份信息
  633. ////uint32_t Flash_SaveBackup(void);
  634. //////获取基本信息
  635. ////uint32_t Flash_GetInfomation(Flash_t *pflash);
  636. //////获取备份信息
  637. ////uint32_t Flash_GetBackup(FlashBackup_t *pbackup);
  638. //
  639. // uint32_t i;
  640. // Flash_t m_testflash;
  641. // FlashBackup_t m_testbackup;
  642. // Fstorage_FlashInit();
  643. //
  644. // #define INFO_PAGE_SIZE 1
  645. // #define BACKUP_PAGE_SIZE 1
  646. // #define STEP_PAGE_SIZE 2
  647. //
  648. // static uint32_t info_index;
  649. // static uint32_t backup_index;
  650. // static uint32_t step_index;
  651. //
  652. // Zone_Init(INFO_PAGE_SIZE, &info_index);
  653. // Zone_Init(BACKUP_PAGE_SIZE, &backup_index);
  654. // Zone_Init(STEP_PAGE_SIZE, &step_index);
  655. //
  656. // SEGGER_RTT_printf(0,"info_index=%d backup_index=%d step_index=%d\n",info_index,backup_index,step_index);
  657. //
  658. //
  659. //
  660. // while(1);
  661. //
  662. // //测试基本信息和备份信息的写入和读取
  663. // for(i=0;i<10000;i++)
  664. // {
  665. // mFlash.mStep.num = i;
  666. // SEGGER_RTT_printf(0,"Flash_SaveInfomation[%d]:%d \n",i,Flash_SaveInfomation());
  667. //
  668. // mBackup.hardVersion = i;
  669. // SEGGER_RTT_printf(0,"Flash_SaveBackup[%d]:%d \n",i,Flash_SaveBackup());
  670. // }
  671. //
  672. // SEGGER_RTT_printf(0,"Flash_GetInfomation[%d]:%d \n",i,Flash_GetInfomation(&m_testflash));
  673. // SEGGER_RTT_printf(0,"m_testflash:%d \n",m_testflash.mStep.num);
  674. //
  675. // SEGGER_RTT_printf(0,"Flash_GetInfomation[%d]:%d \n",i,Flash_GetBackup(&m_testbackup));
  676. // SEGGER_RTT_printf(0,"m_testbackup:%d \n",m_testbackup.hardVersion);
  677. // //测试步数信息的写入和读取
  678. // for(i=0;i<1500;i++)
  679. // {
  680. // mFlash.mStep.num = i;
  681. // mFlash.mStep.stepCur[0] = i;
  682. // mFlash.mStep.stepCur[1] = i+i;
  683. //
  684. // SEGGER_RTT_printf(0,"Flash_SaveStep[%d]:%d \n\n",i,Flash_SaveStep());
  685. // }
  686. // uint32_t checkflash[750];
  687. // Read_N_Byte_flash(FLASH_ADDR_STEP , (uint32_t*)checkflash, sizeof(checkflash));
  688. //
  689. // for(i=0;i<750;i++)
  690. // {
  691. // SEGGER_RTT_printf(0,"checkflash[%d]:0x%x ",i,checkflash[i]);
  692. // if(i%5 == 0)SEGGER_RTT_printf(0,"\n");
  693. // nrf_delay_ms(5);
  694. // }
  695. SEGGER_RTT_printf(0,"TestHalFlashInterface done !!!!!!!\n");
  696. }