Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * SPI testing utility (using spidev driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2007  MontaVista Software, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2007  Anton Vorontsov <avorontsov@ru.mvista.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Cross-compile with cross-gcc -I/path/to/cross-kernel/include
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <getopt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/spi/spidev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static void pabort(const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	if (errno != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		perror(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		printf("%s\n", s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	abort();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static const char *device = "/dev/spidev1.1";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static uint32_t mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static uint8_t bits = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static char *input_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static char *output_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static uint32_t speed = 500000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static uint16_t delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static int verbose;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static int transfer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static int iterations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static int interval = 5; /* interval in seconds for showing transfer rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static uint8_t default_tx[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	0xF0, 0x0D,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static char *input_tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static void hex_dump(const void *src, size_t length, size_t line_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		     char *prefix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	const unsigned char *address = src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	const unsigned char *line = address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	unsigned char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	printf("%s | ", prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	while (length-- > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		printf("%02X ", *address++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		if (!(++i % line_size) || (length == 0 && i % line_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			if (length == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 				while (i++ % line_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 					printf("__ ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			printf(" |");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			while (line < address) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 				c = *line++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 				printf("%c", (c < 32 || c > 126) ? '.' : c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			printf("|\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			if (length > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 				printf("%s | ", prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  *  Unescape - process hexadecimal escape character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  *      converts shell input "\x23" -> 0x23
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static int unescape(char *_dst, char *_src, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	int match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	char *src = _src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	char *dst = _dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	unsigned int ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	while (*src) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		if (*src == '\\' && *(src+1) == 'x') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			match = sscanf(src + 2, "%2x", &ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			if (!match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 				pabort("malformed input string");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			src += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			*dst++ = (unsigned char)ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			*dst++ = *src++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		ret++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	int out_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	struct spi_ioc_transfer tr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		.tx_buf = (unsigned long)tx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		.rx_buf = (unsigned long)rx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		.len = len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		.delay_usecs = delay,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		.speed_hz = speed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		.bits_per_word = bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (mode & SPI_TX_OCTAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		tr.tx_nbits = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	else if (mode & SPI_TX_QUAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		tr.tx_nbits = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	else if (mode & SPI_TX_DUAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		tr.tx_nbits = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (mode & SPI_RX_OCTAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		tr.rx_nbits = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	else if (mode & SPI_RX_QUAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		tr.rx_nbits = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	else if (mode & SPI_RX_DUAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		tr.rx_nbits = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (!(mode & SPI_LOOP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		if (mode & (SPI_TX_OCTAL | SPI_TX_QUAD | SPI_TX_DUAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			tr.rx_buf = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		else if (mode & (SPI_RX_OCTAL | SPI_RX_QUAD | SPI_RX_DUAL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			tr.tx_buf = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (ret < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		pabort("can't send spi message");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		hex_dump(tx, len, 32, "TX");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if (output_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		out_fd = open(output_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		if (out_fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			pabort("could not open output file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		ret = write(out_fd, rx, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		if (ret != len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			pabort("not all bytes written to output file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		close(out_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (verbose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		hex_dump(rx, len, 32, "RX");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static void print_usage(const char *prog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	printf("Usage: %s [-DsbdlHOLC3vpNR24SI]\n", prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	puts("  -D --device   device to use (default /dev/spidev1.1)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	     "  -s --speed    max speed (Hz)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	     "  -d --delay    delay (usec)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	     "  -b --bpw      bits per word\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	     "  -i --input    input data from a file (e.g. \"test.bin\")\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	     "  -o --output   output data to a file (e.g. \"results.bin\")\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	     "  -l --loop     loopback\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	     "  -H --cpha     clock phase\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	     "  -O --cpol     clock polarity\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	     "  -L --lsb      least significant bit first\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	     "  -C --cs-high  chip select active high\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	     "  -3 --3wire    SI/SO signals shared\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	     "  -v --verbose  Verbose (show tx buffer)\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	     "  -p            Send data (e.g. \"1234\\xde\\xad\")\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	     "  -N --no-cs    no chip select\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	     "  -R --ready    slave pulls low to pause\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	     "  -2 --dual     dual transfer\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	     "  -4 --quad     quad transfer\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	     "  -8 --octal    octal transfer\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	     "  -S --size     transfer size\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	     "  -I --iter     iterations\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static void parse_opts(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		static const struct option lopts[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			{ "device",  1, 0, 'D' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			{ "speed",   1, 0, 's' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			{ "delay",   1, 0, 'd' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			{ "bpw",     1, 0, 'b' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			{ "input",   1, 0, 'i' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			{ "output",  1, 0, 'o' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			{ "loop",    0, 0, 'l' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			{ "cpha",    0, 0, 'H' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			{ "cpol",    0, 0, 'O' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			{ "lsb",     0, 0, 'L' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			{ "cs-high", 0, 0, 'C' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			{ "3wire",   0, 0, '3' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			{ "no-cs",   0, 0, 'N' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			{ "ready",   0, 0, 'R' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			{ "dual",    0, 0, '2' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			{ "verbose", 0, 0, 'v' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			{ "quad",    0, 0, '4' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			{ "octal",   0, 0, '8' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			{ "size",    1, 0, 'S' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			{ "iter",    1, 0, 'I' },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			{ NULL, 0, 0, 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		int c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR248p:vS:I:",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 				lopts, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		if (c == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		switch (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		case 'D':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			device = optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		case 's':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			speed = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		case 'd':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			delay = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		case 'b':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			bits = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		case 'i':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			input_file = optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		case 'o':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			output_file = optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		case 'l':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			mode |= SPI_LOOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		case 'H':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			mode |= SPI_CPHA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		case 'O':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			mode |= SPI_CPOL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		case 'L':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			mode |= SPI_LSB_FIRST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		case 'C':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			mode |= SPI_CS_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		case '3':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			mode |= SPI_3WIRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		case 'N':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			mode |= SPI_NO_CS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		case 'v':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			verbose = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		case 'R':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			mode |= SPI_READY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		case 'p':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			input_tx = optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		case '2':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			mode |= SPI_TX_DUAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		case '4':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			mode |= SPI_TX_QUAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		case '8':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			mode |= SPI_TX_OCTAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		case 'S':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			transfer_size = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		case 'I':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			iterations = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			print_usage(argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	if (mode & SPI_LOOP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		if (mode & SPI_TX_DUAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			mode |= SPI_RX_DUAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		if (mode & SPI_TX_QUAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			mode |= SPI_RX_QUAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		if (mode & SPI_TX_OCTAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			mode |= SPI_RX_OCTAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static void transfer_escaped_string(int fd, char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	size_t size = strlen(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	uint8_t *tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	uint8_t *rx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	tx = malloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (!tx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		pabort("can't allocate tx buffer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	rx = malloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (!rx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		pabort("can't allocate rx buffer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	size = unescape((char *)tx, str, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	transfer(fd, tx, rx, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	free(rx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	free(tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static void transfer_file(int fd, char *filename)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	ssize_t bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	struct stat sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	int tx_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	uint8_t *tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	uint8_t *rx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (stat(filename, &sb) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		pabort("can't stat input file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	tx_fd = open(filename, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (tx_fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		pabort("can't open input file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	tx = malloc(sb.st_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (!tx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		pabort("can't allocate tx buffer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	rx = malloc(sb.st_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (!rx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		pabort("can't allocate rx buffer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	bytes = read(tx_fd, tx, sb.st_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (bytes != sb.st_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		pabort("failed to read input file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	transfer(fd, tx, rx, sb.st_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	free(rx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	free(tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	close(tx_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static uint64_t _read_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static uint64_t _write_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) static void show_transfer_rate(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	static uint64_t prev_read_count, prev_write_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	double rx_rate, tx_rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	rx_rate = ((_read_count - prev_read_count) * 8) / (interval*1000.0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	tx_rate = ((_write_count - prev_write_count) * 8) / (interval*1000.0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	printf("rate: tx %.1fkbps, rx %.1fkbps\n", rx_rate, tx_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	prev_read_count = _read_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	prev_write_count = _write_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static void transfer_buf(int fd, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	uint8_t *tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	uint8_t *rx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	tx = malloc(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (!tx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		pabort("can't allocate tx buffer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	for (i = 0; i < len; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		tx[i] = random();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	rx = malloc(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	if (!rx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		pabort("can't allocate rx buffer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	transfer(fd, tx, rx, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	_write_count += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	_read_count += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	if (mode & SPI_LOOP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		if (memcmp(tx, rx, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			fprintf(stderr, "transfer error !\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			hex_dump(tx, len, 32, "TX");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			hex_dump(rx, len, 32, "RX");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	free(rx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	free(tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	parse_opts(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	if (input_tx && input_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		pabort("only one of -p and --input may be selected");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	fd = open(device, O_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		pabort("can't open device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	 * spi mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	ret = ioctl(fd, SPI_IOC_WR_MODE32, &mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	if (ret == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		pabort("can't set spi mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	ret = ioctl(fd, SPI_IOC_RD_MODE32, &mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	if (ret == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		pabort("can't get spi mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	 * bits per word
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (ret == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		pabort("can't set bits per word");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	if (ret == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		pabort("can't get bits per word");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	 * max speed hz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	if (ret == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		pabort("can't set max speed hz");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	if (ret == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		pabort("can't get max speed hz");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	printf("spi mode: 0x%x\n", mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	printf("bits per word: %u\n", bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	printf("max speed: %u Hz (%u kHz)\n", speed, speed/1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	if (input_tx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		transfer_escaped_string(fd, input_tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	else if (input_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		transfer_file(fd, input_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	else if (transfer_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		struct timespec last_stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		clock_gettime(CLOCK_MONOTONIC, &last_stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		while (iterations-- > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			struct timespec current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 			transfer_buf(fd, transfer_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 			clock_gettime(CLOCK_MONOTONIC, &current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 			if (current.tv_sec - last_stat.tv_sec > interval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 				show_transfer_rate();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 				last_stat = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		printf("total: tx %.1fKB, rx %.1fKB\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		       _write_count/1024.0, _read_count/1024.0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		transfer(fd, default_tx, default_rx, sizeof(default_tx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }