Custom bit rate for CAN bus

The CAN library initialises the connection with one of four standard bit rates ( BR_125k, BR_250k, BR_500k, BR_1000k).

These are defined in ArduinoCore-API/api/HardwareCAN.h at master · arduino/ArduinoCore-API · GitHub

and used in the R7FA4M1_CAN::begin function.

The bus I want to connect to runs at a very low speed (16.7kbps) so I need some guidance on how to modify the code to support this bit rate.

I did this on the Adafruit Feather M4 CAN board as the initialisation routine allowed the bit rate to be input directly (rather than as a constant)

Has anybody else done this before, or do I need to figure it out bit-by-bit ?

Thanks

Those values ultimately get passed into a function calc_can_bit_timing where a loop figures out the closest figures to put into the 3 relevant registers ( baud_rate_prescaler, time_segment_1 (TSEG1) & time_segment_2 (TSEG2))

Yes, the source code also had a note referring to section 30.4.3, RA4M1 Group User Manual, which I had better go and read :slight_smile:

And I just found the thread you created showing where to access the source code for all types of registers in the underlying mcu.
I've got a feeling the CAN module will need to be in Reset mode for any change in bus timing to work. A shame the Arduino folks only support 4 bus speeds out of the box.

1 Like

I've modified the enum for the bit rates and can get it down to 40kbps. Anything lower means the bounds on TSEG1 and TSEG2 are exceeded* so the function to start the CAN module fails.
Is it possible this mcu cannot support lower bus speeds ?

* From section 30.4
TSEG1 = 4 Tq to 16 Tq
TSEG2 = 2 Tq to 8 Tq

The code in the CanUtils.cpp needs an overhaul.
Following some help from the Renases forums I've hard coded the values to get this to work.

I will create a code change and make a PR in the github repo as I can't be the only person with this issue.

1 Like

PR added to the github repo

2 Likes

And another PR to the github repo to support bit rate values which are just below (as well as just above) the theoretical rate.