Main Site Documentation

CAN baud rate and bit timing


#1

CAN bit timing and baud rate is a nightmare for anyone to use. You really have to know a lot to understand how it exactly works. In most cases, you do not have to get things perfect as if you need so then you probably understand all CAN parameters (sync, propagation, phase 1, phase 2, SJW, time quanta and more!!!) http://en.wikipedia.org/wiki/Controller_area_network

This forum is not meant to cover CAN but here is some steps if you need to use CAN bus on our devices:

  1. Divide the system clock (72Mhz) to get an appropriate clock for the CAN peripheral (this is NOT the baud rate) This is called the BRP
  2. figure out how many clocks you need to make up one bit. Usually this is 24 or 16. This is called TQ
  3. Assign values to T1 and T2 where T1+T2+1=TQ

Let us say we need 250Kbps

  1. From 72Mhz system clock, I want the CAN clock to be 6Mhz so I need to divide by 12 (BRP=12)
  2. 6Mhz/250kbps = 24 TQ (we usually want 16 or 24)
  3. T1 = 15, T2 = 8 will give us 15 + 8 + 1 = 24 and this is what we need

I got the T1 and T2 values from http://www.kvaser.com/can/protocol/index.htm
I picked the first value and subtracted 1 from T1 because the calculator included the sync bit

Here is the code with good comments


// These numbers were calculated using the calculator on this link:
// http://www.kvaser.com/can/protocol/index.htm
// We used the very first value from the calculator output
/////////////////////////////////////////////////////////////////////////////////////////////
// Bitrate 250Kbps
// CLK = 72 Mhz, with BRP = 12 -> 6Mhz CAN clock
// 6Mhz/250Kbps = 24 TQ
// T1 = 16 minus 1 for sync = 15
// T2 = 8
// 15 + 1 + 8 = 24 TQs which is what we need
/////////////////////////////////////////////////////////////////////////////////////////////
BRP = 12;
T1 = 15;
T2 = 8;
// For 500Kbps you can use BRP=6 and for 1Mbps you can use BRP=3 and for 125Kbps use BRP=24...and so on
// Keep T1 and T2 the same to keep the sampling pint the same (between 70% and 80%) 

CAN canChannel = new CAN(CAN.CANChannel.Channel_1, ((T2 - 1) << 20) | ((T1 - 1) << 16) | ((BRP - 1) << 0));

#2

For the g120, is the can clock also 6mhz?


#3

It is what you set BRP (prescaler) to. See this from my original post please.


#4

So i start from 120mhz? As can clock?
Best sampling point is between 60 and 80% and sjw=1?

Edit i get a q value 20 instead of 24 then


#5

hi,

I am currently in a situation where I don’t get any response in my G120 can channel, and doubting the bit rate calculation.
I need to figure out the correct parameters in case of the G120, of 120 mHz. similar to
the ones you have mentioned for 72Mhz example.

I did not understand though what you meant by -
“From 72Mhz system clock, I want the CAN clock to be 6Mhz so I need to divide by 12 (BRP=12)”

could you put some light into it, how do i go about selecting the CAN clock in my case?

@ David - looks like my question is same as your’s.


#6

Hi and welcome, CAN setting are changed in 4.3, this topic was about timing setting in 4.2

I suggest to start a new topic with a minimal sample code of your problem.


#7

Hi,

My dev environment is also SDK4.2 in G120 , where for CAN construction I need to pass the BTR (Bit Timing Reg). I am finding it difficult to calibrate and eventually not getting the CAN program to work, I don’t get any response on the CAN callback.

Earlier, there was USBizi board where the timing calculation was done like below and its working for tht board, I need to just modify it for the G120 board.

===================================================================
databus recommendation: Bitrate = 250Kbps, SP% > 85%
// Processor clock = 72 mhz
// Baud Rate Prescaler (BRP) = 12
// Use BRP=3 for 1Mbps, BRP=6 for 500Kbps, BRP=12 for 250Kbps, and BRP=24 for 125Kpbs
// CAN Clock = Processor Clock / BRP (72Mhz / 12 = 6Mhz CAN Clock)
// TQ = CAN Clock / Bitrate (6Mhz/250Kbps = 24 TQ)
// Bitrate values from calculator at http://www.kvaser.com/can/protocol/index.htm
// From calculator with TQ = 24, pick T1 = 16, T2 = 8

    // Pick best values from calculator (T1 = 14, T2 = 2, TQ = 16)
    // BRP = Processor Clock / Bitrate / TQ (72Mhz / 250Kbps / 16) = 18

    // T1 = 14
    // T2 = 2
    // TQ = 16
    // SP% = 87.5
    // BRP = 18
    // Sync bit = 1
    // Sync bit needs to be included so it is considered part of T1 (see BTR equation)
    // Bus Timing Register (BTR) = ((T2 - 1) << 20) | (((T1 - Sync) - 1) << 16) | ((BRP - 1) << 0)

=====================================================================

I need some help to figure out simlar correct values for the G120 board.

The sample program I wote just to post some CAN message and expecting some response, but in vain. Please see if it is correct.

