Skip to content

BitTiming: align validation with ISO 11898-1 and improve from_sample_point - #2147

Merged
zariiii9003 merged 10 commits into
hardbyte:mainfrom
zariiii9003:bit-timing-fixes
Nov 22, 2026
Merged

zariiii9003 merged 10 commits into
hardbyte:mainfrom
zariiii9003:bit-timing-fixes

Conversation

@zariiii9003

Copy link
Copy Markdown
Collaborator

Summary of Changes

Review of can/bit_timing.py against ISO 11898-1:2015, with fixes, docs and test cleanup.

  • Validation: BitTiming validated nothing unless strict=True. Both classes now always check the basic structure (brp, tseg1, tseg2 ≥ 1; 1 ≤ sjw ≤ min(tseg1, tseg2); f_clock/bitrate > 0). Invalid input raises ValueError instead of ZeroDivisionError later.
  • BTR registers: btr0/btr1 raise instead of silently overflowing (pcan, kvaser, vector, slcan, canalystii send these to hardware). str() shows BTR: n/a. from_registers accepts only 8-bit values.
  • Strict mode follows the ISO minimum configuration ranges: classic tseg1 2–16 and tseg2 2–8, FD prescalers 1–32, sjw < tseg1 in the nominal phase. The ≥ 50 % sample point rule now applies only in strict mode and the solvers; ISO does not restrict the sample point.
  • Solvers: from_sample_point() prefers the most accurate bitrate, then the closest sample point. For CAN FD a shared prescaler comes right after bitrate accuracy, because equal time quanta give the best clock tolerance. The solvers use round() instead of int() and reject sample points ≥ 100 %.
  • oscillator_tolerance() uses the exact time quantum (12.5 ns was rounded to 12 ns).
  • Config files: complete but invalid bit timing parameters are logged as a warning instead of being dropped silently. The --timing CLI error now says "incomplete or invalid".
  • Docs: new "Strict Mode" section, corrected segment/SJW/brp explanations, a note on the TDC prescaler limit, fixed docstring examples.
  • Tests: new tests for every change; redundant tests removed, with identical coverage.

Related Issues / Pull Requests

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Other (please describe): stricter validation and different solver results (see notes)

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added or updated documentation as appropriate.
  • I have added a news fragment for towncrier.
  • All checks and tests pass (tox).

Additional Notes

Intended behaviour changes:

  • Structurally invalid timings now raise at construction, also with strict=False.
  • Strict mode rejects tseg1 = 1 / tseg2 = 1 (classic) and prescalers > 32 (FD). FD solver: nominal bitrates below about 31 kbit/s at 80 MHz no longer find a solution.
  • from_sample_point() returns different timings for about 20 % of common classic inputs: no tseg2 = 1, SJW limited by the ISO rules, and equal or better bitrate. No common input loses its solution. This also affects gs_usb (10 of 63 tested bitrate/clock combinations at its default 87.5 %). Odd bitrates find solutions more often.
  • Non-strict BitTimingFd now accepts sample points below 50 %.
  • A config timing that is now invalid is still ignored (fallback to bitrate, as before), but a warning is logged.

🤖 Generated with AI

@zariiii9003
zariiii9003 merged commit ff052f7 into hardbyte:main Nov 22, 2026
28 checks passed
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

Successfully merging this pull request may close these issues.

1 participant