123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- /****************************************************************************
- *
- * 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 <cis_def.h>
- #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;
- uint8_t Uart_Send_reg[Uart_Send_LEN];
- uint8_t Uart_Rece_CRCbuffer[Uart_Rece_LEN];
- /** \brief 电压传输 */
- UINT8 *Uart_Rece_BattCellU1_U4;
- UINT8 *Uart_Rece_BattCellU5_U8;
- UINT8 *Uart_Rece_BattCellU9_U12;
- UINT8 *Uart_Rece_BattCellU13_U16;
- UINT8 *Uart_Rece_BattT;
- volatile bool Can_Task_Flag = false;
- volatile bool Uart_Task_Flag = false;
- typedef enum
- {
- PROCESS_STATE_IDLE = 0,
- PROCESS_STATE_WORK,
- 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;
- }
- uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg)
- {
- volatile uint16_t CRC_chk_Sebuffer;
- volatile uint16_t CRC_chk_Rebuffer;
- uint8_t Uart_Send_buffer[Uart_Send_LEN];
- uint8_t Rece_Data_Len;
- static uint8_t Uart_Rece_buffer[Uart_Rece_LEN];
- uint8_t Uart_Rece_Out[Uart_Rece_LEN];
- Uart_Send_buffer[0] = Uart_Receive_Msg.Bms_Address;
- Uart_Send_buffer[1] = Uart_Receive_Msg.Bms_Read_Funcode;
- Uart_Send_buffer[2] = Uart_Receive_Msg.Reg_Begin_H;
- Uart_Send_buffer[3] = Uart_Receive_Msg.Reg_Begin_L;
- Uart_Send_buffer[4] = Uart_Receive_Msg.Reg_Num_H;
- Uart_Send_buffer[5] = Uart_Receive_Msg.Reg_Num_L;
- CRC_chk_Sebuffer = crc_chk(Uart_Send_buffer,6);
- Uart_Send_buffer[6] = CRC_chk_Sebuffer;
- Uart_Send_buffer[7] = CRC_chk_Sebuffer>>8;
- USARTdrv->Send(Uart_Send_buffer,Uart_Send_LEN);
- USARTdrv->Receive(Uart_Rece_buffer,Uart_Rece_LEN);
- Rece_Data_Len = Uart_Rece_buffer[2]+3;
- for(uint8_t i =0;i<Rece_Data_Len;i++)
- {
- Uart_Rece_CRCbuffer[i] = Uart_Rece_buffer[i];
- }
- CRC_chk_Rebuffer = crc_chk(Uart_Rece_CRCbuffer,Rece_Data_Len);
- if (Uart_Rece_buffer[Rece_Data_Len+1]==CRC_chk_Rebuffer && Uart_Rece_buffer[Rece_Data_Len+2]==CRC_chk_Rebuffer>>8)
- {
- for (uint8_t i = 0; i <Uart_Rece_buffer[2]; i++)
- {
- Uart_Rece_Out[i]=Uart_Rece_buffer[i+3];
- }
- return Uart_Rece_Out;
- }
- else
- {
- for (uint8_t i = 0; i <Uart_Rece_buffer[2]; i++)
- {
- Uart_Rece_Out[i]=0x00;
- }
- return Uart_Rece_Out;
- }
-
- //接收数据的校验
- }
- 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)
- {
- UINT8 Can_index = 0;
- UINT8 Uart_index = 0;
- Can_Task_Flag = false;
- Uart_Task_Flag = false;
- for (uint8_t i = 0; i <=10000; i++)//100s
- {
- osDelay(10/portTICK_PERIOD_MS);//10ms
- Can_index++;
- Uart_index++;
- if (Uart_index >=50)//Uart 50ms 调用一次
- {
- Uart_Task_Flag = true;
- Uart_index = 0;
- }
- if (Can_index >=100)//Can 100ms 调用一次
- {
- Can_Task_Flag = true;
- Can_index = 0;
- }
-
- }
-
- }
- }
- 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);
- int send_index = 0;
- while(1)
- {
- switch(gProcState)
- {
- case PROCESS_STATE_IDLE:
- {
- if(Can_Task_Flag)
- {
- PROC_STATE_SWITCH(PROCESS_STATE_WORK);
- FaultDisplay(LED_TURN_ON);
- }
- break;
- }
- case PROCESS_STATE_WORK:
- {
- switch(send_index)
- {
- case 0:
- {
- Can_ID = 0x000;
- Can_TxMsg.Data[0] = send_index;
- Can_TxMsg.Data[1] = 0x00;
- Can_TxMsg.Data[2] = 0x00;
- Can_TxMsg.Data[3] = 0x00;
- Can_TxMsg.Data[4] = 0x00;
- Can_TxMsg.Data[5] = 0x00;
- Can_TxMsg.Data[6] = 0x00;
- Can_TxMsg.Data[7] = 0x00;
- break;
- }
- case 1:
- {
- Can_ID = 0x001;
- Can_TxMsg.Data[0] = send_index;
- Can_TxMsg.Data[1] = 0x00;
- Can_TxMsg.Data[2] = 0x00;
- Can_TxMsg.Data[3] = 0x00;
- Can_TxMsg.Data[4] = 0x00;
- Can_TxMsg.Data[5] = 0x00;
- Can_TxMsg.Data[6] = 0x00;
- Can_TxMsg.Data[7] = 0x00;
- break;
- }
- case 2:
- {
- Can_ID = 0x002;
- Can_TxMsg.Data[0] = send_index;
- Can_TxMsg.Data[1] = 0x00;
- Can_TxMsg.Data[2] = 0x00;
- Can_TxMsg.Data[3] = 0x00;
- Can_TxMsg.Data[4] = 0x00;
- Can_TxMsg.Data[5] = 0x00;
- Can_TxMsg.Data[6] = 0x00;
- Can_TxMsg.Data[7] = 0x00;
- break;
- }
- case 3:
- {
- Can_ID = 0x003;
- Can_TxMsg.Data[0] = send_index;
- Can_TxMsg.Data[1] = 0x00;
- Can_TxMsg.Data[2] = 0x00;
- Can_TxMsg.Data[3] = 0x00;
- Can_TxMsg.Data[4] = 0x00;
- Can_TxMsg.Data[5] = 0x00;
- Can_TxMsg.Data[6] = 0x00;
- Can_TxMsg.Data[7] = 0x00;
- break;
- }
- case 4:
- {
- Can_ID = 0x004;
- Can_TxMsg.Data[0] = send_index;
- Can_TxMsg.Data[1] = 0x00;
- Can_TxMsg.Data[2] = 0x00;
- Can_TxMsg.Data[3] = 0x00;
- Can_TxMsg.Data[4] = 0x00;
- Can_TxMsg.Data[5] = 0x00;
- Can_TxMsg.Data[6] = 0x00;
- Can_TxMsg.Data[7] = 0x00;
- break;
- }
- }
- Can_TxMsg.stdIDH = Can_ID>>3;
- Can_TxMsg.stdIDL = Can_ID<<5;
- Can_TxMsg.DLC = 8;
- HAL_Can_Transmit(Can_TxMsg);
- send_index++;
- if (send_index >4)
- {
- send_index = 0;
- FaultDisplay(LED_TURN_OFF);
- Can_Task_Flag = false;
- PROC_STATE_SWITCH(PROCESS_STATE_IDLE);
- }
- break;
- }
- case PROCESS_STATE_SLEEP:
- {
- 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;
- Uart_Receive_Type Uart_Receive_Msg;
- PROC_STATE_SWITCH(PROCESS_STATE_IDLE);
- while(1)
- {
- switch(gProcState)
- {
- case PROCESS_STATE_IDLE:
- {
- if(Uart_Task_Flag)
- {
- NetSocDisplay(3,LED_TURN_ON);
- Rece_index = 0;
- PROC_STATE_SWITCH(PROCESS_STATE_WORK);
- }
- break;
- }
- case PROCESS_STATE_WORK:
- {
- Uart_Receive_Msg.Bms_Address = 0x01;
- Uart_Receive_Msg.Bms_Read_Funcode = 0x03;
- switch(Rece_index)
- {
- case 0://读取电压1-4
- {
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
- Uart_Receive_Msg.Reg_Begin_L= 0x02;
- Uart_Receive_Msg.Reg_Num_H = 0x00;
- Uart_Receive_Msg.Reg_Num_L = 0x04;
- Uart_Rece_BattCellU1_U4 = Uart_Receive_func(Uart_Receive_Msg);
- break;
- }
- case 1://读取电压5-8
- {
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
- Uart_Receive_Msg.Reg_Begin_L= 0x06;
- Uart_Receive_Msg.Reg_Num_H = 0x00;
- Uart_Receive_Msg.Reg_Num_L = 0x04;
- Uart_Rece_BattCellU5_U8 = Uart_Receive_func(Uart_Receive_Msg);
- break;
- }
- case 2://读取温度
- {
- Uart_Receive_Msg.Reg_Begin_H = 0x00;
- Uart_Receive_Msg.Reg_Begin_L= 0x14;
- Uart_Receive_Msg.Reg_Num_H = 0x00;
- Uart_Receive_Msg.Reg_Num_L = 0x05;
- Uart_Rece_BattT = Uart_Receive_func(Uart_Receive_Msg);
- break;
- }
-
- }
- Rece_index++;
- if (Rece_index>3)
- {
- Rece_index=0;
- Uart_Task_Flag = false;
- NetSocDisplay(3,LED_TURN_OFF);
- PROC_STATE_SWITCH(PROCESS_STATE_IDLE);
- }
- 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);
- }
|