hc
2024-09-20 a36159eec6ca17402b0e146b86efaf76568dc353
kernel/tools/spi/spidev_test.c
....@@ -1,12 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * SPI testing utility (using spidev driver)
34 *
45 * Copyright (c) 2007 MontaVista Software, Inc.
56 * Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>
6
- *
7
- * This program is free software; you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License as published by
9
- * the Free Software Foundation; either version 2 of the License.
107 *
118 * Cross-compile with cross-gcc -I/path/to/cross-kernel/include
129 */
....@@ -16,6 +13,7 @@
1613 #include <stdio.h>
1714 #include <stdlib.h>
1815 #include <string.h>
16
+#include <errno.h>
1917 #include <getopt.h>
2018 #include <fcntl.h>
2119 #include <time.h>
....@@ -29,7 +27,11 @@
2927
3028 static void pabort(const char *s)
3129 {
32
- perror(s);
30
+ if (errno != 0)
31
+ perror(s);
32
+ else
33
+ printf("%s\n", s);
34
+
3335 abort();
3436 }
3537
....@@ -45,7 +47,7 @@
4547 static int iterations;
4648 static int interval = 5; /* interval in seconds for showing transfer rate */
4749
48
-uint8_t default_tx[] = {
50
+static uint8_t default_tx[] = {
4951 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5052 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
5153 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
....@@ -54,8 +56,8 @@
5456 0xF0, 0x0D,
5557 };
5658
57
-uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
58
-char *input_tx;
59
+static uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
60
+static char *input_tx;
5961
6062 static void hex_dump(const void *src, size_t length, size_t line_size,
6163 char *prefix)
....@@ -73,12 +75,12 @@
7375 while (i++ % line_size)
7476 printf("__ ");
7577 }
76
- printf(" | "); /* right close */
78
+ printf(" |");
7779 while (line < address) {
7880 c = *line++;
79
- printf("%c", (c < 33 || c == 255) ? 0x2E : c);
81
+ printf("%c", (c < 32 || c > 126) ? '.' : c);
8082 }
81
- printf("\n");
83
+ printf("|\n");
8284 if (length > 0)
8385 printf("%s | ", prefix);
8486 }
....@@ -126,18 +128,22 @@
126128 .bits_per_word = bits,
127129 };
128130
129
- if (mode & SPI_TX_QUAD)
131
+ if (mode & SPI_TX_OCTAL)
132
+ tr.tx_nbits = 8;
133
+ else if (mode & SPI_TX_QUAD)
130134 tr.tx_nbits = 4;
131135 else if (mode & SPI_TX_DUAL)
132136 tr.tx_nbits = 2;
133
- if (mode & SPI_RX_QUAD)
137
+ if (mode & SPI_RX_OCTAL)
138
+ tr.rx_nbits = 8;
139
+ else if (mode & SPI_RX_QUAD)
134140 tr.rx_nbits = 4;
135141 else if (mode & SPI_RX_DUAL)
136142 tr.rx_nbits = 2;
137143 if (!(mode & SPI_LOOP)) {
138
- if (mode & (SPI_TX_QUAD | SPI_TX_DUAL))
144
+ if (mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL))
139145 tr.rx_buf = 0;
140
- else if (mode & (SPI_RX_QUAD | SPI_RX_DUAL))
146
+ else if (mode & (SPI_RX_OCTAL | SPI_RX_QUAD | SPI_RX_DUAL))
141147 tr.tx_buf = 0;
142148 }
143149
....@@ -185,6 +191,7 @@
185191 " -R --ready slave pulls low to pause\n"
186192 " -2 --dual dual transfer\n"
187193 " -4 --quad quad transfer\n"
194
+ " -8 --octal octal transfer\n"
188195 " -S --size transfer size\n"
189196 " -I --iter iterations\n");
190197 exit(1);
....@@ -211,13 +218,14 @@
211218 { "dual", 0, 0, '2' },
212219 { "verbose", 0, 0, 'v' },
213220 { "quad", 0, 0, '4' },
221
+ { "octal", 0, 0, '8' },
214222 { "size", 1, 0, 'S' },
215223 { "iter", 1, 0, 'I' },
216224 { NULL, 0, 0, 0 },
217225 };
218226 int c;
219227
220
- c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR24p:vS:I:",
228
+ c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR248p:vS:I:",
221229 lopts, NULL);
222230
223231 if (c == -1)
....@@ -278,6 +286,9 @@
278286 case '4':
279287 mode |= SPI_TX_QUAD;
280288 break;
289
+ case '8':
290
+ mode |= SPI_TX_OCTAL;
291
+ break;
281292 case 'S':
282293 transfer_size = atoi(optarg);
283294 break;
....@@ -286,7 +297,6 @@
286297 break;
287298 default:
288299 print_usage(argv[0]);
289
- break;
290300 }
291301 }
292302 if (mode & SPI_LOOP) {
....@@ -294,6 +304,8 @@
294304 mode |= SPI_RX_DUAL;
295305 if (mode & SPI_TX_QUAD)
296306 mode |= SPI_RX_QUAD;
307
+ if (mode & SPI_TX_OCTAL)
308
+ mode |= SPI_RX_OCTAL;
297309 }
298310 }
299311
....@@ -408,6 +420,9 @@
408420
409421 parse_opts(argc, argv);
410422
423
+ if (input_tx && input_file)
424
+ pabort("only one of -p and --input may be selected");
425
+
411426 fd = open(device, O_RDWR);
412427 if (fd < 0)
413428 pabort("can't open device");
....@@ -446,11 +461,8 @@
446461 pabort("can't get max speed hz");
447462
448463 printf("spi mode: 0x%x\n", mode);
449
- printf("bits per word: %d\n", bits);
450
- printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
451
-
452
- if (input_tx && input_file)
453
- pabort("only one of -p and --input may be selected");
464
+ printf("bits per word: %u\n", bits);
465
+ printf("max speed: %u Hz (%u kHz)\n", speed, speed/1000);
454466
455467 if (input_tx)
456468 transfer_escaped_string(fd, input_tx);