Skip to content

Commit

Permalink
use strict in from_bitrate_and_segments and recreate_with_f_clock
Browse files Browse the repository at this point in the history
  • Loading branch information
zariiii9003 committed Aug 2, 2023
1 parent 083174a commit 9288998
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
18 changes: 16 additions & 2 deletions can/bit_timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
In this case, the bit will be sampled three quanta in a row,
with the last sample being taken in the edge between TSEG1 and TSEG2.
Three samples should only be used for relatively slow baudrates.
:param strict:
:param bool strict:
If True, restrict bit timings to the minimum required range as defined in
ISO 11898. This can be used to ensure compatibility across a wide variety
of CAN hardware.
Expand Down Expand Up @@ -124,6 +124,7 @@ def from_bitrate_and_segments(
tseg2: int,
sjw: int,
nof_samples: int = 1,
strict: bool = False,
) -> "BitTiming":
"""Create a :class:`~can.BitTiming` instance from bitrate and segment lengths.
Expand All @@ -145,6 +146,10 @@ def from_bitrate_and_segments(
In this case, the bit will be sampled three quanta in a row,
with the last sample being taken in the edge between TSEG1 and TSEG2.
Three samples should only be used for relatively slow baudrates.
:param bool strict:
If True, restrict bit timings to the minimum required range as defined in
ISO 11898. This can be used to ensure compatibility across a wide variety
of CAN hardware.
:raises ValueError:
if the arguments are invalid.
"""
Expand All @@ -160,6 +165,7 @@ def from_bitrate_and_segments(
tseg2=tseg2,
sjw=sjw,
nof_samples=nof_samples,
strict=strict,
)
if abs(bt.bitrate - bitrate) > bitrate / 256:
raise ValueError(
Expand Down Expand Up @@ -390,6 +396,7 @@ def recreate_with_f_clock(self, f_clock: int) -> "BitTiming":
tseg2=self.tseg2,
sjw=self.sjw,
nof_samples=self.nof_samples,
strict=True,
)
except ValueError:
pass
Expand Down Expand Up @@ -531,7 +538,7 @@ def __init__( # pylint: disable=too-many-arguments
:param int data_sjw:
The Synchronization Jump Width for the data phase. This value determines
the maximum number of time quanta that the controller can resynchronize every bit.
:param strict:
:param bool strict:
If True, restrict bit timings to the minimum required range as defined in
ISO 11898. This can be used to ensure compatibility across a wide variety
of CAN hardware.
Expand Down Expand Up @@ -649,6 +656,7 @@ def from_bitrate_and_segments(
data_tseg1: int,
data_tseg2: int,
data_sjw: int,
strict: bool = False,
) -> "BitTimingFd":
"""
Create a :class:`~can.BitTimingFd` instance with the bitrates and segments lengths.
Expand Down Expand Up @@ -677,6 +685,10 @@ def from_bitrate_and_segments(
:param int data_sjw:
The Synchronization Jump Width for the data phase. This value determines
the maximum number of time quanta that the controller can resynchronize every bit.
:param bool strict:
If True, restrict bit timings to the minimum required range as defined in
ISO 11898. This can be used to ensure compatibility across a wide variety
of CAN hardware.
:raises ValueError:
if the arguments are invalid.
"""
Expand All @@ -698,6 +710,7 @@ def from_bitrate_and_segments(
data_tseg1=data_tseg1,
data_tseg2=data_tseg2,
data_sjw=data_sjw,
strict=strict,
)

if abs(bt.nom_bitrate - nom_bitrate) > nom_bitrate / 256:
Expand Down Expand Up @@ -1007,6 +1020,7 @@ def recreate_with_f_clock(self, f_clock: int) -> "BitTimingFd":
data_tseg1=self.data_tseg1,
data_tseg2=self.data_tseg2,
data_sjw=self.data_sjw,
strict=True,
)
except ValueError:
pass
Expand Down
12 changes: 4 additions & 8 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,10 @@ def test_adjust_timing_fd(self):
)
assert new_timing.__class__ == BitTimingFd
assert new_timing.f_clock == 80_000_000
assert new_timing.nom_bitrate == 500_000
assert new_timing.nom_tseg1 == 119
assert new_timing.nom_tseg2 == 40
assert new_timing.nom_sjw == 40
assert new_timing.data_bitrate == 2_000_000
assert new_timing.data_tseg1 == 29
assert new_timing.data_tseg2 == 10
assert new_timing.data_sjw == 10
assert new_timing.nom_bitrate == timing.nom_bitrate
assert new_timing.nom_sample_point == timing.nom_sample_point
assert new_timing.data_bitrate == timing.data_bitrate
assert new_timing.data_sample_point == timing.data_sample_point

with pytest.raises(CanInitializationError):
check_or_adjust_timing_clock(timing, valid_clocks=[8_000, 16_000])
Expand Down

0 comments on commit 9288998

Please sign in to comment.