8000 port/esp32: Add CAN(TWAI) driver. by IhorNehrutsa · Pull Request #12331 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

port/esp32: Add CAN(TWAI) driver. #12331

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 @@ -651,6 +651,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
8000
408 changes: 408 additions & 0 deletions docs/library/machine.CAN.rst

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/library/machine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ Classes
machine.Signal.rst
machine.ADC.rst
machine.ADCBlock.rst
machine.CAN.rst
machine.PWM.rst
machine.UART.rst
machine.SPI.rst
Expand Down
73 changes: 73 additions & 0 deletions examples/esp32_can.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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=18,
rx=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: 2 additions & 0 deletions ports/esp32/boards/sdkconfig.base
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ CONFIG_LOG_MAXIMUM_LEVEL_INFO=y
# Main XTAL Config
# Only on: ESP32
CONFIG_XTAL_FREQ_AUTO=y
CONFIG_XTAL_FREQ_40=y
CONFIG_XTAL_FREQ=40

# ESP System Settings
# Only on: ESP32, ESP32S3
Expand Down
1 change: 1 addition & 0 deletions ports/esp32/esp32_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ list(APPEND MICROPY_SOURCE_PORT
fatfs_port.c
help.c
machine_bitstream.c
machine_can.c
machine_timer.c
machine_pin.c
machine_touchpad.c
Expand Down
Loading
Loading
0