app_flash.c 21 KB

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