📄 bdlc.c
字号:
{
BackRxBuffer = &RX_BUFF; /* Point to 0x0700 */
BackRxBuffer->u8Bk_BuffPayLoad = EMPTY;
BackRxBuffer->u8BufferPayLoad = EMPTY;
BackRxBuffer->u8DataReady = FALSE; /* Clear Rx Buffer ready flag */
}
}
UINT8 u8fCheckBufferPriority(void)
{
UINT8 pu8Buff = 0, u8HighPriority = 0;
RETRANSMIT_BUFF->pu8CmdPtr = RETRANSMIT_BUFF->u8LastCmdPrior;
for(pu8Buff = RETRANSMIT_BUFF->u8LastCmdPrior ;pu8Buff < (BKTX_BUFF_SIZE - 1) ;pu8Buff++)
{
if(RETRANSMIT_BUFF->u8Command[++RETRANSMIT_BUFF->pu8CmdPtr] > NULL)
{
u8HighPriority = RETRANSMIT_BUFF->u8Command[RETRANSMIT_BUFF->pu8CmdPtr];
}else
{
u8HighPriority = RETRANSMIT_BUFF->u8Command[RETRANSMIT_BUFF->u8LastCmdPrior];
RETRANSMIT_BUFF->pu8CmdPtr--;
}
}
return(u8HighPriority); /* Return High Priority Cmd */
}
void vfnClearRetransmitBuff(void)
{
UINT8 pu8Buff = 0;
for(pu8Buff = BKTX_BUFF_SIZE;pu8Buff > 1;pu8Buff--)
{
RETRANSMIT_BUFF->u8Command[pu8Buff - 1]= EMPTY;
}
}
void vfnClearAllRxBuffers(void)
{
UINT8 pu8Buff = 0, *u8BuffPtr = 0;
BackRxBuffer = &BACK_RX_BUFF;
for(pu8Buff = DATA_BUFFER_SIZE;pu8Buff > 0;pu8Buff--)
{
RxBuffer->pu8RxData[pu8Buff - 1] = EMPTY;
BackRxBuffer->pu8RxData[pu8Buff - 1] = EMPTY;
}
BackRxBuffer = &RX_BUFF;
}
/*****************************************************************************/
UINT8 CalculateRateSelection(UINT32 BusClock)
{
if(BusClock >= 20000000){ /* Bus Clock >= 20MHz */
return ((UINT8)((BusClock >> 20) + 1)); /* return the calculated value
that must be stored in DLCBRSR
register.
*/
}else{
return((UINT8)(BusClock >> 20)); /* return the calculated value
that must be stored in DLCBRSR
register.
*/
}
}
/******************************************************************************
* LoopBack Test Function *
******************************************************************************/
void vfnBDLC_LoopBackTest(UINT8 LoopMode)
{
/** Stores the Test Status when a message has been sent */
UINT8 TestStatusByte = 0;
DLCBCR2 |= (LoopMode);
EnableBDLCModule; /* BDLC module is enabled */
//u8fnBDLCTransmit(TestMsg, SizeTestMsg);
}
/*******************************************************************************
* TRANSMIT FUNCTION *
*******************************************************************************/
UINT8 u8fnBDLCTransmit(void)
{
/** This variable tells in which position of the frame we're */
int u16CmdCount = 0;
BDLCStatusFlags.TxFlag = TRUE; /* Transmit in progress */
TxFrameLenght = TransmitBuffer.u8CmdSize; /* Saves Command size */
BDLCOutBufferPtr = TransmitBuffer.pu8DataBuffer; /* Store pointer */
TxBuffer =(UINT8) *BDLCOutBufferPtr++; /* Store char to the transmitter register */
return (FrameStatus = TRUE);
}
/******************************************************************************
* ERROR HANDLING ROUTINE
******************************************************************************/
void vfnBDLCErrorHandling(UINT8 ErrorSource)
{
if(ErrorSource == LOA)
{
sBDLCErrFlags.LoaErr = TRUE; /* Set Lost Of Arbitration flag */
u8RxPointer = NULL;
if(BDLCStatusFlags.TxFlag == TRUE) /* Check if the error was generated during Tx */
{
vfnErrDo_s();
}
}else if(ErrorSource == CRC_ERROR)
{
sBDLCErrFlags.CrcErr = TRUE; /* Set CRC error flag */
u8RxPointer = NULL;
IgnoreMessage;
if(BDLCStatusFlags.TxFlag == TRUE) /* Check if the error was generated during Tx */
{
vfnErrDo_s();
}
}else if(ErrorSource == INV_SYMBOL)
{
sBDLCErrFlags.InvSymErr = TRUE; /* Set Invalid Symbol error flag */
u8RxPointer = NULL;
IgnoreMessage;
if(BDLCStatusFlags.TxFlag == TRUE) /* Check if the error was generated during Tx */
{
vfnErrDo_s();
}
}
}
void vfnErrDo_s(void)
{
BDLCStatusFlags.ReTransmit = TRUE; /* Package must be retransmited */
RETRANSMIT_BUFF->u8PendingCmds++;
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
/******************************************************************************/
/**
* \brief BDLC Transmit and Receive interrupt Vector. All TX and RX\n
* are handled in this interupt routine.
* \author R01160
* \param void
* \return void
*/
/******************************************************************************/
interrupt void BDLC_Isr (void)
{
u8ReceiveCmd = DLCBSVR;
u8Data = DLCBDR;
vfnBDLCErrorHandling(u8ReceiveCmd); /* Check any possible error during Tx/Rx */
if(u8ReceiveCmd == RxDataReady) /* has an RX interrupt arrives? */
{
BDLCStatusFlags.IsAlive = TRUE; /* J1850 Connection is Alive */
BackRxBuffer->pu8RxData[u8RxPointer++] = u8Data; /* Saves Receive data into Buffer */
if(u8RxPointer == 2){
BDLCStatusFlags.RxFrameReady = FALSE; /* Command NOT ready to process */
}
}else if(u8ReceiveCmd == TxRegEmpty) { /* Tx data register empty */
if(TxCount < (TxFrameLenght-1)) { /* If there are bytes to transmit */
TxBuffer = (UINT8)*(BDLCOutBufferPtr++); /* Store byte to the transmitter register */
TxCount++;
}else {
CompleteMessageTransmitted; /* Set Transmit end of data bit */
TxCount = 0; /* Increase number of transmitted bytes */
}
}else if(u8ReceiveCmd == EndOfFrame)
{
if(BDLCStatusFlags.TxFlag)
{
BDLCStatusFlags.TxFrameOut = TRUE; /* Sent Frame WITHOUT problems */
BDLCStatusFlags.TxFlag = FALSE; /* Non-transmition in progress */
}else /* Check if data is not from TX */
{
BackRxBuffer->u8DataReady = DATA_READY; /* Complete Frame ready to process */
BDLCStatusFlags.RxFrameReady = TRUE; /* Command ready to process */
BDLCStatusFlags.IsAlive = FALSE;
u8RxPointer = 0; /* Reset Buffer Pointer */
}
}
}
#pragma CODE_SEG DEFAULT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -