CAN4 register Access

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

CAN4 register Access

2,430 Views
rehan_khan
Contributor III

Hi,

 

I am using sample CAN example to test FlexCAN. I have modified the code for CAN4 instead of CAN0. When I try to configure CAN4 registers It doesn't allow me to do that whereas CAN0 and CAN1 can be configured. Below is my code

 

/* main.c: FlexCAN program */

/* Description: Transmit one message from FlexCAN 0 buf. 0 to FlexCAN C buf. 1 */

/* Rev 0.1 Jan 16, 2006 S.Mihalik, Copyright Freescale, 2006. All Rights Reserved */

/* Rev 0.2 Jun 6 2006 SM - changed Flexcan A to C & enabled 64 msg buffers */

/* Rev 0.3 Jun 15 2006 SM - 1. Made globals uninitialized */

/*         2. RecieveMsg function:  read CANx_TIMER, removed setting buffer's CODE*/

/*         3. added idle loop code for smoother Nexus trace */

/*         4. modified for newer Freescale header files (r 16) */

/* Rev 0.4 Aug 11 2006 SM - Removed redundant CAN_A.MCR init */

/* Rev 0.5 Jan 31 2007 SM - Removed other redundant CAN_C.MCR init */

/* Rev 0.6 Mar 08 2007 SM - Corrected init of MBs- cleared 64 MBs, instead of 63 */

/* Rev 0.7 Jul 20 2007 SM - Changes for MPC5510 */

/* Rev 0.8 May 15 2008 SM - Changes for new header file symbols */

/* Rev 0.9 May 22 2009 SM - Changes for MPC56xxB/P/S */

/* Rev 1.0 Jul 10 2009 SM - Simplified, cleared CAN Msg Buf flag by writing to reg */

/*   not bit, increased Tx pads slew rate, changed RxCODE, RxLENGTH, dummy data types*/

/*   & init receiving CAN first to allow CAN bus sync time before receiving 1st msg*/

/* Rev 1.1 Mar 14 2010 SM - modified initModesAndClock, updated header file */

/*  NOTE!! structure canbuf_t in header file modified to allow byte addressing*/

 

 

#include "MPC5607B.h" /* Use proper include file */

 

 

uint32_t RxCODE;              /* Received message buffer code */

uint32_t RxID;                 /* Received message ID */

uint32_t RxLENGTH;            /* Recieved message number of data bytes */

uint8_t  RxDATA[8];            /* Received message data string*/

uint32_t RxTIMESTAMP;          /* Received message time */                        

 

 

void initModesAndClks(void) {

  ME.MER.R = 0x0000001D;          /* Enable DRUN, RUN0, SAFE, RESET modes */

                                  /* Initialize PLL before turning it on: */

/* Use 1 of the next 2 lines depending on crystal frequency: */

  CGM.FMPLL_CR.R = 0x02400100;    /* 8 MHz xtal: Set PLL0 to 64 MHz */  

/*CGM.FMPLL_CR.R = 0x12400100;*/  /* 40 MHz xtal: Set PLL0 to 64 MHz */  

  ME.RUN[0].R = 0x001F0074;       /* RUN0 cfg: 16MHzIRCON,OSC0ON,PLL0ON,syclk=PLL */

  ME.RUNPC[1].R = 0x00000010;  /* Peri. Cfg. 1 settings: only run in RUN0 mode */

  ME.PCTL[34].R = 0x01;           /* MPC56xxB/P/S FlexCAN4: select ME.RUNPC[1] */

  //ME.PCTL[17].R = 0x01;         /* MPC56xxB/S FlexCAN1:  select ME.RUNPC[1] */

  ME.PCTL[68].R = 0x01;           /* MPC56xxB/S SIUL:  select ME.RUNPC[1] */

                                  /* Mode Transition to enter RUN0 mode: */

  ME.MCTL.R = 0x40005AF0;         /* Enter RUN0 Mode & Key */

  ME.MCTL.R = 0x4000A50F;         /* Enter RUN0 Mode & Inverted Key */ 

  while (ME.GS.B.S_MTRANS) {}     /* Wait for mode transition to complete */   

                                  /* Note: could wait here using timer and/or I_TC IRQ */

  while(ME.GS.B.S_CURRENTMODE != 4) {} /* Verify RUN0 is the current mode */

}

 

 

