8000 docs/library/pyb.CAN: Update CAN docs to match revised API. · micropython/micropython@5cdf964 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5cdf964

Browse files
iabdalkaderdpgeorge
authored andcommitted
docs/library/pyb.CAN: Update CAN docs to match revised API.
1 parent 5562ed3 commit 5cdf964

File tree

1 file changed

+92
-48
lines changed

1 file changed

+92
-48
lines changed

docs/library/pyb.CAN.rst

Lines changed: 92 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,29 @@
44
class CAN -- controller area network communication bus
55
======================================================
66

7-
CAN implements the standard CAN communications protocol. At
8-
the physical level it consists of 2 lines: RX and TX. Note that
9-
to connect the pyboard to a CAN bus you must use a CAN transceiver
10-
to convert the CAN logic signals from the pyboard to the correct
7+
CAN implements support for classic CAN (available on F4, F7 MCUs) and CAN FD (H7 series) controllers.
8+
At the physical level CAN bus consists of 2 lines: RX and TX. Note that to connect the pyboard to a
9+
CAN bus you must use a CAN transceiver to convert the CAN logic signals from the pyboard to the correct
1110
voltage levels on the bus.
1211

13-
Example usage (works without anything connected)::
12+
Example usage for classic CAN controller in Loopback (transceiver-less) mode::
1413

1514
from pyb import CAN
1615
can = CAN(1, CAN.LOOPBACK)
1716
can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126)) # set a fil 10000 ter to receive messages with id=123, 124, 125 and 126
1817
can.send('message!', 123) # send a message with id 123
1918
can.recv(0) # receive message on FIFO 0
2019

20+
Example usage for CAN FD controller with all of the possible options enabled::
21+
22+
# FD frame + BRS mode + Extended frame ID. 500 Kbit/s for arbitration phase, 1Mbit/s for data phase.
23+
can = CAN(1, CAN.NORMAL, baudrate=500_000, brs_baudrate=1_000_000, sample_point=80)
24+
can.setfilter(0, CAN.RANGE, 0, (0xFFF0, 0xFFFF))
25+
can.send('a'*64, 0xFFFF, fdf=True, brs=True, extframe=True)
26+
can.recv(0)
27+
28+
The following CAN module functions and their arguments are available
29+
for both classic and FD CAN controllers, unless otherwise stated.
2130

2231
Constructors
2332
------------
@@ -35,43 +44,48 @@ Constructors
3544
- ``CAN(1)`` is on ``YA``: ``(RX, TX) = (Y3, Y4) = (PB8, PB9)``
3645
- ``CAN(2)`` is on ``YB``: ``(RX, TX) = (Y5, Y6) = (PB12, PB13)``
3746

38-
Class Methods
39-
-------------
40-
.. classmethod:: CAN.initfilterbanks(nr)
41-
42-
Reset and disable all filter banks and assign how many banks should be available for CAN(1).
43-
44-
STM32F405 has 28 filter banks that are shared between the two available CAN bus controllers.
45-
This function configures how many filter banks should be assigned to each. *nr* is the number of banks
46-
that will be assigned to CAN(1), the rest of the 28 are assigned to CAN(2).
47-
At boot, 14 banks are assigned to each controller.
48-
4947
Methods
5048
-------
5149

52-
.. method:: CAN.init(mode, extframe=False, prescaler=100, *, sjw=1, bs1=6, bs2=8, auto_restart=False, baudrate=0, sample_point=75)
50+
.. method:: CAN.init(mode, prescaler=100, *, sjw=1, bs1=6, bs2=8, auto_restart=False, baudrate=0, sample_point=75,
51+
num_filter_banks=14, brs_sjw=1, brs_bs1=8, brs_bs2=3, brs_baudrate=0, brs_sample_point=75)
5352

5453
Initialise the CAN bus with the given parameters:
5554

