Ver Fonte

【修正】睡眠测试完成,CAN信息发送完成,CAN使能完成,UART信息完成。

CHENJIE-PC\QiXiang_CHENJIE há 4 anos atrás
pai
commit
c8aef39fcc
1 ficheiros alterados com 260 adições e 13 exclusões
  1. 260 13
      src/app.c

+ 260 - 13
src/app.c

@@ -25,14 +25,18 @@
 //空间及变量定义
 #define PROC_MAIN_TASK_STACK_SIZE           (1024)
 #define PROC_UART_TASK_STACK_SIZE           (1024)
+#define PROC_CAN_TASK_STACK_SIZE           (1024)
 
 uint8_t deepslpTimerID          = DEEPSLP_TIMER_ID7;
 uint8_t MainSlpHandler          = 0xff;
 uint8_t UartSlpHandler          = 0xfe;
+uint8_t CanSlpHandler          = 0xfd;
+
 
 #define Uart_Send_LEN         (8)
 #define Uart_Rece_LEN         (40)
 
+#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;
@@ -42,6 +46,8 @@ uint8_t Batt_Cell_Num = 14;//默认数值14、17
 uint8_t Batt_Cell_Num_2 ;//默认数值
 uint8_t Batt_Temp_Num = 5;//默认数值5、7
 int16_t Uart_Rece_BattI=0x0000;
