exception.c 14 KB

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