Files
micropython/tests/multi_extmod/machine_can_08_init_mode.py
T
Angus Gratton 6cac2d275d
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
alif port / build_alif (alif_ae3_build) (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_c2_c5_c6) (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_p4) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (bigendian) (push) Has been cancelled
qemu port / build_and_test_arm (sabrelite) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_hardfp) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_softfp) (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
qemu port / build_and_test_rv64 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / longlong (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / gil_enabled (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / repr_b (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
unix port / sanitize_address (push) Has been cancelled
unix port / sanitize_undefined (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled
stm32: Add machine.CAN implementation.
Implemented according to API docs in a parent comment.

Adds new multi_extmod/machine_can_* tests which pass when testing between
NUCLEO_G474RE, NUCLEO_H723ZG and PYBDV11.

This work was mostly funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2026-03-19 17:36:50 +11:00

103 lines
3.3 KiB
Python

from machine import CAN
from micropython import const
import time
# instance0 transitions through various modes, instance1
# listens for various messages (or not)
#
# Note this test assumes no other CAN nodes are connected apart from the test
# instances (or if they are connected they must be in silent mode.)
#
# TODO: This test needs to eventually support the case where modes aren't supported
# on a controller, maybe by printing fake output if the mode switch fails?
# MODE_NORMAL, MODE_SLEEP, MODE_LOOPBACK, MODE_SILENT, MODE_SILENT_LOOPBACK
can = CAN(1, 500_000, mode=CAN.MODE_NORMAL)
# While instance0 is in Silent mode, instance1 sends a message with this ID
# that will be retried for 100ms (as instance0 won't ACK). So don't print every one.
_SILENT_RX_ID = const(0x53)
silent_rx_count = 0
def irq_print(can):
global silent_rx_count
while flags := can.irq().flags():
if flags & can.IRQ_RX:
can_id, data, _flags, errors = can.recv()
if can_id != _SILENT_RX_ID:
print("recv", hex(can_id), bytes(data))
else:
silent_rx_count += 1
if flags & can.IRQ_TX: # note: only enabled on instance1 to avoid race conditions
print("send", "failed" if flags & can.IRQ_TX_FAILED else "ok")
def reinit_with_mode(mode):
can.deinit()
can.init(bitrate=500_000, mode=mode)
can.irq(irq_print, trigger=can.IRQ_RX, hard=False)
can.set_filters(None) # receive all
def instance0():
multitest.next()
multitest.wait("instance1 ready")
reinit_with_mode(can.MODE_NORMAL)
print("Normal", "MODE_NORMAL" in str(can))
can.send(0x50, b"Normal")
time.sleep_ms(100)
# Skipping MODE_SLEEP as means different things on different hardware
reinit_with_mode(can.MODE_LOOPBACK)
print("Loopback", "MODE_LOOPBACK" in str(can))
# This message should go out to the bus, but will also be received by instance0 itself
can.send(0x51, b"Loopback")
time.sleep_ms(100)
reinit_with_mode(can.MODE_SILENT)
print("Silent", "MODE_SLIENT" in str(can))
# This message shouldn't go out onto the bus
idx = can.send(0x52, b"Silent")
multitest.broadcast("silent")
multitest.wait("silent done")
# we should have received the message from instance1 many times, as instance0 won't have ACKed it
print("silent_rx_count", silent_rx_count > 5)
can.cancel_send(idx)
reinit_with_mode(can.MODE_SILENT_LOOPBACK)
print("Silent Loopback", "MODE_SILENT_LOOPBACK" in str(can))
# This message should be received by instance0 only
idx = can.send(0x54, b"SiLoop")
time.sleep_ms(50)
reinit_with_mode(can.MODE_NORMAL)
print("Normal again", "MODE_NORMAL" in str(can))
can.send(0x55, b"Normal2") # should be received by instance1 only, again
multitest.broadcast("normal done")
# Receiver
def instance1():
can.irq(irq_print, trigger=can.IRQ_RX | can.IRQ_TX, hard=False)
can.set_filters(None) # receive all
multitest.next()
multitest.broadcast("instance1 ready")
# The IRQ does most of the work on this instance
multitest.wait("silent")
# Sending this message back, it should fail to send as Silent mode won't ACK it
idx = can.send(0x53, b"Silent2")
time.sleep_ms(20)
can.cancel_send(idx)
multitest.broadcast("silent done")
multitest.wait("normal done")