5655
- *mode* is one of: NORMAL, LOOPBACK, SILENT, SILENT_LOOPBACK
57-
- if *extframe* is True then the bus uses extended identifiers in the frames
58-
(29 bits); otherwise it uses standard 11 bit identifiers
59-
- *prescaler* is used to set the duration of 1 time quanta; the time quanta
60-
will be the input clock (PCLK1, see :meth:`pyb.freq()`) divided by the prescaler
61-
- *sjw* is the resynchronisation jump width in units of the time quanta;
62-
it can be 1, 2, 3, 4
63-
- *bs1* defines the location of the sample point in units of the time quanta;
64-
it can be between 1 and 1024 inclusive
65-
- *bs2* defines the location of the transmit point in units of the time quanta;
66-
it can be between 1 and 16 inclusive
56+
- *prescaler* is the value by which the CAN input clock is divided to generate the
57+
nominal bit time quanta. The prescaler can be a value between 1 and 1024 inclusive
58+
for classic CAN, and between 1 and 512 inclusive for CAN FD.
59+
- *sjw* is the resynchronisation jump width in units of time quanta for nominal bits;
60+
it can be a value between 1 and 4 inclusive for classic CAN, and between 1 and 128 inclusive for CAN FD.
61+
- *bs1* defines the location of the sample point in units of the time quanta for nominal bits;
62+
it can be a value between 1 and 16 inclusive for classic CAN, and between 2 and 256 inclusive for CAN FD.
63+
- *bs2* defines the location of the transmit point in units of the time quanta for nominal bits;
64+
it can be a value between 1 and 8 inclusive for classic CAN, and between 2 and 128 inclusive for CAN FD.
6765
- *auto_restart* sets whether the controller will automatically try and restart
6866
communications after entering the bus-off state; if this is disabled then
6967
:meth:`~CAN.restart()` can be used to leave the bus-off state
7068
- *baudrate* if a baudrate other than 0 is provided, this function will try to automatically
71-
calculate a CAN bit-timing (overriding *prescaler*, *bs1* and *bs2*) that satisfies both
72-
the baudrate and the desired *sample_point*.
73-
- *sample_point* given in a percentage of the bit time, the *sample_point* specifies the position
74-
of the last bit sample with respect to the whole bit time. The default *sample_point* is 75%.
69+
calculate the CAN nominal bit time (overriding *prescaler*, *bs1* and *bs2*) that satisfies
70+
both the baudrate and the desired *sample_point*.
71+
- *sample_point* given in a percentage of the nominal bit time, the *sample_point* specifies the position
72+
of the bit sample with respect to the whole nominal bit time. The default *sample_point* is 75%.
73+
- *num_filter_banks* for classic CAN, this is the number of banks that will be assigned to CAN(1),
74+
the rest of the 28 are assigned to CAN(2).
75+
- *brs_prescaler* is the value by which the CAN FD input clock is divided to generate the
76+
data bit time quanta. The prescaler can be a value between 1 and 32 inclusive.
77+
- *brs_sjw* is the resynchronisation jump width in units of time quanta for data bits;
78+
it can be a value between 1 and 16 inclusive
79+
- *brs_bs1* defines the location of the sample point in units of the time quanta for data bits;
80+
it can be a value between 1 and 32 inclusive
81+
- *brs_bs2* defines the location of the transmit point in units of the time quanta for data bits;
82+
it can be a value between 1 and 16 inclusive
83+
- *brs_baudrate* if a baudrate other than 0 is provided, this function will try to automatically
84+
calculate the CAN data bit time (overriding *brs_prescaler*, *brs_bs1* and *brs_bs2*) that satisfies
85+
both the baudrate and the desired *brs_sample_point*.
86+
- *brs_sample_point* given in a percentage of the data bit time, the *brs_sample_point* specifies the position
87+
of the bit sample with respect 2364 to the whole data bit time. The default *brs_sample_point* is 75%.
88+
7589

7690
The time quanta tq is the basic unit of time for the CAN bus. tq is the CAN
7791
prescaler value divided by PCLK1 (the frequency of internal peripheral bus 1);
@@ -140,17 +154,17 @@ Methods
140154
- number of pending RX messages on fifo 0
141155
- number of pending RX messages on fifo 1
142156

143-
.. method:: CAN.setfilter(bank, mode, fifo, params, *, rtr)
157+
.. method:: CAN.setfilter(bank, mode, fifo, params, *, rtr, extframe=False)
144158

145159
Configure a filter bank:
146160

147-
- *bank* is the filter bank that is to be configured.
148-
- *mode* is the mode the filter should operate in.
161+
- *bank* is the classic CAN controller filter bank, or CAN FD filter index, to configure.
162+
- *mode* is the mode the filter should operate in, see the tables below.
149163
- *fifo* is which fifo (0 or 1) a message should be stored in, if it is accepted by this filter.
150164
- *params* is an array of values the defines the filter. The contents of the array depends on the *mode* argument.
151165

152166
+-----------+---------------------------------------------------------+
153-
|*mode* |contents of *params* array |
167+
|*mode* |Contents of *params* array for classic CAN controller |
154168
+===========+=========================================================+
155169
|CAN.LIST16 |Four 16 bit ids that will be accepted |
156170
+-----------+---------------------------------------------------------+
@@ -165,10 +179,20 @@ Methods
165179
|CAN.MASK32 |As with CAN.MASK16 but with only one 32 bit id/mask pair.|
166180
+-----------+---------------------------------------------------------+
167181

