exception.c 14 KB

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