From dff0bbb54ba6d58a1a7bae44fe2ca3d084772494 Mon Sep 17 00:00:00 2001 From: Jacob Schaer Date: Thu, 7 Mar 2024 20:39:21 -0800 Subject: [PATCH 1/5] Add basic non-iso support --- can/interfaces/kvaser/canlib.py | 10 +++++++++- doc/interfaces/kvaser.rst | 5 +++++ doc/interfaces/pcan.rst | 7 +++++++ doc/interfaces/socketcan.rst | 5 +++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/can/interfaces/kvaser/canlib.py b/can/interfaces/kvaser/canlib.py index 4c621ecf4..b01779cca 100644 --- a/can/interfaces/kvaser/canlib.py +++ b/can/interfaces/kvaser/canlib.py @@ -415,6 +415,10 @@ 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 bool fd_non_iso: + Open the channel in Non-ISO (Bosch) FD mode. Only applies for FD buses. + This changes the handling of the stuff-bit counter and the CRC. Defaults + to False (ISO mode) :param bool driver_mode: Silent or normal. @@ -454,6 +458,7 @@ def __init__( accept_virtual = kwargs.get("accept_virtual", True) fd = isinstance(timing, BitTimingFd) if timing else kwargs.get("fd", False) data_bitrate = kwargs.get("data_bitrate", None) + fd_non_iso = kwargs.get("fd_non_iso", False) try: channel = int(channel) @@ -483,7 +488,10 @@ def __init__( if accept_virtual: flags |= canstat.canOPEN_ACCEPT_VIRTUAL if fd: - flags |= canstat.canOPEN_CAN_FD + if fd_non_iso: + flags |= canstat.canOPEN_CAN_FD_NONISO + else: + flags |= canstat.canOPEN_CAN_FD log.debug("Creating read handle to bus channel: %s", channel) self._read_handle = canOpenChannel(channel, flags) diff --git a/doc/interfaces/kvaser.rst b/doc/interfaces/kvaser.rst index f2c93f85b..5c7eb9f61 100644 --- a/doc/interfaces/kvaser.rst +++ b/doc/interfaces/kvaser.rst @@ -39,6 +39,11 @@ in the ``recv`` method. If a message does not match any of the filters, ``recv()`` will return None. +ISO/Non-ISO CAN FD Mode +----------------------- + +Kvaser devices in FD mode can be configured for either ISO or Non-ISO mode in the bus initialization with the ``fd_non_iso`` option. + Custom methods ~~~~~~~~~~~~~~ diff --git a/doc/interfaces/pcan.rst b/doc/interfaces/pcan.rst index 790264627..f6b19728a 100644 --- a/doc/interfaces/pcan.rst +++ b/doc/interfaces/pcan.rst @@ -37,7 +37,14 @@ Here is an example configuration file for using `PCAN-USB Date: Thu, 7 Mar 2024 20:57:12 -0800 Subject: [PATCH 2/5] Update test_kvaser.py Add unit test --- test/test_kvaser.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/test_kvaser.py b/test/test_kvaser.py index abaf7b38f..e9f062124 100644 --- a/test/test_kvaser.py +++ b/test/test_kvaser.py @@ -196,6 +196,7 @@ def test_can_timing(self): def test_canfd_timing(self): canlib.canSetBusParams.reset_mock() canlib.canSetBusParamsFd.reset_mock() + canlib.canOpenChannel.reset_mock() timing = can.BitTimingFd.from_bitrate_and_segments( f_clock=80_000_000, nom_bitrate=500_000, @@ -210,6 +211,27 @@ def test_canfd_timing(self): can.Bus(channel=0, interface="kvaser", timing=timing) canlib.canSetBusParams.assert_called_once_with(0, 500_000, 68, 11, 10, 1, 0) canlib.canSetBusParamsFd.assert_called_once_with(0, 2_000_000, 10, 9, 8) + canlib.canOpenChannel.assert_called_with(0, constants.canOPEN_CAN_FD | constants.canOPEN_ACCEPT_VIRTUAL) + + def test_canfd_non_iso(self): + canlib.canSetBusParams.reset_mock() + canlib.canSetBusParamsFd.reset_mock() + canlib.canOpenChannel.reset_mock() + timing = can.BitTimingFd.from_bitrate_and_segments( + f_clock=80_000_000, + nom_bitrate=500_000, + nom_tseg1=68, + nom_tseg2=11, + nom_sjw=10, + data_bitrate=2_000_000, + data_tseg1=10, + data_tseg2=9, + data_sjw=8, + ) + can.Bus(channel=0, interface="kvaser", timing=timing, fd_non_iso=True) + canlib.canSetBusParams.assert_called_once_with(0, 500_000, 68, 11, 10, 1, 0) + canlib.canSetBusParamsFd.assert_called_once_with(0, 2_000_000, 10, 9, 8) + canlib.canOpenChannel.assert_called_with(0, constants.canOPEN_CAN_FD_NONISO | constants.canOPEN_ACCEPT_VIRTUAL) def test_canfd_nondefault_data_bitrate(self): canlib.canSetBusParams.reset_mock() From 5217f8c9ec2fcfa847795792b1ddeffbb1f5b2dd Mon Sep 17 00:00:00 2001 From: Jacob Schaer Date: Mon, 11 Mar 2024 23:09:37 -0700 Subject: [PATCH 3/5] Update test_kvaser.py Black to pass premerge checklist --- test/test_kvaser.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/test_kvaser.py b/test/test_kvaser.py index e9f062124..7eeeaee07 100644 --- a/test/test_kvaser.py +++ b/test/test_kvaser.py @@ -211,7 +211,9 @@ def test_canfd_timing(self): can.Bus(channel=0, interface="kvaser", timing=timing) canlib.canSetBusParams.assert_called_once_with(0, 500_000, 68, 11, 10, 1, 0) canlib.canSetBusParamsFd.assert_called_once_with(0, 2_000_000, 10, 9, 8) - canlib.canOpenChannel.assert_called_with(0, constants.canOPEN_CAN_FD | constants.canOPEN_ACCEPT_VIRTUAL) + canlib.canOpenChannel.assert_called_with( + 0, constants.canOPEN_CAN_FD | constants.canOPEN_ACCEPT_VIRTUAL + ) def test_canfd_non_iso(self): canlib.canSetBusParams.reset_mock() @@ -231,7 +233,9 @@ def test_canfd_non_iso(self): can.Bus(channel=0, interface="kvaser", timing=timing, fd_non_iso=True) canlib.canSetBusParams.assert_called_once_with(0, 500_000, 68, 11, 10, 1, 0) canlib.canSetBusParamsFd.assert_called_once_with(0, 2_000_000, 10, 9, 8) - canlib.canOpenChannel.assert_called_with(0, constants.canOPEN_CAN_FD_NONISO | constants.canOPEN_ACCEPT_VIRTUAL) + canlib.canOpenChannel.assert_called_with( + 0, constants.canOPEN_CAN_FD_NONISO | constants.canOPEN_ACCEPT_VIRTUAL + ) def test_canfd_nondefault_data_bitrate(self): canlib.canSetBusParams.reset_mock() From f4c6aa1a456f551f7cc9be5cb7971325f2d479ce Mon Sep 17 00:00:00 2001 From: Jacob Schaer Date: Sat, 16 Mar 2024 12:23:07 -0700 Subject: [PATCH 4/5] Made code changes New enum for non-iso mode Documentation sections fixes Test update --- can/bus.py | 3 ++- can/interfaces/kvaser/canlib.py | 14 +++++++++----- doc/interfaces/kvaser.rst | 5 ----- doc/interfaces/pcan.rst | 4 ++-- test/test_kvaser.py | 3 ++- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/can/bus.py b/can/bus.py index c3a906757..27e7e8e42 100644 --- a/can/bus.py +++ b/can/bus.py @@ -44,7 +44,8 @@ class CanProtocol(Enum): """The CAN protocol type supported by a :class:`can.BusABC` instance""" CAN_20 = auto() - CAN_FD = auto() + CAN_FD = auto() # ISO Mode + CAN_FD_NON_ISO = auto() CAN_XL = auto() diff --git a/can/interfaces/kvaser/canlib.py b/can/interfaces/kvaser/canlib.py index b01779cca..85bcac28f 100644 --- a/can/interfaces/kvaser/canlib.py +++ b/can/interfaces/kvaser/canlib.py @@ -415,10 +415,6 @@ 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 bool fd_non_iso: - Open the channel in Non-ISO (Bosch) FD mode. Only applies for FD buses. - This changes the handling of the stuff-bit counter and the CRC. Defaults - to False (ISO mode) :param bool driver_mode: Silent or normal. @@ -433,6 +429,10 @@ def __init__( computer, set this to True or set single_handle to True. :param bool fd: If CAN-FD frames should be supported. + :param bool fd_non_iso: + Open the channel in Non-ISO (Bosch) FD mode. Only applies for FD buses. + This changes the handling of the stuff-bit counter and the CRC. Defaults + to False (ISO mode) :param bool exclusive: Don't allow sharing of this CANlib channel. :param bool override_exclusive: @@ -467,7 +467,11 @@ def __init__( self.channel = channel self.single_handle = single_handle - self._can_protocol = CanProtocol.CAN_FD if fd else CanProtocol.CAN_20 + self._can_protocol = CanProtocol.CAN_20 + if fd_non_iso: + self._can_protocol = CanProtocol.CAN_FD_NON_ISO + elif fd: + self._can_protocol = CanProtocol.CAN_FD log.debug("Initialising bus instance") num_channels = ctypes.c_int(0) diff --git a/doc/interfaces/kvaser.rst b/doc/interfaces/kvaser.rst index 5c7eb9f61..f2c93f85b 100644 --- a/doc/interfaces/kvaser.rst +++ b/doc/interfaces/kvaser.rst @@ -39,11 +39,6 @@ in the ``recv`` method. If a message does not match any of the filters, ``recv()`` will return None. -ISO/Non-ISO CAN FD Mode ------------------------ - -Kvaser devices in FD mode can be configured for either ISO or Non-ISO mode in the bus initialization with the ``fd_non_iso`` option. - Custom methods ~~~~~~~~~~~~~~ diff --git a/doc/interfaces/pcan.rst b/doc/interfaces/pcan.rst index f6b19728a..88ab6838d 100644 --- a/doc/interfaces/pcan.rst +++ b/doc/interfaces/pcan.rst @@ -38,9 +38,9 @@ Here is an example configuration file for using `PCAN-USB Date: Tue, 19 Mar 2024 16:11:46 -0700 Subject: [PATCH 5/5] Update bus.py Black cleanup --- can/bus.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can/bus.py b/can/bus.py index 27e7e8e42..b10bb01b9 100644 --- a/can/bus.py +++ b/can/bus.py @@ -44,7 +44,7 @@ class CanProtocol(Enum): """The CAN protocol type supported by a :class:`can.BusABC` instance""" CAN_20 = auto() - CAN_FD = auto() # ISO Mode + CAN_FD = auto() # ISO Mode CAN_FD_NON_ISO = auto() CAN_XL = auto()