📄 can.c
字号:
//****************************************************************************
// @Module TwinCAN Module (CAN)
// @Filename CAN.C
// @Project TwinCAN.dav
//----------------------------------------------------------------------------
// @Controller Infineon XC164CS-16F20
//
// @Compiler Keil
//
// @Codegenerator 2.8
//
// @Description This file contains functions that use the CAN module.
//
//----------------------------------------------------------------------------
// @Date 2007-5-25 16:53:14
//
//****************************************************************************
// USER CODE BEGIN (CAN_General,1)
// USER CODE END
//****************************************************************************
// @Project Includes
//****************************************************************************
#include "MAIN.H"
// USER CODE BEGIN (CAN_General,2)
// USER CODE END
//****************************************************************************
// @Macros
//****************************************************************************
// USER CODE BEGIN (CAN_General,3)
// USER CODE END
//****************************************************************************
// @Defines
//****************************************************************************
// Structure for a single TwinCAN object
// A total of 31 such object structures exists
struct stCanObj
{
ubyte ubData[8]; // Message Data 0..7
ulong ulCANAR; // Arbitration Register
ulong ulCANAMR; // Acceptance Mask Register
uword uwMSGCTR; // Message Control Register
uword uwCounter; // Frame Counter
uword uwMSGCFG; // Message Configuration Register
uword uwINP; // Interrupt Node Pointer
uword uwCANFCR; // FIFO / Gateway Control Register
uword uwCANPTR; // FIFO Pointer
ulong ulReserved; // Reserved
};
#define CAN_HWOBJ ((struct stCanObj volatile far *) 0x200300)
// USER CODE BEGIN (CAN_General,4)
// USER CODE END
//****************************************************************************
// @Typedefs
//****************************************************************************
// USER CODE BEGIN (CAN_General,5)
// USER CODE END
//****************************************************************************
// @Imported Global Variables
//****************************************************************************
// USER CODE BEGIN (CAN_General,6)
// USER CODE END
//****************************************************************************
// @Global Variables
//****************************************************************************
// USER CODE BEGIN (CAN_General,7)
// USER CODE END
//****************************************************************************
// @External Prototypes
//****************************************************************************
// USER CODE BEGIN (CAN_General,8)
// USER CODE END
//****************************************************************************
// @Prototypes Of Local Functions
//****************************************************************************
// USER CODE BEGIN (CAN_General,9)
// USER CODE END
//****************************************************************************
// @Function void CAN_vInit(void)
//
//----------------------------------------------------------------------------
// @Description This is the initialization function of the CAN function
// library. It is assumed that the SFRs used by this library
// are in its reset state.
//
//----------------------------------------------------------------------------
// @Returnvalue None
//
//----------------------------------------------------------------------------
// @Parameters None
//
//----------------------------------------------------------------------------
// @Date 2007-5-25
//
//****************************************************************************
// USER CODE BEGIN (Init,1)
// USER CODE END
void CAN_vInit(void)
{
// USER CODE BEGIN (Init,2)
// USER CODE END
/// -----------------------------------------------------------------------
/// Configuration of CAN Node A:
/// -----------------------------------------------------------------------
/// General Configuration of the Node A:
/// - set INIT and CCE
/// - enable interrupt generation when a message transfer is completed
/// - transmit / receive OK interrupt node pointer: TwinCAN SRN 0
CAN_ACR = 0x0045; // load global control register
CAN_AGINP = 0x0000; // load global interrupt node pointer
// register
/// Configuration of the Node A Error Counter:
/// - the error warning threshold value (warning level) is 96
CAN_AECNTH = 0x0060; // load error counter register high
/// Configuration of the used CAN Port Pins:
/// - P4.5 is used for CAN Interface Input (RXDCA)
/// - P4.6 is used for CAN Interface Output (TXDCA)
ALTSEL0P4 |= 0x0040; // select alternate output function
DP4 = (DP4 & ~(uword)0x0040) | 0x0040; //set direction register
/// Configuration of the Node A Baud Rate:
/// - required baud rate = 250.000 kbaud
/// - real baud rate = 250.000 kbaud
/// - sample point = 60.00 %
/// - there are 5 time quanta before sample point
/// - there are 4 time quanta after sample point
/// - the (re)synchronization jump width is 2 time quanta
CAN_ABTRL = 0x3447; // load bit timing register low
CAN_ABTRH = 0x0000; // load bit timing register high
CAN_AFCRL = 0x0000; // load frame counter timing register low
CAN_AFCRH = 0x0000; // load frame counter timing register high
/// -----------------------------------------------------------------------
/// Configuration of CAN Node B:
/// -----------------------------------------------------------------------
/// General Configuration of the Node B:
/// - set INIT and CCE
/// - enable interrupt generation when a message transfer is completed
/// - transmit / receive OK interrupt node pointer: TwinCAN SRN 1
CAN_BCR = 0x0045; // load global control register
CAN_BGINP = 0x0100; // load global interrupt node pointer
// register
/// Configuration of the Node B Error Counter:
/// - the error warning threshold value (warning level) is 96
CAN_BECNTH = 0x0060; // load error counter register high
/// Configuration of the used CAN Port Pins:
/// - P4.4 is used for CAN Interface Input (RXDCB)
/// - P4.7 is used for CAN Interface Output (TXDCB)
ALTSEL0P4 |= 0x0080; // select alternate output function
DP4 = (DP4 & ~(uword)0x0080) | 0x0080; //set direction register
/// Configuration of the Node B Baud Rate:
/// - required baud rate = 250.000 kbaud
/// - real baud rate = 250.000 kbaud
/// - sample point = 60.00 %
/// - there are 5 time quanta before sample point
/// - there are 4 time quanta after sample point
/// - the (re)synchronization jump width is 2 time quanta
CAN_BBTRL = 0x3447; // load bit timing register low
CAN_BBTRH = 0x0000; // load bit timing register high
CAN_BFCRL = 0x0000; // load frame counter timing register low
CAN_BFCRH = 0x0000; // load frame counter timing register high
/// -----------------------------------------------------------------------
/// Configuration of the CAN Message Objects 0 - 31:
/// -----------------------------------------------------------------------
/// -----------------------------------------------------------------------
/// Configuration of Message Object 0:
/// -----------------------------------------------------------------------
/// - message object 0 is valid
/// - enable transmit interrupt; bit INTPND is set after successfull
/// transmission of a frame
/// - message object is used as transmit object
/// - standard 11-bit identifier
/// - 8 valid data bytes
/// - this message object works with CAN node A
/// - remote monitoring is disabled
/// - transmit interrupt node pointer: TwinCAN SRN 0
CAN_MSGCFGL0 = 0x0088; // load message configuration register low
CAN_MSGCFGH0 = 0x0000; // load message configuration register high
/// - acceptance mask 11-bit: 0x7FF
/// - identifier 11-bit: 0x123
CAN_MSGAMRL0 = 0xFFFF; // load acceptance mask register low
CAN_MSGAMRH0 = 0xFFFF; // load acceptance mask register high
CAN_MSGARL0 = 0x0000; // load arbitration register low
CAN_MSGARH0 = 0x048C; // load arbitration register high
CAN_MSGDRL00 = 0x0100; // load data register 0 low
CAN_MSGDRH00 = 0x0302; // load data register 0 high
CAN_MSGDRL04 = 0x0504; // load data register 4 low
CAN_MSGDRH04 = 0x0706; // load data register 4 high
/// - functionality of standard message object
CAN_MSGFGCRL0 = 0x0000; // load FIFO/gateway control register low
CAN_MSGFGCRH0 = 0x0000; // load FIFO/gateway control register high
CAN_MSGCTRH0 = 0x0000; // load message control register high
CAN_MSGCTRL0 = 0x55A5; // load message control register low
/// -----------------------------------------------------------------------
/// Configuration of Message Object 1:
/// -----------------------------------------------------------------------
/// - message object 1 is valid
/// - enable receive interrupt; bit INTPND is set after successfull
/// reception of a frame
/// - message object is used as receive object
/// - standard 11-bit identifier
/// - 8 valid data bytes
/// - this message object works with CAN node B
/// - remote monitoring is disabled
/// - receive interrupt node pointer: TwinCAN SRN 1
CAN_MSGCFGL1 = 0x0082; // load message configuration register low
CAN_MSGCFGH1 = 0x0001; // load message configuration register high
/// - acceptance mask 11-bit: 0x7FF
/// - identifier 11-bit: 0x123
CAN_MSGAMRL1 = 0xFFFF; // load acceptance mask register low
CAN_MSGAMRH1 = 0xFFFF; // load acceptance mask register high
CAN_MSGARL1 = 0x0000; // load arbitration register low
CAN_MSGARH1 = 0x048C; // load arbitration register high
CAN_MSGDRL10 = 0x0000; // load data register 0 low
CAN_MSGDRH10 = 0x0000; // load data register 0 high
CAN_MSGDRL14 = 0x0000; // load data register 4 low
CAN_MSGDRH14 = 0x0000; // load data register 4 high
/// - functionality of standard message object
CAN_MSGFGCRL1 = 0x0000; // load FIFO/gateway control register low
CAN_MSGFGCRH1 = 0x0001; // load FIFO/gateway control register high
CAN_MSGCTRH1 = 0x0000; // load message control register high
CAN_MSGCTRL1 = 0x5599; // load message control register low
/// -----------------------------------------------------------------------
/// Configuration of Service Request Nodes 0 - 7:
/// -----------------------------------------------------------------------
/// SRN0 service request node configuration:
/// - SRN0 interrupt priority level (ILVL) = 12
/// - SRN0 interrupt group level (GLVL) = 0
/// - SRN0 group priority extension (GPX) = 0
CAN_0IC = 0x0070;
/// SRN1 service request node configuration:
/// - SRN1 interrupt priority level (ILVL) = 11
/// - SRN1 interrupt group level (GLVL) = 1
/// - SRN1 group priority extension (GPX) = 0
CAN_1IC = 0x006D;
// USER CODE BEGIN (Init,3)
// USER CODE END
CAN_PISEL = 0x0000; // load port input select register
// -----------------------------------------------------------------------
// Start the CAN Nodes:
// -----------------------------------------------------------------------
CAN_ACR &= ~(uword)0x0041; // reset INIT and CCE
CAN_BCR &= ~(uword)0x0041; // reset INIT and CCE
// USER CODE BEGIN (Init,4)
// USER CODE END
} // End of function CAN_vInit
//****************************************************************************
// @Function void CAN_vGetMsgObj(ubyte ubObjNr, TCAN_SWObj *pstObj)
//
//----------------------------------------------------------------------------
// @Description This function fills the forwarded SW message object with
// the content of the chosen HW message object.
//
// The structure of the SW message object is defined in the
// header file CAN.H (see TCAN_SWObj).
//
//----------------------------------------------------------------------------
// @Returnvalue None
//
//----------------------------------------------------------------------------
// @Parameters ubObjNr:
// Number of the message object to be read (0-31)
// @Parameters *pstObj:
// Pointer on a message object to be filled by this function
//
//----------------------------------------------------------------------------
// @Date 2007-5-25
//
//****************************************************************************
// USER CODE BEGIN (GetMsgObj,1)
// USER CODE END
void CAN_vGetMsgObj(ubyte ubObjNr, TCAN_SWObj *pstObj)
{
ubyte i;
for(i = 0; i < (CAN_HWOBJ[ubObjNr].uwMSGCFG & 0x00f0) >> 4; i++)
{
pstObj->ubData[i] = CAN_HWOBJ[ubObjNr].ubData[i];
}
if(CAN_HWOBJ[ubObjNr].uwMSGCFG & 0x04) // extended identifier
{
pstObj->ulID = CAN_HWOBJ[ubObjNr].ulCANAR;
pstObj->ulMask = CAN_HWOBJ[ubObjNr].ulCANAMR;
}
else // standard identifier
{
pstObj->ulID = CAN_HWOBJ[ubObjNr].ulCANAR >> 18;
pstObj->ulMask = CAN_HWOBJ[ubObjNr].ulCANAMR >> 18;
}
pstObj->uwCounter = CAN_HWOBJ[ubObjNr].uwCounter;
pstObj->uwMsgCfg = CAN_HWOBJ[ubObjNr].uwMSGCFG;
} // End of function CAN_vGetMsgObj
//****************************************************************************
// @Function ubyte CAN_ubRequestMsgObj(ubyte ubObjNr)
//
//----------------------------------------------------------------------------
// @Description If a TRANSMIT OBJECT is to be reconfigured it must first be
// accessed. The access to the transmit object is exclusive.
// This function checks whether the choosen message object is
// still executing a transmit request, or if the object can be
// accessed exclusively.
// After the message object is reserved, it can be
// reconfigured by using the function CAN_vConfigMsgObj or
// CAN_vLoadData.
// Both functions enable access to the object for the CAN
// controller.
// By calling the function CAN_vTransmit transfering of data
// is started.
//
//----------------------------------------------------------------------------
// @Returnvalue 0 message object is busy (a transfer is active), else 1
//
//----------------------------------------------------------------------------
// @Parameters ubObjNr:
// Number of the message object (0-31)
//
//----------------------------------------------------------------------------
// @Date 2007-5-25
//
//****************************************************************************
// USER CODE BEGIN (RequestMsgObj,1)
// USER CODE END
ubyte CAN_ubRequestMsgObj(ubyte ubObjNr)
{
ubyte ubReturn;
ubReturn = 0;
if((CAN_HWOBJ[ubObjNr].uwMSGCTR & 0x3000) == 0x1000) // if reset TXRQ
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -