]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control.git/blob - pmsm-control/test_sw/rp_spi.c
3ae1c6340ab3a88795dc7191120b7088e0a23ab9
[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
18 //#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
19 #define ARRAY_SIZE 16
20 static void pabort(const char *s)
21 {
22         perror(s);
23         abort();
24 }
25
26 static const char *device = "/dev/spidev0.1";
27 static uint8_t mode = 0;
28 static uint8_t bits = 8;
29 static uint32_t speed = 500000;
30 static uint16_t delay = 0;
31 static int fd;
32
33 /**
34  * \brief Testovaci funkce. Odesle same nuly. Delka 64bit. Vysledek vypise.
35  */
36 void transfer()
37 {
38         int ret;
39         uint8_t tx[]={0,0,0,0,0,0,0,0} ;/*= {
40                 0x00, 0x00, 0x01, 0x23, 0x45, 0x67,
41                 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23,
42                 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
43                 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB,
44                 0xCD, 0xEF
45         };/**/
46         printf("ARRAY_SIZE=%d\n",ARRAY_SIZE);
47         uint8_t rx[ARRAY_SIZE] = {0, }; /* inicializace vsech prvku na nulu? */
48         struct spi_ioc_transfer tr = {
49                 .tx_buf = (unsigned long)tx,
50                 .rx_buf = (unsigned long)rx,
51                 .len = ARRAY_SIZE,
52                 .delay_usecs = delay,
53                 .speed_hz = speed,
54                 .bits_per_word = bits,
55         };
56
57         ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
58         if (ret < 1)
59                 pabort("can't send spi message");
60
61         for (ret = 0; ret < ARRAY_SIZE; ret++) {
62                 if (!(ret % 6))
63                         puts("");
64                 printf("%.2X ", rx[ret]);
65         }
66         puts("");
67 }
68 struct rpi_in spi_read(uint8_t * tx)
69 {
70         uint8_t *uint8_p;
71         uint16_t tmp;
72         int sign;
73         struct rpi_in in;
74         int ret;
75
76         uint8_t rx[ARRAY_SIZE] = {0, }; /* inicializace vsech prvku na nulu? */
77         struct spi_ioc_transfer tr = {
78                 .tx_buf = (unsigned long)tx,
79                 .rx_buf = (unsigned long)rx,
80                 .len = ARRAY_SIZE,
81                 .delay_usecs = delay,
82                 .speed_hz = speed,
83                 .bits_per_word = bits,
84         };
85
86         memset(&in,0,sizeof(in));
87
88         ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
89         /*vypisovani prichozich dat */
90
91         if (ret < 1)
92                 pabort("can't send spi message");
93
94         for (ret = 0; ret < ARRAY_SIZE; ret++) {
95                 in.debug_rx[ret]=rx[ret];
96         }
97
98         /*/
99
100         /*prichozi data:
101          * rx[0] - bity 127 downto 120 the first income bit..127
102          * rx[1] - bity 119 downto 112
103          * rx[2] - bity 111 downto 104
104          * rx[3] - bity 103 downto 96
105          * rx[4] - bity 95 downto 88
106          * rx[5] - bity 87 downto 80
107          * rx[6] - bity 79 downto 72
108          * rx[7] - bity 71 downto 64
109          * rx[8] - bity 63 downto 56
110          * rx[9] - bity 55 downto 48
111          * rx[10] - bity 47 downto 40
112          * rx[11] - bity 39 downto 32
113          * rx[12] - bity 31 downto 24
114          * rx[13] - bity 23 downto 16
115          * rx[14] - bity 15 downto 8
116          * rx[15] - bity 7 downto 0     the last income bit..0
117          */
118
119         /*uprava endianity pozice*/
120         uint8_p=(uint8_t*)&in.pozice;
121         /* x86 je Little-Endian */
122         uint8_p[0]=rx[3]; /* LSB */
123         uint8_p[1]=rx[2];
124         uint8_p[2]=rx[1];
125         uint8_p[3]=rx[0]; /*MSB*/ /*with sign bit*/
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         /* index position
137          * bits 92 downto 81
138          *      92..88 in rx[4] last 5 bits (from left)
139          *      87..81 in rx[5] first 7 bits (from left)
140          */
141         in.index_position=0x1F & rx[4];
142         in.index_position<<=8;
143         in.index_position|=0xFE & rx[5];
144         in.index_position>>=1;
145
146         /* current measurments count
147          * bits 80 downto 72
148          * bit 80 in rx[5]
149          * bits 79..72 in rx[6]
150          */
151
152         in.adc_m_count=0x01 & rx[5];
153         in.adc_m_count<<=8;
154         in.adc_m_count|=rx[6];
155
156
157         /** currents
158          * ch2 - bits 71 downto 48
159          *      71..64 in rx[7] - all byte
160          *      63..56 in rx[8] - all byte
161          *      55..48 in rx[9] - all byte
162          * ch0 - bits 47 downto 24
163          *      47..40 in rx[10] - all byte
164          *      39..32 in rx[11] - all byte
165          *      31..24 in rx[12] - all byte
166          * ch1 - bits 23 downto 0
167          *      23..16 in rx[13] - all byte
168          *      15..8 in rx[14] - all byte
169          *      7..0 in rx[15] - all byte
170          */
171
172         in.ch2=rx[7];
173         in.ch2<<=8;
174         in.ch2|=rx[8];
175         in.ch2<<=8;
176         in.ch2|=rx[9];
177
178         in.ch0=rx[10];
179         in.ch0<<=8;
180         in.ch0|=rx[11];
181         in.ch0<<=8;
182         in.ch0|=rx[12];
183
184         in.ch1=rx[13];
185         in.ch1<<=8;
186         in.ch1|=rx[14];
187         in.ch1<<=8;
188         in.ch1|=rx[15];
189
190
191         return in;
192
193
194 }
195
196 int spi_init()
197 {
198         int ret = 0;
199
200         fd = open(device, O_RDWR);
201         if (fd < 0)
202                 pabort("can't open device");
203         printf("device open\n");
204         /*
205          * spi mode
206          */
207         ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
208         if (ret == -1)
209                 pabort("can't set spi mode");
210
211         ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
212         if (ret == -1)
213                 pabort("can't get spi mode");
214
215         /*
216          * bits per word
217          */
218         ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
219         if (ret == -1)
220                 pabort("can't set bits per word");
221
222         ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
223         if (ret == -1)
224                 pabort("can't get bits per word");
225
226         /*
227          * max speed hz
228          */
229         ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
230         if (ret == -1)
231                 pabort("can't set max speed hz");
232
233         ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
234         if (ret == -1)
235                 pabort("can't get max speed hz");
236
237         printf("spi mode: %d\n", mode);
238         printf("bits per word: %d\n", bits);
239         printf("delay: %d\n", delay);
240         printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
241
242
243         return ret;
244 }
245
246 void spi_disable(){
247         close(fd);
248
249 }