]>
Commit | Line | Data |
---|---|---|
ef0d77af | 1 | /* |
2 | * SPI testing utility (using spidev driver) | |
3 | * | |
4 | * Copyright (c) 2007 MontaVista Software, Inc. | |
5 | * Copyright (c) 2007 Anton Vorontsov <[email protected]> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License. | |
10 | * | |
11 | * Cross-compile with cross-gcc -I/path/to/cross-kernel/include | |
12 | */ | |
13 | ||
4b5f5fcb | 14 | #include "spi.h" |
ef0d77af | 15 | #include <getopt.h> |
16 | #include <fcntl.h> | |
17 | #include <sys/ioctl.h> | |
18 | #include <linux/types.h> | |
19 | #include <linux/spi/spidev.h> | |
0b983baf | 20 | static spiMode _mode; |
ef0d77af | 21 | |
4b5f5fcb | 22 | static int fd; |
a6a035a7 | 23 | |
558dac45 | 24 | extern int transfer(uint8_t tx[], uint8_t rx[], unsigned int size) |
ef0d77af | 25 | { |
26 | int ret; | |
ef0d77af | 27 | struct spi_ioc_transfer tr = { |
28 | .tx_buf = (unsigned long)tx, | |
29 | .rx_buf = (unsigned long)rx, | |
0b983baf | 30 | .len = size, |
558dac45 | 31 | .speed_hz = _mode.speed, |
4b5f5fcb | 32 | .delay_usecs = _mode.delay, |
558dac45 | 33 | .bits_per_word = _mode.bits}; |
ef0d77af | 34 | |
35 | ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); | |
36 | if (ret < 1) | |
d41e803b | 37 | printf("can't send spi message\n"); |
558dac45 | 38 | return ret; |
ef0d77af | 39 | } |
558dac45 | 40 | extern int spi_init(spiMode *modes, const char *device) |
4b5f5fcb | 41 | { |
0b983baf | 42 | _mode = *modes; |
4b5f5fcb | 43 | int ret; |
0b983baf | 44 | fd = open(device, O_RDWR); |
558dac45 | 45 | if (fd < 0) |
46 | { | |
47 | printf("could not open %s\n", device); | |
0b983baf | 48 | return fd; |
49 | } | |
50 | ret = ioctl(fd, SPI_IOC_WR_MODE, &(modes->mode)); | |
558dac45 | 51 | if (ret == -1) |
52 | { | |
0b983baf | 53 | printf("can't set spi mode\n"); |
54 | return ret; | |
ef0d77af | 55 | } |
0b983baf | 56 | ret = ioctl(fd, SPI_IOC_RD_MODE, &(modes->mode)); |
558dac45 | 57 | if (ret == -1) |
58 | { | |
0b983baf | 59 | printf("can't get spi mode\n"); |
60 | return ret; | |
61 | } | |
62 | ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &(modes->bits)); | |
558dac45 | 63 | if (ret == -1) |
64 | { | |
0b983baf | 65 | printf("can't set bits per word\n"); |
66 | return ret; | |
67 | } | |
68 | ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &(modes->bits)); | |
558dac45 | 69 | if (ret == -1) |
70 | { | |
0b983baf | 71 | printf("can't get bits per word\n"); |
72 | return ret; | |
73 | } | |
74 | ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &(modes->speed)); | |
558dac45 | 75 | if (ret == -1) |
76 | { | |
0b983baf | 77 | printf("can't set max speed hz\n"); |
78 | return ret; | |
79 | } | |
80 | ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &(modes->speed)); | |
558dac45 | 81 | if (ret == -1) |
82 | { | |
0b983baf | 83 | printf("can't get max speed hz\n"); |
84 | return ret; | |
85 | } | |
86 | printf("spi mode: %d\n", modes->mode); | |
87 | printf("bits per word: %d\n", modes->bits); | |
558dac45 | 88 | printf("max speed: %d Hz (%d KHz)\n", modes->speed, modes->speed / 1000); |
4b5f5fcb | 89 | return fd; |
0b983baf | 90 | } |
4b5f5fcb | 91 | extern void spi_deinit() |
ef0d77af | 92 | { |
558dac45 | 93 | close(fd); |
ef0d77af | 94 | } |