void initPeriClkGen(void) {

  CGM.SC_DC0.R = 0x80;     /* MPC56xxB/S: Enable peri set 2 sysclk divided by 1 */

}

 

 

void disableWatchdog(void) {

  SWT.SR.R = 0x0000c520;     /* Write keys to clear soft lock bit */

  SWT.SR.R = 0x0000d928;

  SWT.CR.R = 0x8000010A;     /* Clear watchdog enable (WEN) */

}       

 

 

void initCAN_1 (void) {

  uint8_t   i;

 

  CAN_1.MCR.R = 0x5000003F;       /* Put in Freeze Mode & enable all 64 msg bufs */

  CAN_1.CR.R = 0x04DB0006;        /* Configure for 8MHz OSC, 100KHz bit time */

  for (i=0; i<64; i++) {

    CAN_1.BUF[i].CS.B.CODE = 0;   /* Inactivate all message buffers */

  }

  CAN_1.BUF[4].CS.B.IDE = 0;      /* MB 4 will look for a standard ID */

  CAN_1.BUF[4].ID.B.STD_ID = 555; /* MB 4 will look for ID = 555 */

  CAN_1.BUF[4].CS.B.CODE = 4;     /* MB 4 set to RX EMPTY */

  CAN_1.RXGMASK.R = 0x1FFFFFFF;   /* Global acceptance mask */

  SIU.PCR[42].R = 0x0624;         /* MPC56xxB: Config port C10 as CAN1TX, open drain */

  SIU.PCR[35].R = 0x0100;         /* MPC56xxB: Configure port C3 as CAN1RX */

  SIU.PSMI[0].R = 0x00;           /* MPC56xxB: Select PCR 35 for CAN1RX Input */

  CAN_1.MCR.R = 0x0000003F;       /* Negate FlexCAN 1 halt state for  64 MB */

}

 

 

void initCAN_4(void) {

  uint8_t   i;

 

  CAN_4.MCR.R = 0x5000003F;       /* Put in Freeze Mode & enable all 64 msg bufs */

  CAN_4.CR.R = 0x04DB0006;        /* Configure for 8MHz OSC, 100KHz bit time */

  for (i=0; i<64; i++) {

   CAN_4.BUF[i].CS.B.CODE = 0;   /* Inactivate all message buffers */

  }

  CAN_4.BUF[0].CS.B.CODE = 8;     /* Message Buffer 0 set to TX INACTIVE */

  SIU.PCR[37].R = 0x0A24;         /* MPC56xxB: Config port B0 as CAN0TX, open drain */

  //SIU.PCR[17].R = 0x0100;         /* MPC56xxB: Configure port B1 as CAN0RX */

  CAN_4.MCR.R = 0x0000003F;       /* Negate FlexCAN 0 halt state for 64 MB */

}

 

 

void TransmitMsg (void) {

  uint8_t i;

                                   /* Assumption:  Message buffer CODE is INACTIVE */

  const uint8_t TxData[] = {"Hello"};  /* Transmit string*/

  CAN_4.BUF[0].CS.B.IDE = 0;           /* Use standard ID length */

  CAN_4.BUF[0].ID.B.STD_ID = 555;      /* Transmit ID is 555 */

  CAN_4.BUF[0].CS.B.RTR = 0;           /* Data frame, not remote Tx request frame */

  CAN_4.BUF[0].CS.B.LENGTH = sizeof(TxData) -1 ; /* # bytes to transmit w/o null */

  for (i=0; i<sizeof(TxData); i++) {

    CAN_4.BUF[0].DATA.B[i] = TxData[i];      /* Data to be transmitted */

  }

  CAN_4.BUF[0].CS.B.SRR = 1;           /* Tx frame (not req'd for standard frame)*/

  CAN_4.BUF[0].CS.B.CODE =0xC;         /* Activate msg. buf. to transmit data frame */

}

 

 

void RecieveMsg (void) {

  uint8_t j;

  uint32_t dummy;

 

 

  while (CAN_1.IFRL.B.BUF04I == 0) {};  /* Wait for CAN 1 MB 4 flag */

  RxCODE   = CAN_1.BUF[4].CS.B.CODE;    /* Read CODE, ID, LENGTH, DATA, TIMESTAMP */

  RxID     = CAN_1.BUF[4].ID.B.STD_ID;

  RxLENGTH = CAN_1.BUF[4].CS.B.LENGTH;

  for (j=0; j<RxLENGTH; j++) {

    RxDATA[j] = CAN_1.BUF[4].DATA.B[j];

  }

  RxTIMESTAMP = CAN_1.BUF[4].CS.B.TIMESTAMP;

  dummy = CAN_1.TIMER.R;                /* Read TIMER to unlock message buffers */   

  CAN_1.IFRL.R = 0x00000010;            /* Clear CAN 1 MB 4 flag */

}

 

 

void main(void) {

  volatile uint32_t IdleCtr = 0;

 

  initModesAndClks();      /* Initialize mode entries */

  initPeriClkGen();        /* Initialize peripheral clock generation for DSPIs */

  disableWatchdog();       /* Disable watchdog */

//initCAN_1();             /* Initialize FLEXCAN 1 & one of its buffers for receive*/

  initCAN_4();             /* Initialize FlexCAN 4 & one of its buffers for transmit*/

  TransmitMsg();           /* Transmit one message from a FlexCAN 0 buffer */

  //RecieveMsg();            /* Wait for the message to be recieved at FlexCAN 1 */

  while (1) {              /* Idle loop: increment counter */

    IdleCtr++;              

  }

}

Labels (1)
8 Replies

2,217 Views
martin_kovar
NXP Employee
NXP Employee

Hi,

could you please tell me, which IDE you use? Is it CodeWarrior(which), S32DS or some other? What debug probe you use?

Also could you please clarify what does it mean "When I try to configure CAN4 registers It doesn't allow me to do that"?

Do you get any compiler error or another kind of error?

Regards,

Martin

2,217 Views
rehan_khan
Contributor III

I am using CodeWarrior 10.6.4. When I say I am trying to change the value of register that means I am trying to change the value either directly through debugger register windows or in code using software as given above.

0 Kudos
Reply

2,217 Views
martin_kovar
NXP Employee
NXP Employee

Hi,

ME.PCTL[34].R does not set FlexCAN4 module. If you want to enable FlexCAN4 use register PCTL[20].

Regards,

Martin

0 Kudos
Reply

2,217 Views
rehan_khan
Contributor III

Yes It is working but I don't get any data on CAN. Here is my question

1. Why cant we use ME.PCTL[34] because as per the data sheet for CAN 4 TX the PCR 34 is the  AF2(CAN4TX)

2. We have CAN Transceiver(TJA 1043T) which I am enabling using following config in my code to enable it

  SIU.PCR[10].R = 0x0200;

  SIU.PCR[11].R = 0x0200;

   

  SIU.GPDO[10].B.PDO = 1;

  SIU.GPDO[11].B.PDO = 1;

3. I dont see any data on CAN. Please find my code below

#include "MPC5607B.h" /* Use proper include file */

uint32_t RxCODE;              /* Received message buffer code */

uint32_t RxID;                 /* Received message ID */

uint32_t RxLENGTH;            /* Recieved message number of data bytes */

uint8_t  RxDATA[8];            /* Received message data string*/

uint32_t RxTIMESTAMP;          /* Received message time */                        

void initModesAndClks(void) {

  ME.MER.R = 0x0000001D;          /* Enable DRUN, RUN0, SAFE, RESET modes */

                                  /* Initialize PLL before turning it on: */

/* Use 1 of the next 2 lines depending on crystal frequency: */

  CGM.FMPLL_CR.R = 0x02400100;    /* 8 MHz xtal: Set PLL0 to 64 MHz */  

/*CGM.FMPLL_CR.R = 0x12400100;*/  /* 40 MHz xtal: Set PLL0 to 64 MHz */  

  ME.RUN[0].R = 0x001F0074;       /* RUN0 cfg: 16MHzIRCON,OSC0ON,PLL0ON,syclk=PLL */

  ME.RUNPC[1].R = 0x00000010;  /* Peri. Cfg. 1 settings: only run in RUN0 mode */

  ME.PCTL[20].R = 0x01;           /* MPC56xxB/P/S FlexCAN4: select ME.RUNPC[1] */

  //ME.PCTL[17].R = 0x01;         /* MPC56xxB/S FlexCAN1:  select ME.RUNPC[1] */

  ME.PCTL[68].R = 0x01;           /* MPC56xxB/S SIUL:  select ME.RUNPC[1] */

 

  /* Mode Transition to enter RUN0 mode: */

  ME.MCTL.R = 0x40005AF0;         /* Enter RUN0 Mode & Key */

  ME.MCTL.R = 0x4000A50F;         /* Enter RUN0 Mode & Inverted Key */ 

  while (ME.GS.B.S_MTRANS) {}     /* Wait for mode transition to complete */   

                                  /* Note: could wait here using timer and/or I_TC IRQ */

  while(ME.GS.B.S_CURRENTMODE != 4) {} /* Verify RUN0 is the current mode */

}

