//**************************************************************************** // @Module TwinCAN Module (CAN) // @Filename CAN.C // @Project CAN.dav //---------------------------------------------------------------------------- // @Controller Infineon XC164CM-8F20 // // @Compiler Keil // // @Codegenerator 1.1 // // @Description This file contains functions that use the CAN module. // //---------------------------------------------------------------------------- // @Date 15.01.2008 21:54:12 // //**************************************************************************** // 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) extern i; // 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 reset state. // //---------------------------------------------------------------------------- // @Returnvalue None // //---------------------------------------------------------------------------- // @Parameters None // //---------------------------------------------------------------------------- // @Date 15.01.2008 // //**************************************************************************** // 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 CAN_ACR = 0x0041; // load global control register /// ----------------------------------------------------------------------- /// Configuration of CAN Node B: /// ----------------------------------------------------------------------- /// General Configuration of the Node B: /// - set INIT and CCE CAN_BCR = 0x0041; // 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 = 100,000 kbaud /// - real baud rate = 100,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 = 0x3453; // 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 /// - message object is used as transmit object /// - standard 11-bit identifier /// - 1 valid data bytes /// - this message object works with CAN node B /// - remote monitoring is disabled CAN_MSGCFGL0 = 0x001A; // load message configuration register low CAN_MSGCFGH0 = 0x0000; // load message configuration register high /// - acceptance mask 11-bit: 0x7FF /// - identifier 11-bit: 0x001 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 = 0x0004; // load arbitration register high CAN_MSGDRL00 = 0x00AA; // load data register 0 low CAN_MSGDRH00 = 0x0000; // load data register 0 high CAN_MSGDRL04 = 0x0000; // load data register 4 low CAN_MSGDRH04 = 0x0000; // 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 = 0x5595; // 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 /// - 0 valid data bytes /// - this message object works with CAN node B /// - remote monitoring is disabled /// - receive interrupt node pointer: TwinCAN SRN 0 CAN_MSGCFGL1 = 0x0002; // load message configuration register low CAN_MSGCFGH1 = 0x0000; // load message configuration register high /// - acceptance mask 11-bit: 0x7FF /// - identifier 11-bit: 0x000 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 = 0x0000; // 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) = 8 /// - SRN0 interrupt group level (GLVL) = 0 /// - SRN0 group priority extension (GPX) = 0 CAN_0IC = 0x0060; // USER CODE BEGIN (Init,3) // USER CODE END CAN_PISEL = 0x0008; // load port input select register // ----------------------------------------------------------------------- // Start the CAN Nodes: // ----------------------------------------------------------------------- CAN_BCR &= ~(uword)0x0041; // reset INIT and CCE // USER CODE BEGIN (Init,4) // USER CODE END } // End of function CAN_vInit //**************************************************************************** // @Function void CAN_vTransmit(ubyte ubObjNr) // //---------------------------------------------------------------------------- // @Description This function triggers the CAN controller to send the // selected message. // If the selected message object is a TRANSMIT OBJECT then // this function triggers the sending of a data frame. If // however the selected message object is a RECEIVE OBJECT // this function triggers the sending of a remote frame. // //---------------------------------------------------------------------------- // @Returnvalue None // //---------------------------------------------------------------------------- // @Parameters ubObjNr: // Number of the message object (0-31) // //---------------------------------------------------------------------------- // @Date 15.01.2008 // //**************************************************************************** // USER CODE BEGIN (Transmit,1) // USER CODE END void CAN_vTransmit(ubyte ubObjNr) { CAN_HWOBJ[ubObjNr].uwMSGCTR = 0xe7ff; // set TXRQ, reset CPUUPD } // End of function CAN_vTransmit //**************************************************************************** // @Function void CAN_viSRN0(void) // //---------------------------------------------------------------------------- // @Description This is the interrupt service routine for the Service // Request Node 0 of the TwinCAN module. // //---------------------------------------------------------------------------- // @Returnvalue None // //---------------------------------------------------------------------------- // @Parameters None // //---------------------------------------------------------------------------- // @Date 15.01.2008 // //**************************************************************************** // USER CODE BEGIN (SRN0,1) // USER CODE END void CAN_viSRN0(void) interrupt CAN_SRN0INT using RB_LEVEL8 { // USER CODE BEGIN (SRN0,2) // USER CODE END while((( ((ulong)CAN_RXIPNDH << 16) + CAN_RXIPNDL) & 0x00000002)) { // message object 1 interrupt if((CAN_HWOBJ[1].uwMSGCTR & 0x0003) == 0x0002) // if INTPND { if(CAN_RXIPNDL & CAN_RXIPNDL_RXIPND1) // message object 1 receive interrupt { if((CAN_HWOBJ[1].uwMSGCTR & 0x0300) == 0x0200) // if NEWDAT is set { if ((CAN_HWOBJ[1].uwMSGCTR & 0x0c00) == 0x0800) // if MSGLST is set { // Indicates that the CAN controller has stored a new // message into this object, while NEWDAT was still set, // ie. the previously stored message is lost. CAN_HWOBJ[1].uwMSGCTR = 0xf7ff; // reset MSGLST // USER CODE BEGIN (SRN0_OBJ1,1) // USER CODE END } else { // The CAN controller has stored a new message // into this object. // USER CODE BEGIN (SRN0_OBJ1,2) // USER CODE END } CAN_HWOBJ[1].uwMSGCTR = 0xfdff; // reset NEWDAT } } // End of RXIPND1 CAN_HWOBJ[1].uwMSGCTR = 0xfffd; // reset INTPND // USER CODE BEGIN (SRN0_OBJ1,6) // USER CODE END } // USER CODE BEGIN (SRN0,3) // USER CODE END } // End of while() // USER CODE BEGIN (SRN0,7) i=0; // USER CODE END } // End of function CAN_viSRN0 // USER CODE BEGIN (CAN_General,10) // USER CODE END