4
4
class CAN -- controller area network communication bus
5
5
======================================================
6
6
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
11
10
voltage levels on the bus.
12
11
13
- Example usage (works without anything connected) ::
12
+ Example usage for classic CAN controller in Loopback (transceiver-less) mode ::
14
13
15
14
from pyb import CAN
16
15
can = CAN(1, CAN.LOOPBACK)
17
16
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
18
17
can.send('message!', 123) # send a message with id 123
19
18
can.recv(0) # receive message on FIFO 0
20
19
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.
21
30
22
31
Constructors
23
32
------------
@@ -35,43 +44,48 @@ Constructors
35
44
- ``CAN(1) `` is on ``YA ``: ``(RX, TX) = (Y3, Y4) = (PB8, PB9) ``
36
45
- ``CAN(2) `` is on ``YB ``: ``(RX, TX) = (Y5, Y6) = (PB12, PB13) ``
37
46
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
-
49
47
Methods
50
48
-------
51
49
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)
53
52
54
53
Initialise the CAN bus with the given parameters:
55
54
56
55
- *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.
67
65
- *auto_restart * sets whether the controller will automatically try and restart
68
66
communications after entering the bus-off state; if this is disabled then
69
67
:meth: `~CAN.restart() ` can be used to leave the bus-off state
70
68
- *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
+
75
89
76
90
The time quanta tq is the basic unit of time for the CAN bus. tq is the CAN
77
91
prescaler value divided by PCLK1 (the frequency of internal peripheral bus 1);
@@ -140,17 +154,17 @@ Methods
140
154
- number of pending RX messages on fifo 0
141
155
- number of pending RX messages on fifo 1
142
156
143
- .. method :: CAN.setfilter(bank, mode, fifo, params, *, rtr)
157
+ .. method :: CAN.setfilter(bank, mode, fifo, params, *, rtr, extframe=False )
144
158
145
159
Configure a filter bank:
146
160
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 .
149
163
- *fifo * is which fifo (0 or 1) a message should be stored in, if it is accepted by this filter.
150
164
- *params * is an array of values the defines the filter. The contents of the array depends on the *mode * argument.
151
165
152
166
+-----------+---------------------------------------------------------+
153
- | *mode* |contents of *params* array |
167
+ | *mode* |Contents of *params* array for classic CAN controller |
154
168
+===========+=========================================================+
155
169
| CAN.LIST16 |Four 16 bit ids that will be accepted |
156
170
+-----------+---------------------------------------------------------+
@@ -165,10 +179,20 @@ Methods
165
179
| CAN.MASK32 |As with CAN.MASK16 but with only one 32 bit id/mask pair.|
166
180
+-----------+---------------------------------------------------------+
167
181
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.
172
196
173
197
+-----------+----------------------+
174
198
| *mode* |length of *rtr* array |
@@ -182,11 +206,17 @@ Methods
182
206
| CAN.MASK32 |1 |
183
207
+-----------+----------------------+
184
208
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)
186
214
187
215
Clear and disables a filter bank:
188
216
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).
190
220
191
221
.. method :: CAN.any(fifo)
192
222
@@ -200,21 +230,22 @@ Methods
200
230
- *list * is an optional list object to be used as the return value
201
231
- *timeout * is the timeout in milliseconds to wait for the receive.
202
232
203
- Return value: A tuple containing four values.
233
+ Return value: A tuple containing five values.
204
234
205
235
- The id of the message.
236
+ - A boolean that indicates if the message ID is standard or extended.
206
237
- A boolean that indicates if the message is an RTR message.
207
238
- The FMI (Filter Match Index) value.
208
239
- An array containing the data.
209
240
210
241
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).
212
243
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
215
246
from either a bytearray or an array of type 'B' or 'b', and this array must
216
247
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
218
249
will be resized inplace to the size of the data and filled in with that data.
219
250
The same list and memoryview objects can be reused in subsequent calls to
220
251
this method, providing a way of receiving data without using the heap.
@@ -225,7 +256,7 @@ Methods
225
256
# No heap memory is allocated in the following call
226
257
can.recv(0, lst)
227
258
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 )
229
260
230
261
Send a message on the bus:
231
262
@@ -236,6 +267,13 @@ Methods
236
267
a remote transmission request. If *rtr * is True then only the length
237
268
of *data * is used to fill in the DLC slot of the frame; the actual
238
269
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.
239
277
240
278
If timeout is 0 the message is placed in a buffer in one of three hardware
241
279
buffers and the method returns immediately. If all three buffers are in use
@@ -302,4 +340,10 @@ Constants
302
340
CAN.LIST32
303
341
CAN.MASK32
304
342
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