app_flash.c 24 KB

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