void initPeriClkGen(void) {

  CGM.SC_DC0.R = 0x80;     /* MPC56xxB/S: Enable peri set 2 sysclk divided by 1 */

}

void disableWatchdog(void) {

  SWT.SR.R = 0x0000c520;     /* Write keys to clear soft lock bit */

  SWT.SR.R = 0x0000d928;

  SWT.CR.R = 0x8000010A;     /* Clear watchdog enable (WEN) */

}       

void initCAN_1 (void) {

  uint8_t   i;

  CAN_1.MCR.R = 0x5000003F;       /* Put in Freeze Mode & enable all 64 msg bufs */

  CAN_1.CR.R = 0x04DB0006;        /* Configure for 8MHz OSC, 100KHz bit time */

  for (i=0; i<64; i++) {

    CAN_1.BUF[i].CS.B.CODE = 0;   /* Inactivate all message buffers */

  }

  CAN_1.BUF[4].CS.B.IDE = 0;      /* MB 4 will look for a standard ID */

  CAN_1.BUF[4].ID.B.STD_ID = 555; /* MB 4 will look for ID = 555 */

  CAN_1.BUF[4].CS.B.CODE = 4;     /* MB 4 set to RX EMPTY */

  CAN_1.RXGMASK.R = 0x1FFFFFFF;   /* Global acceptance mask */

  SIU.PCR[42].R = 0x0624;         /* MPC56xxB: Config port C10 as CAN1TX, open drain */

  SIU.PCR[35].R = 0x0100;         /* MPC56xxB: Configure port C3 as CAN1RX */

  SIU.PSMI[0].R = 0x00;           /* MPC56xxB: Select PCR 35 for CAN1RX Input */

  CAN_1.MCR.R = 0x0000003F;       /* Negate FlexCAN 1 halt state for  64 MB */

}

void initCAN_0 (void) {

  uint8_t   i;

  CAN_4.MCR.R = 0x5000003F;       /* Put in Freeze Mode & enable all 64 msg bufs */

  CAN_4.CR.R = 0x0F928040;        /* Configure for 8MHz OSC, 100KHz bit time */

  for (i=0; i<64; i++) {

   CAN_4.BUF[i].CS.B.CODE = 0;   /* Inactivate all message buffers */

  }

  CAN_4.BUF[0].CS.B.CODE = 8;     /* Message Buffer 0 set to TX INACTIVE */

  SIU.PCR[34].R = 0x0A00;         /* MPC56xxB: Config port B0 as CAN0TX, open drain */

  //SIU.PCR[17].R = 0x0100;         /* MPC56xxB: Configure port B1 as CAN0RX */

  CAN_4.MCR.R = 0x0000003F;       /* Negate FlexCAN 0 halt state for 64 MB */

}

void TransmitMsg (void) {

  uint8_t i;

                                   /* Assumption:  Message buffer CODE is INACTIVE */

  const uint8_t TxData[] = {"Hello"};  /* Transmit string*/

  CAN_4.BUF[0].CS.B.IDE = 0;           /* Use standard ID length */

  CAN_4.BUF[0].ID.B.STD_ID = 555;      /* Transmit ID is 555 */

  CAN_4.BUF[0].CS.B.RTR = 0;           /* Data frame, not remote Tx request frame */

  CAN_4.BUF[0].CS.B.LENGTH = sizeof(TxData) -1 ; /* # bytes to transmit w/o null */

  for (i=0; i<sizeof(TxData); i++) {

    CAN_4.BUF[0].DATA.B[i] = TxData[i];      /* Data to be transmitted */

  }

  CAN_4.BUF[0].CS.B.SRR = 1;           /* Tx frame (not req'd for standard frame)*/

  CAN_4.BUF[0].CS.B.CODE =0xC;         /* Activate msg. buf. to transmit data frame */

}

