/**************************************************************************** * * 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 RECV_BUFFER_LEN (16) #define SEND_BUFFER_LEN (16) #define Uart_Send_LEN (8) #define Uart_Rece_LEN (32) #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 recBuffer[RECV_BUFFER_LEN]; /** \brief usart send buffer */ uint8_t sendBuffer[RECV_BUFFER_LEN]; /** \brief receive timeout flag */ volatile bool isRecvTimeout = false; /** \brief receive complete flag */ volatile bool isRecvComplete = false; volatile bool Rece_complete_flag = false; uint8_t Uart_Send_buffer[Uart_Send_LEN]; uint8_t Uart_Send_reg[Uart_Send_LEN]; uint8_t Uart_Rece_CRCbuffer[Uart_Rece_LEN]; static UINT8 Uart_BMS_Address = 0x01; static UINT8 Uart_BMS_read_func = 0x03; volatile uint8_t Uart_Send_reg_beginH; volatile uint8_t Uart_Send_reg_beginL; volatile uint8_t Uart_Send_reg_numH; volatile uint8_t Uart_Send_reg_numL; volatile uint16_t CRC_chk_Sebuffer; volatile uint16_t CRC_chk_Rebuffer; volatile uint8_t CAN_stdID; /** \brief 电压传输 */ UINT8 *Uart_Rece_battv1_v4; UINT8 *Uart_Rece_battv5_v8; UINT8 *Uart_Rece_battv8_v12; UINT8 *Uart_Rece_battv12_v16; volatile uint8_t CAN_ID_0[8]; volatile uint8_t CAN_ID_1[8]; volatile uint8_t CAN_ID_2[8]; volatile uint8_t CAN_ID_3[8]; int send_index = 0; typedef enum { PROCESS_STATE_IDLE = 0, PROCESS_STATE_WORK1, PROCESS_STATE_WORK2, PROCESS_STATE_WORK3, PROCESS_STATE_SLEEP }processSM; 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 processSM gProcState = PROCESS_STATE_IDLE; #define PROC_STATE_SWITCH(a) (gProcState = a) unsigned int crc_chk(unsigned char* data, unsigned char length) { int j; unsigned int 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; } void LED_Control_func(Void) { for (int i = 0; i <=3; i++) { NetSocDisplay(i,LED_TURN_ON); osDelay(100/portTICK_PERIOD_MS); } for (int i = 3; i >= 0; i--) { NetSocDisplay(i,LED_TURN_OFF); osDelay(100/portTICK_PERIOD_MS); } } uint8_t* Uart_Receive_func(Void) { static uint8_t Uart_Rece_buffer[Uart_Rece_LEN]; Uart_Send_buffer[0] = Uart_BMS_Address; Uart_Send_buffer[1] = Uart_BMS_read_func; Uart_Send_buffer[2] = Uart_Send_reg_beginH; Uart_Send_buffer[3] = Uart_Send_reg_beginL; Uart_Send_buffer[4] = Uart_Send_reg_numH; Uart_Send_buffer[5] = Uart_Send_reg_numL; CRC_chk_Sebuffer = crc_chk(Uart_Send_buffer,6); Uart_Send_buffer[6] = CRC_chk_Sebuffer; Uart_Send_buffer[7] = CRC_chk_Sebuffer>>8; Uart_Rece_buffer[0]= 0x00; USARTdrv->Send(Uart_Send_buffer,Uart_Send_LEN ); USARTdrv->Receive(Uart_Rece_buffer,Uart_Rece_LEN); for(int i =0;i<11;i++) { Uart_Rece_CRCbuffer[i] = Uart_Rece_buffer[i]; } CRC_chk_Rebuffer = crc_chk(Uart_Rece_CRCbuffer,11); if (Uart_Rece_buffer[11]==CRC_chk_Rebuffer && Uart_Rece_buffer[12]==CRC_chk_Rebuffer>>8) { return Uart_Rece_buffer; } else { NetSocDisplay(3,LED_TURN_OFF); for (int i = 0; i < 16; i++) { Uart_Rece_buffer[i]=0xff; } return Uart_Rece_buffer; } //接收数据的校验 } void USART_callback(uint32_t event) { if(event & ARM_USART_EVENT_RX_TIMEOUT) { isRecvTimeout = true; } if(event & ARM_USART_EVENT_RECEIVE_COMPLETE) { isRecvComplete = true; } } static void process0AppTask(void* arg) { while(1) { uint32_t msDelay = 0; while (1) { for (uint32_t i = 0; i < 70000; i++) {;} msDelay++; } } } static void process1AppTask(void* arg) { PROC_STATE_SWITCH(PROCESS_STATE_IDLE); uint32_t Can_ID; NVIC_EnableIRQ(PadWakeup0_IRQn); Can_InitType param; Can_TxMsgType Can_TxMsg; param.baudrate = CAN_500Kbps; param.mode = REQOP_NORMAL; param.TxStdIDH = 0x00; param.TxStdIDL = 0x00; param.RxStdIDH[0] = 0x00; param.RxStdIDL[0] = 0x00; /*stdid 0000 0000 001x*/ param.RxStdIDH[1] = 0x00; param.RxStdIDL[1] = 0x20; /*stdid 0000 0000 010x */ param.RxStdIDH[2] = 0x00; param.RxStdIDL[2] = 0x40; /*stdid 0000 0000 011x*/ param.RxStdIDH[3] = 0x00; param.RxStdIDL[3] =0x60; /*stdid 0000 0000 100x */ param.RxStdIDH[4] = 0x00; param.RxStdIDL[4] = 0x80; /*stdid 0000 0000 101x*/ param.RxStdIDH[5] = 0x00; param.RxStdIDL[5] =0xa0; param.packType = STD_PACK; HAL_Can_Init(param); while(1) { switch(gProcState) { case PROCESS_STATE_IDLE: { PROC_STATE_SWITCH(PROCESS_STATE_WORK1); break; } case PROCESS_STATE_WORK1: { FaultDisplay(LED_TURN_ON); switch(send_index) { case 0: case 1: case 2: case 3: case 4: case 5: { Can_ID = 0x0009; Can_TxMsg.stdIDH = Can_ID>>3; Can_TxMsg.stdIDL = Can_ID<<5; Can_TxMsg.DLC = 8; Can_TxMsg.Data[0] = *(Uart_Rece_battv1_v4+3); Can_TxMsg.Data[1] = *(Uart_Rece_battv1_v4+4); Can_TxMsg.Data[2] = *(Uart_Rece_battv1_v4+5); Can_TxMsg.Data[3] = *(Uart_Rece_battv1_v4+6); Can_TxMsg.Data[4] = *(Uart_Rece_battv1_v4+7); Can_TxMsg.Data[5] = *(Uart_Rece_battv1_v4+8); Can_TxMsg.Data[6] = *(Uart_Rece_battv1_v4+9); Can_TxMsg.Data[7] = *(Uart_Rece_battv1_v4+10); HAL_Can_Transmit(Can_TxMsg); break; } } send_index++; if (send_index >10) { send_index=0; PROC_STATE_SWITCH(PROCESS_STATE_SLEEP); } break; } case PROCESS_STATE_SLEEP: { FaultDisplay(LED_TURN_OFF); osDelay(100/portTICK_PERIOD_MS); PROC_STATE_SWITCH(PROCESS_STATE_WORK1); break; } } } } static void process2AppTask(void* arg) { USARTdrv->Initialize(USART_callback); USARTdrv->PowerControl(ARM_POWER_FULL); USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_DATA_BITS_8 | ARM_USART_PARITY_NONE | ARM_USART_STOP_BITS_1 | ARM_USART_FLOW_CONTROL_NONE, 9600); uint8_t *chk_point; int Rece_index = 0; PROC_STATE_SWITCH(PROCESS_STATE_IDLE); while(1) { switch(gProcState) { case PROCESS_STATE_IDLE: { PROC_STATE_SWITCH(PROCESS_STATE_WORK1); break; } case PROCESS_STATE_WORK1: { NetSocDisplay(3,LED_TURN_ON); Uart_Send_reg_beginH = 0x00; Uart_Send_reg_numH = 0x00; switch(Rece_index) { case 0: { Uart_Send_reg_beginL = 0x02; Uart_Send_reg_numL = 0x04; chk_point = Uart_Receive_func(); Uart_Rece_battv1_v4 = chk_point; break; } default: { break; } } Rece_index++; if (Rece_index!=0) { Rece_index=0; } PROC_STATE_SWITCH(PROCESS_STATE_WORK2); break; } case PROCESS_STATE_WORK2: { osDelay(100/portTICK_PERIOD_MS); PROC_STATE_SWITCH(PROCESS_STATE_WORK1); break; } case PROCESS_STATE_SLEEP: { //此处写休眠程序 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); } /** \fn process1Init(void) \brief process1Init function. \return */ void process1Init(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(gProcessTaskStack1, 0xA5,PROC_TASK_STACK_SIZE); task_attr.name = "Process1AppTask"; task_attr.stack_mem = gProcessTaskStack1; task_attr.stack_size = PROC_TASK_STACK_SIZE; task_attr.priority = osPriorityNormal;//osPriorityBelowNormal; task_attr.cb_mem = &gProcessTask1;//task control block task_attr.cb_size = sizeof(StaticTask_t);//size of task control block osThreadNew(process1AppTask, NULL, &task_attr); } /** \fn process2Init(void) \brief process2Init function. \return */ void process2Init(void) { osThreadAttr_t task_attr; memset(&task_attr,0,sizeof(task_attr)); memset(gProcessTaskStack2, 0xA5,PROC_TASK_STACK_SIZE); task_attr.name = "Process2AppTask"; task_attr.stack_mem = gProcessTaskStack2; task_attr.stack_size = PROC_TASK_STACK_SIZE; task_attr.priority = osPriorityNormal;//osPriorityBelowNormal; task_attr.cb_mem = &gProcessTask2;//task control block task_attr.cb_size = sizeof(StaticTask_t);//size of task control block osThreadNew(process2AppTask, NULL, &task_attr); } /** \fn appInit(void) \brief appInit function. \return */ void appInit(void *arg) { process0Init(); process1Init(); process2Init(); } /** \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); }