📄 can.c
字号:
//****************************************************************************
// @Module TwinCAN Module (CAN)
// @Filename CAN.C
// @Project 164.dav
//----------------------------------------------------------------------------
// @Controller Infineon XC164CM-4F40
//
// @Compiler Keil
//
// @Codegenerator 1.1
//
// @Description This file contains functions that use the CAN module.
//
//----------------------------------------------------------------------------
// @Date 2007-5-2 13:51:07
//
//****************************************************************************
// 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
//****************************************************************************
static ubyte aubFIFOWritePtr[16];
static ubyte aubFIFOReadPtr[16];
// 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 reset state.
//
//----------------------------------------------------------------------------
// @Returnvalue None
//
//----------------------------------------------------------------------------
// @Parameters None
//
//----------------------------------------------------------------------------
// @Date 2007-5-2
//
//****************************************************************************
// USER CODE BEGIN (Init,1)
// USER CODE END
void CAN_vInit(void)
{
ubyte i;
// USER CODE BEGIN (Init,2)
// USER CODE END
/// -----------------------------------------------------------------------
/// Configuration of CAN Node A:
/// -----------------------------------------------------------------------
/// General Configuration of the Node A:
/// - set INIT and CCE
/// - enable the CAN analyzer mode; monitoring CAN bus traffic without
/// participating in the CAN bus protocol itself
/// - enable interrupt generation when a message transfer is completed
/// - transmit / receive OK interrupt node pointer: TwinCAN SRN 0
/// - enable interrupt generation on a change of bit BOFF or EWARN
/// - error (BusOff and ErrorWarning) interrupt node pointer: TwinCAN SRN 0
/// - enable interrupt generation on setting an error code in bit field LEC
/// - last error code interrupt node pointer: TwinCAN SRN 0
CAN_ACR = 0x00DD; // 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:
/// - P9.3 is used for CAN Interface Output (TXDCA)
/// - P9.2 is used for CAN Interface Input (RXDCA)
ALTSEL0P9 |= 0x0008; // select alternate output function
ALTSEL1P9 |= 0x0008; // select alternate output function
DP9 = (DP9 & ~(uword)0x0008) | 0x0008; //set direction register
/// Configuration of the Node A Baud Rate:
/// - required baud rate = 400.000 kbaud
/// - real baud rate = 400.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 = 0x3449; // 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 the CAN analyzer mode; monitoring CAN bus traffic without
/// participating in the CAN bus protocol itself
/// - enable interrupt generation when a message transfer is completed
/// - transmit / receive OK interrupt node pointer: TwinCAN SRN 0
/// - enable interrupt generation on a change of bit BOFF or EWARN
/// - error (BusOff and ErrorWarning) interrupt node pointer: TwinCAN SRN 0
/// - enable interrupt generation on setting an error code in bit field LEC
/// - last error code interrupt node pointer: TwinCAN SRN 0
CAN_BCR = 0x00DD; // load global control register
CAN_BGINP = 0x0000; // 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:
/// - P9.0 is used for CAN Interface Input (RXDCB)
/// - P9.1 is used for CAN Interface Output (TXDCB)
ALTSEL0P9 |= 0x0002; // select alternate output function
ALTSEL1P9 |= 0x0002; // select alternate output function
DP9 = (DP9 & ~(uword)0x0002) | 0x0002; //set direction register
/// Configuration of the Node B Baud Rate:
/// - required baud rate = 400.000 kbaud
/// - real baud rate = 400.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 = 0x3449; // 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 Service Request Nodes 0 - 7:
/// -----------------------------------------------------------------------
/// -----------------------------------------------------------------------
/// Initialization of the FIFO Pointer:
/// -----------------------------------------------------------------------
for (i = 0; i < 16; i++)
{
aubFIFOWritePtr[i] = i * 2;
aubFIFOReadPtr[i] = i * 2;
}
// USER CODE BEGIN (Init,3)
// USER CODE END
CAN_PISEL = 0x000B; // 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-2
//
//****************************************************************************
// 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];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -