1 // SPDX-License-Identifier: GPL-2.0
12 #include <linux/types.h>
13 #include <linux/spi/spidev.h>
18 static void do_read(int fd
, int len
)
20 unsigned char buf
[32], *bp
;
23 /* read at least 2 bytes, no more than 32 */
26 else if (len
> sizeof(buf
))
28 memset(buf
, 0, sizeof buf
);
30 status
= read(fd
, buf
, len
);
36 fprintf(stderr
, "short read\n");
40 printf("read(%2d, %2d): %02x %02x,", len
, status
,
45 printf(" %02x", *bp
++);
49 static void do_msg(int fd
, int len
)
51 struct spi_ioc_transfer xfer
[2];
52 unsigned char buf
[32], *bp
;
55 memset(xfer
, 0, sizeof xfer
);
56 memset(buf
, 0, sizeof buf
);
62 xfer
[0].tx_buf
= (unsigned long)buf
;
65 xfer
[1].rx_buf
= (unsigned long) buf
;
68 status
= ioctl(fd
, SPI_IOC_MESSAGE(2), xfer
);
70 perror("SPI_IOC_MESSAGE");
74 printf("response(%2d, %2d): ", len
, status
);
75 for (bp
= buf
; len
; len
--)
76 printf(" %02x", *bp
++);
80 static void dumpstat(const char *name
, int fd
)
85 if (ioctl(fd
, SPI_IOC_RD_MODE32
, &mode
) < 0) {
86 perror("SPI rd_mode");
89 if (ioctl(fd
, SPI_IOC_RD_LSB_FIRST
, &lsb
) < 0) {
90 perror("SPI rd_lsb_fist");
93 if (ioctl(fd
, SPI_IOC_RD_BITS_PER_WORD
, &bits
) < 0) {
94 perror("SPI bits_per_word");
97 if (ioctl(fd
, SPI_IOC_RD_MAX_SPEED_HZ
, &speed
) < 0) {
98 perror("SPI max_speed_hz");
102 printf("%s: spi mode 0x%x, %d bits %sper word, %d Hz max\n",
103 name
, mode
, bits
, lsb
? "(lsb first) " : "", speed
);
106 int main(int argc
, char **argv
)
114 while ((c
= getopt(argc
, argv
, "hm:r:v")) != EOF
) {
117 msglen
= atoi(optarg
);
122 readcount
= atoi(optarg
);
133 "usage: %s [-h] [-m N] [-r N] /dev/spidevB.D\n",
139 if ((optind
+ 1) != argc
)
143 fd
= open(name
, O_RDWR
);
155 do_read(fd
, readcount
);