Skip to main content
Cde M.1
Associate
October 20, 2023
Solved

Cannot change CAN Baud on the fly - STM32F103C8T6

  • October 20, 2023
  • 3 replies
  • 2162 views

Hi, I'm developing an CAN to USB converter, this converter needs to change the baud rate of the CAN when the user sends an determined message via UART.

The Prescaler, TimeSeg1, TimeSeg2 are all configured correctly for the baud rate I desire, as example:

 

 

      hcan.Init.Prescaler = 100;
      hcan.Init.TimeSeg1  = CAN_BS1_15TQ;
      hcan.Init.TimeSeg2  = CAN_BS2_2TQ;
 
In the example above, I choose the baud rate to be in 20kbps, when the command is sent the values are correctly stored, but checking with the osciloscope I noticed that the baud rate didn't change.
 
The steps that im using are:
 
    HAL_CAN_Stop(&hcan);
    HAL_CAN_MspDeInit(&hcan);
    
    hcan.Init.Prescaler = 100;
    hcan.Init.TimeSeg1  = CAN_BS1_15TQ;
    hcan.Init.TimeSeg2  = CAN_BS2_2TQ;
    HAL_CAN_MspInit(&hcan);
    HAL_CAN_Start(&hcan);
 
Is there something that I'm missing with this routine?
 
Values in hcan before sending the baud rate change
CdeM1_0-1697816846474.png

 Values of hcan after sending baud rate change

CdeM1_1-1697816921680.png

 

This topic has been closed for replies.
Best answer by Tesla DeLorean

>>Is there something that I'm missing with this routine?

Wouldn't HAL_CAN_Init() be the function to be calling? This would then presumably call HAL_CAN_MspInit(), a routine you supply to configure the clocks, pins, etc.

3 replies

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
October 20, 2023

>>Is there something that I'm missing with this routine?

Wouldn't HAL_CAN_Init() be the function to be calling? This would then presumably call HAL_CAN_MspInit(), a routine you supply to configure the clocks, pins, etc.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Karl Yamashita
Lead III
October 20, 2023

Call HAL_CAN_DeInit first. Change your baud rate. Then call HAL_CAN_Init then HAL_CAN_Start

 

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
Cde M.1
Cde M.1Author
Associate
October 21, 2023

@Karl Yamashita @Tesla DeLorean Thank you so much! I noticed it just a few moments after posting this topic. But anyway, that was exactly the problem. Thank you so muuuuch!!