exception - 副本.c 13 KB

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