Skip to content

Commit 8a3ed83

Browse files
Tinyu-Zhaolbuque
authored andcommitted
lib/units: Add a method to initialize CAN.
Signed-off-by: Tinyu-Zhao <[email protected]>
1 parent b3e4371 commit 8a3ed83

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed

docs/en/refs/unit.can.ref

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
.. |init.png| image:: https://m5stack.oss-cn-shenzhen.aliyuncs.com/mpy_docs/unit/can/init.png
1212

13+
.. |init1.png| image:: https://m5stack.oss-cn-shenzhen.aliyuncs.com/mpy_docs/unit/can/init1.png
14+
15+
1316
.. |tx_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/can/tx_example.png
1417

1518
.. |stickc_plus2_can_tx_example.m5f2| raw:: html

docs/en/units/can.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,27 @@ Constructors
6363

6464
|init.png|
6565

66+
.. class:: CANUnit(tx, rx, mode, prescaler=32, sjw=3, bs1=15, bs2=4, triple_sampling=False)
67+
:no-index:
68+
69+
Initialise the CAN bus with the given parameters:
70+
71+
- ``tx`` is the pin to use for transmitting data
72+
- ``rx`` is the pin to use for receiving data
73+
- ``mode`` is one of: NORMAL, NO_ACKNOWLEDGE, LISTEN_ONLY
74+
- ``prescaler`` is the value by which the CAN input clock is divided to generate the
75+
nominal bit time quanta. The prescaler can be a value between 1 and 1024 inclusive
76+
for classic CAN.
77+
- ``sjw`` is the resynchronisation jump width in units of time quanta for nominal bits;
78+
it can be a value between 1 and 4 inclusive for classic CAN.
79+
- ``bs1`` defines the location of the sample point in units of the time quanta for nominal bits;
80+
it can be a value between 1 and 16 inclusive for classic CAN.
81+
- ``bs2`` defines the location of the transmit point in units of the time quanta for nominal bits;
82+
it can be a value between 1 and 8 inclusive for classic CAN.
83+
- ``triple_sampling`` is Enables triple sampling when the TWAI controller samples a bit
84+
85+
UIFLOW2:
86+
87+
|init1.png|
6688

6789
CANUnit class inherits CAN class, See :ref:`hardware.CAN <hardware.CAN>` for more details.

m5stack/libs/unit/can.py

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,54 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
from m5can import CAN
6-
7-
8-
class CANUnit(CAN):
9-
def __init__(self, port, mode, baudrate=125000):
10-
timing_table = {
11-
25000: (128, 16, 8, 3, False),
12-
50000: (80, 15, 4, 3, False),
13-
100000: (40, 15, 4, 3, False),
14-
125000: (32, 15, 4, 3, False),
15-
250000: (16, 15, 4, 3, False),
16-
500000: (8, 15, 4, 3, False),
17-
800000: (4, 16, 8, 3, False),
18-
1000000: (4, 15, 4, 3, False),
19-
}
20-
timing = timing_table.get(baudrate)
5+
import m5can
6+
import sys
7+
8+
if sys.platform != "esp32":
9+
from typing import Literal
10+
11+
12+
class CANUnit(m5can.CAN):
13+
_timing_table = {
14+
# prescaler, sjw, bs1, bs2, triple_sampling
15+
25000: (128, 3, 16, 8, False),
16+
50000: (80, 3, 15, 4, False),
17+
100000: (40, 3, 15, 4, False),
18+
125000: (32, 3, 15, 4, False),
19+
250000: (16, 3, 15, 4, False),
20+
500000: (8, 3, 15, 4, False),
21+
800000: (4, 3, 16, 8, False),
22+
1000000: (4, 3, 15, 4, False),
23+
}
24+
25+
def __init__(
26+
self,
27+
id: Literal[0, 1],
28+
port: list | tuple = None,
29+
mode: int = m5can.CAN.NORMAL,
30+
*args,
31+
**kwargs,
32+
):
33+
if len(kwargs) == 1:
34+
(prescaler, sjw, bs1, bs2, triple_sampling) = self._timing_table.get(
35+
kwargs.get("baudrate")
36+
)
37+
elif len(args) == 5:
38+
(prescaler, sjw, bs1, bs2, triple_sampling) = args
2139
super().__init__(
2240
0,
2341
mode,
2442
port[1],
2543
port[0],
26-
timing[0], # prescaler
27-
timing[3], # sjw
28-
timing[1], # bs1
29-
timing[2], # bs2
30-
timing[4], # triple_sampling
44+
prescaler,
45+
sjw,
46+
bs1,
47+
bs2,
48+
triple_sampling,
3149
)
3250

3351

52+
# can_0 = CANUnit(0, (33, 32), CANUnit.NORMAL, baudrate=1000000)
53+
# can_0 = CANUnit(0, (33, 32), CANUnit.NORMAL, 4, 3, 15, 4, False)
54+
3455
MiniCANUnit = CANUnit

0 commit comments

Comments
 (0)