/**************************************************************************** * * Copy right: 2020-, Copyrigths of QIXIANG TECH Ltd. * File name: app.c * Description: QX app source file * History: Rev1.0 2020-10-16 * Athuor: chenjie * ****************************************************************************/ //include #include "bsp.h" #include "bsp_custom.h" #include "osasys.h" #include "ostask.h" #include "queue.h" #include "ps_event_callback.h" #include "app.h" #include "cmisim.h" #include "cmimm.h" #include "cmips.h" #include "sockets.h" #include "psifevent.h" #include "ps_lib_api.h" #include "lwip/netdb.h" #include #include "debug_log.h" #include "slpman_ec616.h" #include "plat_config.h" //define // app task static stack and control block #define PROC_TASK_STACK_SIZE (1024) //uart def #define Uart_Send_LEN (8) #define Uart_Rece_LEN (16) #define RTE_UART_RX_IO_MODE RTE_UART1_RX_IO_MODE //statement variable extern ARM_DRIVER_USART Driver_USART1; static ARM_DRIVER_USART *USARTdrv = &Driver_USART1; /** \brief usart receive buffer */ uint8_t Uart_Data_buffer[8]; /** \brief usart send buffer */ /** \brief receive timeout flag */ volatile bool isRecvTimeout = false; /** \brief receive complete flag */ volatile bool isRecvComplete = false; uint8_t process0SlpHandler = 0xff; uint8_t process1SlpHandler = 0xff; uint8_t process2SlpHandler = 0xff; uint8_t deepslpTimerID = 7; /** \brief 电压传输 */ uint8_t Uart_Rece_BattCellU1_U4[8]; uint8_t Uart_Rece_BattCellU5_U8[8]; uint8_t Uart_Rece_BattCellU9_U12[8]; uint8_t Uart_Rece_BattCellU13_U14[8]; uint8_t Uart_Rece_BattT[8]; int16_t Uart_Rece_BattI=0x0000; uint8_t Uart_Rece_Batt_states[6]; uint16_t Uart_Rece_BattU; uint16_t Uart_Rece_Batt_MaxcellU; uint16_t Uart_Rece_Batt_MincellU; uint8_t Can_Rece_buffer[8]; typedef enum { PROCESS_STATE_IDLE = 0, PROCESS_STATE_WORK, PROCESS_STATE_SLEEP }processSM; typedef enum { PROCESS_Uart_STATE_IDLE = 0, PROCESS_Uart_STATE_WORK, PROCESS_Uart_STATE_CHECK, PROCESS_Uart_STATE_SLEEP }process_Uart; static StaticTask_t gProcessTask0; static UINT8 gProcessTaskStack0[PROC_TASK_STACK_SIZE]; static StaticTask_t gProcessTask1; static UINT8 gProcessTaskStack1[PROC_TASK_STACK_SIZE]; static StaticTask_t gProcessTask2; static UINT8 gProcessTaskStack2[PROC_TASK_STACK_SIZE]; static StaticTask_t gProcessTask3; static UINT8 gProcessTaskStack3[PROC_TASK_STACK_SIZE]; processSM gProc0State = PROCESS_STATE_IDLE; process_Uart gProc1State = PROCESS_Uart_STATE_IDLE; processSM gProc2State = PROCESS_STATE_IDLE; processSM gProc3State = PROCESS_STATE_IDLE; #define PROC_Task_STATE_SWITCH(a) (gProc0State = a) //任务调度切换 #define PROC_Uart_STATE_SWITCH(a) (gProc1State = a) //uart 状态切换 #define PROC_Can_STATE_SWITCH(a) (gProc2State = a) //can 状态切换 #define PROC_NB_STATE_SWITCH(a) (gProc3State = a) //can 状态切换 unsigned int crc_chk(uint8_t* data, uint8_t length) { int j; uint16_t reg_crc=0xFFFF; while(length--) { reg_crc ^= *data++; for(j=0;j<8;j++) { if(reg_crc & 0x01) { reg_crc=(reg_crc>>1) ^ 0xA001; } else { reg_crc=reg_crc >>1; } } } return reg_crc; } uint8_t bcc_chk(uint8_t* data, uint8_t length) { uint8_t bcc_chk_return = 0x00; uint8_t count = 0; while (count=100) { NetSocDisplay(0,LED_TURN_OFF); sleep_index=0; PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP); } break; } case PROCESS_STATE_WORK: { osDelay(10);//10ms Can_index++; Uart_index++; if (Uart_index >10)//Uart 100ms 调用一次 { PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_WORK); Uart_index = 0; } if (Can_index >=100)//Can 1000ms 调用一次 { PROC_Can_STATE_SWITCH(PROCESS_STATE_WORK); PROC_NB_STATE_SWITCH(PROCESS_STATE_WORK); Can_index = 0; } if((Uart_Rece_BattI==0x0000)&&(Can_Rece_buffer[0]==0xff)) { sleep_index++; } else { sleep_index = 0; } if (sleep_index >=6000) { PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP); sleep_index = 0; } break; } case PROCESS_STATE_SLEEP: { slpManSlpState_t State; uint8_t cnt; PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_SLEEP); PROC_Can_STATE_SWITCH(PROCESS_STATE_SLEEP); slpManPlatVoteEnableSleep(process0SlpHandler,SLP_SLP2_STATE); slpManPlatVoteDisableSleep(process0SlpHandler, SLP_HIB_STATE); slpManDeepSlpTimerStart(deepslpTimerID, 300000); // create a 10s timer, DeepSleep Timer is always oneshoot while(1) // now app can enter hib, but ps and phy maybe not, so wait here { osDelay(3000); } } } } } static void process3AppTask(void* arg) { CHAR serverip[] = "47.97.127.222"; UINT16 serverport = 8712; int TcpConnectID = -1; int TcpSendID = -1; OsaUtcTimeTValue timestracture; uint8_t TcpSendBuffer[92] = {0x23,0x23,0x02,0xFE,0x47,0x59,0x54,0x45,0x53,0x54, 0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30, 0x32,0x01,0x00,0x43,0x14,0x0B,0x07,0x0E,0x39,0x37, 0x80,0x14,0x0B,0x07,0x0E,0x39,0x37,0x1A,0x00,0x00, 0x00,0x00,0x00,0x02,0x15,0x02,0x15,0x03,0x43,0x64, 0x00,0x00,0x00,0x00,0x0E,0x0E,0x53,0x0E,0x53,0x0E, 0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E, 0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E,0x53,0x0E, 0x53,0x0E,0x53,0x05,0x13,0x13,0x13,0x13,0x13,0x00, 0x00,0x00}; PROC_NB_STATE_SWITCH(PROCESS_STATE_IDLE); while(1) { switch(gProc3State) { case PROCESS_STATE_IDLE: { break; } case PROCESS_STATE_WORK: { while(TcpConnectID<0) { TcpConnectID = tcpipConnectionCreate(1,PNULL,PNULL,serverip,serverport,TcpCallBack); } appGetSystemTimeUtcSync(×tracture); TcpSendBuffer[91] = bcc_chk(TcpSendBuffer,91); TcpSendBuffer[24] = timestracture.UTCtimer1>>16; TcpSendBuffer[24] = TcpSendBuffer[24]-0x07D0; TcpSendBuffer[25] = timestracture.UTCtimer1>>8; TcpSendBuffer[26] = timestracture.UTCtimer1; TcpSendBuffer[27] = timestracture.UTCtimer2>>24; TcpSendBuffer[28] = timestracture.UTCtimer2>>16; TcpSendBuffer[29] = timestracture.UTCtimer2>>8; TcpSendID = tcpipConnectionSend(TcpConnectID,TcpSendBuffer,92,PNULL,PNULL,PNULL); #ifdef USING_PRINTF printf("[%d]TcpConnectID:%d \r\n",__LINE__,TcpConnectID); #endif #ifdef USING_PRINTF printf("[%d]TcpSendID:%d \r\n",__LINE__,TcpSendID); #endif #ifdef USING_PRINTF printf("[%d]Time1:%x TIme2:%x \r\n",__LINE__,timestracture.UTCtimer1,timestracture.UTCtimer2); #endif for (int i = 0; i < 92; i++) { #ifdef USING_PRINTF printf("%x ",TcpSendBuffer[i]); #endif } #ifdef USING_PRINTF printf("\r\n "); #endif PROC_NB_STATE_SWITCH(PROCESS_STATE_IDLE); break; } case PROCESS_STATE_SLEEP: { while(1) { osDelay(3000); } break; } } } } /** \fn process0Init(void) \brief process0Init function. \return */ void process0Init(void) { osThreadAttr_t task_attr; #ifndef USING_PRINTF if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0) { HAL_UART_RecvFlowControl(false); } #endif memset(&task_attr,0,sizeof(task_attr)); memset(gProcessTaskStack0, 0xA5,PROC_TASK_STACK_SIZE); task_attr.name = "Process0AppTask"; task_attr.stack_mem = gProcessTaskStack0; task_attr.stack_size = PROC_TASK_STACK_SIZE; task_attr.priority = osPriorityNormal;//osPriorityBelowNormal; task_attr.cb_mem = &gProcessTask0;//task control block task_attr.cb_size = sizeof(StaticTask_t);//size of task control block osThreadNew(process0AppTask, NULL, &task_attr); } void process3Init(void) { osThreadAttr_t task_attr; memset(&task_attr,0,sizeof(task_attr)); memset(gProcessTaskStack3, 0xA5,PROC_TASK_STACK_SIZE); task_attr.name = "Process3AppTask"; task_attr.stack_mem = gProcessTaskStack3; task_attr.stack_size = PROC_TASK_STACK_SIZE; task_attr.priority = osPriorityNormal;//osPriorityBelowNormal; task_attr.cb_mem = &gProcessTask3;//task control block task_attr.cb_size = sizeof(StaticTask_t);//size of task control block osThreadNew(process3AppTask, NULL, &task_attr); } /** \fn appInit(void) \brief appInit function. \return */ void appInit(void *arg) { process0Init();//任务调度和检测程序 process3Init();//NB程序 } /** \fn int main_entry(void) \brief main entry function. \return */ void main_entry(void) { BSP_CommonInit(); osKernelInitialize(); registerAppEntry(appInit, NULL); if (osKernelGetState() == osKernelReady) { osKernelStart(); } while(1); }