CAN Bus Timing (MCU : MC9S12XF512MLM)

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

CAN Bus Timing (MCU : MC9S12XF512MLM)

跳至解决方案
2,584 次查看
Kartoon67
Contributor III

Good afternoon,

 

I have some difficulties to configure CAN Bus Timing.

 

My clock source is configured at 4MHz. The configuration used in freescale example give me a CAN Bus Timing to 500kbp/s.

 

I would like to upgrade this speed to 1Mbit/s.

 

I try some example to modify Time Segment Values and prescaler, but it don't works for now.

 

That's why I required your help.

 

Best Regards,

 

 

 

Here the example of CAN Initilisation :

 

void vfnCAN_Init(void) {    CAN0CTL0 = 0x01;    /* MSCAN in initialization mode */        while (!(CAN0CTL1_INITAK))            ;         /* Wait for initialization mode acknowledge */                CAN0CTL1_CANE = 1;      /* Enable MSCAN module */    CAN0CTL1_CLKsrc=0;    /* Clock source is OSCCLK, CANCLK = 4MHz */        CAN0CTL1_LOOPB = 0;     /* Set to 1 for LoopBack Mode, 0 otherwise */    CAN0CTL1_LISTEN = 0;    /* Not listen only mode */                           /* Baud rate = CANCLK/(Prescaler * time quantas) */                                                                               CAN0BTR1_TSEG_10 = 0x04;    /* Time Segment 1 = 5 */    CAN0BTR1_TSEG_20 = 0x01;    /* Time Segment 2 = 2 */                                /* TSEG1 + TSEG2 + SYNCH_SEG = 8 time quantas */                                                                      /* Prescaler = CANCLK/(Baud rate * time quantas) = 4MHz/(500kHz * 8) = 1 */        CAN0BTR0_BRP = 0x00;    /* Baud rate prescaler = 1 */      CAN0BTR0_SJW = 0x01;    /* Sinchronization jump width = 2 clock cycles */                                      CAN0BTR1_SAMP = 0;      /* One sample per bit */                        CAN0IDAC_IDAM = 0x01;               /* Four 16-bit acceptance filters */                                                                  CAN0IDAR0 = 0xFF;                   /* 16 bit Filter 0 */    CAN0IDMR0 = 0xFF;                   /* No Filter */    CAN0IDAR1 = 0xFF;         CAN0IDMR1 = 0xFF;            CAN0IDAR2 = 0xFF;                   /* 16 bit Filter 1 */    CAN0IDMR2 = 0xFF;                   /* No Filter */    CAN0IDAR3 = 0xFF;                       CAN0IDMR3 = 0xFF;            CAN0IDAR4 = 0xFF;                   /* 16 bit Filter 2 */    CAN0IDMR4 = 0xFF;                   /* No Filter */    CAN0IDAR5 = 0xFF;                       CAN0IDMR5 = 0xFF;            CAN0IDAR6 = 0xFF;                   /* 16 bit Filter 3 */    CAN0IDMR6 = 0xFF;                   /* No Filter */    CAN0IDAR7 = 0xFF;                       CAN0IDMR7 = 0xFF;                CAN0CTL0 = 0x00;            /* Exit initialization mode request */        while (CAN0CTL1_INITAK)            ;                /* Wait for normal mode */                           while(!(CAN0CTL0_SYNCH))            ;                   /* Wait for CAN synchronization */       CAN0RFLG_RXF = 1;       /* Clear receiver flags */                                       CAN0RIER_RXFIE = 1;     /* Enable Full Receive Buffer interrupt */                                  }
标签 (1)
0 项奖励
回复
1 解答
2,087 次查看
kef
Specialist I

Yes, you can use bus clock to clock CAN module. But you may either need to not enable PLL frequency modulation, or verify that resulting clock jitter won't disturb 1Mbps CAN bus. I'm not sure if this is a problem or not, I'm using higher oscilator clock for 1Mbps CAN.

If you are not sure about your settings, try creating experimental project with Processor Expert enabled, and see how it initializes peripherals.

在原帖中查看解决方案

0 项奖励
回复
5 回复数
2,087 次查看
kef
Specialist I
  • My clock source is configured at 4MHz. The configuration used in freescale example give me a
  • CAN Bus Timing to 500kbp/s.
  • I would like to upgrade this speed to 1Mbit/s.

You can't . Your clock is to slow. But you may rise it probably using PLL.

0 项奖励
回复
2,087 次查看
Kartoon67
Contributor III

Hi,

 

And thanks for you reply.

 

The following parameter :

