diff --git a/tools/spi/Makefile b/tools/spi/Makefile index 7fccd245a535..8cf21ff8cd03 100644 --- a/tools/spi/Makefile +++ b/tools/spi/Makefile @@ -12,7 +12,7 @@ endif # (this improves performance and avoids hard-to-debug behaviour); MAKEFLAGS += -r -CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include +CFLAGS += -O2 -Wall -g -D_GNU_SOURCE -I$(OUTPUT)include -I$(srctree)/tools/include ALL_TARGETS := spidev_test spidev_fdx ALL_PROGRAMS := $(patsubst %,$(OUTPUT)%,$(ALL_TARGETS)) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index f2135d619a0b..bd5ee7a6ac82 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -4,8 +4,6 @@ * * Copyright (c) 2007 MontaVista Software, Inc. * Copyright (c) 2007 Anton Vorontsov - * - * Cross-compile with cross-gcc -I/path/to/cross-kernel/include */ #include @@ -22,8 +20,7 @@ #include #include #include - -#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#include static void pabort(const char *s) { -- knotmirror 0.0.1 From a9e74a4c991126e263e2984fca327267ade5e224 Mon Sep 17 00:00:00 2001 From: Jonas Rebmann Date: Thu, 24 Sep 2026 15:43:52 +0200 Subject: [PATCH 02/13] spi: spidev_test: clarify usage for --size By default, spidev_test transmits the predetermined message default_tx. Given the --size parameter, the given number of random bytes are transmitted instead. Make all the input options (random bytes, string, file) mutually exclusive. Clarify the help text for --size. While at it, make the -p help text consistently lowercase. Signed-off-by: Jonas Rebmann Link: https://patch.msgid.link/20260924-spi-sun4i-spidev_test-v7-2-126e31a07fdc@pengutronix.de Signed-off-by: Mark Brown --- tools/spi/spidev_test.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index bd5ee7a6ac82..0bd6ae419cbf 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -44,6 +44,7 @@ static int verbose; static int transfer_size; static int iterations; static int interval = 5; /* interval in seconds for showing transfer rate */ +static int input_choices; static uint8_t default_tx[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, @@ -191,8 +192,8 @@ static void print_usage(const char *prog) "data:\n" " -i --input input data from a file (e.g. \"test.bin\")\n" " -o --output output data to a file (e.g. \"results.bin\")\n" - " -p Send data (e.g. \"1234\\xde\\xad\")\n" - " -S --size transfer size\n" + " -p send data (e.g. \"1234\\xde\\xad\")\n" + " -S --size transfer the given number of random bytes\n" " -I --iter iterations\n" "additional parameters:\n" " -b --bpw bits per word\n" @@ -262,6 +263,7 @@ static void parse_opts(int argc, char *argv[]) break; case 'i': input_file = optarg; + input_choices++; break; case 'o': output_file = optarg; @@ -304,6 +306,7 @@ static void parse_opts(int argc, char *argv[]) break; case 'p': input_tx = optarg; + input_choices++; break; case '2': mode |= SPI_TX_DUAL; @@ -316,6 +319,7 @@ static void parse_opts(int argc, char *argv[]) break; case 'S': transfer_size = atoi(optarg); + input_choices++; break; case 'I': iterations = atoi(optarg); @@ -446,8 +450,9 @@ int main(int argc, char *argv[]) parse_opts(argc, argv); - if (input_tx && input_file) - pabort("only one of -p and --input may be selected"); + if (input_choices > 1) + pabort("at most one of -S (--size), -p, -i (--input) may be selected, " + "and each may be specified only once"); fd = open(device, O_RDWR); if (fd < 0) -- knotmirror 0.0.1 From df79e49bd7d77121a0396a094676568de79b3f6c Mon Sep 17 00:00:00 2001 From: Jonas Rebmann Date: Thu, 24 Sep 2026 15:43:53 +0200 Subject: [PATCH 03/13] spi: spidev_test: make size argument mandatory for --size When the argument to --size is omitted, abort with a message instead of transmitting default_tx[]. Signed-off-by: Jonas Rebmann Link: https://patch.msgid.link/20260924-spi-sun4i-spidev_test-v7-3-126e31a07fdc@pengutronix.de Signed-off-by: Mark Brown --- tools/spi/spidev_test.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 0bd6ae419cbf..36292d868d69 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -41,9 +41,10 @@ static uint32_t speed = 500000; static uint16_t delay; static uint16_t word_delay; static int verbose; -static int transfer_size; +static int transfer_size = -1; static int iterations; static int interval = 5; /* interval in seconds for showing transfer rate */ +static int random_input; static int input_choices; static uint8_t default_tx[] = { @@ -319,6 +320,7 @@ static void parse_opts(int argc, char *argv[]) break; case 'S': transfer_size = atoi(optarg); + random_input = 1; input_choices++; break; case 'I': @@ -454,6 +456,9 @@ int main(int argc, char *argv[]) pabort("at most one of -S (--size), -p, -i (--input) may be selected, " "and each may be specified only once"); + if (random_input && transfer_size < 0) + pabort("a size argument is mandatory for -S (--size)"); + fd = open(device, O_RDWR); if (fd < 0) pabort("can't open device"); @@ -509,7 +514,7 @@ int main(int argc, char *argv[]) transfer_escaped_string(fd, input_tx); else if (input_file) transfer_file(fd, input_file); - else if (transfer_size) { + else if (transfer_size >= 0) { struct timespec last_stat; clock_gettime(CLOCK_MONOTONIC, &last_stat); -- knotmirror 0.0.1 From bd511c42d290807a04a325d584d65b98fd6721d7 Mon Sep 17 00:00:00 2001 From: Jonas Rebmann Date: Thu, 24 Sep 2026 15:43:54 +0200 Subject: [PATCH 04/13] spi: spidev_test: allow zero-length transfers SPI drivers may in fact handle zero-length transfers. The behavior so far was that such transfers would be executed but then result in the "can't send spi message" error message because the spi_sync() call returning 0 would be treated as an error while really it simply indicates that zero bytes were successfully transferred in a message. Don't treat a return value of zero as indication of an error. Update hex_dump() to work with empty buffers now printing e.g. TX | __ || Now including a trailing newline. Signed-off-by: Jonas Rebmann Link: https://patch.msgid.link/20260924-spi-sun4i-spidev_test-v7-4-126e31a07fdc@pengutronix.de Signed-off-by: Mark Brown --- tools/spi/spidev_test.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 36292d868d69..908ed038889b 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -68,6 +68,10 @@ static void hex_dump(const void *src, size_t length, size_t line_size, unsigned char c; printf("%s | ", prefix); + + if (length == 0) + printf("__ ||\n"); + while (length-- > 0) { printf("%02X ", *address++); if (!(++i % line_size) || (length == 0 && i % line_size)) { @@ -149,7 +153,7 @@ static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len) } ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); - if (ret < 1) + if (ret < 0) pabort("can't send spi message"); if (verbose) -- knotmirror 0.0.1 From 683d7fac9b0ff7aa8f0ff2ccfa0f4bbba99ee273 Mon Sep 17 00:00:00 2001 From: Jonas Rebmann Date: Thu, 24 Sep 2026 15:43:55 +0200 Subject: [PATCH 05/13] spi: spidev_test: abort when -I is selected without -S -I (--iter) for transferring multiple iterations of the selected input currently only has an effect when transferring random data using -S (--size). Error out if -I is used without -S to avoid confusion. Signed-off-by: Jonas Rebmann Link: https://patch.msgid.link/20260924-spi-sun4i-spidev_test-v7-5-126e31a07fdc@pengutronix.de Signed-off-by: Mark Brown --- tools/spi/spidev_test.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 908ed038889b..97d09bf4dafb 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -463,6 +463,9 @@ int main(int argc, char *argv[]) if (random_input && transfer_size < 0) pabort("a size argument is mandatory for -S (--size)"); + if (iterations && transfer_size < 0) + pabort("-I (--iter) is only implemented for -S (--size)"); + fd = open(device, O_RDWR); if (fd < 0) pabort("can't open device"); -- knotmirror 0.0.1 From 2d736e05dbcd1294ed621fe5d4b6d06ef0c259be Mon Sep 17 00:00:00 2001 From: Jonas Rebmann Date: Thu, 24 Sep 2026 15:43:56 +0200 Subject: [PATCH 06/13] spi: spidev_test: always compare loopback data In loopback mode, spidev_test verifies that the received data matches the sent data. However this test was only performed when random data was transferred using the -S switch. Always perform the test by moving it from transfer_buf() to transfer(). Signed-off-by: Jonas Rebmann Link: https://patch.msgid.link/20260924-spi-sun4i-spidev_test-v7-6-126e31a07fdc@pengutronix.de Signed-off-by: Mark Brown --- tools/spi/spidev_test.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 97d09bf4dafb..e46eaef4d2ab 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -173,6 +173,15 @@ static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len) if (verbose) hex_dump(rx, len, 32, "RX"); + + if (mode & SPI_LOOP) { + if (memcmp(tx, rx, len)) { + fprintf(stderr, "transfer error !\n"); + hex_dump(tx, len, 32, "TX"); + hex_dump(rx, len, 32, "RX"); + exit(1); + } + } } static void print_usage(const char *prog) @@ -435,15 +444,6 @@ static void transfer_buf(int fd, int len) _write_count += len; _read_count += len; - if (mode & SPI_LOOP) { - if (memcmp(tx, rx, len)) { - fprintf(stderr, "transfer error !\n"); - hex_dump(tx, len, 32, "TX"); - hex_dump(rx, len, 32, "RX"); - exit(1); - } - } - free(rx); free(tx); } -- knotmirror 0.0.1 From a47226866b8ae4c3a63802b668cdb99f4a76b151 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Thu, 24 Sep 2026 15:43:57 +0200 Subject: [PATCH 07/13] spi: spidev_test: add compare mode Check whether the received data correspond to the transferred data but without enabling loopback mode in the controller. This is for testing with TX physically bridged to RX which is useful if a loopback mode is unavailable or insufficient. Signed-off-by: Marc Kleine-Budde Signed-off-by: Jonas Rebmann Link: https://patch.msgid.link/20260924-spi-sun4i-spidev_test-v7-7-126e31a07fdc@pengutronix.de Signed-off-by: Mark Brown --- tools/spi/spidev_test.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index e46eaef4d2ab..7b343803903e 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -44,6 +44,7 @@ static int verbose; static int transfer_size = -1; static int iterations; static int interval = 5; /* interval in seconds for showing transfer rate */ +static int compare; static int random_input; static int input_choices; @@ -174,7 +175,7 @@ static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len) if (verbose) hex_dump(rx, len, 32, "RX"); - if (mode & SPI_LOOP) { + if (compare || mode & SPI_LOOP) { if (memcmp(tx, rx, len)) { fprintf(stderr, "transfer error !\n"); hex_dump(tx, len, 32, "TX"); @@ -186,13 +187,14 @@ static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len) static void print_usage(const char *prog) { - printf("Usage: %s [-2348CDFHILMNORSZbdilopsvw]\n", prog); + printf("Usage: %s [-2348CDFHILMNORSZbcdilopsvw]\n", prog); puts("general device settings:\n" " -D --device device to use (default /dev/spidev1.1)\n" " -s --speed max speed (Hz)\n" " -d --delay delay (usec)\n" " -w --word-delay word delay (usec)\n" " -l --loop loopback\n" + " -c --compare compare RX'ed and TX'ed data\n" "spi mode:\n" " -H --cpha clock phase\n" " -O --cpol clock polarity\n" @@ -230,6 +232,7 @@ static void parse_opts(int argc, char *argv[]) { "delay", 1, 0, 'd' }, { "word-delay", 1, 0, 'w' }, { "loop", 0, 0, 'l' }, + { "compare", 0, 0, 'c' }, { "cpha", 0, 0, 'H' }, { "cpol", 0, 0, 'O' }, { "rx-cpha-flip", 0, 0, 'F' }, @@ -253,7 +256,7 @@ static void parse_opts(int argc, char *argv[]) }; int c; - c = getopt_long(argc, argv, "D:s:d:w:b:i:o:lHOLC3ZFMNR248p:vS:I:", + c = getopt_long(argc, argv, "D:s:d:w:b:i:o:lcHOLC3ZFMNR248p:vS:I:", lopts, NULL); if (c == -1) @@ -285,6 +288,9 @@ static void parse_opts(int argc, char *argv[]) case 'l': mode |= SPI_LOOP; break; + case 'c': + compare = 1; + break; case 'H': mode |= SPI_CPHA; break; @@ -466,6 +472,9 @@ int main(int argc, char *argv[]) if (iterations && transfer_size < 0) pabort("-I (--iter) is only implemented for -S (--size)"); + if (compare && mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL)) + pabort("-c (--compare) conflicts with -2 (--dual), -4 (--quad) or -8 (--octal)"); + fd = open(device, O_RDWR); if (fd < 0) pabort("can't open device"); -- knotmirror 0.0.1 From d2b908c75a8103efbcefe9c6efa290c58c8fe849 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Thu, 24 Sep 2026 15:43:58 +0200 Subject: [PATCH 08/13] spi: spidev_test: allow disabling rx or tx buffers Allow not providing rx or tx buffers. This is useful to check if drivers that don't use SPI_CONTROLLER_MUST_RX (or -TX respectively) handle their operations correctly without a buffer. Signed-off-by: Marc Kleine-Budde Signed-off-by: Jonas Rebmann Link: https://patch.msgid.link/20260924-spi-sun4i-spidev_test-v7-8-126e31a07fdc@pengutronix.de Signed-off-by: Mark Brown --- tools/spi/spidev_test.c | 84 ++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 27 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 7b343803903e..b02a6384d27b 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -45,6 +45,7 @@ static int transfer_size = -1; static int iterations; static int interval = 5; /* interval in seconds for showing transfer rate */ static int compare; +static int do_tx = 1, do_rx = 1; static int random_input; static int input_choices; @@ -157,7 +158,7 @@ static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len) if (ret < 0) pabort("can't send spi message"); - if (verbose) + if (verbose && tx) hex_dump(tx, len, 32, "TX"); if (output_file) { @@ -172,10 +173,10 @@ static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len) close(out_fd); } - if (verbose) + if (verbose && rx) hex_dump(rx, len, 32, "RX"); - if (compare || mode & SPI_LOOP) { + if (tx && rx && (compare || mode & SPI_LOOP)) { if (memcmp(tx, rx, len)) { fprintf(stderr, "transfer error !\n"); hex_dump(tx, len, 32, "TX"); @@ -187,7 +188,7 @@ static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len) static void print_usage(const char *prog) { - printf("Usage: %s [-2348CDFHILMNORSZbcdilopsvw]\n", prog); + printf("Usage: %s [-2348CDFHILMNORSZbcdiloprstvw]\n", prog); puts("general device settings:\n" " -D --device device to use (default /dev/spidev1.1)\n" " -s --speed max speed (Hz)\n" @@ -195,6 +196,8 @@ static void print_usage(const char *prog) " -w --word-delay word delay (usec)\n" " -l --loop loopback\n" " -c --compare compare RX'ed and TX'ed data\n" + " -t --no-tx don't send data\n" + " -r --no-rx don't receive data\n" "spi mode:\n" " -H --cpha clock phase\n" " -O --cpol clock polarity\n" @@ -233,6 +236,8 @@ static void parse_opts(int argc, char *argv[]) { "word-delay", 1, 0, 'w' }, { "loop", 0, 0, 'l' }, { "compare", 0, 0, 'c' }, + { "no-tx", 0, 0, 't' }, + { "no-rx", 0, 0, 'r' }, { "cpha", 0, 0, 'H' }, { "cpol", 0, 0, 'O' }, { "rx-cpha-flip", 0, 0, 'F' }, @@ -256,7 +261,7 @@ static void parse_opts(int argc, char *argv[]) }; int c; - c = getopt_long(argc, argv, "D:s:d:w:b:i:o:lcHOLC3ZFMNR248p:vS:I:", + c = getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:vS:I:", lopts, NULL); if (c == -1) @@ -291,6 +296,12 @@ static void parse_opts(int argc, char *argv[]) case 'c': compare = 1; break; + case 't': + do_tx = 0; + break; + case 'r': + do_rx = 0; + break; case 'H': mode |= SPI_CPHA; break; @@ -363,15 +374,17 @@ static void transfer_escaped_string(int fd, char *str) { size_t size = strlen(str); uint8_t *tx; - uint8_t *rx; + uint8_t *rx = NULL; tx = malloc(size); if (!tx) pabort("can't allocate tx buffer"); - rx = malloc(size); - if (!rx) - pabort("can't allocate rx buffer"); + if (do_rx) { + rx = malloc(size); + if (!rx) + pabort("can't allocate rx buffer"); + } size = unescape((char *)tx, str, size); transfer(fd, tx, rx, size); @@ -385,7 +398,7 @@ static void transfer_file(int fd, char *filename) struct stat sb; int tx_fd; uint8_t *tx; - uint8_t *rx; + uint8_t *rx = NULL; if (stat(filename, &sb) == -1) pabort("can't stat input file"); @@ -398,9 +411,12 @@ static void transfer_file(int fd, char *filename) if (!tx) pabort("can't allocate tx buffer"); - rx = malloc(sb.st_size); - if (!rx) - pabort("can't allocate rx buffer"); + + if (do_rx) { + rx = malloc(sb.st_size); + if (!rx) + pabort("can't allocate rx buffer"); + } bytes = read(tx_fd, tx, sb.st_size); if (bytes != sb.st_size) @@ -431,24 +447,29 @@ static void show_transfer_rate(void) static void transfer_buf(int fd, int len) { - uint8_t *tx; - uint8_t *rx; + uint8_t *tx = NULL; + uint8_t *rx = NULL; int i; - tx = malloc(len); - if (!tx) - pabort("can't allocate tx buffer"); - for (i = 0; i < len; i++) - tx[i] = random(); + if (do_tx) { + tx = malloc(len); + if (!tx) + pabort("can't allocate tx buffer"); + for (i = 0; i < len; i++) + tx[i] = random(); + } - rx = malloc(len); - if (!rx) - pabort("can't allocate rx buffer"); + if (do_rx) { + rx = malloc(len); + if (!rx) + pabort("can't allocate rx buffer"); + } transfer(fd, tx, rx, len); - - _write_count += len; - _read_count += len; + if (do_tx) + _write_count += len; + if (do_rx) + _read_count += len; free(rx); free(tx); @@ -472,6 +493,15 @@ int main(int argc, char *argv[]) if (iterations && transfer_size < 0) pabort("-I (--iter) is only implemented for -S (--size)"); + if (compare && (!do_tx || !do_rx)) + pabort("-c (--compare) conflicts with -t (--no-tx) or -r (--no-rx)"); + + if (!do_rx && output_file) + pabort("-r (--no-rx) conflicts with -o (--output)"); + + if (!do_tx && transfer_size < 0) + pabort("-t (--no-tx) is only implemented for -S (--size)"); + if (compare && mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL)) pabort("-c (--compare) conflicts with -2 (--dual), -4 (--quad) or -8 (--octal)"); @@ -549,7 +579,7 @@ int main(int argc, char *argv[]) printf("total: tx %.1fKB, rx %.1fKB\n", _write_count/1024.0, _read_count/1024.0); } else - transfer(fd, default_tx, default_rx, sizeof(default_tx)); + transfer(fd, default_tx, do_rx ? default_rx : NULL, sizeof(default_tx)); close(fd); -- knotmirror 0.0.1 From 951a60af6c0baa36d6bed82cf3f7e8b754426ea7 Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Thu, 24 Sep 2026 15:43:59 +0200 Subject: [PATCH 09/13] spi: spidev_test: don't send 0x0 or 0xff Particularly when using compare mode, if the controller fails to transfer any data, asserting on a read of 0x00 or 0xff may lead to a false negative test, indicating a byte was successfully transferred when the values simply originate from the pull-up or pull-down of RX. Signed-off-by: Marc Kleine-Budde Signed-off-by: Jonas Rebmann Link: https://patch.msgid.link/20260924-spi-sun4i-spidev_test-v7-9-126e31a07fdc@pengutronix.de Signed-off-by: Mark Brown --- tools/spi/spidev_test.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index b02a6384d27b..7ca427c27f07 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -45,6 +45,7 @@ static int transfer_size = -1; static int iterations; static int interval = 5; /* interval in seconds for showing transfer rate */ static int compare; +static int nonzero; static int do_tx = 1, do_rx = 1; static int random_input; static int input_choices; @@ -188,7 +189,7 @@ static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len) static void print_usage(const char *prog) { - printf("Usage: %s [-2348CDFHILMNORSZbcdiloprstvw]\n", prog); + printf("Usage: %s [-2348CDFHILMNORSZbcdiloprstvwz]\n", prog); puts("general device settings:\n" " -D --device device to use (default /dev/spidev1.1)\n" " -s --speed max speed (Hz)\n" @@ -212,6 +213,7 @@ static void print_usage(const char *prog) " -i --input input data from a file (e.g. \"test.bin\")\n" " -o --output output data to a file (e.g. \"results.bin\")\n" " -p send data (e.g. \"1234\\xde\\xad\")\n" + " -z --nonzero don't send 0x00 or 0xff bytes\n" " -S --size transfer the given number of random bytes\n" " -I --iter iterations\n" "additional parameters:\n" @@ -249,6 +251,7 @@ static void parse_opts(int argc, char *argv[]) { "input", 1, 0, 'i' }, { "output", 1, 0, 'o' }, { "size", 1, 0, 'S' }, + { "nonzero", 0, 0, 'z' }, { "iter", 1, 0, 'I' }, { "bpw", 1, 0, 'b' }, { "lsb", 0, 0, 'L' }, @@ -261,7 +264,7 @@ static void parse_opts(int argc, char *argv[]) }; int c; - c = getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:vS:I:", + c = getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:vS:zI:", lopts, NULL); if (c == -1) @@ -353,6 +356,9 @@ static void parse_opts(int argc, char *argv[]) random_input = 1; input_choices++; break; + case 'z': + nonzero = 1; + break; case 'I': iterations = atoi(optarg); break; @@ -455,8 +461,11 @@ static void transfer_buf(int fd, int len) tx = malloc(len); if (!tx) pabort("can't allocate tx buffer"); - for (i = 0; i < len; i++) - tx[i] = random(); + for (i = 0; i < len; i++) { + do + tx[i] = random(); + while (nonzero && (tx[i] == 0x0 || tx[i] == 0xff)); + } } if (do_rx) { @@ -493,6 +502,9 @@ int main(int argc, char *argv[]) if (iterations && transfer_size < 0) pabort("-I (--iter) is only implemented for -S (--size)"); + if (nonzero && transfer_size < 0) + pabort("-z (--nonzero) is only implemented for -S (--size)"); + if (compare && (!do_tx || !do_rx)) pabort("-c (--compare) conflicts with -t (--no-tx) or -r (--no-rx)"); -- knotmirror 0.0.1 From c807c71d21eb77862fc497899720d28d5a149830 Mon Sep 17 00:00:00 2001 From: Jonas Rebmann Date: Thu, 24 Sep 2026 15:44:00 +0200 Subject: [PATCH 10/13] spi: spidev_test: send predictable data Introduce a flag to test on a predictable byte sequence instead of random bytes. This is useful when comparing multiple runs with the same option set e.g. with an oscilloscope. Signed-off-by: Marc Kleine-Budde Signed-off-by: Jonas Rebmann Link: https://patch.msgid.link/20260924-spi-sun4i-spidev_test-v7-10-126e31a07fdc@pengutronix.de Signed-off-by: Mark Brown --- tools/spi/spidev_test.c | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 7ca427c27f07..a58680b6288a 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -48,6 +48,7 @@ static int compare; static int nonzero; static int do_tx = 1, do_rx = 1; static int random_input; +static int predictable; static int input_choices; static uint8_t default_tx[] = { @@ -189,7 +190,7 @@ static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len) static void print_usage(const char *prog) { - printf("Usage: %s [-2348CDFHILMNORSZbcdiloprstvwz]\n", prog); + printf("Usage: %s [-2348CDFHILMNOPRSZbcdiloprstvwz]\n", prog); puts("general device settings:\n" " -D --device device to use (default /dev/spidev1.1)\n" " -s --speed max speed (Hz)\n" @@ -214,6 +215,7 @@ static void print_usage(const char *prog) " -o --output output data to a file (e.g. \"results.bin\")\n" " -p send data (e.g. \"1234\\xde\\xad\")\n" " -z --nonzero don't send 0x00 or 0xff bytes\n" + " -P --predictable transfer the given number of sequential bytes\n" " -S --size transfer the given number of random bytes\n" " -I --iter iterations\n" "additional parameters:\n" @@ -259,12 +261,13 @@ static void parse_opts(int argc, char *argv[]) { "no-cs", 0, 0, 'N' }, { "ready", 0, 0, 'R' }, { "mosi-idle-low", 0, 0, 'M' }, + { "predictable", 1, 0, 'P' }, { "verbose", 0, 0, 'v' }, { NULL, 0, 0, 0 }, }; int c; - c = getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:vS:zI:", + c = getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:P:vS:zI:", lopts, NULL); if (c == -1) @@ -342,6 +345,11 @@ static void parse_opts(int argc, char *argv[]) input_tx = optarg; input_choices++; break; + case 'P': + transfer_size = atoi(optarg); + predictable = 1; + input_choices++; + break; case '2': mode |= SPI_TX_DUAL; break; @@ -462,9 +470,17 @@ static void transfer_buf(int fd, int len) if (!tx) pabort("can't allocate tx buffer"); for (i = 0; i < len; i++) { - do - tx[i] = random(); - while (nonzero && (tx[i] == 0x0 || tx[i] == 0xff)); + if (predictable) { + int v = i - iterations; + + if (nonzero) + v = ((v % 254) + 254) % 254 + 1; + tx[i] = (char)v; + } else { + do { + tx[i] = random(); + } while (nonzero && (tx[i] == 0x0 || tx[i] == 0xff)); + } } } @@ -493,17 +509,17 @@ int main(int argc, char *argv[]) parse_opts(argc, argv); if (input_choices > 1) - pabort("at most one of -S (--size), -p, -i (--input) may be selected, " + pabort("at most one of -S (--size), -p, -i (--input), -P (--predictable) may be selected, " "and each may be specified only once"); - if (random_input && transfer_size < 0) - pabort("a size argument is mandatory for -S (--size)"); + if ((random_input || predictable) && transfer_size < 0) + pabort("a size argument is mandatory for -S (--size) and -P (--predictable)"); if (iterations && transfer_size < 0) - pabort("-I (--iter) is only implemented for -S (--size)"); + pabort("-I (--iter) is only implemented for -S (--size) and -P (--predictable)"); if (nonzero && transfer_size < 0) - pabort("-z (--nonzero) is only implemented for -S (--size)"); + pabort("-z (--nonzero) is only implemented for -S (--size) and -P (--predictable)"); if (compare && (!do_tx || !do_rx)) pabort("-c (--compare) conflicts with -t (--no-tx) or -r (--no-rx)"); @@ -512,7 +528,7 @@ int main(int argc, char *argv[]) pabort("-r (--no-rx) conflicts with -o (--output)"); if (!do_tx && transfer_size < 0) - pabort("-t (--no-tx) is only implemented for -S (--size)"); + pabort("-t (--no-tx) is only implemented for -S (--size) and -P (--predictable)"); if (compare && mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL)) pabort("-c (--compare) conflicts with -2 (--dual), -4 (--quad) or -8 (--octal)"); -- knotmirror 0.0.1 From 31ce698070aa1e177f8e608d7fb92045fb01275c Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Thu, 24 Sep 2026 15:44:01 +0200 Subject: [PATCH 11/13] spi: spidev_test: add option to split message into multiple transfers SPI_IOC_MESSAGE() submits a single SPI message containing one or more transfers. Introduce an option to allow splitting the message into multiple transfers. Signed-off-by: Marc Kleine-Budde Signed-off-by: Jonas Rebmann Link: https://patch.msgid.link/20260924-spi-sun4i-spidev_test-v7-11-126e31a07fdc@pengutronix.de Signed-off-by: Mark Brown --- tools/spi/spidev_test.c | 113 +++++++++++++++++++++++++++++----------- 1 file changed, 82 insertions(+), 31 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index a58680b6288a..ac24d051835f 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -42,6 +42,7 @@ static uint16_t delay; static uint16_t word_delay; static int verbose; static int transfer_size = -1; +static int transfers = 1; static int iterations; static int interval = 5; /* interval in seconds for showing transfer rate */ static int compare; @@ -123,40 +124,79 @@ static int unescape(char *_dst, char *_src, size_t len) return ret; } -static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len) +static void transfer(int fd, uint8_t const * const tx, uint8_t const * const rx, size_t len) { int ret; int out_fd; - struct spi_ioc_transfer tr = { - .tx_buf = (unsigned long)tx, - .rx_buf = (unsigned long)rx, - .len = len, - .delay_usecs = delay, - .word_delay_usecs = word_delay, - .speed_hz = speed, - .bits_per_word = bits, - }; - - if (mode & SPI_TX_OCTAL) - tr.tx_nbits = 8; - else if (mode & SPI_TX_QUAD) - tr.tx_nbits = 4; - else if (mode & SPI_TX_DUAL) - tr.tx_nbits = 2; - if (mode & SPI_RX_OCTAL) - tr.rx_nbits = 8; - else if (mode & SPI_RX_QUAD) - tr.rx_nbits = 4; - else if (mode & SPI_RX_DUAL) - tr.rx_nbits = 2; - if (!(mode & SPI_LOOP)) { - if (mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL)) - tr.rx_buf = 0; - else if (mode & (SPI_RX_OCTAL | SPI_RX_QUAD | SPI_RX_DUAL)) - tr.tx_buf = 0; + size_t bytes_per_word = DIV_ROUND_UP(bits, 8); + int effective_transfers; + struct spi_ioc_transfer *tr; + size_t len_per_transfer; + const uint8_t *tx_buf = tx; + const uint8_t *rx_buf = rx; + size_t rem; + + if (len) { + effective_transfers = min_t(size_t, transfers, DIV_ROUND_UP(len, bytes_per_word)); + len_per_transfer = roundup(DIV_ROUND_UP(len, effective_transfers), bytes_per_word); + } else { + /* zero-length transfers may be handled by the driver, deliver as ordered */ + effective_transfers = transfers; + len_per_transfer = 0; } - ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); + if (SPI_MSGSIZE(effective_transfers) == 0) + pabort("too many transfers, SPI_IOC_MESSAGE size limit exceeded"); + + tr = calloc(effective_transfers, sizeof(*tr)); + if (!tr) + pabort("can't allocate transfer array"); + + rem = len; + + for (int i = 0; i < effective_transfers; i++) { + size_t n = min(rem, len_per_transfer); + + tr[i] = (struct spi_ioc_transfer) { + .tx_buf = (unsigned long)tx_buf, + .rx_buf = (unsigned long)rx_buf, + .len = n, + .delay_usecs = delay, + .word_delay_usecs = word_delay, + .speed_hz = speed, + .bits_per_word = bits, + }; + + if (tx_buf) + tx_buf += n; + + if (rx_buf) + rx_buf += n; + + rem -= n; + + if (mode & SPI_TX_OCTAL) + tr[i].tx_nbits = 8; + else if (mode & SPI_TX_QUAD) + tr[i].tx_nbits = 4; + else if (mode & SPI_TX_DUAL) + tr[i].tx_nbits = 2; + if (mode & SPI_RX_OCTAL) + tr[i].rx_nbits = 8; + else if (mode & SPI_RX_QUAD) + tr[i].rx_nbits = 4; + else if (mode & SPI_RX_DUAL) + tr[i].rx_nbits = 2; + if (!(mode & SPI_LOOP)) { + if (mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL)) + tr[i].rx_buf = 0; + else if (mode & (SPI_RX_OCTAL | SPI_RX_QUAD | SPI_RX_DUAL)) + tr[i].tx_buf = 0; + } + } + + ret = ioctl(fd, SPI_IOC_MESSAGE(effective_transfers), tr); + free(tr); if (ret < 0) pabort("can't send spi message"); @@ -190,7 +230,7 @@ static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len) static void print_usage(const char *prog) { - printf("Usage: %s [-2348CDFHILMNOPRSZbcdiloprstvwz]\n", prog); + printf("Usage: %s [-2348CDFHILMNOPRSTZbcdiloprstvwz]\n", prog); puts("general device settings:\n" " -D --device device to use (default /dev/spidev1.1)\n" " -s --speed max speed (Hz)\n" @@ -225,6 +265,7 @@ static void print_usage(const char *prog) " -N --no-cs no chip select\n" " -R --ready slave pulls low to pause\n" " -M --mosi-idle-low leave mosi line low when idle\n" + " -T --transfers number of transfers\n" "misc:\n" " -v --verbose Verbose (show tx buffer)\n"); exit(1); @@ -262,12 +303,13 @@ static void parse_opts(int argc, char *argv[]) { "ready", 0, 0, 'R' }, { "mosi-idle-low", 0, 0, 'M' }, { "predictable", 1, 0, 'P' }, + { "transfers", 1, 0, 'T' }, { "verbose", 0, 0, 'v' }, { NULL, 0, 0, 0 }, }; int c; - c = getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:P:vS:zI:", + c = getopt_long(argc, argv, "D:s:d:w:b:i:o:lctrHOLC3ZFMNR248p:P:T:vS:zI:", lopts, NULL); if (c == -1) @@ -332,6 +374,9 @@ static void parse_opts(int argc, char *argv[]) case 'M': mode |= SPI_MOSI_IDLE_LOW; break; + case 'T': + transfers = atoi(optarg); + break; case 'N': mode |= SPI_NO_CS; break; @@ -533,6 +578,12 @@ int main(int argc, char *argv[]) if (compare && mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL)) pabort("-c (--compare) conflicts with -2 (--dual), -4 (--quad) or -8 (--octal)"); + if (transfers < 1) + pabort("-T (--transfers) must be 1 or above"); + + if (bits < 1) + pabort("-b (--bpw) must be 1 or above"); + fd = open(device, O_RDWR); if (fd < 0) pabort("can't open device"); -- knotmirror 0.0.1 From a73c71e1f25a4f7d6266a6c1cc5e3c903b52cc6a Mon Sep 17 00:00:00 2001 From: Marc Kleine-Budde Date: Thu, 24 Sep 2026 15:44:02 +0200 Subject: [PATCH 12/13] spi: spidev_test: print TX on error If sending an spi message fails, dump its first up to 256 bytes to the console. Signed-off-by: Marc Kleine-Budde Signed-off-by: Jonas Rebmann Link: https://patch.msgid.link/20260924-spi-sun4i-spidev_test-v7-12-126e31a07fdc@pengutronix.de Signed-off-by: Mark Brown --- tools/spi/spidev_test.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index ac24d051835f..61951ed1c908 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -197,8 +197,18 @@ static void transfer(int fd, uint8_t const * const tx, uint8_t const * const rx, ret = ioctl(fd, SPI_IOC_MESSAGE(effective_transfers), tr); free(tr); - if (ret < 0) + if (ret < 0) { + const size_t dump_len = min_t(size_t, len, 256); + int saved_errno = errno; + + if (tx) { + hex_dump(tx, dump_len, 32, "TX"); + if (len > dump_len) + printf("... (%zu more bytes)\n", len - dump_len); + } + errno = saved_errno; pabort("can't send spi message"); + } if (verbose && tx) hex_dump(tx, len, 32, "TX"); -- knotmirror 0.0.1 From c79bbeadabc07e9a4c68ca16a3744a2264d6fbae Mon Sep 17 00:00:00 2001 From: Jonas Rebmann Date: Thu, 24 Sep 2026 15:44:03 +0200 Subject: [PATCH 13/13] spi: spidev_test: rewrite unescape() to stay in bounds unescape() could read the source buffer out of bounds for inputs like "\\x" and could write the destination buffer out of bounds due to lack of checking against the len parameter. For better readability and consistency, rewrite it as a simplified version of unescape_string() in string_helpers.h. This includes rearranging src and dst parameters. Signed-off-by: Jonas Rebmann Link: https://patch.msgid.link/20260924-spi-sun4i-spidev_test-v7-13-126e31a07fdc@pengutronix.de Signed-off-by: Mark Brown --- tools/spi/spidev_test.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 61951ed1c908..5bd3159f61a7 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -100,28 +101,31 @@ static void hex_dump(const void *src, size_t length, size_t line_size, * Unescape - process hexadecimal escape character * converts shell input "\x23" -> 0x23 */ -static int unescape(char *_dst, char *_src, size_t len) +static int unescape(char *src, char *dst, size_t size) { - int ret = 0; - int match; - char *src = _src; - char *dst = _dst; + char *out = dst; unsigned int ch; - while (*src) { - if (*src == '\\' && *(src+1) == 'x') { - match = sscanf(src + 2, "%2x", &ch); - if (!match) - pabort("malformed input string"); + while (*src && size--) { + if (src[0] == '\\' && src[1] != '\0') { + src++; + + if (src[0] == 'x' && + isxdigit((unsigned char)src[1]) && + isxdigit((unsigned char)src[2]) && + sscanf(&src[1], "%2x", &ch)) { + *out++ = (unsigned char)ch; + src += 3; + continue; + } - src += 4; - *dst++ = (unsigned char)ch; - } else { - *dst++ = *src++; + *out++ = '\\'; + if (!size--) + break; } - ret++; + *out++ = *src++; } - return ret; + return out - dst; } static void transfer(int fd, uint8_t const * const tx, uint8_t const * const rx, size_t len) @@ -455,7 +459,7 @@ static void transfer_escaped_string(int fd, char *str) pabort("can't allocate rx buffer"); } - size = unescape((char *)tx, str, size); + size = unescape(str, (char *)tx, size); transfer(fd, tx, rx, size); free(rx); free(tx);