Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update can.rst #7

Open
wants to merge 187 commits into
base: pr/5310
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
187 commits
Select commit Hold shift + click to select a range
96b8646
first commit
Sep 26, 2019
4b06c23
Enabled module
nos86 Sep 28, 2019
db0bb5a
Stable basic code for module loading in Python
nos86 Sep 29, 2019
263338c
Full initialization and dummy write
nos86 Oct 6, 2019
79540e3
added function to retrieve information of CAN STATUS
nos86 Oct 6, 2019
5c6a318
Update quickref.rst
nos86 Oct 6, 2019
83297b4
Function SEND and RECV implemented
nos86 Oct 6, 2019
e184574
improve object print
nos86 Oct 6, 2019
e4abe72
remove prefix CAN_ from constants
nos86 Oct 6, 2019
872ce73
implementation of hardware filter
nos86 Oct 8, 2019
b2e7e23
update includePath
nos86 Oct 8, 2019
d175e3c
Changed author
nos86 Dec 10, 2019
11cec6d
STM32 Harmonization: rename get_state() to state(), add info() and an…
nos86 Dec 10, 2019
1baa023
STM32 Harmonization: added restart() signature
nos86 Dec 10, 2019
175073d
STM32 Harmonization: move private methods in c file
nos86 Dec 10, 2019
b8a5c0e
Enabled compiler switch for module
nos86 Dec 10, 2019
195088f
Code Cleaning: Reordered the function sequence
nos86 Dec 10, 2019
a03c23b
STM32 Harmonization: init() returns mp_const_none and raise exception…
nos86 Dec 10, 2019
c0c8c8b
STM32 Harmonization: Alignment of init function to pyCan.
nos86 Dec 12, 2019
b362179
STM32 Harmonization: Alignment of send(...) and recv(...) methods to …
nos86 Dec 12, 2019
02cadd0
removed auto_restart option since the esp32 framework doesn't have th…
nos86 Dec 12, 2019
82017a9
Added signature for RX Callback; currently this method is not impleme…
nos86 Dec 13, 2019
a65026a
Python Interface has been moved from header to source
nos86 Dec 13, 2019
de8c71b
Removed redundant function
nos86 Dec 13, 2019
1e0d857
beautify
nos86 Dec 13, 2019
bbdfa6f
STM32 Harmonization: implemented same CAN_MODE
nos86 Dec 13, 2019
cf0f92a
minor fix
nos86 Dec 17, 2019
9e00b41
manage flags for loopback and extframe
nos86 Dec 17, 2019
8e45d11
removed unused input variable from any()
nos86 Dec 17, 2019
215dd81
bugfix
nos86 Dec 17, 2019
9d01908
Improved print function
nos86 Dec 17, 2019
7fed709
Implementation of restart function
nos86 Dec 18, 2019
38b2e6c
data type bugfix
nos86 Dec 18, 2019
bb0ed63
Added alert function
nos86 Dec 19, 2019
a508f4c
Added docs for ESP32 CAN
nos86 Dec 19, 2019
11cca71
Removed filter setup from init.
nos86 Dec 19, 2019
57efe79
STM32 Harmonization
nos86 Dec 19, 2019
3f04346
Integrated you docs with existing one
nos86 Dec 19, 2019
c346b59
Code cleaning
Dec 20, 2019
d13310e
WIP: first implementation of filters
Dec 20, 2019
3405345
WIP
Dec 20, 2019
c43aef3
Syntax Correction
nos86 Dec 21, 2019
68a3880
CAN configuration is stored in object struct
nos86 Dec 22, 2019
960435c
Bugfixes
nos86 Dec 22, 2019
7b9a9a9
WIP: implementation of setfilter()
nos86 Dec 22, 2019
96721c9
Completed setfilter()
nos86 Dec 23, 2019
8920885
added clearfilter()
nos86 Dec 23, 2019
2c6b6f4
Added unittest as example
nos86 Dec 23, 2019
5432a7d
removed machine_can.h
nos86 Dec 23, 2019
2616b9e
Bugfix
nos86 Dec 23, 2019
f5ed914
Docs updated
nos86 Dec 23, 2019
ef0ae9f
Update Author name
nos86 Dec 23, 2019
e5a7e9d
remove .vscode folder
nos86 Dec 23, 2019
66c8a32
Formatting sources
nos86 Jan 9, 2020
c807e7d
Removed additional constants and added in documentation
nos86 Jan 9, 2020
b07db3a
Documentation bugfix
nos86 Jan 9, 2020
c8afef6
removed implemented methods
nos86 Jan 9, 2020
b952513
beautify: brackets inline
nos86 Jan 30, 2020
faf2e90
documentation update: from esp32 specifc module to machine module
nos86 Jan 30, 2020
d959762
revert unnecessary changes
nos86 Jan 30, 2020
3cf09b0
code beautify
nos86 Feb 4, 2020
1e7e038
Beautify
nos86 Feb 9, 2020
0129d02
Merge branch 'master' into pr/5310
IhorNehrutsa Jun 8, 2021
0f81b62
Merge branch 'master' into pr/5310
IhorNehrutsa Jun 11, 2021
8fba25f
esp32: modmachine.c Fix after merge from master
IhorNehrutsa Jun 11, 2021
6dabef8
esp32: Add machine_can.c to CMakeLists.txt
IhorNehrutsa Jun 11, 2021
cd92379
Merge branch 'micropython:master' into pr/5310
IhorNehrutsa Jun 11, 2021
023342b
esp32:machine_can
IhorNehrutsa Jun 11, 2021
2b6b1a7
esp32:CAN Add TWAI block diagram
IhorNehrutsa Jun 11, 2021
0e0686e
esp32:machine_can Replace ESP_STATUS_CHECK() to check_esp_err()
IhorNehrutsa Jun 11, 2021
730148d
esp32:machine_can.c Replace mp_raise_OSError(-status) to check_esp_err()
IhorNehrutsa Jun 11, 2021
3b9610a
Update esp32.rst
IhorNehrutsa Jun 11, 2021
d286a88
Update machine.CAN.rst
IhorNehrutsa Jun 11, 2021
f0d7a41
esp32: CAN docs fix
IhorNehrutsa Jun 11, 2021
73ef89b
docs: machine.CAN.rst: Add CAN modes images.
IhorNehrutsa Jun 14, 2021
94316fe
.gitignore: Add docs/_build directory under Windows
Jun 15, 2021
60d8fef
Merge branch 'pr/5310' of https://github.com/IhorNehrutsa/micropython…
Jun 15, 2021
6ed2fa5
ESP32: machine_can.c: Remove meaningless comparison
Jun 18, 2021
3175cf6
ESP32: machine_can.c: Convert timeout in ms to RTOS ticks pdMS_TO_TIC…
Jun 18, 2021
e947db4
ESP32: machine_can.c: Simplify error messages
Jun 18, 2021
cea632b
ESP32: machine_can.c: Use LOOPBACK_MASK instead of magic 0x10
Jun 18, 2021
05d119e
ESP32: machine_can.c: Use direct baudrate values in bit/s instead of …
Jun 18, 2021
c74423a
ESP32: machine_can.c: Dict more informative than list
Jun 18, 2021
0df569d
ESP32: machine_can.c: Add some comments
Jun 18, 2021
51be1ab
ESP32: machine_can.c: Set default inits
Jun 18, 2021
99b827c
esp32: CAN: Use twai.h
IhorNehrutsa Jun 18, 2021
85984fa
esp32: CAN: Remove trailing spaces
IhorNehrutsa Jun 18, 2021
7e8a8f4
esp32: machine_can.c fix initialisation argument ARG_bs2
IhorNehrutsa Jul 8, 2021
a6c3c65
first commit
Sep 26, 2019
60be660
Enabled module
nos86 Sep 28, 2019
831d9ab
Stable basic code for module loading in Python
nos86 Sep 29, 2019
a3ca589
Full initialization and dummy write
nos86 Oct 6, 2019
883435a
added function to retrieve information of CAN STATUS
nos86 Oct 6, 2019
5b42a78
Update quickref.rst
nos86 Oct 6, 2019
6449e7f
Function SEND and RECV implemented
nos86 Oct 6, 2019
ea189d0
improve object print
nos86 Oct 6, 2019
086ed59
remove prefix CAN_ from constants
nos86 Oct 6, 2019
37f445a
implementation of hardware filter
nos86 Oct 8, 2019
f1fa355
update includePath
nos86 Oct 8, 2019
c07e0ab
Changed author
nos86 Dec 10, 2019
537ad1c
STM32 Harmonization: rename get_state() to state(), add info() and an…
nos86 Dec 10, 2019
817dea5
STM32 Harmonization: added restart() signature
nos86 Dec 10, 2019
617680d
STM32 Harmonization: move private methods in c file
nos86 Dec 10, 2019
2b2fd9f
Enabled compiler switch for module
nos86 Dec 10, 2019
3098d65
Code Cleaning: Reordered the function sequence
nos86 Dec 10, 2019
caddb54
STM32 Harmonization: init() returns mp_const_none and raise exception…
nos86 Dec 10, 2019
1c80c6e
STM32 Harmonization: Alignment of init function to pyCan.
nos86 Dec 12, 2019
cdcc19c
STM32 Harmonization: Alignment of send(...) and recv(...) methods to …
nos86 Dec 12, 2019
b0c68a7
removed auto_restart option since the esp32 framework doesn't have th…
nos86 Dec 12, 2019
dcb65b6
Added signature for RX Callback; currently this method is not impleme…
nos86 Dec 13, 2019
5e8a6c6
Python Interface has been moved from header to source
nos86 Dec 13, 2019
d47fec4
Removed redundant function
nos86 Dec 13, 2019
eac0a8c
beautify
nos86 Dec 13, 2019
39c1dae
STM32 Harmonization: implemented same CAN_MODE
nos86 Dec 13, 2019
08590f9
minor fix
nos86 Dec 17, 2019
07e172d
manage flags for loopback and extframe
nos86 Dec 17, 2019
84f4147
removed unused input variable from any()
nos86 Dec 17, 2019
cf7d349
bugfix
nos86 Dec 17, 2019
5bfdd02
Improved print function
nos86 Dec 17, 2019
cbcb18f
Implementation of restart function
nos86 Dec 18, 2019
dde3edf
data type bugfix
nos86 Dec 18, 2019
314c113
Added alert function
nos86 Dec 19, 2019
3a540c7
Added docs for ESP32 CAN
nos86 Dec 19, 2019
f27aa39
Removed filter setup from init.
nos86 Dec 19, 2019
2aa0185
STM32 Harmonization
nos86 Dec 19, 2019
c6997c0
Integrated you docs with existing one
nos86 Dec 19, 2019
39f7295
Code cleaning
Dec 20, 2019
158c6ef
WIP: first implementation of filters
Dec 20, 2019
c2904eb
WIP
Dec 20, 2019
513448d
Syntax Correction
nos86 Dec 21, 2019
673039b
CAN configuration is stored in object struct
nos86 Dec 22, 2019
787fe86
Bugfixes
nos86 Dec 22, 2019
59b3127
WIP: implementation of setfilter()
nos86 Dec 22, 2019
6acf0cc
Completed setfilter()
nos86 Dec 23, 2019
6130a68
added clearfilter()
nos86 Dec 23, 2019
13e41a5
Added unittest as example
nos86 Dec 23, 2019
8331ac9
removed machine_can.h
nos86 Dec 23, 2019
d2f6bb6
Bugfix
nos86 Dec 23, 2019
53dfb94
Docs updated
nos86 Dec 23, 2019
8977bd7
Update Author name
nos86 Dec 23, 2019
670f6fa
remove .vscode folder
nos86 Dec 23, 2019
ca75414
Formatting sources
nos86 Jan 9, 2020
d407c3e
Removed additional constants and added in documentation
nos86 Jan 9, 2020
51f021a
Documentation bugfix
nos86 Jan 9, 2020
a383e56
removed implemented methods
nos86 Jan 9, 2020
ac1e238
beautify: brackets inline
nos86 Jan 30, 2020
8ec3b94
documentation update: from esp32 specifc module to machine module
nos86 Jan 30, 2020
27bd666
revert unnecessary changes
nos86 Jan 30, 2020
4ef5a43
code beautify
nos86 Feb 4, 2020
9de1bf0
Beautify
nos86 Feb 9, 2020
efc173f
esp32: modmachine.c Fix after merge from master
IhorNehrutsa Jun 11, 2021
1b7e3b8
esp32: Add machine_can.c to CMakeLists.txt
IhorNehrutsa Jun 11, 2021
7a8fe5e
esp32:machine_can
IhorNehrutsa Jun 11, 2021
f467fc1
esp32:CAN Add TWAI block diagram
IhorNehrutsa Jun 11, 2021
ad45ac1
esp32:machine_can Replace ESP_STATUS_CHECK() to check_esp_err()
IhorNehrutsa Jun 11, 2021
d4fe9bf
esp32:machine_can.c Replace mp_raise_OSError(-status) to check_esp_err()
IhorNehrutsa Jun 11, 2021
5a27f0f
Update esp32.rst
IhorNehrutsa Jun 11, 2021
773b456
Update machine.CAN.rst
IhorNehrutsa Jun 11, 2021
bd9fea8
esp32: CAN docs fix
IhorNehrutsa Jun 11, 2021
834d966
.gitignore: Add docs/_build directory under Windows
Jun 15, 2021
012ccba
docs: machine.CAN.rst: Add CAN modes images.
IhorNehrutsa Jun 14, 2021
5e8df29
ESP32: machine_can.c: Remove meaningless comparison
Jun 18, 2021
870d451
ESP32: machine_can.c: Convert timeout in ms to RTOS ticks pdMS_TO_TIC…
Jun 18, 2021
954a099
ESP32: machine_can.c: Simplify error messages
Jun 18, 2021
7908d71
ESP32: machine_can.c: Use LOOPBACK_MASK instead of magic 0x10
Jun 18, 2021
519ee68
ESP32: machine_can.c: Use direct baudrate values in bit/s instead of …
Jun 18, 2021
7a3c239
ESP32: machine_can.c: Dict more informative than list
Jun 18, 2021
7e545a2
ESP32: machine_can.c: Add some comments
Jun 18, 2021
54ea3cb
ESP32: machine_can.c: Set default inits
Jun 18, 2021
d6ac976
esp32: CAN: Use twai.h
IhorNehrutsa Jun 18, 2021
4204497
esp32: CAN: Remove trailing spaces
IhorNehrutsa Jun 18, 2021
bea5e26
esp32: machine_can.c fix initialisation argument ARG_bs2
IhorNehrutsa Jul 8, 2021
111247b
Add to can docs
anfroholic Jul 13, 2021
9c3daa0
Merge branch 'pr/5310' of https://github.com/IhorNehrutsa/micropython…
IhorNehrutsa Aug 30, 2021
0f870cb
Fix #endif after rebase
IhorNehrutsa Sep 7, 2021
4d1d644
WIP
IhorNehrutsa Sep 7, 2021
b2181e6
Update .gitignore
IhorNehrutsa Sep 8, 2021
51046ce
Update quickref.rst
IhorNehrutsa Sep 8, 2021
17f2256
Update machine.rst
IhorNehrutsa Sep 8, 2021
c55a7c5
Update Makefile
IhorNehrutsa Sep 8, 2021
b6bbae3
Update modmachine.c
IhorNehrutsa Sep 8, 2021
cb8612b
Merge branch 'master' into pr/5310
IhorNehrutsa Sep 8, 2021
ebba08b
Merge remote-tracking branch 'upstream/master' into pr/5310
IhorNehrutsa Oct 7, 2022
e6baddf
WIP
IhorNehrutsa Nov 13, 2022
110790f
quickref.rst
IhorNehrutsa Feb 25, 2023
c0268d2
Update machine.CAN.rst
anfroholic Mar 4, 2023
33eaf25
Update machine.CAN.rst
anfroholic Mar 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# Build directories
######################
build/
_build/
build-*/

# Test failure outputs
Expand Down
Binary file added docs/esp32/img/twai_blockdiag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions docs/esp32/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,30 @@ users is encouraged. Based on this feedback, the I2S class API and implementati

ESP32 has two I2S buses with id=0 and id=1

CAN bus
-------

See :ref:`machine.CAN <machine.CAN>` ::

The CAN driver is based on hardware implementation.
Any available output-capablepins can be used for TX, RX, BUS-OFF, and CLKOUT signal lines.

.. image:: img/twai_blockdiag.png

The driver is accessed via the :ref:`machine.CAN <machine.CAN>` class::

from machine import CAN
can = CAN(0, tx=5, rx=4, mode=CAN.NORMAL, baudrate=500000)
can.setfilter(0, CAN.FILTER_ADDRESS, [0x102]) # set a filter to receive messages with id = 0x102
can.send([1,2,3], 0x102) # send a message with id 123
can.recv() # receive message

can.any() # returns True if there are any message to receive
can.info() # get information about the controller’s error states and TX and RX buffers
can.deinit() # turn off the can bus
can.clear_rx_queue() # clear messages in the FIFO
can.clear_tx_queue() # clear messages in the transmit buffer

Real time clock (RTC)
---------------------

Expand Down
Binary file added docs/library/img/can_mode_listen_only.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/library/img/can_mode_loopback.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/library/img/can_mode_normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/library/img/can_mode_silent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/library/img/can_mode_silent_loopback.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
242 changes: 242 additions & 0 deletions docs/library/machine.CAN.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
.. currentmodule:: machine
.. _machine.CAN:

