Commit 4655f414 authored by 时海鑫's avatar 时海鑫

feat:提交代码 uds

parent 1ad0a9ce
/******************************************************************************
文 件 名:Diag_ID_Def
功能描述:诊断地址配置文件
作 者:张暄
版 本:V1.0
日 期:2016.9.22
******************************************************************************/
#ifndef _DIAG_ID_DEF_H_
#define _DIAG_ID_DEF_H_
#define DIAG_ID_Tx 0x0725 /*发送ID*/
#define DIAG_ID_Rx_PHY 0x0705 /*接收ID,物理寻址*/
#define DIAG_ID_Rx_FUN 0x07DF /*接收ID,功能寻址*/
#endif
/******************************************************************************
文 件 名:DoCAN_ISO15765.c
功能描述:ISO 15765 规范规定的诊断服务函数库文件
作 者:张暄
版 本:V1.0
日 期:2016.7.18
******************************************************************************/
#include "DoCAN_ISO15765.h"
#include "can.h"
#include "UDS_ISO14229_Server.h"
#include "UDS_ISO14229_Server.h"
LinkRxFIFOStruct LinkRxFIFO;
LinkTxCtrlStruct LinkTxCtrl;
volatile TransportDataUnion TransportRxData;
volatile TransportDataUnion TransportTxData;
N_USDataRxStruct N_USDataRxBuffer;
N_USDataTxStruct N_USDataTxBuffer;
TransportControlStruct TpCtrl;
TransportTimingControlStruct TpTimingCtrl;
uint8_t DiagnosticReceived;
/******************************************************************************
后台服务
******************************************************************************/
/******************************************************************************
函数名:DoCAN_Communication_Service
功 能:基于CAN总线的诊断通信服务后台控制函数
参 数:无
返回值:无
*******************************************************************************
注 意:该服务函数必须被实时调用
******************************************************************************/
void DoCAN_Communication_Service(void)
{
DoCAN_Update_Timer( );
DoCAN_Handle_Time_Out( );
UDS_Server_Application_Service( );
DoCAN_Receive_And_Assemble_N_USData( );
DoCAN_Disassemble_And_Transmit_N_USData( );
}
/******************************************************************************
函数名:DoCAN_Timer_Update
功 能:更新定时器数值,用于时序控制以及STmin计时
参 数:Interval:定时器两次更新的时间间隔,单位us
返回值:无
*******************************************************************************
注 意:该服务函数必须被嵌入到定时中断中
******************************************************************************/
void DoCAN_Timer_Update(uint16_t Interval)
{
TpTimingCtrl.Cnt += Interval;
}
/******************************************************************************
Services provided by network layer to higher layers
******************************************************************************/
/******************************************************************************
函数名:DoCAN_N_USData_Request
功 能:该服务函数用于请求传输数据
This service is used to request the transfer of data. If necessary, the
network layer segments the data.
参 数:N_TAtype :目标地址类型,发送数据请使用 DIAG_ID_Tx
MessageData :请求传输的数据
Length :数据长度
返回值:无
******************************************************************************/
void DoCAN_N_USData_Request(uint16_t N_TAtype, uint8_t *MessageData, uint16_t Length)
{
uint8_t i;
// 非空闲状态下不允许发送
if ( TpCtrl.Process != TP_IDLE )
{
DoCAN_N_USData_Confirm(N_TAtype, N_ERROR);
return;
}
if ( (Length == 0) || (Length > N_USDATA_TX_BUFFER_SIZE) ) // 非法的长度
{
DoCAN_N_USData_Confirm(N_TAtype, N_ERROR);
return;
}
// 将数据缓存到发送Buffer中
N_USDataTxBuffer.N_TAtype = N_TAtype;
N_USDataTxBuffer.Length = Length;
for ( i = 0; i < Length; i++ )
{
N_USDataTxBuffer.MsgData [ i ] = MessageData [ i ];
}
// 更新传输层状态
TpCtrl.NonStopMode = 0;
TpCtrl.TotalLen = Length; // 记录字节总数
TpCtrl.BSMax = 0xFF; // 初始化BSMax数值
TpCtrl.STmin = 0x7F;
// 拆分为N_PDU
if ( TpCtrl.TotalLen < 8 ) // 数据总长度小于8,采用单帧发送
{
TpCtrl.BlockSize = 1; // 单帧,Block总数为1
TpCtrl.Len = TpCtrl.TotalLen; // 单帧发出全部的数据
TpCtrl.BlockCnt = 1; // 记录已发送Block数
TransportTxData.N_PDU_SF.N_TAtype = N_USDataTxBuffer.N_TAtype;
TransportTxData.N_PDU_SF.N_PCI.Type = SINGLE_FRAME;
TransportTxData.N_PDU_SF.N_PCI.SF_DL = ( uint8_t )(TpCtrl.TotalLen);
TransportTxData.N_PDU_SF.DLC = ( uint8_t )(TpCtrl.TotalLen) + 1;
for ( i = 0; i < TpCtrl.TotalLen; i++ )
TransportTxData.N_PDU_SF.N_Data [ i ] = N_USDataTxBuffer.MsgData [ i ];
}
else // 数据总长度大于等于8,采用多帧发送,首先发送首帧
{
if ( (Length + 1) % 7 ) // 计算BlockSize
TpCtrl.BlockSize = (Length + 8) / 7; // 等效于(Length + 1) / 7 + 1
else
TpCtrl.BlockSize = (Length + 1) / 7;
TpCtrl.Len = 6; // 首帧发出数据长度为6
TpCtrl.BlockCnt = 1; // 记录已发送Block数
TransportTxData.N_PDU_FF.N_TAtype = N_USDataTxBuffer.N_TAtype;
TransportTxData.N_PDU_FF.N_PCI.Type = FIRST_FRAME;
TransportTxData.N_PDU_FF.N_PCI.FF_DL_H = ( uint8_t )((TpCtrl.TotalLen >> 8) & 0x000F);
TransportTxData.N_PDU_FF.N_PCI.FF_DL_L = ( uint8_t )(TpCtrl.TotalLen & 0x00FF);
TransportTxData.N_PDU_FF.DLC = 8;
for ( i = 0; i < 6; i++ )
TransportTxData.N_PDU_FF.N_Data [ i ] = N_USDataTxBuffer.MsgData [ i ];
}
DoCAN_Start_Timer(TIMING_PARA_N_As); // 启动N_As计时
DoCAN_L_Data_Request(TransportTxData.Frame.Identifier, TransportTxData.Frame.DLC, ( uint8_t * )TransportTxData.Frame.Data);
TpCtrl.Process = TP_TX_INIT; // 数据帧已发出,等待发送完成
DoCAN_L_Data_Confirm(TransportTxData.Frame.Identifier, COMPLETE);
}
/******************************************************************************
函数名:DoCAN_N_USData_Confirm
功 能:该服务函数由网络层发起,用于确认前一次使用N_USData.request服务向
N_TAtype地址发送的数据是否发送完成
The N_USData.confirm service is issued by the network layer. The service
primitive confirms the completion of an N_USData.request service
identified by the address information in N_TAtype.
参 数:N_TAtype :N_USData.confirm服务请求的发送地址
N_Result :N_USData.request服务的传输状态
返回值:无
******************************************************************************/
void DoCAN_N_USData_Confirm(uint16_t N_TAtype, N_ResultEnum N_Result)
{
UDS_N_USData_Confirm(N_TAtype, N_Result);
}
/******************************************************************************
函数名:DoCAN_N_USData_FF_Indication
功 能:该服务函数由网络层发起,用于向上层指示地址为N_TAtype的一组多帧数据的首帧
的到来
The N_USData_FF.indication service is issued by the network layer. The
service primitive indicates to the adjacent upper layer the arrival of
a FirstFrame (FF) of a segmented message received from a peer protocol
entity, identified by the address information N_TAtype.
参 数:N_TAtype :新到的多帧数据首帧的地址信息
Length :新到的多帧数据的总长度
返回值:无
******************************************************************************/
void DoCAN_N_USData_FF_Indication(uint16_t N_TAtype, uint16_t Length)
{
UDS_N_USData_FF_Indication(N_TAtype, Length);
}
/******************************************************************************
函数名:DoCAN_N_USData_Indication
功 能:该服务函数由网络层发起,用于向上层指出由N_TAtype地址发送来的长度为Length
的MessageData数据的传送结果N_Result,并同时传递这一数据
The N_USData.indication service is issued by the network layer. The
service primitive indicates <N_Result> events and delivers
<MessageData> with <Length> bytes received from a peer protocol entity
identified by the address information in N_TAtype to the adjacent upper
layer.
参 数:N_TAtype :接收到的数据地址信息
MessageData :接收到的数据 (仅在N_Result为N_OK时有效)
Length :接收到的数据长度 (仅在N_Result为N_OK时有效)
N_Result :数据的接收结果
返回值:无
******************************************************************************/
void DoCAN_N_USData_Indication(uint16_t N_TAtype, uint8_t *MessageData, uint16_t Length, N_ResultEnum N_Result)
{
UDS_N_USData_Indication(N_TAtype, MessageData, Length, N_Result);
}
/******************************************************************************
下层接口函数
******************************************************************************/
/******************************************************************************
函数名:DoCAN_L_Data_Request
功 能:该服务原语请求向<Identifier>传输特定格式的<Data>数据
The service primitive requests transmission of <Data> that shall be
mapped within specific attributes of the data link protocol data unit
selected by means of <Identifier>.
参 数:Identifier :报文ID
dlc :数据长度
Data :数据<Data>于数据链路层的存放地址
返回值:无
******************************************************************************/
void DoCAN_L_Data_Request(uint16_t Identifier, uint8_t dlc, uint8_t *Data)
{
uint8_t i;
CanTxRxMsg msg_diag_tx;
LinkTxCtrl.Identifier = Identifier;
for (i = 0; i < dlc; i++)
{
LinkTxCtrl.Data[i] = Data[i];
}
for (i = dlc; i < 8; i++)
{
LinkTxCtrl.Data[i] = FILLER_BYTE;
}
msg_diag_tx.Id = LinkTxCtrl.Identifier;
msg_diag_tx.DLC = 8;
msg_diag_tx.RTR = 0;
msg_diag_tx.IDE = 0;
msg_diag_tx.CacheType = CAN_CacheType_Tx;
for (i = 0; i < 8; i++)
{
msg_diag_tx.Data[i] = LinkTxCtrl.Data[i];
}
CAN_Transmit(CAN0MSG00, &msg_diag_tx);
// Can_Write(&msg);
// CANFD_Ch1_L_Data_Request(LinkTxCtrl.Identifier,8,LinkTxCtrl.Data);
LinkTxCtrl.Busy = 1;
}
/******************************************************************************
函数名:DoCAN_L_Data_Confirm
功 能:该服务原语用于确认具有特定<Identifier>的报文是否发送完成。
<TransferStatus>参数用于传递发送完成与否的状态。
The service primitive confirms the completion of an L_Data.request
service for a specific <Identifier>.The parameter <TransferStatus>
provides the status of the service request.
参 数:Identifier :报文ID
TransferStatus :Not_Complete - 未完成
Complete - 已发送完成
返回值:无
******************************************************************************/
void DoCAN_L_Data_Confirm(uint16_t Identifier, uint8_t TransferStatus)
{
// 如果数据发送成功,收发状态相应改变
if ( (LinkTxCtrl.Identifier == Identifier) && (TransferStatus == COMPLETE) )
{
switch ( TpCtrl.Process )
{
case TP_TX_INIT:
if ( TpCtrl.BlockSize == 1 ) // BlockSize为1,单帧发送成功
{
TpCtrl.Process = TP_IDLE; // 所有数据发送完毕,回到空闲状态
DoCAN_Stop_Timer( ); // 停止计时
DoCAN_N_USData_Confirm(N_USDataTxBuffer.N_TAtype, N_OK);
}
else // BlockSize不为1,首帧发送成功
{
DoCAN_Start_Timer(TIMING_PARA_N_Bs); // 启动N_Bs计时
TpCtrl.Process = TP_TX_RTS; // 等待接收机许可多帧发送
}
break;
case TP_TX_CTS:
if ( TpCtrl.BlockCnt == TpCtrl.BlockSize ) // 最后一帧发送完成
{
TpCtrl.Process = TP_IDLE; // 所有数据发送完毕,回到空闲状态
DoCAN_Stop_Timer( ); // 停止计时
// DoCAN_Start_Timer(TIMING_PARA_N_Bs);
DoCAN_N_USData_Confirm(N_USDataTxBuffer.N_TAtype, N_OK);
}
else
{
// DoCAN_Start_Timer ( TIMING_PARA_N_Bs ); //启动/重启N_Bs计时
if ( TpCtrl.BSMax ) // 还可以继续发送数据帧,则启动STmin计时
{
DoCAN_Stop_Timer( );
DoCAN_Start_STmin_Timer(TpCtrl.STmin);
}
else
{
DoCAN_Start_Timer(TIMING_PARA_N_Bs);
}
}
break;
case TP_RX_RTS:
DoCAN_Start_Timer(TIMING_PARA_N_Cr); // 启动N_Cr计时
TpCtrl.Process = TP_RX_CTS;
break;
case TP_RX_WAIT:
DoCAN_Start_Timer(TIMING_PARA_N_Cr); // 启动N_Cr计时
TpCtrl.Process = TP_RX_CTS;
break;
case TP_RX_OVFL:
TpCtrl.Process = TP_IDLE; // 停止接收过程
DoCAN_Stop_Timer( ); // 停止计时
DoCAN_N_USData_Indication(N_USDataRxBuffer.N_TAtype, N_USDataRxBuffer.MsgData,
N_USDataRxBuffer.Length, N_ERROR);
break;
default:
break;
}
}
}
/******************************************************************************
函数名:DoCAN_L_Data_Indication
功 能:该服务原语标示一个数据链路层事件到相邻上层并根据<Identifier>传送<Data>
The service primitive indicates a data link layer event to the adjacent
upper layer and delivers <Data> identified by <Identifier>
参 数:Identifier :报文ID
dlc :数据长度
pData :数据<Data>于数据链路层的存放地址
返回值:无
******************************************************************************/
void DoCAN_L_Data_Indication(uint16_t Identifier, uint32_t dlc, uint8_t *pData)
{
uint8_t i;
if ( LinkRxFIFO.Depth >= LINK_RX_FIFO_MAX_DEPTH ) // FIFO未满时装入报文,否则抛弃报文
{
#if DOCAN_DEBUG_EN
LINK_RX_FIFO_OVERFLOW_INDICATOR( );
#endif
return;
}
LinkRxFIFO.LinkData [ LinkRxFIFO.IPtr ].Identifier = Identifier; // 装入报文ID
for ( i = 0; i < dlc; i++ ) // 拷贝报文
LinkRxFIFO.LinkData [ LinkRxFIFO.IPtr ].Data [ i ] = *(pData + i);
LinkRxFIFO.LinkData [ LinkRxFIFO.IPtr ].DLC = (uint8_t)dlc; // 保存报文长度
LinkRxFIFO.Depth++; // 新报文装入,深度+1
LinkRxFIFO.IPtr++; // 输入指针+1
if ( LinkRxFIFO.IPtr >= LINK_RX_FIFO_MAX_DEPTH )
LinkRxFIFO.IPtr = 0;
}
/******************************************************************************
传输层协议解析
******************************************************************************/
/******************************************************************************
函数名:DoCAN_Receive_And_Assemble_N_USData();
功 能:接收N_PDU并重组数据
参 数:无
返回值:无
******************************************************************************/
void DoCAN_Receive_And_Assemble_N_USData(void)
{
DoCAN_Get_N_PDU( );
if ( TransportRxData.N_PDU.New )
{
switch ( TransportRxData.N_PDU.N_PCI.Type )
{
case SINGLE_FRAME:
DoCAN_Receive_Single_Frame_N_Data( );
break;
case FIRST_FRAME:
DoCAN_Receive_First_Frame_N_Data( );
DoCAN_Transmit_Flow_Control( ); // 首帧发送完毕后必须发送流控帧
break;
case CONSECUTIVE_FRAME:
DoCAN_Receive_Consecutive_Frame_N_Data( );
DoCAN_Transmit_Flow_Control( ); // 当接收满一个BlockSize时,需要发送流控帧
break;
case FLOW_CONTROL:
DoCAN_Receive_Flow_Control( );
break;
default:
DoCAN_Handle_Unknown_N_PDU( );
break;
}
TransportRxData.N_PDU_FC.New = 0;
}
}
/******************************************************************************
函数名:DoCAN_Get_N_PDU
功 能:从数据链路层获取N_PDU(network protocol data unit)
参 数:无
返回值:无
******************************************************************************/
void DoCAN_Get_N_PDU(void)
{
uint8_t i;
if ( LinkRxFIFO.Depth ) // FIFO中有数据
{
TransportRxData.Frame.Identifier = LinkRxFIFO.LinkData [ LinkRxFIFO.OPtr ].Identifier; // 取出ID
for ( i = 0; i < 8; i++ ) // 拷贝报文
TransportRxData.Frame.Data [ i ] = LinkRxFIFO.LinkData [ LinkRxFIFO.OPtr ].Data [ i ];
TransportRxData.Frame.DLC = LinkRxFIFO.LinkData [ LinkRxFIFO.OPtr ].DLC; // 取出报文长度
TransportRxData.Frame.New = 1;
LinkRxFIFO.Depth--; // 报文已取出,深度-1
LinkRxFIFO.OPtr++; // 输出指针+1
if ( LinkRxFIFO.OPtr >= LINK_RX_FIFO_MAX_DEPTH )
LinkRxFIFO.OPtr = 0;
}
}
/******************************************************************************
函数名:DoCAN_Receive_Single_Frame_N_Data
功 能:单帧数据接收
参 数:无
返回值:无
******************************************************************************/
void DoCAN_Receive_Single_Frame_N_Data(void)
{
uint8_t i;
if ( (TpCtrl.Process & TP_DIR_MASK) == TP_TX ) // 发送中不接收
return;
/*---------------------------------------------------------------------------
Whether the interleaving Tester Present messages would lead to a loss of CF
or termination of the reception of an ongoing multiple frame request
2018.11.29
---------------------------------------------------------------------------*/
if ( (TpCtrl.Process != TP_IDLE) && (TransportRxData.N_PDU_SF.N_TAtype != DIAG_ID_Rx_PHY) )
return;
/*---------------------------------------------------------------------------
Tester sends a Single frame with a CAN-DLC shorter and equal to the transport
protocol data length field. ECU must not send a response.
2018.11.28添加长度还要等于8
---------------------------------------------------------------------------*/
if ( (TransportRxData.N_PDU_SF.DLC <= TransportRxData.N_PDU_SF.N_PCI.SF_DL) || (TransportRxData.N_PDU_SF.DLC != 8) )
return;
/*---------------------------------------------------------------------------
Tester sends a Single frame with a data length that is not allowed by the
protocol. ECU must not send a response.
---------------------------------------------------------------------------*/
if ( (TransportRxData.N_PDU_SF.N_PCI.SF_DL == 0) || (TransportRxData.N_PDU_SF.N_PCI.SF_DL > 7) )
return;
N_USDataRxBuffer.N_TAtype = TransportRxData.N_PDU_SF.N_TAtype;
N_USDataRxBuffer.Length = TransportRxData.N_PDU_SF.N_PCI.SF_DL;
for ( i = 0; i < N_USDataRxBuffer.Length; i++ )
N_USDataRxBuffer.MsgData [ i ] = TransportRxData.N_PDU_SF.N_Data [ i ];
TpCtrl.Process = TP_IDLE; // 接收完成,回到IDLE状态
DoCAN_Stop_Timer( );
DoCAN_N_USData_Indication(N_USDataRxBuffer.N_TAtype, N_USDataRxBuffer.MsgData,
N_USDataRxBuffer.Length, N_OK);
}
/******************************************************************************
函数名:DoCAN_Receive_First_Frame_N_Data
功 能:接收首帧数据并初始化多帧数据接收
参 数:无
返回值:无
******************************************************************************/
void DoCAN_Receive_First_Frame_N_Data(void)
{
uint8_t i;
uint16_t Len;
if ( (TpCtrl.Process & TP_DIR_MASK) == TP_TX ) // 发送中不接收
return;
/*-----------------------------------------------------------------------------
Tester sends a functional addressed First frame. ECU must not send a response.
-----------------------------------------------------------------------------*/
if ( TransportRxData.N_PDU_FF.N_TAtype != DIAG_ID_Rx_PHY )
return;
/*---------------------------------------------------------------------------
(8.3) A FirstFrame protocol data unit (FF N_PDU), containing the first six
data bytes
---------------------------------------------------------------------------*/
if ( TransportRxData.N_PDU_FF.DLC != 8 ) // 首帧总长(N_PCI + N_Data)应为8
return;
/*---------------------------------------------------------------------------
Tester sends a First frame with Data length set to 0. ECU must not send a
response.
-----------------------------------------------------------------------------
(8.5.3.3) If the network layer receives an FF with an FF_DL that is less than
eight when using normal addressing, then the network layer shall ignore the
received FF N_PDU and not transmit an FC N_PDU.
---------------------------------------------------------------------------*/
Len = ( uint16_t )(TransportRxData.N_PDU_FF.N_PCI.FF_DL_H);
Len <<= 8;
Len |= ( uint16_t )(TransportRxData.N_PDU_FF.N_PCI.FF_DL_L);
if ( Len < 8 )
return;
/*---------------------------------------------------------------------------
Tester sends a First frame of a segmented request. After the receipt of the
ECU Flow Control the Tester sends another segmented request. The ECU must
send a response for the second request.
---------------------------------------------------------------------------*/
#if ( N_MAX_BS )
TpCtrl.NonStopMode = 0;
#else
TpCtrl.NonStopMode = 1;
#endif
TpCtrl.Len = 6; // 首帧接收6字节
TpCtrl.TotalLen = Len; // 记录字节总数
TpCtrl.BlockCnt = 1; // 首帧接收1个block
TpCtrl.BSMax = N_MAX_BS; // 装入最大的BS数
TpCtrl.WFTCnt = N_WFTmax; // 装入最大等待次数
// 首帧含6字节数据,连续帧含7字节数据,根据字节数计算Block总数
if ( (TpCtrl.TotalLen + 1) % 7 )
TpCtrl.BlockSize = (TpCtrl.TotalLen + 8) / 7; // 等效于(TpCtrl.TotalLen + 1) / 7 + 1
else
TpCtrl.BlockSize = (TpCtrl.TotalLen + 1) / 7;
// 开始接收首帧数据
N_USDataRxBuffer.N_TAtype = TransportRxData.N_PDU_FF.N_TAtype;
N_USDataRxBuffer.Length = Len;
for ( i = 0; i < 6; i++ )
N_USDataRxBuffer.MsgData [ i ] = TransportRxData.N_PDU_FF.N_Data [ i ];
DoCAN_Start_Timer(TIMING_PARA_N_Br); // 启动N_Br计时
TpCtrl.Process = TP_RX_INIT;
DoCAN_N_USData_FF_Indication(N_USDataRxBuffer.N_TAtype, N_USDataRxBuffer.Length);
}
/******************************************************************************
函数名:DoCAN_Receive_Consecutive_Frame_N_Data
功 能:接收连接帧数据
参 数:无
返回值:无
******************************************************************************/
void DoCAN_Receive_Consecutive_Frame_N_Data(void)
{
uint8_t i;
/*---------------------------------------------------------------------------
Tester sends a request with a response that is longer than one frame. After
the Flow control the Tester sends a Consecutive frame. ECU must send a
diagnostic response for the first request. ECU must not send a response
for the Consecutive frame.
-----------------------------------------------------------------------------
Tester sends a single Consecutive frame. The ECU must not send a response.
---------------------------------------------------------------------------*/
if ( TpCtrl.Process != TP_RX_CTS )
return;
if ( TransportRxData.N_PDU_CF.N_TAtype != DIAG_ID_Rx_PHY )
return;
/*---------------------------------------------------------------------------
Send a request which requires more than 4 frames. Drop the third consecutive
frame (CF). The ECU should not respond.
-----------------------------------------------------------------------------
Send a multi frame message to the ECU. Send the first consecutive frame (CF)
twice. No response is expected.
---------------------------------------------------------------------------*/
if ( TransportRxData.N_PDU_CF.N_PCI.SN != ( uint8_t )(TpCtrl.BlockCnt & 0x000F) )
{
TpCtrl.Process = TP_IDLE; // 停止接收过程
DoCAN_Stop_Timer( ); // 停止计时
DoCAN_N_USData_Indication(N_USDataRxBuffer.N_TAtype, N_USDataRxBuffer.MsgData,
N_USDataRxBuffer.Length, N_WRONG_SN);
return;
}
TpCtrl.BlockCnt++; // 接收到1个Block
TpCtrl.BSMax--;
if ( TpCtrl.BlockCnt < TpCtrl.BlockSize ) // 此次接收不是最后一帧
{
if ( TransportRxData.N_PDU_CF.DLC != 8 ) // 此时连续帧总长(N_PCI + N_Data)应为8
return;
for ( i = 0; i < 7; i++ )
{
N_USDataRxBuffer.MsgData [ TpCtrl.Len ] = TransportRxData.N_PDU_CF.N_Data [ i ];
TpCtrl.Len++;
}
DoCAN_Start_Timer(TIMING_PARA_N_Cr); // 重启N_Cr计时
}
else // 此次接收是最后一帧
{
/*-------------------------------------------------------------------------
In a segmented request the Tester sends a Consecutive frame with a CAN-DLC
shorter or equal to transport protocol data length field. ECU must not send
a response.
-------------------------------------------------------------------------*/
if ( (TransportRxData.N_PDU_CF.DLC <= TpCtrl.TotalLen - TpCtrl.Len) || (TransportRxData.N_PDU_CF.DLC != 8) ) // 此时连续帧总长应不小于剩余数据长度
return;
i = 0;
while ( TpCtrl.Len < TpCtrl.TotalLen )
{
N_USDataRxBuffer.MsgData [ TpCtrl.Len ] = TransportRxData.N_PDU_CF.N_Data [ i ];
i++;
TpCtrl.Len++;
}
TpCtrl.Process = TP_IDLE; // 接收完成,回到IDLE状态
DoCAN_Stop_Timer( );
DoCAN_N_USData_Indication(N_USDataRxBuffer.N_TAtype, N_USDataRxBuffer.MsgData,
N_USDataRxBuffer.Length, N_OK);
}
}
/******************************************************************************
函数名:DoCAN_Receive_Flow_Control
功 能:接收流控帧数据
参 数:无
返回值:无
******************************************************************************/
void DoCAN_Receive_Flow_Control(void)
{
/*--------------------------------------------------------------------------
Tester sends a segmented request interrupted by a Flow control after the ECU
Flow control. After that the Tester sends the remaining consecutive frames.
ECU should send a diagnostic response for the request.
-----------------------------------------------------------------------------
Tester sends a single Flow Control. The ECU must not send a response.
---------------------------------------------------------------------------*/
if ( (TpCtrl.Process != TP_TX_RTS) && (TpCtrl.Process != TP_TX_CTS) && (TpCtrl.Process != TP_TX_WAIT) )
return;
/*---------------------------------------------------------------------------
Tester sends a request with a response that is longer than one frame. After
the First frame is received the Tester sends a functional addressed Flow
control. ECU must abort sending of the response.
---------------------------------------------------------------------------*/
if ( TransportRxData.N_PDU_FC.N_TAtype != DIAG_ID_Rx_PHY )
{
DoCAN_N_USData_Confirm(N_USDataTxBuffer.N_TAtype, N_ERROR);
DoCAN_Stop_Timer( ); // 停止计时
TpCtrl.Process = TP_IDLE; // 中止数据发送,回到空闲状态
return;
}
/*---------------------------------------------------------------------------
Tester sends a Flow control with a too short CAN-DLC for a response that is
longer than one frame. ECU must not send Consecutive frames. After that the
tester sends a new request. ECU must send a response for the last request.
---------------------------------------------------------------------------*/
if ( TransportRxData.N_PDU_FC.DLC != 8 )
{
DoCAN_N_USData_Confirm(N_USDataTxBuffer.N_TAtype, N_ERROR);
DoCAN_Stop_Timer( ); // 停止计时
TpCtrl.Process = TP_IDLE; // 中止数据发送,回到空闲状态
return;
}
switch ( TransportRxData.N_PDU_FC.N_PCI.FS )
{
case FC_FS_CTS:
if ( TpCtrl.NonStopMode )
break;
/*-------------------------------------------------------
Tester sends a Flow control with Blocksize 0 for a
response that is longer than one frame. ECU must send the
complete response.
---------------------------------------------------------
Tester sends a Flow control with Blocksize 0 for a
response that is longer than one frame. ECU must send the
complete response without waiting for another Flow
control. Tester verifies that every Consecutive frame is
received within TimeoutCr.
-------------------------------------------------------*/
if ( TransportRxData.N_PDU_FC.N_PCI.BS == 0 )
TpCtrl.NonStopMode = 1;
/*-------------------------------------------------------
Tester sends a Flow control with a special Blocksize for
a response that is longer than one frame. ECU must send
the number of Consecutive frames that matches the
blocksize. Blocksize 1, 8 and 20 is checked if the
response is long enough.
-------------------------------------------------------*/
else
TpCtrl.BSMax = TransportRxData.N_PDU_FC.N_PCI.BS;
/*-------------------------------------------------------
Tester sends a Flow control for a response that is longer
than one frame. ECU must send the complete response.
Tester verifies that the time between the Consecutive
Frames is not below STMin time. This is tested for STMin
values 1,10,20,30,40,50 and 60.
-------------------------------------------------------*/
if ( TpCtrl.Process != TP_TX_CTS )
{
if(TransportRxData.N_PDU_FC.N_PCI.STmin == 0)
{
TpCtrl.STmin = 1;
}
else
{
TpCtrl.STmin = TransportRxData.N_PDU_FC.N_PCI.STmin;
}
}
/*-------------------------------------------------------
Request a service where the response requires more than
one response frame. Tester sends two Flow Controls (FC)
instead of one. A response from the ECU is expected.
-------------------------------------------------------*/
TpCtrl.Process = TP_TX_CTS; // 发送机被许可发送
DoCAN_Stop_Timer( );
DoCAN_Start_STmin_Timer(TpCtrl.STmin); // 启动STmin计时
break;
case FC_FS_WAIT:
if ( TpCtrl.NonStopMode )
break;
/*-------------------------------------------------------
Tester sends a Flow control with Status value wait (WT)
for a response that is longer than one frame. ECU must
not send Consecutive frames. After the N_Bs Timeout the
Tester sends another Flow control with status continue to
send (CTS). Then the tester sends a new request. ECU must
send a response for the last request.
-------------------------------------------------------*/
TpCtrl.Process = TP_TX_WAIT; // 接收机请求发送机等待
DoCAN_Start_Timer(TIMING_PARA_N_Bs); // 启动/重启N_Bs计时
break;
case FC_FS_OVFL:
if ( TpCtrl.NonStopMode )
break;
/*-------------------------------------------------------
Tester sends a Flow control with status overflow for a
response that is longer than one frame. ECU must not send
Consecutive frame(s).
---------------------------------------------------------
Tester sends a request with a response that is longer
than one frame. After sending the Flow control the Tester
receives the first Consecutive frame and sends another
Flow control with status overflow (OVFLW). ECU must send
a diagnostic response for the first request. ECU must not
send a response for the Flow control.
-------------------------------------------------------*/
if ( TpCtrl.Process == TP_TX_RTS )
{
DoCAN_N_USData_Confirm(N_USDataTxBuffer.N_TAtype, N_BUFFER_OVFLW);
DoCAN_Stop_Timer( ); // 停止计时
TpCtrl.Process = TP_IDLE; // 中止数据发送,回到空闲状态
}
break;
default: /*-------------------------------------------------------
Tester sends a Flow control with an invalid Status value
(3-15) for a response that is longer than one frame. ECU
must not send a response.
-------------------------------------------------------*/
DoCAN_N_USData_Confirm(N_USDataTxBuffer.N_TAtype, N_INVALID_FS);
DoCAN_Stop_Timer( ); // 停止计时
TpCtrl.Process = TP_IDLE; // 中止数据发送,回到空闲状态
break;
}
}
/******************************************************************************
函数名:DoCAN_Transmit_Flow_Control
功 能:根据当前的接收机状态发送流控帧
参 数:无
返回值:无
******************************************************************************/
void DoCAN_Transmit_Flow_Control(void)
{
if ( TpCtrl.Process == TP_RX_INIT )
{
/*-------------------------------------------------------------------------
(8.5.3.3) If the network layer receives an FF with an FF_DL that is greater
than the available receiver buffer size, then this shall be considered as
an error condition. The network layer shall abort the message reception and
send an FC N_PDU with the parameter FlowStatus = Overflow.
-------------------------------------------------------------------------*/
if ( N_USDataRxBuffer.Length > N_USDATA_RX_BUFFER_SIZE )
{
TransportTxData.N_PDU_FC.N_PCI.FS = FC_FS_OVFL;
TpCtrl.Process = TP_RX_OVFL;
}
else
{
TransportTxData.N_PDU_FC.N_PCI.FS = FC_FS_CTS;
TpCtrl.Process = TP_RX_RTS;
}
}
else if ( TpCtrl.Process == TP_RX_CTS )
{
if ( TpCtrl.BSMax ) // 仍然没有接收满一个BS的N_PDU
return;
if ( TpCtrl.NonStopMode ) // 如果是连续接收模式(BS本来就是0)
return;
/*
if (0) //如果满足等待条件,则发送等待FC帧
{
if(TpCtrl.WFTCnt == 0)
{
TpCtrl.Process = TP_IDLE;
DoCAN_Stop_Timer();
DoCAN_N_USData_Indication(N_USDataRxBuffer.N_TAtype, N_USDataRxBuffer.MsgData, \
N_USDataRxBuffer.Length, N_WFT_OVRN);
return;
}
TpCtrl.WFTCnt--;
TransportTxData.N_PDU_FC.N_PCI.FS = FC_FS_WAIT;
}*/
TpCtrl.BSMax = N_MAX_BS; // 重新装入最大的BS数
TransportTxData.N_PDU_FC.N_PCI.FS = FC_FS_CTS;
TpCtrl.Process = TP_RX_RTS; // 等待RTS发送完成
}
else
return;
TransportTxData.N_PDU_FC.N_TAtype = DIAG_ID_Tx;
TransportTxData.N_PDU_FC.N_PCI.Type = FLOW_CONTROL;
TransportTxData.N_PDU_FC.N_PCI.BS = 0;
/*---------------------------------------------------------------------------
Tester sends a segmented request to check for a valid STMin time in the ECU
Flow control. The STMin time value must be within 0x01-0x7F or 0xF1-0xF9.
---------------------------------------------------------------------------*/
TransportTxData.N_PDU_FC.N_PCI.STmin = N_STmin;
TransportTxData.N_PDU_FC.DLC = 3;
DoCAN_Start_Timer(TIMING_PARA_N_Ar); // 启动N_Ar计时
DoCAN_L_Data_Request(TransportTxData.Frame.Identifier, TransportTxData.Frame.DLC, ( uint8_t * )TransportTxData.Frame.Data);
DoCAN_L_Data_Confirm(TransportTxData.Frame.Identifier, COMPLETE);
}
/******************************************************************************
函数名:DoCAN_Handle_Unknown_N_PDU
功 能:处理接收到的未知类型的N_PDU
参 数:无
返回值:无
******************************************************************************/
void DoCAN_Handle_Unknown_N_PDU(void)
{
}
/******************************************************************************
函数名:DoCAN_Disassemble_And_Transmit_N_USData
功 能:将数据拆分为连续帧(CF)并发送出去
参 数:无
返回值:无
******************************************************************************/
void DoCAN_Disassemble_And_Transmit_N_USData(void)
{
uint8_t i;
uint8_t Len;
if ( TpCtrl.Process != TP_TX_CTS ) // 未被许可时不可以发送连续帧
return;
// 仅在STmin计时时间到后发送一次
if ( DoCAN_Get_STmin_Timer_Status( ) == STmin_TIME_UP )
{
TransportTxData.N_PDU_CF.N_TAtype = N_USDataTxBuffer.N_TAtype;
TransportTxData.N_PDU_CF.N_PCI.Type = CONSECUTIVE_FRAME;
TransportTxData.N_PDU_CF.N_PCI.SN = ( uint8_t )(TpCtrl.BlockCnt & 0x000F);
// 当BSMax减小到0时,链路层数据发送完毕后,将不会再启动STmin计数器(详情见DoCAN_L_Data_Confirm函数)
// 因此STmin_TIME_UP状态将不会再出现,从而停止了连续帧的发送
// 直到有新的CF帧到来给BSMax赋予新值,或N_Bs计时超时
TpCtrl.BSMax--;
TpCtrl.BlockCnt++;
if ( TpCtrl.BlockCnt < TpCtrl.BlockSize ) // 将要发送的不是最后一帧
{
TransportTxData.N_PDU_CF.DLC = 8;
for ( i = 0; i < 7; i++ )
{
TransportTxData.N_PDU_CF.N_Data [ i ] = N_USDataTxBuffer.MsgData [ TpCtrl.Len ];
TpCtrl.Len++;
}
}
else if ( TpCtrl.BlockCnt == TpCtrl.BlockSize ) // 将要发送最后一帧
{
Len = ( uint8_t )(TpCtrl.TotalLen - TpCtrl.Len);
TransportTxData.N_PDU_CF.DLC = Len + 1;
for ( i = 0; i < Len; i++ )
{
TransportTxData.N_PDU_CF.N_Data [ i ] = N_USDataTxBuffer.MsgData [ TpCtrl.Len ];
TpCtrl.Len++;
}
}
else // 已发送帧数不可能大于帧总数,除非有错误发生
{
DoCAN_Stop_Timer( ); // 停止计时
TpCtrl.Process = TP_IDLE; // 中止数据发送,回到空闲状态
DoCAN_N_USData_Confirm(N_USDataTxBuffer.N_TAtype, N_ERROR);
return;
}
DoCAN_Start_Timer(TIMING_PARA_N_As); // 启动N_As计时
DoCAN_L_Data_Request(TransportTxData.Frame.Identifier, TransportTxData.Frame.DLC, ( uint8_t * )TransportTxData.Frame.Data);
DoCAN_L_Data_Confirm(TransportTxData.Frame.Identifier, COMPLETE);
}
}
/******************************************************************************
传输层时序管理
******************************************************************************/
/******************************************************************************
函数名:DoCAN_Update_Timer
功 能:定时器更新函数
参 数:无
返回值:无
******************************************************************************/
void DoCAN_Update_Timer(void)
{
uint16_t CurrentCnt;
uint16_t Dec;
CurrentCnt = TpTimingCtrl.Cnt;
if ( CurrentCnt != TpTimingCtrl.LastCnt ) // 定时器计数值有更新
{
if ( CurrentCnt > TpTimingCtrl.LastCnt )
Dec = CurrentCnt - TpTimingCtrl.LastCnt;
else
Dec = 65535 - TpTimingCtrl.LastCnt + 1 + CurrentCnt;
TpTimingCtrl.LastCnt = CurrentCnt;
if ( TpTimingCtrl.Type != TIMING_PARA_NONE )
{
if ( TpTimingCtrl.NTimer )
{
if ( TpTimingCtrl.NTimer > ( uint32_t )Dec )
TpTimingCtrl.NTimer -= Dec;
else
TpTimingCtrl.NTimer = 0;
}
}
if ( TpTimingCtrl.STminStatus == STmin_TIMING )
{
if ( TpTimingCtrl.STimer )
{
if ( TpTimingCtrl.STimer > ( uint32_t )Dec )
TpTimingCtrl.STimer -= Dec;
else
TpTimingCtrl.STimer = 0;
}
if ( TpTimingCtrl.STimer == 0 )
TpTimingCtrl.STminStatus = STmin_TIME_UP;
}
}
}
/******************************************************************************
函数名:DoCAN_Handle_Time_Out
功 能:超时处理
参 数:无
返回值:无
******************************************************************************/
void DoCAN_Handle_Time_Out(void)
{
if ( TpTimingCtrl.NTimer == 0 ) // 计时超时
{
switch ( TpTimingCtrl.Type )
{
case TIMING_PARA_N_As:
if ( TpCtrl.Process != TP_IDLE )
{
TpCtrl.Process = TP_IDLE; // 中止数据收发,回到空闲状态
DoCAN_N_USData_Confirm(N_USDataTxBuffer.N_TAtype, N_TIMEOUT_A);
}
break;
case TIMING_PARA_N_Ar:
if ( TpCtrl.Process != TP_IDLE )
{
TpCtrl.Process = TP_IDLE; // 中止数据收发,回到空闲状态
DoCAN_N_USData_Indication(N_USDataRxBuffer.N_TAtype, N_USDataRxBuffer.MsgData,
N_USDataRxBuffer.Length, N_TIMEOUT_A);
}
break;
case TIMING_PARA_N_Bs:
if ( TpCtrl.Process != TP_IDLE )
{
TpCtrl.Process = TP_IDLE; // 中止数据发送,回到空闲状态
DoCAN_N_USData_Confirm(N_USDataTxBuffer.N_TAtype, N_TIMEOUT_Bs);
}
break;
case TIMING_PARA_N_Br:
if ( TpCtrl.Process != TP_IDLE )
{
TpCtrl.Process = TP_IDLE; // 中止数据收发,回到空闲状态
DoCAN_N_USData_Indication(N_USDataRxBuffer.N_TAtype, N_USDataRxBuffer.MsgData,
N_USDataRxBuffer.Length, N_ERROR);
}
break;
case TIMING_PARA_N_Cs:
if ( TpCtrl.Process != TP_IDLE )
{
TpCtrl.Process = TP_IDLE; // 中止数据收发,回到空闲状态
DoCAN_N_USData_Confirm(N_USDataTxBuffer.N_TAtype, N_ERROR);
}
break;
case TIMING_PARA_N_Cr:
if ( TpCtrl.Process != TP_IDLE )
{
TpCtrl.Process = TP_IDLE; // 中止数据接收,回到空闲状态
DoCAN_N_USData_Indication(N_USDataRxBuffer.N_TAtype, N_USDataRxBuffer.MsgData,
N_USDataRxBuffer.Length, N_TIMEOUT_Cr);
}
break;
default:
break;
}
DoCAN_Stop_Timer( ); // 停止计时
}
}
/******************************************************************************
函数名:DoCAN_Start_Timer
功 能:启动时序定时器
参 数:TimingParameter :时序参数,可以是下列参数中的一种
TIMING_PARA_N_As
TIMING_PARA_N_Ar
TIMING_PARA_N_Bs
TIMING_PARA_N_Br
TIMING_PARA_N_Cs
TIMING_PARA_N_Cr
返回值:无
******************************************************************************/
void DoCAN_Start_Timer(uint8_t TimingParameter)
{
switch ( TimingParameter )
{
case TIMING_PARA_N_As:
TpTimingCtrl.Type = TIMING_PARA_N_As;
TpTimingCtrl.NTimer = N_As * 1000;
break;
case TIMING_PARA_N_Ar:
TpTimingCtrl.Type = TIMING_PARA_N_Ar;
TpTimingCtrl.NTimer = N_Ar * 1000;
break;
case TIMING_PARA_N_Bs:
TpTimingCtrl.Type = TIMING_PARA_N_Bs;
TpTimingCtrl.NTimer = N_Bs * 1000;
break;
case TIMING_PARA_N_Br:
TpTimingCtrl.Type = TIMING_PARA_N_Br;
TpTimingCtrl.NTimer = N_Br * 1000;
break;
case TIMING_PARA_N_Cs:
TpTimingCtrl.Type = TIMING_PARA_N_Cs;
TpTimingCtrl.NTimer = N_Cs * 1000;
break;
case TIMING_PARA_N_Cr:
TpTimingCtrl.Type = TIMING_PARA_N_Cr;
TpTimingCtrl.NTimer = N_Cr * 1000;
break;
default:
TpTimingCtrl.Type = TIMING_PARA_NONE;
TpTimingCtrl.NTimer = 0;
break;
}
}
/******************************************************************************
函数名:DoCAN_Stop_Timer
功 能:停止时序定时器
参 数:无
返回值:无
******************************************************************************/
void DoCAN_Stop_Timer(void)
{
TpTimingCtrl.Type = TIMING_PARA_NONE;
}
/******************************************************************************
函数名:DoCAN_Start_STmin_Timer
功 能:启动STmin定时器
参 数:STminTime:STmin时间
返回值:无
******************************************************************************/
void DoCAN_Start_STmin_Timer(uint8_t STminTime)
{
if ( STminTime <= 0x7F ) // 0x00 - 0x7F:0ms - 127ms
TpTimingCtrl.STimer = ( uint32_t )STminTime * 1000;
else if ( (STminTime >= 0xF1) && (STminTime <= 0xF9) ) // 0xF1 - 0xF9:100us - 900us
TpTimingCtrl.STimer = ( uint32_t )(STminTime & 0x0F) * 100;
else // Reserved :127ms
TpTimingCtrl.STimer = 127000;
TpTimingCtrl.STminStatus = STmin_TIMING; // STmin开始计时
}
/******************************************************************************
函数名:DoCAN_Get_STmin_Timer_Status
功 能:获取当前STmin定时器的状态
参 数:无
返回值:STmin_TIMER_IDLE:STmin定时器空闲
STmin_TIME_UP :STmin定时时间到
STmin_TIMING :STmin定时器计时中
******************************************************************************/
uint8_t DoCAN_Get_STmin_Timer_Status(void)
{
uint8_t Status;
Status = TpTimingCtrl.STminStatus;
if ( Status == STmin_TIME_UP )
TpTimingCtrl.STminStatus = STmin_TIMER_IDLE;
return Status;
}
/******************************************************************************
文 件 名:DoCAN_ISO15765.h
功能描述:ISO 15765 规范规定的诊断服务函数头文件
作 者:张暄
版 本:V1.0
日 期:2016.7.18
******************************************************************************/
/******************************************************************************
诊断服务的OSI模型映射
===============================================================================
* NO. OSI Layer Diagnostics services
-------------------------------------------------------------------------------
7 Application ISO 14229-1 ISO 14229-3
6 Presentation -
5 Session ISO 14229-2
* 4 Transport ISO 15765-2
* 3 Network ISO 15765-2
2 Data Link ISO 11898
1 Physical ISO 11898
===============================================================================
******************************************************************************/
#ifndef _DOCAN_ISO15765_H_
#define _DOCAN_ISO15765_H_
#include "stdint.h"
#include "DoCAN_ISO15765_Config.h"
/*-----------------------------------------------------------------------------
Transfer_Status
-----------------------------------------------------------------------------*/
#ifndef COMPLETE
#define COMPLETE 0x00
#endif
#ifndef NOT_COMPLETE
#define NOT_COMPLETE (! COMPLETE)
#endif
/*-----------------------------------------------------------------------------
N_PDU 名称
-----------------------------------------------------------------------------*/
#define SINGLE_FRAME 0x00
#define FIRST_FRAME 0x01
#define CONSECUTIVE_FRAME 0x02
#define FLOW_CONTROL 0x03
/*-----------------------------------------------------------------------------
流控帧类型
-----------------------------------------------------------------------------*/
#define FC_FS_CTS 0x00
#define FC_FS_WAIT 0x01
#define FC_FS_OVFL 0x02
/*-----------------------------------------------------------------------------
时序参数名称
-----------------------------------------------------------------------------*/
#define TIMING_PARA_NONE 0X00
#define TIMING_PARA_N_As 0X01
#define TIMING_PARA_N_Ar 0X02
#define TIMING_PARA_N_Bs 0X03
#define TIMING_PARA_N_Br 0X04
#define TIMING_PARA_N_Cs 0X05
#define TIMING_PARA_N_Cr 0X06
/*-----------------------------------------------------------------------------
STmin计时状态
-----------------------------------------------------------------------------*/
#define STmin_TIMER_IDLE 0X00 // STmin定时器空闲
#define STmin_TIME_UP 0X01 // STmin定时时间到
#define STmin_TIMING 0X02 // STmin定时器计时中
/*-----------------------------------------------------------------------------
传输层收发控制
-----------------------------------------------------------------------------*/
#define TP_DIR_MASK 0x80 // 收发标志位
#define TP_RX 0x00 // 传输层接收状态
#define TP_TX 0x80 // 传输层发送状态
#define TP_IDLE 0x00 // Rx:空闲状态
// Tx:空闲状态
#define TP_ERR 0x01 // Rx:接收系统错误
// Tx:发送系统错误
#define TP_RX_INIT 0x10 // Rx:已接收到首帧,初始化多帧接收
#define TP_TX_INIT 0x90 // Tx:发送初始化完成,正在发送首帧或单帧(注意单帧也使用此标志)
#define TP_RX_CTS 0x11 // Rx:接收机允许发送机发送连续帧(Clear To Send)
#define TP_TX_CTS 0x91 // Tx:发送机被许可发送连续帧(Clear To Send)
#define TP_RX_RTS 0x20 // Rx:请求继续发送多帧(Request To Send)
#define TP_TX_RTS 0xA0 // Tx:等待接收机许可多帧发送(Request To Send)
#define TP_RX_WAIT 0x21 // Rx:请求发送机暂停发送
#define TP_TX_WAIT 0xA1 // Tx:接收机请求等待
#define TP_RX_OVFL 0x22 // Rx:接收机接收溢出
#define TP_TX_OVFL 0xA2 // Tx:接收机接收溢出
/******************************************************************************
结构体声明
******************************************************************************/
/*** 链路层数据帧结构 ***/
typedef struct
{
uint16_t Identifier; // 帧ID
uint8_t Data [ 8 ]; // 帧数据
uint8_t DLC; // 帧长度
} LinkDataStruct;
/*** 链路层接收FIFO结构 ***/
typedef struct
{
LinkDataStruct LinkData [ LINK_RX_FIFO_MAX_DEPTH ];
uint8_t Depth; // FIFO深度
uint8_t IPtr; // 输入指针
uint8_t OPtr; // 输出指针
} LinkRxFIFOStruct;
/*** 链路层发送控制结构 ***/
typedef struct
{
uint16_t Identifier; // 帧ID
uint8_t Data [ 8 ]; // 帧数据
uint8_t Busy; // 发送器忙标志
} LinkTxCtrlStruct;
/*---------------------------------------------------------------------------*/
/*** 传输层数据帧结构 ***/
typedef struct
{
uint16_t Identifier;
uint8_t Data [ 8 ];
uint8_t DLC;
uint8_t New;
} TP_Data_Struct;
/*** 通用N_PCI解析结构 ***/
typedef struct
{
uint8_t Rsvd : 4;
uint8_t Type : 4;
} TP_N_PCI_Struct;
/*** 通用N_PDU解析结构 ***/
typedef struct
{
uint16_t N_TAtype;
TP_N_PCI_Struct N_PCI;
uint8_t N_Data [ 7 ];
uint8_t DLC;
uint8_t New;
} TP_N_PDU_Struct;
/*** 单帧N_PCI解析结构 ***/
typedef struct
{
uint8_t SF_DL : 4;
uint8_t Type : 4;
} TP_SF_N_PCI_Struct;
/*** 单帧N_PDU解析结构 ***/
typedef struct
{
uint16_t N_TAtype;
TP_SF_N_PCI_Struct N_PCI;
uint8_t N_Data [ 7 ];
uint8_t DLC;
uint8_t New;
} TP_N_PDU_SF_Struct;
/*** 首帧N_PCI解析结构 ***/
typedef struct
{
uint8_t FF_DL_H : 4;
uint8_t Type : 4;
uint16_t FF_DL_L : 8;
} TP_FF_N_PCI_Struct;
/*** 首帧N_PDU解析结构 ***/
typedef struct
{
uint16_t N_TAtype;
TP_FF_N_PCI_Struct N_PCI;
uint8_t N_Data [ 6 ];
uint8_t DLC;
uint8_t New;
} TP_N_PDU_FF_Struct;
/*** 连续帧N_PCI解析结构 ***/
typedef struct
{
uint8_t SN : 4;
uint8_t Type : 4;
} TP_CF_N_PCI_Struct;
/*** 连续帧N_PDU解析结构 ***/
typedef struct
{
uint16_t N_TAtype;
TP_CF_N_PCI_Struct N_PCI;
uint8_t N_Data [ 7 ];
uint8_t DLC;
uint8_t New;
} TP_N_PDU_CF_Struct;
/*** 流控帧N_PCI解析结构 ***/
typedef struct
{
uint8_t FS : 4;
uint8_t Type : 4;
uint8_t BS : 8;
uint8_t STmin : 8;
} TP_FC_N_PCI_Struct;
/*** 流控帧N_PDU解析结构 ***/
typedef struct
{
uint16_t N_TAtype;
TP_FC_N_PCI_Struct N_PCI;
uint8_t N_Data [ 5 ];
uint8_t DLC;
uint8_t New;
} TP_N_PDU_FC_Struct;
/*** 传输层数据解析联合体 ***/
typedef union
{
TP_Data_Struct Frame;
TP_N_PDU_Struct N_PDU;
TP_N_PDU_SF_Struct N_PDU_SF;
TP_N_PDU_FF_Struct N_PDU_FF;
TP_N_PDU_CF_Struct N_PDU_CF;
TP_N_PDU_FC_Struct N_PDU_FC;
} TransportDataUnion;
/*** 传输层收发控制结构 ***/
typedef struct
{
uint8_t Process; // 当前收发进程
uint8_t NonStopMode; // 不间断收发模式(不再理会后续的FC帧)
uint16_t Len; // 已接收到或已发送出的数据长度
uint16_t TotalLen; // 数据总长
uint16_t BlockSize; // Block总数
uint16_t BlockCnt; // 已接收到或已发送出的Block数
uint8_t BSMax; // 当前接收机剩余的最大Block数
uint8_t WFTCnt; //[仅接收用]等待次数计数
uint8_t STmin; //[仅发送用]当前接收机要求的最小间隔时间
} TransportControlStruct;
/*** 时序控制结构 ***/
typedef struct
{
uint16_t Cnt; // 滚动计数器
uint16_t LastCnt; // 上次更新时的滚动计数器
uint32_t NTimer; // 时序定时器,单位:us
uint32_t STimer; // 最小间隔时间(STmin)定时器,单位:us
uint8_t Type; // 当前定时器计时参数名称
uint8_t STminStatus; // STmin计时状态
} TransportTimingControlStruct;
/*---------------------------------------------------------------------------*/
/*** 数据收发结果N_Result枚举 ***/
typedef enum
{
N_OK = 0,
N_TIMEOUT_A,
N_TIMEOUT_Bs,
N_TIMEOUT_Cr,
N_WRONG_SN,
N_INVALID_FS,
N_UNEXP_PDU,
N_WFT_OVRN,
N_BUFFER_OVFLW,
N_ERROR,
} N_ResultEnum;
/*** 接收用N_USData数据结构 ***/
typedef struct
{
uint16_t N_TAtype;
uint16_t Length;
uint8_t MsgData [ N_USDATA_RX_BUFFER_SIZE ];
} N_USDataRxStruct;
/*** 发送用N_USData数据结构 ***/
typedef struct
{
uint16_t N_TAtype;
uint16_t Length;
uint8_t MsgData [ N_USDATA_TX_BUFFER_SIZE ];
} N_USDataTxStruct;
/******************************************************************************
函数声明
******************************************************************************/
/*-- 外部接口函数 -----------------------------------------------------------*/
/*** 后台服务函数 ***/
extern void DoCAN_Communication_Service(void);
extern void DoCAN_Timer_Update(uint16_t Interval);
/*** 网络层上层接口函数 ***/
void DoCAN_N_USData_Request(uint16_t N_TAtype, uint8_t *MessageData, uint16_t Length);
void DoCAN_N_USData_Confirm(uint16_t N_TAtype, N_ResultEnum N_Result);
void DoCAN_N_USData_FF_Indication(uint16_t N_TAtype, uint16_t Length);
void DoCAN_N_USData_Indication(uint16_t N_TAtype, uint8_t *MessageData, uint16_t Length, N_ResultEnum N_Result);
/*** 链路层下层接口函数 ***/
void DoCAN_L_Data_Request(uint16_t Identifier, uint8_t dlc, uint8_t *Data);
void DoCAN_L_Data_Confirm(uint16_t Identifier, uint8_t TransferStatus);
extern void DoCAN_L_Data_Indication(uint16_t Identifier, uint32_t dlc, uint8_t *pData);
extern void CAN_LLC_Ch5_Data_Request(uint32_t Identifier, uint16_t idx, uint8_t DLC, uint8_t *Data);
/*-- 内部函数 ---------------------------------------------------------------*/
/*** 传输层协议解析 ***/
void DoCAN_Receive_And_Assemble_N_USData(void); // 接收主函数
void DoCAN_Get_N_PDU(void);
void DoCAN_Receive_Single_Frame_N_Data(void);
void DoCAN_Receive_First_Frame_N_Data(void);
void DoCAN_Receive_Consecutive_Frame_N_Data(void);
void DoCAN_Receive_Flow_Control(void);
void DoCAN_Transmit_Flow_Control(void);
void DoCAN_Handle_Unknown_N_PDU(void);
void DoCAN_Disassemble_And_Transmit_N_USData(void); // 发送主函数
/*** 时序管理 ***/
void DoCAN_Update_Timer(void);
void DoCAN_Handle_Time_Out(void);
void DoCAN_Start_Timer(uint8_t TimingParameter);
void DoCAN_Stop_Timer(void);
void DoCAN_Start_STmin_Timer(uint8_t STminTime);
uint8_t DoCAN_Get_STmin_Timer_Status(void);
#endif
/******************************************************************************
文 件 名:DoCAN_ISO15765_Config.h
功能描述:ISO 15765 规范规定的诊断服务配置文件
作 者:张暄
版 本:V1.0
日 期:2016.7.18
******************************************************************************/
/******************************************************************************
Diagnostic/Programming Specifications Applicable to the OSI Layers
===============================================================================
* NO. OSI Layer Diagnostics services
-------------------------------------------------------------------------------
7 Application ISO 14229-1 ISO 14229-3
6 Presentation -
5 Session ISO 14229-2
* 4 Transport ISO 15765-2
* 3 Network ISO 15765-2
2 Data Link ISO 11898
1 Physical ISO 11898
===============================================================================
******************************************************************************/
#ifndef _DOCAN_ISO15765_CONFIG_H_
#define _DOCAN_ISO15765_CONFIG_H_
#include "Diag_ID_Def.h"
/*-----------------------------------------------------------------------------
If CAN frame data padding is used, the DLC is always set to 8, even if the
N_PDU to be transmitted is shorter than 8 bytes. The sender has to pad any
unused bytes in the frame. In particular, this can be the case for an SF, FC
frame or the last CF of a segmented message.
-----------------------------------------------------------------------------*/
#define FILLER_BYTE_HANDLING 1
#define FILLER_BYTE 0xAA
/*-----------------------------------------------------------------------------
Link layer Rx FIFO depth
-----------------------------------------------------------------------------*/
#define LINK_RX_FIFO_MAX_DEPTH 2
/*-----------------------------------------------------------------------------
Network layer buffer size
-----------------------------------------------------------------------------*/
#define N_USDATA_RX_BUFFER_SIZE 96
#define N_USDATA_TX_BUFFER_SIZE 96
/******************************************************************************
The FlowControl mechanism allows the receiver to inform the sender about the
receiver’s capabilities. Since different nodes may have different capabilities,
the FlowControl sent by the receiver informs the sender about its capabilities.
The sender shall conform to the receiver’s capabilities.
These capabilities are defined as follows.
******************************************************************************/
/*-----------------------------------------------------------------------------
— BlockSize (BS): The maximum number of N_PDUs the receiver allows the sender
to send, before waiting for an authorization to continue transmission of the
following N_PDUs.
-----------------------------------------------------------------------------*/
#define N_MAX_BS 8
/*-----------------------------------------------------------------------------
— SeparationTime minimum (STmin): The minimum time the sender is to wait
between transmission of two CF N_PDUs. (Unit: ms)
-----------------------------------------------------------------------------*/
#define N_STmin 20
/*-----------------------------------------------------------------------------
— N_WFTmax: Upper limit to the number of FC.WAIT a receiver is allowed to send
in a row. This parameter is a system design constant and is not
transmitted in the first FC N_PDU.
-----------------------------------------------------------------------------*/
#define N_WFTmax 0
/******************************************************************************
Timing parameters
******************************************************************************/
/*-----------------------------------------------------------------------------
- Timing Parametera : N_As
- Description : Time for transmission of the CAN frame (any N_PDU)
on the sender side
- Start : L_Data.request
- End : L_Data.confirm
- Timeout(Max) : 1,000 ms
- Performance Requirement :
-----------------------------------------------------------------------------*/
#define N_As (( uint32_t )25)
/*-----------------------------------------------------------------------------
- Timing Parametera : N_Ar
- Description : Time for transmission of the CAN frame (any N_PDU)
on the receiver side
- Start : L_Data.request
- End : L_Data.confirm
- Timeout(Max) : 1,000 ms
- Performance Requirement :
-----------------------------------------------------------------------------*/
#define N_Ar (( uint32_t )25)
/*-----------------------------------------------------------------------------
- Timing Parametera : N_Bs
- Description : Time until reception of the next FlowControl N_PDU
- Start : L_Data.confirm (FF) / L_Data.confirm (CF) /
L_Data.indication (FC)
- End : L_Data.indication (FC)
- Timeout(Max) : 1,000 ms
- Performance Requirement :
-----------------------------------------------------------------------------*/
#define N_Bs (( uint32_t )75)
/*-----------------------------------------------------------------------------
- Timing Parametera : N_Br
- Description : Time until transmission of the next FlowControl
N_PDU
- Start : L_Data.indication (FF) / L_Data.indication (CF)
L_Data.confirm (FC)
- End : L_Data.request (FC)
- Timeout(Max) : N/A
- Performance Requirement : (N_Br + N_Ar) < (0.9 × N_Bs timeout)
-----------------------------------------------------------------------------*/
#define N_Br (( uint32_t )9)
/*-----------------------------------------------------------------------------
- Timing Parametera : N_Cs
- Description : Time until transmission of the next
ConsecutiveFrame N_PDU
- Start : L_Data.indication (FC) / L_Data.confirm (CF)
- End : L_Data.request (CF)
- Timeout(Max) : N/A
- Performance Requirement : (N_Cs + N_As) < (0.9 × N_Cr timeout)
-----------------------------------------------------------------------------*/
#define N_Cs (( uint32_t )0)
/*-----------------------------------------------------------------------------
- Timing Parametera : N_Cr
- Description : Time until reception of the next Consecutive Frame
N_PDU
- Start : L_Data.confirm (FC) / L_Data.indication (CF)
- End : L_Data.indication (CF)
- Timeout(Max) : 1,000 ms
- Performance Requirement :
-----------------------------------------------------------------------------*/
#define N_Cr (( uint32_t )150)
/******************************************************************************
For debug purpose
******************************************************************************/
/*-----------------------------------------------------------------------------
Global debug switch
-----------------------------------------------------------------------------*/
#define DOCAN_DEBUG_EN 0
/*-----------------------------------------------------------------------------
Bug indicator functions
-----------------------------------------------------------------------------*/
#if DOCAN_DEBUG_EN
#define LINK_RX_FIFO_OVERFLOW_INDICATOR( )
#endif
#endif
/******************************************************************************
文 件 名:UDS_ISO14229_Server.c
功能描述:ISO 14229 规范规定的诊断服务服务器端函数库文件
作 者:张暄
版 本:V1.0
日 期:2016.11.1
******************************************************************************/
#include "UDS_ISO14229_Server.h"
UDS_APP_RX_Union UDS_APP_RX;
UDS_APP_TX_Union UDS_APP_TX;
UDS_APP_TX_NEG_Union UDS_APP_TX_NEG;
extern DiagSendDataNeg NegRes;
extern uint8_t CAN_Current_state;
/******************************************************************************
后台服务
******************************************************************************/
/******************************************************************************
函数名:UDS_Server_Application_Service
功 能:UDS服务器应用层控制服务
参 数:无
返回值:无
*******************************************************************************
注 意:该服务函数必须被实时调用
******************************************************************************/
void UDS_Server_Application_Service(void)
{
UDS_Process_Service_Request( );
/*
if ( IsNMTimerOut ( NM_Tdiag_Timer ) )
{
NMStopTimer ( NM_Tdiag_Timer );
CanNm_NetworkRelease ( NM_REQ_DIAG_TIMEOUT );
}
*/
}
/******************************************************************************
会话层下层接口及计时功能实现
******************************************************************************/
/******************************************************************************
函数名:UDS_N_USData_Request
功 能:该服务函数用于请求传输数据
This service is used to request the transfer of data. If necessary, the
network layer segments the data.
参 数:N_TAtype :目标地址类型,发送数据请使用 DIAG_ID_Tx
MessageData :请求传输的数据
Length :数据长度
返回值:无
******************************************************************************/
void UDS_N_USData_Request(uint16_t N_TAtype, uint8_t *MessageData, uint16_t Length)
{
DoCAN_N_USData_Request(N_TAtype, MessageData, Length);
// 计时
}
/******************************************************************************
函数名:UDS_N_USData_Confirm
功 能:该服务函数由网络层发起,用于确认前一次使用N_USData.request服务向
N_TAtype地址发送的数据是否发送完成
The N_USData.confirm service is issued by the network layer. The service
primitive confirms the completion of an N_USData.request service
identified by the address information in N_TAtype.
参 数:N_TAtype :N_USData.confirm服务请求的发送地址
N_Result :N_USData.request服务的传输状态
返回值:无
******************************************************************************/
extern void UDS_N_USData_Confirm(uint16_t N_TAtype, uint8_t N_Result)
{
if ( N_Result == N_OK )
UDS_S_Data_Confirm(N_TAtype, S_OK);
else
UDS_S_Data_Confirm(N_TAtype, S_NOK);
// 计时
}
/******************************************************************************
函数名:UDS_N_USData_FF_Indication
功 能:该服务函数由网络层发起,用于向上层指示地址为N_TAtype的一组多帧数据的首帧
的到来
The N_USData_FF.indication service is issued by the network layer. The
service primitive indicates to the adjacent upper layer the arrival of
a FirstFrame (FF) of a segmented message received from a peer protocol
entity, identified by the address information N_TAtype.
参 数:N_TAtype :新到的多帧数据首帧的地址信息
Length :新到的多帧数据的总长度
返回值:无
******************************************************************************/
extern void UDS_N_USData_FF_Indication(uint16_t N_TAtype, uint16_t Length)
{
// 计时
}
/******************************************************************************
函数名:UDS_N_USData_Indication
功 能:该服务函数由网络层发起,用于向上层指出由N_TAtype地址发送来的长度为Length
的MessageData数据的传送结果N_Result,并同时传递这一数据
The N_USData.indication service is issued by the network layer. The
service primitive indicates <N_Result> events and delivers
<MessageData> with <Length> bytes received from a peer protocol entity
identified by the address information in N_TAtype to the adjacent upper
layer.
参 数:N_TAtype :接收到的数据地址信息
MessageData :接收到的数据 (仅在N_Result为N_OK时有效)
Length :接收到的数据长度 (仅在N_Result为N_OK时有效)
N_Result :数据的接收结果
返回值:无
******************************************************************************/
extern void UDS_N_USData_Indication(uint16_t N_TAtype, uint8_t *MessageData, uint16_t Length, uint8_t N_Result)
{
if ( N_Result == N_OK )
UDS_S_Data_Indication(N_TAtype, MessageData, Length, S_OK);
else
UDS_S_Data_Indication(N_TAtype, MessageData, Length, S_NOK);
// 计时
}
/******************************************************************************
会话层上层接口
******************************************************************************/
/******************************************************************************
函数名:UDS_S_Data_Request
功 能:该服务用于请求向目标地址S_TAtype发送长度为S_Length的S_Data.
The service primitive requests transmission of S_Data with S_Length
number of bytes from the sender to the receiver peer entities
identified by the address information in S_TAtype.
参 数:S_TAtype :发送数据的目标地址
S_Data :发送的数据
S_Length :数据的长度
返回值:无
******************************************************************************/
void UDS_S_Data_Request(uint16_t S_TAtype, uint8_t *S_Data, uint16_t S_Length)
{
UDS_N_USData_Request(S_TAtype, S_Data, S_Length);
}
/******************************************************************************
函数名:UDS_S_Data_Request
功 能:该服务由会话层发起,用于指出以S_TAtype为目标地址的S_Data.request发送请求
是否完成
The S_Data.confirm service is issued by the session layer. The service
primitive confirms the completion of an S_Data.request service
identified by the address information in S_TAtype.
参 数:S_TAtype :发送数据的目标地址
S_Result :数据的发送结果
返回值:无
******************************************************************************/
void UDS_S_Data_Confirm(uint16_t S_TAtype, S_Result_Enum S_Result)
{
}
/******************************************************************************
函数名:UDS_S_Data_Request
功 能:该服务由会话层发起,用于向上层指出S_Result接收状态以及传递从S_TAtype地址
接收到的S_Length长度的S_Data数据
The S_Data.indication service is issued by the session layer. The
service primitive indicates S_Result events and delivers S_Data with
S_Length bytes received from a peer protocol entity identified by the
address information in S_TAtype to the adjacent upper layer.
参 数:S_TAtype :接收到的数据地址信息
S_Data :接收到的数据 (仅在S_Result为S_OK时有效)
S_Length :接收到的数据长度 (仅在S_Result为S_OK时有效)
S_Result :数据的接收结果
返回值:无
******************************************************************************/
void UDS_S_Data_Indication(uint16_t S_TAtype, uint8_t *S_Data, uint16_t S_Length, S_Result_Enum S_Result)
{
uint16_t i;
if ( S_Result == S_OK )
{
UDS_APP_RX.Data.New = 1;
UDS_APP_RX.Data.TA_type = S_TAtype;
UDS_APP_RX.Data.Length = S_Length - 1;
for ( i = 0; i < S_Length; i++ )
{
UDS_APP_RX.Data.A_Data [ i ] = S_Data[i];
}
}
}
/******************************************************************************
应用层服务控制
******************************************************************************/
/******************************************************************************
函数名:UDS_Process_Service_Request
功 能:该函数用于处理来自客户端的服务请求,当有新的服务请求到达时,根据SI选择相
应的服务实现函数执行服务
参 数:无
返回值:无 SYSC0_RUNPLL2CNTR
******************************************************************************/
extern uint8_t S3_Server_refresh;
void UDS_Process_Service_Request(void)
{
if (UDS_APP_RX.A_PDU.New)
{
S3_Server_refresh = 1;
switch (UDS_APP_RX.A_PDU.A_PCI.SI)
{
case 0x10: UDS_Service_10_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data);
break;
case 0x22: UDS_Service_22_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data);
break;
case 0x27: UDS_Service_27_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data);
break;
case 0x28: UDS_Service_28_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data);
break;
case 0x85: UDS_Service_85_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data);
break;
case 0x11: UDS_Service_11_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data);
break;
case 0x3E: UDS_Service_3E_Indication(UDS_APP_RX.A_PDU.TA_type, UDS_APP_RX.A_PDU.Length, UDS_APP_RX.A_PDU.Data);
break;
default:
if (UDS_APP_RX.A_PDU.TA_type != DIAG_ID_Rx_FUN)
{
NegRes.code = serviceNotSupported;
UDS_Service_Response(UDS_APP_RX.A_PDU.A_PCI.SI, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
}
break;
}
UDS_APP_RX.A_PDU.New = 0;
}
}
/******************************************************************************
函数名:UDS_Service_Response
功 能:该函数根据反馈类型生成相应的反馈数据传递给客户端,通用于各个服务的反馈
参 数:si :服务ID
RspType :反馈类型 POSITIVE_RSP 正反馈
NEGATIVE_RSP 负反馈
A_TA_type :目标地址类型
A_Length :随反馈携带的数据的长度(仅是数据A_Data的长度)
A_Data :随反馈携带的数据
返回值:无
******************************************************************************/
extern void UDS_Service_Response(uint8_t si, uint8_t RspType, uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
uint16_t i;
if ( RspType )
{
UDS_APP_TX_NEG.A_PDU.TA_type = A_TA_type;
UDS_APP_TX_NEG.A_PDU.A_PCI.NR_SI = 0x7F;
UDS_APP_TX_NEG.A_PDU.A_PCI.SI = si;
UDS_APP_TX_NEG.A_PDU.Length = A_Length;
for ( i = 0; i < UDS_APP_TX_NEG.A_PDU.Length; i++ )
{
UDS_APP_TX_NEG.A_PDU.Data [ i ] = A_Data[i];
}
UDS_S_Data_Request(UDS_APP_TX_NEG.Data.TA_type, UDS_APP_TX_NEG.Data.A_Data, UDS_APP_TX_NEG.Data.Length + 2);
}
else
{
UDS_APP_TX.A_PDU.TA_type = A_TA_type;
UDS_APP_TX.A_PDU.A_PCI.SI = si | 0x40;
UDS_APP_TX.A_PDU.Length = A_Length;
for ( i = 0; i < UDS_APP_TX.A_PDU.Length; i++ )
{
UDS_APP_TX.A_PDU.Data [ i ] = A_Data[i];
}
UDS_S_Data_Request(UDS_APP_TX.Data.TA_type, UDS_APP_TX.Data.A_Data, UDS_APP_TX.Data.Length + 1);
}
}
/******************************************************************************
文 件 名:UDS_ISO14229_Server.h
功能描述:ISO 14229 规范规定的诊断服务服务器端函数头文件
作 者:张暄
版 本:V1.0
日 期:2016.11.1
******************************************************************************/
/******************************************************************************
诊断服务的OSI模型映射
===============================================================================
* NO. OSI Layer Diagnostics services
-------------------------------------------------------------------------------
* 7 Application ISO 14229-1 ISO 14229-3
6 Presentation -
* 5 Session ISO 14229-2
4 Transport ISO 15765-2
3 Network ISO 15765-2
2 Data Link ISO 11898
1 Physical ISO 11898
===============================================================================
******************************************************************************/
#ifndef _UDS_ISO14229_SERVER_H_
#define _UDS_ISO14229_SERVER_H_
#include "DoCAN_ISO15765.h"
#include "DoCAN_ISO15765_Config.h"
#include "UDS_ISO14229_Services.h"
/******************************************************************************
结构体声明
******************************************************************************/
/*** 会话层数据发送结果枚举 ***/
typedef enum
{
S_OK = 0,
S_NOK,
} S_Result_Enum;
/*---------------------------------------------------------------------------*/
/*** A_PCI解析结构(非负反馈) ***/
typedef struct
{
uint8_t SI;
} UDS_A_PCI_Struct;
/*** A_PCI解析结构(负反馈) ***/
typedef struct
{
uint8_t NR_SI;
uint8_t SI;
} UDS_NEG_A_PCI_Struct;
/*** 应用层接收数据解析结构 ***/
typedef struct
{
uint16_t TA_type;
uint8_t A_Data [ A_DATA_RX_BUFFER_SIZE ];
uint16_t Length; // 有效数据长度(不含A_PCI)
uint8_t New; // 新收到的数据
} UDS_RX_App_Data_Struct;
/*** 应用层接收A_PDU解析结构 ***/
typedef struct
{
uint16_t TA_type;
UDS_A_PCI_Struct A_PCI;
uint8_t Data [ A_DATA_RX_BUFFER_SIZE - 1 ];
uint16_t Length; // 有效数据长度(不含A_PCI)
uint8_t New; // 新收到的数据
} UDS_RX_A_PDU_Struct;
/*** 应用层发送数据解析结构 ***/
typedef struct
{
uint16_t TA_type;
uint8_t A_Data [ A_DATA_TX_BUFFER_SIZE ];
uint16_t Length; // 有效数据长度(不含A_PCI)
} UDS_TX_App_Data_Struct;
/*** 应用层发送正反馈A_PDU解析结构 ***/
typedef struct
{
uint16_t TA_type;
UDS_A_PCI_Struct A_PCI;
uint8_t Data [ A_DATA_TX_BUFFER_SIZE - 1 ];
uint16_t Length; // 有效数据长度(不含A_PCI)
} UDS_TX_A_PDU_Struct;
/*** 应用层发送负反馈A_PDU解析结构 ***/
typedef struct
{
uint16_t TA_type;
UDS_NEG_A_PCI_Struct A_PCI;
uint8_t Data [ A_DATA_TX_BUFFER_SIZE - 2 ];
uint16_t Length; // 有效数据长度(不含A_PCI)
} UDS_TX_NEG_A_PDU_Struct;
/*** 应用层接收数据解析联合体 ***/
typedef union
{
UDS_RX_App_Data_Struct Data;
UDS_RX_A_PDU_Struct A_PDU;
} UDS_APP_RX_Union;
/*** 应用层发送正反馈数据解析联合体 ***/
typedef union
{
UDS_TX_App_Data_Struct Data;
UDS_TX_A_PDU_Struct A_PDU;
} UDS_APP_TX_Union;
/*** 应用层发送负反馈数据解析联合体 ***/
typedef union
{
UDS_TX_App_Data_Struct Data;
UDS_TX_NEG_A_PDU_Struct A_PDU;
} UDS_APP_TX_NEG_Union;
/* 全局变量 变更为局部变量
uint8_t Service10DiagDataLength;
uint8_t Service11DiagDataLength;
uint8_t Service14DiagDataLength;
uint8_t Service19DiagDataLength;
uint8_t Service22DiagDataLength;
uint8_t Service27DiagDataLength;
uint8_t Service28DiagDataLength;
uint8_t Service2EDiagDataLength;
uint8_t Service2FDiagDataLength;
uint8_t Service31DiagDataLength;
uint8_t Service34DiagDataLength;
uint8_t Service36DiagDataLength;
uint8_t Service37DiagDataLength;
uint8_t Service3EDiagDataLength;
uint8_t Service85DiagDataLength;
*/
/******************************************************************************
函数声明
******************************************************************************/
/*-- 外部接口函数 -----------------------------------------------------------*/
/*** 后台服务函数 ***/
void UDS_Server_Application_Service(void);
/*** 传输层/网络层下层接口函数 ***/
void UDS_N_USData_Request(uint16_t N_TAtype, uint8_t *MessageData, uint16_t Length);
void UDS_N_USData_Confirm(uint16_t N_TAtype, uint8_t N_Result);
void UDS_N_USData_FF_Indication(uint16_t N_TAtype, uint16_t Length);
void UDS_N_USData_Indication(uint16_t N_TAtype, uint8_t *MessageData, uint16_t Length, uint8_t N_Result);
/*-- 内部函数 ---------------------------------------------------------------*/
/*** 会话层数据接口函数 ***/
void UDS_S_Data_Request(uint16_t S_TAtype, uint8_t *S_Data, uint16_t S_Length);
void UDS_S_Data_Confirm(uint16_t S_TAtype, S_Result_Enum S_Result);
void UDS_S_Data_Indication(uint16_t S_TAtype, uint8_t *S_Data, uint16_t S_Length, S_Result_Enum S_Result);
/*** 会话层时序控制函数 ***/
/*** 应用层服务控制函数 ***/
void UDS_Process_Service_Request(void);
extern void UDS_Service_Response(uint8_t si, uint8_t RspType, uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
#endif
/******************************************************************************
文 件 名:UDS_ISO14229_Server_Config.h
功能描述:ISO 14229 规范规定的诊断服务服务器端配置文件
作 者:张暄
版 本:V1.0
日 期:2016.11.1
******************************************************************************/
/******************************************************************************
诊断服务的OSI模型映射
===============================================================================
* NO. OSI Layer Diagnostics services
-------------------------------------------------------------------------------
* 7 Application ISO 14229-1 ISO 14229-3
6 Presentation -
* 5 Session ISO 14229-2
4 Transport ISO 15765-2
3 Network ISO 15765-2
2 Data Link ISO 11898
1 Physical ISO 11898
===============================================================================
******************************************************************************/
#ifndef _UDS_ISO14229_SERVER_CONFIG_H_
#define _UDS_ISO14229_SERVER_CONFIG_H_
#include "stdint.h"
#include "Diag_ID_Def.h"
#define A_DATA_RX_BUFFER_SIZE N_USDATA_RX_BUFFER_SIZE
#define A_DATA_TX_BUFFER_SIZE N_USDATA_TX_BUFFER_SIZE
#define P2_SERVER (uint16_t)50 /*P2Server*/
#define P2_AST_SERVER (uint16_t)200 /*P2*Server*/
#define S3_SERVER (uint8_t)50 /*S3Server 50 00ms*/
#endif
/******************************************************************************
�� �� ��;UDS_ISO14229_Services.c
��������;ISO 14229 �淶�涨����Ϸ���������˷���ʵ�ֺ������ļ�
�� ��;����
�� ��;V1.0
�� ��;2016.11.1
******************************************************************************/
#include "UDS_ISO14229_Services.h"
#include "flash.h"
#include "CAN_CH0_CAN_Communication_Matrix.h"
#include <string.h>
typedef struct
{
uint8_t ProjecName[16]; // 项目名
uint8_t PartNumber[16]; // 零件号
uint8_t PlatForm[16]; // 平台
struct
{
uint8_t type[16]; //标准*
uint8_t ch[16]; //通道*
uint8_t baudrate[16]; //速率*
uint32_t diagID[4]; //诊断ID*
}DiagCanCfg[1]; //(诊断CAN)通道配置*
} ProjectInfoStruct;
extern void UDS_Service_Response(uint8_t si, uint8_t RspType, uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
/*诊断使用, 判断App一致性, 禁止修改(内部版本号除外)*/
extern const ProjectInfoStruct ProjectInfo __attribute__((section(".ARM.__at_0x0000C200"))) =
{
{"BENDA_V1000"}, // 凯威易行T06
{"44W00.471420"},
{"BAT32G179GM64FB"}, // 中微芯片
{
"CAN_Ext",
"CAN_CH_0",
"CAN_500Kbps",
{DIAG_ID_Tx, DIAG_ID_Rx_PHY, DIAG_ID_Rx_FUN, 0}
},
};
DiagSendDataNeg NegRes;
uint8_t UDS_ISO14229_Transfer[250];
DiagDFlashData DiagDataForDFlash;
DiagFlag DiagDataForFlag;
Ser27_FlowCtrlCntUnion Ser27_FlowCtrlCnt;
uint8_t FlashDriverCheckSum = 0;
uint8_t BlocKCnt = 0;
/****************S3Timer********************/
uint8_t S3_ServerEN = 0;
uint8_t S3_ServerCnt = 0;
uint8_t S3_Server_refresh = 0;
/****************DiagnosticSession*****************/
uint8_t SessionType = 0x01;
/****************Ser28*********************/
uint8_t ControlType;
uint8_t CommunicationType;
/****************Ser27*********************/
uint8_t wait10cnt = 0;
uint8_t Wait10sFlag = 0;
uint8_t DiagLockFlag = 0;
uint8_t Seed [ 4 ];
uint32_t ValidSeedKey;
uint8_t Services27_01_Requested;
/******************************************************************************
The service access point of the diagnostics application layer provides a number
of services that all have the same general structure. For each service, three
service primitives are specified:
- a service indication primitive, used by the diagnostics application layer, to
pass data to the server function of the ECU diagnostic application;
- a service response primitive, used by the server function in the ECU
diagnostic application, to pass response data provided by the requested
diagnostic service to the diagnostics application layer;
- a service response-confirmation primitive, used by the server function in the
ECU diagnostic application, to indicate that the data passed in the service
response primitive is successfully sent on the vehicle communication bus the
ECU received the diagnostic request on;
******************************************************************************/
//软件版本号 F195
uint8_t MCU_SWversion[6] = {'S', 'V', (uint8_t)((SWV >> 8u) & 0x0Fu) + 0x30u, '.', (uint8_t)((SWV >> 4u) & 0x0Fu) + 0x30u, (uint8_t)(SWV & 0x0Fu) + 0x30u};
//硬件版本号 F193
uint8_t MCU_HWversion[6] = {'H', 'V', (uint8_t)((HWV >> 8u) & 0x0Fu) + 0x30u, '.', (uint8_t)((HWV >> 4u) & 0x0Fu) + 0x30u, (uint8_t)(HWV & 0x0Fu) + 0x30u};
//引导程序版本信息 F180
uint8_t MCU_FBLversion[6] = {'B', 'V', (uint8_t)((BTV >> 8u) & 0x0Fu) + 0x30u, '.', (uint8_t)((BTV >> 4u) & 0x0Fu) + 0x30u, (uint8_t)(BTV & 0x0Fu) + 0x30u};
//零件号信息 F187
uint8_t MCU_PartNumber[12] = {'4', '4', 'W', '0', '0', '.', '4', '7', '1', '4', '2', '0'};
void Data_Set_DiagPara(void)
{
uint8_t i;
//获取DID的值,等待写入DFlash
for(i = 0; i < 6; i++)
{
DiagDataForDFlash.DID_F180[i] = MCU_FBLversion[i];
}
for(i = 0; i < 12; i++)
{
DiagDataForDFlash.DID_F187[i] = MCU_PartNumber[i];
}
for(i = 0; i < 6; i++)
{
DiagDataForDFlash.DID_F193[i] = MCU_HWversion[i];
}
for(i = 0; i < 6; i++)
{
DiagDataForDFlash.DID_F195[i] = MCU_SWversion[i];
}
for(i = 0; i < 2; i++)
{
DiagDataForDFlash.Filldata[i] = 0u;
}
}
/*写App有效性标志*/
void Write_App_InValid(uint32_t m32)
{
//设置App状态值
DiagDataForFlag.Flag = 0xA77A5AA5u;
DiagDataForFlag.APP_STATUS = m32;
//擦除扇区
EraseSector (APP_STATUS_ADDR);
//App程序状态和22服务数据全写进去
ProgramPage (APP_STATUS_ADDR, 8U, (uint8_t *)(&DiagDataForFlag));
}
/*写22服务数据*/
void DFlash_init(void)
{
}
void Data_Read_DiagPara(void)
{
uint8_t i;
DiagDFlashData* ReadDiagDataForDFlash =(DiagDFlashData*)APP_DATA_INFO;
if( (ReadDiagDataForDFlash->Flag != 0x5AA5A77Au)||
(0!=memcmp(ReadDiagDataForDFlash->DID_F180 , MCU_FBLversion,sizeof(MCU_FBLversion)))||
(0!=memcmp(ReadDiagDataForDFlash->DID_F187 , MCU_PartNumber,sizeof(MCU_PartNumber)))||
(0!=memcmp(ReadDiagDataForDFlash->DID_F193 , MCU_HWversion ,sizeof(MCU_HWversion )))||
(0!=memcmp(ReadDiagDataForDFlash->DID_F195 , MCU_SWversion ,sizeof(MCU_SWversion )))
)
{
Data_Set_DiagPara();
DiagDataForDFlash.Flag = 0x5AA5A77Au;
//擦除扇区
EraseSector (APP_DATA_INFO);
//App程序状态和22服务数据全写进去
ProgramPage (APP_DATA_INFO, sizeof(DiagDataForDFlash), (uint8_t *)(&DiagDataForDFlash));
}
else
{
memcpy((uint8_t *)&DiagDataForDFlash,(uint8_t *)&ReadDiagDataForDFlash,sizeof(DiagDFlashData));
}
}
void S3_ServerCNTT(void)
{
if ( Wait10sFlag == 1 )
{
if ( wait10cnt >= 100 )
{
Ser27_FlowCtrlCnt.RequestSeedCnt = 2;
Ser27_FlowCtrlCnt.Attemptcnt = 2;
// WriteDFlashData(0x02u, ( uint32_t * )&Ser27_FlowCtrlCnt.Flag, sizeof(Ser27_FlowCtrlCnt) / 4u, NoNeedWait);
wait10cnt = 0;
Wait10sFlag = 0;
}
else
{
++wait10cnt;
}
}
if ( S3_ServerEN == 1 )
{
if ( S3_Server_refresh == 1 )
{
S3_Server_refresh = 0;
S3_ServerCnt = 0;
}
if ( S3_ServerCnt >= S3_SERVER )
{
DIAG_InitParameter( );
CAN_RX_SetEnable(&CAN_CH0_CanMsgOp, CAN_N_RX_Enable);
CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Enable);//S3超时解除
}
else
{
++S3_ServerCnt;
}
}
}
uint32_t RANDOM = 0xA77A;
void Randomcnt(void)
{
RANDOM++;
if (RANDOM >= 0xFF0F00F0)
{
RANDOM = 0x363;
}
}
uint8_t SerXXNRC12Filter(uint8_t insub, const uint8_t *supportsub, uint8_t size, uint8_t *index)
{
uint8_t sub = insub;
uint8_t i = 0;
uint8_t nrccode = 0;
if ( size > 0 )
{
nrccode = subFunctionNotSupported;
while ( i < size )
{
if ( supportsub [ i ] == sub )
{
nrccode = 0;
*index = i;
break;
}
i++;
}
}
else
{
*index = 0;
}
return nrccode;
}
uint8_t SerXXNRC13Filter(uint8_t type, uint16_t serlength, const uint8_t *length, uint8_t index)
{
uint8_t nrccode;
nrccode = incorrectMessageLength;
if ( 0 == type )
{
if ( length != 0 )
{
if ( length [ 0 ] <= serlength )
{
nrccode = 0;
}
}
else
{
nrccode = incorrectMessageLength;
}
}
else
{
if ( length != 0 )
{
if ( length [ index ] == serlength )
{
nrccode = 0;
}
}
else
{
nrccode = incorrectMessageLength;
}
}
return nrccode;
}
uint8_t SerXXNRC22Filter(void)
{
uint8_t nrccode = 0;
nrccode = 0;
return nrccode;
}
uint8_t SerXXNRC31DIDFilter(uint16_t inDID, const uint16_t *DIDList, uint8_t size, uint8_t *index)
{
uint8_t nrccode = 0;
uint8_t i = 0;
if ( size > 0 )
{
nrccode = requestOutOfRange;
while ( i < size )
{
if ( inDID == DIDList [ i ] )
{
nrccode = 0;
*index = i;
break;
}
else
{
++i;
}
}
}
return nrccode;
}
uint8_t SerXXNRC33Filter(const uint8_t *levelList, uint8_t index)
{
uint8_t nrccode;
nrccode = 0;
if ( NoNeed != levelList [ index ] )
{
if ( DiagLockFlag != levelList [ index ] )
{
nrccode = securityAccessDenied;
}
}
return nrccode;
}
uint8_t SerXXNRC7EFilter(const uint8_t *SubSupportSession, uint8_t index)
{
uint8_t nrccode;
nrccode = 0;
if ( 0 == (GetCurrentSession( ) & SubSupportSession [ index ]) )
{
nrccode = subfunctionNotSupportinActiveSession;
}
return nrccode;
}
uint8_t SerXXNRC7FFilter(uint8_t SupportSession)
{
uint8_t nrccode;
nrccode = 0;
if ( 0 == (GetCurrentSession( ) & SupportSession) )
{
nrccode = serviceNotSupportedInActiveSession;
}
return nrccode;
}
uint8_t GetCurrentSession(void)
{
uint8_t SessionMode;
if ( DefaultSession == SessionType )
{
SessionMode = DefaultMode;
}
else if ( ProgrammingSession == SessionType )
{
SessionMode = ProgrammingMode;
}
else if ( ExtendedDiagnosticSession == SessionType )
{
SessionMode = ExtendedDiagnosticMode;
}
else
{
SessionType = DefaultSession;
SessionMode = DefaultMode;
}
return SessionMode;
}
/******************************************************************************
10# - DiagnosticSessionControl
******************************************************************************/
static const uint8_t Ser10BanResponse = AbleResponseType;
static const uint8_t Ser10Sub [] = {DefaultSession, ProgrammingSession, ExtendedDiagnosticSession};
static const uint8_t Ser10MinLen [] = {1};
static const uint8_t Ser10SubLen [] = {1, 1, 1};
static const uint8_t Ser10UnlockLevel [] = {NoNeed, NoNeed, NoNeed};
static const uint8_t Ser10SupportSession = DefaultMode | ProgrammingMode | ExtendedDiagnosticMode;
static const uint8_t Ser10SubSupportSession [] = {DefaultMode | ProgrammingMode | ExtendedDiagnosticMode, ProgrammingMode | ExtendedDiagnosticMode,
DefaultMode | ExtendedDiagnosticMode};
void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
uint8_t i;
uint8_t si = DiagnosticSessionControl;
uint8_t NrcCode = 0;
uint8_t Index = 0;
uint8_t SubFunction = 0;
uint16_t Service10DiagDataLength = A_Length;
uint16_t FunorPhy = A_TA_type;
for (i = 0; i < Service10DiagDataLength; ++i)
{
UDS_ISO14229_Transfer[i] = *(A_Data + i);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC7FFilter(Ser10SupportSession);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC13Filter(MinLengthType, Service10DiagDataLength, Ser10MinLen, Index);
}
if (0 == NrcCode)
{
if (Ser10BanResponse == AbleResponseType)
{
SubFunction = UDS_ISO14229_Transfer[0] & 0x7F;
}
else
{
SubFunction = UDS_ISO14229_Transfer[0];
}
NrcCode = SerXXNRC12Filter(SubFunction, Ser10Sub, sizeof(Ser10Sub), &Index);
}
if ((0 == NrcCode) && (DIAG_ID_Rx_FUN == FunorPhy))
{
if(Index < 3)
{
if(ProgrammingSession == Ser10Sub[Index])
{
NrcCode = subFunctionNotSupported;
}
}
}
if (0 == NrcCode && Index < sizeof(Ser10UnlockLevel))
{
NrcCode = SerXXNRC33Filter(Ser10UnlockLevel, Index);
}
if (0 == NrcCode && (Index < sizeof(Ser10SubSupportSession)))
{
NrcCode = SerXXNRC7EFilter(Ser10SubSupportSession, Index);
}
if (0 == NrcCode && (Index < sizeof(Ser10SubLen)))
{
NrcCode = SerXXNRC13Filter(SubLengthType, Service10DiagDataLength, Ser10SubLen, Index);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC22Filter();
}
if (0 == NrcCode)
{
UDS_ISO14229_Transfer[1] = 0;
UDS_ISO14229_Transfer[2] = 50;
UDS_ISO14229_Transfer[3] = 0;
UDS_ISO14229_Transfer[4] = 200;
if (DefaultSession == SubFunction)
{
S3_ServerEN = 0;
SessionType = DefaultSession;
DIAG_InitParameter();
CAN_RX_SetEnable(&CAN_CH0_CanMsgOp, CAN_N_RX_Enable);
CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Enable);//切会话解除
if (AbleResponseType == Ser10BanResponse)
{
if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
}
}
else
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
}
}
else if (ProgrammingSession == SubFunction)
{
S3_ServerEN = 1;
S3_Server_refresh = 1;
SessionType = ProgrammingSession;
Services27_01_Requested = 0;
DiagLockFlag = 0;
if (AbleResponseType == Ser10BanResponse)
{
if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
{
NegRes.code = requestCorrectlyReceivedResponsePending;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
Write_App_InValid(Jump_To_Boot_Need_Answer);
for (i = 0; i < 250; ++i)
{
__NOP();
}
__NVIC_SystemReset();
}
else
{
Write_App_InValid(Jump_To_Boot_No_Answer);
for (i = 0; i < 250; ++i)
{
__NOP();
}
__NVIC_SystemReset();
}
}
else
{
NegRes.code = requestCorrectlyReceivedResponsePending;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
Write_App_InValid(Jump_To_Boot_Need_Answer);
for (i = 0; i < 250; ++i)
{
__NOP();
}
__NVIC_SystemReset();
}
}
else if (ExtendedDiagnosticSession == SubFunction)
{
S3_ServerEN = 1;
S3_Server_refresh = 1;
SessionType = ExtendedDiagnosticSession;
Services27_01_Requested = 0;
DiagLockFlag = 0;
if (AbleResponseType == Ser10BanResponse)
{
if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
}
}
else
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
}
}
}
else
{
if (DIAG_ID_Rx_FUN == FunorPhy)
{
if ((NrcCode != 0x11) && (NrcCode != 0x12) && (NrcCode != 0x13) && (NrcCode != 0x7E) && (NrcCode != 0x7F) && (NrcCode != 0x31))
{
NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
}
}
else
{
NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
}
}
}
/******************************************************************************
11# - ECUReset
******************************************************************************/
static const uint8_t Ser11BanResponse = AbleResponseType;
static const uint8_t Ser11Sub [] = {HardReset};
static const uint8_t Ser11MinLen [] = {1};
static const uint8_t Ser11SubLen [] = {1};
static const uint8_t Ser11UnlockLevel [] = {NoNeed};
static const uint8_t Ser11SupportSession = ProgrammingMode | ExtendedDiagnosticMode;
static const uint8_t Ser11SubSupportSession [] = {ProgrammingMode | ExtendedDiagnosticMode};
void UDS_Service_11_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
uint8_t si = ECUReset;
uint8_t NrcCode = 0;
uint8_t Index = 0;
uint8_t SubFunction = 0;
uint16_t i;
uint16_t Service11DiagDataLength = A_Length;
uint16_t FunorPhy = A_TA_type;
for (i = 0; i < Service11DiagDataLength; ++i)
{
UDS_ISO14229_Transfer[i] = *(A_Data + i);
}
if ((0 == NrcCode) && (DIAG_ID_Rx_FUN == FunorPhy))
{
NrcCode = serviceNotSupported;
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC7FFilter(Ser11SupportSession);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC13Filter(MinLengthType, Service11DiagDataLength, Ser11MinLen, Index);
}
if (0 == NrcCode)
{
if (AbleResponseType == Ser11BanResponse)
{
SubFunction = UDS_ISO14229_Transfer[0] & 0x7F;
}
else
{
SubFunction = UDS_ISO14229_Transfer[0];
}
NrcCode = SerXXNRC12Filter(SubFunction, Ser11Sub, sizeof(Ser11Sub), &Index);
}
if (0 == NrcCode && Index < sizeof(Ser11UnlockLevel))
{
NrcCode = SerXXNRC33Filter(Ser11UnlockLevel, Index);
}
if (0 == NrcCode && (Index < sizeof(Ser11SubSupportSession)))
{
NrcCode = SerXXNRC7EFilter(Ser11SubSupportSession, Index);
}
if (0 == NrcCode && (Index < sizeof(Ser11SubLen)))
{
NrcCode = SerXXNRC13Filter(SubLengthType, Service11DiagDataLength, Ser11SubLen, Index);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC22Filter();
}
if (0 == NrcCode)
{
if (HardReset == SubFunction)
{
if (AbleResponseType == Ser11BanResponse)
{
if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
}
}
else
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
}
i = 900;
while (--i)
{
__NOP();
}
__NVIC_SystemReset();
}
}
else
{
if (DIAG_ID_Rx_FUN == FunorPhy)
{
if ((NrcCode != 0x11) && (NrcCode != 0x12) && (NrcCode != 0x7E) && (NrcCode != 0x7F) && (NrcCode != 0x31))
{
NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
}
}
else
{
NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
}
}
}
/******************************************************************************
22# - ReadDataByIdentifier
******************************************************************************/
static const uint8_t Ser22BanResponse = DisableResponseType;
static const uint8_t Ser22Sub [] = {0xFF};
static const uint8_t Ser22MinLen [] = {2};
static const uint8_t Ser22SubLen [] = {2};
static const uint16_t Ser22DIDList [] = {0xF195, 0xF193, 0xF180, 0xF187, 0x1024, 0x1028, 0xF184, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006};
static const uint8_t Ser22UnlockLevel [] = {NoNeed};
static const uint8_t Ser22SupportSession = DefaultMode | ProgrammingMode | ExtendedDiagnosticMode;
static const uint8_t Ser22SubSupportSession [] = {DefaultMode | ProgrammingMode | ExtendedDiagnosticMode};
void UDS_Service_22_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
uint8_t i;
uint8_t si = ReadDataByIdentifier;
uint8_t NrcCode = 0;
uint8_t Index = 0;
uint8_t SubFunction = 0;
uint16_t DID;
uint16_t Service22DiagDataLength = A_Length;
uint16_t FunorPhy = A_TA_type;
for (i = 0; i < Service22DiagDataLength; ++i)
{
UDS_ISO14229_Transfer[i] = *(A_Data + i);
}
if ((0 == NrcCode) && (DIAG_ID_Rx_FUN == FunorPhy))
{
NrcCode = serviceNotSupported;
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC7FFilter(Ser22SupportSession);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC13Filter(MinLengthType, Service22DiagDataLength, Ser22MinLen, Index);
}
if (0 == NrcCode)
{
if (AbleResponseType == Ser22BanResponse)
{
SubFunction = UDS_ISO14229_Transfer[0] & 0x7F;
}
else
{
SubFunction = UDS_ISO14229_Transfer[0];
}
NrcCode = SerXXNRC12Filter(SubFunction, Ser22Sub, 0, &Index);
}
if (0 == NrcCode && Index < sizeof(Ser22UnlockLevel))
{
NrcCode = SerXXNRC33Filter(Ser22UnlockLevel, Index);
}
if (0 == NrcCode && (Index < sizeof(Ser22SubSupportSession)))
{
NrcCode = SerXXNRC7EFilter(Ser22SubSupportSession, Index);
}
if (0 == NrcCode && (Index < sizeof(Ser22SubLen)))
{
NrcCode = SerXXNRC13Filter(SubLengthType, Service22DiagDataLength, Ser22SubLen, Index);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC22Filter();
}
if (0 == NrcCode)
{
DID = ((uint16_t)UDS_ISO14229_Transfer[0] << 8) | UDS_ISO14229_Transfer[1];
NrcCode = SerXXNRC31DIDFilter(DID, Ser22DIDList, (uint8_t)(sizeof(Ser22DIDList) / 2), &Index);
}
if (0 == NrcCode)
{
switch (DID)
{
case 0xF195: // 读软件版本号
for (i = 0; i < 6; i++)
{
UDS_ISO14229_Transfer[i + 2] = MCU_SWversion[i];
}
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 6, UDS_ISO14229_Transfer);
break;
case 0xF193: // 读硬件版本号
for (i = 0; i < 6; i++)
{
UDS_ISO14229_Transfer[i + 2] = MCU_HWversion[i];
}
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 6, UDS_ISO14229_Transfer);
break;
case 0xF180: // 读硬件版本号
for (i = 0; i < 6; i++)
{
UDS_ISO14229_Transfer[i + 2] = MCU_FBLversion[i];
}
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 6, UDS_ISO14229_Transfer);
break;
case 0xF187: // 读零件号
for (i = 0; i < 12; i++)
{
UDS_ISO14229_Transfer[i + 2] = MCU_PartNumber[i];
}
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 12, UDS_ISO14229_Transfer);
break;
case 0x1024: // 读内部版本号
UDS_ISO14229_Transfer[0] = 0x10u;
UDS_ISO14229_Transfer[1] = 0x24u;
UDS_ISO14229_Transfer[2] = 'A';
UDS_ISO14229_Transfer[3] = '_';
UDS_ISO14229_Transfer[4] = 'V';
UDS_ISO14229_Transfer[5] = (uint8_t)((INTLV >> 8u) & 0x0Fu) + 0x30u;
UDS_ISO14229_Transfer[6] = '.';
UDS_ISO14229_Transfer[7] = (uint8_t)((INTLV >> 4u) & 0x0Fu) + 0x30u;
UDS_ISO14229_Transfer[8] = (uint8_t)(INTLV & 0x0Fu) + 0x30u;
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 7, UDS_ISO14229_Transfer);
break;
case 0x1028: // 读内部版本号
UDS_ISO14229_Transfer[0] = 0x10u;
UDS_ISO14229_Transfer[1] = 0x28u;
UDS_ISO14229_Transfer[2] = '2';
UDS_ISO14229_Transfer[3] = '0';
UDS_ISO14229_Transfer[4] = (uint8_t)((PROG_Y >> 4u) & 0x0Fu) + 0x30u;
UDS_ISO14229_Transfer[5] = (uint8_t)(PROG_Y & 0x0Fu) + 0x30u;
UDS_ISO14229_Transfer[6] = (uint8_t)((PROG_M >> 4u) & 0x0Fu) + 0x30u;
UDS_ISO14229_Transfer[7] = (uint8_t)(PROG_M & 0x0Fu) + 0x30u;
UDS_ISO14229_Transfer[8] = (uint8_t)((PROG_D >> 4u) & 0x0Fu) + 0x30u;
UDS_ISO14229_Transfer[9] = (uint8_t)(PROG_D & 0x0Fu) + 0x30u;
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 8, UDS_ISO14229_Transfer);
break;
case 0x2001: // 读AmtLdrVer版本号
UDS_ISO14229_Transfer[0] = 0x20u;
UDS_ISO14229_Transfer[1] = 0x01u;
UDS_ISO14229_Transfer[2] = 'A';
UDS_ISO14229_Transfer[3] = 'm';
UDS_ISO14229_Transfer[4] = 't';
UDS_ISO14229_Transfer[5] = 'L';
UDS_ISO14229_Transfer[6] = 'd';
UDS_ISO14229_Transfer[7] = 'r';
UDS_ISO14229_Transfer[8] = 'V';
UDS_ISO14229_Transfer[9] = 'e';
UDS_ISO14229_Transfer[10] = 'r';
UDS_ISO14229_Transfer[11] = ':';
UDS_ISO14229_Transfer[12] = amt630Version.AmtLdrVer[0];
UDS_ISO14229_Transfer[13] = amt630Version.AmtLdrVer[1];
UDS_ISO14229_Transfer[14] = amt630Version.AmtLdrVer[2];
UDS_ISO14229_Transfer[15] = amt630Version.AmtLdrVer[3];
UDS_ISO14229_Transfer[16] = amt630Version.AmtLdrVer[4];
UDS_ISO14229_Transfer[17] = amt630Version.AmtLdrVer[5];
UDS_ISO14229_Transfer[18] = amt630Version.AmtLdrVer[6];
UDS_ISO14229_Transfer[19] = amt630Version.AmtLdrVer[7];
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 18, UDS_ISO14229_Transfer);
break;
case 0x2002: // 读StepLdrVer版本号
UDS_ISO14229_Transfer[0] = 0x20u;
UDS_ISO14229_Transfer[1] = 0x02u;
UDS_ISO14229_Transfer[2] = 'S';
UDS_ISO14229_Transfer[3] = 't';
UDS_ISO14229_Transfer[4] = 'e';
UDS_ISO14229_Transfer[5] = 'p';
UDS_ISO14229_Transfer[6] = 'L';
UDS_ISO14229_Transfer[7] = 'd';
UDS_ISO14229_Transfer[8] = 'r';
UDS_ISO14229_Transfer[9] = 'V';
UDS_ISO14229_Transfer[10] = 'e';
UDS_ISO14229_Transfer[11] = 'r';
UDS_ISO14229_Transfer[12] = ':';
UDS_ISO14229_Transfer[13] = amt630Version.StepLdrVer[0];
UDS_ISO14229_Transfer[14] = amt630Version.StepLdrVer[1];
UDS_ISO14229_Transfer[15] = amt630Version.StepLdrVer[2];
UDS_ISO14229_Transfer[16] = amt630Version.StepLdrVer[3];
UDS_ISO14229_Transfer[17] = amt630Version.StepLdrVer[4];
UDS_ISO14229_Transfer[18] = amt630Version.StepLdrVer[5];
UDS_ISO14229_Transfer[19] = amt630Version.StepLdrVer[6];
UDS_ISO14229_Transfer[20] = amt630Version.StepLdrVer[7];
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 19, UDS_ISO14229_Transfer);
break;
case 0x2003: // 读AmtAppVer版本号
UDS_ISO14229_Transfer[0] = 0x20u;
UDS_ISO14229_Transfer[1] = 0x03u;
UDS_ISO14229_Transfer[2] = 'A';
UDS_ISO14229_Transfer[3] = 'm';
UDS_ISO14229_Transfer[4] = 't';
UDS_ISO14229_Transfer[5] = 'A';
UDS_ISO14229_Transfer[6] = 'P';
UDS_ISO14229_Transfer[7] = 'P';
UDS_ISO14229_Transfer[8] = 'V';
UDS_ISO14229_Transfer[9] = 'e';
UDS_ISO14229_Transfer[10] = 'r';
UDS_ISO14229_Transfer[11] = ':';
UDS_ISO14229_Transfer[12] = amt630Version.AmtAppVer[0];
UDS_ISO14229_Transfer[13] = amt630Version.AmtAppVer[1];
UDS_ISO14229_Transfer[14] = amt630Version.AmtAppVer[2];
UDS_ISO14229_Transfer[15] = amt630Version.AmtAppVer[3];
UDS_ISO14229_Transfer[16] = amt630Version.AmtAppVer[4];
UDS_ISO14229_Transfer[17] = amt630Version.AmtAppVer[5];
UDS_ISO14229_Transfer[18] = amt630Version.AmtAppVer[6];
UDS_ISO14229_Transfer[19] = amt630Version.AmtAppVer[7];
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 18, UDS_ISO14229_Transfer);
break;
case 0x2004: // 读UiVer版本号
UDS_ISO14229_Transfer[0] = 0x20u;
UDS_ISO14229_Transfer[1] = 0x04u;
UDS_ISO14229_Transfer[2] = 'U';
UDS_ISO14229_Transfer[3] = 'i';
UDS_ISO14229_Transfer[4] = 'V';
UDS_ISO14229_Transfer[5] = 'e';
UDS_ISO14229_Transfer[6] = 'r';
UDS_ISO14229_Transfer[7] = ':';
UDS_ISO14229_Transfer[8] = amt630Version.UiVer[0];
UDS_ISO14229_Transfer[9] = amt630Version.UiVer[1];
UDS_ISO14229_Transfer[10] = amt630Version.UiVer[2];
UDS_ISO14229_Transfer[11] = amt630Version.UiVer[3];
UDS_ISO14229_Transfer[12] = amt630Version.UiVer[4];
UDS_ISO14229_Transfer[13] = amt630Version.UiVer[5];
UDS_ISO14229_Transfer[14] = amt630Version.UiVer[6];
UDS_ISO14229_Transfer[15] = amt630Version.UiVer[7];
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 14, UDS_ISO14229_Transfer);
break;
case 0x2005: // 读SoundVer版本号
UDS_ISO14229_Transfer[0] = 0x20u;
UDS_ISO14229_Transfer[1] = 0x05u;
UDS_ISO14229_Transfer[2] = 'S';
UDS_ISO14229_Transfer[3] = 'o';
UDS_ISO14229_Transfer[4] = 'u';
UDS_ISO14229_Transfer[5] = 'n';
UDS_ISO14229_Transfer[6] = 'd';
UDS_ISO14229_Transfer[7] = 'V';
UDS_ISO14229_Transfer[8] = 'e';
UDS_ISO14229_Transfer[9] = 'r';
UDS_ISO14229_Transfer[10] = ':';
UDS_ISO14229_Transfer[11] = amt630Version.SoundVer[0];
UDS_ISO14229_Transfer[12] = amt630Version.SoundVer[1];
UDS_ISO14229_Transfer[13] = amt630Version.SoundVer[2];
UDS_ISO14229_Transfer[14] = amt630Version.SoundVer[3];
UDS_ISO14229_Transfer[15] = amt630Version.SoundVer[4];
UDS_ISO14229_Transfer[16] = amt630Version.SoundVer[5];
UDS_ISO14229_Transfer[17] = amt630Version.SoundVer[6];
UDS_ISO14229_Transfer[18] = amt630Version.SoundVer[7];
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 17, UDS_ISO14229_Transfer);
break;
case 0x2006: // 读FontVer版本号
UDS_ISO14229_Transfer[0] = 0x20u;
UDS_ISO14229_Transfer[1] = 0x06u;
UDS_ISO14229_Transfer[2] = 'F';
UDS_ISO14229_Transfer[3] = 'o';
UDS_ISO14229_Transfer[4] = 'n';
UDS_ISO14229_Transfer[5] = 't';
UDS_ISO14229_Transfer[6] = 'V';
UDS_ISO14229_Transfer[7] = 'e';
UDS_ISO14229_Transfer[8] = 'r';
UDS_ISO14229_Transfer[9] = ':';
UDS_ISO14229_Transfer[10] = amt630Version.FontVer[0];
UDS_ISO14229_Transfer[11] = amt630Version.FontVer[1];
UDS_ISO14229_Transfer[12] = amt630Version.FontVer[2];
UDS_ISO14229_Transfer[13] = amt630Version.FontVer[3];
UDS_ISO14229_Transfer[14] = amt630Version.FontVer[4];
UDS_ISO14229_Transfer[15] = amt630Version.FontVer[5];
UDS_ISO14229_Transfer[16] = amt630Version.FontVer[6];
UDS_ISO14229_Transfer[17] = amt630Version.FontVer[7];
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 2 + 16, UDS_ISO14229_Transfer);
break;
default:
// if (FunorPhy == DIAG_ID_Rx_FUN)
// {
// return;
// }
NegRes.code = requestOutOfRange;
UDS_Service_Response ( 0x22, NEGATIVE_RSP, DIAG_ID_Tx, 1, ( uint8_t * ) ( &NegRes.code ) );
break;
}
}
else
{
if (DIAG_ID_Rx_FUN == FunorPhy)
{
if ((NrcCode != 0x11) && (NrcCode != 0x12) && (NrcCode != 0x7E) && (NrcCode != 0x7F) && (NrcCode != 0x31))
{
NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
}
}
else
{
NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
}
}
}
void GetSeed(void)
{
uint8_t SeedHigh;
uint8_t SeedLow;
uint16_t tempbuffer;
tempbuffer = (uint16_t)(RANDOM);
SeedHigh = (uint8_t)(tempbuffer >> 8);
SeedLow = (uint8_t)(tempbuffer & 0xff);
Seed[0] = 0x31 + ~SeedHigh;
Seed[1] = 0x23 + ~SeedLow;
Seed[2] = 0x56 + SeedHigh;
Seed[3] = 0x71 + SeedLow;
}
const uint32_t KeyK = 0x00000201;
void CalculateKey(void)
{
uint32_t SaccSeed;
uint32_t KeyResult;
SaccSeed = ((uint32_t)Seed[0]) << 24 | ((uint32_t)Seed[1]) << 16 | ((uint32_t)Seed[2]) << 8 | ((uint32_t)Seed[3]);
KeyResult = (((SaccSeed >> 1) ^ SaccSeed) << 3) ^ (SaccSeed >> 2);
KeyResult = KeyResult ^ KeyK;
ValidSeedKey = KeyResult;
}
void CalculateKeyLv11(void)
{
uint32_t SaccSeed;
uint32_t KeyResult;
SaccSeed = (( uint32_t )Seed [ 0 ]) << 24 | (( uint32_t )Seed [ 1 ]) << 16 | (( uint32_t )Seed [ 2 ]) << 8 | (( uint32_t )Seed [ 3 ]);
KeyResult = (((SaccSeed >> 1) ^ SaccSeed) << 3) ^ (SaccSeed >> 2);
KeyResult = KeyResult ^ KeyK;
ValidSeedKey = KeyResult;
}
/******************************************************************************
27# - SecurityAccess
******************************************************************************/
static const uint8_t Ser27BanResponse = DisableResponseType;
static const uint8_t Ser27Sub [] = {requestSeed_LV1, sendKey_LV1};
static const uint8_t Ser27MinLen [] = {1};
static const uint8_t Ser27SubLen [] = {1, 5};
static const uint8_t Ser27UnlockLevel [] = {NoNeed, NoNeed};
static const uint8_t Ser27SupportSession = ProgrammingMode | ExtendedDiagnosticMode;
static const uint8_t Ser27SubSupportSession [] = {ProgrammingMode | ExtendedDiagnosticMode, ProgrammingMode | ExtendedDiagnosticMode};
void UDS_Service_27_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
uint8_t i;
uint8_t si = SecurityAccess;
uint8_t NrcCode = 0;
uint8_t Index = 0;
uint8_t SubFunction = 0;
uint16_t FunorPhy = A_TA_type;
uint16_t Service27DiagDataLength = A_Length;
uint32_t KeyReceive;
for (i = 0; i < Service27DiagDataLength; ++i)
{
UDS_ISO14229_Transfer[i] = *(A_Data + i);
}
if ((0 == NrcCode) && (DIAG_ID_Rx_FUN == FunorPhy))
{
NrcCode = serviceNotSupported;
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC7FFilter(Ser27SupportSession);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC13Filter(MinLengthType, Service27DiagDataLength, Ser27MinLen, Index);
}
if (0 == NrcCode)
{
if (AbleResponseType == Ser27BanResponse)
{
SubFunction = UDS_ISO14229_Transfer[0] & 0x7F;
}
else
{
SubFunction = UDS_ISO14229_Transfer[0];
}
NrcCode = SerXXNRC12Filter(SubFunction, Ser27Sub, sizeof(Ser27Sub), &Index);
}
if (0 == NrcCode && Index < sizeof(Ser27UnlockLevel))
{
NrcCode = SerXXNRC33Filter(Ser27UnlockLevel, Index);
}
if (0 == NrcCode && (Index < sizeof(Ser27SubSupportSession)))
{
NrcCode = SerXXNRC7EFilter(Ser27SubSupportSession, Index);
}
if (0 == NrcCode && (Index < sizeof(Ser27SubLen)))
{
NrcCode = SerXXNRC13Filter(SubLengthType, Service27DiagDataLength, Ser27SubLen, Index);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC22Filter();
}
if (0 == NrcCode)
{
if (1 == Wait10sFlag)
{
NrcCode = requiredTimeDelayNotExpired;
}
}
if (0 == NrcCode)
{
if (0 == Services27_01_Requested && sendKey_LV1 == SubFunction)
{
NrcCode = requestSequenceError;
}
}
if (0 == NrcCode)
{
KeyReceive = ((uint32_t)UDS_ISO14229_Transfer[1] << 24) | ((uint32_t)UDS_ISO14229_Transfer[2] << 16) | ((uint16_t)UDS_ISO14229_Transfer[3] << 8) | UDS_ISO14229_Transfer[4];
if (sendKey_LV1 == SubFunction && (KeyReceive == 0x00000000 || KeyReceive == 0xFFFFFFFF))
{
NrcCode = requestOutOfRange;
}
}
if (0 == NrcCode)
{
if (sendKey_LV1 == SubFunction && KeyReceive != ValidSeedKey)
{
Services27_01_Requested = 0;
Ser27_FlowCtrlCnt.Attemptcnt++;
// WriteDFlashData(0x02u, (uint32_t *)&Ser27_FlowCtrlCnt.Flag, sizeof(Ser27_FlowCtrlCnt) / 4u, NeedWait);
NrcCode = invalidKey;
}
}
if (0 == NrcCode || invalidKey == NrcCode)
{
if (sendKey_LV1 == SubFunction)
{
if (AttemptMaxCnt <= Ser27_FlowCtrlCnt.Attemptcnt)
{
Wait10sFlag = 1;
NrcCode = exceedNumberOfAttempts;
}
}
if (requestSeed_LV1 == SubFunction)
{
Ser27_FlowCtrlCnt.Attemptcnt = Ser27_FlowCtrlCnt.RequestSeedCnt;
Ser27_FlowCtrlCnt.RequestSeedCnt++;
// WriteDFlashData(0x02u, (uint32_t *)&Ser27_FlowCtrlCnt.Flag, sizeof(Ser27_FlowCtrlCnt) / 4u, NeedWait);
if (RequestSeedMaxCnt <= Ser27_FlowCtrlCnt.RequestSeedCnt)
{
Wait10sFlag = 1;
NrcCode = exceedNumberOfAttempts;
}
}
}
if (0 == NrcCode)
{
if (requestSeed_LV1 == SubFunction)
{
if (NormalKeyLock == DiagLockFlag)
{
Services27_01_Requested = 1;
// do
// {
GetSeed();
// } while (Seed[0] == 0 && Seed[1] == 0 && Seed[2] == 0 && Seed[3] == 0);
UDS_ISO14229_Transfer[1] = Seed[0];
UDS_ISO14229_Transfer[2] = Seed[1];
UDS_ISO14229_Transfer[3] = Seed[2];
UDS_ISO14229_Transfer[4] = Seed[3];
if (AbleResponseType == Ser27BanResponse)
{
if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
}
}
else
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
}
CalculateKey();
}
else if (NormalKeyUnlock == DiagLockFlag)
{
UDS_ISO14229_Transfer[1] = 0;
UDS_ISO14229_Transfer[2] = 0;
UDS_ISO14229_Transfer[3] = 0;
UDS_ISO14229_Transfer[4] = 0;
if (AbleResponseType == Ser27BanResponse)
{
if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
}
}
else
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 5, UDS_ISO14229_Transfer);
}
}
}
else if (sendKey_LV1 == SubFunction)
{
Services27_01_Requested = 0;
Ser27_FlowCtrlCnt.RequestSeedCnt = 0;
Ser27_FlowCtrlCnt.Attemptcnt = 0;
DiagLockFlag = NormalKeyUnlock;
if (AbleResponseType == Ser27BanResponse)
{
if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
}
}
else
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
}
// WriteDFlashData(0x02u, (uint32_t *)&Ser27_FlowCtrlCnt.Flag, sizeof(Ser27_FlowCtrlCnt) / 4u, NeedWait);
}
}
else
{
if (DIAG_ID_Rx_FUN == FunorPhy)
{
if ((NrcCode != 0x11) && (NrcCode != 0x12) && (NrcCode != 0x7E) && (NrcCode != 0x7F) && (NrcCode != 0x31))
{
NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
}
}
else
{
NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
}
}
}
/******************************************************************************
28# - CommunicationControl
******************************************************************************/
static const uint8_t Ser28BanResponse = AbleResponseType;
static const uint8_t Ser28Sub [] = {enableRxAndTx,/*enableRxAndDisableTx, disableRxAndEnableTx, */disableRxAndTx};
static const uint8_t Ser28MinLen [] = {2};
static const uint8_t Ser28SubLen [] = {2, /*2, 2,*/ 2};
static const uint8_t Ser28UnlockLevel [] = {NoNeed, NoNeed, NoNeed, NoNeed};
static const uint8_t Ser28SupportSession = ProgrammingMode | ExtendedDiagnosticMode;
static const uint8_t Ser28SubSupportSession [] = {ProgrammingMode | ExtendedDiagnosticMode, ProgrammingMode | ExtendedDiagnosticMode, ProgrammingMode | ExtendedDiagnosticMode, ProgrammingMode | ExtendedDiagnosticMode};
void UDS_Service_28_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
uint8_t i;
uint8_t si = CommunicationControl;
uint8_t NrcCode = 0;
uint8_t Index = 0;
uint8_t SubFunction = 0;
uint16_t FunorPhy = A_TA_type;
uint16_t Service28DiagDataLength = A_Length;
for (i = 0; i < Service28DiagDataLength; ++i)
{
UDS_ISO14229_Transfer[i] = *(A_Data + i);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC7FFilter(Ser28SupportSession);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC13Filter(MinLengthType, Service28DiagDataLength, Ser28MinLen, Index);
}
if (0 == NrcCode)
{
if (AbleResponseType == Ser28BanResponse)
{
SubFunction = UDS_ISO14229_Transfer[0] & 0x7F;
}
else
{
SubFunction = UDS_ISO14229_Transfer[0];
}
NrcCode = SerXXNRC12Filter(SubFunction, Ser28Sub, sizeof(Ser28Sub), &Index);
}
if (0 == NrcCode && Index < sizeof(Ser28UnlockLevel))
{
NrcCode = SerXXNRC33Filter(Ser28UnlockLevel, Index);
}
if (0 == NrcCode && (Index < sizeof(Ser28SubSupportSession)))
{
NrcCode = SerXXNRC7EFilter(Ser28SubSupportSession, Index);
}
if (0 == NrcCode && (Index < sizeof(Ser28SubLen)))
{
NrcCode = SerXXNRC13Filter(SubLengthType, Service28DiagDataLength, Ser28SubLen, Index);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC22Filter();
}
// if (0 == NrcCode)
// {
// if (normalCommunicationMessages != UDS_ISO14229_Transfer[1])
// {
// NrcCode = requestOutOfRange;
// }
// }
if (0 == NrcCode)
{
if (enableRxAndTx == SubFunction)
{
if (UDS_ISO14229_Transfer[1] & normalCommunicationMessages)
{
CAN_RX_SetEnable(&CAN_CH0_CanMsgOp, CAN_N_RX_Enable);
CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Enable);
}
if (UDS_ISO14229_Transfer[1] & networkManagementCommunicationMessages)
{
}
}
else if (disableRxAndTx == SubFunction)
{
if (UDS_ISO14229_Transfer[1] & normalCommunicationMessages)
{
CAN_RX_SetEnable(&CAN_CH0_CanMsgOp, CAN_N_RX_Disable);
CAN_TX_SetEnable(&CAN_CH0_CanMsgTxOp, CAN_N_TX_Disable);
}
if (UDS_ISO14229_Transfer[1] & networkManagementCommunicationMessages)
{
}
if (UDS_ISO14229_Transfer[1] & networkManagementCommunicationMessagesnormalCommunicationMessages)
{
}
}
if (AbleResponseType == Ser28BanResponse)
{
if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
}
}
else
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
}
}
else
{
if (DIAG_ID_Rx_FUN == FunorPhy)
{
if ((NrcCode != 0x11) && (NrcCode != 0x12) && (NrcCode != 0x13) && (NrcCode != 0x7E) && (NrcCode != 0x7F) && (NrcCode != 0x31))
{
NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
}
}
else
{
NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
}
}
}
/******************************************************************************
3E# - TesterPresent
******************************************************************************/
static const uint8_t Ser3EBanResponse = AbleResponseType;
static const uint8_t Ser3ESub [] = {0x00};
static const uint8_t Ser3EMinLen [] = {1};
static const uint8_t Ser3ESubLen [] = {1};
static const uint8_t Ser3EUnlockLevel [] = {NoNeed};
static const uint8_t Ser3ESupportSession = DefaultMode | ProgrammingMode | ExtendedDiagnosticMode;
static const uint8_t Ser3ESubSupportSession [] = {DefaultMode | ProgrammingMode | ExtendedDiagnosticMode};
void UDS_Service_3E_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
uint8_t i;
uint8_t si = TesterPresent;
uint8_t NrcCode = 0;
uint8_t Index = 0;
uint8_t SubFunction = 0;
uint16_t Service3EDiagDataLength = A_Length;
uint16_t FunorPhy = A_TA_type;
for (i = 0; i < Service3EDiagDataLength; ++i)
{
UDS_ISO14229_Transfer[i] = *(A_Data + i);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC7FFilter(Ser3ESupportSession);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC13Filter(MinLengthType, Service3EDiagDataLength, Ser3EMinLen, Index);
}
if (0 == NrcCode)
{
if (AbleResponseType == Ser3EBanResponse)
{
SubFunction = UDS_ISO14229_Transfer[0] & 0x7F;
}
else
{
SubFunction = UDS_ISO14229_Transfer[0];
}
NrcCode = SerXXNRC12Filter(SubFunction, Ser3ESub, sizeof(Ser3ESub), &Index);
}
if (0 == NrcCode && Index < sizeof(Ser3EUnlockLevel))
{
NrcCode = SerXXNRC33Filter(Ser3EUnlockLevel, Index);
}
if (0 == NrcCode && (Index < sizeof(Ser3ESubSupportSession)))
{
NrcCode = SerXXNRC7EFilter(Ser3ESubSupportSession, Index);
}
if (0 == NrcCode && (Index < sizeof(Ser3ESubLen)))
{
NrcCode = SerXXNRC13Filter(SubLengthType, Service3EDiagDataLength, Ser3ESubLen, Index);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC22Filter();
}
if (0 == NrcCode)
{
if (0x00 == SubFunction)
{
S3_Server_refresh = 1;
if (AbleResponseType == Ser3EBanResponse)
{
if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
}
}
else
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
}
}
}
else
{
if (DIAG_ID_Rx_FUN == FunorPhy)
{
if ((NrcCode != 0x11) && (NrcCode != 0x12) && (NrcCode != 0x13) && (NrcCode != 0x7E) && (NrcCode != 0x7F) && (NrcCode != 0x31))
{
NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
}
}
else
{
NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
}
}
}
/******************************************************************************
85# - ControlDTCSetting
******************************************************************************/
static const uint8_t Ser85BanResponse = AbleResponseType;
static const uint8_t Ser85Sub [] = {DTCSettingOn, DTCSettingOff};
static const uint8_t Ser85MinLen [] = {1};
static const uint8_t Ser85SubLen [] = {1, 1};
static const uint8_t Ser85UnlockLevel [] = {NoNeed, NoNeed};
static const uint8_t Ser85SupportSession = ProgrammingMode | ExtendedDiagnosticMode;
static const uint8_t Ser85SubSupportSession [] = {ProgrammingMode | ExtendedDiagnosticMode};
void UDS_Service_85_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data)
{
uint8_t i;
uint8_t si = ControlDTCSetting;
uint8_t NrcCode = 0;
uint8_t Index = 0;
uint8_t SubFunction = 0;
uint16_t Service85DiagDataLength = A_Length;
uint16_t FunorPhy = A_TA_type;
for (i = 0; i < Service85DiagDataLength; ++i)
{
UDS_ISO14229_Transfer[i] = *(A_Data + i);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC7FFilter(Ser85SupportSession);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC13Filter(MinLengthType, Service85DiagDataLength, Ser85MinLen, Index);
}
if (0 == NrcCode)
{
if (AbleResponseType == Ser85BanResponse)
{
SubFunction = UDS_ISO14229_Transfer[0] & 0x7F;
}
else
{
SubFunction = UDS_ISO14229_Transfer[0];
}
NrcCode = SerXXNRC12Filter(SubFunction, Ser85Sub, sizeof(Ser85Sub), &Index);
}
if (0 == NrcCode && Index < sizeof(Ser85UnlockLevel))
{
NrcCode = SerXXNRC33Filter(Ser85UnlockLevel, Index);
}
if (0 == NrcCode && (Index < sizeof(Ser85SubSupportSession)))
{
NrcCode = SerXXNRC7EFilter(Ser85SubSupportSession, Index);
}
if (0 == NrcCode && (Index < sizeof(Ser85SubLen)))
{
NrcCode = SerXXNRC13Filter(SubLengthType, Service85DiagDataLength, Ser85SubLen, Index);
}
if (0 == NrcCode)
{
NrcCode = SerXXNRC22Filter();
}
if (0 == NrcCode)
{
if (DTCSettingOn == SubFunction)
{
// CAN_DTC_OFF = 0;
}
else if (DTCSettingOff == SubFunction)
{
// CAN_DTC_OFF = 1;
}
if (AbleResponseType == Ser85BanResponse)
{
if (bit_is_clear(UDS_ISO14229_Transfer[0], 7))
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
}
}
else
{
UDS_Service_Response(si, POSITIVE_RSP, DIAG_ID_Tx, 1, UDS_ISO14229_Transfer);
}
}
else
{
if (DIAG_ID_Rx_FUN == FunorPhy)
{
if ((NrcCode != 0x11) && (NrcCode != 0x12) && (NrcCode != 0x13) && (NrcCode != 0x7E) && (NrcCode != 0x7F) && (NrcCode != 0x31))
{
NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
}
}
else
{
NegRes.code = NrcCode;
UDS_Service_Response(si, NEGATIVE_RSP, DIAG_ID_Tx, 1, (uint8_t *)(&NegRes.code));
return;
}
}
}
/*************IGN on,S3 Timeout****************/
void DIAG_InitParameter(void)
{
S3_ServerCnt = 0;
S3_ServerEN = 0;
SessionType = DefaultSession;
DiagLockFlag = 0;
RANDOM = 1573;
Services27_01_Requested = 0;
}
/******************************************************************************
�� �� ����UDS_ISO14229_Services.h
����������ISO 14229 �淶�涨����Ϸ���������˷���ʵ�ֺ���ͷ�ļ�
�� �ߣ�����
�� ����V1.0
�� �ڣ�2016.11.1
******************************************************************************/
/******************************************************************************
��Ϸ����OSIģ��ӳ��
===============================================================================
* NO. OSI Layer Diagnostics services
-------------------------------------------------------------------------------
* 7 Application ISO 14229-1 ISO 14229-3
6 Presentation -
* 5 Session ISO 14229-2
4 Transport ISO 15765-2
3 Network ISO 15765-2
2 Data Link ISO 11898
1 Physical ISO 11898
===============================================================================
******************************************************************************/
#ifndef _UDS_ISO14229_SERVICES_H_
#define _UDS_ISO14229_SERVICES_H_
#include "UDS_ISO14229_Server_Config.h"
/**
*DFlash
*/
#define NoNeedWait 0x00u
#define NeedWait 0x01u
#define APP_STATUS_ADDR (0x501000)
#define APP_DATA_INFO (0x501400)
#define APP_DATA_WRITE (0x501800)
#define UP_DATA_WRITE (0x502200)
#define Data_Flash_Addr_BootValid_OFFSET 0x00000000
#define Data_Flash_Addr_DiagReceive_OFFSET 0x00002000
#define Data_Flash_Addr_Diag2E_OFFSET 0x00004000
#define Data_Flash_Addr_DiagDTC_OFFSET 0x00006000
#define Data_Flash_Addr_GHdata_OFFSET 0x0000A000 // ��������
#define Jump_To_Boot_Need_Answer 0xA77A3AA3
#define Jump_To_Boot_No_Answer 0xA77A8AA8
#define Flag_App_OTAStatus 0x3663B88B // ����Boot��ԭ������ΪOTA
#define EnableInterrupts \
{ \
__asm CLI; \
}
#define DisableInterrupts \
{ \
__asm SEI; \
}
/*-----------------------------------------------------------------------------
Response
-----------------------------------------------------------------------------*/
#define POSITIVE_RSP 0x00
#define NEGATIVE_RSP (! POSITIVE_RSP)
/*-----------------------------------------------------------------------------
SPRMB
-----------------------------------------------------------------------------*/
#define AbleResponseType 0x00
#define DisableResponseType (! AbleResponseType)
/*-----------------------------------------------------------------------------
NRC13 check type
-----------------------------------------------------------------------------*/
#define MinLengthType 0x00
#define SubLengthType (! MinLengthType)
/*-----------------------------------------------------------------------------
Session mode
-----------------------------------------------------------------------------*/
#define DefaultMode 0x01
#define ProgrammingMode 0x02
#define ExtendedDiagnosticMode 0x04
/*----------------------------------------------- ------------------------------
Security level
-----------------------------------------------------------------------------*/
#define NoNeed 0x00
#define NeedLevel1 0x01
#define NeedLevel2 0x02
/******************************************************************************
Function and subFunction
Diagnostic Session
*******************************************************************************/
#define DiagnosticSessionControl 0x10
#define DefaultSession 0x01
#define ProgrammingSession 0x02
#define ExtendedDiagnosticSession 0x03
/******************************************************************************
Function and subFunction
ECUReset
********************************************************************************/
#define ECUReset 0x11
#define HardReset 0x01
#define SoftReset 0x03
/******************************************************************************
Function and subFunction
ClearDiagnosticInformation
********************************************************************************/
#define ClearDiagnosticInformation 0x14
/******************************************************************************
Function and subFunction
ReadDTCInformation
********************************************************************************/
#define ReadDTCInformation 0x19
#define Error 0x09
#define NoError 0x08
#define OTA_None 0x00
#define OTA_Reqed 0x01
#define OTA_Quit 0x02
// typedef void (*pfuMsgLost)(void);
// typedef void (*pfuMsgOk)(void);
// typedef struct
// {
// const uint16_t Msg_ID;
// volatile uint16_t *DTC_NodeLostCnt;
// volatile uint8_t *DTC_NodeLostFlag;
// const uint16_t DTC_LostPeriod;
// pfuMsgLost pfuLostHandleCBK;
// pfuMsgOk pfuOkHandleCBK;
// } DTC_NodeLostType;
#define _CLRBIT(p, b) p &= ~(1 << b)
#define _SETBIT(p, b) p |= (1 << b)
#define _bit_is_set(p, b) p & (1 << b)
#define CLRBIT(p, b) p &= ~(1 << b)
#define SETBIT(p, b) p |= (1 << b)
#define bit_is_set(p, b) p & (1 << b)
#define bit_is_clear(p, b) ! (p & (1 << b))
#define bit_2_set(p, b) ((p >> b) & 0x03)
#define bit_3_set(p, b) ((p >> b) & 0x07)
#define bit_4_set(p, b) ((p >> b) & 0x0F)
#define bit_5_set(p, b) ((p >> b) & 0x1F)
#define bit_6_set(p, b) ((p >> b) & 0x3F)
#define bit_7_set(p, b) ((p >> b) & 0x7F)
/******************************************************************************
Function and subFunction
ReadDataByIdentifier
********************************************************************************/
#define ReadDataByIdentifier 0x22
/******************************************************************************
Function and subFunction
SecurityAccess
********************************************************************************/
#define SecurityAccess 0x27
#define requestSeed_LV1 0x01
#define sendKey_LV1 0x02
// #define requestSeed_LV3 0x03
// #define sendKey_LV3 0x04
// #define requestSeed_LV5 0x05
// #define sendKey_LV5 0x06
// #define requestSeed_LV11 0x11
// #define sendKey_LV11 0x12
#define NormalKeyUnlock 0x01
#define NormalKeyLock 0x00
#define BootKeyUnlock 0x02
#define BootKeyLock 0x00
#define RequestSeedMaxCnt 0x04
#define AttemptMaxCnt 0x03
#define FLASH_SECTOR_SIZE 0x400
/******************************************************************************
Function and subFunction and type
CommunicationControl
********************************************************************************/
#define CommunicationControl 0x28
#define enableRxAndTx 0x00
#define enableRxAndDisableTx 0x01
#define disableRxAndEnableTx 0x02
#define disableRxAndTx 0x03
#define normalCommunicationMessages 0x01
#define networkManagementCommunicationMessages 0x02
#define networkManagementCommunicationMessagesnormalCommunicationMessages 0x03
/************************28SerStart*********************/
#define DIAG_COM_NOR_TX 0x01
#define DIAG_COM_NOR_RX 0x02
#define DIAG_COM_NW_TX 0x04
#define DIAG_COM_NW_RX 0x08
#define DIAG_SET_COM_DISABLE(Var, BitField, Type) ((Var) |= ( Type )(BitField))
#define DIAG_SET_COM_ENABLE(Var, BitField, Type) ((Var) &= (Type) ~( Type )(BitField))
#define DIAG_TST_BIT_SET(Var, BitField, Type) (0U != ((Var) & ( Type )(BitField)))
#define DIAG_TST_BIT_RESET(Var, BitField, Type) (0U == ((Var) & ( Type )(BitField)))
/************************28SerEnd***********************/
/******************************************************************************
Function and subFunction
WriteDataByIdentifier
********************************************************************************/
#define WriteDataByIdentifier 0x2E
/******************************************************************************
Function and subFunction
InputOutputControlByIdentifier
********************************************************************************/
#define InputOutputControlByIdentifier 0x2F
#define WhiteColor 0x01
#define BlackColor 0x02
#define RedColor 0x03
#define GreenColor 0x04
#define BlueColor 0x05
#define GaugeSpeed 0x01
#define GaugeTacho 0x02
#define GaugeFuel 0x03
#define GaugeEngCoolantTemp 0x04
#define ZeroPosition 0x00
#define HalfPosition 0x01
#define FullPosition 0x02
#define Clear 0x08
#define Contrl 0x03
#define Resume 0x00
/******************************************************************************
Function and subFunction
RoutineControl
********************************************************************************/
#define RoutineControl 0x31
#define startRoutine 0x01
#define stopRoutine 0x02
#define requestRoutineResults 0x03
#define MaintenanceReset 0X62F1
#define CheckMemory 0x0202
#define UpdateConditionCheck 0x0203
#define ECU_selftest 0xDFF0
#define EraseMemory 0xFF00
#define CheckProgrammingDependencies 0xFF01
#define RoutineCompletedSuccessfully 0x00
#define RoutineCompletedUnsuccessfully 0x05
/******************************************************************************
Function and subFunction
RequestDownload
********************************************************************************/
#define RequestDownload 0X34
#define LengthFormatIdentifier 0x20
#define Download 0x01
/******************************************************************************
Function and subFunction
TransferDataf
********************************************************************************/
#define TransferData 0x36
/******************************************************************************
Function and subFunction
RequestTransferExit
********************************************************************************/
#define RequestTransferExit 0x37
/******************************************************************************
Function and subFunction
TesterPresent
********************************************************************************/
#define TesterPresent 0x3E
/******************************************************************************
Function and subFunction
ControlDTCSetting
********************************************************************************/
#define ControlDTCSetting 0x85
#define DTCSettingOn 0x01
#define DTCSettingOff 0x02
/******************************************************************************
Supported negative response codes
*******************************************************************************/
#define positiveResponse 0x00
#define serviceNotSupported 0x11
#define subFunctionNotSupported 0x12
#define incorrectMessageLength 0x13
#define responseTooLong 0x14
#define conditionsNotCorrect 0x22
#define requestSequenceError 0x24
#define requestOutOfRange 0x31
#define securityAccessDenied 0x33
#define invalidKey 0x35
#define exceedNumberOfAttempts 0x36
#define requiredTimeDelayNotExpired 0x37
#define uploadDownloadNotAccepted 0x70
#define transferDataSuspended 0x71
#define generalProgrammingFailure 0x72
#define wrongBlockSequenceCounter 0x73
#define requestCorrectlyReceivedResponsePending 0x78
#define subfunctionNotSupportinActiveSession 0x7E
#define serviceNotSupportedInActiveSession 0x7F
#define engineIsRunning 0x83
#define engineIsNotRunning 0x84
#define engineRunTimeTooLow 0x85
#define voltageTooHigh 0x92
#define voltageTooLow 0x93
#define noErr 0
#define SRecRangeError 1
#define SRecOddError 2 // S-Record Size Must Be Even
#define FlashProgramError 3 // Flash Programming Error
#define FlashEraseError 4 // Flash Erase Error
#define BadHexData 5 // Bad Hex Data
#define SRecTooLong 6 // S-Record Too Long
#define CheckSumErr 7 // Checksum Error
#define UnknownPartID 8 // Unknown Part ID
#define SWV 0x125// 0x100 = 1.00 software version 软件版本号
#define HWV 0x116 // 0x100 = 1.00 hardware version 硬件版本号
#define BTV 0x103 // 0x100 = 1.00 bootloader version boot程序版本号
#define INTLV 0x125 // 0x100 = 1.00 internal version 内部版本号
#define PROG_Y 0x25 // 0x24 = 2024年, program year
#define PROG_M 0x05 // 0x03 = 3月, program month
#define PROG_D 0x20 // 0x19 = 19日, program day
typedef struct
{
uint32_t Flag;
uint8_t Attemptcnt;
uint8_t RequestSeedCnt;
uint8_t AttemptIIcnt;
uint8_t RequestSeedIICnt;
} Ser27_FlowCtrlCntUnion;
typedef struct
{
uint32_t Flag;
uint8_t DID_F180[6];
uint8_t DID_F187[12];
uint8_t DID_F193[6];
uint8_t DID_F195[6];
uint8_t Filldata[2];
}DiagDFlashData;
extern DiagDFlashData DiagDataForDFlash;
typedef struct
{
uint32_t Flag;
uint32_t APP_STATUS;
}DiagFlag;
extern DiagFlag DiagDataForFlag;
typedef struct
{
uint8_t Para;
uint8_t code;
uint8_t OpCode;
} DiagSendDataNeg;
void UDS_Service_10_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
void UDS_Service_11_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
void UDS_Service_22_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
void UDS_Service_2E_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
void UDS_Service_27_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
void UDS_Service_28_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
void UDS_Service_3E_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
void UDS_Service_85_Indication(uint16_t A_TA_type, uint16_t A_Length, uint8_t *A_Data);
void S3_ServerCNTT(void);
void Randomcnt(void);
extern void DIAG_InitParameter(void);
void GetSeed(void);
void CalculateKey(void);
void CalculateKeyLv11(void);
typedef void (*InitFunction)(void);
/***********Local Functin*************/
uint8_t GetCurrentSession(void);
uint8_t SerXXNRC12Filter(uint8_t insub, const uint8_t *supportsub, uint8_t size, uint8_t *index);
uint8_t SerXXNRC13Filter(uint8_t type, uint16_t serlength, const uint8_t *length, uint8_t index);
uint8_t SerXXNRC22Filter(void);
uint8_t SerXXNRC31DIDFilter(uint16_t inDID, const uint16_t *DIDList, uint8_t size, uint8_t *index);
uint8_t SerXXNRC33Filter(const uint8_t *levelList, uint8_t index);
uint8_t SerXXNRC7EFilter(const uint8_t *SubSupportSession, uint8_t index);
uint8_t SerXXNRC7FFilter(uint8_t SupportSession);
extern void Data_Set_DiagPara(void);
void Write_App_InValid(uint32_t m32);
void Data_Read_DiagPara(void);
void DFlash_init(void);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment