123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077 |
- /****************************************************************************
- *
- * 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 PROC0_TASK_STACK_SIZE (512)
- #define PROC1_TASK_STACK_SIZE (1024)
- #define PROC2_TASK_STACK_SIZE (1024)
- #define PROC3_TASK_STACK_SIZE (1536)
- //uart def
- #define Uart_Send_LEN (8)
- #define Uart_Rece_LEN (40)//串口读取的最大数量,40个字节,能满足一次性读取17个单体
- #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 receive timeout flag */
- volatile bool isRecvTimeout = false;
- /** \brief receive complete flag */
- volatile bool isRecvComplete = false;
- uint8_t process0SlpHandler = 0x00;
- uint8_t process1SlpHandler = 0x01;
- uint8_t process2SlpHandler = 0x02;
- uint8_t process3SlpHandler = 0x03;
- uint8_t deepslpTimerID = 7;
- uint8_t Can_Rece_buffer[8];
- 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 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
- * */
- uint16_t data_index = 0x0000;
-
- 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;
- typedef enum
- {
- PROCESS_NB_STATE_IDLE = 0,
- PROCESS_NB_STATE_WORK,
- PROCESS_NB_STATE_CONNECT,
- PROCESS_NB_STATE_SLEEP
- }process_NB;
- static StaticTask_t gProcessTask0;
- static UINT8 gProcessTaskStack0[PROC0_TASK_STACK_SIZE];
- static StaticTask_t gProcessTask1;
- static UINT8 gProcessTaskStack1[PROC1_TASK_STACK_SIZE];
- static StaticTask_t gProcessTask2;
- static UINT8 gProcessTaskStack2[PROC2_TASK_STACK_SIZE];
- static StaticTask_t gProcessTask3;
- static UINT8 gProcessTaskStack3[PROC3_TASK_STACK_SIZE];
- processSM gProc0State = PROCESS_STATE_IDLE;
- process_Uart gProc1State = PROCESS_Uart_STATE_IDLE;
- processSM gProc2State = PROCESS_STATE_IDLE;
- process_NB gProc3State = PROCESS_NB_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) //NB状态切换
- //-------------------------------------------------------------------------------------------------------------------------------------------------------
- static void appBeforeHib(void *pdata, slpManLpState state)
- {
- uint32_t *p_param = (uint32_t *)pdata;
- #ifdef USING_PRINTF
- printf("Before Hibernate:%d \n",state);
- #endif
- ECOMM_TRACE(UNILOG_PLA_APP, appBeforeHib_1, P_SIG, 1, "Before Hibernate = %x",*p_param);
- slpManAONIOLatchEn(AonIOLatch_Enable);
- }
- static void appAfterHib(void *pdata, slpManLpState state)
- {
- #ifdef USING_PRINTF
- printf("Try Hibernate Failed:%d \n",state);
- #endif
- ECOMM_TRACE(UNILOG_PLA_APP, appAfterHib_1, P_SIG, 0, "Try Hibernate Failed, Interrupt Pending. Only sleep failed this function will excute");
- }
- static void appBeforeSlp1(void *pdata, slpManLpState state)
- {
- #ifdef USING_PRINTF
- printf("Before Sleep1:%d \n",state);
- #endif
- ECOMM_TRACE(UNILOG_PLA_APP, appBeforeSlp1_1, P_SIG, 0, "Before Sleep1");
- slpManAONIOLatchEn(AonIOLatch_Enable);
- }
- static void appAfterSlp1(void *pdata, slpManLpState state)
- {
- #ifdef USING_PRINTF
- printf("After Sleep1:%d \n",state);
- #endif
- PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP);
- ECOMM_TRACE(UNILOG_PLA_APP, appAfterSlp1_1, P_SIG, 0, "After Sleep1, no matter sleep success or not this function will excute");
- }
- static void appBeforeSlp2(void *pdata, slpManLpState state)
- {
- #ifdef USING_PRINTF
- printf("before sleep2:%d \n",state);
- #endif
- ECOMM_TRACE(UNILOG_PLA_APP, appBeforeSlp2_1, P_SIG, 0, "Before Sleep2");
- slpManAONIOLatchEn(AonIOLatch_Enable);
- }
- static void appAfterSlp2(void *pdata, slpManLpState state)
- {
- #ifdef USING_PRINTF
- printf("sleep2 failed:%d \n",state);
- #endif
- ECOMM_TRACE(UNILOG_PLA_APP, appAfterSlp2_1, P_SIG, 0, "Sleep2 Failed, Interrupt Pending. Only sleep failed this function will excute");
- }
- 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* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg,uint8_t *Uart_Rece_buffer)
- {
- uint16_t CRC_Rece_buffer;
- uint16_t CRC_chk_buffer;
- uint8_t Uart_Send_buffer[8];
- uint8_t Rece_Data_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_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=0;
- USARTdrv->Send(Uart_Send_buffer,8);
- Rece_Data_Len = Uart_Receive_Msg.Reg_Num_L<<1;//读取几个寄存器的值,数据长度乘以二
- USARTdrv->Receive(Uart_Rece_buffer,Rece_Data_Len+5);
- while((isRecvTimeout == false) && (isRecvComplete == false))// 未收到数据不叫时间超时,收到数据但是不全叫时间超时
- {
- timeout++;
- if (timeout>7000000)
- {
- timeout =0;
- isRecvTimeout = true;
- break;
- }
- }
- if (isRecvComplete == true)
- {
- Rece_Data_Len = *(Uart_Rece_buffer+2);
- isRecvComplete = false;
- CRC_Rece_buffer =*(Uart_Rece_buffer+Rece_Data_Len+4)<<8|*(Uart_Rece_buffer+Rece_Data_Len+3);
- CRC_chk_buffer = crc_chk(Uart_Rece_buffer,Rece_Data_Len+3);
- // #ifdef USING_PRINTF
- // // printf("Uart_Send_buffer: ");
- // // for(int i=0;i<8;i++)
- // // {
- // // printf("%x ",Uart_Send_buffer[i]);
- // // }
- // // printf("\n");
- // printf("Uart_Rece_buffer: ");
- // for(int i=0;i<Rece_Data_Len+5;i++)
- // {
- // printf("%x ",*(Uart_Rece_buffer+i));
- // }
- // printf("crcchk:%x,%x ",CRC_chk_buffer,CRC_Rece_buffer);
- // #endif
- if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
- {
- return Uart_Rece_buffer+3;
- }
- else //接收数据的校验不过屏蔽
- {
- memset(Uart_Rece_buffer,0xff,Rece_Data_Len+5);
- return Uart_Rece_buffer+3;
- }
- }
- if (isRecvTimeout == true)//没收到数据,全部为空值
- {
- memset(Uart_Rece_buffer,0x00,Rece_Data_Len+5);
- isRecvTimeout = false;
- osDelay(1000);
- return Uart_Rece_buffer;
- }
- 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)//任务调度程序
- {
- UINT16 Can_index = 0;
- UINT16 Uart_index = 0;
- UINT16 NB_index = 0;
- uint32_t sleep_index = 0;
- int32_t inParam = 0xAABBCCDD;
- PROC_Task_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("pro0",&process0SlpHandler);
- slpManPlatVoteDisableSleep(process0SlpHandler, 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_Task_STATE_SWITCH(PROCESS_STATE_IDLE);
- }
- else
- {
- PROC_Task_STATE_SWITCH(PROCESS_STATE_WORK);
- }
-
- while(1)
- {
- switch(gProc0State)
- {
- case PROCESS_STATE_IDLE:
- {
- #ifdef USING_PRINTF
- printf("wake up 5s \n");
- #endif
- PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_CHECK);
- NetSocDisplay(LED_SOC_0,LED_TURN_ON);
- osDelay(100/portTICK_PERIOD_MS);//delay 100ms
- if(Uart_Rece_BattI!=0x0000)
- {
- PROC_Task_STATE_SWITCH(PROCESS_STATE_WORK);
- }
- sleep_index++;
- if(sleep_index>=50)
- {
- NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
- sleep_index=0;
- PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP);
- }
- break;
- }
- case PROCESS_STATE_WORK:
- {
- osDelay(10/portTICK_PERIOD_MS);//10ms
- Can_index++;
- Uart_index++;
- NB_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);
- Can_index = 0;
- }
- if (NB_index >=1000)//NB 10s 调用一次
- {
- PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_CONNECT);
- NB_index = 0;
- }
- if((Uart_Rece_BattI==0x0000)&&(Can_Rece_buffer[0]!=0x01))
- {
- sleep_index++;
- }
- else
- {
- sleep_index = 0;
- }
- if (sleep_index >=6000)
- {
- PROC_Task_STATE_SWITCH(PROCESS_STATE_SLEEP);
- sleep_index = 0;
- #ifdef USING_PRINTF
- printf("sleep_index:%d,Current:%x,CAN:%x \n",sleep_index,Uart_Rece_BattI,Can_Rece_buffer[0]);
- #endif
- }
- break;
-
- }
- case PROCESS_STATE_SLEEP:
- {
-
- slpManSlpState_t State;
- uint8_t cnt;
- PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_SLEEP);
- PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_SLEEP);
- PROC_Can_STATE_SWITCH(PROCESS_STATE_SLEEP);
- osDelay(1000/portTICK_PERIOD_MS);
- #if 1
- if(slpManCheckVoteState(process0SlpHandler, &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(process0SlpHandler, SLP_SLP2_STATE); // cancel the prohibition of sleep2
- slpManPlatVoteEnableSleep(process0SlpHandler, SLP_SLP2_STATE);
- if(slpManCheckVoteState(process0SlpHandler, &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
- #endif
- FaultDisplay(LED_TURN_ON);
- slpManDeepSlpTimerStart(deepslpTimerID,60000);
- while(1);
- }
- }
- }
- }
- static void process1AppTask(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);
- int Rece_index = 0;
- uint8_t *Uart_Reve_Point = NULL;
- Uart_Receive_Type Uart_Receive_Msg;
- PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
- slpManApplyPlatVoteHandle("pro1",&process1SlpHandler);
- slpManPlatVoteDisableSleep(process1SlpHandler, SLP_SLP2_STATE);
- Uart_Receive_Msg.Bms_Address = 0x01;
- Uart_Receive_Msg.Bms_Read_Funcode = 0x03;
- uint8_t *Uart_Rece_buffer;
- Batt_Cell_Num_2 = Batt_Cell_Num<<1;
- Uart_Rece_buffer = (uint8_t *)malloc(Uart_Rece_LEN);
- while(1)
- {
- switch(gProc1State)
- {
- case PROCESS_Uart_STATE_IDLE:
- {
- NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
- Rece_index = 0;
- break;
- }
- case PROCESS_Uart_STATE_CHECK://检查电流数值
- {
- memset(Uart_Rece_buffer,0x00,8);
- 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;
- Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
- Uart_Rece_BattI = *(Uart_Reve_Point+0)<<8 |*(Uart_Reve_Point+1);
- #ifdef USING_PRINTF
- printf("Check_Current!\n");
- #endif
- PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
- break;
- }
- case PROCESS_Uart_STATE_WORK:
- {
- NetSocDisplay(LED_SOC_1,LED_TURN_ON);
- memset(Uart_Rece_buffer,0xff,Uart_Rece_LEN);
- switch(Rece_index)
- {
- case 0://读取电流
- {
- 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;
- Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
- Uart_Rece_BattI = *(Uart_Reve_Point+0)<<8 |*(Uart_Reve_Point+1);
- break;
- }
- case 1://读取单体电压
- {
- 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;
- Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
- // #ifdef USING_PRINTF
- // printf("BattCellV: ");
- // for (size_t i = 0; i < Batt_Cell_Num_2; i++)
- // {
- // printf("%x ",*(Uart_Reve_Point+i));
- // }
- // printf("\n");
- // #endif
- battbuffer[30] = Batt_Cell_Num;
- memcpy(&battbuffer[31],Uart_Reve_Point,Batt_Cell_Num_2);
- break;
- }
- case 2://读取温度
- {
- 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;
- Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
- 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_Reve_Point+2*i+1);
- }
- // #ifdef USING_PRINTF
- // printf("BattCellT: ");
- // for (size_t i = 0; i < Batt_Temp_Num; i++)
- // {
- // printf("%x ",*(Uart_Reve_Point+2*i+1));
- // }
- // printf("\n");
- // #endif
- break;
- }
- case 3://读取总电压,目前保护板只有一个电压
- {
- 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;
- Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
- battbuffer[19] = *(Uart_Reve_Point+0);//Link U
- battbuffer[20] = *(Uart_Reve_Point+1);
- battbuffer[21] = *(Uart_Reve_Point+0);//Pack U
- battbuffer[22] = *(Uart_Reve_Point+1);
- break;
- }
- case 4://读取状态及SOC
- {
- 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;
- Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
- battbuffer[23] = *(Uart_Reve_Point+0)>>1;//mos状态
- battbuffer[24] = *(Uart_Reve_Point+5);//SOC
- battbuffer[25] = *(Uart_Reve_Point+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;
- Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
- memcpy(&battbuffer[26],Uart_Reve_Point,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;
- Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
- memcpy(&battbuffer[34+Batt_Cell_Num_2+Batt_Temp_Num],Uart_Reve_Point,4);
- break;
- }
- default:
- {
- PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
- break;
- }
- }
- Rece_index++;
- break;
- }
- case PROCESS_Uart_STATE_SLEEP:
- {
- free(Uart_Rece_buffer);
- slpManPlatVoteEnableSleep(process1SlpHandler, SLP_SLP2_STATE);
- while (1)
- {
- ;
- }
-
- break;
- }
- }
- }
- }
- static void process2AppTask(void* arg)
- {
- PROC_Can_STATE_SWITCH(PROCESS_STATE_IDLE);
- uint32_t Can_ID;
- NVIC_EnableIRQ(PadWakeup1_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;
- uint16_t Batt_Cell_addU = 0x0000;
- Can_Rece_buffer[0]=0xff;
- slpManApplyPlatVoteHandle("pro2",&process2SlpHandler);
- slpManPlatVoteDisableSleep(process2SlpHandler, SLP_SLP2_STATE);
- while(1)
- {
- switch(gProc2State)
- {
- case PROCESS_STATE_IDLE:
- {
- HAL_Can_Receive(Can_Rece_buffer);
- osDelay(10);
- send_index = 0;
- break;
- }
- case PROCESS_STATE_WORK:
- {
- 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:
- {
- PROC_Can_STATE_SWITCH(PROCESS_STATE_IDLE);
- }
- }
- send_index ++;
- break;
- }
- case PROCESS_STATE_SLEEP:
- {
- slpManPlatVoteEnableSleep(process2SlpHandler, SLP_SLP2_STATE);
- while (1)
- {
- ;
- }
-
- break;
- }
- }
- }
- }
- uint8_t bcc_chk(uint8_t* data, uint8_t length)
- {
- uint8_t bcc_chk_return = 0x00;
- uint8_t count = 0;
- while (count<length)
- {
- bcc_chk_return^=data[count];
- count++;
- }
- return bcc_chk_return;
- }
- void TcpCallBack(void)
- {
- #ifdef USING_PRINTF
- printf("[%d]TcpCallBack\n",__LINE__);
- #endif
- }
- void Tcp_Data_Assemble(uint8_t datatype)
- {
- int16_t Batt_current;
- uint8_t csq=0;
- int8_t snr=0;
- int8_t rsnr=0;
- Batt_current = Uart_Rece_BattI;
- OsaUtcTimeTValue timestracture;
- appGetSystemTimeUtcSync(×tracture);
- battbuffer[0] = timestracture.UTCtimer1>>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;
- switch (datatype)
- {
- case 0x80:
- {
- 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,加1000的偏移量,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++;
- break;
- }
- default:
- break;
- }
- return;
- }
- void GGACallBack(UINT8 res, UINT8 * params)
- {
- #ifdef USING_PRINTF
- printf("GPSCallBack,len:%d,GPS:%s\n",res,params);
- #endif
- }
- static void process3AppTask(void* arg)
- {
- CHAR SN[] = "GYTEST00000000003";
- CHAR serverip[] = "47.97.127.222";
- UINT16 serverport = 8712;
- int TcpConnectID = -1;
- int TcpSendID = -1;
- int NB_send_len=59+Batt_Cell_Num_2+Batt_Temp_Num;//设定网络发送最大数值
- slpManApplyPlatVoteHandle("pro3",&process3SlpHandler);
- slpManPlatVoteDisableSleep(process3SlpHandler, SLP_SLP2_STATE);
- PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_IDLE);
- uint8_t* TcpSendBuffer;
- TcpSendBuffer = (uint8_t *)malloc(NB_send_len);//申请发送的数据内存
- while(1)
- {
- switch(gProc3State)
- {
- case PROCESS_NB_STATE_IDLE:
- {
- osDelay(100);
- break;
- }
- case PROCESS_NB_STATE_CONNECT:
- {
- while(TcpConnectID<0)
- {
-
- TcpConnectID = tcpipConnectionCreate(1,PNULL,PNULL,serverip,serverport,TcpCallBack);
- osDelay(100);
- #ifdef USING_PRINTF
- printf("ConnectID:%d\n ",TcpConnectID);
- #endif
- }
- PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_WORK);
- break;
- }
- case PROCESS_NB_STATE_WORK:
- {
- posGGAServiceStart(GGACallBack);
- memset(TcpSendBuffer,0x00,92);
- *(TcpSendBuffer+0) = 0x23;
- *(TcpSendBuffer+1) = 0x23;
- *(TcpSendBuffer+2) = 0x02;
- *(TcpSendBuffer+3) = 0xfe;
- memcpy(TcpSendBuffer+4,SN,17);
- *(TcpSendBuffer+21) = 0x01;//不加密
- Tcp_Data_Assemble(0x80);
-
- *(TcpSendBuffer+22) = data_index>>8;//数据长度
- *(TcpSendBuffer+23) = data_index;//数据长度
- memcpy(TcpSendBuffer+24,battbuffer,data_index);
- #ifdef USING_PRINTF
- printf("battbuffer:");
- for (int i = 0; i < data_index; i++)
- {
- printf("%x ",battbuffer[i]);
- }
- printf("\n ");
- #endif
- *(TcpSendBuffer+NB_send_len-1) = bcc_chk(TcpSendBuffer,NB_send_len-1);
- // #ifdef USING_PRINTF
- // printf("[%d]sizeof:%d \r\n",__LINE__,sizeof(TcpSendBuffer)-1);
- // #endif
- // #ifdef USING_PRINTF
- // printf("[%d]Tcpchk:%#X \r\n",__LINE__,*(TcpSendBuffer+91));
- // #endif
- TcpSendID = tcpipConnectionSend(TcpConnectID,TcpSendBuffer,NB_send_len,PNULL,PNULL,PNULL);
- // #ifdef USING_PRINTF
- // printf("ConnectID:%d,TcpSend:%d,data length:%d,Data: ",TcpConnectID,TcpSendID,NB_send_len);
- // for (int i = 0; i < NB_send_len; i++)
- // {
- // printf("%x ",*(TcpSendBuffer+i));
- // }
- // printf("\n ");
- // #endif
- PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_IDLE);
- break;
- }
- case PROCESS_NB_STATE_SLEEP:
- {
- tcpipConnectionClose(TcpConnectID);
- posGGAServiceStop();
- free(TcpSendBuffer);
- slpManPlatVoteEnableSleep(process3SlpHandler, SLP_SLP2_STATE); // cancel the prohibition of sleep2
- while(1);
- 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,PROC0_TASK_STACK_SIZE);
- task_attr.name = "Process0AppTask";
- task_attr.stack_mem = gProcessTaskStack0;
- task_attr.stack_size = PROC0_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,PROC1_TASK_STACK_SIZE);
- task_attr.name = "Process1AppTask";
- task_attr.stack_mem = gProcessTaskStack1;
- task_attr.stack_size = PROC1_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,PROC2_TASK_STACK_SIZE);
- task_attr.name = "Process2AppTask";
- task_attr.stack_mem = gProcessTaskStack2;
- task_attr.stack_size = PROC2_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);
- }
- void process3Init(void)
- {
- osThreadAttr_t task_attr;
- memset(&task_attr,0,sizeof(task_attr));
- memset(gProcessTaskStack3, 0xA5,PROC3_TASK_STACK_SIZE);
- task_attr.name = "Process3AppTask";
- task_attr.stack_mem = gProcessTaskStack3;
- task_attr.stack_size = PROC3_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();//任务调度和检测程序
- process1Init();//Uart程序
- process2Init();//Can程序
- 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);
- }
|