class CAN -- controller area network communication bus
======================================================

CAN implements the standard CAN communications protocol. At
the physical level it consists of 2 lines: RX and TX. Note that
to connect the microcontroller to a CAN bus you must use a CAN transceiver
to convert the CAN logic signals from the microcontroller to the correct
voltage levels on the bus.

Example usage (must have transceiver connected)::

from machine import CAN
can = CAN(0, tx=4, rx=16, extframe=True, mode=CAN.LOOPBACK, baudrate=500000)
can.setfilter(0, CAN.FILTER_ADDRESS, [0x102, 0]) # set a filter to receive messages with id = 0x102

can.send([1,2,3], 123) # send a message with id 123 and payload b'\x01\x02\x03'
can.recv() # receive message
can.any() # returns True if FIFO is not empty, else False
can.info() # get information about the controller’s error states and TX and RX buffers
can.deinit() # turn off the can bus
can.clear_rx_queue() # clear messages in the receive buffer
can.clear_tx_queue() # clear messages in the transmit buffer


Constructors
------------

.. class:: machine.CAN(bus, ...)

Construct a CAN object on the given bus(controller). *bus* can be 0 or 1 for STM32 and 0 for ESP32.
With no additional parameters, the CAN object is created but not
initialised (it has the settings from the last initialisation of
the bus, if any). If extra arguments are given, the bus is initialised.
See :meth:`CAN.init` for parameters of initialisation.

