123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- #include "uart_process.h"
- #include "var.h"
- #include "crc_cal.h"
- #include "stdlib.h"
- #include "stdio.h"
- #include "string.h"
- //命令处理
- void UART_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data)
- {
- // uint8_t DataLength;
- static uint8_t EnterCnt = 0;
-
- //通信正常标志
- IsComOK_TE = TRUE;
-
- //命令解析
- // DataLength = (uint8_t)(Command &0x00FF);
- switch(Command)
- {
- case 0x8008:
- {
- //复制TE返回故障码
- memcpy((uint8_t*)&MC_TE_SensorStatus.TE_ErrorCode, Data, sizeof(MC_TE_SensorStatus));
- //发送同步信号,清零计数
- EnterCnt++;
- if(EnterCnt >= 20)
- {
- if(MC_TE_SensorStatus.TE_ErrorCode.Status_Bit.BikeSpeed == 0)
- {
- MC_TE_SensorData.SpeedSensorTrigCnt = 0;
- }
- if(MC_TE_SensorStatus.TE_ErrorCode.Status_Bit.StopBreak == 0)
- {
- MC_TE_SensorData.BreakTrgiCnt = 0;
- }
- if(MC_TE_SensorStatus.TE_ErrorCode.Status_Bit.CadenseSensor_1 == 0)
- {
- MC_TE_SensorData.CadenceHall_1_Cnt = 0;
- }
- if(MC_TE_SensorStatus.TE_ErrorCode.Status_Bit.CadenseSensor_2 == 0)
- {
- MC_TE_SensorData.CadenceHall_2_Cnt = 0;
- }
- if(MC_TE_SensorStatus.TE_ErrorCode.Status_Bit.HallSensor_A == 0)
- {
- MC_TE_SensorData.MotorHall_A_Cnt = 0;
- }
- if(MC_TE_SensorStatus.TE_ErrorCode.Status_Bit.HallSensor_B == 0)
- {
- MC_TE_SensorData.MotorHall_B_Cnt = 0;
- }
- HAL_GPIO_TogglePin(SYC_IO_GPIO_Port, SYC_IO_Pin);
- EnterCnt = 0;
- }
- break;
- }
- default:break;
- }
- }
- //发送数据
- void SendCmdData(USART_Buf_TypeDef * ptTx, uint8_t Mode, uint16_t Command, uint8_t* Data)
- {
- uint8_t SendBuf[96] = {0};
- uint8_t CRC_Buf[98] = {0};
- uint32_t CRC_Result = 0x00000000;
- uint8_t DataLength;
-
- DataLength = (uint8_t)(Command & 0xFF);
- SendBuf[0] = FRAME_BEGIN1;
- SendBuf[1] = FRAME_BEGIN2;
- SendBuf[2] = 0x07;
- SendBuf[3] = 0xFF;
- SendBuf[4] = Mode;
- SendBuf[5] = DataLength + 2;
- SendBuf[6] = (uint8_t)((Command >> 8) & 0xFF);
- SendBuf[7] = DataLength;
- memcpy(SendBuf + 8, Data, DataLength);
- memcpy(CRC_Buf, SendBuf, 8 + DataLength);
-
- CRC_Result = CRC32_Calculate(CRC_Buf, DataLength + 8);
-
- SendBuf[8 + DataLength] = (uint8_t)((CRC_Result >> 24) & 0xFF);
- SendBuf[9 + DataLength] = (uint8_t)((CRC_Result >> 16) & 0xFF);
- SendBuf[10 + DataLength] = (uint8_t)((CRC_Result >> 8) & 0xFF);
- SendBuf[11 + DataLength] = (uint8_t)(CRC_Result & 0xFF);
-
- SendBuf[12 + DataLength] = FRAME_END;
-
- UART_SendData(ptTx, SendBuf, DataLength + 13);
- }
- //串口数据解析
- void Uart_RxData_Process(USART_Buf_TypeDef* ptUartRx, uint16_t TimeOut)
- {
- uint8_t Mode, CmdLength, DataLength, Data[64], CRC_Buf[78];
- uint16_t Cmd, i;
- uint32_t CrcResult, CrcData;
- uint8_t FrameBegin1, FrameBegin2;
- static uint32_t DelayTimeCnt = 0;
- static TrueOrFalse_Flag_Struct_t IsWaitRX_Flag = FALSE;
-
- if(ptUartRx->ucBufCnt >= 13)
- {
- //读取帧头
- FrameBegin1 = UART_ReadChar(ptUartRx, 0);
- CRC_Buf[0] = FrameBegin1;
- FrameBegin2 = UART_ReadChar(ptUartRx, 1);
- CRC_Buf[1] = FrameBegin2;
- if((FrameBegin1 == FRAME_BEGIN1) && (FrameBegin2 == FRAME_BEGIN2))
- {
- CRC_Buf[2] = UART_ReadChar(ptUartRx, 2);
- CRC_Buf[3] = UART_ReadChar(ptUartRx, 3);
- //读取帧模式
- Mode = UART_ReadChar(ptUartRx, 4);
- CRC_Buf[4] = Mode;
- if((Mode == MODE_READ) || (Mode == MODE_WRITE) || (Mode == MODE_REPORT))
- {
- //读取命令段长度和命令字
- CmdLength = UART_ReadChar(ptUartRx, 5);
- CRC_Buf[5] = CmdLength;
- Cmd = (UART_ReadChar(ptUartRx, 6) << 8) + UART_ReadChar(ptUartRx, 7);
- CRC_Buf[6] = (uint8_t)((Cmd >> 8) & 0xFF);
- CRC_Buf[7] = (uint8_t)(Cmd & 0xFF);
- DataLength = UART_ReadChar(ptUartRx, 7);
- if((CmdLength - DataLength) == 2)
- {
- if(ptUartRx->ucBufCnt < (CmdLength + 11))//帧头2bytes + ID2bytes 模式1byte + 命令段长度1byte + 校验位4bytes + 帧尾1byte
- {
- if(IsWaitRX_Flag == FALSE)
- {
- DelayTimeCnt = HAL_GetTick();
- IsWaitRX_Flag = TRUE;
- }
- if((HAL_GetTick() - DelayTimeCnt) > TimeOut)//超时,单位ms
- {
- UART_DelChar(ptUartRx, ptUartRx->ucBufCnt);
- IsWaitRX_Flag = FALSE;
- }
- return;
- }
- else
- {
- IsWaitRX_Flag = FALSE;
- //接收到完整正确数据包
- for(i=0; i<DataLength; i++)//读取数据段
- {
- Data[i] = UART_ReadChar(ptUartRx, 8 + i);
- CRC_Buf[8 + i] = Data[i];
- }
- CrcData = (UART_ReadChar(ptUartRx, 8 + DataLength) << 24) + \
- (UART_ReadChar(ptUartRx, 9 + DataLength) << 16) + \
- (UART_ReadChar(ptUartRx, 10 + DataLength) << 8) + \
- UART_ReadChar(ptUartRx, 11 + DataLength);
- CrcResult = CRC32_Calculate(CRC_Buf, 8 + DataLength);
- if((CrcData - CrcResult) == 0) // 比较校验
- {
- //数据处理
- UART_DataProcess(Mode, Cmd, Data);//Mode为帧模式,Cmd为命令字,Data为数据段
- UART_DelChar(ptUartRx, CmdLength + 11);
- return;
- }
- UART_DelChar(ptUartRx, 1);
- }
- }
- else
- {
- UART_DelChar(ptUartRx, 1);
- }
- }
- else
- {
- UART_DelChar(ptUartRx, 1);
- }
- }
- else
- {
- UART_DelChar(ptUartRx, 1);
- }
- }
- }
|