exception.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*Includes ----------------------------------------------*/
  2. #include "nrf_delay.h"
  3. #include "nrf_strerror.h"
  4. #include "ble_comm.h"
  5. #include "exception.h"
  6. #include "system.h"
  7. #include "bsp_time.h"
  8. #include "hal_flash.h"
  9. #include "hal_ser_imu_mode_manage.h"
  10. #include "hal_led.h"
  11. #include "hal_ble_client.h"
  12. #include "hal_ble_host.h"
  13. #include "hal_ser_imu_mode_manage.h"
  14. #include "app_flash.h"
  15. #include "app_error.h"
  16. #include "app_client.h"
  17. #include "app_flash.h"
  18. #include "app_util_platform.h"
  19. /*Private macro ------------------------------------------------*/
  20. #define EXCEPT_LED_ON_ACC_Z 1500 //加速度Z轴触发异常灯开启
  21. #define EXCEPT_LED_OFF_ACC_Z -1500 //加速度Z轴触发异常灯关闭
  22. #define LED_PROCESS_CYCLE 200 //异常灯线程周期,ms为单位
  23. #define EXCEPT_LED_TOGGLE_TIME 200 //异常灯翻转周期,ms为单位
  24. #define EXCEPT_LED_DISPLAY_TIME 10000 //异常灯显示时长,ms为单位
  25. #define UNKOWN_RESET_INFO_SEND_CYCLE 100 //未知重启异常信息发送线程周期,ms为单位
  26. #define __NO_INIT__ __attribute__((at(0x2000FFA0)))
  27. /*STRUCTION ----------------------------------------------------*/
  28. //原理如下:当异常时,硬件会将一些CPU的寄存器保存到栈中,通过在异常时获取堆栈指针SP的值,通过SP获取当前栈的位置,然后获取异常之前的PC指针的值,就知道异常的位置了。
  29. //这个就是CPU寄存器在栈中的排列
  30. //栈数据定义
  31. typedef struct
  32. {
  33. uint32_t R0;
  34. uint32_t R1;
  35. uint32_t R2;
  36. uint32_t R3;
  37. uint32_t R12;
  38. uint32_t LR;
  39. uint32_t PC;
  40. uint32_t xPSR;
  41. }STACK_DATA_TYPE;
  42. typedef enum{
  43. EXCEPT_LED_ON, //异常灯 - 开
  44. EXCEPT_LED_OFF, //异常灯 - 关
  45. } EXCEPT_LED_SWITCH_e;
  46. typedef enum{
  47. EXCEPT_UNKOWN_RESET_TRIGGER_WDT = 0, //异常——未知重启触发原因——看门狗
  48. EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT, //异常——未知重启触发原因——跑飞
  49. EXCEPT_UNKOWN_RESET_TRIGGER_NUMS, //异常——未知重启触发原因——数量
  50. } EXCEPT_UNKOWN_RESET_TRIGGER_e;
  51. typedef struct _except_unkown_reset_info
  52. {
  53. STACK_DATA_TYPE stack; //堆栈信息
  54. uint32_t cur_process_id; //当前正在处理的进程号
  55. EXCEPT_UNKOWN_RESET_TRIGGER_e trigger; //导致未知重启触发的类型
  56. bool is_upload; //是否正在上传
  57. } Except_Unkown_Reset_Info_t;
  58. typedef struct no_init
  59. {
  60. Except_Unkown_Reset_Info_t unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_NUMS];
  61. } No_Init_t;
  62. typedef struct exception{
  63. /*private member*/
  64. EXCEPT_LED_SWITCH_e led_switch; //异常灯开关
  65. uint16_t led_display_time; //异常灯显示时长
  66. uint16_t led_toggle_time; //异常灯翻转时长
  67. uint8_t except_type[EXCEPT_NUM]; //异常类型
  68. uint16_t except_number_of_exist; //已存在的异常数量
  69. } Exception_t;
  70. /*Local Variable ----------------------------------------------*/
  71. static Exception_t ob_exception;
  72. static volatile __NO_INIT__ No_Init_t no_init_data;
  73. battercb_t battery_record={0};
  74. /*Local Functions ----------------------------------------------*/
  75. //没有操作系统时的异常处理
  76. static void Except_NotOSHardFault_Handler(uint32_t msp_addr)
  77. {
  78. STACK_DATA_TYPE *p; //堆栈中存储的数据
  79. msp_addr -= 4; //堆栈指针减去4,因为默认堆栈指针指向的是下一个空的地方-所以必须减去4
  80. DEBUG_LOG("\r\n-----------------ERROR -----------------\r\nHardFault_Handler\r\n");
  81. if((msp_addr >> 20) != 0x200) //判断地址范围,必须是0x200xxxxx 范围
  82. {
  83. DEBUG_LOG("Warning: stack pointer is damaged, unable to record the scene!\r\n");
  84. return;
  85. }
  86. msp_addr += 8; //进入中断后,堆栈又进入了2个u32数据,因此需要往后推
  87. no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT].is_upload = true;
  88. no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT].trigger = EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT;
  89. memcpy(&no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT].stack, (STACK_DATA_TYPE *)msp_addr, sizeof(STACK_DATA_TYPE));
  90. p = (STACK_DATA_TYPE *)msp_addr;
  91. p->PC -= 3; //PC指针要减去3
  92. //
  93. // DEBUG_LOG("R0:0x%08X\r\n", p->R0);
  94. // DEBUG_LOG("R1:0x%08X\r\n", p->R1);
  95. // DEBUG_LOG("R2:0x%08X\r\n", p->R2);
  96. // DEBUG_LOG("R3:0x%08X\r\n", p->R3);
  97. // DEBUG_LOG("R12:0x%08X\r\n", p->R12);
  98. // DEBUG_LOG("LR:0x%08X\r\n", p->LR);
  99. // DEBUG_LOG("PC:0x%08X\r\n", p->PC);
  100. // DEBUG_LOG("xPSR:0x%08X\r\n", p->xPSR);
  101. // DEBUG_LOG("system reset...\r\n");
  102. }
  103. ////有操作系统时的异常处理
  104. //static void Except_OSHardFault_Handler(uint32_t psp_addr)
  105. //{
  106. // STACK_DATA_TYPE *p; //堆栈中存储的数据
  107. //
  108. // psp_addr -= 4; //堆栈指针减去4,因为默认堆栈指针指向的是下一个空的地方-所以必须减去4
  109. // DEBUG_LOG("\r\n-----------------ERROR -----------------\r\nHardFault_Handler\r\n");
  110. //
  111. //
  112. // if((psp_addr >> 20) != 0x200) //判断地址范围,必须是0x200xxxxx 范围
  113. // {
  114. // DEBUG_LOG("Warning: stack pointer is damaged, unable to record the scene!\r\n");
  115. // return;
  116. // }
  117. //
  118. // no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT].is_upload = true;
  119. //
  120. // no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT].trigger = EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT;
  121. //
  122. // memcpy(&no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT].stack, (STACK_DATA_TYPE *)psp_addr, sizeof(STACK_DATA_TYPE));
  123. //
  124. // p = (STACK_DATA_TYPE *)psp_addr;
  125. // p->PC -= 3; //PC指针要减去3
  126. //
  127. //// DEBUG_LOG("R0:0x%08X\r\n", p->R0);
  128. //// DEBUG_LOG("R1:0x%08X\r\n", p->R1);
  129. //// DEBUG_LOG("R2:0x%08X\r\n", p->R2);
  130. //// DEBUG_LOG("R3:0x%08X\r\n", p->R3);
  131. //// DEBUG_LOG("R12:0x%08X\r\n", p->R12);
  132. //// DEBUG_LOG("LR:0x%08X\r\n", p->LR);
  133. //// DEBUG_LOG("PC:0x%08X\r\n", p->PC);
  134. //// DEBUG_LOG("xPSR:0x%08X\r\n", p->xPSR);
  135. //// DEBUG_LOG("system reset...\r\n");
  136. //}
  137. static void Exception_Led_Process(void);
  138. static void Except_Led_Close(void)
  139. {
  140. //全功率关闭
  141. Process_SetHoldOn(Exception_Led_Process,0);
  142. ob_exception.led_switch = EXCEPT_LED_OFF;
  143. ob_exception.led_display_time = EXCEPT_LED_DISPLAY_TIME/LED_PROCESS_CYCLE;
  144. ob_exception.led_toggle_time = EXCEPT_LED_TOGGLE_TIME/LED_PROCESS_CYCLE;
  145. LED_Stop(LED_EXCEPT);
  146. }
  147. static void Except_Led_OpenOnce(void)
  148. {
  149. //异常灯显示/关闭
  150. if(ob_exception.led_display_time != 0)
  151. {
  152. //全功率开启
  153. Process_SetHoldOn(Exception_Led_Process,1);
  154. if(ob_exception.led_toggle_time == 0)
  155. {
  156. if(ob_exception.led_switch == EXCEPT_LED_ON)ob_exception.led_switch = EXCEPT_LED_OFF;
  157. else ob_exception.led_switch = EXCEPT_LED_ON;
  158. ob_exception.led_toggle_time = EXCEPT_LED_TOGGLE_TIME/LED_PROCESS_CYCLE;
  159. }
  160. else
  161. {
  162. if(ob_exception.led_switch == EXCEPT_LED_ON)LED_Start(LED_EXCEPT,COLOR_ORANGE);
  163. if(ob_exception.led_switch == EXCEPT_LED_OFF)LED_Start(LED_EXCEPT,COLOR_BLACK);
  164. ob_exception.led_toggle_time--;
  165. }
  166. ob_exception.led_display_time--;
  167. }
  168. }
  169. static void Exception_Led_Process(void)
  170. {
  171. int16_t f_acc[3];
  172. ser_imu_data_t data;
  173. //读取ACC值
  174. if(hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT) >= 1){
  175. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, 0, &data);
  176. f_acc[0] = data.acc[0];f_acc[1] = data.acc[1];f_acc[2] = data.acc[2];
  177. }
  178. else return;
  179. //把鞋子倒转平放,且存在异常
  180. // DEBUG_LOG("Exception_Led_Process:%d,%d,%d\n",f_acc[0],f_acc[1],f_acc[2]);
  181. if(f_acc[2] >= EXCEPT_LED_ON_ACC_Z && ob_exception.except_number_of_exist > 0)
  182. {
  183. Except_Led_OpenOnce();
  184. }//把鞋子正放
  185. else if(f_acc[2] <= EXCEPT_LED_OFF_ACC_Z)
  186. {
  187. Except_Led_Close();
  188. }
  189. }
  190. static void Exception_UnkownReset_Info_Send_Process(void)
  191. {
  192. char buf[250]={0};
  193. memset(buf,0,sizeof(buf));
  194. // static uint8_t cur_flow = 0;
  195. for(int i = 0; i < EXCEPT_UNKOWN_RESET_TRIGGER_NUMS; i++)
  196. {
  197. if(no_init_data.unkown_reset_info[i].is_upload == true)
  198. {
  199. switch(no_init_data.unkown_reset_info[i].trigger)
  200. {
  201. case EXCEPT_UNKOWN_RESET_TRIGGER_WDT:
  202. sprintf(buf,"id:%d\r\n",no_init_data.unkown_reset_info[i].cur_process_id);
  203. if(Except_TxError(EXCEPT_UNKOWN_RESET,buf) == 0){no_init_data.unkown_reset_info[i].is_upload = false;}
  204. break;
  205. case EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT:
  206. sprintf(buf,"id:0x%x,R:0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,H:0x%x,S:0x%x\r\n", no_init_data.unkown_reset_info[i].cur_process_id, \
  207. no_init_data.unkown_reset_info[i].stack.R0, \
  208. no_init_data.unkown_reset_info[i].stack.R1, \
  209. no_init_data.unkown_reset_info[i].stack.R2, \
  210. no_init_data.unkown_reset_info[i].stack.R3, \
  211. no_init_data.unkown_reset_info[i].stack.R12, \
  212. no_init_data.unkown_reset_info[i].stack.LR, \
  213. no_init_data.unkown_reset_info[i].stack.PC - 3, \
  214. no_init_data.unkown_reset_info[i].stack.xPSR,
  215. HARDWARE_VERSION,SOFTWARE_VERSION);
  216. // DEBUG_LOG("%s\r\n", buf);
  217. if(Except_TxError(EXCEPT_UNKOWN_RESET,buf) == 0){no_init_data.unkown_reset_info[i].is_upload = false;}
  218. break;
  219. default:
  220. no_init_data.unkown_reset_info[i].is_upload = false;
  221. break;
  222. }
  223. if(no_init_data.unkown_reset_info[i].trigger == EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT)
  224. {
  225. // DEBUG_LOG("R0:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.R0);
  226. // DEBUG_LOG("R1:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.R1);
  227. // DEBUG_LOG("R2:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.R2);
  228. // DEBUG_LOG("R3:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.R3);
  229. // DEBUG_LOG("R12:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.R12);
  230. // DEBUG_LOG("LR:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.LR);
  231. // DEBUG_LOG("PC:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.PC - 3);
  232. // DEBUG_LOG("xPSR:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.xPSR);
  233. }
  234. else
  235. {
  236. // DEBUG_LOG("id:%d\r\n", no_init_data.unkown_reset_info[i].cur_process_id);
  237. }
  238. }
  239. }
  240. }
  241. /*API ----------------------------------------------*/
  242. /**
  243. @brief 注册接收右鞋错误信息的回调
  244. @param Handle
  245. @return 无
  246. */
  247. void cb_BLE_Host_R_ERR(void* handle)
  248. {
  249. BLE_Host_Rx_t* target = handle;
  250. BLE_Client_Tx_Send(0,BLE_ERR,target->pDat,target->datLen);
  251. }
  252. void Exception_Init(void)
  253. {
  254. // memset(&no_init_data, 0 , sizeof(No_Init_t)*EXCEPT_UNKOWN_RESET_TRIGGER_NUMS);
  255. memset(&ob_exception, 0 , sizeof(ob_exception));
  256. ob_exception.led_switch = EXCEPT_LED_OFF;
  257. ob_exception.led_display_time = EXCEPT_LED_DISPLAY_TIME/LED_PROCESS_CYCLE;
  258. ob_exception.led_toggle_time = EXCEPT_LED_TOGGLE_TIME/LED_PROCESS_CYCLE;
  259. Process_Start(LED_PROCESS_CYCLE,"Exception_Led_Process",Exception_Led_Process);
  260. Process_Start(UNKOWN_RESET_INFO_SEND_CYCLE,"Exception_UnkownReset_Info_Send_Process",Exception_UnkownReset_Info_Send_Process);
  261. BLE_Host_Rx_Regist(BLE_ERR,cb_BLE_Host_R_ERR);
  262. DEBUG_LOG("Exception_Init - sizeof(no_init_data):%d\r\n", sizeof(no_init_data));
  263. memcpy(&battery_record,mFlash.batter,sizeof(battercb_t));
  264. extern void printbatter_cb(battercb_t *c,unsigned char *buff);
  265. printbatter_cb(&battery_record,mFlash.batter);
  266. }
  267. /**
  268. @brief 系统错误回调
  269. @param id:
  270. @return 无
  271. */
  272. void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
  273. {
  274. Flash_SaveLog(id,pc,info);
  275. DEBUG_LOG("app_error_fault_handler,System reset\n");
  276. nrf_gpio_cfg_output(PIN_RESET_PIN); nrf_gpio_pin_write(PIN_RESET_PIN,0);
  277. }
  278. int Except_TxError(ExcepType_t excep_type,const char *errstr){
  279. uint8_t buf[250];
  280. uint8_t L=0;
  281. uint16_t err_strlen =0;
  282. err_strlen = strlen(errstr);
  283. if(err_strlen > (sizeof(buf)-2))return -1;
  284. if(mFlash.isHost)buf[L++] =0;//0左鞋1右鞋
  285. else buf[L++] =1;
  286. buf[L++] = excep_type; //错误编号
  287. memcpy(&buf[L],errstr,err_strlen);
  288. L +=err_strlen;
  289. return BLE_Client_Send(BLE_ERR,buf,L);
  290. }
  291. int Except_Display(ExcepType_t excep_type,const char *errstr){
  292. int err =0;
  293. // uint8_t buf[250];
  294. // uint8_t L=0;
  295. // uint16_t err_strlen =0;
  296. // err_strlen = strlen(errstr);
  297. // if(err_strlen > (sizeof(buf)-2))return -1;
  298. // if(mFlash.isHost)buf[L++] =0;//0左鞋1右鞋
  299. // else buf[L++] =1;
  300. // buf[L++] = excep_type; //错误编号
  301. //
  302. // memcpy(&buf[L],errstr,err_strlen);
  303. // L +=err_strlen;
  304. //
  305. // err = BLE_Client_Send(BLE_ERR,buf,L);
  306. return err;
  307. }
  308. void Except_SetExceptype(ExcepType_t excep_type)
  309. {
  310. if(ob_exception.except_type[excep_type] != 1)
  311. {
  312. ob_exception.except_type[excep_type] = 1;
  313. ob_exception.except_number_of_exist++;
  314. }
  315. }
  316. void Except_ClearExceptype(ExcepType_t excep_type)
  317. {
  318. if(ob_exception.except_type[excep_type] == 1)
  319. {
  320. ob_exception.except_type[excep_type] = 0;
  321. if(ob_exception.except_number_of_exist != 0)ob_exception.except_number_of_exist--;
  322. }
  323. }
  324. bool Except_IsError(ExcepType_t excep_type)
  325. {
  326. if(ob_exception.except_type[excep_type] == 1)
  327. {
  328. return true;
  329. }
  330. return false;
  331. }
  332. bool Except_IsErrorExist(void)
  333. {
  334. if(ob_exception.except_number_of_exist > 0)return true;
  335. return false;
  336. }
  337. //硬件中断
  338. void HardFault_Handler (void)
  339. {
  340. uint32_t msp_addr = __get_MSP(); //获取线程模式下堆栈指针位置
  341. uint32_t psp_addr = __get_PSP(); //获取中断下的堆栈指针位置-用于OS启动后
  342. #if(UCOS_II_EN) //使能了操作系统的一个宏定义,自己去定义
  343. if(SYS_GetOsStartup()) //操作系统运行了-自己定义一个状态,可以获取操作系统是否启动
  344. {
  345. Except_OSHardFault_Handler(psp_addr);
  346. }
  347. else
  348. {
  349. Except_NotOSHardFault_Handler(msp_addr);
  350. }
  351. #else //没有使能操作系统
  352. Except_NotOSHardFault_Handler(msp_addr);
  353. #endif //UCOS_II_EN
  354. nrf_delay_ms(10); //延时一下,防止重启速度太快
  355. NVIC_SystemReset(); //复位重启
  356. }
  357. void Except_Get_Cur_Porcess_ID(uint32_t cur_process_id)
  358. {
  359. for(int i = 0; i < EXCEPT_UNKOWN_RESET_TRIGGER_NUMS; i++)
  360. {
  361. if(no_init_data.unkown_reset_info[i].is_upload == false)
  362. {
  363. no_init_data.unkown_reset_info[i].cur_process_id = cur_process_id;
  364. }
  365. }
  366. }
  367. void Except_Unkown_Reset_WDT_Set(void)
  368. {
  369. no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_WDT].is_upload = true;
  370. }
  371. /**
  372. @brief 获取电量记录缓存区地址
  373. @return 电量记录缓存区地址
  374. */
  375. battercb_t* Except_Get_Battery_Record_Buff(void)
  376. {
  377. return &battery_record;
  378. }