exception.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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(0x2000FF60)))
  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. char battery_record_buff[64];
  62. } No_Init_t;
  63. typedef struct exception{
  64. /*private member*/
  65. EXCEPT_LED_SWITCH_e led_switch; //异常灯开关
  66. uint16_t led_display_time; //异常灯显示时长
  67. uint16_t led_toggle_time; //异常灯翻转时长
  68. uint8_t except_type[EXCEPT_NUM]; //异常类型
  69. uint16_t except_number_of_exist; //已存在的异常数量
  70. } Exception_t;
  71. /*Local Variable ----------------------------------------------*/
  72. static Exception_t ob_exception;
  73. static __NO_INIT__ No_Init_t no_init_data;
  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. SEGGER_RTT_printf(0,"\r\n-----------------ERROR -----------------\r\nHardFault_Handler\r\n");
  81. if((msp_addr >> 20) != 0x200) //判断地址范围,必须是0x200xxxxx 范围
  82. {
  83. SEGGER_RTT_printf(0,"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. //BackupArea_RecordHardFault(RTC_GetSec(), p->PC); //记录异常pc指针信息到备份区
  93. SEGGER_RTT_printf(0,"R0:0x%08X\r\n", p->R0);
  94. SEGGER_RTT_printf(0,"R1:0x%08X\r\n", p->R1);
  95. SEGGER_RTT_printf(0,"R2:0x%08X\r\n", p->R2);
  96. SEGGER_RTT_printf(0,"R3:0x%08X\r\n", p->R3);
  97. SEGGER_RTT_printf(0,"R12:0x%08X\r\n", p->R12);
  98. SEGGER_RTT_printf(0,"LR:0x%08X\r\n", p->LR);
  99. SEGGER_RTT_printf(0,"PC:0x%08X\r\n", p->PC);
  100. SEGGER_RTT_printf(0,"xPSR:0x%08X\r\n", p->xPSR);
  101. SEGGER_RTT_printf(0,"system reset...\r\n");
  102. }
  103. //有操作系统时的异常处理
  104. static void Except_OSHardFault_Handler(uint32_t psp_addr)
  105. {
  106. STACK_DATA_TYPE *p; //堆栈中存储的数据
  107. psp_addr -= 4; //堆栈指针减去4,因为默认堆栈指针指向的是下一个空的地方-所以必须减去4
  108. SEGGER_RTT_printf(0,"\r\n-----------------ERROR -----------------\r\nHardFault_Handler\r\n");
  109. if((psp_addr >> 20) != 0x200) //判断地址范围,必须是0x200xxxxx 范围
  110. {
  111. SEGGER_RTT_printf(0,"Warning: stack pointer is damaged, unable to record the scene!\r\n");
  112. return;
  113. }
  114. no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT].is_upload = true;
  115. no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT].trigger = EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT;
  116. memcpy(&no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT].stack, (STACK_DATA_TYPE *)psp_addr, sizeof(STACK_DATA_TYPE));
  117. p = (STACK_DATA_TYPE *)psp_addr;
  118. p->PC -= 3; //PC指针要减去3
  119. //BackupArea_RecordHardFault(RTC_GetSec(), p->PC); //记录异常pc指针信息到备份区
  120. SEGGER_RTT_printf(0,"R0:0x%08X\r\n", p->R0);
  121. SEGGER_RTT_printf(0,"R1:0x%08X\r\n", p->R1);
  122. SEGGER_RTT_printf(0,"R2:0x%08X\r\n", p->R2);
  123. SEGGER_RTT_printf(0,"R3:0x%08X\r\n", p->R3);
  124. SEGGER_RTT_printf(0,"R12:0x%08X\r\n", p->R12);
  125. SEGGER_RTT_printf(0,"LR:0x%08X\r\n", p->LR);
  126. SEGGER_RTT_printf(0,"PC:0x%08X\r\n", p->PC);
  127. SEGGER_RTT_printf(0,"xPSR:0x%08X\r\n", p->xPSR);
  128. SEGGER_RTT_printf(0,"system reset...\r\n");
  129. }
  130. static void Exception_Led_Process(void);
  131. static void Except_Led_Close(void)
  132. {
  133. //全功率关闭
  134. Process_SetHoldOn(Exception_Led_Process,0);
  135. ob_exception.led_switch = EXCEPT_LED_OFF;
  136. ob_exception.led_display_time = EXCEPT_LED_DISPLAY_TIME/LED_PROCESS_CYCLE;
  137. ob_exception.led_toggle_time = EXCEPT_LED_TOGGLE_TIME/LED_PROCESS_CYCLE;
  138. LED_Stop(LED_EXCEPT);
  139. }
  140. static void Except_Led_OpenOnce(void)
  141. {
  142. //异常灯显示/关闭
  143. if(ob_exception.led_display_time != 0)
  144. {
  145. //全功率开启
  146. Process_SetHoldOn(Exception_Led_Process,1);
  147. if(ob_exception.led_toggle_time == 0)
  148. {
  149. if(ob_exception.led_switch == EXCEPT_LED_ON)ob_exception.led_switch = EXCEPT_LED_OFF;
  150. else ob_exception.led_switch = EXCEPT_LED_ON;
  151. ob_exception.led_toggle_time = EXCEPT_LED_TOGGLE_TIME/LED_PROCESS_CYCLE;
  152. }
  153. else
  154. {
  155. if(ob_exception.led_switch == EXCEPT_LED_ON)LED_Start(LED_EXCEPT,COLOR_ORANGE);
  156. if(ob_exception.led_switch == EXCEPT_LED_OFF)LED_Start(LED_EXCEPT,COLOR_BLACK);
  157. ob_exception.led_toggle_time--;
  158. }
  159. ob_exception.led_display_time--;
  160. }
  161. }
  162. static void Exception_Led_Process(void)
  163. {
  164. int16_t f_acc[3];
  165. ser_imu_data_t data;
  166. //读取ACC值
  167. if(hal_ser_imu_mode_manage_get_data_num(SER_IMU_DIR_FRONT) >= 1){
  168. hal_ser_imu_mode_manage_get_data(SER_IMU_DIR_FRONT, 0, &data);
  169. f_acc[0] = data.acc[0];f_acc[1] = data.acc[1];f_acc[2] = data.acc[2];
  170. }
  171. //把鞋子倒转平放,且存在异常
  172. // SEGGER_RTT_printf(0,"Exception_Led_Process:%d,%d,%d\n",f_acc[0],f_acc[1],f_acc[2]);
  173. if(f_acc[2] >= EXCEPT_LED_ON_ACC_Z && ob_exception.except_number_of_exist > 0)
  174. {
  175. Except_Led_OpenOnce();
  176. }//把鞋子正放
  177. else if(f_acc[2] <= EXCEPT_LED_OFF_ACC_Z)
  178. {
  179. Except_Led_Close();
  180. }
  181. }
  182. static void Exception_UnkownReset_Info_Send_Process(void)
  183. {
  184. char buf[255];
  185. for(int i = 0; i < EXCEPT_UNKOWN_RESET_TRIGGER_NUMS; i++)
  186. {
  187. if(no_init_data.unkown_reset_info[i].is_upload == true)
  188. {
  189. switch(no_init_data.unkown_reset_info[i].trigger)
  190. {
  191. case EXCEPT_UNKOWN_RESET_TRIGGER_WDT:
  192. sprintf(buf,"id:%d\r\n",no_init_data.unkown_reset_info[i].cur_process_id);
  193. if(Except_TxError(EXCEPT_UNKOWN_RESET,buf) == 0){no_init_data.unkown_reset_info[i].is_upload = false;}
  194. break;
  195. case EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT:
  196. sprintf(buf,"id:%d,R:0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x\r\n", no_init_data.unkown_reset_info[i].cur_process_id, \
  197. no_init_data.unkown_reset_info[i].stack.R0, \
  198. no_init_data.unkown_reset_info[i].stack.R1, \
  199. no_init_data.unkown_reset_info[i].stack.R2, \
  200. no_init_data.unkown_reset_info[i].stack.R3, \
  201. no_init_data.unkown_reset_info[i].stack.R12, \
  202. no_init_data.unkown_reset_info[i].stack.LR, \
  203. no_init_data.unkown_reset_info[i].stack.PC - 3, \
  204. no_init_data.unkown_reset_info[i].stack.xPSR);
  205. if(Except_TxError(EXCEPT_UNKOWN_RESET,buf) == 0){no_init_data.unkown_reset_info[i].is_upload = false;}
  206. break;
  207. default:
  208. break;
  209. }
  210. if(no_init_data.unkown_reset_info[i].trigger == EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT)
  211. {
  212. SEGGER_RTT_printf(0,"R0:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.R0);
  213. SEGGER_RTT_printf(0,"R1:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.R1);
  214. SEGGER_RTT_printf(0,"R2:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.R2);
  215. SEGGER_RTT_printf(0,"R3:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.R3);
  216. SEGGER_RTT_printf(0,"R12:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.R12);
  217. SEGGER_RTT_printf(0,"LR:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.LR);
  218. SEGGER_RTT_printf(0,"PC:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.PC - 3);
  219. SEGGER_RTT_printf(0,"xPSR:0x%08X\r\n", no_init_data.unkown_reset_info[i].stack.xPSR);
  220. SEGGER_RTT_printf(0,"id:%d\r\n", no_init_data.unkown_reset_info[i].cur_process_id);
  221. no_init_data.battery_record_buff[0] = 300;
  222. }
  223. else
  224. {
  225. SEGGER_RTT_printf(0,"id:%d\r\n", no_init_data.unkown_reset_info[i].cur_process_id);
  226. no_init_data.battery_record_buff[0] = 300;
  227. }
  228. }
  229. }
  230. }
  231. /*API ----------------------------------------------*/
  232. /**
  233. @brief 注册接收右鞋错误信息的回调
  234. @param Handle
  235. @return 无
  236. */
  237. void cb_BLE_Host_R_ERR(void* handle)
  238. {
  239. BLE_Host_Rx_t* target = handle;
  240. BLE_Client_Tx_Send(0,BLE_ERR,target->pDat,target->datLen);
  241. }
  242. void Exception_Init(void)
  243. {
  244. // memset(&no_init_data, 0 , sizeof(No_Init_t));
  245. memset(&ob_exception, 0 , sizeof(ob_exception));
  246. ob_exception.led_switch = EXCEPT_LED_OFF;
  247. ob_exception.led_display_time = EXCEPT_LED_DISPLAY_TIME/LED_PROCESS_CYCLE;
  248. ob_exception.led_toggle_time = EXCEPT_LED_TOGGLE_TIME/LED_PROCESS_CYCLE;
  249. Process_Start(LED_PROCESS_CYCLE,"Exception_Led_Process",Exception_Led_Process);
  250. Process_Start(UNKOWN_RESET_INFO_SEND_CYCLE,"Exception_UnkownReset_Info_Send_Process",Exception_UnkownReset_Info_Send_Process);
  251. BLE_Host_Rx_Regist(BLE_ERR,cb_BLE_Host_R_ERR);
  252. SEGGER_RTT_printf(0,"except--------------sizeof(no_init_data):%d!!!!!!\r\n", sizeof(no_init_data));
  253. SEGGER_RTT_printf(0,"except--------------no_init_data.battery_record_buff[0]:%d!!!!!!\r\n", no_init_data.battery_record_buff[0]);
  254. SEGGER_RTT_printf(0,"except--------------&no_init_data.battery_record_buff[0]:0x%x!!!!!!\r\n", &no_init_data.battery_record_buff[0]);
  255. }
  256. /**
  257. @brief 系统错误回调
  258. @param id:
  259. @return 无
  260. */
  261. void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
  262. {
  263. Flash_SaveLog(id,pc,info);
  264. SEGGER_RTT_printf(0,"app_error_fault_handler,System reset\n");
  265. NVIC_SystemReset(); //复位重启
  266. }
  267. int Except_TxError(ExcepType_t excep_type,const char *errstr){
  268. uint8_t buf[250];
  269. uint8_t L=0;
  270. uint16_t err_strlen =0;
  271. err_strlen = strlen(errstr);
  272. if(err_strlen > (sizeof(buf)-2))return -1;
  273. if(mFlash.isHost)buf[L++] =0;//0左鞋1右鞋
  274. else buf[L++] =1;
  275. buf[L++] = excep_type; //错误编号
  276. memcpy(&buf[L],errstr,err_strlen);
  277. L +=err_strlen;
  278. return BLE_Client_Send(BLE_ERR,buf,L);
  279. }
  280. void Except_SetExceptype(ExcepType_t excep_type)
  281. {
  282. if(ob_exception.except_type[excep_type] != 1)
  283. {
  284. ob_exception.except_type[excep_type] = 1;
  285. ob_exception.except_number_of_exist++;
  286. }
  287. }
  288. void Except_ClearExceptype(ExcepType_t excep_type)
  289. {
  290. if(ob_exception.except_type[excep_type] == 1)
  291. {
  292. ob_exception.except_type[excep_type] = 0;
  293. if(ob_exception.except_number_of_exist != 0)ob_exception.except_number_of_exist--;
  294. }
  295. }
  296. bool Except_IsError(ExcepType_t excep_type)
  297. {
  298. if(ob_exception.except_type[excep_type] == 1)
  299. {
  300. return true;
  301. }
  302. return false;
  303. }
  304. bool Except_IsErrorExist(void)
  305. {
  306. if(ob_exception.except_number_of_exist > 0)return true;
  307. return false;
  308. }
  309. //硬件中断
  310. void HardFault_Handler (void)
  311. {
  312. uint32_t msp_addr = __get_MSP(); //获取线程模式下堆栈指针位置
  313. uint32_t psp_addr = __get_PSP(); //获取中断下的堆栈指针位置-用于OS启动后
  314. #if(UCOS_II_EN) //使能了操作系统的一个宏定义,自己去定义
  315. if(SYS_GetOsStartup()) //操作系统运行了-自己定义一个状态,可以获取操作系统是否启动
  316. {
  317. Except_OSHardFault_Handler(psp_addr);
  318. }
  319. else
  320. {
  321. Except_NotOSHardFault_Handler(msp_addr);
  322. }
  323. #else //没有使能操作系统
  324. Except_NotOSHardFault_Handler(msp_addr);
  325. #endif //UCOS_II_EN
  326. nrf_delay_ms(10); //延时一下,防止重启速度太快
  327. NVIC_SystemReset(); //复位重启
  328. }
  329. void Except_Get_Cur_Porcess_ID(int cur_process_id)
  330. {
  331. for(int i = 0; i < EXCEPT_UNKOWN_RESET_TRIGGER_NUMS; i++)
  332. {
  333. if(no_init_data.unkown_reset_info[i].is_upload == false)
  334. {
  335. no_init_data.unkown_reset_info[i].cur_process_id = cur_process_id;
  336. }
  337. }
  338. }
  339. void Except_Unkown_Reset_WDT_Set(void)
  340. {
  341. no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_WDT].is_upload = true;
  342. }