11 #include <linux/types.h>
12 #include <linux/spi/spidev.h>
17 static void do_read(int fd
, int len
)
19 unsigned char buf
[32], *bp
;
22 /* read at least 2 bytes, no more than 32 */
25 else if (len
> sizeof(buf
))
27 memset(buf
, 0, sizeof buf
);
29 status
= read(fd
, buf
, len
);
35 fprintf(stderr
, "short read\n");
39 printf("read(%2d, %2d): %02x %02x,", len
, status
,
44 printf(" %02x", *bp
++);
48 static void do_msg(int fd
, int len
)
50 struct spi_ioc_transfer xfer
[2];
51 unsigned char buf
[32], *bp
;
54 memset(xfer
, 0, sizeof xfer
);
55 memset(buf
, 0, sizeof buf
);
61 xfer
[0].tx_buf
= (__u64
) buf
;
64 xfer
[1].rx_buf
= (__u64
) buf
;
67 status
= ioctl(fd
, SPI_IOC_MESSAGE(2), xfer
);
69 perror("SPI_IOC_MESSAGE");
73 printf("response(%2d, %2d): ", len
, status
);
74 for (bp
= buf
; len
; len
--)
75 printf(" %02x", *bp
++);
79 static void dumpstat(const char *name
, int fd
)
84 if (ioctl(fd
, SPI_IOC_RD_MODE
, &mode
) < 0) {
85 perror("SPI rd_mode");
88 if (ioctl(fd
, SPI_IOC_RD_LSB_FIRST
, &lsb
) < 0) {
89 perror("SPI rd_lsb_fist");
92 if (ioctl(fd
, SPI_IOC_RD_BITS_PER_WORD
, &bits
) < 0) {
93 perror("SPI bits_per_word");
96 if (ioctl(fd
, SPI_IOC_RD_MAX_SPEED_HZ
, &speed
) < 0) {
97 perror("SPI max_speed_hz");
101 printf("%s: spi mode %d, %d bits %sper word, %d Hz max\n",
102 name
, mode
, bits
, lsb
? "(lsb first) " : "", speed
);
105 int main(int argc
, char **argv
)
113 while ((c
= getopt(argc
, argv
, "hm:r:v")) != EOF
) {
116 msglen
= atoi(optarg
);
121 readcount
= atoi(optarg
);
132 "usage: %s [-h] [-m N] [-r N] /dev/spidevB.D\n",
138 if ((optind
+ 1) != argc
)
142 fd
= open(name
, O_RDWR
);
154 do_read(fd
, readcount
);