20 #include <sys/ioctl.h>
21 #include <linux/types.h>
24 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
26 static void pabort(
const char *
s)
32 static const char *
device =
"/dev/spidev1.1";
38 static void transfer(
int fd)
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,
52 .tx_buf = (
unsigned long)tx,
57 .bits_per_word =
bits,
62 pabort(
"can't send spi message");
72 static void print_usage(
const char *prog)
74 printf(
"Usage: %s [-DsbdlHOLC3]\n", prog);
75 puts(
" -D --device device to use (default /dev/spidev1.1)\n"
76 " -s --speed max speed (Hz)\n"
77 " -d --delay delay (usec)\n"
78 " -b --bpw bits per word \n"
79 " -l --loop loopback\n"
80 " -H --cpha clock phase\n"
81 " -O --cpol clock polarity\n"
82 " -L --lsb least significant bit first\n"
83 " -C --cs-high chip select active high\n"
84 " -3 --3wire SI/SO signals shared\n");
88 static void parse_opts(
int argc,
char *argv[])
91 static const struct option lopts[] = {
92 {
"device", 1, 0,
'D' },
93 {
"speed", 1, 0,
's' },
94 {
"delay", 1, 0,
'd' },
96 {
"loop", 0, 0,
'l' },
97 {
"cpha", 0, 0,
'H' },
98 {
"cpol", 0, 0,
'O' },
100 {
"cs-high", 0, 0,
'C' },
101 {
"3wire", 0, 0,
'3' },
102 {
"no-cs", 0, 0,
'N' },
103 {
"ready", 0, 0,
'R' },
108 c = getopt_long(argc, argv,
"D:s:d:b:lHOLC3NR", lopts,
NULL);
118 speed = atoi(optarg);
121 delay = atoi(optarg);
151 print_usage(argv[0]);
157 int main(
int argc,
char *argv[])
162 parse_opts(argc, argv);
166 pabort(
"can't open device");
173 pabort(
"can't set spi mode");
177 pabort(
"can't get spi mode");
184 pabort(
"can't set bits per word");
188 pabort(
"can't get bits per word");
195 pabort(
"can't set max speed hz");
199 pabort(
"can't get max speed hz");
203 printf(
"max speed: %d Hz (%d KHz)\n", speed, speed/1000);