The physical pins of the CAN bus can be assigned during init.

Methods
-------

.. method:: CAN.init(bus, *, tx, rx, baudrate, prescaler, mode, extframe=False)

Initialise the CAN bus with the given parameters:

- *bus* ESP32 has only one bus, but still must be defined
- *mode* is one of: NORMAL, LOOPBACK, SILENT, SILENT_LOOPBACK
- if *extframe* is True then the bus uses extended identifiers in the frames
(29 bits); otherwise it uses standard 11 bit identifiers
- *baudrate* is used to define a standard speed. Standard speeds are 25000, 50000, 100000, 125000, 250000, 500000, 1000000
- *tx* defines the gpio used for transmission
- *rx* defines the gpio used for receiving
- *bus_off* defines the gpio used for BUS-OFF signal line(optional)
- *clkout* defines the gpio used for CLKOUT signal line(optional)
- *tx_queue* defines the number of waiting tx messages can be stored
- *rx_queue* defines the number of received messages can be stored



.. method:: CAN.deinit()

Turn off the CAN bus.

.. method:: CAN.restart()

Force a software restart of the CAN controller without resetting its
configuration.

If the controller enters the bus-off state then it will no longer participate
in bus activity. If the controller is not configured to automatically restart
(see :meth:`~CAN.init()`) then this method can be used to trigger a restart,
and the controller will follow the CAN protocol to leave the bus-off state and
go into the error active state.

