Files
micropython/tests/multi_extmod/machine_can_04_tx_order.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

171 lines
4.7 KiB
Python

from machine import CAN
import time
from random import seed, randrange
import micropython
micropython.alloc_emergency_exception_buf(256)
seed(0)
# Testing that transmit order obeys the priority ordering
ID_LOW = 0x500
ID_HIGH = 0x200
NUM_MSGS = 255
MSG_LEN = 4
can = CAN(1, 500_000)
def check_sequence(items, label):
# The full range of NUM_MSGS values should have been received or sent, in
# order, without duplicates
if len(items) != NUM_MSGS:
print(label, "wrong count", len(items), "vs", NUM_MSGS)
if items == list(range(NUM_MSGS)):
print(label, "OK")
else:
print(label, "error:")
print(items)
# Receiver
# lists of received messages, one list per ID
received_low = []
received_high = []
def irq_recv(can):
while can.irq().flags() & can.IRQ_RX:
can_id, data, flags, _errors = can.recv()
if can_id == ID_LOW and len(data) == MSG_LEN:
received_low.append(data[0])
elif can_id == ID_HIGH and len(data) == MSG_LEN:
received_high.append(data[0])
else:
print("unexpected recv", can_id, data, flags)
def instance0():
can.irq(irq_recv, trigger=can.IRQ_RX, hard=False)
can.set_filters(None) # receive all
multitest.next()
multitest.wait("sender done")
check_sequence(received_low, "Low prio received")
check_sequence(received_high, "High prio received")
# Sender
## Messages pending to send
pending_low = list(range(NUM_MSGS))
pending_high = list(range(NUM_MSGS))
# List of the messages currently queued to send
tx_queue = [None] * CAN.TX_QUEUE_LEN
# Messages sent, recorded in order as [high_prio, val]
sent = []
for _ in range(NUM_MSGS * 2):
sent.append([None, None])
num_sent = 0
def irq_send(can):
global num_sent
while flags := can.irq().flags():
assert flags & can.IRQ_TX # the only enabled IRQ
idx = (flags >> can.IRQ_TX_IDX_SHIFT) & can.IRQ_TX_IDX_MASK
success = not (flags & can.IRQ_TX_FAILED)
if not success:
return # We don't worry about failures here
if not tx_queue[idx]:
print("bad done", idx, success)
return
was_high, val = tx_queue[idx]
tx_queue[idx] = None
sent[num_sent][0] = was_high
sent[num_sent][1] = val
num_sent += 1
def instance1():
# note: this test can pass with hard=True, but in a debug build
# the completion IRQ may race ahead of setting tx_queue[idx], below
can.irq(irq_send, trigger=can.IRQ_TX, hard=False)
data = bytearray(MSG_LEN)
multitest.next()
while pending_low or pending_high:
if pending_high:
val = pending_high.pop(0)
data[0] = val
data[1] = 1
while True:
idx = can.send(ID_HIGH, data)
if idx is None:
continue # keep trying until a queue spot opens up
old = tx_queue[idx]
tx_queue[idx] = (True, val)
if old:
print("error high priority queue race", idx, val, old)
break
for _ in range(randrange(4)):
# Try and queue many low priority messages (expecting most will fail)
if pending_low:
val = pending_low[0]
data[0] = val
data[1] = 0
idx = can.send(ID_LOW, data)
if idx is None:
# don't retry indefinitely for low priority messages
continue
old = tx_queue[idx]
tx_queue[idx] = (False, val)
pending_low.pop(0)
if old is not None:
print("error low priority queue race", idx, val, old)
print("waiting for tx queue to empty...")
while any(x is not None for x in tx_queue):
pass
multitest.broadcast("sender done")
# Check we sent the right number of messages
if num_sent != 2 * NUM_MSGS:
print("Sent %d expected %d" % (num_sent, 2 * NUM_MSGS))
else:
print("Sent right number of messages")
# Check the low and high priority messages all arrived in order
sent_low = [val for (prio, val) in sent[:num_sent] if prio == False]
sent_high = [val for (prio, val) in sent[:num_sent] if prio == True]
check_sequence(sent_low, "Low prio sent")
check_sequence(sent_high, "High prio sent")
# check that high priority message queue items always stayed ahead of the low priority
high_val = -1
for idx, (prio, val) in enumerate(sent):
if prio:
high_val = val
elif high_val <= val and val < NUM_MSGS - 1:
print(
"Low priority message %d overtook high priority %d at index %d"
% (val, high_val, idx)
)