Skip to content

Commit

Permalink
port/esp32: Add CAN(TWAI) class.
Browse files Browse the repository at this point in the history
Signed-off-by: IhorNehrutsa <[email protected]>
  • Loading branch information
IhorNehrutsa committed Feb 12, 2025
1 parent 30acb16 commit 835b943
Show file tree
Hide file tree
Showing 5 changed files with 980 additions and 0 deletions.
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
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_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)
Loading

0 comments on commit 835b943

Please sign in to comment.