123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- #include "can_process.h"
- #include "uart_process.h"
- #include "crc_cal.h"
- #include "stdlib.h"
- #include "stdio.h"
- #include "string.h"
- #include "encrypt.h"
- #include "Update.h"
- #include "ctf_process.h"
- uint8_t Power_mode = 0;//0-default,1-off,2-on
- uint32_t T_startPowerOnOff = 0;
- void PowerOnOff(uint8_t *mode)
- {
- if(*mode == 2)
- {
- if((HAL_GetTick() - T_startPowerOnOff) >= 200)
- {
- T_startPowerOnOff = HAL_GetTick();
- uint8_t data[7] = {0x00,0x00,0x00,0x0B,0x00,0x00,0x00};//AD_CHECKMODE
- CAN_SendData(0x641,data,7);
- }
- }
- // if(*mode == 1)
- // {
- // if((HAL_GetTick() - T_startPowerOnOff) >= 1000)//Off延时1s
- // {
- // HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_RESET);//Off
- // *mode = 0;
- // }
- // }
- // else if(*mode == 2)
- // {
- // if((HAL_GetTick() - T_startPowerOnOff) >= 3000)//On延时3s
- // {
- // HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_RESET);//Off
- // *mode = 0;
- // }
- // }
- }
- //命令处理
- static void UART_DataProcess(uint8_t Mode, uint16_t Command, uint8_t* Data)
- {
- switch(Command)
- {
- //CDL在线检测
- case 0x1100:
- {
- SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x1100, (uint8_t*)NULL);
- break;
- }
- //设备开关机
- case 0x2201:
- {
- // if(Power_mode == 0)
- // {
- // T_startPowerOnOff = HAL_GetTick();
- //
- // if(Data[0] == 0xF0)//关机
- // {
- // Power_mode = 1;
- // HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_RESET);//On
- // }
- // else if(Data[0] == 0xF1)//开机
- // {
- // Power_mode = 2;
- // HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_SET);//Off
- // }
- // }
-
- if(Data[0] == 0xF0)//关机
- {
- Power_mode = 1;
- HAL_GPIO_WritePin(SwitchOnOrOff_GPIO_Port, SwitchOnOrOff_Pin, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(ACC_MOTINOVA_EN_GPIO_Port, ACC_MOTINOVA_EN_Pin, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_RESET);//Off
- }
- else if(Data[0] == 0xF1)//开机
- {
- Power_mode = 2;
- HAL_GPIO_WritePin(SwitchOnOrOff_GPIO_Port, SwitchOnOrOff_Pin, GPIO_PIN_SET);
- HAL_GPIO_WritePin(ACC_MOTINOVA_EN_GPIO_Port, ACC_MOTINOVA_EN_Pin, GPIO_PIN_SET);
- HAL_GPIO_WritePin(ACC_WELLING_EN_GPIO_Port, ACC_WELLING_EN_Pin, GPIO_PIN_SET);//On
- }
- break;
- }
- //复位
- case 0x4400:
- {
- Refresh_Flag();
- __set_FAULTMASK(1);//关闭所有中断
- HAL_NVIC_SystemReset();
- break;
- }
- //随机码和密钥进行校验
- case 0x5514:
- {
- static uint8_t InputCode[12];
- static uint8_t InputKey[8];
- static uint8_t OutputCode[12];
- static uint8_t SendData[27];
-
- memcpy(InputCode, Data, 12);
- memcpy(InputKey, Data + 12, 8);
- CheckCodeCal(InputCode, InputKey, OutputCode);
- memcpy(SendData, OutputCode, 12);
- memcpy(SendData + 12, (uint8_t*)"V3.0.2_20250312", 15);
-
- SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x551B, SendData);
- break;
- }
- //授权
- case 0x6600:
- {
- uint8_t SendData[24];
- Ctf_CalAndSave();
- memcpy(SendData, MAC_ID, 12);
- memcpy(SendData + 12, Check_Code, 12);
- SendCmdData(&UART_TxBuff_Struct3, MODE_REPORT, 0x6618, SendData);
- HAL_Delay(200);
- __set_FAULTMASK(1);//关闭所有中断
- HAL_NVIC_SystemReset();
- break;
- }
- default:break;
- }
- }
- //发送数据
- void SendCmdData(USART_Buf_TypeDef * ptTx, uint8_t Mode, uint16_t Command, uint8_t* Data)
- {
- uint8_t SendBuf[255] = {0};
- uint8_t CRC_Buf[255] = {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;
- static uint8_t Data[255], CRC_Buf[255];
- uint16_t Cmd, i, ID;
- 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);
- ID = (uint16_t)(CRC_Buf[2] << 8) + CRC_Buf[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为数据段
- //数据转发CAN
- memcpy((uint8_t*)(CRC_Buf + 2), (uint8_t*)(CRC_Buf + 4), DataLength + 4);
- CRC_Buf[6 + DataLength] = UART_ReadChar(ptUartRx, 8 + DataLength);
- CRC_Buf[7 + DataLength] = UART_ReadChar(ptUartRx, 9 + DataLength);
- CRC_Buf[8 + DataLength] = UART_ReadChar(ptUartRx, 10 + DataLength);
- CRC_Buf[9 + DataLength] = UART_ReadChar(ptUartRx, 11 + DataLength);
- CRC_Buf[10 + DataLength] = FRAME_END;
- CAN_SendData(ID, CRC_Buf, DataLength + 11);
- //清除缓存
- UART_DelChar(ptUartRx, CmdLength + 11);
- //通信指示
- HAL_GPIO_TogglePin(LED_Display_GPIO_Port, LED_Display_Pin);
- return;
- }
- UART_DelChar(ptUartRx, 1);
- }
- }
- else
- {
- UART_DelChar(ptUartRx, 1);
- }
- }
- else
- {
- UART_DelChar(ptUartRx, 1);
- }
- }
- else
- {
- UART_DelChar(ptUartRx, 1);
- }
- }
- }
|