.. method:: CAN.state()

Return the state of the controller. The return value can be one of:

- ``CAN.STOPPED`` -- the controller is completely off and reset;
- ``CAN.ERROR_ACTIVE`` -- the controller is on and in the Error Active state
(both TEC and REC are less than 96);
- ``CAN.BUS_OFF`` -- the controller is on but not participating in bus activity
(TEC overflowed beyond 255).
- ``CAN.RECOVERING`` -- the controller is under recover from bus-off state;


.. method:: CAN.info()

Get information about the controller's error states and TX and RX buffers.
Returns a dict

The values in the dict are:

- state: current state of the bus
- tx_failed_count: number of failed transmissions
- msgs_to_tx: messages in outbox ready to transmit
- rx_error_counter: number of errors on message recieve
- msgs_to_rx: number of messages in outbox
- arb_lost_count: number of times controller has lost arbitration
- rx_missed_count: number of times inbox has overflowed and message was lost
- bus_error_count: number of times there was an error on the bus
- tx_error_counter: number of times there was an error on message transmission

.. method:: CAN.setfilter(bank, mode, fifo, params, \*, rtr)

Configure a filter bank:

- *bank* is the filter bank that is to be configured (0 for extended, 0 or 1 for standard msg)
- *mode* is the mode the filter should operate in.
- *params* is an array of two values that defines the filter.
The first element will be the id to filter and the second element will be the mask to apply.
mask bit implementation considers 1 as a don't care state and 0 as a check state.

