1515#include <unistd.h>
1616#include <stdio.h>
1717#include <stdlib.h>
18+ #include <string.h>
1819#include <getopt.h>
1920#include <fcntl.h>
2021#include <sys/ioctl.h>
@@ -34,24 +35,79 @@ static uint32_t mode;
3435static uint8_t bits = 8 ;
3536static uint32_t speed = 500000 ;
3637static uint16_t delay ;
38+ static int verbose ;
3739
38- static void transfer (int fd )
40+ uint8_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+
49+ uint8_t default_rx [ARRAY_SIZE (default_tx )] = {0 , };
50+ char * input_tx ;
51+
52+ static void hex_dump (const void * src , size_t length , size_t line_size , char * prefix )
53+ {
54+ int i = 0 ;
55+ const unsigned char * address = src ;
56+ const unsigned char * line = address ;
57+ unsigned char c ;
58+
59+ printf ("%s | " , prefix );
60+ while (length -- > 0 ) {
61+ printf ("%02X " , * address ++ );
62+ if (!(++ i % line_size ) || (length == 0 && i % line_size )) {
63+ if (length == 0 ) {
64+ while (i ++ % line_size )
65+ printf ("__ " );
66+ }
67+ printf (" | " ); /* right close */
68+ while (line < address ) {
69+ c = * line ++ ;
70+ printf ("%c" , (c < 33 || c == 255 ) ? 0x2E : c );
71+ }
72+ printf ("\n" );
73+ if (length > 0 )
74+ printf ("%s | " , prefix );
75+ }
76+ }
77+ }
78+
79+ /*
80+ * Unescape - process hexadecimal escape character
81+ * converts shell input "\x23" -> 0x23
82+ */
83+ int 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+
103+ static void transfer (int fd , uint8_t const * tx , uint8_t const * rx , size_t len )
39104{
40105 int ret ;
41- uint8_t tx [] = {
42- 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
43- 0x40 , 0x00 , 0x00 , 0x00 , 0x00 , 0x95 ,
44- 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
45- 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
46- 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
47- 0xDE , 0xAD , 0xBE , 0xEF , 0xBA , 0xAD ,
48- 0xF0 , 0x0D ,
49- };
50- uint8_t rx [ARRAY_SIZE (tx )] = {0 , };
106+
51107 struct spi_ioc_transfer tr = {
52108 .tx_buf = (unsigned long )tx ,
53109 .rx_buf = (unsigned long )rx ,
54- .len = ARRAY_SIZE ( tx ) ,
110+ .len = len ,
55111 .delay_usecs = delay ,
56112 .speed_hz = speed ,
57113 .bits_per_word = bits ,
@@ -76,12 +132,9 @@ static void transfer(int fd)
76132 if (ret < 1 )
77133 pabort ("can't send spi message" );
78134
79- for (ret = 0 ; ret < ARRAY_SIZE (tx ); ret ++ ) {
80- if (!(ret % 6 ))
81- puts ("" );
82- printf ("%.2X " , rx [ret ]);
83- }
84- puts ("" );
135+ if (verbose )
136+ hex_dump (tx , len , 32 , "TX" );
137+ hex_dump (rx , len , 32 , "RX" );
85138}
86139
87140static void print_usage (const char * prog )
@@ -97,6 +150,8 @@ static void print_usage(const char *prog)
97150 " -L --lsb least significant bit first\n"
98151 " -C --cs-high chip select active high\n"
99152 " -3 --3wire SI/SO signals shared\n"
153+ " -v --verbose Verbose (show tx buffer)\n"
154+ " -p Send data (e.g. \"1234\\xde\\xad\")\n"
100155 " -N --no-cs no chip select\n"
101156 " -R --ready slave pulls low to pause\n"
102157 " -2 --dual dual transfer\n"
@@ -121,12 +176,13 @@ static void parse_opts(int argc, char *argv[])
121176 { "no-cs" , 0 , 0 , 'N' },
122177 { "ready" , 0 , 0 , 'R' },
123178 { "dual" , 0 , 0 , '2' },
179+ { "verbose" , 0 , 0 , 'v' },
124180 { "quad" , 0 , 0 , '4' },
125181 { NULL , 0 , 0 , 0 },
126182 };
127183 int c ;
128184
129- c = getopt_long (argc , argv , "D:s:d:b:lHOLC3NR24 " , lopts , NULL );
185+ c = getopt_long (argc , argv , "D:s:d:b:lHOLC3NR24p:v " , lopts , NULL );
130186
131187 if (c == -1 )
132188 break ;
@@ -165,9 +221,15 @@ static void parse_opts(int argc, char *argv[])
165221 case 'N' :
166222 mode |= SPI_NO_CS ;
167223 break ;
224+ case 'v' :
225+ verbose = 1 ;
226+ break ;
168227 case 'R' :
169228 mode |= SPI_READY ;
170229 break ;
230+ case 'p' :
231+ input_tx = optarg ;
232+ break ;
171233 case '2' :
172234 mode |= SPI_TX_DUAL ;
173235 break ;
@@ -191,6 +253,9 @@ int main(int argc, char *argv[])
191253{
192254 int ret = 0 ;
193255 int fd ;
256+ uint8_t * tx ;
257+ uint8_t * rx ;
258+ int size ;
194259
195260 parse_opts (argc , argv );
196261
@@ -235,7 +300,17 @@ int main(int argc, char *argv[])
235300 printf ("bits per word: %d\n" , bits );
236301 printf ("max speed: %d Hz (%d KHz)\n" , speed , speed /1000 );
237302
238- 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+ }
239314
240315 close (fd );
241316
0 commit comments