Browse Source

UART 读取写入测试成功

CHENJIE-PC\QiXiang_CHENJIE 4 years ago
parent
commit
ae13cddd49
5 changed files with 209 additions and 11 deletions
  1. 1 1
      inc/MainTask.h
  2. 1 1
      inc/UartTask.h
  3. 5 1
      inc/app.h
  4. 1 1
      src/MainTask.c
  5. 201 7
      src/UartTask.c

+ 1 - 1
inc/MainTask.h

@@ -7,7 +7,7 @@
  *
  ****************************************************************************/
 //全局变量
-extern uint32_t Timer_count;//每100ms加1
+extern UINT32 Timer_count;//每100ms加1
 extern volatile bool Sleep_flag;//睡眠标志位
 /*---------------------------------------------------------------------------*/
 #define PROC_MAIN_TASK_STACK_SIZE           (512)

+ 1 - 1
inc/UartTask.h

@@ -8,9 +8,9 @@
  ****************************************************************************/
 
 #define PROC_UART_TASK_STACK_SIZE           (1024)
+#define BMS_ADDRESS_CODE 0x01
 #define UART_READ_CODE 0x03
 #define UART_WRITE_CODE 0x10
-#define BMS_ADDRESS_CODE 0x01
 typedef struct Uart_Read_Msg_Type
 {
     uint8_t Bms_Address; 

+ 5 - 1
inc/app.h

@@ -97,7 +97,11 @@ typedef enum
     PROCESS_TCP_STATE_SLEEP
 }process_TCP;
 
-
+typedef struct _UartRedMsg
+{
+    void * dataPtr;
+    UINT16 len;
+}UartReadMsg;
 
 
 

+ 1 - 1
src/MainTask.c

@@ -32,7 +32,7 @@
 
 
 //全局变量
-uint32_t Timer_count;//每100ms加1
+UINT32 Timer_count;//每100ms加1
 volatile bool Sleep_flag = false;//睡眠标志位
 
 

+ 201 - 7
src/UartTask.c

@@ -28,7 +28,10 @@
 #include "UartTask.h"
 #include "MainTask.h"
 #include "app.h"
-
+//全局变量测试区
+UINT8 UDSService=1;
+QueueHandle_t UartReadQueueHandle = NULL;
+UartReadMsg Uart_Recv_Msg_Test;
 //
 extern ARM_DRIVER_USART Driver_USART1;
 static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
@@ -42,19 +45,95 @@ static osThreadId_t           UartTaskId = NULL;
 static process_Uart             gProcess_Uart_Task = PROCESS_UART_STATE_IDLE;
 #define PROC_UART_STATE_SWITCH(a)  (gProcess_Uart_Task = a)
 
-
+//函数声明区
+void USART_callback(uint32_t event);
+UINT8 Uart_Transmit_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer);
+UINT16 crc_chk(UINT8* data, UINT8 length);
 //Uart线程任务区
 static void UartTask(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);
+    PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
+    UINT16  Reg_Num = 0;
+    UINT16  Uart_Uds_LEN;
+    UINT16  Uart_Recv_LEN;
+    Uart_Read_Msg_Type Uart_Read_Msg;
+    UartReadMsg UartQueueMsg;
     while (1)
     {
-        osDelay(100);
-        #ifdef USING_PRINTF
-        if (Timer_count%50==0)
+        switch (gProcess_Uart_Task)
         {
-            printf("Uart Task,Batt_Cell_Num:%d,%d!\n",BATT_CELL_VOL_NUM,Timer_count);
+            case PROCESS_UART_STATE_IDLE:
+            {
+                osDelay(100);
+                if (Timer_count%50==0)
+                {
+                    #ifdef USING_PRINTF
+                        printf("[%d]Uart Timer 5s:%d\n",__LINE__,Timer_count);
+                    #endif
+                    if(UDSService==1)
+                        PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_CHECK);
+                    else
+                        PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WORK);
+                }
+                break;
+            }
+            case PROCESS_UART_STATE_CHECK:
+            {
+                if(UartReadQueueHandle==NULL)
+                {
+                    UartReadQueueHandle = osMessageQueueNew(1,sizeof(UartReadMsg), NULL);
+                    if(UartReadQueueHandle==NULL)
+                    {
+                        PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
+                        break;
+                    }
+                }
+                Reg_Num = 0x21+BATT_CELL_VOL_NUM+BATT_TEMP_NUM;//按照协议里面的0x21+X+N的结束地址
+                Uart_Read_Msg.Bms_Address = BMS_ADDRESS_CODE;
+                Uart_Read_Msg.Bms_Funcode = UART_READ_CODE;
+                Uart_Read_Msg.Reg_Begin_H = 0x00;
+                Uart_Read_Msg.Reg_Begin_L= 0x00;
+                Uart_Read_Msg.Reg_Num_H = Reg_Num>>8;
+                Uart_Read_Msg.Reg_Num_L = Reg_Num;
+                Uart_Uds_LEN = Reg_Num*2;
+                UartQueueMsg.dataPtr = malloc(Uart_Uds_LEN+1);
+                memset(UartQueueMsg.dataPtr,0x00,Uart_Uds_LEN);
+                Uart_Recv_LEN = Uart_Transmit_func((UINT8 *)&Uart_Read_Msg,UartQueueMsg.dataPtr);
+                UartQueueMsg.len = Uart_Recv_LEN;
+                osMessageQueuePut(UartReadQueueHandle,&UartQueueMsg,0,1000);
+                osDelay(1000);
+                osMessageQueueGet(UartReadQueueHandle,&Uart_Recv_Msg_Test,0,1000);
+                 #ifdef USING_PRINTF
+                    printf("Uart_Recv_Queue-%d: ",Uart_Recv_Msg_Test.len);
+                    for(int i=0;i<Uart_Recv_Msg_Test.len;i++)
+                    {
+                        printf("%x ",*((UINT8 *)Uart_Recv_Msg_Test.dataPtr+i));
+                    }
+                    printf("\n");
+                #endif
+                if(Uart_Recv_Msg_Test.dataPtr)
+                    free(Uart_Recv_Msg_Test.dataPtr);
+                Uart_Recv_Msg_Test.dataPtr = PNULL;
+                PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
+                break;
+            }
+            case PROCESS_UART_STATE_WORK:
+            {
+                break;
+            }
+            case PROCESS_UART_STATE_SLEEP:
+            {
+                break;
+            }
+
         }
-        #endif
     }
 }
 //Uart线程初始化
@@ -75,4 +154,119 @@ void UartTaskDeInit(void *arg)
 {
     osThreadTerminate(UartTaskId);
     UartTaskId = NULL;
+}
+//函数区
+//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校验程序
+UINT16 crc_chk(UINT8* data, UINT8 length)
+{  
+    UINT8 j;
+    UINT16 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发送接收函数
+UINT8 Uart_Transmit_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer)
+{
+    UINT16 CRC_Rece_buffer;
+    UINT16 CRC_chk_buffer;
+    UINT16 Data_Len ;
+    UINT8 timeout = 0x00;
+    Data_Len = (*(Uart_Read_Msg+4)|*(Uart_Read_Msg+5))*2+5;
+    CRC_chk_buffer = crc_chk(Uart_Read_Msg,6);
+    *(Uart_Read_Msg+6) = CRC_chk_buffer;
+    *(Uart_Read_Msg+7)  = CRC_chk_buffer>>8;
+    USARTdrv->Send(Uart_Read_Msg,8);
+    #ifdef USING_PRINTF
+        printf("Uart_Send_buffer:  ");
+        for(int i=0;i<8;i++)
+        {
+            printf("%x ",*(Uart_Read_Msg+i));
+        }
+        printf("\n");
+    #endif
+    USARTdrv->Receive(Uart_Recv_Buffer,Data_Len);
+    while((isRecvTimeout == false) && (isRecvComplete == false))
+    {
+        timeout++;
+        osDelay(100);
+        if (timeout>=50)
+        {
+            timeout =0;
+            isRecvTimeout = true;
+            break;
+        }
+    }
+    // #ifdef USING_PRINTF
+    //     printf("Uart_Rece_buffer1: ");
+    //     for(int i=0;i<Data_Len;i++)
+    //     {
+    //     printf("%x ",*(Uart_Recv_Buffer+i));
+    //     }
+    // #endif
+    if (isRecvComplete == true)
+    {
+        isRecvComplete = false;
+        CRC_Rece_buffer =*(Uart_Recv_Buffer+Data_Len-1)<<8|*(Uart_Recv_Buffer+Data_Len-2);
+        CRC_chk_buffer = crc_chk(Uart_Recv_Buffer,Data_Len-2);
+        #ifdef USING_PRINTF
+            printf("Uart_Rece_buffer after Crc: ");
+            for(int i=0;i<Data_Len;i++)
+            {
+            printf("%x ",*(Uart_Recv_Buffer+i));
+            }
+            printf("\ncrcchk:%x,%x\n ",CRC_chk_buffer,CRC_Rece_buffer);
+        #endif
+        if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
+        {
+            return Data_Len;//此处指针移位出现重启问题
+        }
+        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_Recv_Buffer,0xff,Data_Len);
+            return 0;
+        }
+    }
+    else
+    {
+        memset(Uart_Recv_Buffer,0x00,Data_Len);
+        isRecvTimeout = false;
+        return 0;
+    }
+
+
 }