168-
- *rtr* is an array of booleans that states if a filter should accept a
169-
remote transmission request message. If this argument is not given
170-
then it defaults to ``False`` for all entries. The length of the array
171-
depends on the *mode* argument.
182+
+-----------+---------------------------------------------------------+
183+
|*mode* |Contents of *params* array for CAN FD controller |
184+
+===========+=========================================================+
185+
|CAN.RANGE |Two ids that represent a range of accepted ids. |
186+
+-----------+---------------------------------------------------------+
187+
|CAN.DUAL |Two ids that will be accepted. For example (1, 2) |
188+
+-----------+---------------------------------------------------------+
189+
|CAN.MASK |One filter ID and a mask. For example (0x111, 0x7FF) |
190+
+-----------+---------------------------------------------------------+
191+
192+
- *rtr* For classic CAN controllers, this is an array of booleans that states if
193+
a filter should accept a remote transmission request message. If this argument
194+
is not given then it defaults to ``False`` for all entries. The length of the
195+
array depends on the *mode* argument. For CAN FD, this argument is ignored.
172196

173197
+-----------+----------------------+
174198
|*mode* |length of *rtr* array |
@@ -182,11 +206,17 @@ Methods
182206
|CAN.MASK32 |1 |
183207
+-----------+----------------------+
184208

185-
.. method:: CAN.clearfilter(bank)
209+
- *extframe* If True the frame will have an extended identifier (29 bits),
210+
otherwise a standard identifier (11 bits) is used.
211+
212+
213+
.. method:: CAN.clearfilter(bank, extframe=False)
186214

187215
Clear and disables a filter bank:
188216

189-
- *bank* is the filter bank that is to be cleared.
217+
- *bank* is the classic CAN controller filter bank, or CAN FD filter index, to clear.
218+
- *extframe* For CAN FD controllers, if True, clear an extended filter (configured with extframe=True),
219+
otherwise the clear a standard identifier (configured with extframe=False).
190220

191221
.. method:: CAN.any(fifo)
192222

@@ -200,21 +230,22 @@ Methods
200230
- *list* is an optional list object to be used as the return value
201231
- *timeout* is the timeout in milliseconds to wait for the receive.
202232

203-
Return value: A tuple containing four values.
233+
Return value: A tuple containing five values.
204234

205235
- The id of the message.
236+
- A boolean that indicates if the message ID is standard or extended.
206237
- A boolean that indicates if the message is an RTR message.
207238
- The FMI (Filter Match Index) value.
208239
- An array containing the data.
209240

210241
If *list* is ``None`` then a new tuple will be allocated, as well as a new
211-
bytes object to contain the data (as the fourth element in the tuple).
242+
bytes object to contain the data (as the fifth element in the tuple).
212243

213-
If *list* is not ``None`` then it should be a list object with a least four
214-
elements. The fourth element should be a memoryview object which is created
244+
If *list* is not ``None`` then it should be a list object with a least five
245+
elements. The fifth element should be a memoryview object which is created
215246
from either a bytearray or an array of type 'B' or 'b', and this array must
216247
have enough room for at least 8 bytes. The list object will then be
217-
populated with the first three return values above, and the memoryview object
248+
populated with the first four return values above, and the memoryview object
218249
will be resized inplace to the size of the data and filled in with that data.
219250
The same list and memoryview objects can be reused in subsequent calls to
220251
this method, providing a way of receiving data without using the heap.
@@ -225,7 +256,7 @@ Methods
225256
# No heap memory is allocated in the following call
226257
can.recv(0, lst)
227258

228-
.. method:: CAN.send(data, id, *, timeout=0, rtr=False)
259+
.. method:: CAN.send(data, id, *, timeout=0, rtr=False, extframe=False, fdf=False, brs=False)
229260

230261
Send a message on the bus:
231262

@@ -236,6 +267,13 @@ Methods
236267
a remote transmission request. If *rtr* is True then only the length
237268
of *data* is used to fill in the DLC slot of the frame; the actual
238269
bytes in *data* are unused.
270+
- *extframe* if True the frame will have an extended identifier (29 bits),
271+
otherwise a standard identifier (11 bits) is used.
272+
- *fdf* for CAN FD controllers, if set to True, the frame will have an FD
273+
frame format, which supports data payloads up to 64 bytes.
274+
- *brs* for CAN FD controllers, if set to True, the bitrate switching mode
275+
is enabled, in which the data phase is transmitted at a differet bitrate.
276+
See :meth:`CAN.init` for the data bit timing configuration parameters.
239277

240278
If timeout is 0 the message is placed in a buffer in one of three hardware
241279
buffers and the method returns immediately. If all three buffers are in use
@@ -302,4 +340,10 @@ Constants
302340
CAN.LIST32
303341
CAN.MASK32
304342

305-
The operation mode of a filter used in :meth:`~CAN.setfilter()`.
343+
The operation mode of a filter used in :meth:`~CAN.setfilter()` for classic CAN.
344+
345+
.. data:: CAN.DUAL
346+
CAN.RANGE
347+
CAN.MASK
348+
349+
The operation mode of a filter used in :meth:`~CAN.setfilter()` for CAN FD.

0 commit comments

Comments
 (0)
0