CELIS/UDS/uds_api.c
lidun 1e7070334c
增加UDS升级功能
Signed-off-by: lidun <1084178170@qq.com>
2024-01-28 22:06:01 +08:00

340 lines
12 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "uds_config.h"
#include "uds_nvm.h"
#include "24cxx.h"
#include "can.h"
#include "sys.h"
#include "dc300.h"
#include "adc.h"
#include "app_pwr_manage.h"
static uint32_t bootSwAddr;
extern DIDs gDIDCache;
extern void DTC_SignalInit(void);
extern void DID_SignalInit(void);
extern int8_t can_write2( CanTxMsg *TxMessage);
/* uds强制清除硬件自身DTC相关故障标志位 */
void UdsApi_HardwareFault_ForceClear(void)
{
//硬件强制清除自身DTC相关故障,请根据实际情况填入
}
#ifdef CS_TEMP_MODIFY
//extern bus_off_recovery_t* can_bus_off_recovery_info(uint8_t ch);
extern qiruibat_info_t QiruiBatteryInfo;
#endif
/* 硬件向UDS更新车身信号 */
void UdsApi_VehicleInfo2UdsProc(void)
{
vehicleInfo2uds.IgnSts = 1;
#ifdef CS_TEMP_MODIFY
vehicleInfo2uds.IgnSts = ChkInputIGN1();
if(battery_volt_state < NORM_VOLT)//供电电源低
vehicleInfo2uds.BattLow = 1;
else
vehicleInfo2uds.BattLow = 0;
if(battery_volt_state > NORM_VOLT)//供电电源高
vehicleInfo2uds.BattHigh = 1;
else
vehicleInfo2uds.BattHigh = 0;
vehicleInfo2uds.InOverVoltFault = mExistFdcFault( fault_grid_volt_over); // DCB是否有输入过压故障 0:false 1:true
vehicleInfo2uds.InUnderVoltFault =mExistFdcFault( fault_grid_volt_under); //DCB是否有输入欠压故障 0:false 1:true
vehicleInfo2uds.OutOverVoltFault =mExistFdcFault( fault_Bat_OverVolt); //DCB是否有输出过压故障 0:false 1:true
vehicleInfo2uds.OutUnderVoltFault =mExistFdcFault( fault_Bat_UnderVolt); //DCB是否有输出欠压故障 0:false 1:true
vehicleInfo2uds.InOverCurFault =mExistFdcFault( fault_grid_curr_over); //DCB是否有输入过流故障 0:false 1:true
vehicleInfo2uds.OutOverCurFault = 0; //DCB是否有输出过流故障 0:false 1:true
// vehicleInfo2uds.OutOverCurFault =mExistFdcFault( fault_grid_volt_over); //DCB是否有输出过流故障 0:false 1:true
vehicleInfo2uds.OverTemperture =mExistFdcFault( fault_temp_over); //FCDC环境过温保护故障 0:false 1:true
vehicleInfo2uds.InputShort =mExistFdcFault( fault_grid_input_reverse); //输入短路故障 0:false 1:true
vehicleInfo2uds.OutputShort =mExistFdcFault( fault_grid_output_reverse); //输出短路故障 0:false 1:true
// vehicleInfo2uds.PrechargeFault =0; //预充失败故障 0:false 1:true
// vehicleInfo2uds.FCDC_HardFault =0; //FCDC硬件故障 0:false 1:true
// vehicleInfo2uds.ChargeOverTemp =0; //充电座过温故障 0:false 1:true
// vehicleInfo2uds.FCDC_FanFault =0; //FCDC风扇故障 0:false 1:true
// vehicleInfo2uds.FCDC_CC1Fault =0; //FCDC检测CC1插枪检测故障 0:false 1:true
vehicleInfo2uds.FCDC_InAstVoltFault =mExistFdcFault( fault_power24_err); //FCDC输入辅助电源电压异常故障 0:false 1:true
// vehicleInfo2uds.FCDCIGBT_OverTempFault =0; //FCDCIGBT过温保护故障 0:false 1:true
// busoff计数器进入busoff后每恢复一次就累加1。CAN总线正常后退出busoff就清零。
vehicleInfo2uds.BusoffCnt = can_bus_off_recovery_times(1); // busoff计数器进入busoff后每恢复一次就累加1。CAN总线正常后退出busoff就清零。
vehicleInfo2uds.Snapshot.PowerMode = QiruiBatteryInfo.BCM_1.bits.KeySts ;//ECU供电模式(格式与诊断调查表 3_2_Snapshot&Extended Data List 一致)
vehicleInfo2uds.Snapshot.TolOdometerH = (QiruiBatteryInfo.ICM_1.bits.TotalOdometer_km >>16)&0xff; // 总里程高byte
vehicleInfo2uds.Snapshot.TolOdometerM = (QiruiBatteryInfo.ICM_1.bits.TotalOdometer_km >>8)&0xff; // 总里程中间byte
vehicleInfo2uds.Snapshot.TolOdometerL = (QiruiBatteryInfo.ICM_1.bits.TotalOdometer_km)&0xff; // 总里程低byte(格式与诊断调查表 3_2_Snapshot&Extended Data List 一致)
// vehicleInfo2uds.Snapshot.DateSec 时间 秒(格式与诊断调查表 3_2_Snapshot&Extended Data List 一致)
// vehicleInfo2uds.Snapshot.DateMin 时间 分(格式与诊断调查表 3_2_Snapshot&Extended Data List 一致)
// vehicleInfo2uds.Snapshot.DateHour 时间 小时(格式与诊断调查表 3_2_Snapshot&Extended Data List 一致)
// vehicleInfo2uds.Snapshot.DateDay 时间 日(格式与诊断调查表 3_2_Snapshot&Extended Data List 一致)
// vehicleInfo2uds.Snapshot.DateMonth 时间 月(格式与诊断调查表 3_2_Snapshot&Extended Data List 一致)
// vehicleInfo2uds.Snapshot.DateYear 时间 年(格式与诊断调查表 3_2_Snapshot&Extended Data List 一致)
// vehicleInfo2uds.Snapshot.VehSpeedH 车速高byte
// vehicleInfo2uds.Snapshot.VehSpeedL 车速低byte(格式与诊断调查表 3_2_Snapshot&Extended Data List 一致)
vehicleInfo2uds.Snapshot.DateSec = QiruiBatteryInfo.TBOX_2.bits.second;
vehicleInfo2uds.Snapshot.DateMin = QiruiBatteryInfo.TBOX_2.bits.minute;
vehicleInfo2uds.Snapshot.DateHour = QiruiBatteryInfo.TBOX_2.bits.hour;
vehicleInfo2uds.Snapshot.DateDay = QiruiBatteryInfo.TBOX_2.bits.day;
vehicleInfo2uds.Snapshot.DateMonth = QiruiBatteryInfo.TBOX_2.bits.month;
vehicleInfo2uds.Snapshot.DateYear = QiruiBatteryInfo.TBOX_2.bits.year;
vehicleInfo2uds.Snapshot.VehSpeedH = (uint8_t)(QiruiBatteryInfo.MCU_1.bits.MCU_SpeedVechile>>8);
vehicleInfo2uds.Snapshot.VehSpeedL = (uint8_t)(QiruiBatteryInfo.MCU_1.bits.MCU_SpeedVechile);
vehicleInfo2uds.Snapshot.BatVolt = (uint8_t)(Sample_DataS.VIN_Input_Voltage*10);// 蓄电池电压(格式与诊断调查表 3_2_Snapshot&Extended Data List 一致)
vehicleInfo2uds.Snapshot.SOCH = (uint8_t)((QiruiBatteryInfo.soc/5)>>8);// SOC(格式与诊断调查表 3_2_Snapshot&Extended Data List 一致)
vehicleInfo2uds.Snapshot.SOCL = (uint8_t)((QiruiBatteryInfo.soc/5)>>0);
/* 请根据实际情况填入
vehicleInfo2uds.IgnSts = ;
vehicleInfo2uds.BattLow = ;
vehicleInfo2uds.BattHigh = ;
vehicleInfo2uds.InOverVoltFault = ;
vehicleInfo2uds.InUnderVoltFault = ;
vehicleInfo2uds.OutOverVoltFault = ;
vehicleInfo2uds.OutUnderVoltFault = ;
vehicleInfo2uds.InOverCurFault = ;
vehicleInfo2uds.OutOverCurFault = ;
vehicleInfo2uds.OverTemperture = ;
vehicleInfo2uds.InputShort = ;
vehicleInfo2uds.OutputShort = ;
vehicleInfo2uds.PrechargeFault = ;
vehicleInfo2uds.FCDC_HardFault = ;
vehicleInfo2uds.ChargeOverTemp = ;
vehicleInfo2uds.FCDC_FanFault = ;
vehicleInfo2uds.FCDC_CC1Fault = ;
vehicleInfo2uds.FCDC_InAstVoltFault = ;
vehicleInfo2uds.FCDCIGBT_OverTempFault = ;
vehicleInfo2uds.BusoffCnt = ;
vehicleInfo2uds.Snapshot.PowerMode = ;
vehicleInfo2uds.Snapshot.TolOdometerH = ;
vehicleInfo2uds.Snapshot.TolOdometerM = ;
vehicleInfo2uds.Snapshot.TolOdometerL = ;
vehicleInfo2uds.Snapshot.DateSec = ;
vehicleInfo2uds.Snapshot.DateMin = ;
vehicleInfo2uds.Snapshot.DateHour = ;
vehicleInfo2uds.Snapshot.DateDay = ;
vehicleInfo2uds.Snapshot.DateMonth = ;
vehicleInfo2uds.Snapshot.DateYear = ;
vehicleInfo2uds.Snapshot.VehSpeedH = ;
vehicleInfo2uds.Snapshot.VehSpeedL = ;
vehicleInfo2uds.Snapshot.BatVolt = ;
vehicleInfo2uds.Snapshot.SOC = ;
*/
#endif
}
/* uds parameter init*/
void UdsApi_ParaInit(void)
{
#if BOOTLOADER_CODE_FLAG
memcpy(gDIDCache.DID_BootSoftware, BOOTLOADER_SOFTWARE_VERSION, DIDSize[BootSoftware]);
#else
memcpy(gDIDCache.DID_BootSoftwareNumber, (uint8_t *)bootSwAddr, DIDSize[BootSoftwareNumber]);
//以下请根据实际填入数据 ,长度不足用空格代替
memcpy(gDIDCache.DID_ECUManufacturerSparePartNumber, "2109140-SZ01", DIDSize[ECUManufacturerSparePartNumber]);
memcpy(gDIDCache.DID_SystemSupplierIdentifierData, " ", DIDSize[SystemSupplierIdentifierData]);
memcpy(gDIDCache.DID_SystemName, "DCU ", DIDSize[SystemName]);
memcpy(gDIDCache.DID_VehicleManufacturerECUHardwareNumber, "HW:A.1.0", DIDSize[VehicleManufacturerECUHardwareNumber]);
memcpy(gDIDCache.DID_VehicleManufacturerECUSoftwareNumber, "SW:DCU.1.34.01 ", DIDSize[VehicleManufacturerECUSoftwareNumber]);
memcpy(gDIDCache.DID_SystemSupplierECUHardwareNumber, " ", DIDSize[SystemSupplierECUHardwareNumber]);
memcpy(gDIDCache.DID_SystemSupplierECUSoftwareNumber, " ", DIDSize[SystemSupplierECUSoftwareNumber]);
// memcpy(gDIDCache.DID_ECUSerialNumber, "123456789012", DIDSize[ECUSerialNumber]);
#ifdef CS_TEMP_MODIFY
memcpy(gDIDCache.DID_BootSoftware, (uint8_t*)GetBootloaderSwVersionAddress(), DIDSize[BootSoftware]);
memset(gDIDCache.DID_ProgrammingDate, 0x00, DIDSize[ProgrammingDate]);
memset(gDIDCache.DID_TesterSerialNumber, 0x00, DIDSize[TesterSerialNumber]);
memset(gDIDCache.DID_VehicleIdentificationNumber, 0x00, DIDSize[VehicleIdentificationNumber]);
gDIDCache.DID_ActiveDiagnosticSession[0] = _UDS_DEFAULT_SESSION;
gDIDCache.DID_InputOvervoltageValue[0] = MSB(350);
gDIDCache.DID_InputOvervoltageValue[1] = LSB(350);
gDIDCache.DID_InputUndervoltageValue[0] = MSB(200);
gDIDCache.DID_InputUndervoltageValue[1] = LSB(200);
gDIDCache.DID_OutputOvervoltageValue[0] = MSB(130);
gDIDCache.DID_OutputOvervoltageValue[1] = LSB(130);
gDIDCache.DID_OutputUndervoltageValue[0] = MSB(60);
gDIDCache.DID_OutputUndervoltageValue[1] = LSB(60);
gDIDCache.DID_OutputOvercurrentFaultValue[0] = 120;
gDIDCache.DID_InputOvercurrentFaultValue[0] = 40;
gDIDCache.DID_OverTempertureProtectValue[0] = 85+40;
gDIDCache.DID_IGBTOverTempertureProtectValue[0] = 95+40;
gDIDCache.DID_ChargeOverTemperture[0] = 100+40;
#endif
#endif
}
/* uds 1ms task */
void UdsApi_1ms_task(void)
{
UdsProcTask();
}
/* uds 10ms task */
void UdsApi_10ms_task(void)
{
UdsApi_VehicleInfo2UdsProc();
}
/* uds can receive msg in */
void UdsApi_MsgQueueIn(CanRxMsg msg)
{
can_msg_t uds_msg;
uds_msg.id = msg.StdId;
uds_msg.dlc = msg.DLC;
memcpy(uds_msg.data, msg.Data, uds_msg.dlc);
UdsQueueCanIn(uds_msg);
}
/* uds transmit diag msg */
void UdsApi_MsgQueueOut(can_msg_t uds_msg)
{
CanTxMsg msg;
msg.IDE = CAN_Id_Standard;
msg.StdId = uds_msg.id;
msg.DLC = uds_msg.dlc;
msg.RTR = CAN_RTR_Data;
memcpy(msg.Data, uds_msg.data, msg.DLC);
// Can2_Send_Message(msg);
can_write2(&msg);
}
/* uds 生成随机数 */
uint32_t UdsApi_GetRandom(void)
{
static uint32_t Seed=0;
uint32_t Random;
if(Seed == 0)
{
Seed = SysTick->VAL;
Seed *= 91939597;
}
Random = Seed+SysTick->VAL;
Seed += 13957;
Random = (u32)( (u32)Random<<16)>>16;
Random |= ((Seed*5)<<16);
return Random;
}
/* eeprom write*/
uint8_t UdsApi_EE_WriteBytes(uint8_t *_pWriteBuf, uint16_t _usAddress, uint16_t _usSize)
{
return ee_WriteBytes(_pWriteBuf, _usAddress, _usSize);
}
/* eeprom read*/
uint8_t UdsApi_EE_ReadBytes(uint8_t *_pReadBuf, uint16_t _usAddress, uint16_t _usSize)
{
return ee_ReadBytes(_pReadBuf, _usAddress, _usSize);
}
/* NVM 初始化*/
void UdsApi_NVM_Init(void)
{
static uint8_t enter_flag = 0;
// Delay_MS(50);//等待电源稳定
if (!enter_flag) {
enter_flag = 1;
NVM_Init();
DID_SignalInit();
DTC_SignalInit();
}
}
/* 查询UDS是否使能常规通信报文发送 */
bool UdsApi_Is_UdsEnableNCMTx(void)
{
bool bRst=false;
if (udsInfo2Vehicle.NcmTxEnable) {
bRst = true;
}
return bRst;
}
/* 查询UDS是否使能网络管理报文发送 */
bool UdsApi_Is_UdsEnableNWMCMTx(void)
{
bool bRst=false;
if (udsInfo2Vehicle.NmcmTxEnable) {
bRst = true;
}
return bRst;
}
/* 查询UDS是否使能常规通信报文接收 */
bool UdsApi_Is_UdsEnableNCMRx(void)
{
bool bRst=false;
if (udsInfo2Vehicle.NcmRxEnable) {
bRst = true;
}
return bRst;
}
/* 查询UDS是否使能网络管理报文接收 */
bool UdsApi_Is_UdsEnableNWMCMRx(void)
{
bool bRst=false;
if (udsInfo2Vehicle.NmcmRxEnable) {
bRst = true;
}
return bRst;
}
/* 查询UDS是否在线 */
bool UdsApi_Is_UdsOnline(void)
{
bool bRst=false;
if (!udsInfo2Vehicle.UdsOffLine) {
bRst = true;
}
return bRst;
}
/* 查询是否为UDS响应消息 */
bool UdsApi_Is_UdsResponeMsg(uint16_t msg_id)
{
bool bRst=false;
if (msg_id == UDS_PHY_RX_ID) {
bRst = true;
}
return bRst;
}
/* 取BootLoader软件版本地址 */
void UdsApi_BootSwAddr_Get(void)
{
bootSwAddr = BKP->DR42;
bootSwAddr = (bootSwAddr << 16) | BKP->DR41;
}