]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control.git/blob - pmsm-control/test_sw/rp_spi.c
Attemp to solve bug. ADC channels association should be pwm1-ch0 pwm2-ch1 pwm3-ch2...
[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                 in.debug_rx[ret]=rx[ret];
93         }
94
95         /*/
96
97         /*prichozi data:
98          * rx[0] - bity 127 downto 120 the first income bit..127
99          * rx[1] - bity 119 downto 112
100          * rx[2] - bity 111 downto 104
101          * rx[3] - bity 103 downto 96
102          * rx[4] - bity 95 downto 88
103          * rx[5] - bity 87 downto 80
104          * rx[6] - bity 79 downto 72
105          * rx[7] - bity 71 downto 64
106          * rx[8] - bity 63 downto 56
107          * rx[9] - bity 55 downto 48
108          * rx[10] - bity 47 downto 40
109          * rx[11] - bity 39 downto 32
110          * rx[12] - bity 31 downto 24
111          * rx[13] - bity 23 downto 16
112          * rx[14] - bity 15 downto 8
113          * rx[15] - bity 7 downto 0     the last income bit..0
114          */
115
116         /*uprava endianity pozice*/
117         uint8_p=(uint8_t*)&in.pozice;
118         /* x86 je Little-Endian */
119         uint8_p[0]=rx[3]; /* LSB */
120         uint8_p[1]=rx[2];
121         uint8_p[2]=rx[1];
122         uint8_p[3]=rx[0]; /*MSB*/ /*with sign bit*/
123         uint8_p[4]=uint8_p[5]=uint8_p[6]=uint8_p[7]=0;
124
125         /*halove sondy
126          * hal1 - bit95
127          * hal2 - bit94
128          * hal3 - bit93
129          */
130         in.hal1=!!(0x80 & rx[4]);
131         in.hal2=!!(0x40 & rx[4]);
132         in.hal3=!!(0x20 & rx[4]);
133
134         /*pwm enable
135          * en1 - bit92
136          * en2 - bit91
137          * en2 - bit90
138          */
139         in.en1=!!(0x10 & rx[4]);
140         in.en2=!!(0x08 & rx[4]);
141         in.en3=!!(0x04 & rx[4]);
142
143         /*shutdown
144          * shdn1 - bit89
145          * shdn2 - bit88
146          * shdn3 - bit87
147          */
148         in.shdn1=!!(0x02 & rx[4]);
149         in.shdn2=!!(0x01 & rx[4]);
150         in.shdn3=!!(0x80 & rx[5]);
151
152         /* current measurments count
153          * bits 80 downto 72
154          * bit 80 in rx[5]
155          * bits 79..72 in rx[6]
156          */
157
158         in.adc_m_count=0x01 & rx[5];
159         in.adc_m_count<<=8;
160         in.adc_m_count|=rx[6];
161
162
163         /** currents
164          * ch2 - bits 71 downto 48
165          *      71..64 in rx[7] - all byte
166          *      63..56 in rx[8] - all byte
167          *      55..48 in rx[9] - all byte
168          * ch0 - bits 47 downto 24
169          *      47..40 in rx[10] - all byte
170          *      39..32 in rx[11] - all byte
171          *      31..24 in rx[12] - all byte
172          * ch1 - bits 23 downto 0
173          *      23..16 in rx[13] - all byte
174          *      15..8 in rx[14] - all byte
175          *      7..0 in rx[15] - all byte
176          */
177
178         in.ch2=rx[7];
179         in.ch2<<=8;
180         in.ch2|=rx[8];
181         in.ch2<<=8;
182         in.ch2|=rx[9];
183
184         in.ch0=rx[10];
185         in.ch0<<=8;
186         in.ch0|=rx[11];
187         in.ch0<<=8;
188         in.ch0|=rx[12];
189
190         in.ch1=rx[13];
191         in.ch1<<=8;
192         in.ch1|=rx[14];
193         in.ch1<<=8;
194         in.ch1|=rx[15];
195
196
197         return in;
198
199
200 }
201
202 int spi_init()
203 {
204         int ret = 0;
205
206         fd = open(device, O_RDWR);
207         if (fd < 0)
208                 pabort("can't open device");
209         printf("device open\n");
210         /*
211          * spi mode
212          */
213         ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
214         if (ret == -1)
215                 pabort("can't set spi mode");
216
217         ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
218         if (ret == -1)
219                 pabort("can't get spi mode");
220
221         /*
222          * bits per word
223          */
224         ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
225         if (ret == -1)
226                 pabort("can't set bits per word");
227
228         ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
229         if (ret == -1)
230                 pabort("can't get bits per word");
231
232         /*
233          * max speed hz
234          */
235         ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
236         if (ret == -1)
237                 pabort("can't set max speed hz");
238
239         ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
240         if (ret == -1)
241                 pabort("can't get max speed hz");
242
243         printf("spi mode: %d\n", mode);
244         printf("bits per word: %d\n", bits);
245         printf("delay: %d\n", delay);
246         printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
247
248
249         return ret;
250 }
251
252 void spi_disable(){
253         close(fd);
254
255 }