Skip to content

Commit 2814a31

Browse files
Tinyu-Zhaolbuque
authored andcommitted
lib/base: Add Atom CAN Base support.
Signed-off-by: Tinyu-Zhao <[email protected]>
1 parent accdd35 commit 2814a31

File tree

6 files changed

+179
-1
lines changed

6 files changed

+179
-1
lines changed

docs/en/base/atom_can.rst

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
ATOM CAN Base
2+
==============
3+
4+
.. include:: ../refs/base.can.ref
5+
6+
The following products are supported:
7+
8+
================== ==================
9+
|Atom CAN| |Atomic CAN Base|
10+
================== ==================
11+
12+
13+
Micropython TX Example:
14+
15+
.. literalinclude:: ../../../examples/unit/can/stickc_plus2_can_tx_example.py
16+
:language: python
17+
:linenos:
18+
19+
20+
Micropython RX Example:
21+
22+
.. literalinclude:: ../../../examples/unit/can/dial_can_rx_example.py
23+
:language: python
24+
:linenos:
25+
26+
27+
UIFLOW2 TX Example:
28+
29+
|tx_example.png|
30+
31+
32+
UIFLOW2 RX Example:
33+
34+
|rx_example.png|
35+
36+
37+
.. only:: builder_html
38+
39+
|stickc_plus2_can_tx_example.m5f2|
40+
41+
|dial_can_rx_example.m5f2|
42+
43+
class ATOMCANBase
44+
-----------------
45+
46+
Constructors
47+
------------
48+
49+
.. class:: ATOMCANBase(id, tx, rx, mode, baudrate)
50+
51+
Create an ATOMCANBase object.
52+
53+
parameter is:
54+
55+
- ``id`` is the ID of the CAN bus
56+
- ``tx`` is the pin to use for transmitting data
57+
- ``rx`` is the pin to use for receiving data
58+
- ``mode`` is one of: NORMAL, NO_ACKNOWLEDGE, LISTEN_ONLY
59+
- ``baudrate`` is the baudrate of ATOMCANBase
60+
61+
UIFLOW2:
62+
63+
|init.png|
64+
65+
.. class:: ATOMCANBase(id, tx, rx, mode, prescaler, sjw, bs1, bs2, triple_sampling=False)
66+
:no-index:
67+
68+
Initialise the CAN bus with the given parameters:
69+
70+
- ``id`` is the ID of the CAN bus
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+
|init_advanced.png|
88+
89+
ATOMCANBase class inherits CAN class, See :ref:`hardware.CAN <hardware.CAN>` for more details.

docs/en/base/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Base
33

44
.. toctree::
55
:maxdepth: 1
6-
6+
7+
atom_can.rst
78
atom_socket.rst
89
motion.rst

docs/en/refs/base.can.ref

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.. |Atom CAN| image:: https://static-cdn.m5stack.com/resource/docs/products/atom/atom_can/atom_can_01.webp
2+
:target: https://docs.m5stack.com/en/atom/atom_can
3+
:height: 200px
4+
:width: 200px
5+
6+
.. |Atomic CAN Base| image:: https://static-cdn.m5stack.com/resource/docs/products/atom/Atomic%20CAN%20Base/img-8c8eb3c4-9657-4330-9e1a-a0f5f3290197.webp
7+
:target: https://docs.m5stack.com/en/atom/Atomic%20CAN%20Base
8+
:height: 200px
9+
:width: 200px
10+
11+
.. |init.png| image:: https://m5stack.oss-cn-shenzhen.aliyuncs.com/mpy_docs/base/can/init.png
12+
13+
.. |init_advanced.png| image:: https://m5stack.oss-cn-shenzhen.aliyuncs.com/mpy_docs/base/can/init_advanced.png
14+
15+
16+
.. |tx_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/can/tx_example.png
17+
18+
.. |stickc_plus2_can_tx_example.m5f2| raw:: html
19+
20+
<a
21+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/unit/can/stickc_plus2_can_tx_example.m5f2"
22+
target="_blank"
23+
>
24+
stickc_plus2_can_tx_example.m5f2
25+
</a>
26+
27+
.. |rx_example.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/can/rx_example.png
28+
29+
.. |dial_can_rx_example.m5f2| raw:: html
30+
31+
<a
32+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/unit/can/dial_can_rx_example.m5f2"
33+
target="_blank"
34+
>
35+
dial_can_rx_example.m5f2
36+
</a>

m5stack/libs/base/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: MIT
44

55
_attrs = {
6+
"ATOMCANBase": "atom_can",
67
"ATOMSocketBase": "atom_socket",
78
"Motion": "motion",
89
}

m5stack/libs/base/atom_can.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import m5can
6+
import sys
7+
8+
if sys.platform != "esp32":
9+
from typing import Literal
10+
from micropython import const
11+
12+
13+
class ATOMCANBase(m5can.CAN):
14+
_timing_table = {
15+
# prescaler, sjw, bs1, bs2, triple_sampling
16+
25000: (128, 3, 16, 8, False),
17+
50000: (80, 3, 15, 4, False),
18+
100000: (40, 3, 15, 4, False),
19+
125000: (32, 3, 15, 4, False),
20+
250000: (16, 3, 15, 4, False),
21+
500000: (8, 3, 15, 4, False),
22+
800000: (4, 3, 16, 8, False),
23+
1000000: (4, 3, 15, 4, False),
24+
}
25+
26+
def __init__(
27+
self,
28+
id: Literal[0, 1],
29+
port: list | tuple = None,
30+
mode: int = m5can.CAN.NORMAL,
31+
*args,
32+
**kwargs,
33+
):
34+
if len(kwargs) == 1:
35+
(prescaler, sjw, bs1, bs2, triple_sampling) = self._timing_table.get(
36+
kwargs.get("baudrate")
37+
)
38+
elif len(args) == 5:
39+
(prescaler, sjw, bs1, bs2, triple_sampling) = args
40+
super().__init__(
41+
0,
42+
mode,
43+
port[1],
44+
port[0],
45+
prescaler,
46+
sjw,
47+
bs1,
48+
bs2,
49+
triple_sampling,
50+
)

m5stack/libs/base/manifest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"base",
66
(
77
"__init__.py",
8+
"atom_can.py",
89
"atom_socket.py",
910
"motion.py",
1011
),

0 commit comments

Comments
 (0)