1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549 |
- /*******************************
- *
- * 骑享程序测试
- *
- * ****************************/
- #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 PROC_MAIN_TASK_STACK_SIZE (1024*2)
- #define PROC_UART_TASK_STACK_SIZE (1024)
- #define PROC_CAN_TASK_STACK_SIZE (1024)
- #define PROC_TCP_TASK_STACK_SIZE (2048)
- #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 battbuffer_len (110)
- #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;
- QueueHandle_t gpsMsgQueue = NULL;
- GPS_INFO Gps_Data;
- static volatile UINT32 Event;
- static QueueHandle_t psEventQueueHandle;
- static UINT8 gImsi[16] = {0};
- static INT32 sockfd = -1;
- static UINT32 gCellID = 0;
- int TcpconnectID = -1;
- volatile uint8_t Tcp_statuts = 0;
- //slpManTimerID_e Timer_ID;
- //#define BAT6060
- #define BAT4830
- #ifdef BAT4830
- uint8_t Batt_Cell_Num = 14;
- uint8_t Batt_Temp_Num = 5;
- #else
- uint8_t Batt_Cell_Num = 17;
- uint8_t Batt_Temp_Num = 7;
- #endif
- CHAR SN[] = "GY483000000000001";//SN应写到osfile里面
- uint8_t Batt_Cell_Num_2 ;//默认数值
- int16_t Uart_Rece_BattI=0x0000;
- uint8_t Can_Rece_buffer[8];
- volatile bool Can_Enable = false;
- uint16_t data_index = 0x0000;
- volatile bool Sleep_Flag = false;
- volatile bool Uart_Sleep_Flag = false;
- volatile bool Can_Sleep_Flag = false;
- volatile bool Tcp_Sleep_Flag = false;
- uint8_t battbuffer[battbuffer_len];//电池数据都存在此数组中————电压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
- * */
- //GPS信息
- uint8_t gpsbuffer[30];//定位都存在此数组中
- /**
- * 存放规则如下:
- * 位置: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
- * 数据: 年 月 日 时 分 秒 信息体标志 年 月 日 时 分 秒 定位标志 卫星数目 航向H 航向L
- *
- * 17 18 19 20 21 22 23 24 25 26 27 28
- * 速度H 速度L 海拔H 海拔L 纬度HH 纬度HL 纬度LH 纬度LL 经度HH 经度HL 经度LH 经度LL
- *
- * */
- //GPS信息
- //状态机定义
- 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_MAIN_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];
- #ifdef BL_FILE_LOG
- uint8_t lockoutState=0;
- #endif
- //睡眠进出函数
- 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;
- uint32_t sleep_time_flag;
- int32_t inParam = 0xAABBCCDD;
- uint32_t Chrgend_Work_time = 180000;
- uint32_t Wakeup_Work_time = 5*60*100;
- uint32_t Sleep_Time = 5*60*1000;
- //上述参数应写入文件里
- slpManWakeSrc_e Wakeup_source;
- 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);
- #ifdef BL_FILE_LOG
- if(lockoutState)
- bluejoy_del_logfile();
- bluejoy_read_logfile();
- bluejoy_printf(BL_LEVEL3,"processAppTask\n");
- #endif
- slpManSlpState_t slpstate = slpManGetLastSlpState();
- #ifdef USING_PRINTF
- printf("slpstate:%d \n",slpstate);
- #endif
- #ifdef BL_FILE_LOG
- bluejoy_printf(BL_LEVEL3,"Last slpstate:%d \n",slpstate);
- #endif
- Wakeup_source = slpManGetWakeupSrc();//获取唤醒源
- #ifdef USING_PRINTF
- printf("Wakeup_source:%d \n",Wakeup_source);
- #endif
- #ifdef BL_FILE_LOG
- bluejoy_printf(BL_LEVEL3,"Wakeup_source:%d \n",Wakeup_source);
- #endif
- if (Wakeup_source==0)
- {
- sleep_time_flag = Wakeup_Work_time;
- }
- else if (Wakeup_source==1)
- {
- sleep_time_flag = Wakeup_Work_time;
- }
- else
- {
- sleep_time_flag = Chrgend_Work_time;
- }
- if((slpstate == SLP_SLP2_STATE) || (slpstate == SLP_HIB_STATE))
- {
- PROC_MAIN_STATE_SWITCH(PROCESS_STATE_WORK);
- }
- else
- {
- PROC_MAIN_STATE_SWITCH(PROCESS_STATE_WORK);
- }
- // if (Timer_ID!=7)
- // {
- // PROC_MAIN_STATE_SWITCH(PROCESS_STATE_SLEEP);
- // }
- // else
- // {
- // PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE);
- // }
-
- //线程初始化完成
- 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 >=500)//Tcp 5s 调用一次
- {
- PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_LINK);
- Tcp_index = 0;
- }
- if((!Can_Enable)&&((Uart_Rece_BattI==0x0000)||(Uart_Rece_BattI==0xffff)))
- {
- sleep_index++;
- }
- else
- {
- sleep_index = 0;
- }
- if ((Uart_Rece_BattI!=0x0000)&&(Uart_Rece_BattI<=0x8000))
- {
- sleep_time_flag = Chrgend_Work_time;
- }
- if (sleep_index >=sleep_time_flag)
- {
- PROC_MAIN_STATE_SWITCH(PROCESS_STATE_SLEEP);
- sleep_index = 0;
- }
- break;
- }
- case PROCESS_STATE_SLEEP:
- {
- while(true)
- {
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_SLEEP);
- PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SLEEP);
- PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_SLEEP);
- Sleep_Flag = TRUE;
- if((Uart_Sleep_Flag)&&(Can_Sleep_Flag)&&(Tcp_Sleep_Flag))
- break;
- }
- #ifdef BL_FILE_LOG
- bluejoy_printf(BL_LEVEL3,"sleep\n");
- #endif
- osDelay(10000);
- #ifdef USING_PRINTF
- printf("Ms_sleep:uart_%d,can_%d,tcp_%d\r\n",gProcess_Uart_Task,gProcess_Can_Task,gProcess_Tcp_Task);
- #endif
- slpManSlpState_t State;
- uint8_t cnt;
- if(slpManCheckVoteState(MainSlpHandler, &State, &cnt)==RET_TRUE)
- {
- #ifdef USING_PRINTF
- printf("[%d]We Can Check Vote Main State, state=%d, cnt=%d\r\n",__LINE__,State,cnt);
- #endif
- }
- slpManPlatVoteForceEnableSleep(MainSlpHandler, SLP_SLP2_STATE); //增加强制投票
- if(slpManCheckVoteState(MainSlpHandler, &State, &cnt)==RET_TRUE)
- {
- #ifdef USING_PRINTF
- printf("[%d]We Can Check Vote Main State Again, state=%d, cnt=%d\r\n",__LINE__,State,cnt);
- #endif
- }
- #ifdef USING_PRINTF
- printf("[%d]Waiting sleep\r\n",__LINE__);
- #endif
- FaultDisplay(LED_TURN_ON);
- slpManSlpState_t slpstate = slpManPlatGetSlpState();
- #ifdef USING_PRINTF
- printf("which slpstate can go now :%d \n",slpstate);
- #endif
- slpManDeepSlpTimerStart(deepslpTimerID, Sleep_Time);
- while(1)
- {
- if(!slpManDeepSlpTimerIsRunning(deepslpTimerID))
- {
- slpManDeepSlpTimerDel(deepslpTimerID);
- }
- slpManDeepSlpTimerStart(deepslpTimerID, Sleep_Time);
- osDelay(60000/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++;
- osDelay(100);
- if (timeout>=10)
- {
- timeout =0;
- isRecvTimeout = true;
- break;
- }
- }
- // #ifdef USING_PRINTF
- // printf("Uart_Rece_buffer1: ");
- // for(int i=0;i<Data_Len;i++)
- // {
- // printf("%x ",*(Uart_Rece_buffer+i));
- // }
- // #endif
- if (isRecvComplete == true)
- {
- 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);
- // #ifdef USING_PRINTF
- // printf("Uart_Rece_buffer2: ");
- // for(int i=0;i<Data_Len;i++)
- // {
- // printf("%x ",*(Uart_Rece_buffer+i));
- // }
- // printf("\ncrcchk:%x,%x\n ",CRC_chk_buffer,CRC_Rece_buffer);
- // #endif
- if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
- {
- return Uart_Rece_buffer;//此处指针移位出现重启问题
- }
- else //接收数据的校验不过
- {
- USARTdrv->Uninitialize();
- osDelay(1000);
- 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);
- memset(Uart_Rece_buffer,0xff,Data_Len);
- return Uart_Rece_buffer;
- }
- }
- else
- {
- osDelay(100);
- memset(Uart_Rece_buffer,0x00,Data_Len);
- isRecvTimeout = false;
- return Uart_Rece_buffer;
- }
- }
- void GGACallBack(UINT8 res, UINT8 * params)
- {
- #ifdef USING_PRINTF
- printf("GPS:len=%d,data=%s\r\n",res,params);
- #else
- ECOMM_STRING(UNILOG_PLA_STRING, GPS_DATA, P_INFO, "gps:%s", params);
- #endif
- }
- //Uart线程
- static void Uart_Task(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;
- volatile uint8_t Data_Len;
- Uart_Receive_Type Uart_Receive_Msg;
- memset(&battbuffer[0],0x00,battbuffer_len);
- 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:
- {
- 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:
- {
- #ifdef USING_PRINTF
- //printf("UART_STATE_WORK!\n");
- #endif
- 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://最高最低温度和加热和充放电MOS控制
- {
- 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 = 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);
- memcpy(&battbuffer[34+Batt_Cell_Num_2+Batt_Temp_Num],Uart_Rece_buffer+3,4);
- memcpy(&battbuffer[32+Batt_Cell_Num_2+Batt_Temp_Num],Uart_Rece_buffer+3+6,2);
- 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:
- {
- USARTdrv->PowerControl(ARM_POWER_LOW);
- slpManPlatVoteEnableSleep(UartSlpHandler, SLP_SLP2_STATE);
- #ifdef USING_PRINTF
- printf("UART_STATE_SLEEP\n");
- #endif
- Uart_Sleep_Flag = true;
- while(1)
- {
- osDelay(60000/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);
- posGGAServiceStart(GGACallBack);
- posGGAReset();
- 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:
- {
- //加入GPS启动程序
- if(gpsMsgQueue == NULL)
- {
- gpsMsgQueue = osMessageQueueNew(1,sizeof(GPS_INFO), NULL);
- }
- osMessageQueueGet(gpsMsgQueue, &Gps_Data, 0, 1000);
- #ifdef USING_PRINTF
- printf("Gpsmsgqueue: %d,%d,%d,%d\r\n",Gps_Data.latitude,Gps_Data.longitude,Gps_Data.speed,Gps_Data.direction);
- #endif
- //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
- {
- PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_IDLE);
- Can_Enable = false;
- }
- break;
- }
- case PROCESS_CAN_STATE_SEND:
- {
- #ifdef USING_PRINTF
- printf("CAN_STATE_SEND!\n");
- #endif
- Can_Flag=false;
- while(!Can_Flag)
- {
- switch(send_index)
- {
- case 0:
- {
- Can_ID = 0x001;
- for (int i = 0; i < 8; i++)
- {
- Can_TxMsg.Data[i] = battbuffer[i+31+0*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+1*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+2*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:
- {
- #ifdef BAT4830
- Can_ID = 0x031;
- for (int i = 0; i < 4; i++)
- {
- Can_TxMsg.Data[i] = battbuffer[i+31+3*8];
- }
- Can_TxMsg.stdIDH = Can_ID>>3;
- Can_TxMsg.stdIDL = Can_ID<<5;
- Can_TxMsg.DLC = 8;
- HAL_Can_Transmit(Can_TxMsg);
- #else
- Can_ID = 0x031;
- for (int i = 0; i < 8; i++)
- {
- Can_TxMsg.Data[i] = battbuffer[i+31+3*8];
- }
- Can_TxMsg.stdIDH = Can_ID>>3;
- Can_TxMsg.stdIDL = Can_ID<<5;
- Can_TxMsg.DLC = 8;
- HAL_Can_Transmit(Can_TxMsg);
- Can_ID = 0x041;
- for (int i = 0; i < 2; i++)
- {
- Can_TxMsg.Data[i] = battbuffer[i+31+4*8];
- }
- for (int i = 0; i < 6; i++)
- {
- Can_TxMsg.Data[i+2] = 0x00;
- }
- Can_TxMsg.stdIDH = Can_ID>>3;
- Can_TxMsg.stdIDL = Can_ID<<5;
- Can_TxMsg.DLC = 8;
- HAL_Can_Transmit(Can_TxMsg);
- #endif
- 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:
- {
- Can_ID = 0x200;
- 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])/10;
- }
- 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:
- {
- posGGAServiceStop();
- HAL_Can_Sleep();
- slpManSlpState_t State;
- uint8_t cnt;
- if(slpManCheckVoteState(CanSlpHandler, &State, &cnt)==RET_TRUE)
- {
- #ifdef USING_PRINTF
- printf("[%d]We Can Check Vote Can State, state=%d, cnt=%d\r\n",__LINE__,State,cnt);
- #endif
- }
- slpManPlatVoteEnableSleep(CanSlpHandler, SLP_SLP2_STATE);
- if(slpManCheckVoteState(CanSlpHandler, &State, &cnt)==RET_TRUE)
- {
- #ifdef USING_PRINTF
- printf("[%d]We Can Check Vote can State Again, state=%d, cnt=%d\r\n",__LINE__,State,cnt);
- #endif
- }
- Can_Sleep_Flag = true;
- while(true)
- {
- osDelay(60000/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");
- }
- }
- }
- // void slpManTimerCb(slpManTimerID_e ID)
- // {
- // Timer_ID = ID;
- // }
- 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);
- Tcp_statuts = 1;
- 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<length)
- {
- bcc_chk_return^=data[count];
- count++;
- }
- return bcc_chk_return;
- }
- //电池数据组装函数
- void Tcp_Batt_Data_Assemble(void)
- {
- int16_t Batt_current;
- uint16_t BattU = 0x00;
- 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;
- 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 = Batt_current/10;
- Batt_current = 0x2710 - Batt_current;
- Batt_current = Batt_current;
- }
- else
- {
- Batt_current = Batt_current/10;
- Batt_current = Batt_current+0x2710;
- Batt_current = Batt_current;
- }
- battbuffer[17] = Batt_current>>8;
- battbuffer[18] = Batt_current;
- BattU =( battbuffer[19]<<8|battbuffer[20])/10;
- battbuffer[19] = BattU>>8;
- battbuffer[20] = BattU;
- battbuffer[21] = BattU>>8;
- battbuffer[22] = BattU;//外电压
- data_index = 32+Batt_Cell_Num_2+Batt_Temp_Num;
- if(Batt_current==10000)
- {
- battbuffer[data_index] = 0x00;//电池状态
- }
- else if(Batt_current>10000)
- {
- battbuffer[data_index] = 0x02;//电池状态
- }
- else if(Batt_current<10000)
- {
- battbuffer[data_index] = 0x01;//电池状态
- }
-
- data_index++;
- //battbuffer[data_index] = 0x00;//是否加热
- data_index++;
- }
- //Gps 数据组装函数
- void Tcp_Gps_Data_Assemble(void)
- {
- OsaUtcTimeTValue timestracture;
- appGetSystemTimeUtcSync(×tracture);
- gpsbuffer[0] = timestracture.UTCtimer1>>16;
- gpsbuffer[0] = gpsbuffer[0] - 0x07D0;
- gpsbuffer[1] = timestracture.UTCtimer1>>8;
- gpsbuffer[2] = timestracture.UTCtimer1;
- gpsbuffer[3] = timestracture.UTCtimer2>>24;
- gpsbuffer[4] = timestracture.UTCtimer2>>16;
- gpsbuffer[5] = timestracture.UTCtimer2>>8;
- gpsbuffer[6] = 0x82;//信息体标志,此处为GPS信息
- gpsbuffer[7] = gpsbuffer[0];//年
- gpsbuffer[8] = gpsbuffer[1];//月
- gpsbuffer[9] = gpsbuffer[2];//日
- gpsbuffer[10] = gpsbuffer[3];//时 0时区时间
- gpsbuffer[11] = gpsbuffer[4];//分
- gpsbuffer[12] = gpsbuffer[5];//秒
- gpsbuffer[13] = Gps_Data.status;
- gpsbuffer[14] = Gps_Data.satellite_num;
- gpsbuffer[15] = Gps_Data.direction>>8;
- gpsbuffer[16] = Gps_Data.direction;
- gpsbuffer[17] = Gps_Data.speed>>8;
- gpsbuffer[18] = Gps_Data.speed;
- gpsbuffer[19] = Gps_Data.altitude>>8;
- gpsbuffer[20] = Gps_Data.altitude;
- gpsbuffer[21] = Gps_Data.latitude>>24;
- gpsbuffer[22] = Gps_Data.latitude>>16;
- gpsbuffer[23] = Gps_Data.latitude>>8;
- gpsbuffer[24] = Gps_Data.latitude;
- gpsbuffer[25] = Gps_Data.longitude>>24;
- gpsbuffer[26] = Gps_Data.longitude>>16;
- gpsbuffer[27] = Gps_Data.longitude>>8;
- gpsbuffer[28] = Gps_Data.longitude;
- }
- //Tcp线程
- static void Tcp_Task(void* arg)
- {
- CHAR serverip[] = "47.97.127.222";
- CHAR serverport[] = "8712";
- struct addrinfo hints, *server_res;
- int TcpsendID = -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*));
- 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;
- uint8_t pmode;
- uint32_t tau;
- uint32_t act;
- uint8_t NB_send_len;
- uint8_t Reconnect_Num = 0;
- appSetEDRXSettingSync(0,5,1800000);
- appSetPSMSettingSync(1,3*60*60,10);
- slpManWakeSrc_e Wakeup_source;
- Wakeup_source = slpManGetWakeupSrc();//获取唤醒源
- if(Wakeup_source==0)
- {
- Tcp_statuts = 0;
- }
- else
- {
- Tcp_statuts = 1;
- }
- NetSocDisplay(LED_SOC_2,LED_TURN_ON);
- appSetCFUN(1);
- while(true)
- {
- switch(gProcess_Tcp_Task)
- {
- case PROCESS_TCP_STATE_IDLE:
- {
- osDelay(100);
- break;
- }
- case PROCESS_TCP_STATE_LINK:
- {
- #ifdef USING_PRINTF
- printf("TCP_statuts:%d\n",Tcp_statuts);
- #endif
- switch(Tcp_statuts)
- {
- case 0://0代表网络为驻网前
- {
- #ifdef USING_PRINTF
- printf("TCP Not Ready\n");
- #endif
- PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
- break;
- }
- case 1://1代表网络驻网
- {
- if (getaddrinfo( serverip, serverport , &hints, &server_res ) != 0 )
- {
- #ifdef USING_PRINTF
- printf("TCP connect unresolved dns\n");
- #endif
- #ifdef BL_FILE_LOG
- bluejoy_printf(BL_LEVEL3,"TCP connect unresolved dns\n");
- #endif
- }
- Tcp_statuts=2;
- break;
- }
- case 2://2代表尝试建立连接
- {
- #ifdef USING_PRINTF
- printf("Connecting...!%d\n",TcpconnectID);
- #endif
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if(sockfd<0)
- {
- #ifdef USING_PRINTF
- printf("socket create error\n");
- #endif
- #ifdef BL_FILE_LOG
- bluejoy_printf(BL_LEVEL3,"socket create error\n");
- #endif
- Tcp_statuts=1;
- }
- else
- {
- TcpconnectID = connect(sockfd, (struct sockaddr *) server_res->ai_addr, server_res->ai_addrlen);
- if(TcpconnectID<0 && errno != EINPROGRESS)
- {
- #ifdef USING_PRINTF
- printf("Connect failed!%d,Reconnect:%d\n",TcpconnectID,Reconnect_Num);
- #endif
- close(sockfd);
- Tcp_statuts=2;
- PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
- Reconnect_Num++;
- NetSocDisplay(LED_SOC_0,LED_TURN_ON);
- }
- else
- {
- NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
- Tcp_statuts=3;
- Reconnect_Num = 0;
- }
- if(Reconnect_Num>=20)
- {
- #ifdef BL_FILE_LOG
- bluejoy_printf(BL_LEVEL3,"20 Connect failed!%d,Reconnect:%d\n",TcpconnectID,Reconnect_Num);
- #endif
- appSetCFUN(0);
- osDelay(10);
- appSetCFUN(1);
- PSInitEventCallbackTable();
- Tcp_statuts=1;
- Reconnect_Num = 0;
- }
- }
- break;
-
- }
- case 3://3代表连接建立成功
- {
- PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_WORK);
- break;
- }
- case 4://4代表连接断开
- {
- Tcp_statuts=2;
- break;
- }
- }
- break;
- }
- case PROCESS_TCP_STATE_WORK:
- {
- #ifdef USING_PRINTF
- printf("TCP_STATE_WORK!\n");
- #endif
- appGetPSMSettingSync(&pmode,&tau,&act);
- #ifdef USING_PRINTF
- printf("psm:pmode-%d,tau-%d,act-%d!\n",pmode,tau,act);
- #endif
- Tcp_Flag = false;
- while (!Tcp_Flag)
- {
- switch(Tcp_Index)
- {
- case 0://发送登录信息
- {
- Tcp_Index=1;
- break;
- }
- case 1://发送电池数据
- {
- NB_send_len=59+Batt_Cell_Num_2+Batt_Temp_Num;//电池数据长度
- TcpSendBuffer = (uint8_t *)malloc(NB_send_len);
- data_index = 0;
- 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
- free(TcpSendBuffer);
- if (TcpsendID<0)
- {
- #ifdef BL_FILE_LOG
- bluejoy_printf(BL_LEVEL3,"send failed,ConnectID:%d,TcpSend:%d,data length:%d\n",sockfd,TcpsendID,NB_send_len);
- #endif
- closesocket(sockfd);
- TcpconnectID = -1;
- Tcp_Index=-1;
- Tcp_statuts = 4;
- }
- else
- {
- Tcp_Index=2;
- }
- break;
- }
- case 2:
- {
- data_index = 29;
- NB_send_len = 25+29;//数据总长度:25个固定+29个GPS长度
- TcpSendBuffer = (uint8_t *)malloc(NB_send_len);
- 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_Gps_Data_Assemble();//Gps数据组装
- *(TcpSendBuffer+22) = data_index>>8;//数据长度
- *(TcpSendBuffer+23) = data_index;//数据长度
- memcpy(TcpSendBuffer+24,gpsbuffer,data_index);
- *(TcpSendBuffer+NB_send_len-1) = bcc_chk(TcpSendBuffer,NB_send_len-1);
- // #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
- TcpsendID = send(sockfd, TcpSendBuffer, NB_send_len, 0 );
- free(TcpSendBuffer);
- if (TcpsendID<0)
- {
- closesocket(sockfd);
- TcpconnectID = -1;
- Tcp_Index=-1;
- Tcp_statuts = 4;
- NetSocDisplay(LED_SOC_1,LED_TURN_ON);
- }
- else
- {
- NetSocDisplay(LED_SOC_1,LED_TURN_OFF);
- Tcp_Index=3;
- }
- break;
- }
- default:
- {
- PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
- Tcp_Index = 0;
- Tcp_Flag = true;
- }
- }
- }
- break;
- }
- case PROCESS_TCP_STATE_SLEEP:
- {
- slpManPlatVoteEnableSleep(TcpSlpHandler, SLP_SLP2_STATE);
- closesocket(sockfd);
- //deregisterPSEventCallback(socketRegisterPSUrcCallback);//注销NB网络事件函数
- #ifdef USING_PRINTF
- printf("TCP_STATE_SLEEP\n");
- #endif
- Tcp_Sleep_Flag = TRUE;
- appSetCFUN(0);
- 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
- //slpManDeepSlpTimerRegisterExpCb(slpManTimerCb);
- 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 = osPriorityBelowNormal7;
- 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 = osPriorityBelowNormal7;
- 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 = osPriorityBelowNormal7;
- 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();
- GpsTaskInit();
- Tcp_Task_Init();
- }
- //主函数入口
- void main_entry(void) {
- BSP_CommonInit();
- osKernelInitialize();
- registerAppEntry(appInit, NULL);
- if (osKernelGetState() == osKernelReady)
- {
- osKernelStart();
- }
- while(1);
- }
|