11 #include <sys/ioctl.h>
12 #include <linux/types.h>
13 #include <linux/spi/spidev.h>
16 #include "rp_spi.h" /*struct rpi_in */
17 #include "pmsm_state.h"
19 //#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
21 static void pabort(const char *s)
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;
35 * \brief Testovaci funkce. Odesle same nuly. Delka 64bit. Vysledek vypise.
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,
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,
55 .bits_per_word = bits,
58 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
60 pabort("can't send spi message");
62 for (ret = 0; ret < ARRAY_SIZE; ret++) {
65 printf("%.2X ", rx[ret]);
69 void spi_read(struct rpi_state* this)
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,
85 .bits_per_word = bits,
88 memset(&in,0,sizeof(in));
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
105 * bit 94 - enable PWM1
106 * bit 93 - enable PWM2
107 * bit 92 - enable PWM3
116 * bits 47 .. 32 - match PWM1
117 * bits 31 .. 16 - match PWM2
118 * bits 15 .. 0 - match PWM3
123 /* keep the 11-bit cap*/
125 if (this->pwm1>2047) this->pwm1=2047;
126 if (this->pwm2>2047) this->pwm2=2047;
127 if (this->pwm3>2047) this->pwm3=2047;
129 tx[0]=this->test; /*bit 94 - enable PWM1*/
131 /*now we have to switch the bytes due to endianess */
132 /* ARMv6 & ARMv7 instructions are little endian */
134 tx[10]=((uint8_t*)&this->pwm1)[1]; /*MSB*/
135 tx[11]=((uint8_t*)&this->pwm1)[0]; /*LSB*/
138 tx[12]=((uint8_t*)&this->pwm2)[1]; /*MSB*/
139 tx[13]=((uint8_t*)&this->pwm2)[0]; /*LSB*/
142 tx[14]=((uint8_t*)&this->pwm3)[1]; /*MSB*/
143 tx[15]=((uint8_t*)&this->pwm3)[0]; /*LSB*/
145 /*----------------------------------------*/
147 ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
151 pabort("can't send spi message");
154 /*-----------------------------------------*/
157 for (ret = 0; ret < ARRAY_SIZE; ret++) {
158 this->spi_dat->debug_rx[ret]=rx[ret];
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
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 */
188 uint8_p[3]=rx[0]; /*MSB*/ /*with sign bit*/
195 this->spi_dat->hal1=!!(0x80 & rx[4]);
196 this->spi_dat->hal2=!!(0x40 & rx[4]);
197 this->spi_dat->hal3=!!(0x20 & rx[4]);
201 * 92..88 in rx[4] last 5 bits (from left)
202 * 87..81 in rx[5] first 7 bits (from left)
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;
209 /* current measurments count
212 * bits 79..72 in rx[6]
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];
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
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];
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];
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];
261 fd = open(device, O_RDWR);
263 pabort("can't open device");
264 printf("device open\n");
268 ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
270 pabort("can't set spi mode");
272 ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
274 pabort("can't get spi mode");
279 ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
281 pabort("can't set bits per word");
283 ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
285 pabort("can't get bits per word");
290 ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
292 pabort("can't set max speed hz");
294 ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
296 pabort("can't get max speed hz");
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);