app_flash.c 30 KB

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