1014 lines
27 KiB
C
Raw Permalink Normal View History

2023-05-05 11:53:54 +08:00
/******************************************************************************
| File Name : CanTp.c
| Description: Manages segmenting of data in the send direction,
| assembling of data in the receive direction,
| and monitoring of the data stream.
|------------------------------------------------------------------------------
| (c) This software is the proprietary of Wenzhou Changjiang AutoMobile (DAMING)
| All rights are reserved by DAMING.
|------------------------------------------------------------------------------
| Initials Name Company
| -------- -------------------- ----------------------------------------
| DAMING
|------------------------------------------------------------------------------
| R E V I S I O N H I S T O R Y
|------------------------------------------------------------------------------
| Date Version Author Description
| ---------- -------- ------ ----------------------------------------
| 2015-9-1 V1.0 LZY
*****************************************************************************/
/*
2024-03-18 15:19:44 +08:00
TP层理论上 500us APP层其他进程的进行
CAN中断太过频繁会造成时间片的偏移 STmin定为5ms
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
(1000 ms)
() ()
(1000 ms)
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
TP层开始接收新的一帧
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
TP层发送报文TP层认为已经响应当前报文( 7878 1S3时间的响应时间 5S)
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
S3 ACTIVE INACTIVE
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
1S
TP层占用10% APP层占用90% 900ms APP层必须将反馈发送至 TP层 DL = 0
APP层超时TP层将中止该帧报文的响应流程
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
78 1S3时间的响应时间 5S
2023-05-05 11:53:54 +08:00
*/
/******************************************************************************
* Description: : Include File Section
******************************************************************************/
//#include "global.h"
#include "CanTp.h"
/******************************************************************************
* Description: : Global Variable Definition Section
******************************************************************************/
/******************************************************************************
* Description: : Static Variable Definition Section
******************************************************************************/
#define DAMING_TRUE DCM_TRUE
#define DAMING_FALSE DCM_FALSE
#define DAMING_ACTIVE DCM_ACTIVE
#define DAMING_INACTIVE DCM_INACTIVE
2024-03-18 15:19:44 +08:00
static uint8 SaCanTp_u_RxSFBuffer[8]; /*单帧buffer*/
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
static TeCanTp_e_TpSts SeCanTp_e_TpSts;/*TP层工作状态*/
2023-05-05 11:53:54 +08:00
static uint8 SeCanTp_u_BS;
static uint8 SeCanTp_u_STmin;
2024-03-18 15:19:44 +08:00
static uint8 SaCanTp_u_RxMFBuffer[CANTP_MFLEN_MAX];/*多帧接收buffer*/
2023-05-05 11:53:54 +08:00
static uint16 SeCanTp_u_RxLen;
2024-03-18 15:19:44 +08:00
static TeCanTp_e_AddrMethod SeCanTp_e_AddrMethod;/*寻址方式*/
2023-05-05 11:53:54 +08:00
static uint16 SeCanTp_u_RxMFDataCnt;
static uint8 SeCanTp_u_LastSN;
static uint16 SeCanTp_u_Timer;
static TeCanTp_e_MFTimerSwitch SeCanTp_e_TimerSwitch;
2024-03-18 15:19:44 +08:00
static TeCanTp_e_TxSts SeCanTp_e_TxSts;/*发送状态*/
2023-05-05 11:53:54 +08:00
static uint8 SaCanTp_u_TxDataBuffer[CANTP_MFLEN_MAX];
static uint8 SaCanTp_u_TxBuffer[8];
static uint16 SeCanTp_u_TxLen;
static uint16 SeCanTp_u_TxMFIndex;
static TeCanTp_e_MFTxSts SeCanTp_e_MFTxSts;
static uint8 SeCanTp_u_MFTxSN;
static uint8 SeCanTp_u_MFTxSpacing;
2024-03-18 15:19:44 +08:00
static uint16 SeCanTp_w_RespTimer; /*反馈计时器*/
2023-05-05 11:53:54 +08:00
static uint8 SeCanTp_u_RespFlag;
/******************************************************************************
* Description: : Static Function Prototype Declaration
*****************************************************************************/
/******************************************************************************
* Description: : Write User Function Code
*****************************************************************************/
/******************************************************************************
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* : TeCanTp_e_AddrMethod* LeCanTp_p_AddrMethod , Addressing Method
2023-05-05 11:53:54 +08:00
uint16* LeCanTp_p_DL , Data Length
uint8* LeCanTp_p_DataAddr array of data pointer
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* : get frame data
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
*****************************************************************************/
TeCanTp_e_RxBufferSts GetCanTp_e_RxBufferSts(uint8* LeCanTp_p_AddrMethod , uint16* LeCanTp_p_DL , uint8** LeCanTp_p_DataAddr)
{
TeCanTp_e_RxBufferSts LeCanTp_e_RxSts;
if(SeCanTp_e_TpSts == CeCanTp_e_SF_Get)
{
SeCanTp_e_TpSts = CeCanTp_e_Answer;
*LeCanTp_p_AddrMethod = SeCanTp_e_AddrMethod;
*LeCanTp_p_DL = SeCanTp_u_RxLen;
*LeCanTp_p_DataAddr = SaCanTp_u_RxSFBuffer;
LeCanTp_e_RxSts = CeCanTp_e_Full;
}
else if(SeCanTp_e_TpSts == CeCanTp_e_MF_Get)
{
SeCanTp_e_TpSts = CeCanTp_e_Answer;
*LeCanTp_p_AddrMethod = SeCanTp_e_AddrMethod;
*LeCanTp_p_DL = SeCanTp_u_RxLen;
*LeCanTp_p_DataAddr = SaCanTp_u_RxMFBuffer;
LeCanTp_e_RxSts = CeCanTp_e_Full;
}
else
{
LeCanTp_e_RxSts = CeCanTp_e_Empty;
}
return LeCanTp_e_RxSts;
}
/******************************************************************************
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* : CanIf调用 TP数据函数
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
*****************************************************************************/
void RxIndicationCanTP_AddrPhy_Message(CanIf_HwHandleType LeCanTp_e_Hrh,
CanIf_CanIdType LeCanTp_e_CanId,
CanIf_CanDlcType LeCanTp_e_CanDlc,
const CanIf_CanDataType LeCanTp_e_CanSduPtr[])
{
uint8 LeCanTp_u_FrameInfo = 0;
uint8 LeCanTp_u_DataIndex;
(void)LeCanTp_e_Hrh;
(void)LeCanTp_e_CanDlc;
if(LeCanTp_e_CanId == CANTP_ADDR_PHY_REQ)
{
LeCanTp_u_FrameInfo = LeCanTp_e_CanSduPtr[0] >> 4;
2024-03-18 15:19:44 +08:00
if((SeCanTp_e_TpSts == CeCanTp_e_SF_Get)/*已接收到单帧*/
||(SeCanTp_e_TpSts == CeCanTp_e_MF_Get)/*已接收到多帧*/
||(SeCanTp_e_TpSts == CeCanTp_e_Answer))/*当前正在响应*/
2023-05-05 11:53:54 +08:00
{
2024-03-18 15:19:44 +08:00
if((SeCanTp_e_TpSts == CeCanTp_e_Answer) && (SeCanTp_e_MFTxSts == CeCanTp_e_TX_FCWaiting))/*反馈阶段 发送多帧数据*/
2023-05-05 11:53:54 +08:00
{
if(LeCanTp_u_FrameInfo == 3)/*FC*/
{
2024-03-18 15:19:44 +08:00
if((LeCanTp_e_CanSduPtr[0]&0x0F) == 0x00)/*FS判断 正常*/
2023-05-05 11:53:54 +08:00
{
SeCanTp_e_MFTxSts = CeCanTp_e_TX_CFGaving;
if(LeCanTp_e_CanSduPtr[1] == 0)
{
SeCanTp_u_BS = CANTP_MFLEN_MAX;
}
else
{
SeCanTp_u_BS = LeCanTp_e_CanSduPtr[1];
}
if(SeCanTp_u_STmin < 0x7FU)
{
SeCanTp_u_STmin = ((LeCanTp_e_CanSduPtr[2]-1)/CANTP_TASK)+1;
}
else
{
SeCanTp_u_STmin = 0;
}
2024-03-18 15:19:44 +08:00
SeCanTp_u_MFTxSpacing = 0;/*开始最小间隔计时*/
2023-05-05 11:53:54 +08:00
SeCanTp_u_Timer = 0;
}
2024-03-18 15:19:44 +08:00
else if((LeCanTp_e_CanSduPtr[0]&0x0F) == 0x01)/*FS判断 等待*/
2023-05-05 11:53:54 +08:00
{
2024-03-18 15:19:44 +08:00
/*等待 清空计时器 再等一个 bs*/
2023-05-05 11:53:54 +08:00
SeCanTp_u_Timer = 0;
}
else
{
2024-03-18 15:19:44 +08:00
/*过载丢帧*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_MFTxSts = CeCanTp_e_TX_Idel;
SeCanTp_e_TpSts = CeCanTp_e_Idel;
SeCanTp_e_TxSts = CeCanTp_e_Tx_Idle;
}
}
}
2024-03-18 15:19:44 +08:00
else{/*丢帧*/}
2023-05-05 11:53:54 +08:00
}
else
{
if(LeCanTp_u_FrameInfo == 0)/*SF*/
{
SeCanTp_u_RxLen = LeCanTp_e_CanSduPtr[0]&0x0f;
if((SeCanTp_u_RxLen < 8) && (SeCanTp_u_RxLen > 0))
{
2024-03-18 15:19:44 +08:00
SeCanTp_e_TpSts = CeCanTp_e_SF_Get;/*单帧接收*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_AddrMethod = CeCanTp_e_Physical;
for(LeCanTp_u_DataIndex = 0;LeCanTp_u_DataIndex < SeCanTp_u_RxLen;LeCanTp_u_DataIndex++)
{
SaCanTp_u_RxSFBuffer[LeCanTp_u_DataIndex] = LeCanTp_e_CanSduPtr[LeCanTp_u_DataIndex+1];
}
SeCanTp_u_RespFlag = DAMING_TRUE;
SeCanTp_w_RespTimer = 1000U;
}
else
2024-03-18 15:19:44 +08:00
{/*丢帧*/}
2023-05-05 11:53:54 +08:00
}
else if(LeCanTp_u_FrameInfo == 1)/*FF*/
{
SeCanTp_u_RxLen =(uint16)(((uint16)(LeCanTp_e_CanSduPtr[0] & 0x0f) << 8) + (uint16)LeCanTp_e_CanSduPtr[1]);
if(SeCanTp_u_RxLen > CANTP_MFLEN_MAX) // add for 15765-2-6.5.2.2
{
2024-03-18 15:19:44 +08:00
/*丢帧*/
/*直接回复过载流控帧*/
2023-05-05 11:53:54 +08:00
SaCanTp_u_TxBuffer[0] = 0x30;
SaCanTp_u_TxBuffer[1] = 2;
SaCanTp_u_TxBuffer[2] = CANTP_RX_STMIN;
2024-03-08 10:25:00 +08:00
SaCanTp_u_TxBuffer[3] = 0xcc;
SaCanTp_u_TxBuffer[4] = 0xcc;
SaCanTp_u_TxBuffer[5] = 0xcc;
SaCanTp_u_TxBuffer[6] = 0xcc;
SaCanTp_u_TxBuffer[7] = 0xcc;
2023-05-05 11:53:54 +08:00
TransmitCanIf_Buffer(CANTP_RESP_CANID,8,SaCanTp_u_TxBuffer);
}
else if(SeCanTp_u_RxLen > 7)
{
SeCanTp_e_AddrMethod = CeCanTp_e_Physical;
2024-03-18 15:19:44 +08:00
/*获得多帧长度*/
2023-05-05 11:53:54 +08:00
SeCanTp_u_RxMFDataCnt = 0;
2024-03-18 15:19:44 +08:00
/*获得前6个数据*/
2023-05-05 11:53:54 +08:00
for(LeCanTp_u_DataIndex = 2;LeCanTp_u_DataIndex < 8;LeCanTp_u_DataIndex++)
{
SaCanTp_u_RxMFBuffer[SeCanTp_u_RxMFDataCnt] = LeCanTp_e_CanSduPtr[LeCanTp_u_DataIndex];
SeCanTp_u_RxMFDataCnt++;
}
SeCanTp_u_LastSN = 1;
2024-03-18 15:19:44 +08:00
/*直接回复流控帧*/
2023-05-05 11:53:54 +08:00
SaCanTp_u_TxBuffer[0] = 0x30;
SaCanTp_u_TxBuffer[1] = 0;
SaCanTp_u_TxBuffer[2] = CANTP_RX_STMIN;
2024-03-08 10:25:00 +08:00
SaCanTp_u_TxBuffer[3] = 0xcc;
SaCanTp_u_TxBuffer[4] = 0xcc;
SaCanTp_u_TxBuffer[5] = 0xcc;
SaCanTp_u_TxBuffer[6] = 0xcc;
SaCanTp_u_TxBuffer[7] = 0xcc;
2023-05-05 11:53:54 +08:00
TransmitCanIf_Buffer(CANTP_RESP_CANID,8,SaCanTp_u_TxBuffer);
2024-03-18 15:19:44 +08:00
SeCanTp_e_TpSts = CeCanTp_e_MF_CFTakein;/*等待接收后续帧*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_TimerSwitch = CeCanTp_e_Cr;
SeCanTp_u_Timer = 0;
}
else
2024-03-18 15:19:44 +08:00
{/*丢帧*/}
2023-05-05 11:53:54 +08:00
}
else if(LeCanTp_u_FrameInfo == 2)/*CF*/
{
if(SeCanTp_e_TpSts == CeCanTp_e_MF_CFTakein)
{
2024-03-18 15:19:44 +08:00
/*检验SN*/
2023-05-05 11:53:54 +08:00
SeCanTp_u_Timer = 0;
if((LeCanTp_e_CanSduPtr[0]&0x0F) == (SeCanTp_u_LastSN & 0x0F))
{
2024-03-18 15:19:44 +08:00
/*获得数据*/
2023-05-05 11:53:54 +08:00
SeCanTp_u_LastSN++;
if((LeCanTp_e_CanDlc < 8)&&(LeCanTp_e_CanDlc >0)&&(SeCanTp_u_RxMFDataCnt < (SeCanTp_u_RxLen - LeCanTp_e_CanDlc)))
{
2024-03-18 15:19:44 +08:00
SeCanTp_e_TpSts = CeCanTp_e_Idel; // 错误的DLC
2023-05-05 11:53:54 +08:00
}
else
{
for(LeCanTp_u_DataIndex = 1;LeCanTp_u_DataIndex < 8;LeCanTp_u_DataIndex++)
{
if(SeCanTp_u_RxMFDataCnt < SeCanTp_u_RxLen)
{
SaCanTp_u_RxMFBuffer[SeCanTp_u_RxMFDataCnt] = LeCanTp_e_CanSduPtr[LeCanTp_u_DataIndex];
SeCanTp_u_RxMFDataCnt++;
}
else
{
2024-03-18 15:19:44 +08:00
/*数据接收完毕*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_TpSts = CeCanTp_e_MF_Get;
SeCanTp_u_RespFlag = DAMING_TRUE;
SeCanTp_w_RespTimer = 1000U;
break;
}
if(SeCanTp_u_RxMFDataCnt == SeCanTp_u_RxLen)
{
2024-03-18 15:19:44 +08:00
/*数据接收完毕*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_TpSts = CeCanTp_e_MF_Get;
SeCanTp_u_RespFlag = DAMING_TRUE;
SeCanTp_w_RespTimer = 1000U;
break;
}
}
}
}
else
{
2024-03-18 15:19:44 +08:00
/*SN错误 丢帧*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_TpSts = CeCanTp_e_Idel;
}
}
else
{
2024-03-18 15:19:44 +08:00
/*当前不是 多帧接受后续帧阶段 丢帧*/
2023-05-05 11:53:54 +08:00
}
}
else
{
2024-03-18 15:19:44 +08:00
/*不接受 流控帧 与 其他状态帧*/
2023-05-05 11:53:54 +08:00
}
}
}
else
{
}
}
/******************************************************************************
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* : CanIf调用 TP数据函数
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
*****************************************************************************/
#if (CANTP_ADDRFUNC_MF_FORBID == DAMING_TRUE)
void RxIndicationCanTP_AddrFunc_Message(CanIf_HwHandleType LeCanTp_e_Hrh,
CanIf_CanIdType LeCanTp_e_CanId,
CanIf_CanDlcType LeCanTp_e_CanDlc,
const CanIf_CanDataType LeCanTp_e_CanSduPtr[])
{
uint8 LeCanTp_u_FrameInfo = 0;
uint8 LeCanTp_u_DataIndex;
(void)LeCanTp_e_Hrh;
(void)LeCanTp_e_CanDlc;
if(LeCanTp_e_CanId == CANTP_ADDR_FUNC_REQ)
{
LeCanTp_u_FrameInfo = LeCanTp_e_CanSduPtr[0] >> 4;
2024-03-18 15:19:44 +08:00
if((SeCanTp_e_TpSts == CeCanTp_e_SF_Get)/*已接收到单帧*/
||(SeCanTp_e_TpSts == CeCanTp_e_MF_Get)/*已接收到多帧*/
||(SeCanTp_e_TpSts == CeCanTp_e_Answer))/*当前正在响应*/
2023-05-05 11:53:54 +08:00
{
2024-03-18 15:19:44 +08:00
if((SeCanTp_e_TpSts == CeCanTp_e_Answer) && (SeCanTp_e_MFTxSts == CeCanTp_e_TX_FCWaiting))/*反馈阶段 发送多帧数据*/
2023-05-05 11:53:54 +08:00
{
if(LeCanTp_u_FrameInfo == 3)/*FC*/
{
2024-03-18 15:19:44 +08:00
if((LeCanTp_e_CanSduPtr[0]&0x0F) == 0x00)/*FS判断 正常*/
2023-05-05 11:53:54 +08:00
{
SeCanTp_e_MFTxSts = CeCanTp_e_TX_CFGaving;
if(LeCanTp_e_CanSduPtr[1] == 0)
{
SeCanTp_u_BS = CANTP_MFLEN_MAX;
}
else
{
SeCanTp_u_BS = LeCanTp_e_CanSduPtr[1];
}
if(SeCanTp_u_STmin < 0x7FU)
{
SeCanTp_u_STmin = ((LeCanTp_e_CanSduPtr[2]-1)/CANTP_TASK)+1;
}
else
{
SeCanTp_u_STmin = 0;
}
2024-03-18 15:19:44 +08:00
SeCanTp_u_MFTxSpacing = 0;/*开始最小间隔计时*/
2023-05-05 11:53:54 +08:00
}
2024-03-18 15:19:44 +08:00
else if((LeCanTp_e_CanSduPtr[0]&0x0F) == 0x01)/*FS判断 等待*/
2023-05-05 11:53:54 +08:00
{
2024-03-18 15:19:44 +08:00
/*等待 清空计时器 再等一个 bs*/
2023-05-05 11:53:54 +08:00
SeCanTp_u_Timer = 0;
}
else
{
2024-03-18 15:19:44 +08:00
/*过载丢帧*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_MFTxSts = CeCanTp_e_TX_Idel;
SeCanTp_e_TpSts = CeCanTp_e_Idel;
SeCanTp_e_TxSts = CeCanTp_e_Tx_Idle;
}
}
}
}
else
{
if(LeCanTp_u_FrameInfo == 0)/*SF*/
{
SeCanTp_u_RxLen = LeCanTp_e_CanSduPtr[0]&0x0f;
if((SeCanTp_u_RxLen < 8) && (SeCanTp_u_RxLen > 0))
{
2024-03-18 15:19:44 +08:00
SeCanTp_e_TpSts = CeCanTp_e_SF_Get;/*单帧接收*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_AddrMethod = CeCanTp_e_Functional;
for(LeCanTp_u_DataIndex = 0;LeCanTp_u_DataIndex < SeCanTp_u_RxLen;LeCanTp_u_DataIndex++)
{
SaCanTp_u_RxSFBuffer[LeCanTp_u_DataIndex] = LeCanTp_e_CanSduPtr[LeCanTp_u_DataIndex+1];
}
SeCanTp_u_RespFlag = DAMING_TRUE;
SeCanTp_w_RespTimer = 1000U;
}
else
2024-03-18 15:19:44 +08:00
{/*丢帧*/}
2023-05-05 11:53:54 +08:00
}
else if(LeCanTp_u_FrameInfo == 1)/*FF*/
{
SeCanTp_u_RxLen =(uint16)(((uint16)(LeCanTp_e_CanSduPtr[0] & 0x0f) << 8) + (uint16)LeCanTp_e_CanSduPtr[1]);
if(SeCanTp_u_RxLen > 7)
{
SeCanTp_e_AddrMethod = CeCanTp_e_Functional;
2024-03-18 15:19:44 +08:00
/*获得多帧长度*/
2023-05-05 11:53:54 +08:00
//SeCanTp_u_RxLen =(uint16)(((uint16)(LeCanTp_e_CanSduPtr[0] & 0x0f) << 8) + (uint16)LeCanTp_e_CanSduPtr[1]);
SeCanTp_u_RxMFDataCnt = 0;
2024-03-18 15:19:44 +08:00
/*获得前6个数据*/
2023-05-05 11:53:54 +08:00
for(LeCanTp_u_DataIndex = 2;LeCanTp_u_DataIndex < 8;LeCanTp_u_DataIndex++)
{
SaCanTp_u_RxMFBuffer[SeCanTp_u_RxMFDataCnt] = LeCanTp_e_CanSduPtr[LeCanTp_u_DataIndex];
SeCanTp_u_RxMFDataCnt++;
}
SeCanTp_u_LastSN = 1;
2024-03-18 15:19:44 +08:00
/*直接回复流控帧*/
2023-05-05 11:53:54 +08:00
SaCanTp_u_TxBuffer[0] = 0x30;
SaCanTp_u_TxBuffer[1] = 0;
SaCanTp_u_TxBuffer[2] = CANTP_RX_STMIN;
TransmitCanIf_Buffer(CANTP_RESP_CANID,8,SaCanTp_u_TxBuffer);
2024-03-18 15:19:44 +08:00
SeCanTp_e_TpSts = CeCanTp_e_MF_CFTakein;/*等待接收后续帧*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_TimerSwitch = CeCanTp_e_Cr;
SeCanTp_u_Timer = 0;
}
else
2024-03-18 15:19:44 +08:00
{/*丢帧*/}
2023-05-05 11:53:54 +08:00
}
else if(LeCanTp_u_FrameInfo == 2)/*CF*/
{
if(SeCanTp_e_TpSts == CeCanTp_e_MF_CFTakein)
{
2024-03-18 15:19:44 +08:00
/*检验SN*/
2023-05-05 11:53:54 +08:00
if((LeCanTp_e_CanSduPtr[0]&0x0F) == ((SeCanTp_u_LastSN+1)&0x0F))
{
2024-03-18 15:19:44 +08:00
/*获得数据*/
2023-05-05 11:53:54 +08:00
SeCanTp_u_LastSN++;
for(LeCanTp_u_DataIndex = 1;LeCanTp_u_DataIndex < 8;LeCanTp_u_DataIndex++)
{
if(SeCanTp_u_RxMFDataCnt < SeCanTp_u_RxLen)
{
SaCanTp_u_RxMFBuffer[SeCanTp_u_RxMFDataCnt] = LeCanTp_e_CanSduPtr[LeCanTp_u_DataIndex];
SeCanTp_u_RxMFDataCnt++;
}
else
{
2024-03-18 15:19:44 +08:00
/*数据接收完毕*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_TpSts = CeCanTp_e_MF_Get;
SeCanTp_u_RespFlag = DAMING_TRUE;
SeCanTp_w_RespTimer = 1000U;
break;
}
if(SeCanTp_u_RxMFDataCnt == SeCanTp_u_RxLen)
{
2024-03-18 15:19:44 +08:00
/*数据接收完毕*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_TpSts = CeCanTp_e_MF_Get;
SeCanTp_u_RespFlag = DAMING_TRUE;
SeCanTp_w_RespTimer = 1000U;
break;
}
}
}
else
{
2024-03-18 15:19:44 +08:00
/*SN错误 丢帧*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_TpSts = CeCanTp_e_Idel;
}
}
else
{
2024-03-18 15:19:44 +08:00
/*当前不是 多帧接受后续帧阶段 丢帧*/
2023-05-05 11:53:54 +08:00
}
}
else
{
2024-03-18 15:19:44 +08:00
/*不接受 流控帧 与 其他状态帧*/
2023-05-05 11:53:54 +08:00
}
}
}
else
{}
}
#else
void RxIndicationCanTP_AddrFunc_Message(CanIf_HwHandleType LeCanTp_e_Hrh,
CanIf_CanIdType LeCanTp_e_CanId,
CanIf_CanDlcType LeCanTp_e_CanDlc,
const CanIf_CanDataType LeCanTp_e_CanSduPtr[])
{
uint8 LeCanTp_u_FrameInfo = 0;
uint8 LeCanTp_u_DataIndex;
(void)LeCanTp_e_Hrh;
(void)LeCanTp_e_CanDlc;
if(LeCanTp_e_CanId == CANTP_ADDR_FUNC_REQ)
{
LeCanTp_u_FrameInfo = LeCanTp_e_CanSduPtr[0] >> 4;
2024-03-18 15:19:44 +08:00
if((SeCanTp_e_TpSts == CeCanTp_e_SF_Get)/*已接收到单帧*/
||(SeCanTp_e_TpSts == CeCanTp_e_MF_Get)/*已接收到多帧*/
||(SeCanTp_e_TpSts == CeCanTp_e_Answer))/*当前正在响应*/
2023-05-05 11:53:54 +08:00
{
2024-03-18 15:19:44 +08:00
/*丢帧*/
2023-05-05 11:53:54 +08:00
}
else
{
if(LeCanTp_u_FrameInfo == 0)/*SF*/
{
SeCanTp_u_RxLen = LeCanTp_e_CanSduPtr[0]&0x0f;
if(SeCanTp_u_RxLen < 8)
{
2024-03-18 15:19:44 +08:00
SeCanTp_e_TpSts = CeCanTp_e_SF_Get;/*单帧接收*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_AddrMethod = CeCanTp_e_Functional;
for(LeCanTp_u_DataIndex = 0;LeCanTp_u_DataIndex < SeCanTp_u_RxLen;LeCanTp_u_DataIndex++)
{
SaCanTp_u_RxSFBuffer[LeCanTp_u_DataIndex] = LeCanTp_e_CanSduPtr[LeCanTp_u_DataIndex+1];
}
SeCanTp_u_RespFlag = DAMING_TRUE;
SeCanTp_w_RespTimer = 1000U;
}
else
2024-03-18 15:19:44 +08:00
{/*丢帧*/}
2023-05-05 11:53:54 +08:00
}
}
}
else
{}
}
#endif
/******************************************************************************
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
*****************************************************************************/
TeCanTp_e_TxResult SetCanTp_TxBuffer(uint16 LeCanTp_p_DL , const uint8 LeCanTp_p_DataAddr[])
{
TeCanTp_e_TxResult LeCanTp_e_TxResult;
uint16 LeCanTp_s_Index;
2024-03-18 15:19:44 +08:00
/*只有在反馈阶段 可以发送数据*/
2023-05-05 11:53:54 +08:00
if ((SeCanTp_e_TpSts == CeCanTp_e_Answer) && (SeCanTp_e_TxSts == CeCanTp_e_Tx_Idle))
{
SeCanTp_u_RespFlag = DAMING_FALSE;
SeCanTp_w_RespTimer = 1000U;
if(LeCanTp_p_DL == 0)
{
2024-03-18 15:19:44 +08:00
/*该报文无反馈*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_TpSts = CeCanTp_e_Idel;
}
else if(LeCanTp_p_DL < 8)
{
2024-03-18 15:19:44 +08:00
/*单帧 直接发送*/
2023-05-05 11:53:54 +08:00
SaCanTp_u_TxBuffer[0] = (uint8)LeCanTp_p_DL;
for(LeCanTp_s_Index = 1; LeCanTp_s_Index < 8; LeCanTp_s_Index++)
{
2023-12-18 23:54:30 +08:00
SaCanTp_u_TxBuffer[LeCanTp_s_Index] = 0xCC;
2023-05-05 11:53:54 +08:00
}
for(LeCanTp_s_Index = 0; LeCanTp_s_Index < LeCanTp_p_DL; LeCanTp_s_Index++)
{
SaCanTp_u_TxBuffer[LeCanTp_s_Index + 1] = LeCanTp_p_DataAddr[LeCanTp_s_Index];
}
TransmitCanIf_Buffer(CANTP_RESP_CANID,8,SaCanTp_u_TxBuffer);
SeCanTp_e_TpSts = CeCanTp_e_Idel;
}
else
{
2024-03-18 15:19:44 +08:00
/*多帧报文*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_TxSts = CeCanTp_e_Tx_Busy;
2024-03-18 15:19:44 +08:00
/*数据提取*/
2023-05-05 11:53:54 +08:00
for(LeCanTp_s_Index = 0; LeCanTp_s_Index < LeCanTp_p_DL; LeCanTp_s_Index++)
{
SaCanTp_u_TxDataBuffer[LeCanTp_s_Index] = LeCanTp_p_DataAddr[LeCanTp_s_Index];
}
SeCanTp_u_TxLen = LeCanTp_p_DL;
2024-03-18 15:19:44 +08:00
/*发送首帧*/
2023-05-05 11:53:54 +08:00
SaCanTp_u_TxBuffer[0] = 0x10 + (uint8)(LeCanTp_p_DL >> 8);
SaCanTp_u_TxBuffer[1] = (uint8)(LeCanTp_p_DL & 0x00ff);
for(SeCanTp_u_TxMFIndex = 0; SeCanTp_u_TxMFIndex < 6; SeCanTp_u_TxMFIndex++)
{
SaCanTp_u_TxBuffer[SeCanTp_u_TxMFIndex+2] = SaCanTp_u_TxDataBuffer[SeCanTp_u_TxMFIndex];
}
TransmitCanIf_Buffer(CANTP_RESP_CANID,8,SaCanTp_u_TxBuffer);
SeCanTp_e_TimerSwitch = CeCanTp_e_Bs;
SeCanTp_u_Timer = 0;
SeCanTp_u_MFTxSN = 1;
SeCanTp_e_MFTxSts = CeCanTp_e_TX_FCWaiting;
}
LeCanTp_e_TxResult = CeCanTp_e_Tx_Succeed;
}
else
{
LeCanTp_e_TxResult = CeCanTp_e_Tx_Wrong;
}
return LeCanTp_e_TxResult;
}
/******************************************************************************
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* : 78
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
*****************************************************************************/
TeCanTp_e_TxResult SetCanTp_Tx_78Neg(uint16 LeCanTp_p_DL , const uint8 LeCanTp_p_DataAddr[])
{
TeCanTp_e_TxResult LeCanTp_e_TxResult;
uint8 LeCanTp_u_Index;
2024-03-18 15:19:44 +08:00
/*只有在反馈阶段 可以发送数据*/
2023-05-05 11:53:54 +08:00
if ((SeCanTp_e_TpSts == CeCanTp_e_Answer) && (SeCanTp_e_TxSts == CeCanTp_e_Tx_Idle))
{
if(LeCanTp_p_DL < 8)
{
2024-03-18 15:19:44 +08:00
/*单帧 直接发送*/
2023-05-05 11:53:54 +08:00
SaCanTp_u_TxBuffer[0] = (uint8)LeCanTp_p_DL;
for(LeCanTp_u_Index = 1; LeCanTp_u_Index < 8; LeCanTp_u_Index++)
{
2023-12-18 23:54:30 +08:00
SaCanTp_u_TxBuffer[LeCanTp_u_Index] = 0xCC;
2023-05-05 11:53:54 +08:00
}
for(LeCanTp_u_Index = 0; LeCanTp_u_Index < LeCanTp_p_DL; LeCanTp_u_Index++)
{
SaCanTp_u_TxBuffer[LeCanTp_u_Index + 1] = LeCanTp_p_DataAddr[LeCanTp_u_Index];
}
TransmitCanIf_Buffer(CANTP_RESP_CANID,8,SaCanTp_u_TxBuffer);
LeCanTp_e_TxResult = CeCanTp_e_Tx_Succeed;
SeCanTp_w_RespTimer = 0u;
}
else
{
LeCanTp_e_TxResult = CeCanTp_e_Tx_Wrong;
}
}
else
{
LeCanTp_e_TxResult = CeCanTp_e_Tx_Wrong;
}
return LeCanTp_e_TxResult;
}
/******************************************************************************
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
*****************************************************************************/
void InitCanTp_Parameter(void)
{
uint8 LeCanTp_u_InitIndex;
for(LeCanTp_u_InitIndex = 0; LeCanTp_u_InitIndex<8 ; LeCanTp_u_InitIndex++)
{
2024-03-18 15:19:44 +08:00
SaCanTp_u_RxSFBuffer[LeCanTp_u_InitIndex] = 0;/*单帧buffer*/
2023-05-05 11:53:54 +08:00
SaCanTp_u_TxBuffer[LeCanTp_u_InitIndex] = 0;
SaCanTp_u_TxDataBuffer[LeCanTp_u_InitIndex] = 0;
2024-03-18 15:19:44 +08:00
SaCanTp_u_RxMFBuffer[LeCanTp_u_InitIndex] = 0;/*多帧接收buffer*/
2023-05-05 11:53:54 +08:00
}
2024-03-18 15:19:44 +08:00
SeCanTp_e_TpSts = CeCanTp_e_Idel;/*TP层工作状态*/
2023-05-05 11:53:54 +08:00
SeCanTp_u_BS = 0;
SeCanTp_u_STmin = 10;
SeCanTp_u_RxLen = 0;
2024-03-18 15:19:44 +08:00
SeCanTp_e_AddrMethod = CeCanTp_e_Physical;/*寻址方式*/
2023-05-05 11:53:54 +08:00
SeCanTp_u_RxMFDataCnt = 0;
SeCanTp_u_LastSN = 0;
SeCanTp_u_Timer = 0;
SeCanTp_e_TimerSwitch = 0;
2024-03-18 15:19:44 +08:00
SeCanTp_e_TxSts = CeCanTp_e_Tx_Idle;/*发送状态*/
2023-05-05 11:53:54 +08:00
SeCanTp_u_TxLen = 0;
SeCanTp_u_TxMFIndex = 0;
SeCanTp_e_MFTxSts = CeCanTp_e_TX_Idel;
SeCanTp_u_MFTxSN = 0;
SeCanTp_u_MFTxSpacing = 0;
2024-03-18 15:19:44 +08:00
SeCanTp_w_RespTimer = 1000U; /*反馈计时器*/
2023-05-05 11:53:54 +08:00
SeCanTp_u_RespFlag = DAMING_FALSE;
}
/******************************************************************************
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
*****************************************************************************/
void MngCanTp_MainFunction(void)
{
uint8 LeCanTp_u_Index;
2024-03-18 15:19:44 +08:00
if(SeCanTp_e_TpSts == CeCanTp_e_MF_CFTakein)/*超时*/
2023-05-05 11:53:54 +08:00
{
if(SeCanTp_e_TimerSwitch == CeCanTp_e_Cr)
{
if(SeCanTp_u_Timer > CANTP_CR_TIMEOUT)
{
2024-03-18 15:19:44 +08:00
/*超时*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_TpSts = CeCanTp_e_Idel;
}
else
{
SeCanTp_u_Timer++;
}
}
else
{
SeCanTp_e_TimerSwitch = CeCanTp_e_Cr;
SeCanTp_u_Timer = 0;
}
}
else
{
}
2024-03-18 15:19:44 +08:00
if(SeCanTp_u_RespFlag == DAMING_TRUE)/*APP反馈超时*/
2023-05-05 11:53:54 +08:00
{
if(SeCanTp_w_RespTimer > CANTP_APP_RESP_78NEG)
{
2024-03-18 15:19:44 +08:00
/*超时*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_TpSts = CeCanTp_e_Idel;
SeCanTp_u_RespFlag = DAMING_FALSE;
SeCanTp_w_RespTimer = 1000U;
}
else
{
SeCanTp_w_RespTimer++;
}
}
else
{
SeCanTp_w_RespTimer = 1000U;
}
if(SeCanTp_e_TpSts == CeCanTp_e_Answer)
{
2024-03-18 15:19:44 +08:00
if(SeCanTp_e_MFTxSts == CeCanTp_e_TX_FCWaiting)/*FC等待超时*/
2023-05-05 11:53:54 +08:00
{
if(SeCanTp_e_TimerSwitch == CeCanTp_e_Bs)
{
if(SeCanTp_u_Timer > CANTP_BS_TIMEOUT)
{
2024-03-18 15:19:44 +08:00
/*超时*/
2023-05-05 11:53:54 +08:00
SeCanTp_e_MFTxSts = CeCanTp_e_TX_Idel;
SeCanTp_e_TpSts = CeCanTp_e_Idel;
SeCanTp_e_TxSts = CeCanTp_e_Tx_Idle;
}
else
{
SeCanTp_u_Timer++;
}
}
else
{
SeCanTp_e_TimerSwitch = CeCanTp_e_Bs;
SeCanTp_u_Timer = 0;
}
}
else
{}
2024-03-18 15:19:44 +08:00
if(SeCanTp_e_MFTxSts == CeCanTp_e_TX_CFGaving)/*发送*/
2023-05-05 11:53:54 +08:00
{
/*CF*/
SeCanTp_u_MFTxSpacing++;
if(SeCanTp_u_BS > 0)
{
if(SeCanTp_u_MFTxSpacing > SeCanTp_u_STmin)
{
2024-03-18 15:19:44 +08:00
SeCanTp_u_MFTxSpacing = 0;//修复stmin少1ms
2023-05-05 11:53:54 +08:00
if(SeCanTp_u_TxMFIndex < (SeCanTp_u_TxLen-7))
{
SaCanTp_u_TxBuffer[0] = 0x20 + SeCanTp_u_MFTxSN;
for(LeCanTp_u_Index = 0; LeCanTp_u_Index < 7; LeCanTp_u_Index++)
{
SaCanTp_u_TxBuffer[LeCanTp_u_Index + 1] = SaCanTp_u_TxDataBuffer[SeCanTp_u_TxMFIndex++];
}
TransmitCanIf_Buffer(CANTP_RESP_CANID,8,SaCanTp_u_TxBuffer);
SeCanTp_u_MFTxSN += 1;
SeCanTp_u_MFTxSN &= 0x0f;
if(SeCanTp_u_BS == 1u)
{
2024-03-18 15:19:44 +08:00
SeCanTp_e_MFTxSts = CeCanTp_e_TX_FCWaiting;/*等流控帧*/
2023-05-05 11:53:54 +08:00
SeCanTp_u_Timer = 0;
SeCanTp_e_TimerSwitch = CeCanTp_e_Bs;
}
else
{
SeCanTp_u_BS--;
}
}
else
{
2024-03-18 15:19:44 +08:00
/*最后一帧*/
2023-05-05 11:53:54 +08:00
uint8 LeCanTp_u_Cnt = SeCanTp_u_TxLen - SeCanTp_u_TxMFIndex;
SaCanTp_u_TxBuffer[0] = 0x20 + SeCanTp_u_MFTxSN;
for(LeCanTp_u_Index = 1; LeCanTp_u_Index < 8; LeCanTp_u_Index++)
{
2023-12-18 23:54:30 +08:00
SaCanTp_u_TxBuffer[LeCanTp_u_Index] = 0xCC;
2023-05-05 11:53:54 +08:00
}
for(LeCanTp_u_Index = 0; LeCanTp_u_Index < LeCanTp_u_Cnt; LeCanTp_u_Index++)
{
SaCanTp_u_TxBuffer[LeCanTp_u_Index + 1] = SaCanTp_u_TxDataBuffer[SeCanTp_u_TxMFIndex++];
}
TransmitCanIf_Buffer(CANTP_RESP_CANID,8,SaCanTp_u_TxBuffer);
SeCanTp_e_MFTxSts = CeCanTp_e_TX_Idel;
SeCanTp_e_TpSts = CeCanTp_e_Idel;
SeCanTp_e_TxSts = CeCanTp_e_Tx_Idle;
SeCanTp_u_BS = 0;
}
}
else
{}
}
else
{
2024-03-18 15:19:44 +08:00
SeCanTp_e_MFTxSts = CeCanTp_e_TX_FCWaiting;/*等流控帧*/
2023-05-05 11:53:54 +08:00
SeCanTp_u_Timer = 0;
SeCanTp_e_TimerSwitch = CeCanTp_e_Bs;
}
}
else
{
}
}
else
{
}
}
/******************************************************************************
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* : S3
S3 DAMING_ACTIVE DAMING_INACTIVE
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
2024-03-18 15:19:44 +08:00
* :
2023-05-05 11:53:54 +08:00
*****************************************************************************/
uint8 GetCanTp_u_S3Sts(void)
{
uint8 LeCanTp_u_Ret;
if(SeCanTp_e_TpSts == CeCanTp_e_Idel)
{
LeCanTp_u_Ret = DAMING_ACTIVE;
}
else
{
LeCanTp_u_Ret = DAMING_INACTIVE;
}
return LeCanTp_u_Ret;
}