11 #include <sys/ioctl.h>
12 #include <linux/types.h>
13 #include <linux/spi/spidev.h>
15 #include "rp_spi.h" /*struct rpi_in */
17 //#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
19 static void pabort(const char *s)
25 static const char *device = "/dev/spidev0.1";
26 static uint8_t mode = 0;
27 static uint8_t bits = 8;
28 static uint32_t speed = 500000;
29 static uint16_t delay = 0;
33 * \brief Testovaci funkce. Odesle same nuly. Delka 64bit. Vysledek vypise.
38 uint8_t tx[]={0,0,0,0,0,0,0,0} ;/*= {
39 0x00, 0x00, 0x01, 0x23, 0x45, 0x67,
40 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23,
41 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
42 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB,
45 printf("ARRAY_SIZE=%d\n",ARRAY_SIZE);
46 uint8_t rx[ARRAY_SIZE] = {0, }; /* inicializace vsech prvku na nulu? */
47 struct spi_ioc_transfer tr = {
48 .tx_buf = (unsigned long)tx,
49 .rx_buf = (unsigned long)rx,
53 .bits_per_word = bits,
56 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
58 pabort("can't send spi message");
60 for (ret = 0; ret < ARRAY_SIZE; ret++) {
63 printf("%.2X ", rx[ret]);
67 struct rpi_in spi_read(uint8_t * tx)
75 uint8_t rx[ARRAY_SIZE] = {0, }; /* inicializace vsech prvku na nulu? */
76 struct spi_ioc_transfer tr = {
77 .tx_buf = (unsigned long)tx,
78 .rx_buf = (unsigned long)rx,
82 .bits_per_word = bits,
85 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
86 /*vypisovani prichozich dat */
89 pabort("can't send spi message");
91 for (ret = 0; ret < ARRAY_SIZE; ret++) {
94 printf("%.2X ", rx[ret]);
100 * rx[0] - bity 95 downto 88 - bits, that come first
101 * rx[1] - bity 87 downto 80
102 * rx[2] - bity 79 downto 72
103 * rx[3] - bity 71 downto 64
104 * rx[4] - bity 63 downto 56
105 * rx[5] - bity 55 downto 48
106 * rx[6] - bity 47 downto 40
107 * rx[7] - bity 39 downto 32
108 * rx[8] - bity 31 downto 24
109 * rx[9] - bity 23 downto 16
110 * rx[10] - bity 15 downto 8
111 * rx[11] - bity 7 downto 0
114 /*uprava endianity pozice*/
115 uint8_p=(uint8_t*)&in.pozice;
116 /* x86 je Little-Endian */
117 uint8_p[0]=rx[3]; /* LSB */
120 uint8_p[3]=rx[0]; /*MSB*/ /*with sign bit*/
121 uint8_p[4]=uint8_p[5]=uint8_p[6]=uint8_p[7]=0;
128 in.hal1=!!(0x80 & rx[4]);
129 in.hal2=!!(0x40 & rx[4]);
130 in.hal3=!!(0x20 & rx[4]);
137 in.en1=!!(0x10 & rx[4]);
138 in.en2=!!(0x08 & rx[4]);
139 in.en3=!!(0x04 & rx[4]);
146 in.shdn1=!!(0x02 & rx[4]);
147 in.shdn2=!!(0x01 & rx[4]);
148 in.shdn3=!!(0x80 & rx[5]);
153 in.b54=!!(0x40 & rx[5]);
154 in.b53=!!(0x20 & rx[5]);
155 in.b52=!!(0x10 & rx[5]);
156 in.b51=!!(0x08 & rx[5]);
157 in.b50=!!(0x04 & rx[5]);
158 in.b49=!!(0x02 & rx[5]);
159 in.b48=!!(0x01 & rx[5]);
160 in.b47=!!(0x80 & rx[6]);
161 in.b46=!!(0x40 & rx[6]);
162 in.b45=!!(0x20 & rx[6]);
163 in.b44=!!(0x10 & rx[6]);
164 in.b43=!!(0x08 & rx[6]);
165 in.b42=!!(0x04 & rx[6]);
166 in.b41=!!(0x02 & rx[6]);
167 in.b40=!!(0x01 & rx[6]);
168 in.b39=!!(0x80 & rx[7]);
169 in.b38=!!(0x40 & rx[7]);
170 in.b37=!!(0x20 & rx[7]);
171 in.b36=!!(0x10 & rx[7]);
174 * ch0 - bits 35 downto 24
175 * 35..32 in rx[7] - last 4
176 * 31..24 in rx[8] - all bytte
177 * ch1 - bits 23 downto 12
178 * 23..16 in rx[9] - all byte
179 * 15..12 in rx[10] - first 4
180 * ch2 - bits 11 downto 0
181 * 11..8 in rx[10] - last 4
182 * 7..0 in rx[11] - all byte
191 in.ch1|=(rx[10] & 0xF0);
194 in.ch2=(0xF & rx[10]);
208 fd = open(device, O_RDWR);
210 pabort("can't open device");
211 printf("device open\n");
215 ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
217 pabort("can't set spi mode");
219 ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
221 pabort("can't get spi mode");
226 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
228 pabort("can't set bits per word");
230 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
232 pabort("can't get bits per word");
237 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
239 pabort("can't set max speed hz");
241 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
243 pabort("can't get max speed hz");
245 printf("spi mode: %d\n", mode);
246 printf("bits per word: %d\n", bits);
247 printf("delay: %d\n", delay);
248 printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);