Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions can/interfaces/seeedstudio/seeedstudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(
frame_type="STD",
operation_mode="normal",
bitrate=500000,
can_filters=None,
**kwargs,
):
"""
Expand All @@ -85,6 +86,12 @@ def __init__(
:param bitrate
CAN bus bit rate, selected from available list.

:param can_filters:
A list of CAN filter dictionaries, where each dictionary contains
the keys 'can_id' and 'can_mask'. For the SeeedBus interface,
this list must not contain more than one filter. Defaults to None
(i.e., no filter).

:raises can.CanInitializationError: If the given parameters are invalid.
:raises can.CanInterfaceNotImplementedError: If the serial module is not installed.
"""
Expand All @@ -94,11 +101,23 @@ def __init__(
"the serial module is not installed"
)

if can_filters is None:
can_filters = [{"can_id": 0x00, "can_mask": 0x00}]

if len(can_filters) > 1:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use the software fallback of python-can if the user passes multiple filters. Check the implementation in BusABC.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Idea will implement it

raise can.CanInitializationError(
f"The SeeedBus interface only supports one hardware filter, "
f"but {len(can_filters)} were provided."
)

# Get the first (and only) filter in can_filters
hw_filter = can_filters[0]

self.bit_rate = bitrate
self.frame_type = frame_type
self.op_mode = operation_mode
self.filter_id = bytearray([0x00, 0x00, 0x00, 0x00])
self.mask_id = bytearray([0x00, 0x00, 0x00, 0x00])
self.filter_id = struct.pack("<I", hw_filter["can_id"])
self.mask_id = struct.pack("<I", hw_filter["can_mask"])
self._can_protocol = CanProtocol.CAN_20

if not channel:
Expand Down
1 change: 1 addition & 0 deletions doc/changelog.d/1995.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added hardware filter support for SeeedBus during initialization.
18 changes: 17 additions & 1 deletion doc/interfaces/seeedstudio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ Configuration
timeout=0.1,
frame_type='STD',
operation_mode='normal',
bitrate=500000)
bitrate=500000,
can_filters=None)

CHANNEL
The serial port created by the USB device when connected.
Expand Down Expand Up @@ -75,3 +76,18 @@ BITRATE
- 20000
- 10000
- 5000

CAN_FILTERS
A list of can filter dictionaries. Defaults to None (i.e. no filtering).
Each filter dictionary should have the following keys:
- ``can_id``: The CAN ID to filter on.
- ``can_mask``: The mask to apply to the ID.

Example: ``[{"can_id": 0x11, "can_mask": 0x21},]``

**Hardware Limitation:** The Seeed Studio device only supports
a single hardware filter. This list must contain at most
one filter dictionary. A ``can.CanInitializationError`` will be
raised if more than one filter is provided.