Skip to main content
Explorer
December 22, 2023
Solved

CAN baud rate calculation

  • December 22, 2023
  • 4 replies
  • 13451 views

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

    This topic has been closed for replies.
    Best answer by mƎALLEm

    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.

     

     

     

     

    4 replies

    Graduate II
    December 22, 2023

    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

    mƎALLEmAnswer
    Technical Moderator
    December 22, 2023

    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.

     

     

     

     

    Explorer
    December 22, 2023

    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

     

    Graduate II
    December 22, 2023

    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
    Technical Moderator
    December 22, 2023

    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).