I am trying to set up a CAN (Controller Area Network) communication between a STM32446E-EVAL and a software application on my PC. The problem is that as soon as I try to receive or transmit a byte, "the dominant error bit" error appears in one of the registers and I suspect it has something to do with the baud rate from what I read on other forums but I couldn't find any option in the official STM IDE to set the baud rate...Does anyone have an idea on how I can set it properly?
3 Answers
From the reference manual §30.7.7 Bit timing:
Rearranging that: BRP = (FPCLK / (BaudRate x (TS1 + TS2 + 3))) - 1
Not all values of TS1 and TS2 result in an integer value for the above formula, so will not generate the correct baud rate. Moreover you have to consider the sample point - various CAN application protocols have specific preferred sample point recommendations, but must in any event be between 50% and 90%. The sample point in percentage terms is:
1 - ((TS2+1) / (TS2+TS1+3)) x 100%
So there are multiple solutions with respect to TS1/TS2 values, but each gives a different sample point, so you need to select values that:
- Generate exactly 500Kbps (NBT = 2 microseconds).
- Has a sample point as near to the preferred value as possible in the range 50 to 90%.
It is not perhaps straight-forward, and without knowing your FPCLK not possible to advise specifically. I created a spreadsheet that generates all valid possibilities with their respective sample point.
Based on the above, TS1=10, TS2=1, BRP=5, will result in 500Kbps with a sample point of 85.7% - Close to the CANopen recommendation of 87.5%.
These values agree with the calculator at http://www.bittiming.can-wiki.info/ Noting that the values in the table are the register values + 1.
Step one: learn what the CAN time quanta is : http://www.bittiming.can-wiki.info/
Step two: check what your can clock is : from your project.
Step three: put this value into the page above.
Step four: get your values ready!!
Step five: program the appropriate CAN register (or struct members if you use HAL) with the step4 values.
Step six : celebrate the success.
I had a similar problem when I was working on a project. I wasn't using the exact board as you, but it was still a STM32 and I was using CubeMx IDE. There is a .ioc file in your project most likely and there you have a field called "Parameter settings" where you will find more fields. In my case I changed the prescaler and the Bit time quanta 1 and 2, as mentioned in the answer above. Now of course, it depends on the configuration of your particular board, but in my case the "winning combination" was 8 for Prescaler and 6 and 5 for the quantas. I used an oscilloscope for measurement until I found out that those were the right values. Changing those 3 parameters should do the trick.