123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- #include "nrf_delay.h"
- #include "nrf_strerror.h"
- #include "ble_comm.h"
- #include "exception.h"
- #include "system.h"
- #include "bsp_time.h"
- #include "hal_led.h"
- #include "hal_ble_client.h"
- #include "hal_ble_host.h"
- #include "app_flash.h"
- #include "app_error.h"
- #include "app_client.h"
- #include "app_flash.h"
- #include "app_util_platform.h"
- #include "bll_imu.h"
- #include "app_detectIsHost.h"
- #define EXCEPT_LED_ON_ACC_Z 1500
- #define EXCEPT_LED_OFF_ACC_Z -1500
- #define LED_PROCESS_CYCLE 200
- #define EXCEPT_LED_TOGGLE_TIME 200
- #define EXCEPT_LED_DISPLAY_TIME 10000
- #define UNKOWN_RESET_INFO_SEND_CYCLE 100
- #define __NO_INIT__ __attribute__((at(0x2000FFA0)))
- typedef struct
- {
- uint32_t R0;
- uint32_t R1;
- uint32_t R2;
- uint32_t R3;
- uint32_t R12;
- uint32_t LR;
- uint32_t PC;
- uint32_t xPSR;
- }STACK_DATA_TYPE;
- typedef enum{
- EXCEPT_LED_ON,
- EXCEPT_LED_OFF,
- } EXCEPT_LED_SWITCH_e;
- typedef enum{
- EXCEPT_UNKOWN_RESET_TRIGGER_WDT = 0,
-
- EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT,
-
- EXCEPT_UNKOWN_RESET_TRIGGER_NUMS,
-
- } EXCEPT_UNKOWN_RESET_TRIGGER_e;
- typedef struct _except_unkown_reset_info
- {
- STACK_DATA_TYPE stack;
- uint32_t cur_process_id;
- EXCEPT_UNKOWN_RESET_TRIGGER_e trigger;
- bool is_upload;
-
- } Except_Unkown_Reset_Info_t;
- typedef struct no_init
- {
- Except_Unkown_Reset_Info_t unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_NUMS];
- } No_Init_t;
- typedef struct exception{
-
-
- EXCEPT_LED_SWITCH_e led_switch;
-
- uint16_t led_display_time;
-
- uint16_t led_toggle_time;
-
- uint8_t except_type[EXCEPT_NUM];
-
- uint16_t except_number_of_exist;
-
- } Exception_t;
- static Exception_t ob_exception;
- static volatile __NO_INIT__ No_Init_t no_init_data;
- battercb_t battery_record={0};
- static void Except_NotOSHardFault_Handler(uint32_t msp_addr)
- {
- STACK_DATA_TYPE *p;
-
- msp_addr -= 4;
- DEBUG_LOG("\r\n-----------------ERROR -----------------\r\nHardFault_Handler\r\n");
-
-
- if((msp_addr >> 20) != 0x200)
- {
- DEBUG_LOG("Warning: stack pointer is damaged, unable to record the scene!\r\n");
- return;
- }
-
-
- msp_addr += 8;
-
- no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT].is_upload = true;
-
- no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT].trigger = EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT;
-
- memcpy(&no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT].stack, (STACK_DATA_TYPE *)msp_addr, sizeof(STACK_DATA_TYPE));
-
- p = (STACK_DATA_TYPE *)msp_addr;
- p->PC -= 3;
- }
- static void Exception_Led_Process(void);
- static void Except_Led_Close(void)
- {
-
- Process_SetHoldOn(Exception_Led_Process,0);
- ob_exception.led_switch = EXCEPT_LED_OFF;
- ob_exception.led_display_time = EXCEPT_LED_DISPLAY_TIME/LED_PROCESS_CYCLE;
- ob_exception.led_toggle_time = EXCEPT_LED_TOGGLE_TIME/LED_PROCESS_CYCLE;
- LED_Stop(LED_EXCEPT);
- }
- static void Except_Led_OpenOnce(void)
- {
-
- if(ob_exception.led_display_time != 0)
- {
-
- Process_SetHoldOn(Exception_Led_Process,1);
-
- if(ob_exception.led_toggle_time == 0)
- {
- if(ob_exception.led_switch == EXCEPT_LED_ON)ob_exception.led_switch = EXCEPT_LED_OFF;
- else ob_exception.led_switch = EXCEPT_LED_ON;
-
- ob_exception.led_toggle_time = EXCEPT_LED_TOGGLE_TIME/LED_PROCESS_CYCLE;
- }
- else
- {
- if(ob_exception.led_switch == EXCEPT_LED_ON)LED_Start(LED_EXCEPT,COLOR_ORANGE);
- if(ob_exception.led_switch == EXCEPT_LED_OFF)LED_Start(LED_EXCEPT,COLOR_BLACK);
- ob_exception.led_toggle_time--;
- }
-
- ob_exception.led_display_time--;
- }
- }
- static void Exception_Led_Process(void)
- {
- int16_t f_acc[3];
- bll_imu_data_t data;
-
- if(bll_imu_get_data_num(BLL_IMU_DIR_FRONT) >= 1){
- bll_imu_get_data(BLL_IMU_DIR_FRONT, 0, &data);
- f_acc[0] = data.acc[0];f_acc[1] = data.acc[1];f_acc[2] = data.acc[2];
- }
- else return;
-
-
- if(f_acc[2] >= EXCEPT_LED_ON_ACC_Z && ob_exception.except_number_of_exist > 0)
- {
- Except_Led_OpenOnce();
- }
- else if(f_acc[2] <= EXCEPT_LED_OFF_ACC_Z)
- {
- Except_Led_Close();
- }
- }
- static void Exception_UnkownReset_Info_Send_Process(void)
- {
- char buf[250]={0};
- memset(buf,0,sizeof(buf));
-
-
- for(int i = 0; i < EXCEPT_UNKOWN_RESET_TRIGGER_NUMS; i++)
- {
- if(no_init_data.unkown_reset_info[i].is_upload == true)
- {
- switch(no_init_data.unkown_reset_info[i].trigger)
- {
- case EXCEPT_UNKOWN_RESET_TRIGGER_WDT:
- sprintf(buf,"id:%d\r\n",no_init_data.unkown_reset_info[i].cur_process_id);
- if(Except_TxError(EXCEPT_UNKOWN_RESET,buf) == 0){no_init_data.unkown_reset_info[i].is_upload = false;}
- break;
-
- case EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT:
- 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, \
- no_init_data.unkown_reset_info[i].stack.R0, \
- no_init_data.unkown_reset_info[i].stack.R1, \
- no_init_data.unkown_reset_info[i].stack.R2, \
- no_init_data.unkown_reset_info[i].stack.R3, \
- no_init_data.unkown_reset_info[i].stack.R12, \
- no_init_data.unkown_reset_info[i].stack.LR, \
- no_init_data.unkown_reset_info[i].stack.PC - 3, \
- no_init_data.unkown_reset_info[i].stack.xPSR,
- HARDWARE_VERSION,SOFTWARE_VERSION);
- if(Except_TxError(EXCEPT_UNKOWN_RESET,buf) == 0){no_init_data.unkown_reset_info[i].is_upload = false;}
- break;
-
- default:
- no_init_data.unkown_reset_info[i].is_upload = false;
- break;
- }
-
- if(no_init_data.unkown_reset_info[i].trigger == EXCEPT_UNKOWN_RESET_TRIGGER_RUN_OUT)
- {
- }
- else
- {
- }
- }
- }
- }
- void cb_BLE_Host_R_ERR(void* handle)
- {
- BLE_Host_Rx_t* target = handle;
- BLE_Client_Tx_Send(0,BLE_ERR,target->pDat,target->datLen);
- }
- void Exception_Init(void)
- {
- memset(&ob_exception, 0 , sizeof(ob_exception));
- ob_exception.led_switch = EXCEPT_LED_OFF;
- ob_exception.led_display_time = EXCEPT_LED_DISPLAY_TIME/LED_PROCESS_CYCLE;
- ob_exception.led_toggle_time = EXCEPT_LED_TOGGLE_TIME/LED_PROCESS_CYCLE;
-
- Process_Start(LED_PROCESS_CYCLE,"Exception_Led_Process",Exception_Led_Process);
-
- Process_Start(UNKOWN_RESET_INFO_SEND_CYCLE,"Exception_UnkownReset_Info_Send_Process",Exception_UnkownReset_Info_Send_Process);
-
- BLE_Host_Rx_Regist(BLE_ERR,cb_BLE_Host_R_ERR);
-
-
- memcpy(&battery_record,&mFlash.mbattercb_t,sizeof(battercb_t));
-
- extern void printbatter_cb(battercb_t *c,battercb_t *C_flash);
-
-
- }
- void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
- {
- Flash_SaveLog(id,pc,info);
- DEBUG_LOG("app_error_fault_handler,System reset\n");
- NVIC_SystemReset();
- }
- int Except_TxError(ExcepType_t excep_type,const char *errstr){
-
- uint8_t buf[250];
- uint8_t L=0;
- uint16_t err_strlen =0;
- err_strlen = strlen(errstr);
- if(err_strlen > (sizeof(buf)-2))return -1;
- if(app_Get_isHost())buf[L++] =0;
- else buf[L++] =1;
- buf[L++] = excep_type;
-
- memcpy(&buf[L],errstr,err_strlen);
- L +=err_strlen;
-
- return BLE_Client_Send(BLE_ERR,buf,L);
- }
- void Except_SetExceptype(ExcepType_t excep_type)
- {
- if(ob_exception.except_type[excep_type] != 1)
- {
- ob_exception.except_type[excep_type] = 1;
- ob_exception.except_number_of_exist++;
- }
- }
- void Except_ClearExceptype(ExcepType_t excep_type)
- {
- if(ob_exception.except_type[excep_type] == 1)
- {
- ob_exception.except_type[excep_type] = 0;
- if(ob_exception.except_number_of_exist != 0)ob_exception.except_number_of_exist--;
- }
- }
- bool Except_IsError(ExcepType_t excep_type)
- {
- if(ob_exception.except_type[excep_type] == 1)
- {
- return true;
- }
-
- return false;
- }
- bool Except_IsErrorExist(void)
- {
- if(ob_exception.except_number_of_exist > 0)return true;
-
- return false;
- }
- void HardFault_Handler (void)
- {
- uint32_t msp_addr = __get_MSP();
- uint32_t psp_addr = __get_PSP();
-
- #if(UCOS_II_EN)
- if(SYS_GetOsStartup())
- {
- Except_OSHardFault_Handler(psp_addr);
- }
- else
- {
- Except_NotOSHardFault_Handler(msp_addr);
- }
- #else
- Except_NotOSHardFault_Handler(msp_addr);
- #endif
-
- nrf_delay_ms(10);
-
- NVIC_SystemReset();
- }
- void Except_Get_Cur_Porcess_ID(uint32_t cur_process_id)
- {
- for(int i = 0; i < EXCEPT_UNKOWN_RESET_TRIGGER_NUMS; i++)
- {
- if(no_init_data.unkown_reset_info[i].is_upload == false)
- {
- no_init_data.unkown_reset_info[i].cur_process_id = cur_process_id;
- }
- }
- }
- void Except_Unkown_Reset_WDT_Set(void)
- {
- no_init_data.unkown_reset_info[EXCEPT_UNKOWN_RESET_TRIGGER_WDT].is_upload = true;
- }
- battercb_t* Except_Get_Battery_Record_Buff(void)
- {
- return &battery_record;
- }
|