]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control.git/blob - pmsm-control/test_sw/rp_spi.c
57a212265ec8713fe12ebecbafb3719ee98d7760
[fpga/rpi-motor-control.git] / pmsm-control / test_sw / rp_spi.c
1 /*
2  *
3  */
4
5 #include <stdint.h>
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <getopt.h>
10 #include <fcntl.h>
11 #include <sys/ioctl.h>
12 #include <linux/types.h>
13 #include <linux/spi/spidev.h>
14
15 #include "rp_spi.h" /*struct rpi_in */
16
17 //#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
18 #define ARRAY_SIZE 16
19 static void pabort(const char *s)
20 {
21         perror(s);
22         abort();
23 }
24
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;
30 static int fd;
31
32 /**
33  * \brief Testovaci funkce. Odesle same nuly. Delka 64bit. Vysledek vypise.
34  */
35 void transfer()
36 {
37         int ret;
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,
43                 0xCD, 0xEF
44         };/**/
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,
50                 .len = ARRAY_SIZE,
51                 .delay_usecs = delay,
52                 .speed_hz = speed,
53                 .bits_per_word = bits,
54         };
55
56         ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
57         if (ret < 1)
58                 pabort("can't send spi message");
59
60         for (ret = 0; ret < ARRAY_SIZE; ret++) {
61                 if (!(ret % 6))
62                         puts("");
63                 printf("%.2X ", rx[ret]);
64         }
65         puts("");
66 }
67 struct rpi_in spi_read(uint8_t * tx)
68 {
69         uint8_t *uint8_p;
70         uint16_t tmp;
71         int sign;
72         struct rpi_in in;
73         int ret;
74
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,
79                 .len = ARRAY_SIZE,
80                 .delay_usecs = delay,
81                 .speed_hz = speed,
82                 .bits_per_word = bits,
83         };
84
85         ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
86         /*vypisovani prichozich dat */
87         /*/
88         if (ret < 1)
89                 pabort("can't send spi message");
90
91         for (ret = 0; ret < ARRAY_SIZE; ret++) {
92                 if (!(ret % 6))
93                         puts("");
94                 printf("%.2X ", rx[ret]);
95         }
96         puts("");
97         /*/
98
99         /*prichozi data:
100          * rx[0] - bity 127 downto 120
101          * rx[1] - bity 119 downto 112
102          * rx[2] - bity 111 downto 104
103          * rx[3] - bity 103 downto 96
104          * rx[4] - bity 95 downto 88
105          * rx[5] - bity 87 downto 80
106          * rx[6] - bity 79 downto 72
107          * rx[7] - bity 71 downto 64
108          * rx[8] - bity 63 downto 56
109          * rx[9] - bity 55 downto 48
110          * rx[10] - bity 47 downto 40
111          * rx[11] - bity 39 downto 32
112          * rx[12] - bity 31 downto 24
113          * rx[13] - bity 23 downto 16
114          * rx[14] - bity 15 downto 8
115          * rx[15] - bity 7 downto 0
116          */
117
118         /*uprava endianity pozice*/
119         uint8_p=(uint8_t*)&in.pozice;
120         /* x86 je Little-Endian */
121         uint8_p[0]=rx[3]; /* LSB */
122         uint8_p[1]=rx[2];
123         uint8_p[2]=rx[1];
124         uint8_p[3]=rx[0]; /*MSB*/ /*with sign bit*/
125         uint8_p[4]=uint8_p[5]=uint8_p[6]=uint8_p[7]=0;
126
127         /*halove sondy
128          * hal1 - bit95
129          * hal2 - bit94
130          * hal3 - bit93
131          */
132         in.hal1=!!(0x80 & rx[4]);
133         in.hal2=!!(0x40 & rx[4]);
134         in.hal3=!!(0x20 & rx[4]);
135
136         /*pwm enable
137          * en1 - bit92
138          * en2 - bit91
139          * en2 - bit90
140          */
141         in.en1=!!(0x10 & rx[4]);
142         in.en2=!!(0x08 & rx[4]);
143         in.en3=!!(0x04 & rx[4]);
144
145         /*shutdown
146          * shdn1 - bit89
147          * shdn2 - bit88
148          * shdn3 - bit87
149          */
150         in.shdn1=!!(0x02 & rx[4]);
151         in.shdn2=!!(0x01 & rx[4]);
152         in.shdn3=!!(0x80 & rx[5]);
153
154         /*debug bits
155          *
156          */
157         in.b54=!!(0x40 & rx[5]);
158         in.b53=!!(0x20 & rx[5]);
159         in.b52=!!(0x10 & rx[5]);
160         in.b51=!!(0x08 & rx[5]);
161         in.b50=!!(0x04 & rx[5]);
162         in.b49=!!(0x02 & rx[5]);
163         in.b48=!!(0x01 & rx[5]);
164         in.b47=!!(0x80 & rx[6]);
165         in.b46=!!(0x40 & rx[6]);
166         in.b45=!!(0x20 & rx[6]);
167         in.b44=!!(0x10 & rx[6]);
168         in.b43=!!(0x08 & rx[6]);
169         in.b42=!!(0x04 & rx[6]);
170         in.b41=!!(0x02 & rx[6]);
171         in.b40=!!(0x01 & rx[6]);
172         in.b39=!!(0x80 & rx[7]);
173         in.b38=!!(0x40 & rx[7]);
174         in.b37=!!(0x20 & rx[7]);
175         in.b36=!!(0x10 & rx[7]);
176
177
178         in.ch0=0x0F & rx[7];
179         in.ch0<<=8;
180         in.ch0|=rx[8];
181
182         in.ch1= rx[9];
183         in.ch1<<=8;
184         in.ch1|=(rx[10] & 0xF0);
185         in.ch1>>=4;
186
187         in.ch2=(0xF & rx[10]);
188         in.ch2<<=8;
189         in.ch2|=rx[11];
190
191
192         return in;
193
194
195 }
196
197 int spi_init()
198 {
199         int ret = 0;
200
201         fd = open(device, O_RDWR);
202         if (fd < 0)
203                 pabort("can't open device");
204         printf("device open\n");
205         /*
206          * spi mode
207          */
208         ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
209         if (ret == -1)
210                 pabort("can't set spi mode");
211
212         ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
213         if (ret == -1)
214                 pabort("can't get spi mode");
215
216         /*
217          * bits per word
218          */
219         ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
220         if (ret == -1)
221                 pabort("can't set bits per word");
222
223         ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
224         if (ret == -1)
225                 pabort("can't get bits per word");
226
227         /*
228          * max speed hz
229          */
230         ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
231         if (ret == -1)
232                 pabort("can't set max speed hz");
233
234         ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
235         if (ret == -1)
236                 pabort("can't get max speed hz");
237
238         printf("spi mode: %d\n", mode);
239         printf("bits per word: %d\n", bits);
240         printf("delay: %d\n", delay);
241         printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
242
243
244         return ret;
245 }
246
247 void spi_disable(){
248         close(fd);
249
250 }