Skip to content

Commit 69ded91

Browse files
Tinyu-Zhaolbuque
authored andcommitted
lib: Add PwrCANModule, optimize Unit RS485.
Signed-off-by: Tinyu-Zhao <[email protected]>
1 parent 7d45749 commit 69ded91

File tree

9 files changed

+509
-54
lines changed

9 files changed

+509
-54
lines changed

docs/en/module/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Module
2222
plus.rst
2323
pm25.rst
2424
pps.rst
25+
pwrcan.rst
2526
rca.rst
2627
relay_2.rst
2728
rs232.rst

docs/en/module/pwrcan.rst

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
PwrCAN Module
2+
===============
3+
4+
.. include:: ../refs/module.pwrcan.ref
5+
6+
PwrCAN Module 13.2 is a multifunctional module designed for the PwrCAN bus, integrating isolated CAN communication and DC 9-24V power bus. The module also includes Pwr485 (with isolation) bus functionality and can provide isolated 5V power supply to the M5 host. The CAN communication part uses the CA-IS3050G isolated transceiver, and the RS485 part uses the CA-IS3082W isolated transceiver. The GPIOs related to CAN and RS485 communication can be selected through dip switches, and the 120-ohm terminal resistance at the CAN and RS485 outputs can also be selected through dip switches. The module's power bus supports DC 9-24V wide voltage input, with the DC socket directly connected to the HT3.96 and XT30 power parts. The built-in isolated power module F0505S-2WR3 provides power to the M5 host. This module is suitable for fields such as robot control, protocol conversion, industrial automation, automotive communication systems, intelligent transportation, and building automation.
7+
8+
Supported Products:
9+
10+
|PwrCANModule|
11+
12+
13+
Micropython Example:
14+
15+
.. literalinclude:: ../../../examples/module/pwrcan/pwrcan_cores3_example.py
16+
:language: python
17+
:linenos:
18+
19+
UIFLOW2 Example:
20+
21+
|example.png|
22+
23+
.. only:: builder_html
24+
25+
|pwrcan_cores3_example.m5f2|
26+
27+
class PwrCANModule
28+
------------------
29+
30+
Constructors
31+
------------
32+
.. class:: PwrCANModule(id, mode, tx, rx, prescaler=32, sjw=3, bs1=15, bs2=4, triple_sampling=False)
33+
:no-index:
34+
35+
Initialise the CAN bus with the given parameters:
36+
37+
- ``id`` is the can bus id
38+
- ``tx`` is the pin to use for transmitting data
39+
- ``rx`` is the pin to use for receiving data
40+
- ``mode`` is one of: NORMAL, NO_ACKNOWLEDGE, LISTEN_ONLY
41+
- ``prescaler`` is the value by which the CAN input clock is divided to generate the
42+
nominal bit time quanta. The prescaler can be a value between 1 and 1024 inclusive
43+
for classic CAN.
44+
- ``sjw`` is the resynchronisation jump width in units of time quanta for nominal bits;
45+
it can be a value between 1 and 4 inclusive for classic CAN.
46+
- ``bs1`` defines the location of the sample point in units of the time quanta for nominal bits;
47+
it can be a value between 1 and 16 inclusive for classic CAN.
48+
- ``bs2`` defines the location of the transmit point in units of the time quanta for nominal bits;
49+
it can be a value between 1 and 8 inclusive for classic CAN.
50+
- ``triple_sampling`` is Enables triple sampling when the TWAI controller samples a bit
51+
52+
UIFLOW2:
53+
54+
|init1.png|
55+
56+
Methods
57+
-------
58+
59+
PwrCANModule class inherits CAN class, See :ref:`hardware.CAN <hardware.CAN>` for more details.
60+
61+
62+
class PwrCANModuleRS485
63+
------------------
64+
65+
Constructors
66+
------------
67+
68+
.. class:: PwrCANModuleRS485(id, baudrate=9600, bits=8, parity=None, stop=1, *, ...)
69+
70+
Construct a UART object of the given id.
71+
72+
For more parameters, please refer to init.
73+
74+
UIFLOW2:
75+
76+
|init.png|
77+
78+
79+
Methods
80+
-------
81+
82+
.. method:: PwrCANModuleRS485.init(baudrate=9600, bits=8, parity=None, stop=1, *, ...)
83+
84+
Initialise the UART bus with the given parameters:
85+
86+
- *baudrate* is the clock rate.
87+
- *bits* is the number of bits per character, 7, 8 or 9.
88+
- *parity* is the parity, ``None``, 0 (even) or 1 (odd).
89+
- *stop* is the number of stop bits, 1 or 2.
90+
91+
Additional keyword-only parameters that may be supported by a port are:
92+
93+
- *tx* specifies the TX pin to use.
94+
- *rx* specifies the RX pin to use.
95+
- *rts* specifies the RTS (output) pin to use for hardware receive flow control.
96+
- *cts* specifies the CTS (input) pin to use for hardware transmit flow control.
97+
- *txbuf* specifies the length in characters of the TX buffer.
98+
- *rxbuf* specifies the length in characters of the RX buffer.
99+
- *timeout* specifies the time to wait for the first character (in ms).
100+
- *timeout_char* specifies the time to wait between characters (in ms).
101+
- *invert* specifies which lines to invert.
102+
103+
- ``0`` will not invert lines (idle state of both lines is logic high).
104+
- ``PwrCANModuleRS485.INV_TX`` will invert TX line (idle state of TX line now logic low).
105+
- ``PwrCANModuleRS485.INV_RX`` will invert RX line (idle state of RX line now logic low).
106+
- ``PwrCANModuleRS485.INV_TX | PwrCANModuleRS485.INV_RX`` will invert both lines (idle state at logic low).
107+
108+
- *flow* specifies which hardware flow control signals to use. The value
109+
is a bitmask.
110+
111+
- ``0`` will ignore hardware flow control signals.
112+
- ``PwrCANModuleRS485.RTS`` will enable receive flow control by using the RTS output pin to
113+
signal if the receive FIFO has sufficient space to accept more data.
114+
- ``PwrCANModuleRS485.CTS`` will enable transmit flow control by pausing transmission when the
115+
CTS input pin signals that the receiver is running low on buffer space.
116+
- ``PwrCANModuleRS485.RTS | PwrCANModuleRS485.CTS`` will enable both, for full hardware flow control.
117+
118+
119+
.. note::
120+
It is possible to call ``init()`` multiple times on the same object in
121+
order to reconfigure UART on the fly. That allows using single UART
122+
peripheral to serve different devices attached to different GPIO pins.
123+
Only one device can be served at a time in that case.
124+
Also do not call ``deinit()`` as it will prevent calling ``init()``
125+
again.
126+
127+
UIFLOW2:
128+
129+
|setup.png|
130+
131+
132+
.. method:: PwrCANModuleRS485.deinit()
133+
134+
Turn off the UART bus.
135+
136+
.. note::
137+
You will not be able to call ``init()`` on the object after ``deinit()``.
138+
A new instance needs to be created in that case.
139+
140+
UIFLOW2:
141+
142+
|deinit.png|
143+
144+
145+
.. method:: PwrCANModuleRS485.any()
146+
147+
Returns an integer counting the number of characters that can be read without
148+
blocking. It will return 0 if there are no characters available and a positive
149+
number if there are characters. The method may return 1 even if there is more
150+
than one character available for reading.
151+
152+
For more sophisticated querying of available characters use select.poll::
153+
154+
poll = select.poll()
155+
poll.register(uart, select.POLLIN)
156+
poll.poll(timeout)
157+
158+
UIFLOW2:
159+
160+
|any.png|
161+
162+
163+
.. method:: PwrCANModuleRS485.read([nbytes])
164+
165+
Read characters. If ``nbytes`` is specified then read at most that many bytes,
166+
otherwise read as much data as possible. It may return sooner if a timeout
167+
is reached. The timeout is configurable in the constructor.
168+
169+
Return value: a bytes object containing the bytes read in. Returns ``None``
170+
on timeout.
171+
172+
UIFLOW2:
173+
174+
|read_all.png|
175+
176+
|read_bytes.png|
177+
178+
|read_raw_data.png|
179+
180+
181+
.. method:: PwrCANModuleRS485.readinto(buf[, nbytes])
182+
183+
Read bytes into the ``buf``. If ``nbytes`` is specified then read at most
184+
that many bytes. Otherwise, read at most ``len(buf)`` bytes. It may return sooner if a timeout
185+
is reached. The timeout is configurable in the constructor.
186+
187+
Return value: number of bytes read and stored into ``buf`` or ``None`` on
188+
timeout.
189+
190+
UIFLOW2:
191+
192+
|readinto.png|
193+
194+
195+
.. method:: PwrCANModuleRS485.readline()
196+
197+
Read a line, ending in a newline character. It may return sooner if a timeout
198+
is reached. The timeout is configurable in the constructor.
199+
200+
Return value: the line read or ``None`` on timeout.
201+
202+
UIFLOW2:
203+
204+
|readline.png|
205+
206+
207+
.. method:: PwrCANModuleRS485.write(buf)
208+
209+
Write the buffer of bytes to the bus.
210+
211+
Return value: number of bytes written or ``None`` on timeout.
212+
213+
UIFLOW2:
214+
215+
|write.png|
216+
217+
|write1.png|
218+
219+
|write_line.png|
220+
221+
|write_list.png|
222+
223+
|write_raw_data.png|
224+
225+
|write_raw_data_list.png|
226+
227+
228+
.. method:: PwrCANModuleRS485.sendbreak()
229+
230+
Send a break condition on the bus. This drives the bus low for a duration
231+
longer than required for a normal transmission of a character.
232+
233+
UIFLOW2:
234+
235+
|sendbreak.png|
236+
237+
238+
.. method:: PwrCANModuleRS485.flush()
239+
240+
Waits until all data has been sent. In case of a timeout, an exception is raised. The timeout
241+
duration depends on the tx buffer size and the baud rate. Unless flow control is enabled, a timeout
242+
should not occur.
243+
244+
.. note::
245+
246+
For the rp2, esp8266 and nrf ports the call returns while the last byte is sent.
247+
If required, a one character wait time has to be added in the calling script.
248+
249+
UIFLOW2:
250+
251+
|flush.png|
252+
253+
254+
.. method:: PwrCANModuleRS485.txdone()
255+
256+
Tells whether all data has been sent or no data transfer is happening. In this case,
257+
it returns ``True``. If a data transmission is ongoing it returns ``False``.
258+
259+
.. note::
260+
261+
For the rp2, esp8266 and nrf ports the call may return ``True`` even if the last byte
262+
of a transfer is still being sent. If required, a one character wait time has to be
263+
added in the calling script.
264+
265+
UIFLOW2:
266+
267+
|txdone.png|

