[PATCH v2 4/8] spi: spidev_test: don't send 0x0 or 0xff
From: Jonas Rebmann
Date: Wed Sep 16 2026 - 13:53:07 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 | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)
diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c
index b4f9c40a246a..b8677fa3d134 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 int input_choices = 0;
@@ -176,7 +177,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"
@@ -200,6 +201,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"
@@ -237,6 +239,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' },
@@ -249,7 +252,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)
@@ -341,6 +344,10 @@ static void parse_opts(int argc, char *argv[])
case 'S':
transfer_size = atoi(optarg);
break;
+ case 'z':
+ nonzero = 1;
+ input_choices++;
+ break;
case 'I':
iterations = atoi(optarg);
break;
@@ -438,8 +445,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) {
@@ -476,7 +486,7 @@ int main(int argc, char *argv[])
parse_opts(argc, argv);
if (input_choices > 1)
- pabort("only one of -p, -i (--input), -t (--no-tx) may be selected");
+ pabort("only one of -p, -i (--input), -t (--no-tx), -z (--nonzero) may be selected");
if (compare && (!do_tx || !do_rx))
pabort("-l/-c (--loop/--compare) conflict with -t (--no-tx) or -r (--no-rx)");
--
2.56.0.rc0.108.gf0ef1b96a0