+-----------------------+----------------------------------------------+
| *mode* | contents of *params* array |
+=======================+==============================================+
| CAN.FILTER_RAW_SINGLE | *params* will be copied in hardware variable |
| | and single_filter_mode will be selected |
| | In this mode, *bank* will be ignored |
+-----------------------+----------------------------------------------+
| CAN.FILTER_RAW_DUAL | *params* will be copied in hardware variable |
| | and single_filter_mode will be cleared |
| | In this mode, *bank* will be ignored |
+-----------------------+----------------------------------------------+
| CAN.FILTER_ADDRESS | *params* will be set in hardware registers |
| | according to *bank* selection |
+-----------------------+----------------------------------------------+

- *rtr* is bool that states if a filter should accept a remote transmission request message.
If this argument is not given then it defaults to ``False``.

.. method:: CAN.clearfilter(bank)

Clear and disables all filters

.. method:: CAN.any(fifo)

Return ``True`` if any message waiting on the FIFO, else ``False``.

.. method:: CAN.recv(list=None, \*, timeout=5000)

Receive data on the bus:

- *list* is an optional list object to be used as the return value
- *timeout* is the timeout in milliseconds to wait for the receive.

Return value: A tuple containing four values.

- The id of the message.
- A boolean that indicates if the message is an RTR message.
- Reserved.
- An array containing the data.

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

If *list* is not ``None`` then it should be a list object with a least four
elements. The fourth element should be a memoryview object which is created
from either a bytearray or an array of type 'B' or 'b', and this array must
have enough room for at least 8 bytes. The list object will then be
populated with the first three return values above, and the memoryview object
will be resized inplace to the size of the data and filled in with that data.
The same list and memoryview objects can be reused in subsequent calls to
this method, providing a way of receiving data without using the heap.
For example::

buf = bytearray(8)
lst = [0, 0, 0, memoryview(buf)]
# No heap memory is allocated in the following call
can.recv(lst, timeout=0)

.. method:: CAN.send(data, id, \*, timeout=0, rtr=False)

Send a message on the bus:

- *data* is the data to send (an integer to send, or a buffer object).
- *id* is the id of the message to be sent.
- *timeout* is the timeout in milliseconds to wait for the send.
- *rtr* is a boolean that specifies if the message shall be sent as
a remote transmission request. If *rtr* is True then only the length
of *data* is used to fill in the DLC slot of the frame; the actual
bytes in *data* are unused.

If timeout is 0 the message is placed in a buffer and the method returns
immediately. If all three buffers are in use an exception is thrown.
If timeout is not 0, the method waits until the message is transmitted.
If the message can't be transmitted within the specified time an exception
is thrown.

Return value: ``None``.

.. method:: CAN.clear_tx_queue()

Clear all messages from transmitting queue.

.. method:: CAN.clear_rx_queue()

Clear all messages from receiving queue.


Constants
---------

.. data:: CAN.NORMAL
CAN.LOOPBACK
CAN.SILENT
CAN.SILENT_LOOPBACK
CAN.LISTEN_ONLY


The mode of the CAN bus used in :meth:`~CAN.init()`.

