Skip to content

fix(bit_timing): make the sample point solver's register ranges configurable - #2165

Open
ChuangMuLinze wants to merge 2 commits into
hardbyte:mainfrom
ChuangMuLinze:fix/bit-timing-configurable-ranges
Open

ChuangMuLinze wants to merge 2 commits into
hardbyte:mainfrom
ChuangMuLinze:fix/bit-timing-configurable-ranges

Conversation

@ChuangMuLinze

Copy link
Copy Markdown

Summary of Changes

iterate_from_sample_point built every candidate with strict=True, whose brp <= 32,
tseg1 <= 16 and tseg2 <= 8 limits are the ISO 11898-1 minimum ranges every compliant
controller must support. A controller whose registers reach further had valid timings rejected
and from_sample_point raised No suitable bit timings found, with no way for a caller to widen
the search.

  • BitTiming, from_sample_point and iterate_from_sample_point take tseg1_max, tseg2_max
    and brp_max.
  • _restrict_to_minimum_range uses those bounds instead of hardcoded ones.
  • The solver scans prescalers up to brp_max rather than stopping at 64.
  • The defaults are the previous limits (16 / 8 / 32), so existing callers are unaffected.
  • Limits below the smallest legal register value are rejected instead of silently yielding no
    solution.

Related Issues / Pull Requests

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Other (please describe):

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added or updated documentation as appropriate — the new parameters are documented in
    the docstrings, which the API docs render.
  • I have added a news fragment (doc/changelog.d/2083.fixed.rst) for towncrier.
  • All checks and tests pass (tox) — see below for what I ran.

Additional Notes

The case from the issue, STM32G431 FDCAN at 160 MHz for 250 kbit/s at 87.5%, now returns the
solution the issue predicted:

>>> can.BitTiming.from_sample_point(
...     f_clock=160_000_000, bitrate=250_000, sample_point=87.5,
...     tseg1_max=256, tseg2_max=256, brp_max=512,
... )
BitTiming(f_clock=160000000, brp=40, tseg1=13, tseg2=2, sjw=2, nof_samples=1)

Without the new arguments it still raises, which test_from_sample_point_keeps_default_ranges
pins.

Test run:

$ python -m pytest test/test_bit_timing.py -q
73 passed

The five new behaviour tests fail on unpatched main (unexpected keyword argument), so they cover
the change.

I ran pytest directly rather than through tox; this sandbox has no tox environment, so I
installed wrapt, pytest-timeout and pytest-cov to satisfy the project's addopts. Everything
else is the repository's own tox.ini configuration.

…gurable

`iterate_from_sample_point` built every candidate with `strict=True`, whose
`brp <= 32`, `tseg1 <= 16` and `tseg2 <= 8` limits are the ISO 11898-1
minimum ranges. A controller whose registers reach further therefore had
valid timings rejected, and `from_sample_point` raised "No suitable bit
timings found" with no way for a caller to widen the search.

STM32G431 FDCAN at 160 MHz for 250 kbit/s at 87.5% is such a clock: the
only solution is brp=40, tseg1=13, tseg2=2, and the strict brp limit
rejected it.

`from_sample_point`, `iterate_from_sample_point` and `BitTiming` now take
`tseg1_max`, `tseg2_max` and `brp_max`, and the solver scans prescalers up
to the configured maximum. The defaults are the previous limits, so
existing callers see no change.

Fixes hardbyte#2083
@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review: PR #2165 – configurable register ranges for the sample point solver

The PR fixes the reported case: from_sample_point(160_000_000, 250_000, 87.5, brp_max=512) returns brp=40, tseg1=13, tseg2=2 instead of raising. The API shape should change before merge, because public parameters cannot be removed later without breaking users.

API design

  • Wider limits are allowed by the standard: the current defaults are the minimum ranges for Classical CAN controllers in ISO 11898-1:2015, §11.3.1.2, Table 8. The same section allows controllers to support wider ranges, so making the limits configurable is justified.

  • Incomplete set of limits: tseg1_max, tseg2_max and brp_max are documented as "the largest value the controller accepts". The solver still keeps the other Table 8 limits fixed: SJW at most 4 and a nominal bit time of 8–25 tq. tseg1 therefore never goes above 22 whatever tseg1_max is, and 29 MHz / 1 Mbit/s still raises with tseg1_max=256. There are also no minimums and no prescaler step. Controllers report their limits as a full set; see Linux struct can_bittiming_const and the gs_usb BT_CONST request named in BitTiming.from_sample_point rejects valid timing solutions due to hardcoded register limits #2083. Each missing field would later need another keyword argument on every solver, and again for BitTimingFd.

  • strict gets two meanings: strict is documented as the ISO 11898-1 minimum ranges (doc/bit_timing.rst, BitTiming.__init__). With the new arguments, strict=True can also mean "custom limits". On BitTiming.__init__ the arguments are ignored without notice when strict=False.

  • Fix: replace the three keyword arguments with one frozen limits object, passed only to iterate_from_sample_point and from_sample_point, and leave BitTiming.__init__ and strict unchanged. When limits is None, keep the current strict path, so existing callers see no change.

    @dataclass(frozen=True)
    class BitTimingLimits:
        tseg1_min: int = 2
        tseg1_max: int = 16
        tseg2_min: int = 2
        tseg2_max: int = 8
        sjw_max: int = 4
        brp_min: int = 1
        brp_max: int = 32
        brp_inc: int = 1

    The solver loops over range(limits.brp_min, limits.brp_max + 1, limits.brp_inc), caps SJW at limits.sjw_max, and accepts a candidate when tseg1 and tseg2 are within the limits. It needs no separate bit-time maximum or _validate_ranges. With STM32G4 FDCAN nominal limits (tseg1 ≤ 256, tseg2 ≤ 128, SJW ≤ 128, BRP ≤ 512), this solves 160 MHz at 250 kbit/s and 50 kbit/s (87.5%), 80 MHz at 125 kbit/s (90%), and 29 MHz at 1 Mbit/s. Export the class in can/__init__.py and add it to doc/bit_timing.rst.

Documentation

  • Claims that results always satisfy strict mode: the iterate_from_sample_point docstring ("All returned timings are valid in strict mode.") and doc/bit_timing.rst, section "Strict Mode" ("The timings returned by from_sample_point() and iterate_from_sample_point() always satisfy strict mode."), hold only without custom limits. A result found with wider limits raises in BitTiming(..., strict=True). Qualify both.
  • "Every ISO 11898-1 compliant controller": the new docstrings describe the defaults this way, but they are the minimum ranges for Classical CAN controllers only. FD-enabled controllers must support wider nominal ranges (ISO 11898-1:2015, Table 8). Say "not FD enabled".

Tests

  • Current tests miss most of the change: with the tseg1/tseg2 limits hardcoded back to 16/8, or the prescaler loop bound set back to 64, the whole suite still passes. The only case needs brp=40 and tseg1=13, both within the old limits. test_iterate_from_sample_point_wider_brp repeats the from_sample_point test, which calls the iterator.
  • Fix: replace the new tests with one parametrized test in test/test_bit_timing.py. Each case must raise with the defaults and solve only with the widened field. For example, brp_max=512 at 160 MHz / 50 kbit/s / 87.5% needs brp > 64, and tseg1_max=22 at 80 MHz / 125 kbit/s / 90% needs tseg1=17. Add a case where the default bit-time limit is exceeded (29 MHz / 1 Mbit/s) and one for brp_inc.

Changelog

  • doc/changelog.d/2083.fixed.rst: the change adds public API, so name it 2083.added.rst and name the new class.

Verdict

request changes

`black --check .` is the first command of the lint environment and reformats
this file: the parametrize decorator fits on one line, and so does the call in
`test_from_sample_point_rejects_impossible_ranges`. No behavior changes.
@ChuangMuLinze

Copy link
Copy Markdown
Author

The static-code-analysis failure was mine and is fixed in c029247: black --check . reformats test/test_bit_timing.py, because the parametrize decorator fits on one line. I checked the whole lint environment locally with the same versions the job uses (black 26.10.1, ruff 0.17.6, pylint 4.1.5): black --check ., ruff check can examples doc and the pylint command all pass, and test/test_bit_timing.py still has its 73 tests passing.

The other four failing jobs are not from this change.

test (windows-latest, py314) fails the same way on main (run 46330592514). It dies while installing the test dependencies, before reaching the tests: asammdf pulls in lxml 6.2.1, which has no cp314 wheel, and the sdist does not build on the runner — fatal error C1083: Cannot open include file: 'libxml/xmlversion.h'.

test (macos-latest, py311, py313, py314) ran the whole suite successfully (869 tests run, 687 passed, 139 skipped) and failed after it, in the coveralls step, which tries to brew tap coverallsapp/coveralls and gets fatal: could not read Username for 'https://github.com'. The same jobs pass on main, so this looks like a flake on the runner rather than anything in the repository.

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.

BitTiming.from_sample_point rejects valid timing solutions due to hardcoded register limits

2 participants