+uint8_t Can_Rece_buffer[8];
+volatile bool Can_Enable = false;
 uint16_t data_index = 0x0000;
 uint8_t battbuffer[100];//电池数据都存在此数组中————电压14,温度5
 /**
@@ -75,13 +81,23 @@ typedef enum
 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)
 
 //堆栈申请
 static StaticTask_t             gProcess_Main_Task_t;
 static UINT8                  gProcess_Main_TaskStack[PROC_UART_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 void appBeforeHib(void *pdata, slpManLpState state)
@@ -193,27 +209,19 @@ static void Main_Task(void* arg)
                 NB_index++;
                 if (Uart_index >100)//Uart 1s 调用一次
                 {
-                    #ifdef USING_PRINTF
-                        printf("Uart_start \n");
-                    #endif
                     PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WORK);
                     Uart_index = 0;
                 }
                 if (Can_index >=100)//Can 1s 调用一次
                 {
-                    #ifdef USING_PRINTF
-                        printf("Can_start \n");
-                    #endif
+                    PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_RECV);
                     Can_index = 0;
                 }
                 if (NB_index >=100)//NB 1s 调用一次
                 {
-                    #ifdef USING_PRINTF
-                        printf("NB_start \n");
-                    #endif
                     NB_index = 0;
                 }
-                if(true)
+                if(!Can_Enable)
                 {
                     sleep_index++;
                 }
@@ -230,9 +238,10 @@ static void Main_Task(void* arg)
             }
             case PROCESS_STATE_SLEEP:
             {
-                while(gProcess_Uart_Task!=PROCESS_UART_STATE_SLEEP)
+                while((gProcess_Uart_Task!=PROCESS_UART_STATE_SLEEP)||(gProcess_Can_Task!=PROCESS_CAN_STATE_SLEEP))
                 {
                     PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_SLEEP);
+                    PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_SLEEP);
                 }
 
                 slpManSlpState_t State;
@@ -395,6 +404,7 @@ static void Uart_Task(void* arg)
     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)
     {
@@ -427,7 +437,7 @@ static void Uart_Task(void* arg)
             case PROCESS_UART_STATE_WORK:
             {
                 NetSocDisplay(LED_SOC_1,LED_TURN_ON);
-                bool Uart_task = false;
+                Uart_task = false;
                 Uart_Rece_buffer = (uint8_t *)malloc(Uart_Rece_LEN);
                 while(!Uart_task)
                 {
@@ -551,6 +561,226 @@ static void Uart_Task(void* arg)
     }
 
 }
+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);
+    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:
+            {
+                //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
+                {
+                    Can_Enable = false;
+                }
+                break;
+            }
+            case PROCESS_CAN_STATE_SEND:
+            {
+                Can_Flag=false;
+                while(!Can_Flag)
+                {
+                    #ifdef USING_PRINTF
+                        printf("Can_Send!\n");
+                    #endif
+                    switch(send_index)
+                    {
+                        case 0:
+                        {
+                            Can_ID = 0x001;
+                            for (int i = 0; i < 8; i++)
+                            {
+                                Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
+                            }
+                            Can_TxMsg.stdIDH = Can_ID>>3;
+                            Can_TxMsg.stdIDL = Can_ID<<5;
+                            Can_TxMsg.DLC  = 8;
+                            HAL_Can_Transmit(Can_TxMsg);
+                            break;
+                        }
+                        case 1:
+                        {
+                            Can_ID = 0x011;
+                            for (int i = 0; i < 8; i++)
+                            {
+                                Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
+                            }
+                            Can_TxMsg.stdIDH = Can_ID>>3;
+                            Can_TxMsg.stdIDL = Can_ID<<5;
+                            Can_TxMsg.DLC  = 8;
+                            HAL_Can_Transmit(Can_TxMsg);
+                            break;
+                        }
+                        case 2:
+                        {
+                            Can_ID = 0x020;
+                            for (int i = 0; i < 8; i++)
+                            {
+                                Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
+                            }
+                            Can_TxMsg.stdIDH = Can_ID>>3;
+                            Can_TxMsg.stdIDL = Can_ID<<5;
+                            Can_TxMsg.DLC  = 8;
+                            HAL_Can_Transmit(Can_TxMsg);
+                            break;
+                        }
+                        case 3:
+                        {
+                            Can_ID = 0x031;
+                            for (int i = 0; i < 4; i++)
+                            {
+                                Can_TxMsg.Data[i] = battbuffer[i+31+send_index*8];
+                            }
+                            Can_TxMsg.Data[4] = 0x00;
+                            Can_TxMsg.Data[5] = 0x00;
+                            Can_TxMsg.Data[6] = 0x00;
+                            Can_TxMsg.Data[7] = 0x00;
+                            Can_TxMsg.stdIDH = Can_ID>>3;
+                            Can_TxMsg.stdIDL = Can_ID<<5;
+                            Can_TxMsg.DLC  = 8;
+                            HAL_Can_Transmit(Can_TxMsg);
+                            break;
+                        }
+                        case 4:
+                        {
+                            Can_ID  = 0x101;
+                            for (int i = 0; i < Batt_Temp_Num; i++)
+                            { 
+                                Can_TxMsg.Data[i] = battbuffer[i+32+Batt_Cell_Num_2];
+                            }
+                            Can_TxMsg.stdIDH = Can_ID>>3;
+                            Can_TxMsg.stdIDL = Can_ID<<5;
+                            Can_TxMsg.DLC  = 8;
+                            HAL_Can_Transmit(Can_TxMsg);
+                            break;
+                        }
+                        case 5:
+                        {
+                            Batt_Cell_addU = 0x0000;
+                            for (int i = 0; i < Batt_Cell_Num; i++)
+                            {
+                                Batt_Cell_addU = Batt_Cell_addU + (battbuffer[31+i*2]<<8|battbuffer[31+i*2+1]);
+                            }
+                            Can_ID = 0x201;
+                            Can_TxMsg.Data[0] = battbuffer[19];
+                            Can_TxMsg.Data[1] = battbuffer[20];
+                            Can_TxMsg.Data[2] = battbuffer[21];
+                            Can_TxMsg.Data[3] = battbuffer[22];//外电压
+                            Can_TxMsg.Data[4] = Batt_Cell_addU>>8;
+                            Can_TxMsg.Data[5] = Batt_Cell_addU;//累加电压
+                            Can_TxMsg.Data[6] = Uart_Rece_BattI>>8;
+                            Can_TxMsg.Data[7] = Uart_Rece_BattI;
+                            Can_TxMsg.stdIDH = Can_ID>>3;
+                            Can_TxMsg.stdIDL = Can_ID<<5;
+                            Can_TxMsg.DLC  = 8;
+                            HAL_Can_Transmit(Can_TxMsg);
+                            break;
+
+                        }
+                        case 6:
+                        {
+                            Can_ID = 0x211;
+                            Can_TxMsg.Data[0] = battbuffer[34+Batt_Cell_Num_2+Batt_Temp_Num];
+                            Can_TxMsg.Data[1] = battbuffer[35+Batt_Cell_Num_2+Batt_Temp_Num];
+                            Can_TxMsg.Data[2] = battbuffer[36+Batt_Cell_Num_2+Batt_Temp_Num];
+                            Can_TxMsg.Data[3] = battbuffer[37+Batt_Cell_Num_2+Batt_Temp_Num];
+                            Can_TxMsg.Data[4] = 0x00;
+                            Can_TxMsg.Data[5] = 0x00;
+                            Can_TxMsg.Data[6] = battbuffer[24];
+                            Can_TxMsg.Data[7] = Batt_Temp_Num<<4|0;
+                            Can_TxMsg.stdIDH = Can_ID>>3;
+                            Can_TxMsg.stdIDL = Can_ID<<5;
+                            Can_TxMsg.DLC  = 8;
+                            HAL_Can_Transmit(Can_TxMsg);
+                            break;
+                        }
+                        case 7:
+                        {
+                            Can_ID = 0x221;
+                            Can_TxMsg.Data[0] = battbuffer[25];//SOH
+                            Can_TxMsg.Data[1] = 0x00;
+                            Can_TxMsg.Data[2] = 0x00;
+                            Can_TxMsg.Data[3] = 0x00;
+                            Can_TxMsg.Data[4] = 0x00;
+                            Can_TxMsg.Data[5] = 0x00;
+                            Can_TxMsg.Data[6] = 0x00;
+                            Can_TxMsg.Data[7] = 0x00;
+                            Can_TxMsg.stdIDH = Can_ID>>3;
+                            Can_TxMsg.stdIDL = Can_ID<<5;
+                            Can_TxMsg.DLC  = 8;
+                            HAL_Can_Transmit(Can_TxMsg);
+                            break;
+                        }
+                        default:
+                        {
+                            Can_Flag=true;
+                            PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_IDLE);
+                            break;
+                        }
+                    }
+                    send_index ++;
+                }
+                break;
+            }
+            case PROCESS_CAN_STATE_SLEEP:
+            {
+                slpManPlatVoteEnableSleep(CanSlpHandler, SLP_SLP2_STATE); 
+                while(true)
+                {
+                    osDelay(5000/portTICK_PERIOD_MS);
+                }
+                break;
+            }
+        }
+    }
+}
 //主任务线程初始化
 void Main_Task_Init()
 {
@@ -588,11 +818,28 @@ void Uart_Task_Init()
 
     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 = "Uart_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);
+
 }
 void appInit(void *arg)
 {
     Main_Task_Init();
     Uart_Task_Init();
+    Can_Task_Init();
 }
 //主函数入口
 void main_entry(void) {