Sample Program:

   private const Byte T1 = 17;
   private const Byte T2 = 7;
   private const Byte BRP = 20;
   private const Byte Sync = 1;
   private const Int32 BTR = ((T2 - 1) << 20) | (((T1 - Sync) - 1) << 16) | ((BRP - 1) << 0);

    main(){
    can1 = new CAN(CAN.Channel.Channel_1, BTR, 100);
    can1.DataReceivedEvent += can1_DataReceivedEvent;
    can1.ErrorReceivedEvent += can1_ErrorReceivedEvent;

    //Just push 0xFFFFFFFF in filter in to keep buffers empty
    uint[] explicitIDs = new uint[] { 4294967295 };
    can1.SetExplicitFilters(explicitIDs);

    //fixed dummy message
    msgOut = new CAN.Message[1];
    msgOut[0] = new CAN.Message();
    msgOut[0].IsEID = true;
    msgOut[0].ArbID = 0x18FEE6EE;  // Random value
    msgOut[0].Data[0] = 255;
    msgOut[0].DLC = 1;
    msgOut[0].DLC = 1;

    //highest prio thread sending can messages out
    SendCanMsg1 = new Thread(SendCanMsg1Thread);
    SendCanMsg1.Priority = ThreadPriority.Highest;
    SendCanMsg1.Start();
    Debug.Print("CAN started");
    //sleep for 1 second (to log some can messages first)
    Thread.Sleep(5000);

}

static void SendCanMsg1Thread()
{
while (true)
{
int _ret = can1.PostMessages(msgOut, 0, 1);
Debug.Print(“Can postmessage count=” + _ret.ToString() + " on " +
DateTime.Now.Minute.ToString() + “:” + DateTime.Now.Second.ToString()
+ “:” + DateTime.Now.Millisecond.ToString());
Thread.Sleep(300);
}
}

static void can1_ErrorReceivedEvent(CAN sender, CANErrorReceivedEventArgs args)
{
    Debug.Print("Can error = " + args.Error.ToString());
    
}

static void can1_DataReceivedEvent(CAN sender, CANDataReceivedEventArgs args)
{
    Debug.Print("Can message received = "+ args.ToString());
}

Output:

Can postmessage count=1 on 41:51:554
CAN started
Can postmessage count=1 on 41:51:658
Can postmessage count=1 on 41:51:762
Can postmessage count=1 on 41:51:866
Can postmessage count=1 on 41:51:970
Can postmessage count=1 on 41:52:74
Can postmessage count=1 on 41:52:178
Can postmessage count=1 on 41:52:282
Can postmessage count=1 on 41:52:387
Can postmessage count=1 on 41:52:490


#8

For the G120 this would be:

125KB - T1 - 15 - T2 - 8 - BRP - 20
250KB - T1 - 15 - T2 - 8 - BRP - 10
500KB - T1 - 15 - T2 - 8 - BRP - 5
1000KB - T1 - 7 - T2 - 4 - BRP - 20


#9

So if I want 25kBps would BPR be 120?


#10

Hi!

I’m trying to configure Cerb40 to work with BMW CAN BUS that is 100Kbps. That option is missing in ControllerAreaNetwork.Speed enum, so I need to create ControllerAreaNetwork.Timings object. What parameters should I pass into constructor?

Timings(int propagation, int phase1, int phase2, int brp, int synchronizationJumpWidth)


#11

Google online for any CAN bit timing calculator and it will have these settings

I actually just did that and this is what I found


#12

Thanks @Gus_Issa! I already saw that calculator in that thread: CAN Bus at 100Kbps bitrate?
but there are some questions how to use it:

  1. What is the frequency of Cerb CAN controller to use as “Input Clock Frequency” in the calculator? Tried to find that info for STM32F405 but no luck.
  2. Where can I find “propagation” parameter in the results table of the calculator?
  3. Should I use the same values as in the old CAN class example phase1=T1=15, phase2=T2=8 or they are not correct for Cerb?
  4. As I understand from the calculator manual, SJW is synchronizationJumpWidth and it should be 1, right?

#13

I used to teach CAN and the bit timing part was the most confusing and it took a bit of work to explain it all. I suggest some more reading on this topic.

  1. I think one of the parameters in the CAN class returns the internal clock used.
  2. Take a look at the bit timing section here https://en.wikipedia.org/wiki/CAN_bus
  3. old? You mean NETMF vs TinyCLR? I wish you started a thread instead of replying to an Archive.
  4. SWJ depends on your system specifics.1 should work.

#14

By the way, the old topic you linked to above is here CAN Bus at 100Kbps bitrate?

We are trying to never use “old” links since those will die in the future.


#15
  1. I use NETMF 4.3 with ControllerAreaNetwork class (not 4.2 with CAN class). Do you mean ControllerAreaNetwork.SourceClock static property? Can I use it in Kvaser CAN calculator as CAN controller Input Clock Frequency?
  2. I already read that article but it’s still not clear what value should I use for propagation parameter. “Propagation” word is mentioned only one time in that article (that it’s one of the four segments within the bit) and there are no details how to calculate it.
  3. Nope, I mean 4.2 by “old”. Since that class is used in the thread I mentioned and it isn’t the same as ControllerAreaNetwork in 4.3. I don’t use TinyCLR in my project for now.
  4. OK, thanks.

PS. I changed the link to the mentioned thread from old to new forum, I won’t use old links anymore :slight_smile:


#16

BTW, I found a thread for STM32 CAN settings: https://aimagin.com/forum/viewtopic.php?f=2&t=309

As I understand, Cerb CAN controller freq is 42Mhz and right values are:

Timings(propagation: ???, phase1: 14 /* or 15? does it include sync? /, phase2: 6, brp: 20 / 100kbps */, synchronizationJumpWidth: 1)

@Gus_Issa please check that parameters and help how to calculate propagation?