📄 lin_driver.c
字号:
//----------------------------------------------------------------------------
// There must stay a ( unsigned char) in front of the data-bytes otherwise
// the translation from unsigned char to unsigned integer isn't able.
//****************************************************************************
unsigned char LIN_ucChecksum(stLIN_MESSAGE *pstLocalHelp)
{
unsigned int uiLocalHelp;
// two bytes must be added at least
uiLocalHelp = (unsigned char)pstLocalHelp->ucDataByte[0] +
(unsigned char)pstLocalHelp->ucDataByte[1];
if(uiLocalHelp > 0xFF) // if addition is over 0xFF then algorithm,
uiLocalHelp -= 0xFF; // see above
// if the Number of datas is more then 2
switch(LIN_ucGetNOD(pstLocalHelp->ucId))
{
case 2:
;
break;
case 4: // add two data-bytes more
uiLocalHelp += ( unsigned char)pstLocalHelp->ucDataByte[2];
if(uiLocalHelp > 0xFF)
uiLocalHelp -= 0xFF;
uiLocalHelp += ( unsigned char)pstLocalHelp->ucDataByte[3];
if(uiLocalHelp > 0xFF)
uiLocalHelp -= 0xFF;
break;
case 8: // add six data-bytes more
uiLocalHelp += ( unsigned char)pstLocalHelp->ucDataByte[2];
if(uiLocalHelp > 0xFF)
uiLocalHelp -= 0xFF;
uiLocalHelp += ( unsigned char)pstLocalHelp->ucDataByte[3];
if(uiLocalHelp > 0xFF)
uiLocalHelp -= 0xFF;
uiLocalHelp += ( unsigned char)pstLocalHelp->ucDataByte[4];
if(uiLocalHelp > 0xFF)
uiLocalHelp -= 0xFF;
uiLocalHelp += ( unsigned char)pstLocalHelp->ucDataByte[5];
if(uiLocalHelp > 0xFF)
uiLocalHelp -= 0xFF;
uiLocalHelp += ( unsigned char)pstLocalHelp->ucDataByte[6];
if(uiLocalHelp > 0xFF)
uiLocalHelp -= 0xFF;
uiLocalHelp += ( unsigned char)pstLocalHelp->ucDataByte[7];
if(uiLocalHelp > 0xFF)
uiLocalHelp -= 0xFF;
break;
default:
uiLocalHelp = 0;
}
// the result stays in an integer and is going to return an integer, but
// the recipient of this value expects an unsigned char.
return(uiLocalHelp); // return the inverted modulo-256 checksum
}
//****************************************************************************
// USER CODE END: unsigned char LIN_ucChecksum(stLIN_MESSAGE *pstLocalHelp)
//****************************************************************************
// @Function: void LIN_vCopy_SendData(unsigned char* pucLocalData)
//
//----------------------------------------------------------------------------
// @Description
// this function saves all requested data from the given start address
// to the LIN-Transceiver-Buffer and sends the first byte instantly
//
//----------------------------------------------------------------------------
// @Returnvalue void
//----------------------------------------------------------------------------
// @Parameters address of the first data byte to send
//----------------------------------------------------------------------------
// @Date 05/19/2000
//
//****************************************************************************
//----------------------------------------------------------------------------
// @Global Variables
// stLinTransceiveBuffer[] : The LIN-Transceive-Buffer, this buffer is
// used by the LIN-Functions to send and receive messages. If a message
// should be sent then all data and Id must be saved in this buffer first.
// And if a message has been received, the ID and the data will be saved
// in this buffer, too.
//----------------------------------------------------------------------------
// @AppNote
// This function should be called from within the User-Call-Back-Function:
// USER_vUCB_TxdId_Received(...)
// This function copies all data which should be sent in the LIN-Trans-
// ceiver Buffer.
// The ID is already standing in the stLinTransceiveBuffer[].ucId got by
// the message header before this function has been called,
//----------------------------------------------------------------------------
//****************************************************************************
void LIN_vCopy_SendData(unsigned char* pucLocalData)
{
unsigned char ucLocCoun = 0;
// get the Number Of Data
while(ucLocCoun < stLinTransceiveBuffer[BUFFER_1].ucNOD)
{
// save next byte in the transceiver-buffer
stLinTransceiveBuffer[BUFFER_1].ucDataByte[ucLocCoun] = \
*(pucLocalData + ucLocCoun);// here ucLocCoun is used as offset to
// the first data-byte given by address
ucLocCoun++;
}
// send first byte, the next byte will be transmitted after receiving this
// byte (in the Rxd-Interrupt function)
ASC_TRANSMIT_BUFFER_REGISTER = \
stLinTransceiveBuffer[BUFFER_1].ucDataByte[0];
}
//****************************************************************************
// USER CODE END: void LIN_vCopy_SendData(unsigned char* pucLocalData)
//****************************************************************************
// @Function: void LIN_vCopy_ReceiveData(unsigned char* ucLocalData)
//
//----------------------------------------------------------------------------
// @Description
// this function copies all received data from the LIN-Transceiver-Buffer
// to the USER-LIN-BUFFER. The user application has to provide the address
// of the first byte in the USER-LIN-BUFFER.
//
//----------------------------------------------------------------------------
// @Returnvalue
//----------------------------------------------------------------------------
// @Parameters address of the first data byte, where the data should be
// stored in the USER-LIN-BUFFER
//----------------------------------------------------------------------------
// @Date 05/22/2000
//
//****************************************************************************
//----------------------------------------------------------------------------
// @Global Variables
// stLinTransceiveBuffer[] : The LIN-Transceive-Buffer, this buffer is
// used by the LIN-Functions to send and receive messages. If a message
// should be sent then all data and Id must be saved in this buffer first.
// And if a message has been received, the ID and the data will be saved
// in this buffer, too.
//----------------------------------------------------------------------------
// @AppNote
// This function should be called from within the User-Call-Back-Function:
// USER_vUCB_RxdId_Received(...)
// The ID is already standing in the stLinTransceiveBuffer[].ucId got by
// the message header (ID-Field receiving) before this function has been
// called.
//----------------------------------------------------------------------------
//****************************************************************************
void LIN_vCopy_ReceiveData(unsigned char* pucLocalData)
{
// saves received byte in the LIN-receive-buffer
unsigned char ucLocCoun = 0;
// get the Number Of Data
while(ucLocCoun < stLinTransceiveBuffer[BUFFER_1].ucNOD)
{
// copy next data from LIN-Transceiverbuffer to the
// USER-LIN-BUFFER
*(pucLocalData + ucLocCoun) = \
stLinTransceiveBuffer[BUFFER_1].ucDataByte[ucLocCoun];
// next data-byte
ucLocCoun++ ;
}
//return (ucLocCoun);
}
//****************************************************************************
// USER CODE END: void LIN_vCopy_ReceiveData(unsigned char* ucLocalData)
//****************************************************************************
// @Function void LIN_vInitASC(void)
//
//----------------------------------------------------------------------------
// @Description This function initializes the ASC1 component. It effects all
// necessary configurations of the SFR, depending on the
// selected operating mode. The configuration determines
// whether the ASC interrupts are to be released, and the
// priority of the released interrupt.
//----------------------------------------------------------------------------
// @Returnvalue none
//
//----------------------------------------------------------------------------
// @Parameters none
//
//----------------------------------------------------------------------------
// @Date 08/25/2000 13:01:22
//
//****************************************************************************
void LIN_vInitASC(void)
{
LIN_PORT_REGISTER |= LIN_TXD_BIT;
LIN_PORT_DIRECTION_REGISTER |= LIN_TXD_BIT;
// 8-bit data asynchronous operation
// one stop bit
// ignore framing error
// ignore overrun error
// receiver enabled
ASC_CONTROL_REGISTER = ASC_MODE_VALUE;
// ASC1 baudrate generator register
/// baudrate depends on selection LIN_hInitNode.H
ASC_BAUDRATE_REGISTER = BAUDRATE_ASC;
// receive interrupt Priority
ASC_RECEIVE_IRQ_CONTROL_REGISTER = ASC_RECEIVE_PRIORITY_VALUE;
// enable Receive Interrupt
ASC_RECEIVE_IRQ_CONTROL_REGISTER |= ASC_RECEIVE_IRQ_ENABLE;
}
//****************************************************************************
// USER CODE END: void LIN_vInitASC(void)
//****************************************************************************
// @Function void LIN_vInitTimer(void)
//
//----------------------------------------------------------------------------
// @Description
// this function initiate the timer
//----------------------------------------------------------------------------
// @Returnvalue void
//
//----------------------------------------------------------------------------
// @Parameters void
//
//----------------------------------------------------------------------------
// @Date 04/05/2000
//
//****************************************************************************
//----------------------------------------------------------------------------
// @Global Variables
//
//----------------------------------------------------------------------------
// @AppNote
// The timer has following settings by f_cpu=20MHz
// Timer-Period = 3,36sec
// Timer-Resolution = 51,2usec
// Timer-Prescaler = 1024
// The Values are written to the init-header-file LIN_hInitNode.H
//
//----------------------------------------------------------------------------
// The Baud-Rate of the LIN-Net should be 19,2 kbaud that means a bittime
// of 52,08usec.
// If the resolution would be 800nsec then a bittime of 52usec would be
// possible but in that way the timer couldn't reach the
// "bus-idle-time-out"-time = 25.000 bittime
//****************************************************************************
void LIN_vInitTimer(void)
{
CC_TIMER_CONTROL_REGISTER |= BITTIME_LIN; // Tx: resol.
CC_TIMER_REGISTER = TIMEBASE_LIN; // Time base = max
// Reload Value = max like base-time
CC_TIMER_RELOAD_REGISTER = TIMEBASE_LIN;
}
//****************************************************************************
// USER CODE END: LIN_vInitTimer(void)
//****************************************************************************
// @Function void LIN_vInitCapCom(void)
//
//----------------------------------------------------------------------------
// @Description
// This function will be called to initialize the CapCom-Registers
//----------------------------------------------------------------------------
// @Returnvalue void
//
//----------------------------------------------------------------------------
// @Parameters void
//
//----------------------------------------------------------------------------
// @Date 5/30/2000
//
//****************************************************************************
//----------------------------------------------------------------------------
// @Global Variables
//
//----------------------------------------------------------------------------
// @AppNote
//
//----------------------------------------------------------------------------
//
//****************************************************************************
void LIN_vInitCapCom(void)
{
// Capture/Compare-functions
CC_MODE_REGISTER &= ~CC_MODE_DISABLE; // reset CapCom-Mode
CC_IRQ_CONTROL_REGISTER = CC_PRIORITY_VALUE;
// set compare mode 0
CC_MODE_REGISTER |= CC_COMPARE_MODE_0;
}
//****************************************************************************
// USER CODE END: void LIN_vInitCapCom(void)
//****************************************************************************
// @Function void LIN_vDefaultCondition(void)
//
//----------------------------------------------------------------------------
// @Description
// this function sets the node in the default condition
// after a send or receive action
//----------------------------------------------------------------------------
// @Returnvalue void
//
//----------------------------------------------------------------------------
// @Parameters void
//
//----------------------------------------------------------------------------
// @Date 08/25/2000
//
//****************************************************************************
//----------------------------------------------------------------------------
// @Global Variables
//
// uiBusIdleTimeOutBuffer:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -