|
@@ -1,1673 +0,0 @@
|
|
-/*******************************
|
|
|
|
- *
|
|
|
|
- * 骑享程序测试
|
|
|
|
- *
|
|
|
|
- * ****************************/
|
|
|
|
-#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)
|
|
|
|
-#define PROC_UART_TASK_STACK_SIZE (1024)
|
|
|
|
-#define PROC_CAN_TASK_STACK_SIZE (1024)
|
|
|
|
-#define PROC_TCP_TASK_STACK_SIZE (1024)
|
|
|
|
-#define GPS_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;
|
|
|
|
-static QueueHandle_t gpsMsgQueue = NULL;
|
|
|
|
-static QueueHandle_t norGpsHandle = NULL;
|
|
|
|
-static osThreadId_t gpsTaskHandle = 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;
|
|
|
|
-uint8_t Batt_Cell_Num = 17;//默认数值14、17
|
|
|
|
-uint8_t Batt_Cell_Num_2 ;//默认数值
|
|
|
|
-uint8_t Batt_Temp_Num = 7;//默认数值5、7
|
|
|
|
-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];
|
|
|
|
-static UINT8 gpsTaskStack[GPS_TASK_STACK_SIZE];
|
|
|
|
-static StaticTask_t gpsTask = NULL;
|
|
|
|
-//睡眠进出函数
|
|
|
|
-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 = 12000;
|
|
|
|
- uint32_t Sleep_Time = 300000;
|
|
|
|
- //上述参数应写入文件里
|
|
|
|
- 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);
|
|
|
|
-
|
|
|
|
- slpManSlpState_t slpstate = slpManGetLastSlpState();
|
|
|
|
-
|
|
|
|
- #ifdef USING_PRINTF
|
|
|
|
- printf("slpstate:%d \n",slpstate);
|
|
|
|
- #endif
|
|
|
|
- Wakeup_source = slpManGetWakeupSrc();//获取唤醒源
|
|
|
|
- #ifdef USING_PRINTF
|
|
|
|
- printf("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:
|
|
|
|
- {
|
|
|
|
- NetSocDisplay(LED_SOC_0,LED_TURN_ON);
|
|
|
|
- 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;
|
|
|
|
- }
|
|
|
|
- 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
|
|
|
|
- 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://最高最低温度和加热和充放电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(norGpsHandle);
|
|
|
|
-
|
|
|
|
- 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\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:
|
|
|
|
- {
|
|
|
|
- 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);
|
|
|
|
-
|
|
|
|
- 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 SN[] = "GYTEST00000000002";//SN应写到osfile里面
|
|
|
|
- CHAR serverip[] = "47.97.127.222";
|
|
|
|
- CHAR serverport[] = "8712";
|
|
|
|
- struct addrinfo hints, *server_res;
|
|
|
|
- int TcpsendID = -1;
|
|
|
|
- int TcprecvID = -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;
|
|
|
|
- uint8_t* TcpRecvBuffer=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;
|
|
|
|
- }
|
|
|
|
- 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
|
|
|
|
- }
|
|
|
|
- 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
|
|
|
|
- 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++;
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- Tcp_statuts=3;
|
|
|
|
- Reconnect_Num = 0;
|
|
|
|
- }
|
|
|
|
- if(Reconnect_Num>=20)
|
|
|
|
- {
|
|
|
|
- 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);
|
|
|
|
- TcpRecvBuffer = (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 );
|
|
|
|
- TcprecvID = recv(sockfd, TcpRecvBuffer, 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");
|
|
|
|
- printf("ConnectID:%d,TcpRecv:%d,data length:%d,Data: ",sockfd,TcprecvID,NB_send_len);
|
|
|
|
- for (int i = 0; i < NB_send_len; i++)
|
|
|
|
- {
|
|
|
|
- printf("%x ",*(TcpRecvBuffer+i));
|
|
|
|
- }
|
|
|
|
- printf("\n");
|
|
|
|
- #endif
|
|
|
|
- free(TcpSendBuffer);
|
|
|
|
- free(TcpRecvBuffer);
|
|
|
|
- if (TcpsendID<0)
|
|
|
|
- {
|
|
|
|
- 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;
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- 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 strdel(char * str,char c)
|
|
|
|
-{
|
|
|
|
- char *p = str;
|
|
|
|
- while(*str)
|
|
|
|
- {
|
|
|
|
- if(*str!=c)
|
|
|
|
- *p++ = *str;
|
|
|
|
- str++;
|
|
|
|
- }
|
|
|
|
- *p = '\0';
|
|
|
|
-}
|
|
|
|
-uint32_t location_handle(char *in1)
|
|
|
|
-{
|
|
|
|
- uint32_t location_temp;
|
|
|
|
- uint32_t location_degree;
|
|
|
|
- uint32_t location_min;
|
|
|
|
- location_temp = atol(in1);
|
|
|
|
- location_degree = location_temp/(1e7);
|
|
|
|
- location_degree = location_degree*(1e6);
|
|
|
|
- location_min = location_temp-location_degree*10;
|
|
|
|
- location_min = location_min/6;
|
|
|
|
- location_temp = location_degree+location_min;
|
|
|
|
- return location_temp;
|
|
|
|
-}
|
|
|
|
-/**
|
|
|
|
- \fn void gpsProcess(void* arg)
|
|
|
|
- \param[in]
|
|
|
|
- \brief handle gps init ,deinit and convert process
|
|
|
|
- \return
|
|
|
|
-*/
|
|
|
|
-static void GpsProcess(void* arg)
|
|
|
|
-{
|
|
|
|
- gpsReqMsg msg;
|
|
|
|
- GPS_INFO Gps_buffer;
|
|
|
|
- Gps_buffer.latitude = 0xffffffff;
|
|
|
|
- Gps_buffer.longitude = 0xffffffff;
|
|
|
|
- char *p=NULL;
|
|
|
|
- const char *delim = "\n";
|
|
|
|
- char *databuffer[14];
|
|
|
|
- /**
|
|
|
|
- *databuffer内容格式:
|
|
|
|
- * 定位标识,时间,位置有效标识,纬度,纬度指示,经度,经度指示,地面速率,地面航向,日期
|
|
|
|
- **/
|
|
|
|
-
|
|
|
|
- #ifdef USING_PRINTF
|
|
|
|
- printf("%s[%d]\r\n",__FUNCTION__, __LINE__);
|
|
|
|
- #endif
|
|
|
|
- while(1)
|
|
|
|
- {
|
|
|
|
- //char temp[] = "$GNGGA,082626.00,2939.91801,N,10637.09500,E,1,03,2.34,-20.3,M,,M,,*40";
|
|
|
|
- char temp[] = "$GNRMC,082626.000,A,2939.91801,N,10637.09500,E,0.543,30.254,261120,,,A,V*17";
|
|
|
|
- osMessageQueueGet(norGpsHandle, &msg, 0, osWaitForever);
|
|
|
|
- if (msg.dataPtr)
|
|
|
|
- {
|
|
|
|
- void *msgtemp = msg.dataPtr;//取指针
|
|
|
|
- // #ifdef USING_PRINTF
|
|
|
|
- // printf("\nGpsProcess msgptr data:%s\r\n",msg.dataPtr);
|
|
|
|
- // #endif
|
|
|
|
- p = strtok(msg.dataPtr,delim);//将信息进行分割
|
|
|
|
- // #ifdef USING_PRINTF
|
|
|
|
- // printf("\nP msgptr data:%s\r\n",p);
|
|
|
|
- // #endif
|
|
|
|
- int i=0;
|
|
|
|
- p = strtok(p,",");//只取第1行的信息RMC
|
|
|
|
- //p = strtok(temp,",");//模拟测试
|
|
|
|
- if (strcmp(p,"$GNRMC")==0)
|
|
|
|
- {
|
|
|
|
- while (p)
|
|
|
|
- {
|
|
|
|
- // #ifdef USING_PRINTF
|
|
|
|
- // printf("p:%s\r\n",p);
|
|
|
|
- // #endif
|
|
|
|
- databuffer[i]=p;
|
|
|
|
- p = strtok(NULL,",");
|
|
|
|
- i++;;
|
|
|
|
- }
|
|
|
|
- if (strcmp(databuffer[1],"V")==0|strcmp(databuffer[2],"V")==0)
|
|
|
|
- {
|
|
|
|
-
|
|
|
|
- #ifdef USING_PRINTF
|
|
|
|
- printf("Gpsdata invalide\n");
|
|
|
|
- #endif
|
|
|
|
- Gps_buffer.status = 0x00;
|
|
|
|
- Gps_buffer.speed = 0xffff;
|
|
|
|
- Gps_buffer.direction= 0xffff;
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- else if (strcmp(databuffer[2],"A")==0)
|
|
|
|
- {
|
|
|
|
- uint32_t speedtemp;
|
|
|
|
- #ifdef USING_PRINTF
|
|
|
|
- printf("Gpsdata valide\n");
|
|
|
|
- #endif
|
|
|
|
- Gps_buffer.status = 0x01;//有效,东经,北纬写定
|
|
|
|
- Gps_buffer.satellite_num = 03;//卫星数目写入1
|
|
|
|
- strdel(databuffer[3],'.');
|
|
|
|
- strdel(databuffer[5],'.');
|
|
|
|
- strdel(databuffer[7],'.');
|
|
|
|
- speedtemp = atol(databuffer[7])*1852;//节换算单位,1节=1.852km每小时
|
|
|
|
- Gps_buffer.speed = speedtemp/1e5;
|
|
|
|
- Gps_buffer.latitude = location_handle(databuffer[3]);
|
|
|
|
- Gps_buffer.longitude = location_handle(databuffer[5]);
|
|
|
|
- Gps_buffer.altitude = 00;
|
|
|
|
- if(Gps_buffer.speed>=100)//大于10km/h才输出方位
|
|
|
|
- {
|
|
|
|
- Gps_buffer.direction = atol(databuffer[8]);
|
|
|
|
- }
|
|
|
|
- Gps_buffer.direction = 0xfffe;
|
|
|
|
- }
|
|
|
|
- osMessageQueuePut(gpsMsgQueue, &Gps_buffer, 0, 1000);
|
|
|
|
- }
|
|
|
|
- free(msgtemp);
|
|
|
|
- msgtemp=NULL;
|
|
|
|
- }
|
|
|
|
- msg.dataPtr=NULL;
|
|
|
|
- if (Sleep_Flag)
|
|
|
|
- {
|
|
|
|
- osThreadExit();
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-//主任务线程初始化
|
|
|
|
-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 = 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);
|
|
|
|
-
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-/**
|
|
|
|
- \fn INT32 GpsTaskInit(void)
|
|
|
|
- \param[in]
|
|
|
|
- \brief create task for checking gps data
|
|
|
|
- \return
|
|
|
|
-*/
|
|
|
|
-INT32 GpsTaskInit(void)
|
|
|
|
-{
|
|
|
|
-
|
|
|
|
- if(norGpsHandle == NULL)
|
|
|
|
- {
|
|
|
|
- norGpsHandle = osMessageQueueNew(1,sizeof(gpsReqMsg), NULL);
|
|
|
|
- if(norGpsHandle == NULL)
|
|
|
|
- return 1;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- if(gpsTaskHandle == NULL)
|
|
|
|
- {
|
|
|
|
- osThreadAttr_t task_attr;
|
|
|
|
- memset(&task_attr , 0 , sizeof(task_attr));
|
|
|
|
- task_attr.name = "GPS";
|
|
|
|
- task_attr.priority = osPriorityNormal1;
|
|
|
|
- task_attr.cb_mem = &gpsTask;
|
|
|
|
- task_attr.cb_size = sizeof(StaticTask_t);
|
|
|
|
- task_attr.stack_mem = gpsTaskStack;
|
|
|
|
- task_attr.stack_size =GPS_TASK_STACK_SIZE;
|
|
|
|
- memset(& gpsTaskStack, 0xa5, GPS_TASK_STACK_SIZE);
|
|
|
|
- gpsTaskHandle = osThreadNew(GpsProcess , NULL,&task_attr);
|
|
|
|
- if(gpsTaskHandle == NULL)
|
|
|
|
- return 1;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return 0;
|
|
|
|
-}
|
|
|
|
-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);
|
|
|
|
-}
|
|
|