cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G474 FDCAN BitTiming Register values?

ma.alam9
Associate III

Hello All,

I got Classic CAN running on the FDCAN3 module of STM32G474CE device using MXCube and other example projects for STM32G474 devices.

Problem is, all these example codes have a fixed baud rate of 1Mbps. My application requires thebaud rate be configurable. Until now, I have not been able to find any table / calculator on the internet that could calculate the bit timing register values for me, based on kernel clock and required baud rate etc... Does anyone here have any clue on this?

4 REPLIES 4
Bouraoui Chemli
ST Employee

Hello @Community member​ 

You can refer to section "44 FD controller area network (FDCAN)" of STM32G4 reference manual with focus on section "44.3.1 Bit timing".

Bouraoui

PHryn
Associate III

You are a beast. This calculator is amazing!!

DS88579
Associate
#define CAN_BITRATE                 ((uint32_t)500000)
#define CAN_SAMPLE_POINT            ((uint8_t)75)
#define CAN_CLOCK                   ((uint32_t)60000000)
#define CAN_PRESCALER               ((uint16_t)2)

/**
  * @brief CAN bit timing calculation
  * @PAram uint32_t: Bitrate (bit/s)
  * @PAram uint8_t: Sample point (%) 
  * @PAram uint32_t: Can clock (Hz)
  * @PAram uint16_t: Prescaler
  * @retval uint32_t: NBTP data
  */
uint32_t CanBitTimingCalculation(uint32_t bitrate, uint8_t samplePoint, 
                                 uint32_t canClock, uint16_t prescaler)
{
    uint32_t nbt = (uint32_t)(canClock / prescaler / bitrate);
    uint32_t seg2 = (uint32_t)(nbt * (100 - samplePoint) / 100);
    uint32_t seg1 = (uint32_t)(nbt - seg2 - 1);
    
    return ((uint32_t)(((seg1 - 1) << FDCAN_NBTP_NTSEG1_Pos) |
                       ((seg2 - 1) <<   FDCAN_NBTP_NTSEG2_Pos) |
                       ((prescaler - 1) << FDCAN_NBTP_NBRP_Pos)));
}

void CanInitNbtp(void)
{
    FDCAN1->NBTP = CanBitTimingCalculation(CAN_BITRATE, CAN_SAMPLE_POINT, 
                                           CAN_CLOCK, CAN_PRESCALER);
}