void RecieveMsg (void) {

  uint8_t j;

  uint32_t dummy;

  while (CAN_1.IFRL.B.BUF04I == 0) {};  /* Wait for CAN 1 MB 4 flag */

  RxCODE   = CAN_1.BUF[4].CS.B.CODE;    /* Read CODE, ID, LENGTH, DATA, TIMESTAMP */

  RxID     = CAN_1.BUF[4].ID.B.STD_ID;

  RxLENGTH = CAN_1.BUF[4].CS.B.LENGTH;

  for (j=0; j<RxLENGTH; j++) {

    RxDATA[j] = CAN_1.BUF[4].DATA.B[j];

  }

  RxTIMESTAMP = CAN_1.BUF[4].CS.B.TIMESTAMP;

  dummy = CAN_1.TIMER.R;                /* Read TIMER to unlock message buffers */   

  CAN_1.IFRL.R = 0x00000010;            /* Clear CAN 1 MB 4 flag */

}

void main(void) {

  volatile uint32_t IdleCtr = 0;

 

  initModesAndClks();      /* Initialize mode entries */

  initPeriClkGen();        /* Initialize peripheral clock generation for DSPIs */

  disableWatchdog();       /* Disable watchdog */

 

  SIU.PCR[10].R = 0x0200;

  SIU.PCR[11].R = 0x0200;

   

  SIU.GPDO[10].B.PDO = 1;

  SIU.GPDO[11].B.PDO = 1;

//initCAN_1();             /* Initialize FLEXCAN 1 & one of its buffers for receive*/

  initCAN_0();             /* Initialize FlexCAN 0 & one of its buffers for transmit*/

  TransmitMsg();           /* Transmit one message from a FlexCAN 0 buffer */

  //RecieveMsg();            /* Wait for the message to be recieved at FlexCAN 1 */

  while (1) {              /* Idle loop: increment counter */

    IdleCtr++;              

  }

}

2,217 Views
rehan_khan
Contributor III

This is configured for 500KHz bit time not 100KHz. In comment it is 100

CAN_4.CR.R = 0x0F928040;        /* Configure for 8MHz OSC, 100KHz bit time */

0 Kudos
Reply

2,217 Views
martin_kovar
NXP Employee
NXP Employee

Hi,

1) Yes, PCR 34 sets I/O pad functionality, but it is completely unrelated with PCTL registers. PCTL register determines, which ME_RUN_PC or ME_LP_PC register will be selected in curent mode. Please look at the Mode Entry module chapter. Also look at the table 6-1 – Peripheral clock sources, where you can find correct PCTL for each peripheral.

2) This code you share definitelly does not enable any transceiver. This code only choose the function for pins PA[10] and PA[11] (FlexCAN2 module by the way). Also you do not have enabled input and output buffers for appropriate pins.

3) Please look at the example in the attachment. There you can find, how to set FlexCan module. Example is created for MPC5668G, but the modules are similar and also there are good comments about correct SIUL setting and also FlexCan settings. 

If you use NXP EVB, please do not forget to check schematic and look, which pins are transceivers connected to. If you have your own design, you can ignore my previous sentence.

Regards,

Martin

2,217 Views
rehan_khan
Contributor III

Thanks for yor your reply

PCR[10] and PCR[11] are enable input for transceivers. After enabling it data starts coming from CAN bus.

I was also not enabling the receiver part of CAN module (//SIU.PCR[17].R = 0x0100; ). I have enabled it. The only problem is we are getting CAN frame error. We need help on register (CAN_4.CR.R = 0x0F928040) config for CAN baudrate 500Kbs.

0 Kudos
Reply

2,217 Views
martin_kovar
NXP Employee
NXP Employee

Hi,

please use this document for correct bit timing calculation for FlexCAN.

FlexCAN bit timing calculation

Regards,

Martin

0 Kudos
Reply