diff options
author | Joshua Clayton <stillcompiling@gmail.com> | 2015-11-18 17:30:39 -0500 |
---|---|---|
committer | Mark Brown <broonie@kernel.org> | 2015-11-23 09:54:01 -0500 |
commit | 7af475a5b55e8e47327818e8133fe22204c1fc4d (patch) | |
tree | b98eadf73ca9b4b848e02b440d3e70f3db789c5c /tools/spi | |
parent | 5c437a401b824399b6fa7342c66b675ebb7af43e (diff) |
spi: spidev_test: accept input from a file
Add input file support to facilitate testing larger data.
Signed-off-by: Joshua Clayton <stillcompiling@gmail.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
Diffstat (limited to 'tools/spi')
-rw-r--r-- | tools/spi/spidev_test.c | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index f9d2957e538b..71a45a4d7d49 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c | |||
@@ -19,6 +19,7 @@ | |||
19 | #include <getopt.h> | 19 | #include <getopt.h> |
20 | #include <fcntl.h> | 20 | #include <fcntl.h> |
21 | #include <sys/ioctl.h> | 21 | #include <sys/ioctl.h> |
22 | #include <sys/stat.h> | ||
22 | #include <linux/types.h> | 23 | #include <linux/types.h> |
23 | #include <linux/spi/spidev.h> | 24 | #include <linux/spi/spidev.h> |
24 | 25 | ||
@@ -33,6 +34,7 @@ static void pabort(const char *s) | |||
33 | static const char *device = "/dev/spidev1.1"; | 34 | static const char *device = "/dev/spidev1.1"; |
34 | static uint32_t mode; | 35 | static uint32_t mode; |
35 | static uint8_t bits = 8; | 36 | static uint8_t bits = 8; |
37 | static char *input_file; | ||
36 | static uint32_t speed = 500000; | 38 | static uint32_t speed = 500000; |
37 | static uint16_t delay; | 39 | static uint16_t delay; |
38 | static int verbose; | 40 | static int verbose; |
@@ -144,6 +146,7 @@ static void print_usage(const char *prog) | |||
144 | " -s --speed max speed (Hz)\n" | 146 | " -s --speed max speed (Hz)\n" |
145 | " -d --delay delay (usec)\n" | 147 | " -d --delay delay (usec)\n" |
146 | " -b --bpw bits per word \n" | 148 | " -b --bpw bits per word \n" |
149 | " -i --input input data from a file (e.g. \"test.bin\")\n" | ||
147 | " -l --loop loopback\n" | 150 | " -l --loop loopback\n" |
148 | " -H --cpha clock phase\n" | 151 | " -H --cpha clock phase\n" |
149 | " -O --cpol clock polarity\n" | 152 | " -O --cpol clock polarity\n" |
@@ -167,6 +170,7 @@ static void parse_opts(int argc, char *argv[]) | |||
167 | { "speed", 1, 0, 's' }, | 170 | { "speed", 1, 0, 's' }, |
168 | { "delay", 1, 0, 'd' }, | 171 | { "delay", 1, 0, 'd' }, |
169 | { "bpw", 1, 0, 'b' }, | 172 | { "bpw", 1, 0, 'b' }, |
173 | { "input", 1, 0, 'i' }, | ||
170 | { "loop", 0, 0, 'l' }, | 174 | { "loop", 0, 0, 'l' }, |
171 | { "cpha", 0, 0, 'H' }, | 175 | { "cpha", 0, 0, 'H' }, |
172 | { "cpol", 0, 0, 'O' }, | 176 | { "cpol", 0, 0, 'O' }, |
@@ -182,7 +186,8 @@ static void parse_opts(int argc, char *argv[]) | |||
182 | }; | 186 | }; |
183 | int c; | 187 | int c; |
184 | 188 | ||
185 | c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR24p:v", lopts, NULL); | 189 | c = getopt_long(argc, argv, "D:s:d:b:i:lHOLC3NR24p:v", |
190 | lopts, NULL); | ||
186 | 191 | ||
187 | if (c == -1) | 192 | if (c == -1) |
188 | break; | 193 | break; |
@@ -200,6 +205,9 @@ static void parse_opts(int argc, char *argv[]) | |||
200 | case 'b': | 205 | case 'b': |
201 | bits = atoi(optarg); | 206 | bits = atoi(optarg); |
202 | break; | 207 | break; |
208 | case 'i': | ||
209 | input_file = optarg; | ||
210 | break; | ||
203 | case 'l': | 211 | case 'l': |
204 | mode |= SPI_LOOP; | 212 | mode |= SPI_LOOP; |
205 | break; | 213 | break; |
@@ -269,6 +277,39 @@ static void transfer_escaped_string(int fd, char *str) | |||
269 | free(tx); | 277 | free(tx); |
270 | } | 278 | } |
271 | 279 | ||
280 | static void transfer_file(int fd, char *filename) | ||
281 | { | ||
282 | ssize_t bytes; | ||
283 | struct stat sb; | ||
284 | int tx_fd; | ||
285 | uint8_t *tx; | ||
286 | uint8_t *rx; | ||
287 | |||
288 | if (stat(filename, &sb) == -1) | ||
289 | pabort("can't stat input file"); | ||
290 | |||
291 | tx_fd = open(filename, O_RDONLY); | ||
292 | if (fd < 0) | ||
293 | pabort("can't open input file"); | ||
294 | |||
295 | tx = malloc(sb.st_size); | ||
296 | if (!tx) | ||
297 | pabort("can't allocate tx buffer"); | ||
298 | |||
299 | rx = malloc(sb.st_size); | ||
300 | if (!rx) | ||
301 | pabort("can't allocate rx buffer"); | ||
302 | |||
303 | bytes = read(tx_fd, tx, sb.st_size); | ||
304 | if (bytes != sb.st_size) | ||
305 | pabort("failed to read input file"); | ||
306 | |||
307 | transfer(fd, tx, rx, sb.st_size); | ||
308 | free(rx); | ||
309 | free(tx); | ||
310 | close(tx_fd); | ||
311 | } | ||
312 | |||
272 | int main(int argc, char *argv[]) | 313 | int main(int argc, char *argv[]) |
273 | { | 314 | { |
274 | int ret = 0; | 315 | int ret = 0; |
@@ -317,8 +358,13 @@ int main(int argc, char *argv[]) | |||
317 | printf("bits per word: %d\n", bits); | 358 | printf("bits per word: %d\n", bits); |
318 | printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); | 359 | printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); |
319 | 360 | ||
361 | if (input_tx && input_file) | ||
362 | pabort("only one of -p and --input may be selected"); | ||
363 | |||
320 | if (input_tx) | 364 | if (input_tx) |
321 | transfer_escaped_string(fd, input_tx); | 365 | transfer_escaped_string(fd, input_tx); |
366 | else if (input_file) | ||
367 | transfer_file(fd, input_file); | ||
322 | else | 368 | else |
323 | transfer(fd, default_tx, default_rx, sizeof(default_tx)); | 369 | transfer(fd, default_tx, default_rx, sizeof(default_tx)); |
324 | 370 | ||