Skip to content

Add SPI loopback test app - #585

Open
ppannuto-claude wants to merge 1 commit into
tock:masterfrom
ppannuto-claude:dev/spi-loopback-test
Open

Add SPI loopback test app#585
ppannuto-claude wants to merge 1 commit into
tock:masterfrom
ppannuto-claude:dev/spi-loopback-test

Conversation

@ppannuto-claude

@ppannuto-claude ppannuto-claude commented Aug 26, 2026

Copy link
Copy Markdown

Summary

Adds examples/tests/spi_loopback, a small standalone app that exercises the
current libtock/peripherals/spi_controller.h API end-to-end: writes a known
pattern and reads it back in the same transfer, then checks the two match,
printing SPI PASS or SPI FAIL: <reason>.

This fills a gap found while adding SPI support to a new board in
tock/tock#5115: there was no
example that builds against the current libtock_spi_controller_* API — the
existing wip/spi/* examples predate it (different function names, no
returncode_t) and don't compile against it without a rewrite.

The test is only meaningful on a board where SPI is configured in hardware
loopback mode (read data is expected to equal write data), such as the new
QEMU qemu_arm_mps2_an385/qemu_arm_mps2_an386 boards. On a board wired to
a real, non-looped-back peripheral, a mismatch is expected and not a bug —
noted in the app's README.

Testing

Built for both cortex-m3 and cortex-m4 (TOCK_TARGETS=cortex-m3 make /
TOCK_TARGETS=cortex-m4 make), and run under QEMU against the boards in
tock/tock#5115 (qemu_arm_mps2_an385/an386) alongside c_hello and
blink, confirming SPI PASS. Re-verified SPI PASS again from the
current examples/tests/spi_loopback path after the review round below.

make format (uncrustify) passes clean with no changes.

AI disclosure

Written by Claude (Claude Code), as part of the same session that produced
tock/tock#5115, in response to:

❯ Clean up the SPI test app and open the libtock-c PR
❯ Address feedback on the libtock-c PR

The second prompt was in response to Pat's own review (as ppannuto,
requesting changes): moved the app to examples/tests/spi_loopback,
renamed wbuf/rbuf to write_buf/read_buf, dated the
set_chip_select comment, lifted the SPI rate to a named SPI_RATE_HZ
const with a comment on its provenance, printed both buffers on mismatch,
and dropped a redundant README note — see inline replies on the review for
detail on each.

Review status: reviewed once by Pat Pannuto (changes requested); the above
addresses that round. Not yet re-reviewed/approved.

@ppannuto ppannuto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meta: This should move to examples/tests/spi_loopback/

Comment thread examples/spi_loopback_test/main.c Outdated
Comment thread examples/spi_loopback_test/main.c Outdated
Comment thread examples/spi_loopback_test/main.c Outdated
Comment thread examples/spi_loopback_test/main.c Outdated
Comment thread examples/spi_loopback_test/README.md Outdated
@ppannuto-claude

Copy link
Copy Markdown
Author

Moved to examples/tests/spi_loopback/ and addressed the inline review comments (renamed buffers, dated the set_chip_select comment, lifted the SPI rate to a named SPI_RATE_HZ const with a comment on its provenance, printed both buffers on mismatch, dropped the redundant README note). Amended the commit and force-pushed; re-verified SPI PASS under QEMU from the new path and re-ran make format clean.

Comment thread examples/tests/spi_loopback/main.c Outdated
@ppannuto-claude

Copy link
Copy Markdown
Author

Fixed: print_buf now takes an explicit len parameter instead of using the global BUF_SIZE. Amended and force-pushed; rebuilt for both targets and re-confirmed SPI PASS under QEMU.

Comment thread examples/tests/spi_loopback/main.c Outdated
Comment thread examples/tests/spi_loopback/main.c Outdated
Comment thread examples/tests/spi_loopback/main.c
There is no example that exercises the current libtock/peripherals/
spi_controller.h API end-to-end -- the existing wip/spi/* examples predate
it and don't build against it. Add a small standalone loopback test:
write a known pattern, read it back over the same transfer, and print
SPI PASS/FAIL depending on whether they match.

Only meaningful on a board with SPI in hardware loopback (e.g. Tock's
QEMU qemu_arm_mps2_an385/an386 boards); on a board with a real,
non-looped-back peripheral, a mismatch is expected.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@ppannuto-claude

Copy link
Copy Markdown
Author

Addressed the third review round: read_buf now pre-fills with i + BUF_SIZE (disjoint from write_buf's pattern) so a no-op can't false-PASS; set_chip_select's return is now checked against RETURNCODE_ENOSUPPORT explicitly; added the suggested progress print before yield_for. Amended and force-pushed, rebuilt for both targets, re-confirmed SPI PASS under QEMU.

@ppannuto
ppannuto marked this pull request as ready for review August 26, 2026 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants