|
@@ -28,14 +28,18 @@
|
|
#include "UartTask.h"
|
|
#include "UartTask.h"
|
|
#include "MainTask.h"
|
|
#include "MainTask.h"
|
|
#include "app.h"
|
|
#include "app.h"
|
|
|
|
+#include "numeric.h"
|
|
//全局变量输入区
|
|
//全局变量输入区
|
|
extern UINT32 Timer_count;
|
|
extern UINT32 Timer_count;
|
|
extern volatile bool Sleep_flag;
|
|
extern volatile bool Sleep_flag;
|
|
|
|
+extern AppNVMDataType AppNVMData;
|
|
//全局变量输出区
|
|
//全局变量输出区
|
|
|
|
+UINT8 Uart_Write_Flag=0;
|
|
UartReadMsgType UartReadMsg;
|
|
UartReadMsgType UartReadMsg;
|
|
osMutexId_t UartMutex = NULL;//Uart数据锁
|
|
osMutexId_t UartMutex = NULL;//Uart数据锁
|
|
|
|
+Uart_Write_Data_Type UartWriteData; //Uart控制命令
|
|
//
|
|
//
|
|
-UINT8 Uart_Write_Flag=0;
|
|
|
|
|
|
+
|
|
extern ARM_DRIVER_USART Driver_USART1;
|
|
extern ARM_DRIVER_USART Driver_USART1;
|
|
static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
|
|
static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
|
|
volatile bool isRecvTimeout = false;
|
|
volatile bool isRecvTimeout = false;
|
|
@@ -51,7 +55,9 @@ static process_Uart gProcess_Uart_Task = PROCESS_UART_STATE_IDLE;
|
|
//函数声明区
|
|
//函数声明区
|
|
void USART_callback(uint32_t event);
|
|
void USART_callback(uint32_t event);
|
|
UINT8 Uart_Transmit_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer);
|
|
UINT8 Uart_Transmit_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer);
|
|
|
|
+UINT8 Uart_WriteCmd_func(Uart_Write_Data_Type UartWriteData);
|
|
UINT16 crc_chk(UINT8* data, UINT8 length);
|
|
UINT16 crc_chk(UINT8* data, UINT8 length);
|
|
|
|
+BOOL BattHeaterSwitch(UINT8* heaterSwitch);
|
|
//Uart线程任务区
|
|
//Uart线程任务区
|
|
static void UartTask(void* arg)
|
|
static void UartTask(void* arg)
|
|
{
|
|
{
|
|
@@ -71,6 +77,19 @@ static void UartTask(void* arg)
|
|
{
|
|
{
|
|
UartMutex = osMutexNew(NULL);
|
|
UartMutex = osMutexNew(NULL);
|
|
}
|
|
}
|
|
|
|
+ //上电起始控制区域
|
|
|
|
+ Uart_Write_Flag=UART_WRITE_FLAG;
|
|
|
|
+ UartWriteData.WriteCmd = 0x01;
|
|
|
|
+ if(AppNVMData.isBattLocked)
|
|
|
|
+ {
|
|
|
|
+ UartWriteData.Data[0] = 0x00;
|
|
|
|
+ UartWriteData.Data[1] = 0x00;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ UartWriteData.Data[0] = 0x00;
|
|
|
|
+ UartWriteData.Data[1] = 0x03;
|
|
|
|
+ }
|
|
while (1)
|
|
while (1)
|
|
{
|
|
{
|
|
switch (gProcess_Uart_Task)
|
|
switch (gProcess_Uart_Task)
|
|
@@ -87,7 +106,7 @@ static void UartTask(void* arg)
|
|
#ifdef USING_PRINTF
|
|
#ifdef USING_PRINTF
|
|
printf("[%d]Uart Timer 5s:%d\n",__LINE__,Timer_count);
|
|
printf("[%d]Uart Timer 5s:%d\n",__LINE__,Timer_count);
|
|
#endif
|
|
#endif
|
|
- if(Uart_Write_Flag==0)
|
|
|
|
|
|
+ if(Uart_Write_Flag==UART_READ_FLAG)
|
|
PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_READ);
|
|
PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_READ);
|
|
else
|
|
else
|
|
PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WRITE);
|
|
PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WRITE);
|
|
@@ -97,7 +116,7 @@ static void UartTask(void* arg)
|
|
case PROCESS_UART_STATE_READ:
|
|
case PROCESS_UART_STATE_READ:
|
|
{
|
|
{
|
|
osStatus_t result = osMutexAcquire(UartMutex, osWaitForever);
|
|
osStatus_t result = osMutexAcquire(UartMutex, osWaitForever);
|
|
- Reg_Num = 0x21+BATT_CELL_VOL_NUM+BATT_TEMP_NUM;//按照协议里面的0x21+X+N的结束地址
|
|
|
|
|
|
+ Reg_Num = 0x21+BATT_CELL_VOL_NUM+BATT_TEMP_NUM + 2;//按照协议里面的0x21+X+N的结束地址
|
|
Uart_Read_Msg.Bms_Address = BMS_ADDRESS_CODE;
|
|
Uart_Read_Msg.Bms_Address = BMS_ADDRESS_CODE;
|
|
Uart_Read_Msg.Bms_Funcode = UART_READ_CODE;
|
|
Uart_Read_Msg.Bms_Funcode = UART_READ_CODE;
|
|
Uart_Read_Msg.Reg_Begin_H = 0x00;
|
|
Uart_Read_Msg.Reg_Begin_H = 0x00;
|
|
@@ -114,6 +133,9 @@ static void UartTask(void* arg)
|
|
}
|
|
}
|
|
case PROCESS_UART_STATE_WRITE:
|
|
case PROCESS_UART_STATE_WRITE:
|
|
{
|
|
{
|
|
|
|
+ Uart_WriteCmd_func(UartWriteData);
|
|
|
|
+ Uart_Write_Flag=UART_READ_FLAG;
|
|
|
|
+ PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
case PROCESS_UART_STATE_SLEEP:
|
|
case PROCESS_UART_STATE_SLEEP:
|
|
@@ -183,6 +205,86 @@ UINT16 crc_chk(UINT8* data, UINT8 length)
|
|
}
|
|
}
|
|
return reg_crc;
|
|
return reg_crc;
|
|
}
|
|
}
|
|
|
|
+//Uart写命令函数
|
|
|
|
+UINT8 Uart_WriteCmd_func(Uart_Write_Data_Type UartWriteData)
|
|
|
|
+{
|
|
|
|
+ Uart_Write_Msg_Type Uart_Write_Msg;
|
|
|
|
+ UINT16 RegAddress = 0x0000;
|
|
|
|
+ UINT16 CRC_chk_buffer;
|
|
|
|
+ UINT8 timeout = 0x00;
|
|
|
|
+ UINT8 Uart_Recv_Buffer[8];
|
|
|
|
+ switch (UartWriteData.WriteCmd)
|
|
|
|
+ {
|
|
|
|
+ case 0x01:
|
|
|
|
+ {
|
|
|
|
+ RegAddress = 0x1B + BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM;
|
|
|
|
+ Uart_Write_Msg.Bms_Address = BMS_ADDRESS_CODE;
|
|
|
|
+ Uart_Write_Msg.Bms_Funcode = UART_WRITE_CODE;
|
|
|
|
+ Uart_Write_Msg.Reg_Begin_H = RegAddress>>8;
|
|
|
|
+ Uart_Write_Msg.Reg_Begin_L = RegAddress;
|
|
|
|
+ Uart_Write_Msg.Reg_Num_H = 0x00;
|
|
|
|
+ Uart_Write_Msg.Reg_Num_L = 0x01;
|
|
|
|
+ Uart_Write_Msg.Data_Count = 0x02;//要写入的字节数
|
|
|
|
+ memcpy(Uart_Write_Msg.Data,UartWriteData.Data,2);
|
|
|
|
+ CRC_chk_buffer = crc_chk((UINT8 *)&Uart_Write_Msg,sizeof(Uart_Write_Msg)-2);
|
|
|
|
+ Uart_Write_Msg.CRC_L = CRC_chk_buffer;
|
|
|
|
+ Uart_Write_Msg.CRC_H = CRC_chk_buffer>>8;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ default:
|
|
|
|
+ {
|
|
|
|
+ UartWriteData.WriteCmd = 0x00;
|
|
|
|
+ return 0;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ USARTdrv->Send((UINT8 *)&Uart_Write_Msg,sizeof(Uart_Write_Msg));
|
|
|
|
+ #ifdef USING_PRINTF
|
|
|
|
+ printf("Uart_Send_buffer: ");
|
|
|
|
+ for(int i=0;i<sizeof(Uart_Write_Msg);i++)
|
|
|
|
+ {
|
|
|
|
+ printf("%x ",*((UINT8 *)&Uart_Write_Msg+i));
|
|
|
|
+ }
|
|
|
|
+ printf("\n");
|
|
|
|
+ #endif
|
|
|
|
+ USARTdrv->Receive(Uart_Recv_Buffer,8);
|
|
|
|
+ while((isRecvTimeout == false) && (isRecvComplete == false))
|
|
|
|
+ {
|
|
|
|
+ timeout++;
|
|
|
|
+ osDelay(100);
|
|
|
|
+ if (timeout>=50)
|
|
|
|
+ {
|
|
|
|
+ timeout =0;
|
|
|
|
+ isRecvTimeout = true;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (isRecvComplete == true)
|
|
|
|
+ {
|
|
|
|
+ #ifdef USING_PRINTF
|
|
|
|
+ printf("Uart_Rece_buffer: ");
|
|
|
|
+ for(int i=0;i<8;i++)
|
|
|
|
+ {
|
|
|
|
+ printf("%x ",Uart_Recv_Buffer[i]);
|
|
|
|
+ }
|
|
|
|
+ printf("n");
|
|
|
|
+ #endif
|
|
|
|
+ isRecvComplete = false;
|
|
|
|
+ if(Uart_Recv_Buffer[1]==0x10)
|
|
|
|
+ {
|
|
|
|
+ return UartWriteData.WriteCmd;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ return 0x00;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ isRecvTimeout = false;
|
|
|
|
+ return 0x00;
|
|
|
|
+ }
|
|
|
|
+}
|
|
//Uart发送接收函数
|
|
//Uart发送接收函数
|
|
UINT8 Uart_Transmit_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer)
|
|
UINT8 Uart_Transmit_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer)
|
|
{
|
|
{
|
|
@@ -260,4 +362,57 @@ UINT8 Uart_Transmit_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer)
|
|
isRecvTimeout = false;
|
|
isRecvTimeout = false;
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
+}
|
|
|
|
+//
|
|
|
|
+/**
|
|
|
|
+ \fn BOOL BattHeaterSwitch(UINT8* heaterSwitch)
|
|
|
|
+ \param[in] (UINT8*) heaterSwitch: the heater switch state
|
|
|
|
+ \brief according to the current switch state and all the cell temp, it will turn on/off the switch
|
|
|
|
+ \return (BOOL) isNeedtoSwitch: true: need to send cmd to turn on/off the switch
|
|
|
|
+ false: do not need to do anything
|
|
|
|
+*/
|
|
|
|
+BOOL BattHeaterSwitch(UINT8* heaterSwitch)
|
|
|
|
+{
|
|
|
|
+ BOOL isNeedtoSwitch = FALSE;
|
|
|
|
+
|
|
|
|
+ UINT8 battCellTemp[BATT_TEMP_NUM];
|
|
|
|
+ UINT8 maxCellTemp,minCellTemp;
|
|
|
|
+
|
|
|
|
+ UINT8 i =0;
|
|
|
|
+ UINT8 currentSwitchState = 0;
|
|
|
|
+
|
|
|
|
+ //get the current switch state and the cell temp
|
|
|
|
+ currentSwitchState = UartReadMsg.data[(0x1C+BATT_CELL_VOL_NUM+(BATT_TEMP_NUM+2))*2+1]&0x01;
|
|
|
|
+ for(int i=0; i<BATT_TEMP_NUM; i++)
|
|
|
|
+ {
|
|
|
|
+ battCellTemp[i] = UartReadMsg.data[(0x06+BATT_CELL_VOL_NUM+i)*2+1];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //cal the maxtemp and mintemp
|
|
|
|
+ maxCellTemp = battCellTemp[0];
|
|
|
|
+ minCellTemp = battCellTemp[0];
|
|
|
|
+ for(i=1;i<BATT_TEMP_NUM;i++)
|
|
|
|
+ {
|
|
|
|
+ maxCellTemp = max(maxCellTemp,battCellTemp[i]);
|
|
|
|
+ minCellTemp = min(minCellTemp,battCellTemp[i]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if(currentSwitchState==0) //当前状态为关闭,判断是否应该开启
|
|
|
|
+ {
|
|
|
|
+ if(minCellTemp<=5+40 && maxCellTemp<25+40)//温度偏移为40
|
|
|
|
+ {
|
|
|
|
+ *heaterSwitch = 1;
|
|
|
|
+ isNeedtoSwitch = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else //当前状态为开启,判断是否应该关闭
|
|
|
|
+ {
|
|
|
|
+ if(minCellTemp>10+40||maxCellTemp>30+40)
|
|
|
|
+ {
|
|
|
|
+ *heaterSwitch = 0;
|
|
|
|
+ isNeedtoSwitch= true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return isNeedtoSwitch;
|
|
}
|
|
}
|