+---------------------+---------------------------------------------+-------+-------+
| *mode* | \ | STM32 | ESP32 |
+=====================+=============================================+=======+=======+
| CAN.NORMAL | .. image:: img/can_mode_normal.png | + | + |
+---------------------+---------------------------------------------+-------+-------+
| CAN.LOOPBACK | .. image:: img/can_mode_loopback.png | + | + |
+---------------------+---------------------------------------------+-------+-------+
| CAN.SILENT | .. image:: img/can_mode_silent.png | + | + |
+---------------------+---------------------------------------------+-------+-------+
| CAN.SILENT_LOOPBACK | .. image:: img/can_mode_silent_loopback.png | + | |
+---------------------+---------------------------------------------+-------+-------+
| CAN.LISTEN_ONLY | .. image:: img/can_mode_listen_only.png | | + |
+---------------------+---------------------------------------------+-------+-------+


.. data:: CAN.STOPPED
CAN.ERROR_ACTIVE
CAN.BUS_OFF
CAN.RECOVERING

Possible states of the CAN controller returned from :meth:`~CAN.state()`.

.. data:: CAN.FILTER_RAW_SINGLE
CAN.FILTER_RAW_DUAL
CAN.FILTER_ADDRESS

The operation mode of a filter used in :meth:`~CAN.setfilter()`.

1 change: 1 addition & 0 deletions docs/library/machine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ Classes
machine.SPI.rst
machine.I2C.rst
machine.I2S.rst
machine.CAN.rst
machine.RTC.rst
machine.Timer.rst
machine.WDT.rst
Expand Down
67 changes: 67 additions & 0 deletions examples/esp32_can.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from machine import CAN
import time


def send_and_check(can_bus, name, id, expected_result=True, extended=False):
can_bus.clear_tx_queue()
can_bus.clear_rx_queue()
can_bus.send([], id, extframe=extended)
time.sleep_ms(100)
if can_bus.any() == expected_result:
print("{}: OK".format(name))
if expected_result:
can_bus.recv()
else:
print("{}: FAILED".format(name))


# 4 and 5 pins must be connected to each other, see documentation
dev = CAN(0, extframe=False, tx=5, rx=4, mode=CAN.SILENT_LOOPBACK, baudrate=50000, auto_restart=False)

# Test send/receive message
print("Loopback Test: no filter - STD")
send_and_check(dev, "No filter", 0x100, True)

# Set filter1
print("Loopback Test: one filter - STD")
dev.setfilter(0, CAN.FILTER_ADDRESS, [0x101, 0])
send_and_check(dev, "Passing Message", 0x101, True)
send_and_check(dev, "Blocked Message", 0x100, False)

# Set filter2
print("Loopback Test: second filter - STD")
dev.setfilter(0, CAN.FILTER_ADDRESS, [0x102, 0])
send_and_check(dev, "Passing Message - Bank 1", 0x102, True)
send_and_check(dev, "Passing Message - Bank 0", 0x101, True)
send_and_check(dev, "Blocked Message", 0x100, False)

# Remove filter
print("Loopback Test: clear filter - STD")
dev.clearfilter()
send_and_check(dev, "Passing Message - Bank 1", 0x102, True)
send_and_check(dev, "Passing Message - Bank 0", 0x101, True)
send_and_check(dev, "Passing any Message", 0x100, True)

# Extended message tests
# Move to Extended
dev = CAN(0,
extframe=True,
mode=CAN.SILENT_LOOPBACK,
baudrate=CAN.BAUDRATE_500k,
tx_io=18, rx_io=19, auto_restart=False)

# Test send/receive message
print("Loopback Test: no filter - Extd")
send_and_check(dev, "No filter", 0x100, True, extended=True)

# Set filter1
print("Loopback Test: one filter - Extd")
dev.setfilter(0, CAN.FILTER_ADDRESS, [0x101, 0], extframe=True)
send_and_check(dev, "Passing Message", 0x101, True, extended=True)
send_and_check(dev, "Blocked Message", 0x100, False, extended=True)

# Remove filter
print("Loopback Test: clear filter - Extd")
dev.clearfilter()
send_and_check(dev, "Passing Message - Bank 0", 0x101, True, extended=True)
send_and_check(dev, "Passing any Message", 0x100, True, extended=True)
2 changes: 1 addition & 1 deletion lib/tinyusb
Submodule tinyusb updated 892 files
Loading