|
@@ -26,12 +26,13 @@
|
|
|
#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)
|
|
|
|
|
|
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)
|
|
@@ -91,6 +92,15 @@ typedef enum
|
|
|
static process_CAN gProcess_Can_Task = PROCESS_CAN_STATE_IDLE;
|
|
|
#define PROC_CAN_STATE_SWITCH(a) (gProcess_Can_Task = a)
|
|
|
|
|
|
+typedef enum
|
|
|
+{
|
|
|
+ PROCESS_TCP_STATE_IDLE = 0,
|
|
|
+ PROCESS_TCP_STATE_LINK,
|
|
|
+ PROCESS_TCP_STATE_WORK,
|
|
|
+ PROCESS_TCP_STATE_SLEEP
|
|
|
+}process_TCP;
|
|
|
+static process_TCP gProcess_Tcp_Task = PROCESS_TCP_STATE_IDLE;
|
|
|
+#define PROC_TCP_STATE_SWITCH(a) (gProcess_Tcp_Task = a)
|
|
|
//堆栈申请
|
|
|
static StaticTask_t gProcess_Main_Task_t;
|
|
|
static UINT8 gProcess_Main_TaskStack[PROC_UART_TASK_STACK_SIZE];
|
|
@@ -98,7 +108,8 @@ static StaticTask_t gProcess_Uart_Task_t;
|
|
|
static UINT8 gProcess_Uart_TaskStack[PROC_UART_TASK_STACK_SIZE];
|
|
|
static StaticTask_t gProcess_Can_Task_t;
|
|
|
static UINT8 gProcess_Can_TaskStack[PROC_CAN_TASK_STACK_SIZE];
|
|
|
-
|
|
|
+static StaticTask_t gProcess_Tcp_Task_t;
|
|
|
+static UINT8 gProcess_Tcp_TaskStack[PROC_TCP_TASK_STACK_SIZE];
|
|
|
//睡眠进出函数
|
|
|
static void appBeforeHib(void *pdata, slpManLpState state)
|
|
|
{
|
|
@@ -154,7 +165,7 @@ static void Main_Task(void* arg)
|
|
|
{
|
|
|
UINT16 Can_index = 0;
|
|
|
UINT16 Uart_index = 0;
|
|
|
- UINT16 NB_index = 0;
|
|
|
+ UINT16 Tcp_index = 0;
|
|
|
uint32_t sleep_index = 0;
|
|
|
int32_t inParam = 0xAABBCCDD;
|
|
|
PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE);
|
|
@@ -206,7 +217,7 @@ static void Main_Task(void* arg)
|
|
|
osDelay(10/portTICK_PERIOD_MS);
|
|
|
Can_index++;
|
|
|
Uart_index++;
|
|
|
- NB_index++;
|
|
|
+ Tcp_index++;
|
|
|
if (Uart_index >100)//Uart 1s 调用一次
|
|
|
{
|
|
|
PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WORK);
|
|
@@ -217,11 +228,12 @@ static void Main_Task(void* arg)
|
|
|
PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_RECV);
|
|
|
Can_index = 0;
|
|
|
}
|
|
|
- if (NB_index >=100)//NB 1s 调用一次
|
|
|
+ if (Tcp_index >=100)//Tcp 1s 调用一次
|
|
|
{
|
|
|
- NB_index = 0;
|
|
|
+ PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_LINK);
|
|
|
+ Tcp_index = 0;
|
|
|
}
|
|
|
- if(!Can_Enable)
|
|
|
+ if((!Can_Enable)&&(Uart_Rece_BattI==0x0000))
|
|
|
{
|
|
|
sleep_index++;
|
|
|
}
|
|
@@ -238,10 +250,11 @@ static void Main_Task(void* arg)
|
|
|
}
|
|
|
case PROCESS_STATE_SLEEP:
|
|
|
{
|
|
|
- while((gProcess_Uart_Task!=PROCESS_UART_STATE_SLEEP)||(gProcess_Can_Task!=PROCESS_CAN_STATE_SLEEP))
|
|
|
+ while((gProcess_Uart_Task!=PROCESS_UART_STATE_SLEEP)||(gProcess_Can_Task!=PROCESS_CAN_STATE_SLEEP)||(gProcess_Tcp_Task!=PROCESS_TCP_STATE_SLEEP))
|
|
|
{
|
|
|
PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_SLEEP);
|
|
|
PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SLEEP);
|
|
|
+ PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_SLEEP);
|
|
|
}
|
|
|
|
|
|
slpManSlpState_t State;
|
|
@@ -330,14 +343,14 @@ uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg,uint8_t* Uart_Rece
|
|
|
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
|
|
|
+ // #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))
|
|
|
{
|
|
@@ -356,6 +369,12 @@ uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg,uint8_t* Uart_Rece
|
|
|
isRecvComplete = false;
|
|
|
CRC_Rece_buffer =*(Uart_Rece_buffer+Data_Len-1)<<8|*(Uart_Rece_buffer+Data_Len-2);
|
|
|
CRC_chk_buffer = crc_chk(Uart_Rece_buffer,Data_Len-2);
|
|
|
+ if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
|
|
|
+ {
|
|
|
+ return Uart_Rece_buffer;//此处指针移位出现重启问题
|
|
|
+ }
|
|
|
+ else //接收数据的校验不过屏蔽
|
|
|
+ {
|
|
|
#ifdef USING_PRINTF
|
|
|
printf("Uart_Rece_buffer: ");
|
|
|
for(int i=0;i<Data_Len;i++)
|
|
@@ -364,12 +383,6 @@ uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg,uint8_t* Uart_Rece
|
|
|
}
|
|
|
printf("crcchk:%x,%x\n ",CRC_chk_buffer,CRC_Rece_buffer);
|
|
|
#endif
|
|
|
- if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
|
|
|
- {
|
|
|
- return Uart_Rece_buffer;//此处指针移位出现重启问题
|
|
|
- }
|
|
|
- else //接收数据的校验不过屏蔽
|
|
|
- {
|
|
|
memset(Uart_Rece_buffer,0xff,Data_Len);
|
|
|
return Uart_Rece_buffer;
|
|
|
}
|
|
@@ -561,6 +574,7 @@ static void Uart_Task(void* arg)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+//Can-线程任务
|
|
|
static void Can_Task(void* arg)
|
|
|
{
|
|
|
uint32_t Can_ID;
|
|
@@ -781,6 +795,196 @@ static void Can_Task(void* arg)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+void TcpCallBack(void)//连接建立回调函数
|
|
|
+{
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("[%d]TcpCallBack\n",__LINE__);
|
|
|
+ #endif
|
|
|
+}
|
|
|
+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;
|
|
|
+ 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 = 0x2710 - Batt_current;
|
|
|
+ Batt_current = Batt_current;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Batt_current = Batt_current+0x2710;
|
|
|
+ Batt_current = Batt_current;
|
|
|
+ }
|
|
|
+ battbuffer[17] = Batt_current>>8;
|
|
|
+ battbuffer[18] = Batt_current;
|
|
|
+ data_index = 32+Batt_Cell_Num_2+Batt_Temp_Num;
|
|
|
+ battbuffer[data_index] = 0x00;//电池状态
|
|
|
+ data_index++;
|
|
|
+ battbuffer[data_index] = 0x00;//是否加热
|
|
|
+ data_index++;
|
|
|
+}
|
|
|
+//Tcp线程
|
|
|
+static void Tcp_Task(void* arg)
|
|
|
+{
|
|
|
+ CHAR SN[] = "GYTEST00000000003";//SN应写到osfile里面
|
|
|
+ CHAR serverip[] = "47.97.127.222";
|
|
|
+ UINT16 serverport = 8712;
|
|
|
+ int TcpConnectID = 1;
|
|
|
+ int TcpSendID = -1;
|
|
|
+ int NB_send_len=59+Batt_Cell_Num_2+Batt_Temp_Num;//设定tcp发送的最大数值,以电池数据为上线
|
|
|
+ slpManApplyPlatVoteHandle("TcpSlp",&TcpSlpHandler);
|
|
|
+ slpManPlatVoteDisableSleep(TcpSlpHandler, SLP_SLP2_STATE);
|
|
|
+ PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
|
|
|
+ uint8_t Tcp_Index = 0;
|
|
|
+ uint8_t* TcpSendBuffer=NULL;
|
|
|
+ volatile bool Tcp_Flag=false;
|
|
|
+ while(true)
|
|
|
+ {
|
|
|
+ switch(gProcess_Tcp_Task)
|
|
|
+ {
|
|
|
+ case PROCESS_TCP_STATE_IDLE:
|
|
|
+ {
|
|
|
+ osDelay(100);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case PROCESS_TCP_STATE_LINK:
|
|
|
+ {
|
|
|
+ while(TcpConnectID<0)//建立连接
|
|
|
+ {
|
|
|
+ TcpConnectID = tcpipConnectionCreate(1,PNULL,PNULL,serverip,serverport,TcpCallBack);
|
|
|
+ osDelay(100);
|
|
|
+ }
|
|
|
+ PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_WORK);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case PROCESS_TCP_STATE_WORK:
|
|
|
+ {
|
|
|
+ Tcp_Flag = false;
|
|
|
+ TcpSendBuffer = (uint8_t *)malloc(NB_send_len);
|
|
|
+ while (!Tcp_Flag)
|
|
|
+ {
|
|
|
+ switch(Tcp_Index)
|
|
|
+ {
|
|
|
+ case 0://发送登录信息
|
|
|
+ {
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("[%d]case 0\n",__LINE__);
|
|
|
+ #endif
|
|
|
+ Tcp_Index=1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 1://发送电池数据
|
|
|
+ {
|
|
|
+ memset(TcpSendBuffer,0x00,92);
|
|
|
+ *(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 = tcpipConnectionSend(TcpConnectID,TcpSendBuffer,NB_send_len,PNULL,PNULL,PNULL);
|
|
|
+ //发送失败暂时不写
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("ConnectID:%d,TcpSend:%d,data length:%d,Data: ",TcpConnectID,TcpSendID,NB_send_len);
|
|
|
+ for (int i = 0; i < NB_send_len; i++)
|
|
|
+ {
|
|
|
+ printf("%x ",*(TcpSendBuffer+i));
|
|
|
+ }
|
|
|
+ printf("\n ");
|
|
|
+ #endif
|
|
|
+ Tcp_Index=2;
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("[%d]case 1\n",__LINE__);
|
|
|
+ #endif
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 2:
|
|
|
+ {
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("[%d]case 2\n",__LINE__);
|
|
|
+ #endif
|
|
|
+ Tcp_Index=3;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ {
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("[%d]case default\n",__LINE__);
|
|
|
+ #endif
|
|
|
+ PROC_TCP_STATE_SWITCH(PROCESS_TCP_STATE_IDLE);
|
|
|
+ Tcp_Index = 0;
|
|
|
+ Tcp_Flag = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ free(TcpSendBuffer);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case PROCESS_TCP_STATE_SLEEP:
|
|
|
+ {
|
|
|
+ slpManPlatVoteEnableSleep(TcpSlpHandler, SLP_SLP2_STATE);
|
|
|
+ tcpipConnectionClose(TcpConnectID);
|
|
|
+ TcpConnectID = -1;
|
|
|
+ while(true)
|
|
|
+ {
|
|
|
+ osDelay(5000/portTICK_PERIOD_MS);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
//主任务线程初始化
|
|
|
void Main_Task_Init()
|
|
|
{
|
|
@@ -825,7 +1029,7 @@ 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 = "Uart_Task";
|
|
|
+ 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;
|
|
@@ -834,12 +1038,29 @@ void Can_Task_Init()
|
|
|
|
|
|
osThreadNew(Can_Task, NULL, &task_attr);
|
|
|
|
|
|
+}
|
|
|
+//Tcp线程初始化
|
|
|
+void Tcp_Task_Init()
|
|
|
+{
|
|
|
+ osThreadAttr_t task_attr;
|
|
|
+ memset(&task_attr,0,sizeof(task_attr));
|
|
|
+ memset(gProcess_Tcp_TaskStack, 0xA5, PROC_TCP_TASK_STACK_SIZE);
|
|
|
+ task_attr.name = "Tcp_Task";
|
|
|
+ task_attr.stack_mem = gProcess_Tcp_TaskStack;
|
|
|
+ task_attr.stack_size = PROC_TCP_TASK_STACK_SIZE;
|
|
|
+ task_attr.priority = osPriorityNormal;
|
|
|
+ task_attr.cb_mem = &gProcess_Tcp_Task_t;
|
|
|
+ task_attr.cb_size = sizeof(StaticTask_t);
|
|
|
+
|
|
|
+ osThreadNew(Tcp_Task, NULL, &task_attr);
|
|
|
+
|
|
|
}
|
|
|
void appInit(void *arg)
|
|
|
{
|
|
|
Main_Task_Init();
|
|
|
Uart_Task_Init();
|
|
|
Can_Task_Init();
|
|
|
+ Tcp_Task_Init();
|
|
|
}
|
|
|
//主函数入口
|
|
|
void main_entry(void) {
|