CAN0CTL1_CLKsrc=0;    /* Clock source is OSCCLK, CANCLK = 4MHz */

 can be configured  into CLOCK SOURCE or BUS CLOCK.

 

So, I know the bus clock do reference to the PLL generation. I generate an PLL of 40MHz like this :

 

void vfnClock_Settings(void){        /* Bus Clock = OSCCLK * (SYNR+1) / ((REFDV+1)(2*POSTDIV))    */    /*     40MHz =  4MHz  * (9+1) / (1)                          */    PLLCTL_PLLON = 0;       /* Disable the PLL */    SYNR_SYN = 0x09;        /* Set PLL synthesizer register to 9 */    REFDV_REFDV = 0x00;     /* Set PLL divider register to 0 */        /* Fvco = 2*OSCCLK*(SYNR+1)/(REFDV+1) = 8MHz* 10/1 = 80MHz   */    /*                                    -> VCOFRQ = 01b        */            SYNR_VCOFRQ1 = 0;    SYNR_VCOFRQ0 = 1;            /* Fref = OSCCLK/(REFDV+1) = 4MHz/1 = 4MHz -> REFRQ= 01b     */        REFDV_REFFRQ1 = 0;    REFDV_REFFRQ0 = 1;        PLLCTL_PLLON = 1;   /* PLL enabled */            while(!CRGFLG_LOCK)            ;       /* Wait until the PLL is within the desired frequency */                    CLKSEL_PLLSEL = 1;    /* Select clock source from PLL */      }

 

 So, i can use this bus clock to generate my CAN speed (1Mbit/s)?

 

Regards,

0 项奖励
回复
2,088 次查看
kef
Specialist I

Yes, you can use bus clock to clock CAN module. But you may either need to not enable PLL frequency modulation, or verify that resulting clock jitter won't disturb 1Mbps CAN bus. I'm not sure if this is a problem or not, I'm using higher oscilator clock for 1Mbps CAN.

If you are not sure about your settings, try creating experimental project with Processor Expert enabled, and see how it initializes peripherals.

0 项奖励
回复
2,087 次查看
Kartoon67
Contributor III

hi,

 

I try this and I think it works :

 

CAN0CTL1_CANE = 1;      /* Enable MSCAN module */    CAN0CTL1_CLKsrc=1;    /* Clock source is Bus CLOCK, CANCLK = 40MHz */        CAN0CTL1_LOOPB = 0;     /* Set to 1 for LoopBack Mode, 0 otherwise */    CAN0CTL1_LISTEN = 0;    /* Not listen only mode */                           /* Baud rate = CANCLK/(Prescaler * time quantas) */                                                                               CAN0BTR1_TSEG_10 = 0x0D;    /* Time Segment 1 = 14 */    CAN0BTR1_TSEG_20 = 0x04;    /* Time Segment 2 = 5 */                                /* TSEG1 + TSEG2 + SYNCH_SEG = 20 time quantas */                                                                      /* Prescaler = CANCLK/(Baud rate * time quantas) = 40MHz/(1MHz * 20) = 1 */        CAN0BTR0_BRP = 0x01;    /* Baud rate prescaler = 2 */      CAN0BTR0_SJW = 0x00;    /* Sinchronization jump width = 1 clock cycles */

 Now, my frames are sending at 1Mbit/s.

 

Regards

0 项奖励
回复
2,087 次查看
Kartoon67
Contributor III

I try this configuration :

 

    CAN0CTL1_CANE = 1;      /* Enable MSCAN module */    CAN0CTL1_CLKsrc=1;    /* Clock source is Bus CLOCK, CANCLK = 40MHz */        CAN0CTL1_LOOPB = 0;     /* Set to 1 for LoopBack Mode, 0 otherwise */    CAN0CTL1_LISTEN = 0;    /* Not listen only mode */                           /* Baud rate = CANCLK/(Prescaler * time quantas) */                                                                               CAN0BTR1_TSEG_10 = 0x04;    /* Time Segment 1 = 5 */    CAN0BTR1_TSEG_20 = 0x01;    /* Time Segment 2 = 2 */                                /* TSEG1 + TSEG2 + SYNCH_SEG = 8 time quantas */                                                                      /* Prescaler = CANCLK/(Baud rate * time quantas) = 40MHz/(8MHz * 8) = 5 */        CAN0BTR0_BRP = 0x05;    /* Baud rate prescaler = 5 */      CAN0BTR0_SJW = 0x01;    /* Sinchronization jump width = 2 clock cycles */                                 

 Am I right?

0 项奖励
回复