docs/en/refs/module.pwrcan.ref

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.. |PwrCANModule| image:: https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/docs/products/module/Module13.2-PwrCAN/4.webp
2+
:target: https://docs.m5stack.com/en/module/Module13.2-PwrCAN
3+
:height: 200px
4+
:width: 200px
5+
6+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/init.png
7+
.. |any.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/any.png
8+
.. |deinit.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/deinit.png
9+
.. |setup.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/setup.png
10+
.. |read_all.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/read_all.png
11+
.. |read_bytes.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/read_bytes.png
12+
.. |read_raw_data.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/read_raw_data.png
13+
.. |readline.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/read_line.png
14+
.. |readinto.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/readinto.png
15+
.. |write.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/write.png
16+
.. |write1.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/write1.png
17+
.. |write_line.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/write_line.png
18+
.. |write_list.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/write_list.png
19+
.. |write_raw_data.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/write_raw_data.png
20+
.. |write_raw_data_list.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/write_raw_data_list.png
21+
.. |txdone.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/txdone.png
22+
.. |flush.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/flush.png
23+
.. |sendbreak.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/sendbreak.png
24+
25+
.. |example.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/pwrcan/example.png
26+
27+
.. |pwrcan_cores3_example.m5f2| raw:: html
28+
29+
<a
30+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/module/pwrcan/pwrcan_cores3_example.m5f2"
31+
target="_blank"
32+
>
33+
pwrcan_cores3_example.m5f2
34+
</a>

0 commit comments

Comments
 (0)