diff options
author | Randy Dunlap <randy.dunlap@oracle.com> | 2008-04-28 05:14:18 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2008-04-28 11:58:31 -0400 |
commit | 31a16294261a897ab7f59a5c26e4935a851fd410 (patch) | |
tree | dc2ba43fad17fe572c36befd6d9d05ec26af8e40 /Documentation/spi/spidev | |
parent | cf43369d55a30a0d8f9ef4700c798c72dbd3afb7 (diff) |
documentation: move spidev_fdx example to its own source file
Move sample source code to its own source file so that it can be used
easier and build-tested/check/maintained by anyone.
(Makefile changes are in a separate patch for all of Documentation/.)
Signed-off-by: Randy Dunlap <randy.dunlap@oracle.com>
Acked-by: David Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'Documentation/spi/spidev')
-rw-r--r-- | Documentation/spi/spidev | 168 |
1 files changed, 2 insertions, 166 deletions
diff --git a/Documentation/spi/spidev b/Documentation/spi/spidev index 5c8e1b988a08..ed2da5e5b28a 100644 --- a/Documentation/spi/spidev +++ b/Documentation/spi/spidev | |||
@@ -126,8 +126,8 @@ NOTES: | |||
126 | FULL DUPLEX CHARACTER DEVICE API | 126 | FULL DUPLEX CHARACTER DEVICE API |
127 | ================================ | 127 | ================================ |
128 | 128 | ||
129 | See the sample program below for one example showing the use of the full | 129 | See the spidev_fdx.c sample program for one example showing the use of the |
130 | duplex programming interface. (Although it doesn't perform a full duplex | 130 | full duplex programming interface. (Although it doesn't perform a full duplex |
131 | transfer.) The model is the same as that used in the kernel spi_sync() | 131 | transfer.) The model is the same as that used in the kernel spi_sync() |
132 | request; the individual transfers offer the same capabilities as are | 132 | request; the individual transfers offer the same capabilities as are |
133 | available to kernel drivers (except that it's not asynchronous). | 133 | available to kernel drivers (except that it's not asynchronous). |
@@ -141,167 +141,3 @@ and bitrate for each transfer segment.) | |||
141 | 141 | ||
142 | To make a full duplex request, provide both rx_buf and tx_buf for the | 142 | To make a full duplex request, provide both rx_buf and tx_buf for the |
143 | same transfer. It's even OK if those are the same buffer. | 143 | same transfer. It's even OK if those are the same buffer. |
144 | |||
145 | |||
146 | SAMPLE PROGRAM | ||
147 | ============== | ||
148 | |||
149 | -------------------------------- CUT HERE | ||
150 | #include <stdio.h> | ||
151 | #include <unistd.h> | ||
152 | #include <stdlib.h> | ||
153 | #include <fcntl.h> | ||
154 | #include <string.h> | ||
155 | |||
156 | #include <sys/ioctl.h> | ||
157 | #include <sys/types.h> | ||
158 | #include <sys/stat.h> | ||
159 | |||
160 | #include <linux/types.h> | ||
161 | #include <linux/spi/spidev.h> | ||
162 | |||
163 | |||
164 | static int verbose; | ||
165 | |||
166 | static void do_read(int fd, int len) | ||
167 | { | ||
168 | unsigned char buf[32], *bp; | ||
169 | int status; | ||
170 | |||
171 | /* read at least 2 bytes, no more than 32 */ | ||
172 | if (len < 2) | ||
173 | len = 2; | ||
174 | else if (len > sizeof(buf)) | ||
175 | len = sizeof(buf); | ||
176 | memset(buf, 0, sizeof buf); | ||
177 | |||
178 | status = read(fd, buf, len); | ||
179 | if (status < 0) { | ||
180 | perror("read"); | ||
181 | return; | ||
182 | } | ||
183 | if (status != len) { | ||
184 | fprintf(stderr, "short read\n"); | ||
185 | return; | ||
186 | } | ||
187 | |||
188 | printf("read(%2d, %2d): %02x %02x,", len, status, | ||
189 | buf[0], buf[1]); | ||
190 | status -= 2; | ||
191 | bp = buf + 2; | ||
192 | while (status-- > 0) | ||
193 | printf(" %02x", *bp++); | ||
194 | printf("\n"); | ||
195 | } | ||
196 | |||
197 | static void do_msg(int fd, int len) | ||
198 | { | ||
199 | struct spi_ioc_transfer xfer[2]; | ||
200 | unsigned char buf[32], *bp; | ||
201 | int status; | ||
202 | |||
203 | memset(xfer, 0, sizeof xfer); | ||
204 | memset(buf, 0, sizeof buf); | ||
205 | |||
206 | if (len > sizeof buf) | ||
207 | len = sizeof buf; | ||
208 | |||
209 | buf[0] = 0xaa; | ||
210 | xfer[0].tx_buf = (__u64) buf; | ||
211 | xfer[0].len = 1; | ||
212 | |||
213 | xfer[1].rx_buf = (__u64) buf; | ||
214 | xfer[1].len = len; | ||
215 | |||
216 | status = ioctl(fd, SPI_IOC_MESSAGE(2), xfer); | ||
217 | if (status < 0) { | ||
218 | perror("SPI_IOC_MESSAGE"); | ||
219 | return; | ||
220 | } | ||
221 | |||
222 | printf("response(%2d, %2d): ", len, status); | ||
223 | for (bp = buf; len; len--) | ||
224 | printf(" %02x", *bp++); | ||
225 | printf("\n"); | ||
226 | } | ||
227 | |||
228 | static void dumpstat(const char *name, int fd) | ||
229 | { | ||
230 | __u8 mode, lsb, bits; | ||
231 | __u32 speed; | ||
232 | |||
233 | if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0) { | ||
234 | perror("SPI rd_mode"); | ||
235 | return; | ||
236 | } | ||
237 | if (ioctl(fd, SPI_IOC_RD_LSB_FIRST, &lsb) < 0) { | ||
238 | perror("SPI rd_lsb_fist"); | ||
239 | return; | ||
240 | } | ||
241 | if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0) { | ||
242 | perror("SPI bits_per_word"); | ||
243 | return; | ||
244 | } | ||
245 | if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) { | ||
246 | perror("SPI max_speed_hz"); | ||
247 | return; | ||
248 | } | ||
249 | |||
250 | printf("%s: spi mode %d, %d bits %sper word, %d Hz max\n", | ||
251 | name, mode, bits, lsb ? "(lsb first) " : "", speed); | ||
252 | } | ||
253 | |||
254 | int main(int argc, char **argv) | ||
255 | { | ||
256 | int c; | ||
257 | int readcount = 0; | ||
258 | int msglen = 0; | ||
259 | int fd; | ||
260 | const char *name; | ||
261 | |||
262 | while ((c = getopt(argc, argv, "hm:r:v")) != EOF) { | ||
263 | switch (c) { | ||
264 | case 'm': | ||
265 | msglen = atoi(optarg); | ||
266 | if (msglen < 0) | ||
267 | goto usage; | ||
268 | continue; | ||
269 | case 'r': | ||
270 | readcount = atoi(optarg); | ||
271 | if (readcount < 0) | ||
272 | goto usage; | ||
273 | continue; | ||
274 | case 'v': | ||
275 | verbose++; | ||
276 | continue; | ||
277 | case 'h': | ||
278 | case '?': | ||
279 | usage: | ||
280 | fprintf(stderr, | ||
281 | "usage: %s [-h] [-m N] [-r N] /dev/spidevB.D\n", | ||
282 | argv[0]); | ||
283 | return 1; | ||
284 | } | ||
285 | } | ||
286 | |||
287 | if ((optind + 1) != argc) | ||
288 | goto usage; | ||
289 | name = argv[optind]; | ||
290 | |||
291 | fd = open(name, O_RDWR); | ||
292 | if (fd < 0) { | ||
293 | perror("open"); | ||
294 | return 1; | ||
295 | } | ||
296 | |||
297 | dumpstat(name, fd); | ||
298 | |||
299 | if (msglen) | ||
300 | do_msg(fd, msglen); | ||
301 | |||
302 | if (readcount) | ||
303 | do_read(fd, readcount); | ||
304 | |||
305 | close(fd); | ||
306 | return 0; | ||
307 | } | ||