/******************************* * * 骑享程序测试 * * ****************************/ #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 PROC_MAIN_TASK_STACK_SIZE (1024) #define PROC_UART_TASK_STACK_SIZE (1024) #define PROC_CAN_TASK_STACK_SIZE (1024) #define PROC_TCP_TASK_STACK_SIZE (1024) #define APP_EVENT_QUEUE_SIZE (10) uint8_t deepslpTimerID = DEEPSLP_TIMER_ID7; uint8_t MainSlpHandler = 0xff; uint8_t UartSlpHandler = 0xfe; uint8_t CanSlpHandler = 0xfd; uint8_t TcpSlpHandler = 0xfd; #define Uart_Send_LEN (8) #define Uart_Rece_LEN (40) #define RTE_UART_RX_IO_MODE RTE_UART1_RX_IO_MODE extern ARM_DRIVER_USART Driver_USART1; static ARM_DRIVER_USART *USARTdrv = &Driver_USART1; volatile bool isRecvTimeout = false; volatile bool isRecvComplete = false; static volatile UINT32 Event; static QueueHandle_t psEventQueueHandle; static UINT8 gImsi[16] = {0}; static INT32 sockfd = -1; static UINT32 gCellID = 0; uint8_t Batt_Cell_Num = 14;//默认数值14、17 uint8_t Batt_Cell_Num_2 ;//默认数值 uint8_t Batt_Temp_Num = 5;//默认数值5、7 int16_t Uart_Rece_BattI=0x0000; uint8_t Can_Rece_buffer[8]; volatile bool Can_Enable = false; uint16_t data_index = 0x0000; uint8_t battbuffer[100];//电池数据都存在此数组中————电压14,温度5 /** * 存放规则如下: * 位置: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 * 数据: 年 月 日 时 分 秒 信息体标志 年 月 日 时 分 秒 网络信号 故障等级 故障代码高 故障代码低 * * 17 18 19 20 21 22 23 24 25 26 27 28 29 30 30+1 .... 30+X*2 31+X*2 31+1...31+X*2+N * 电流H 电流L Link电压H Link电压L Pack电压H Pack电压L 开关状态 SOC SOH 均衡状态 单体个数X 单体v1...单体vX 温度个数N 温度1..温度N * 32+X*2+N 33+X*2+N 34+x*2+N 35 +X*2+N 36+X*2 +N 37+X*2+N * 电池状态 是否加热 最高单体H 最高单体L 最低单体H 最低单体L * */ //状态机定义 typedef enum { PROCESS_STATE_IDLE = 0, PROCESS_STATE_WORK, PROCESS_STATE_SLEEP }process_Main; static process_Main gProcess_Main_Task = PROCESS_STATE_IDLE; #define PROC_MAIN_STATE_SWITCH(a) (gProcess_Main_Task = a) typedef enum { PROCESS_UART_STATE_IDLE = 0, PROCESS_UART_STATE_CHECK, PROCESS_UART_STATE_WORK, PROCESS_UART_STATE_SLEEP }process_Uart; static process_Uart gProcess_Uart_Task = PROCESS_UART_STATE_IDLE; #define PROC_UART_STATE_SWITCH(a) (gProcess_Uart_Task = a) typedef enum { PROCESS_CAN_STATE_IDLE = 0, PROCESS_CAN_STATE_RECV, PROCESS_CAN_STATE_SEND, PROCESS_CAN_STATE_SLEEP }process_CAN; static process_CAN gProcess_Can_Task = PROCESS_CAN_STATE_IDLE; #define PROC_CAN_STATE_SWITCH(a) (gProcess_Can_Task = a) typedef enum { PROCESS_TCP_STATE_IDLE = 0, PROCESS_TCP_STATE_LINK, PROCESS_TCP_STATE_WORK, PROCESS_TCP_STATE_SLEEP }process_TCP; static process_TCP gProcess_Tcp_Task = PROCESS_TCP_STATE_IDLE; #define PROC_TCP_STATE_SWITCH(a) (gProcess_Tcp_Task = a) //堆栈申请 static StaticTask_t gProcess_Main_Task_t; static UINT8 gProcess_Main_TaskStack[PROC_UART_TASK_STACK_SIZE]; static StaticTask_t gProcess_Uart_Task_t; static UINT8 gProcess_Uart_TaskStack[PROC_UART_TASK_STACK_SIZE]; static StaticTask_t gProcess_Can_Task_t; static UINT8 gProcess_Can_TaskStack[PROC_CAN_TASK_STACK_SIZE]; static StaticTask_t gProcess_Tcp_Task_t; static UINT8 gProcess_Tcp_TaskStack[PROC_TCP_TASK_STACK_SIZE]; //睡眠进出函数 static void appBeforeHib(void *pdata, slpManLpState state) { uint32_t *p_param = (uint32_t *)pdata; #ifdef USING_PRINTF printf("Before Hibernate:%d \n",state); #endif slpManAONIOLatchEn(AonIOLatch_Enable); } static void appAfterHib(void *pdata, slpManLpState state) { #ifdef USING_PRINTF printf("Try Hibernate Failed:%d \n",state); #endif } static void appBeforeSlp1(void *pdata, slpManLpState state) { #ifdef USING_PRINTF printf("Before Sleep1:%d \n",state); #endif slpManAONIOLatchEn(AonIOLatch_Enable); } static void appAfterSlp1(void *pdata, slpManLpState state) { #ifdef USING_PRINTF printf("After Sleep1:%d \n",state); #endif } static void appBeforeSlp2(void *pdata, slpManLpState state) { #ifdef USING_PRINTF printf("before sleep2:%d \n",state); #endif slpManAONIOLatchEn(AonIOLatch_Enable); } static void appAfterSlp2(void *pdata, slpManLpState state) { #ifdef USING_PRINTF printf("sleep2 failed:%d \n",state); #endif } // 主任务线程 static void Main_Task(void* arg) { UINT16 Can_index = 0; UINT16 Uart_index = 0; UINT16 Tcp_index = 0; uint32_t sleep_index = 0; int32_t inParam = 0xAABBCCDD; PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE); NetSocDisplay(LED_SOC_0,LED_TURN_OFF); NetSocDisplay(LED_SOC_1,LED_TURN_OFF); NetSocDisplay(LED_SOC_2,LED_TURN_OFF); NetSocDisplay(LED_SOC_3,LED_TURN_OFF); FaultDisplay(LED_TURN_OFF); NVIC_EnableIRQ(PadWakeup1_IRQn); slpManSetPmuSleepMode(true,SLP_HIB_STATE,false); slpManApplyPlatVoteHandle("MainSlp",&MainSlpHandler); slpManPlatVoteDisableSleep(MainSlpHandler, SLP_SLP2_STATE); slpManRegisterUsrdefinedBackupCb(appBeforeHib,&inParam,SLPMAN_HIBERNATE_STATE); slpManRegisterUsrdefinedRestoreCb(appAfterHib,NULL,SLPMAN_HIBERNATE_STATE); slpManRegisterUsrdefinedBackupCb(appBeforeSlp1,NULL,SLPMAN_SLEEP1_STATE); slpManRegisterUsrdefinedRestoreCb(appAfterSlp1,NULL,SLPMAN_SLEEP1_STATE); slpManRegisterUsrdefinedBackupCb(appBeforeSlp2,NULL,SLPMAN_SLEEP2_STATE); slpManRegisterUsrdefinedRestoreCb(appAfterSlp2,NULL,SLPMAN_SLEEP2_STATE); slpManSlpState_t slpstate = slpManGetLastSlpState(); #ifdef USING_PRINTF printf("slpstate:%d \n",slpstate); #endif if((slpstate == SLP_SLP2_STATE) || (slpstate == SLP_HIB_STATE)) { PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE); } else { PROC_MAIN_STATE_SWITCH(PROCESS_STATE_WORK); } //线程初始化完成 while (true) { switch(gProcess_Main_Task) { case PROCESS_STATE_IDLE: { // 暂时不区分唤醒 PROC_MAIN_STATE_SWITCH(PROCESS_STATE_WORK); break; } case PROCESS_STATE_WORK: { osDelay(10/portTICK_PERIOD_MS); Can_index++; Uart_index++; Tcp_index++; if (Uart_index >100)//Uart 1s 调用一次 { PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WORK); Uart_index = 0; } if (Can_index >=100)//Can 1s 调用一次 { PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_RECV); Can_index = 0; } if (Tcp_index >=100)//Tcp 1s 调用一次 { PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_LINK); Tcp_index = 0; } if((!Can_Enable)&&(Uart_Rece_BattI==0x0000)) { sleep_index++; } else { sleep_index = 0; } if (sleep_index >=6000) { PROC_MAIN_STATE_SWITCH(PROCESS_STATE_SLEEP); sleep_index = 0; } break; } case PROCESS_STATE_SLEEP: { while((gProcess_Uart_Task!=PROCESS_UART_STATE_SLEEP)||(gProcess_Can_Task!=PROCESS_CAN_STATE_SLEEP)||(gProcess_Tcp_Task!=PROCESS_TCP_STATE_SLEEP)) { PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_SLEEP); PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SLEEP); PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_SLEEP); } slpManSlpState_t State; uint8_t cnt; if(slpManCheckVoteState(MainSlpHandler, &State, &cnt)==RET_TRUE) { #ifdef USING_PRINTF printf("[%d]We Can Check Vote State, state=%d, cnt=%d\r\n",__LINE__,State,cnt); #endif } slpManPlatVoteEnableSleep(MainSlpHandler, SLP_SLP2_STATE); if(slpManCheckVoteState(MainSlpHandler, &State, &cnt)==RET_TRUE) { #ifdef USING_PRINTF printf("[%d]We Can Check Vote State Again, state=%d, cnt=%d\r\n",__LINE__,State,cnt); #endif } #ifdef USING_PRINTF printf("[%d]Waiting sleep\r\n",__LINE__); #endif #ifdef USING_PRINTF printf("Main_Sleep\n"); #endif FaultDisplay(LED_TURN_ON); slpManDeepSlpTimerStart(deepslpTimerID,300000); while(1) { osDelay(30000/portTICK_PERIOD_MS); } break; } } } } //Uart校验程序 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; } //Uart回调程序 void USART_callback(uint32_t event) { if(event & ARM_USART_EVENT_RX_TIMEOUT) { isRecvTimeout = true; } if(event & ARM_USART_EVENT_RECEIVE_COMPLETE) { isRecvComplete = true; } } //Uart发送接收函数 uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg,uint8_t* Uart_Rece_buffer,uint8_t Data_Len) { uint16_t CRC_Rece_buffer; uint16_t CRC_chk_buffer; uint8_t Uart_Send_buffer[8]; 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_buffer = crc_chk(Uart_Send_buffer,6); Uart_Send_buffer[6] = CRC_chk_buffer; Uart_Send_buffer[7] = CRC_chk_buffer>>8; uint32_t timeout = 0x00000000; USARTdrv->Send(Uart_Send_buffer,8); // #ifdef USING_PRINTF // printf("Uart_Send_buffer: "); // for(int i=0;i<8;i++) // { // printf("%x ",Uart_Send_buffer[i]); // } // printf("\n"); // #endif USARTdrv->Receive(Uart_Rece_buffer,Data_Len); while((isRecvTimeout == false) && (isRecvComplete == false)) { timeout++; if (timeout>=7000000) { timeout =0; isRecvTimeout = true; break; } } if (isRecvComplete == true) { #ifdef USING_PRINTF #endif isRecvComplete = false; CRC_Rece_buffer =*(Uart_Rece_buffer+Data_Len-1)<<8|*(Uart_Rece_buffer+Data_Len-2); CRC_chk_buffer = crc_chk(Uart_Rece_buffer,Data_Len-2); if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验 { return Uart_Rece_buffer;//此处指针移位出现重启问题 } else //接收数据的校验不过屏蔽 { #ifdef USING_PRINTF printf("Uart_Rece_buffer: "); for(int i=0;iInitialize(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); int Rece_index = 0; volatile uint8_t Data_Len; Uart_Receive_Type Uart_Receive_Msg; memset(&battbuffer[0],0x00,100); PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE); slpManApplyPlatVoteHandle("UARTSLP",&UartSlpHandler); slpManPlatVoteDisableSleep(UartSlpHandler, SLP_SLP2_STATE); Uart_Receive_Msg.Bms_Address = 0x01; Uart_Receive_Msg.Bms_Read_Funcode = 0x03; uint8_t *Uart_Rece_buffer = NULL; volatile bool Uart_task = false; Batt_Cell_Num_2 = Batt_Cell_Num<<1; while (true) { switch (gProcess_Uart_Task) { case PROCESS_UART_STATE_IDLE: { NetSocDisplay(LED_SOC_1,LED_TURN_OFF); Rece_index = 0; break; } case PROCESS_UART_STATE_CHECK: { Uart_Rece_buffer = (uint8_t *)malloc(Uart_Rece_LEN); Uart_Receive_Msg.Reg_Begin_H = 0x00; Uart_Receive_Msg.Reg_Begin_L= 0x02+Batt_Cell_Num; Uart_Receive_Msg.Reg_Num_H = 0x00; Uart_Receive_Msg.Reg_Num_L = 0x01; Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5; memset(Uart_Rece_buffer,0x00,Data_Len); Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len); Uart_Rece_BattI = *(Uart_Rece_buffer+3+0)<<8 |*(Uart_Rece_buffer+3+1); #ifdef USING_PRINTF printf("Check_Current!\n"); #endif PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE); free(Uart_Rece_buffer); break; } case PROCESS_UART_STATE_WORK: { NetSocDisplay(LED_SOC_1,LED_TURN_ON); Uart_task = false; Uart_Rece_buffer = (uint8_t *)malloc(Uart_Rece_LEN); while(!Uart_task) { switch (Rece_index) { case 0://current { Uart_Receive_Msg.Reg_Begin_H = 0x00; Uart_Receive_Msg.Reg_Begin_L= 0x02+Batt_Cell_Num; Uart_Receive_Msg.Reg_Num_H = 0x00; Uart_Receive_Msg.Reg_Num_L = 0x01; Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5; memset(Uart_Rece_buffer,0x00,Data_Len); Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len); Uart_Rece_BattI = *(Uart_Rece_buffer+3+0)<<8 |*(Uart_Rece_buffer+3+1); break; } case 1://cell votage { Uart_Receive_Msg.Reg_Begin_H = 0x00; Uart_Receive_Msg.Reg_Begin_L = 0x02; Uart_Receive_Msg.Reg_Num_H = Batt_Cell_Num>>8; Uart_Receive_Msg.Reg_Num_L = Batt_Cell_Num; Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5; memset(Uart_Rece_buffer,0x00,Data_Len); Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len); battbuffer[30] = Batt_Cell_Num; memcpy(&battbuffer[31],Uart_Rece_buffer+3,Batt_Cell_Num_2); break; } case 2://temprature { Uart_Receive_Msg.Reg_Begin_H = 0x00; Uart_Receive_Msg.Reg_Begin_L = 0x06+Batt_Cell_Num; Uart_Receive_Msg.Reg_Num_H = Batt_Temp_Num>>8; Uart_Receive_Msg.Reg_Num_L = Batt_Temp_Num; Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5; memset(Uart_Rece_buffer,0x00,Data_Len); Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len); battbuffer[31+Batt_Cell_Num_2] = Batt_Temp_Num; for (int i = 0; i < Batt_Temp_Num; i++) { battbuffer[32+Batt_Cell_Num_2+i] = *(Uart_Rece_buffer+3+2*i+1); } break; } case 3://batt votage { Uart_Receive_Msg.Reg_Begin_H = 0x00; Uart_Receive_Msg.Reg_Begin_L = 0x18+Batt_Cell_Num+Batt_Temp_Num; Uart_Receive_Msg.Reg_Num_H = 0x00; Uart_Receive_Msg.Reg_Num_L = 0x01; Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5; memset(Uart_Rece_buffer,0x00,Data_Len); Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len); battbuffer[19] = *(Uart_Rece_buffer+3+0);//Link U battbuffer[20] = *(Uart_Rece_buffer+3+1); battbuffer[21] = *(Uart_Rece_buffer+3+0);//Pack U battbuffer[22] = *(Uart_Rece_buffer+3+1); break; } case 4://soc,soh { Uart_Receive_Msg.Reg_Begin_H = 0x00; Uart_Receive_Msg.Reg_Begin_L = 0x09+Batt_Cell_Num+Batt_Temp_Num; Uart_Receive_Msg.Reg_Num_H = 0x00; Uart_Receive_Msg.Reg_Num_L = 0x04; Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5; memset(Uart_Rece_buffer,0x00,Data_Len); Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len); battbuffer[23] = *(Uart_Rece_buffer+3+0)>>1;//mos状态 battbuffer[24] = *(Uart_Rece_buffer+3+5);//SOC battbuffer[25] = *(Uart_Rece_buffer+3+7);//SOH break; } case 5://均衡 { Uart_Receive_Msg.Reg_Begin_H = 0x00; Uart_Receive_Msg.Reg_Begin_L = 0x06+Batt_Cell_Num+Batt_Temp_Num; Uart_Receive_Msg.Reg_Num_H = 0x00; Uart_Receive_Msg.Reg_Num_L = 0x02; Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5; memset(Uart_Rece_buffer,0x00,Data_Len); Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len); memcpy(&battbuffer[26],Uart_Rece_buffer+3,4); break; } case 6: { Uart_Receive_Msg.Reg_Begin_H = 0x00; Uart_Receive_Msg.Reg_Begin_L = 0x19+Batt_Cell_Num+Batt_Temp_Num; Uart_Receive_Msg.Reg_Num_H = 0x00; Uart_Receive_Msg.Reg_Num_L = 0x02; Data_Len = Uart_Receive_Msg.Reg_Num_L*2+5; memset(Uart_Rece_buffer,0x00,Data_Len); Uart_Rece_buffer = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer,Data_Len); memcpy(&battbuffer[34+Batt_Cell_Num_2+Batt_Temp_Num],Uart_Rece_buffer+3,4); break; } default: { PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE); Uart_task = true; break; } } Rece_index++; } free(Uart_Rece_buffer); break; } case PROCESS_UART_STATE_SLEEP: { slpManPlatVoteEnableSleep(UartSlpHandler, SLP_SLP2_STATE); while(1) { osDelay(30000/portTICK_PERIOD_MS); } } } } } //Can-线程任务 static void Can_Task(void* arg) { uint32_t Can_ID; Can_InitType param; Can_TxMsgType Can_TxMsg; param.baudrate = CAN_500Kbps; param.mode = REQOP_NORMAL; //过滤ID配置 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; uint16_t Batt_Cell_addU = 0x0000; slpManApplyPlatVoteHandle("CanSlp",&CanSlpHandler); slpManPlatVoteDisableSleep(CanSlpHandler, SLP_SLP2_STATE); volatile bool Can_Flag=false; PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_IDLE); while(1) { switch(gProcess_Can_Task) { case PROCESS_CAN_STATE_IDLE: { Can_Rece_buffer[0]=0xff; send_index = 0; break; } case PROCESS_CAN_STATE_RECV: { //can采用先接收后发送的策略 HAL_Can_Receive(Can_Rece_buffer); if (Can_Rece_buffer[0]!=0xff)//满足can发送使能 { PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SEND); Can_Enable = true; } else { Can_Enable = false; } break; } case PROCESS_CAN_STATE_SEND: { Can_Flag=false; while(!Can_Flag) { #ifdef USING_PRINTF printf("Can_Send!\n"); #endif switch(send_index) { case 0: { Can_ID = 0x001; for (int i = 0; i < 8; i++) { Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8]; } Can_TxMsg.stdIDH = Can_ID>>3; Can_TxMsg.stdIDL = Can_ID<<5; Can_TxMsg.DLC = 8; HAL_Can_Transmit(Can_TxMsg); break; } case 1: { Can_ID = 0x011; for (int i = 0; i < 8; i++) { Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8]; } Can_TxMsg.stdIDH = Can_ID>>3; Can_TxMsg.stdIDL = Can_ID<<5; Can_TxMsg.DLC = 8; HAL_Can_Transmit(Can_TxMsg); break; } case 2: { Can_ID = 0x020; for (int i = 0; i < 8; i++) { Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8]; } Can_TxMsg.stdIDH = Can_ID>>3; Can_TxMsg.stdIDL = Can_ID<<5; Can_TxMsg.DLC = 8; HAL_Can_Transmit(Can_TxMsg); break; } case 3: { Can_ID = 0x031; for (int i = 0; i < 4; i++) { Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8]; } Can_TxMsg.Data[4] = 0x00; Can_TxMsg.Data[5] = 0x00; Can_TxMsg.Data[6] = 0x00; Can_TxMsg.Data[7] = 0x00; Can_TxMsg.stdIDH = Can_ID>>3; Can_TxMsg.stdIDL = Can_ID<<5; Can_TxMsg.DLC = 8; HAL_Can_Transmit(Can_TxMsg); break; } case 4: { Can_ID = 0x101; for (int i = 0; i < Batt_Temp_Num; i++) { Can_TxMsg.Data[i] = battbuffer[i+32+Batt_Cell_Num_2]; } Can_TxMsg.stdIDH = Can_ID>>3; Can_TxMsg.stdIDL = Can_ID<<5; Can_TxMsg.DLC = 8; HAL_Can_Transmit(Can_TxMsg); break; } case 5: { Batt_Cell_addU = 0x0000; for (int i = 0; i < Batt_Cell_Num; i++) { Batt_Cell_addU = Batt_Cell_addU + (battbuffer[31+i*2]<<8|battbuffer[31+i*2+1]); } Can_ID = 0x201; Can_TxMsg.Data[0] = battbuffer[19]; Can_TxMsg.Data[1] = battbuffer[20]; Can_TxMsg.Data[2] = battbuffer[21]; Can_TxMsg.Data[3] = battbuffer[22];//外电压 Can_TxMsg.Data[4] = Batt_Cell_addU>>8; Can_TxMsg.Data[5] = Batt_Cell_addU;//累加电压 Can_TxMsg.Data[6] = Uart_Rece_BattI>>8; Can_TxMsg.Data[7] = Uart_Rece_BattI; Can_TxMsg.stdIDH = Can_ID>>3; Can_TxMsg.stdIDL = Can_ID<<5; Can_TxMsg.DLC = 8; HAL_Can_Transmit(Can_TxMsg); break; } case 6: { Can_ID = 0x211; Can_TxMsg.Data[0] = battbuffer[34+Batt_Cell_Num_2+Batt_Temp_Num]; Can_TxMsg.Data[1] = battbuffer[35+Batt_Cell_Num_2+Batt_Temp_Num]; Can_TxMsg.Data[2] = battbuffer[36+Batt_Cell_Num_2+Batt_Temp_Num]; Can_TxMsg.Data[3] = battbuffer[37+Batt_Cell_Num_2+Batt_Temp_Num]; Can_TxMsg.Data[4] = 0x00; Can_TxMsg.Data[5] = 0x00; Can_TxMsg.Data[6] = battbuffer[24]; Can_TxMsg.Data[7] = Batt_Temp_Num<<4|0; Can_TxMsg.stdIDH = Can_ID>>3; Can_TxMsg.stdIDL = Can_ID<<5; Can_TxMsg.DLC = 8; HAL_Can_Transmit(Can_TxMsg); break; } case 7: { Can_ID = 0x221; Can_TxMsg.Data[0] = battbuffer[25];//SOH 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; Can_TxMsg.stdIDH = Can_ID>>3; Can_TxMsg.stdIDL = Can_ID<<5; Can_TxMsg.DLC = 8; HAL_Can_Transmit(Can_TxMsg); break; } default: { Can_Flag=true; PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_IDLE); break; } } send_index ++; } break; } case PROCESS_CAN_STATE_SLEEP: { slpManPlatVoteEnableSleep(CanSlpHandler, SLP_SLP2_STATE); while(1) { osDelay(30000/portTICK_PERIOD_MS); } } } } } static void sendQueueMsg(UINT32 msgId, UINT32 xTickstoWait) { eventCallbackMessage_t *queueMsg = NULL; queueMsg = malloc(sizeof(eventCallbackMessage_t)); queueMsg->messageId = msgId; if (psEventQueueHandle) { if (pdTRUE != xQueueSend(psEventQueueHandle, &queueMsg, xTickstoWait)) { ECOMM_TRACE(UNILOG_PLA_APP, sendQueueMsg_1, P_ERROR, 0, "xQueueSend error"); } } } static INT32 socketRegisterPSUrcCallback(urcID_t eventID, void *param, UINT32 paramLen) { CmiSimImsiStr *imsi = NULL; CmiPsCeregInd *cereg = NULL; UINT8 rssi = 0, index = 0; NmAtiNetifInfo *netif = NULL; switch(eventID) { case NB_URC_ID_SIM_READY: { imsi = (CmiSimImsiStr *)param; memcpy(gImsi, imsi->contents, imsi->length); #ifdef USING_PRINTF printf("SIM ready(imsi=%s)\n",(UINT8 *)imsi->contents); #endif break; } case NB_URC_ID_MM_SIGQ: { rssi = *(UINT8 *)param; #ifdef USING_PRINTF printf("RSSI signal=%d\n",rssi); #endif ECOMM_TRACE(UNILOG_PLA_APP, socketRegisterPSUrcCallback_1, P_INFO, 1, "RSSI signal=%d", rssi); break; } case NB_URC_ID_PS_BEARER_ACTED: { #ifdef USING_PRINTF printf("Default bearer activated\n"); #endif break; } case NB_URC_ID_PS_BEARER_DEACTED: { #ifdef USING_PRINTF printf("Default bearer Deactivated\n"); #endif break; } case NB_URC_ID_PS_CEREG_CHANGED: { cereg = (CmiPsCeregInd *)param; gCellID = cereg->celId; #ifdef USING_PRINTF printf("URCCallBack:CEREG changed act:%d celId:%d locPresent:%d tac:%d\n", cereg->act, cereg->celId, cereg->locPresent, cereg->tac); #endif break; } case NB_URC_ID_PS_NETINFO: { netif = (NmAtiNetifInfo *)param; if (netif->netStatus == NM_NETIF_ACTIVATED) sendQueueMsg(QMSG_ID_NW_IP_READY, 0); break; } } return 0; } uint8_t bcc_chk(uint8_t* data, uint8_t length)//发送bcc校验函数 { uint8_t bcc_chk_return = 0x00; uint8_t count = 0; while (count>16; battbuffer[0] = battbuffer[0] - 0x07D0; battbuffer[1] = timestracture.UTCtimer1>>8; battbuffer[2] = timestracture.UTCtimer1; battbuffer[3] = timestracture.UTCtimer2>>24; battbuffer[4] = timestracture.UTCtimer2>>16; battbuffer[5] = timestracture.UTCtimer2>>8; battbuffer[6] = 0x80;//信息体标志,此处为电池信息 battbuffer[7] = battbuffer[0];//年 battbuffer[8] = battbuffer[1];//月 battbuffer[9] = battbuffer[2];//日 battbuffer[10] = battbuffer[3];//时 0时区时间 battbuffer[11] = battbuffer[4];//分 battbuffer[12] = battbuffer[5];//秒 appGetSignalInfoSync(&csq,&snr,&rsnr);//获取信号质量 battbuffer[13] = csq;// 网络信号 battbuffer[14] = 0x00;//故障等级 battbuffer[15] = 0x00;//故障代码高 battbuffer[16] = 0x00;//故障代码低 //电流适应性更改,从int转换到uint,加10000的偏移量,100mA的单位 if (Batt_current>0x8000) { Batt_current = Batt_current|0x7fff; Batt_current = 0x2710 - Batt_current; Batt_current = Batt_current; } else { Batt_current = Batt_current+0x2710; Batt_current = Batt_current; } battbuffer[17] = Batt_current>>8; battbuffer[18] = Batt_current; data_index = 32+Batt_Cell_Num_2+Batt_Temp_Num; battbuffer[data_index] = 0x00;//电池状态 data_index++; battbuffer[data_index] = 0x00;//是否加热 data_index++; } //Tcp线程 static void Tcp_Task(void* arg) { CHAR SN[] = "GYTEST00000000003";//SN应写到osfile里面 CHAR serverip[] = "47.97.127.222"; CHAR serverport[] = "8712"; struct addrinfo hints, *server_res; int TcpsendID = -1; int TcpconnectID = -1; memset( &hints, 0, sizeof( hints ) ); hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; psEventQueueHandle = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(eventCallbackMessage_t*)); int NB_send_len=59+Batt_Cell_Num_2+Batt_Temp_Num;//设定tcp发送的最大数值,以电池数据为上线 slpManApplyPlatVoteHandle("TcpSlp",&TcpSlpHandler); slpManPlatVoteDisableSleep(TcpSlpHandler, SLP_SLP2_STATE); PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE); uint8_t Tcp_Index = 0; uint8_t* TcpSendBuffer=NULL; volatile bool Tcp_Flag=false; eventCallbackMessage_t *queueItem = NULL; while(true) { switch(gProcess_Tcp_Task) { case PROCESS_TCP_STATE_IDLE: { osDelay(100); break; } case PROCESS_TCP_STATE_LINK: { if(TcpconnectID<0) { if (xQueueReceive(psEventQueueHandle, &queueItem, portMAX_DELAY)) { switch(queueItem->messageId) { case QMSG_ID_NW_IP_READY: { if (getaddrinfo( serverip, serverport , &hints, &server_res ) != 0 ) { #ifdef USING_PRINTF printf("TCP connect unresolved dns\n"); #endif } sockfd = socket(AF_INET, SOCK_STREAM, 0); TcpconnectID = connect(sockfd, (struct sockaddr *) server_res->ai_addr, server_res->ai_addrlen); if(TcpconnectID<0) { #ifdef USING_PRINTF printf("Connect failed!%d\n",TcpconnectID); #endif close(sockfd); PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE); break; } else { sendQueueMsg(QMSG_ID_SOCK_SENDPKG, 0); PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_WORK); } break; } } free(queueItem); } } else { PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_WORK); } break; } case PROCESS_TCP_STATE_WORK: { Tcp_Flag = false; TcpSendBuffer = (uint8_t *)malloc(NB_send_len); while (!Tcp_Flag) { switch(Tcp_Index) { case 0://发送登录信息 { Tcp_Index=1; break; } case 1://发送电池数据 { memset(TcpSendBuffer,0x00,NB_send_len); *(TcpSendBuffer+0) = 0x23; *(TcpSendBuffer+1) = 0x23; *(TcpSendBuffer+2) = 0x02; *(TcpSendBuffer+3) = 0xfe; memcpy(TcpSendBuffer+4,SN,17); *(TcpSendBuffer+21) = 0x01;//01表示不加密 Tcp_Batt_Data_Assemble();//数据组装函数 *(TcpSendBuffer+22) = data_index>>8;//数据长度 *(TcpSendBuffer+23) = data_index;//数据长度 memcpy(TcpSendBuffer+24,battbuffer,data_index); *(TcpSendBuffer+NB_send_len-1) = bcc_chk(TcpSendBuffer,NB_send_len-1); #ifdef USING_PRINTF printf("battbuffer:"); for (int i = 0; i < data_index; i++) { printf("%x ",battbuffer[i]); } printf("\n "); #endif TcpsendID = send(sockfd, TcpSendBuffer, NB_send_len, 0 ); //发送失败暂时不写 #ifdef USING_PRINTF printf("ConnectID:%d,TcpSend:%d,data length:%d,Data: ",sockfd,TcpsendID,NB_send_len); for (int i = 0; i < NB_send_len; i++) { printf("%x ",*(TcpSendBuffer+i)); } printf("\n "); #endif Tcp_Index=2; break; } case 2: { Tcp_Index=3; break; } default: { PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE); Tcp_Index = 0; Tcp_Flag = true; } } } free(TcpSendBuffer); break; } case PROCESS_TCP_STATE_SLEEP: { slpManPlatVoteEnableSleep(TcpSlpHandler, SLP_SLP2_STATE); closesocket(sockfd); deregisterPSEventCallback(socketRegisterPSUrcCallback);//注销NB网络事件函数 while(1) { osDelay(30000/portTICK_PERIOD_MS); } } } } } //主任务线程初始化 void Main_Task_Init() { #ifndef USING_PRINTF if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_LOG_CONTROL) != 0) { HAL_UART_RecvFlowControl(false); } #endif osThreadAttr_t task_attr; memset(&task_attr,0,sizeof(task_attr)); memset(gProcess_Main_TaskStack, 0xA5, PROC_MAIN_TASK_STACK_SIZE); task_attr.name = "Main_Task"; task_attr.stack_mem = gProcess_Main_TaskStack; task_attr.stack_size = PROC_MAIN_TASK_STACK_SIZE; task_attr.priority = osPriorityNormal; task_attr.cb_mem = &gProcess_Main_Task_t; task_attr.cb_size = sizeof(StaticTask_t); osThreadNew(Main_Task, NULL, &task_attr); } //Uart读取线程初始化 void Uart_Task_Init() { osThreadAttr_t task_attr; memset(&task_attr,0,sizeof(task_attr)); memset(gProcess_Uart_TaskStack, 0xA5, PROC_UART_TASK_STACK_SIZE); task_attr.name = "Uart_Task"; task_attr.stack_mem = gProcess_Uart_TaskStack; task_attr.stack_size = PROC_UART_TASK_STACK_SIZE; task_attr.priority = osPriorityNormal; task_attr.cb_mem = &gProcess_Uart_Task_t; task_attr.cb_size = sizeof(StaticTask_t); osThreadNew(Uart_Task, NULL, &task_attr); } //Can线程初始化 void Can_Task_Init() { osThreadAttr_t task_attr; memset(&task_attr,0,sizeof(task_attr)); memset(gProcess_Can_TaskStack, 0xA5, PROC_CAN_TASK_STACK_SIZE); task_attr.name = "Can_Task"; task_attr.stack_mem = gProcess_Can_TaskStack; task_attr.stack_size = PROC_CAN_TASK_STACK_SIZE; task_attr.priority = osPriorityNormal; task_attr.cb_mem = &gProcess_Can_Task_t; task_attr.cb_size = sizeof(StaticTask_t); osThreadNew(Can_Task, NULL, &task_attr); } //Tcp线程初始化 void Tcp_Task_Init() { osThreadAttr_t task_attr; registerPSEventCallback(NB_GROUP_ALL_MASK, socketRegisterPSUrcCallback); memset(&task_attr,0,sizeof(task_attr)); memset(gProcess_Tcp_TaskStack, 0xA5, PROC_TCP_TASK_STACK_SIZE); task_attr.name = "Tcp_Task"; task_attr.stack_mem = gProcess_Tcp_TaskStack; task_attr.stack_size = PROC_TCP_TASK_STACK_SIZE; task_attr.priority = osPriorityNormal; task_attr.cb_mem = &gProcess_Tcp_Task_t; task_attr.cb_size = sizeof(StaticTask_t); osThreadNew(Tcp_Task, NULL, &task_attr); } void appInit(void *arg) { Main_Task_Init(); Uart_Task_Init(); Can_Task_Init(); Tcp_Task_Init(); } //主函数入口 void main_entry(void) { BSP_CommonInit(); osKernelInitialize(); registerAppEntry(appInit, NULL); if (osKernelGetState() == osKernelReady) { osKernelStart(); } while(1); }