aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdrian Remonda <adrianremonda@gmail.com>2015-03-10 16:12:32 -0400
committerMark Brown <broonie@kernel.org>2015-03-10 16:51:11 -0400
commit30061915be6e3a2c2a4bc4f22a229d4eb7b7cc65 (patch)
tree5f30593611d11e8ee9a11a0c03667a9363a2c07a
parent31a5c5a72b90e074bd2377ada2bdfd506801ed72 (diff)
spi: spidev_test: Added input buffer from the terminal
Signed-off-by: Adrian Remonda <adrianremonda@gmail.com> Signed-off-by: Mark Brown <broonie@kernel.org>
-rw-r--r--Documentation/spi/spidev_test.c76
1 files changed, 60 insertions, 16 deletions
diff --git a/Documentation/spi/spidev_test.c b/Documentation/spi/spidev_test.c
index 4b6a3803638b..94f574b0fdb2 100644
--- a/Documentation/spi/spidev_test.c
+++ b/Documentation/spi/spidev_test.c
@@ -37,6 +37,18 @@ static uint32_t speed = 500000;
37static uint16_t delay; 37static uint16_t delay;
38static int verbose; 38static int verbose;
39 39
40uint8_t default_tx[] = {
41 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
42 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
43 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
44 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
45 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
46 0xF0, 0x0D,
47};
48
49uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
50char *input_tx;
51
40static void hex_dump(const void *src, size_t length, size_t line_size, char *prefix) 52static void hex_dump(const void *src, size_t length, size_t line_size, char *prefix)
41{ 53{
42 int i = 0; 54 int i = 0;
@@ -64,23 +76,38 @@ static void hex_dump(const void *src, size_t length, size_t line_size, char *pre
64 } 76 }
65} 77}
66 78
67static void transfer(int fd) 79/*
80 * Unescape - process hexadecimal escape character
81 * converts shell input "\x23" -> 0x23
82 */
83int unespcape(char *_dst, char *_src, size_t len)
84{
85 int ret = 0;
86 char *src = _src;
87 char *dst = _dst;
88 unsigned int ch;
89
90 while (*src) {
91 if (*src == '\\' && *(src+1) == 'x') {
92 sscanf(src + 2, "%2x", &ch);
93 src += 4;
94 *dst++ = (unsigned char)ch;
95 } else {
96 *dst++ = *src++;
97 }
98 ret++;
99 }
100 return ret;
101}
102
103static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)
68{ 104{
69 int ret; 105 int ret;
70 uint8_t tx[] = { 106
71 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
72 0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
73 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
74 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
75 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
76 0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
77 0xF0, 0x0D,
78 };
79 uint8_t rx[ARRAY_SIZE(tx)] = {0, };
80 struct spi_ioc_transfer tr = { 107 struct spi_ioc_transfer tr = {
81 .tx_buf = (unsigned long)tx, 108 .tx_buf = (unsigned long)tx,
82 .rx_buf = (unsigned long)rx, 109 .rx_buf = (unsigned long)rx,
83 .len = ARRAY_SIZE(tx), 110 .len = len,
84 .delay_usecs = delay, 111 .delay_usecs = delay,
85 .speed_hz = speed, 112 .speed_hz = speed,
86 .bits_per_word = bits, 113 .bits_per_word = bits,
@@ -106,8 +133,8 @@ static void transfer(int fd)
106 pabort("can't send spi message"); 133 pabort("can't send spi message");
107 134
108 if (verbose) 135 if (verbose)
109 hex_dump(tx, ARRAY_SIZE(tx), 32, "TX"); 136 hex_dump(tx, len, 32, "TX");
110 hex_dump(rx, ARRAY_SIZE(rx), 32, "RX"); 137 hex_dump(rx, len, 32, "RX");
111} 138}
112 139
113static void print_usage(const char *prog) 140static void print_usage(const char *prog)
@@ -124,6 +151,7 @@ static void print_usage(const char *prog)
124 " -C --cs-high chip select active high\n" 151 " -C --cs-high chip select active high\n"
125 " -3 --3wire SI/SO signals shared\n" 152 " -3 --3wire SI/SO signals shared\n"
126 " -v --verbose Verbose (show tx buffer)\n" 153 " -v --verbose Verbose (show tx buffer)\n"
154 " -p Send data (e.g. \"1234\\xde\\xad\")\n"
127 " -N --no-cs no chip select\n" 155 " -N --no-cs no chip select\n"
128 " -R --ready slave pulls low to pause\n" 156 " -R --ready slave pulls low to pause\n"
129 " -2 --dual dual transfer\n" 157 " -2 --dual dual transfer\n"
@@ -154,7 +182,7 @@ static void parse_opts(int argc, char *argv[])
154 }; 182 };
155 int c; 183 int c;
156 184
157 c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR24:v", lopts, NULL); 185 c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR24p:v", lopts, NULL);
158 186
159 if (c == -1) 187 if (c == -1)
160 break; 188 break;
@@ -199,6 +227,9 @@ static void parse_opts(int argc, char *argv[])
199 case 'R': 227 case 'R':
200 mode |= SPI_READY; 228 mode |= SPI_READY;
201 break; 229 break;
230 case 'p':
231 input_tx = optarg;
232 break;
202 case '2': 233 case '2':
203 mode |= SPI_TX_DUAL; 234 mode |= SPI_TX_DUAL;
204 break; 235 break;
@@ -222,6 +253,9 @@ int main(int argc, char *argv[])
222{ 253{
223 int ret = 0; 254 int ret = 0;
224 int fd; 255 int fd;
256 uint8_t *tx;
257 uint8_t *rx;
258 int size;
225 259
226 parse_opts(argc, argv); 260 parse_opts(argc, argv);
227 261
@@ -266,7 +300,17 @@ int main(int argc, char *argv[])
266 printf("bits per word: %d\n", bits); 300 printf("bits per word: %d\n", bits);
267 printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); 301 printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
268 302
269 transfer(fd); 303 if (input_tx) {
304 size = strlen(input_tx+1);
305 tx = malloc(size);
306 rx = malloc(size);
307 size = unespcape((char *)tx, input_tx, size);
308 transfer(fd, tx, rx, size);
309 free(rx);
310 free(tx);
311 } else {
312 transfer(fd, default_tx, default_rx, sizeof(default_tx));
313 }
270 314
271 close(fd); 315 close(fd);
272 316