Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Baudrate is calculated with SJW in the mix #40

Closed
attie-argentum opened this issue Jul 24, 2024 · 2 comments
Closed

Baudrate is calculated with SJW in the mix #40

attie-argentum opened this issue Jul 24, 2024 · 2 comments

Comments

@attie-argentum
Copy link
Contributor

We've been having some issues with CAN communications between a handful of devices.
I started looking in to how the baudrate is calculated, and it seems to be slightly wrong.

SJW is not actually part of the bit-time calculation, I suspect this has been confused with SYNC_SEG (which is always 1).
The following is from the STM32F72xxx reference manual, but applies to all bxCAN peripherals.

screenshot showing the nominal bit timing - SYNC_SEG + BS1 + BS2

I believe the calculation below should be calcBaudrate = (frequency / (prescaler * (1 + bs1 + bs2))).

STM32_CAN/STM32_CAN.cpp

Lines 707 to 717 in ae6ccad

while (sjw <= 4) {
while (prescaler <= 1024) {
while (bs2 <= 3) { // Time segment 2 can get up to 8, but that causes too small sample-point percentages, so this is limited to 3.
while (bs1 <= 15) { // Time segment 1 can get up to 16, but that causes too big sample-point percenages, so this is limited to 15.
int calcBaudrate = (int)(frequency / (prescaler * (sjw + bs1 + bs2)));
if (calcBaudrate == baud)
{
setBaudRateValues(CanHandle, prescaler, bs1, bs2, sjw);
return;
}

Separately, to improve interoperability SJW can be increased from the default value of 1 - this will allow the next transition to arrive earlier than the node expects it - up to min(BS2, 4) time quanta.
In many cases, this can be safely set higher than 1 - and in other cases it's even necessary to avoid error frames and wider disruption on the bus.

Please can you let me know if you agree with this, or if you think I'm mistaken?
I'd be happy to prepare a PR to adjust this accordingly.

@pazi88
Copy link
Owner

pazi88 commented Jul 26, 2024

You might be right. Please prepare the PR. I'll check it more in detail next week.

@pazi88
Copy link
Owner

pazi88 commented Aug 21, 2024

Fixed in #41

@pazi88 pazi88 closed this as completed Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants