cancel
Showing results for 
Search instead for 
Did you mean: 

CAN baud rate calculation

vamshi_kumar
Associate II

Would someone kindly explain to me how to determine the CAN controller's baudrate for the STM32f769 ?

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

Hello @vamshi_kumar ,

This is how to calculate the CAN bitrate on devices featuring bxCAN such as STM32f769.

SofLit_0-1703238938853.png

You can rely on CubeMx to help you to configure the bitrate:

SofLit_1-1703239660176.png

In that case you need to play with different values to match the wanted bitrate.

Also the choice of BS1 (bit segment 1) and BS2 (bit segment 2) should be selected to have the sampling point position between ~75% and ~80%:

So BS1 = ~(75% to 80%) of (BS1+BS2)

The example I provided above with CubeMx (CAN @1Mb/s):

BS1 = 13, BS2 = 4: So the sampling point position in this case: (13 x 100) /(BS1+BS2) = ~76%.

It's also recommended to increase BS1 and BS2 as much as possible and decrease the CAN clock prescaler to match your CAN bitrate.

Hope it helps.

 

 

 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

7 REPLIES 7
Rob Ashworth
Senior

Easiest method I found is to use Cube IDE and it will do the sums for you.  I usually try for a nice round clock value feeding it - I target 40MHz, to get easy numbers for baud of 1MBit, 500kHz etc.

CAN Baud IDE.pngCAN Baud IDE 2.png

SofLit
ST Employee

Hello @vamshi_kumar ,

This is how to calculate the CAN bitrate on devices featuring bxCAN such as STM32f769.

SofLit_0-1703238938853.png

You can rely on CubeMx to help you to configure the bitrate:

SofLit_1-1703239660176.png

In that case you need to play with different values to match the wanted bitrate.

Also the choice of BS1 (bit segment 1) and BS2 (bit segment 2) should be selected to have the sampling point position between ~75% and ~80%:

So BS1 = ~(75% to 80%) of (BS1+BS2)

The example I provided above with CubeMx (CAN @1Mb/s):

BS1 = 13, BS2 = 4: So the sampling point position in this case: (13 x 100) /(BS1+BS2) = ~76%.

It's also recommended to increase BS1 and BS2 as much as possible and decrease the CAN clock prescaler to match your CAN bitrate.

Hope it helps.

 

 

 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

Thanks for your solution,

But when I am trying to calculate the baud rate value using the formula provided in the data sheet, there is a mismatch for baud rate value which is auto generated by the tool cube ide.

APB clock is 45MHZ.

vamshi_kumar_0-1703243661322.png

 

vamshi_kumar_1-1703243712135.png

 

Rob Ashworth
Senior

Here is my code for setting 1MBit...  I haver to say there is usually a slight discrepancy when comparing how other people calculate this, but it has always worked ok for me.

/* CAN Baudrate = 1MBps*/

//Calculation is CANCLK/(BRP*(BS1+BS2+1)) = 40/4*(8+1+1) = 1,

BRP is Prescaller (4)

CANCLK is your Bus speed (45MHz)

You may struggle with clock of 45MHz, because you end up with a fraction in most Baud rates.

Make BS1 bigger than BS2 to ensure the "bit" is sampled at about 75% (I have 8:1 so 80%).

 

/* CAN Baudrate = 1MBps*/
  //Calculation is CANCLK/(BRP*(BS1+BS2+1)) = 40/4*(8+1+1) = 1
/* CAN1 init function */
void CAN_Config(void)
{

//M4

  CAN_FilterTypeDef  sFilterConfig;

  /*##-1- Configure the CAN peripheral #######################################*/
  CanHandle.Instance = CAN1;

  HAL_CAN_MspInit(&CanHandle);

  CanHandle.Init.TimeTriggeredMode = DISABLE;
  CanHandle.Init.AutoBusOff = ENABLE;//ENABLE for auto bus back on
  CanHandle.Init.AutoWakeUp = DISABLE;
  CanHandle.Init.AutoRetransmission = ENABLE;//DISABLE WHEN ETHERCAT!
  CanHandle.Init.ReceiveFifoLocked = DISABLE;
  CanHandle.Init.TransmitFifoPriority = DISABLE;
  CanHandle.Init.Mode = CAN_MODE_NORMAL;//CAN_MODE_LOOPBACK;//CAN_MODE_NORMAL;
  CanHandle.Init.SyncJumpWidth = CAN_SJW_1TQ;
  CanHandle.Init.TimeSeg1 = CAN_BS1_8TQ;
  CanHandle.Init.TimeSeg2 = CAN_BS2_1TQ;
  CanHandle.Init.Prescaler = BITRATE;//4;//1MBit/s is /4, 500kBit/s is /8 etc...BITRATE

No, There is no discrepancy.

See the figure I've already attached:

SofLit_0-1703246526572.png

When you want to do the calculation to set BTR register (using reference manual) you need to use the first formula.

When you want to do the calculation to set the bitrate using HAL or CubeMx you need to use the second formula.

Because in HAL, +1 is added to BS1 and BS2.

In my example: Sys clock is at 216MHz / APB divider = 4 (APB clk = 54MHz), CAN clk prescaler = 3.

If I apply the second formula (related to CubeMx/HAL):

216000000 / 4 x 3x (1+13+4) = 1MHz -> 1Mb/s.

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
SofLit
ST Employee

Updated the figure with updated HAL naming:

SofLit_0-1703253206453.png

The old figure shows the names used in old CAN HAL implementation (updated names in red).

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

Thanks for the solution, Its working....