-
Notifications
You must be signed in to change notification settings - Fork 109
Add SPI loopback test app #585
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
ppannuto-claude
wants to merge
1
commit into
tock:master
Choose a base branch
from
ppannuto-claude:dev/spi-loopback-test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Makefile for user application | ||
|
|
||
| # Specify this directory relative to the current application. | ||
| TOCK_USERLAND_BASE_DIR = ../../.. | ||
|
|
||
| # Which files to compile. | ||
| C_SRCS := $(wildcard *.c) | ||
|
|
||
| # Include userland master makefile. Contains rules and flags for actually | ||
| # building the application. | ||
| include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| SPI Loopback Test | ||
| ================= | ||
|
|
||
| Writes a known pattern out over the SPI controller syscall interface and | ||
| reads it back in the same transfer, then checks the two match. | ||
|
|
||
| This only produces a meaningful result on a board where SPI is configured | ||
| in hardware loopback mode (so the read data is expected to equal the write | ||
| data), such as the QEMU `qemu_arm_mps2_an385`/`qemu_arm_mps2_an386` boards. | ||
| On a board wired to a real, non-looped-back SPI peripheral, a mismatch is | ||
| expected and does not indicate a bug. | ||
|
|
||
| Prints `SPI PASS` or `SPI FAIL: <reason>` so the result can be checked from | ||
| a console transcript. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| #include <libtock/peripherals/spi_controller.h> | ||
| #include <libtock/tock.h> | ||
|
|
||
| #define BUF_SIZE 32 | ||
|
|
||
| // Arbitrary, conservative choice. As of 2026-08-26 this has only been | ||
| // tested on the QEMU ARM MPS2 AN385/AN386 boards; untested on other | ||
| // boards/hardware, so lower it if a real target can't keep up. | ||
| #define SPI_RATE_HZ 400000 | ||
|
|
||
| static uint8_t write_buf[BUF_SIZE]; | ||
| static uint8_t read_buf[BUF_SIZE]; | ||
| static bool done = false; | ||
|
|
||
| static void spi_cb(returncode_t ret __attribute__((unused))) { | ||
| done = true; | ||
| } | ||
|
|
||
| static void print_buf(const char* label, const uint8_t* buf, size_t len) { | ||
| printf("%s:", label); | ||
| for (size_t i = 0; i < len; i++) { | ||
| printf(" %02x", buf[i]); | ||
| } | ||
| printf("\n"); | ||
| } | ||
|
|
||
| int main(void) { | ||
| if (!libtock_spi_controller_exists()) { | ||
| printf("SPI FAIL: driver not present\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| // `read_buf` is pre-filled with a pattern disjoint from `write_buf`'s | ||
| // (offset by BUF_SIZE) so a no-op read_write (e.g. an unimplemented | ||
| // syscall that leaves the buffer untouched) can't accidentally match | ||
| // and produce a false PASS. | ||
| for (unsigned i = 0; i < BUF_SIZE; i++) { | ||
| write_buf[i] = (uint8_t) i; | ||
| read_buf[i] = (uint8_t) (i + BUF_SIZE); | ||
| } | ||
|
|
||
| returncode_t err; | ||
|
|
||
| // As of 2026-08-26, `set_chip_select()` is unimplemented on every Tock | ||
| // board -- the spi_controller capsule always returns NOSUPPORT here | ||
| // until multiple chip selects are supported. Check for exactly that so | ||
| // this test fails loudly if that ever changes, rather than silently | ||
| // masking a real error. | ||
| err = libtock_spi_controller_set_chip_select(0); | ||
| if (err != RETURNCODE_ENOSUPPORT) { | ||
| printf("SPI FAIL: set_chip_select returned %d, expected ENOSUPPORT\n", err); | ||
| return 1; | ||
| } | ||
|
|
||
| err = libtock_spi_controller_set_rate(SPI_RATE_HZ); | ||
| if (err != RETURNCODE_SUCCESS) { | ||
| printf("SPI FAIL: set_rate returned %d\n", err); | ||
| return 1; | ||
| } | ||
|
|
||
| err = libtock_spi_controller_read_write(write_buf, read_buf, BUF_SIZE, spi_cb); | ||
| if (err != RETURNCODE_SUCCESS) { | ||
| printf("SPI FAIL: read_write returned %d\n", err); | ||
| return 1; | ||
| } | ||
|
|
||
| printf("SPI transaction initialized. Waiting for callback.\n"); | ||
| yield_for(&done); | ||
|
|
||
| if (memcmp(write_buf, read_buf, BUF_SIZE) == 0) { | ||
| printf("SPI PASS\n"); | ||
| } else { | ||
| printf("SPI FAIL: loopback mismatch\n"); | ||
| print_buf("write", write_buf, BUF_SIZE); | ||
| print_buf("read ", read_buf, BUF_SIZE); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.