diff --git a/can/bit_timing.py b/can/bit_timing.py index 41c132713..d43ddb49c 100644 --- a/can/bit_timing.py +++ b/can/bit_timing.py @@ -39,6 +39,9 @@ def __init__( sjw: int, nof_samples: int = 1, strict: bool = False, + tseg1_max: int = 16, + tseg2_max: int = 8, + brp_max: int = 32, ) -> None: """ :param int f_clock: @@ -65,6 +68,12 @@ def __init__( require a sample point of at least 50%. This can be used to ensure compatibility across a wide variety of CAN hardware. If False, only the structural requirements are checked. + :param int tseg1_max: + The largest ``tseg1`` the controller accepts. Only used with ``strict``. + :param int tseg2_max: + The largest ``tseg2`` the controller accepts. Only used with ``strict``. + :param int brp_max: + The largest ``brp`` the controller accepts. Only used with ``strict``. :raises ValueError: if the arguments are invalid. """ @@ -78,7 +87,9 @@ def __init__( } self._validate() if strict: - self._restrict_to_minimum_range() + self._restrict_to_minimum_range( + tseg1_max=tseg1_max, tseg2_max=tseg2_max, brp_max=brp_max + ) def _validate(self) -> None: _validate_positive("f_clock", self.f_clock) @@ -89,12 +100,14 @@ def _validate(self) -> None: if self.nof_samples not in (1, 3): raise ValueError("nof_samples must be 1 or 3") - def _restrict_to_minimum_range(self) -> None: - if not 2 <= self.tseg1 <= 16: - raise ValueError(f"tseg1 (={self.tseg1}) must be in [2...16].") + def _restrict_to_minimum_range( + self, tseg1_max: int = 16, tseg2_max: int = 8, brp_max: int = 32 + ) -> None: + if not 2 <= self.tseg1 <= tseg1_max: + raise ValueError(f"tseg1 (={self.tseg1}) must be in [2...{tseg1_max}].") - if not 2 <= self.tseg2 <= 8: - raise ValueError(f"tseg2 (={self.tseg2}) must be in [2...8].") + if not 2 <= self.tseg2 <= tseg2_max: + raise ValueError(f"tseg2 (={self.tseg2}) must be in [2...{tseg2_max}].") if not 1 <= self.sjw <= 4: raise ValueError(f"sjw (={self.sjw}) must be in [1...4].") @@ -110,8 +123,10 @@ def _restrict_to_minimum_range(self) -> None: if not 8 <= self.nbt <= 25: raise ValueError(f"nominal bit time (={self.nbt}) must be in [8...25].") - if not 1 <= self.brp <= 32: - raise ValueError(f"bitrate prescaler (={self.brp}) must be in [1...32].") + if not 1 <= self.brp <= brp_max: + raise ValueError( + f"bitrate prescaler (={self.brp}) must be in [1...{brp_max}]." + ) if not 5_000 <= self.bitrate <= 1_000_000: raise ValueError( @@ -223,7 +238,13 @@ def from_registers( @classmethod def iterate_from_sample_point( - cls, f_clock: int, bitrate: int, sample_point: float = 69.0 + cls, + f_clock: int, + bitrate: int, + sample_point: float = 69.0, + tseg1_max: int = 16, + tseg2_max: int = 8, + brp_max: int = 32, ) -> Iterator["BitTiming"]: """Create a :class:`~can.BitTiming` iterator with all the solutions for a sample point. @@ -235,17 +256,27 @@ def iterate_from_sample_point( Bitrate in bit/s. :param float sample_point: The sample point value in percent, in the range [50...100). + :param int tseg1_max: + The largest ``tseg1`` the controller accepts. The default is the minimum + range every ISO 11898-1 compliant controller has to support. + :param int tseg2_max: + The largest ``tseg2`` the controller accepts. The default is the minimum + range every ISO 11898-1 compliant controller has to support. + :param int brp_max: + The largest ``brp`` the controller accepts. The default is the minimum + range every ISO 11898-1 compliant controller has to support. :raises ValueError: if the arguments are invalid. """ _validate_positive("bitrate", bitrate) + _validate_ranges(tseg1_max=tseg1_max, tseg2_max=tseg2_max, brp_max=brp_max) if not 50.0 <= sample_point < 100.0: raise ValueError( f"sample_point (={sample_point}) must be in the range [50...100)%." ) - for brp in range(1, 65): + for brp in range(1, max(64, brp_max) + 1): nbt = round(f_clock / (bitrate * brp)) if nbt < 8: break @@ -269,6 +300,9 @@ def iterate_from_sample_point( tseg2=tseg2, sjw=sjw, strict=True, + tseg1_max=tseg1_max, + tseg2_max=tseg2_max, + brp_max=brp_max, ) yield bt except ValueError: @@ -276,7 +310,13 @@ def iterate_from_sample_point( @classmethod def from_sample_point( - cls, f_clock: int, bitrate: int, sample_point: float = 69.0 + cls, + f_clock: int, + bitrate: int, + sample_point: float = 69.0, + tseg1_max: int = 16, + tseg2_max: int = 8, + brp_max: int = 32, ) -> "BitTiming": """Create a :class:`~can.BitTiming` instance for a sample point. @@ -295,6 +335,14 @@ def from_sample_point( Bitrate in bit/s. :param float sample_point: The sample point value in percent, in the range [50...100). + :param int tseg1_max: + The largest ``tseg1`` the controller accepts. The default is the minimum + range every ISO 11898-1 compliant controller has to support; pass the + range the datasheet reports to solve clocks the defaults cannot. + :param int tseg2_max: + The largest ``tseg2`` the controller accepts. + :param int brp_max: + The largest ``brp`` the controller accepts. :raises ValueError: if the arguments are invalid. """ @@ -305,7 +353,14 @@ def from_sample_point( ) possible_solutions: list[BitTiming] = list( - cls.iterate_from_sample_point(f_clock, bitrate, sample_point) + cls.iterate_from_sample_point( + f_clock, + bitrate, + sample_point, + tseg1_max=tseg1_max, + tseg2_max=tseg2_max, + brp_max=brp_max, + ) ) if not possible_solutions: @@ -1215,6 +1270,13 @@ def __hash__(self) -> int: return tuple(self._data.values()).__hash__() +def _validate_ranges(tseg1_max: int, tseg2_max: int, brp_max: int) -> None: + """Check the controller range limits handed to the sample point solvers.""" + _validate_positive("tseg1_max", tseg1_max - 1) + _validate_positive("tseg2_max", tseg2_max - 1) + _validate_positive("brp_max", brp_max) + + def _validate_positive(name: str, value: int) -> None: """Raise a :class:`ValueError` if *value* is not greater than zero.""" if value <= 0: diff --git a/doc/changelog.d/2083.fixed.rst b/doc/changelog.d/2083.fixed.rst new file mode 100644 index 000000000..83fa8772e --- /dev/null +++ b/doc/changelog.d/2083.fixed.rst @@ -0,0 +1,4 @@ +``BitTiming.from_sample_point`` and ``BitTiming.iterate_from_sample_point`` accept +``tseg1_max``, ``tseg2_max`` and ``brp_max``, so a clock whose only valid timings exceed +the ISO 11898-1 minimum register ranges is solved instead of raising ``ValueError``. The +defaults keep the previous behaviour. diff --git a/test/test_bit_timing.py b/test/test_bit_timing.py index 1cc3d06cf..912bda165 100644 --- a/test/test_bit_timing.py +++ b/test/test_bit_timing.py @@ -211,6 +211,60 @@ def test_from_sample_point(): assert 69 < fd_timing.data_sample_point < 71 +def test_from_sample_point_wider_controller_ranges(): + # The ISO 11898-1 minimum ranges are the default, which hides valid timings for a + # controller whose registers reach further. STM32G431 FDCAN: 160 MHz clock, brp <= 512, + # tseg1 <= 256. The only solution for 250 kbit/s at 87.5% is brp=40, tseg1=13, tseg2=2, + # and the strict brp limit of 32 rejected it before the ranges were configurable (#2083). + timing = 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, + ) + assert timing.brp == 40 + assert timing.tseg1 == 13 + assert timing.tseg2 == 2 + assert timing.nbt == 16 + assert timing.bitrate == 250_000 + assert timing.sample_point == 87.5 + + +def test_from_sample_point_keeps_default_ranges(): + # The parameters default to the minimum ranges, so the same clock still raises for a + # caller that does not widen them (#2083). + with pytest.raises(ValueError, match="No suitable bit timings found"): + can.BitTiming.from_sample_point( + f_clock=160_000_000, bitrate=250_000, sample_point=87.5 + ) + + +def test_iterate_from_sample_point_wider_brp(): + solutions = list( + can.BitTiming.iterate_from_sample_point( + f_clock=160_000_000, + bitrate=250_000, + sample_point=87.5, + tseg1_max=256, + tseg2_max=256, + brp_max=512, + ) + ) + assert any(bt.brp == 40 and bt.tseg1 == 13 and bt.tseg2 == 2 for bt in solutions) + + +@pytest.mark.parametrize("ranges", [{"tseg1_max": 1}, {"tseg2_max": 1}, {"brp_max": 0}]) +def test_from_sample_point_rejects_impossible_ranges(ranges): + # A limit below the smallest legal register value can never accept a timing, so the + # arguments are refused instead of silently returning "no suitable bit timings" (#2083). + with pytest.raises(ValueError): + can.BitTiming.from_sample_point( + f_clock=16_000_000, bitrate=500_000, sample_point=75.0, **ranges + ) + + def test_iterate_from_sample_point(): for sp in range(50, 100): solutions = list(