Skip to content

Commit acb7acd

Browse files
committed
Added hardware filter support for SeeedBus during initialization
1 parent efd4de5 commit acb7acd

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

can/interfaces/seeedstudio/seeedstudio.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def __init__(
6363
frame_type="STD",
6464
operation_mode="normal",
6565
bitrate=500000,
66+
can_filters=None,
6667
**kwargs,
6768
):
6869
"""
@@ -85,6 +86,12 @@ def __init__(
8586
:param bitrate
8687
CAN bus bit rate, selected from available list.
8788
89+
:param can_filters:
90+
A list of CAN filter dictionaries, where each dictionary contains
91+
the keys 'can_id' and 'can_mask'. For the SeeedBus interface,
92+
this list must not contain more than one filter. Defaults to None
93+
(i.e., no filter).
94+
8895
:raises can.CanInitializationError: If the given parameters are invalid.
8996
:raises can.CanInterfaceNotImplementedError: If the serial module is not installed.
9097
"""
@@ -94,11 +101,23 @@ def __init__(
94101
"the serial module is not installed"
95102
)
96103

104+
if can_filters is None:
105+
can_filters = [{"can_id": 0x00, "can_mask": 0x00}]
106+
107+
if len(can_filters) > 1:
108+
raise can.CanInitializationError(
109+
f"The SeeedBus interface only supports one hardware filter, "
110+
f"but {len(can_filters)} were provided."
111+
)
112+
113+
# Get the first (and only) filter in can_filters
114+
hw_filter = can_filters[0]
115+
97116
self.bit_rate = bitrate
98117
self.frame_type = frame_type
99118
self.op_mode = operation_mode
100-
self.filter_id = bytearray([0x00, 0x00, 0x00, 0x00])
101-
self.mask_id = bytearray([0x00, 0x00, 0x00, 0x00])
119+
self.filter_id = struct.pack("<I", hw_filter["can_id"])
120+
self.mask_id = struct.pack("<I", hw_filter["can_mask"])
102121
self._can_protocol = CanProtocol.CAN_20
103122

104123
if not channel:

doc/changelog.d/1995.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added hardware filter support for SeeedBus during initialization.

doc/interfaces/seeedstudio.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ Configuration
4444
timeout=0.1,
4545
frame_type='STD',
4646
operation_mode='normal',
47-
bitrate=500000)
47+
bitrate=500000,
48+
can_filters=None)
4849

4950
CHANNEL
5051
The serial port created by the USB device when connected.
@@ -75,3 +76,18 @@ BITRATE
7576
- 20000
7677
- 10000
7778
- 5000
79+
80+
CAN_FILTERS
81+
A list of can filter dictionaries. Defaults to None (i.e. no filtering).
82+
Each filter dictionary should have the following keys:
83+
- ``can_id``: The CAN ID to filter on.
84+
- ``can_mask``: The mask to apply to the ID.
85+
86+
Example: ``[{"can_id": 0x11, "can_mask": 0x21},]``
87+
88+
**Hardware Limitation:** The Seeed Studio device only supports
89+
a single hardware filter. This list must contain at most
90+
one filter dictionary. A ``can.CanInitializationError`` will be
91+
raised if more than one filter is provided.
92+
93+

0 commit comments

Comments
 (0)