]>
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; |
23 | extern int transfer(uint8_t tx[], uint8_t rx[],unsigned int size) | |
ef0d77af | 24 | { |
25 | int ret; | |
ef0d77af | 26 | struct spi_ioc_transfer tr = { |
27 | .tx_buf = (unsigned long)tx, | |
28 | .rx_buf = (unsigned long)rx, | |
0b983baf | 29 | .len = size, |
0b983baf | 30 | .speed_hz = _mode.speed, |
4b5f5fcb | 31 | .delay_usecs = _mode.delay, |
32 | .bits_per_word = _mode.bits | |
ef0d77af | 33 | }; |
34 | ||
35 | ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); | |
36 | if (ret < 1) | |
0b983baf | 37 | printf("can't send spi message"); |
38 | return ret; | |
ef0d77af | 39 | } |
4b5f5fcb | 40 | extern int spi_init(spiMode* modes,const char *device ) |
41 | { | |
0b983baf | 42 | _mode = *modes; |
4b5f5fcb | 43 | int ret; |
0b983baf | 44 | fd = open(device, O_RDWR); |
45 | if (fd < 0) { | |
46 | printf("could not open %s\n",device); | |
47 | return fd; | |
48 | } | |
49 | ret = ioctl(fd, SPI_IOC_WR_MODE, &(modes->mode)); | |
50 | if (ret == -1) { | |
51 | printf("can't set spi mode\n"); | |
52 | return ret; | |
ef0d77af | 53 | } |
0b983baf | 54 | ret = ioctl(fd, SPI_IOC_RD_MODE, &(modes->mode)); |
55 | if (ret == -1) { | |
56 | printf("can't get spi mode\n"); | |
57 | return ret; | |
58 | } | |
59 | ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &(modes->bits)); | |
60 | if (ret == -1) { | |
61 | printf("can't set bits per word\n"); | |
62 | return ret; | |
63 | } | |
64 | ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &(modes->bits)); | |
65 | if (ret == -1) { | |
66 | printf("can't get bits per word\n"); | |
67 | return ret; | |
68 | } | |
69 | ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &(modes->speed)); | |
70 | if (ret == -1) { | |
71 | printf("can't set max speed hz\n"); | |
72 | return ret; | |
73 | } | |
74 | ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &(modes->speed)); | |
75 | if (ret == -1) { | |
76 | printf("can't get max speed hz\n"); | |
77 | return ret; | |
78 | } | |
79 | printf("spi mode: %d\n", modes->mode); | |
80 | printf("bits per word: %d\n", modes->bits); | |
81 | printf("max speed: %d Hz (%d KHz)\n", modes->speed, modes->speed/1000); | |
4b5f5fcb | 82 | return fd; |
0b983baf | 83 | } |
4b5f5fcb | 84 | extern void spi_deinit() |
ef0d77af | 85 | { |
4b5f5fcb | 86 | close(fd); |
ef0d77af | 87 | } |