2023-12-22 01:06 AM - last edited on 2023-12-25 04:55 AM by SofLit
Would someone kindly explain to me how to determine the CAN controller's baudrate for the STM32f769 ?
Solved! Go to Solution.
2023-12-22 02:19 AM - edited 2023-12-22 05:16 AM
Hello @vamshi_kumar ,
This is how to calculate the CAN bitrate on devices featuring bxCAN such as STM32f769.
You can rely on CubeMx to help you to configure the bitrate:
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.
2023-12-22 01:22 AM
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.
2023-12-22 02:19 AM - edited 2023-12-22 05:16 AM
Hello @vamshi_kumar ,
This is how to calculate the CAN bitrate on devices featuring bxCAN such as STM32f769.
You can rely on CubeMx to help you to configure the bitrate:
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.
2023-12-22 03:16 AM
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.
2023-12-22 03:30 AM
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
2023-12-22 04:11 AM - edited 2023-12-22 04:56 AM
No, There is no discrepancy.
See the figure I've already attached:
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.
2023-12-22 05:54 AM
Updated the figure with updated HAL naming:
The old figure shows the names used in old CAN HAL implementation (updated names in red).
2023-12-22 10:37 PM
Thanks for the solution, Its working....