]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control.git/blob - pmsm-control/test_sw/rp_spi.c
Correct typo in rpi-mc-1 mapping to Ti AM437x based RICO board.
[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 #include <string.h>
15
16 #include "rp_spi.h" /*struct rpi_in */
17 #include "pmsm_state.h"
18
19 //#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
20 #define ARRAY_SIZE 16
21 static void pabort(const char *s)
22 {
23         perror(s);
24         abort();
25 }
26
27 static const char *device = "/dev/spidev0.1";
28 static uint8_t mode = 0;
29 static uint8_t bits = 8;
30 static uint32_t speed = 500000;
31 static uint16_t delay = 0;
32 static int fd;
33
34 /**
35  * \brief Testovaci funkce. Odesle same nuly. Delka 64bit. Vysledek vypise.
36  */
37 void transfer()
38 {
39         int ret;
40         uint8_t tx[]={0,0,0,0,0,0,0,0} ;/*= {
41                 0x00, 0x00, 0x01, 0x23, 0x45, 0x67,
42                 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23,
43                 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
44                 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB,
45                 0xCD, 0xEF
46         };/**/
47         printf("ARRAY_SIZE=%d\n",ARRAY_SIZE);
48         uint8_t rx[ARRAY_SIZE] = {0, }; /* inicializace vsech prvku na nulu? */
49         struct spi_ioc_transfer tr = {
50                 .tx_buf = (unsigned long)tx,
51                 .rx_buf = (unsigned long)rx,
52                 .len = ARRAY_SIZE,
53                 .delay_usecs = delay,
54                 .speed_hz = speed,
55                 .bits_per_word = bits,
56         };
57
58         ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
59         if (ret < 1)
60                 pabort("can't send spi message");
61
62         for (ret = 0; ret < ARRAY_SIZE; ret++) {
63                 if (!(ret % 6))
64                         puts("");
65                 printf("%.2X ", rx[ret]);
66         }
67         puts("");
68 }
69 void spi_read(struct rpi_state* this)
70 {
71         uint8_t *uint8_p;
72         uint16_t tmp;
73         int sign;
74         struct rpi_in in;
75         int ret;
76
77         uint8_t rx[ARRAY_SIZE] = {0, }; /* inicializace vsech prvku na nulu */
78         uint8_t tx[ARRAY_SIZE] = {0, }; /* inicializace vsech prvku na nulu */
79         struct spi_ioc_transfer tr = {
80                 .tx_buf = (unsigned long)tx,
81                 .rx_buf = (unsigned long)rx,
82                 .len = ARRAY_SIZE,
83                 .delay_usecs = delay,
84                 .speed_hz = speed,
85                 .bits_per_word = bits,
86         };
87
88         memset(&in,0,sizeof(in));
89
90         /*Data format:
91          * tx[4] - bity 95 downto 88 - bits that are sent first
92          * tx[5] - bity 87 downto 80
93          * tx[6] - bity 79 downto 72
94          * tx[7] - bity 71 downto 64
95          * tx[8] - bity 63 downto 56
96          * tx[9] - bity 55 downto 48
97          * tx[10] - bity 47 downto 40
98          * tx[11] - bity 39 downto 32
99          * tx[12] - bity 31 downto 24
100          * tx[13] - bity 23 downto 16
101          * tx[14] - bity 15 downto 8
102          * tx[15] - bity 7 downto 0
103          *
104          * bit 95 - ADC reset
105          * bit 94 - enable PWM1
106          * bit 93 - enable PWM2
107          * bit 92 - enable PWM3
108          * bit 91 - shutdown1
109          * bit 90 - shutdown2
110          * bit 89 - shutdown3
111          *      .
112          *      .
113          *      Unused
114          *      .
115          *      .
116          * bits 47 .. 32 - match PWM1
117          * bits 31 .. 16 - match PWM2
118          * bits 15 .. 0  - match PWM3
119          */
120
121
122
123         /* keep the 11-bit cap*/
124
125         if (this->pwm1>2047) this->pwm1=2047;
126         if (this->pwm2>2047) this->pwm2=2047;
127         if (this->pwm3>2047) this->pwm3=2047;
128
129         tx[0]=this->test; /*bit 94 - enable PWM1*/
130
131         /*now we have to switch the bytes due to endianess */
132         /* ARMv6 & ARMv7 instructions are little endian */
133         /*pwm1*/
134         tx[10]=((uint8_t*)&this->pwm1)[1]; /*MSB*/
135         tx[11]=((uint8_t*)&this->pwm1)[0]; /*LSB*/
136
137         /*pwm2*/
138         tx[12]=((uint8_t*)&this->pwm2)[1]; /*MSB*/
139         tx[13]=((uint8_t*)&this->pwm2)[0]; /*LSB*/
140
141         /*pwm3*/
142         tx[14]=((uint8_t*)&this->pwm3)[1]; /*MSB*/
143         tx[15]=((uint8_t*)&this->pwm3)[0]; /*LSB*/
144
145         /*----------------------------------------*/
146
147         ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
148
149
150         if (ret < 1){
151                 pabort("can't send spi message");
152         }
153
154         /*-----------------------------------------*/
155
156
157         for (ret = 0; ret < ARRAY_SIZE; ret++) {
158                 this->spi_dat->debug_rx[ret]=rx[ret];
159         }
160
161         /*/
162
163         /*prichozi data:
164          * rx[0] - bity 127 downto 120 the first income bit..127
165          * rx[1] - bity 119 downto 112
166          * rx[2] - bity 111 downto 104
167          * rx[3] - bity 103 downto 96
168          * rx[4] - bity 95 downto 88
169          * rx[5] - bity 87 downto 80
170          * rx[6] - bity 79 downto 72
171          * rx[7] - bity 71 downto 64
172          * rx[8] - bity 63 downto 56
173          * rx[9] - bity 55 downto 48
174          * rx[10] - bity 47 downto 40
175          * rx[11] - bity 39 downto 32
176          * rx[12] - bity 31 downto 24
177          * rx[13] - bity 23 downto 16
178          * rx[14] - bity 15 downto 8
179          * rx[15] - bity 7 downto 0     the last income bit..0
180          */
181
182         /*uprava endianity pozice*/
183         uint8_p=(uint8_t*)&this->spi_dat->pozice_raw;
184         /* x86 je Little-Endian */
185         uint8_p[0]=rx[3]; /* LSB */
186         uint8_p[1]=rx[2];
187         uint8_p[2]=rx[1];
188         uint8_p[3]=rx[0]; /*MSB*/ /*with sign bit*/
189
190         /*halove sondy
191          * hal1 - bit95
192          * hal2 - bit94
193          * hal3 - bit93
194          */
195         this->spi_dat->hal1=!!(0x80 & rx[4]);
196         this->spi_dat->hal2=!!(0x40 & rx[4]);
197         this->spi_dat->hal3=!!(0x20 & rx[4]);
198
199         /* index position
200          * bits 92 downto 81
201          *      92..88 in rx[4] last 5 bits (from left)
202          *      87..81 in rx[5] first 7 bits (from left)
203          */
204         this->spi_dat->index_position=0x1F & rx[4];
205         this->spi_dat->index_position<<=8;
206         this->spi_dat->index_position|=0xFE & rx[5];
207         this->spi_dat->index_position>>=1;
208
209         /* current measurments count
210          * bits 80 downto 72
211          * bit 80 in rx[5]
212          * bits 79..72 in rx[6]
213          */
214
215         this->spi_dat->adc_m_count=0x01 & rx[5];
216         this->spi_dat->adc_m_count<<=8;
217         this->spi_dat->adc_m_count|=rx[6];
218
219
220         /** currents
221          * ch2 - bits 71 downto 48
222          *      71..64 in rx[7] - all byte
223          *      63..56 in rx[8] - all byte
224          *      55..48 in rx[9] - all byte
225          * ch0 - bits 47 downto 24
226          *      47..40 in rx[10] - all byte
227          *      39..32 in rx[11] - all byte
228          *      31..24 in rx[12] - all byte
229          * ch1 - bits 23 downto 0
230          *      23..16 in rx[13] - all byte
231          *      15..8 in rx[14] - all byte
232          *      7..0 in rx[15] - all byte
233          */
234
235         this->spi_dat->ch2=rx[7];
236         this->spi_dat->ch2<<=8;
237         this->spi_dat->ch2|=rx[8];
238         this->spi_dat->ch2<<=8;
239         this->spi_dat->ch2|=rx[9];
240
241         this->spi_dat->ch0=rx[10];
242         this->spi_dat->ch0<<=8;
243         this->spi_dat->ch0|=rx[11];
244         this->spi_dat->ch0<<=8;
245         this->spi_dat->ch0|=rx[12];
246
247         this->spi_dat->ch1=rx[13];
248         this->spi_dat->ch1<<=8;
249         this->spi_dat->ch1|=rx[14];
250         this->spi_dat->ch1<<=8;
251         this->spi_dat->ch1|=rx[15];
252
253
254
255 }
256
257 int spi_init()
258 {
259         int ret = 0;
260
261         fd = open(device, O_RDWR);
262         if (fd < 0)
263                 pabort("can't open device");
264         printf("device open\n");
265         /*
266          * spi mode
267          */
268         ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
269         if (ret == -1)
270                 pabort("can't set spi mode");
271
272         ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
273         if (ret == -1)
274                 pabort("can't get spi mode");
275
276         /*
277          * bits per word
278          */
279         ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
280         if (ret == -1)
281                 pabort("can't set bits per word");
282
283         ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
284         if (ret == -1)
285                 pabort("can't get bits per word");
286
287         /*
288          * max speed hz
289          */
290         ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
291         if (ret == -1)
292                 pabort("can't set max speed hz");
293
294         ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
295         if (ret == -1)
296                 pabort("can't get max speed hz");
297
298         printf("spi mode: %d\n", mode);
299         printf("bits per word: %d\n", bits);
300         printf("delay: %d\n", delay);
301         printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
302
303
304         return ret;
305 }
306
307 void spi_disable(){
308         close(fd);
309
310 }