Blame view

tools/spi/spidev_fdx.c 2.73 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
31a162942   Randy Dunlap   documentation: mo...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  #include <stdio.h>
  #include <unistd.h>
  #include <stdlib.h>
  #include <fcntl.h>
  #include <string.h>
  
  #include <sys/ioctl.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  
  #include <linux/types.h>
  #include <linux/spi/spidev.h>
  
  
  static int verbose;
  
  static void do_read(int fd, int len)
  {
  	unsigned char	buf[32], *bp;
  	int		status;
  
  	/* read at least 2 bytes, no more than 32 */
  	if (len < 2)
  		len = 2;
  	else if (len > sizeof(buf))
  		len = sizeof(buf);
  	memset(buf, 0, sizeof buf);
  
  	status = read(fd, buf, len);
  	if (status < 0) {
  		perror("read");
  		return;
  	}
  	if (status != len) {
  		fprintf(stderr, "short read
  ");
  		return;
  	}
  
  	printf("read(%2d, %2d): %02x %02x,", len, status,
  		buf[0], buf[1]);
  	status -= 2;
  	bp = buf + 2;
  	while (status-- > 0)
  		printf(" %02x", *bp++);
  	printf("
  ");
  }
  
  static void do_msg(int fd, int len)
  {
  	struct spi_ioc_transfer	xfer[2];
  	unsigned char		buf[32], *bp;
  	int			status;
  
  	memset(xfer, 0, sizeof xfer);
  	memset(buf, 0, sizeof buf);
  
  	if (len > sizeof buf)
  		len = sizeof buf;
  
  	buf[0] = 0xaa;
1dcf57ceb   Prarit Bhargava   Documentation/spi...
64
  	xfer[0].tx_buf = (unsigned long)buf;
31a162942   Randy Dunlap   documentation: mo...
65
  	xfer[0].len = 1;
1dcf57ceb   Prarit Bhargava   Documentation/spi...
66
  	xfer[1].rx_buf = (unsigned long) buf;
31a162942   Randy Dunlap   documentation: mo...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  	xfer[1].len = len;
  
  	status = ioctl(fd, SPI_IOC_MESSAGE(2), xfer);
  	if (status < 0) {
  		perror("SPI_IOC_MESSAGE");
  		return;
  	}
  
  	printf("response(%2d, %2d): ", len, status);
  	for (bp = buf; len; len--)
  		printf(" %02x", *bp++);
  	printf("
  ");
  }
  
  static void dumpstat(const char *name, int fd)
  {
4189a728a   Geert Uytterhoeven   spi: spidev_fdx: ...
84
85
  	__u8	lsb, bits;
  	__u32	mode, speed;
31a162942   Randy Dunlap   documentation: mo...
86

4189a728a   Geert Uytterhoeven   spi: spidev_fdx: ...
87
  	if (ioctl(fd, SPI_IOC_RD_MODE32, &mode) < 0) {
31a162942   Randy Dunlap   documentation: mo...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  		perror("SPI rd_mode");
  		return;
  	}
  	if (ioctl(fd, SPI_IOC_RD_LSB_FIRST, &lsb) < 0) {
  		perror("SPI rd_lsb_fist");
  		return;
  	}
  	if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0) {
  		perror("SPI bits_per_word");
  		return;
  	}
  	if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) {
  		perror("SPI max_speed_hz");
  		return;
  	}
4189a728a   Geert Uytterhoeven   spi: spidev_fdx: ...
103
104
  	printf("%s: spi mode 0x%x, %d bits %sper word, %d Hz max
  ",
31a162942   Randy Dunlap   documentation: mo...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  		name, mode, bits, lsb ? "(lsb first) " : "", speed);
  }
  
  int main(int argc, char **argv)
  {
  	int		c;
  	int		readcount = 0;
  	int		msglen = 0;
  	int		fd;
  	const char	*name;
  
  	while ((c = getopt(argc, argv, "hm:r:v")) != EOF) {
  		switch (c) {
  		case 'm':
  			msglen = atoi(optarg);
  			if (msglen < 0)
  				goto usage;
  			continue;
  		case 'r':
  			readcount = atoi(optarg);
  			if (readcount < 0)
  				goto usage;
  			continue;
  		case 'v':
  			verbose++;
  			continue;
  		case 'h':
  		case '?':
  usage:
  			fprintf(stderr,
  				"usage: %s [-h] [-m N] [-r N] /dev/spidevB.D
  ",
  				argv[0]);
  			return 1;
  		}
  	}
  
  	if ((optind + 1) != argc)
  		goto usage;
  	name = argv[optind];
  
  	fd = open(name, O_RDWR);
  	if (fd < 0) {
  		perror("open");
  		return 1;
  	}
  
  	dumpstat(name, fd);
  
  	if (msglen)
  		do_msg(fd, msglen);
  
  	if (readcount)
  		do_read(fd, readcount);
  
  	close(fd);
  	return 0;
  }