[PATCH 4/7] spi: spidev_test: don't send 0x0 or 0xff

From: Jonas Rebmann

Date: Mon Sep 14 2026 - 12:25:40 EST


From: Marc Kleine-Budde <mkl@xxxxxxxxxxxxxx>

Particularly when using compare mode, if the controller fails to
transfer any data, asserting on a read of 0x00 or 0xff may lead 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 <mkl@xxxxxxxxxxxxxx>
Signed-off-by: Jonas Rebmann <jre@xxxxxxxxxxxxxx>
---
tools/spi/spidev_test.c | 21 +++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c
index 6b0e839e6c33..235b267c02f2 100644
--- a/tools/spi/spidev_test.c
+++ b/tools/spi/spidev_test.c
@@ -47,6 +47,7 @@ static int transfer_size;
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 uint8_t default_tx[] = {
@@ -175,7 +176,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 [-2348CDFHILMNORSZbdilctropsvw]\n", prog);
+ printf("Usage: %s [-2348CDFHILMNORSZbdilctropsvwz]\n", prog);
puts("general device settings:\n"
" -D --device device to use (default /dev/spidev1.1)\n"
" -s --speed max speed (Hz)\n"
@@ -199,6 +200,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 size\n"
" -I --iter iterations\n"
"additional parameters:\n"
@@ -236,6 +238,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' },
@@ -248,7 +251,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)
@@ -337,6 +340,9 @@ static void parse_opts(int argc, char *argv[])
case 'S':
transfer_size = atoi(optarg);
break;
+ case 'z':
+ nonzero = 1;
+ break;
case 'I':
iterations = atoi(optarg);
break;
@@ -434,8 +440,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) {
@@ -471,8 +480,8 @@ int main(int argc, char *argv[])

parse_opts(argc, argv);

- if (!!input_tx + !!input_file + !do_tx > 0)
- pabort("only one of -p, -i (--input), -t (--no-tx) may be selected");
+ if (!!input_tx + !!input_file + !do_tx + !!nonzero > 1)
+ pabort("only one of -p, -i (--input), -t (--no-tx), -z (--nonzero) may be selected");

if (compare && (!do_tx || !do_rx))
pabort("-l/-c conflict with -t or -r");

--
2.55.0.806.gb8242b093d