-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfpga.c
executable file
·468 lines (406 loc) · 10.5 KB
/
fpga.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include "datatype.h"
#include "fpga.h"
#include "control.h"
static const char *spidev = "/dev/spidev0.0";
static int spi_fd = -1;
static uint8 spi_tx[SPI_BUF_SIZE];
static uint8 spi_rx[SPI_BUF_SIZE];
struct spi_ioc_transfer spi_tr;
static int spi_setup(int fd)
{
int ret = -1;
unsigned int mode = SPI_MODE;
unsigned int bits = SPI_BITS_WIDTH_16;
unsigned int lsb = SPI_LSB;
unsigned int speed = SPI_SPEED;
ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1) {
print_err("can't set spi mode");
return SPI_SETUP_FAILED;
}
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1) {
print_err("can't set bits per word");
return SPI_SETUP_FAILED;
}
ret = ioctl(fd, SPI_IOC_WR_LSB_FIRST, &lsb);
if (ret == -1) {
print_err("can't set lsb first");
return SPI_SETUP_FAILED;
}
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1) {
print_err("can't set max speed hz");
return SPI_SETUP_FAILED;
}
return ret;
}
static int dumpstat(const char *name, int fd)
{
unsigned int mode, lsb, bits;
unsigned int speed;
if (ioctl(fd, SPI_IOC_RD_MODE, &mode) < 0) {
print_err("cannot get spi mode");
return SPI_DUMP_FAILED;
}
if (ioctl(fd, SPI_IOC_RD_LSB_FIRST, &lsb) < 0) {
print_err("cannot get lsb first");
return SPI_DUMP_FAILED;
}
if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits) < 0) {
print_err("cannot get bits per word");
return SPI_DUMP_FAILED;
}
if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) {
print_err("cannot get spi max speed");
return SPI_DUMP_FAILED;
}
printf("SPI mode: ........ 0x%02X\n", mode);
printf("Bits per word: ... %d\n", bits);
printf("LSB First: ....... %d\n", lsb);
printf("Max speed: ....... %d Hz \n", speed);
return 1;
}
int spi_open()
{
int ret=-1;
spi_fd = open(spidev, O_RDWR);
if (spi_fd < 0) {
print_err("open %s failed\n", spidev);
return SPI_OPEN_FAILED;
}
ret=spi_setup(spi_fd);
if(ret<0)
//spi setup failed
return ret;
else
ret=dumpstat(spidev,spi_fd);
if(ret<0)
//spi dump failed
return ret;
spi_tr.tx_buf = (unsigned long)spi_tx;
spi_tr.rx_buf = (unsigned long)spi_rx;
spi_tr.len = 0;
spi_tr.delay_usecs = SPI_DELAY;
spi_tr.speed_hz = SPI_SPEED;
spi_tr.bits_per_word = SPI_BITS_WIDTH_16;
return spi_fd;
}
void spi_close()
{
if (spi_fd > 0)
close(spi_fd);
spi_fd = -1;
}
uint16 spi_read_one_word(uint16 addr)
{
int ret;
spi_tr.len = 4;
spi_tx[0] = addr & 0xff ;
spi_tx[1] = addr >> 8;
spi_tx[2] = 0;
spi_tx[3] = 0;
ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &spi_tr);
if (ret < 1){
print_err("can't send spi message");
return 0;
}
else{
return *(uint16*)(spi_rx+2);
}
}
int spi_write_one_word(uint16 addr, uint16 data)
{
int ret=0;
spi_tr.len = 8;
// write data
spi_tx[0] = addr & 0xff ;
spi_tx[1] = addr >> 8;
spi_tx[2] = data & 0xff;
spi_tx[3] = data >> 8;
// send read command to read back the data for checking
spi_tx[4] = (0xC000|addr) & 0xff ;
spi_tx[5] = (0xC000|addr) >> 8;
spi_tx[6] = 0;
spi_tx[7] = 0;
ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &spi_tr);
if (ret < 1){
print_err("can't send spi word\n");
return -1;
}
// compare the data read back with the original data
if(data != (*(uint16*)(spi_rx+6))){
#ifdef debug
print_err("spi word write error \n");
#else
fault_status_return(SPI_WRITE_FAILED);
#endif
return SPI_WRITE_FAILED;
}
return 0;
}
int spi_write_one_word_no_check(uint16 addr, uint16 data)
{
int ret;
spi_tr.len = 4;
// write data
spi_tx[0] = addr & 0xff ;
spi_tx[1] = addr >> 8;
spi_tx[2] = data & 0xff;
spi_tx[3] = data >> 8;
ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &spi_tr);
if (ret < 1){
print_err("can't send spi word\n");
ret = -1;
}
return 0;
}
int write_pwm_data(uint16 *data)
{
int ret;
uint16 *p;
uint16 *d;
uint8 i;
uint8 error=0;
spi_tr.len = 32;
p=(uint16*)spi_tx;
d=(uint16*)spi_rx;
// write data
*p = SPI_WRITE_PWM_CH1;
*(p+1) = data[0];
*(p+2) = SPI_WRITE_PWM_CH2;
*(p+3) = data[1];
*(p+4) = SPI_WRITE_PWM_CH3;
*(p+5) = data[2];
*(p+6) = SPI_WRITE_PWM_CH4;
*(p+7) = data[3];
*(p+8) = SPI_WRITE_PWM_CH5;
*(p+9) = data[4];
*(p+10) = SPI_WRITE_PWM_CH6;
*(p+11) = data[5];
*(p+12) = SPI_WRITE_PWM_CH7;
*(p+13) = data[6];
*(p+14) = SPI_WRITE_PWM_CH8;
*(p+15) = data[7];
ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &spi_tr);
if (ret < 1){
print_err("can't send pwm data\n");
return -1;
}
/*
spi_tr.len = 64;
p=(uint16*)spi_tx;
d=(uint16*)spi_rx;
// write data
*p = SPI_WRITE_PWM_CH1;
*(p+1) = data[0];
*(p+2) = SPI_WRITE_PWM_CH2;
*(p+3) = data[1];
*(p+4) = SPI_WRITE_PWM_CH3;
*(p+5) = data[2];
*(p+6) = SPI_WRITE_PWM_CH4;
*(p+7) = data[3];
*(p+8) = SPI_WRITE_PWM_CH5;
*(p+9) = data[4];
*(p+10) = SPI_WRITE_PWM_CH6;
*(p+11) = data[5];
*(p+12) = SPI_WRITE_PWM_CH7;
*(p+13) = data[6];
*(p+14) = SPI_WRITE_PWM_CH8;
*(p+15) = data[7];
// send read command to read back the data for checking
*(p+16) = SPI_READ_PWM_CH1;
*(p+17) = 0;
*(p+18) = SPI_READ_PWM_CH2;
*(p+19) = 0;
*(p+20) = SPI_READ_PWM_CH3;
*(p+21) = 0;
*(p+22) = SPI_READ_PWM_CH4;
*(p+23) = 0;
*(p+24) = SPI_READ_PWM_CH5;
*(p+25) = 0;
*(p+26) = SPI_READ_PWM_CH6;
*(p+27) = 0;
*(p+28) = SPI_READ_PWM_CH7;
*(p+29) = 0;
*(p+30) = SPI_READ_PWM_CH8;
*(p+31) = 0;
ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &spi_tr);
if (ret < 1){
print_err("can't send pwm data\n");
return -1;
}
// compare the data read back with the original data
for(i=1;i<16;i=i+2){
//print_debug("%4x --- %4x \n",p[i],d[i+12]);
if(p[i] != d[i+16]){
#ifdef debug
print_err("pwm write error :%d %d--%d\n",(int)((i-1)/2),p[i],d[i+16]);
#else
fault_status_return(PWM_WRITE_FAILED);
#endif
error = 1;
}
}
if(error){
*(p+0) = SPI_READ_PWM_CH1;
*(p+1) = 0;
*(p+2) = SPI_READ_PWM_CH2;
*(p+3) = 0;
*(p+4) = SPI_READ_PWM_CH3;
*(p+5) = 0;
*(p+6) = SPI_READ_PWM_CH4;
*(p+7) = 0;
*(p+8) = SPI_READ_PWM_CH5;
*(p+9) = 0;
*(p+10) = SPI_READ_PWM_CH6;
*(p+11) = 0;
*(p+12) = SPI_READ_PWM_CH7;
*(p+13) = 0;
*(p+14) = SPI_READ_PWM_CH8;
*(p+15) = 0;
for(i=0;i<16;i++){
*(d+i) = 0;
}
ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &spi_tr);
if (ret < 1){
print_err("can't send pwm data\n");
return -1;
}
printf("%d,%d,%d,%d,%d,%d,%d,%d\n",d[1],d[3],d[5],d[7],d[9],d[11],d[13],d[15]);
return PWM_WRITE_FAILED;
}
*/
// check success ,write pwm data load command
ret = spi_write_one_word_no_check(SPI_WRITE_PWM_LOAD,0);
return ret;
}
/* read_spi_rc_data()
* read remote controller data captured by FPGA
* return -1 send command failed
* >0 number of bytes sent
*/
int read_rc_data(uint16 *data)
{
int ret;
uint16 *p;
uint16 *d;
uint8 i;
uint8 error=0;
spi_tr.len =28;
p=(uint16*)spi_tx;
d=(uint16*)spi_rx;
*p = SPI_READ_RC_CH1;
*(p+1) = 0;
*(p+2) = SPI_READ_RC_CH2;
*(p+3) = 0;
*(p+4) = SPI_READ_RC_CH3;
*(p+5) = 0;
*(p+6) = SPI_READ_RC_CH4;
*(p+7) = 0;
*(p+8) = SPI_READ_RC_CH5;
*(p+9) = 0;
*(p+10) = SPI_READ_RC_CH6;
*(p+11) = 0;
*(p+12) = SPI_READ_RC_PERIOD;
*(p+13) = 0;
ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &spi_tr);
if (ret < 1){
print_err("can't send rc read command \n");
ret = -1;
}
else{
*data = *(d+1); //rc channel1 data
*(data+1) = *(d+3); //rc channel2 data
*(data+2) = *(d+5); //rc channel3 data
*(data+3) = *(d+7); //rc channel4 data
*(data+4) = *(d+9); //rc channel5 data
*(data+5) = *(d+11); //rc channel6 data
*(data+6) = *(d+13); //rc period data
}
for(i=0;i<6;i++){
if((*(data+i))==0)
error++;
}
if(error>0){
ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &spi_tr);
if (ret < 1){
print_err("can't send rc read command \n");
ret = -1;
}
else{
*data = *(d+1); //rc channel1 data
*(data+1) = *(d+3); //rc channel2 data
*(data+2) = *(d+5); //rc channel3 data
*(data+3) = *(d+7); //rc channel4 data
*(data+4) = *(d+9); //rc channel5 data
*(data+5) = *(d+11); //rc channel6 data
*(data+6) = *(d+13); //rc period data
}
}
return ret;
}
void joystick_execute(int *pwm)
{
int i = 0;
for (i = 0; i < 8; i++) {
print_debug("0x%04x ", pwm[i]);
//spi_write(base_addr + i, pwm[i]);//???
}
print_debug("\n");
}
uint16 get_board_id()
{
return spi_read_one_word(SPI_READ_BOARD_ID);
}
uint32 get_fpga_version()
{
uint16 low;
uint16 high;
low=spi_read_one_word(SPI_READ_VERSION_LOW);
high=spi_read_one_word(SPI_READ_VERSION_HIGH);
return (high<<16) | low;
}
uint16 get_sonar_data()
{
uint16 raw_data_58us;
//float data_cm;
raw_data_58us=spi_read_one_word(SPI_READ_SONAR_DATA);
//data_cm = ((float)raw_data_58us)/58;// the raw data is 1cm per 58us;
return raw_data_58us;
}
int set_control_register(int mask_bit)
{
int ret=0;
uint16 data;
data = spi_read_one_word(SPI_READ_CONTROL_REG);
data = data | (1 << mask_bit);
ret=spi_write_one_word(SPI_WRITE_CONTROL_REG,data);
return ret;
}
int reset_control_register(int mask_bit)
{
int ret=0;
uint16 data;
data = spi_read_one_word(SPI_READ_CONTROL_REG);
data = data & (0<< mask_bit);
ret=spi_write_one_word(SPI_WRITE_CONTROL_REG,data);
return ret;
}
void set_servo_pwm_period(uint16 data)
{
printf("set pwm period as :%d us \n",data);
spi_write_one_word(SPI_WRITE_PWM_PERIOD,data);
spi_write_one_word(SPI_WRITE_PWM_CH7_PERIOD,data);
spi_write_one_word(SPI_WRITE_PWM_CH8_PERIOD,data);
}