-
Notifications
You must be signed in to change notification settings - Fork 0
/
ficlib2.c
1657 lines (1340 loc) · 45.1 KB
/
ficlib2.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ficlib2.h"
volatile unsigned *gpio;
//-----------------------------------------------------------------------------
struct _prog_stat PROG_STAT = {
.stat = PM_STAT_INIT,
.smap_mode = PM_SMAP_8,
.prog_mode = PM_NORMAL,
.prog_st_time = 0,
.prog_ed_time = 0,
.prog_size = 0,
.tx_size = 0,
};
//-----------------------------------------------------------------------------
// GPIO operation (with GPIO check)
//-----------------------------------------------------------------------------
static inline int fic_set_gpio_fast(uint32_t set) {
SET_GPIO = set;
while ((GET_GPIO & set) ^ set) asm("nop");
return 0;
}
static inline int fic_clr_gpio_fast(uint32_t set) {
CLR_GPIO = set;
while (GET_GPIO & set) asm("nop");
return 0;
}
//-----------------------------------------------------------------------------
// GPIO operation (with GPIO check)
//-----------------------------------------------------------------------------
static inline int fic_set_gpio(uint32_t set) {
time_t t1, t2;
SET_GPIO = set;
time(&t1);
while ((GET_GPIO & set) ^ set) {
time(&t2);
if (t2 - t1 > COMM_TIMEOUT) {
PErr("Communication timeout");
return -1;
}
//usleep(1);
}
return 0;
}
static inline int fic_clr_gpio(uint32_t set) {
time_t t1, t2;
CLR_GPIO = set;
time(&t1);
while (GET_GPIO & set) {
time(&t2);
if (t2 - t1 > COMM_TIMEOUT) {
PErr("Communication timeout");
return -1;
}
//usleep(1);
}
return 0;
}
//-----------------------------------------------------------------------------
// Display GPIO status for DEBUG
// Use DEBUGCOM macro for debug
//-----------------------------------------------------------------------------
void fic_comm_busdebug(int line) {
PDebug("L%d GPIO=%x ", line, GET_GPIO);
if (GET_GPIO & RP_PIN_RREQ) {
PDebug("RREQ = 1 ");
} else {
PDebug("RREQ = 0 ");
}
if (GET_GPIO & RP_PIN_FREQ) {
PDebug("FREQ = 1 ");
} else {
PDebug("FREQ = 0 ");
}
if (GET_GPIO & RP_PIN_RSTB) {
PDebug("RSTB = 1 ");
} else {
PDebug("RSTB = 0 ");
}
if (GET_GPIO & RP_PIN_FACK) {
PDebug("FACK = 1 ");
} else {
PDebug("FACK = 0 ");
}
puts("");
}
//-----------------------------------------------------------------------------
int fic_done() {
SET_INPUT(RP_DONE);
if (GET_GPIO & RP_PIN_DONE) {
return 1;
}
return 0;
}
int fic_power() {
SET_INPUT(RP_PWOK);
if (GET_GPIO & RP_PIN_PWOK) {
return 1;
}
return 0;
}
//-----------------------------------------------------------------------------
inline int fic_comm_setup() {
int i;
SET_ALL_INPUT;
for (i = 0; i < GPIO_PIN_MAX; i++) {
if (i == RP_FACK || i == RP_FREQ ) {
SET_INPUT(i);
}
if (i == RP_RREQ || i == RP_RSTB || (i >= RP_DATA_LOW && i <= RP_DATA_TOP)) {
SET_INPUT(i);
SET_OUTPUT(i);
if (fic_clr_gpio(0x01 << i) < 0) return -1; // Negate
}
}
return 0;
}
//-----------------------------------------------------------------------------
// DESC: Reset I/F FSM
//-----------------------------------------------------------------------------
int fic_comm_reset() {
if (fic_comm_setup() < 0) return -1;
// RREQ dessert
if (fic_clr_gpio(RP_PIN_RREQ) < 0) return -1;
// STB assert
if (fic_set_gpio(RP_PIN_RSTB) < 0) return -1;
return 0;
}
//-----------------------------------------------------------------------------
// DESC: Change RPi-FiC I/F transfer direction
//-----------------------------------------------------------------------------
static inline void fic_comm_portdir(enum COMM_PORT_DIR dir) {
int i;
if (dir == COMM_PORT_SND) {
for (i = RP_DATA_LOW; i <= RP_DATA_TOP; i++ ) {
SET_INPUT(i);
SET_OUTPUT(i);
}
} else if (dir == COMM_PORT_RCV) {
for (i = RP_DATA_LOW; i <= RP_DATA_TOP; i++ ) {
SET_INPUT(i);
}
}
}
//-----------------------------------------------------------------------------
// DESC: Wait until fack signal is down
//-----------------------------------------------------------------------------
static inline int fic_comm_wait_fack_down() {
time_t t1, t2;
time(&t1);
while (GET_GPIO_PIN(RP_FACK) == 1) {
time(&t2);
if (t2 - t1 > COMM_TIMEOUT) {
PErr("Communication timeout");
return -1;
}
}
return 0;
}
static inline int fic_comm_wait_fack_down_notimeout() {
int i = 0;
while (GET_GPIO_PIN(RP_FACK) == 1) {
if (i > COMM_TIMEOUT_NUM) return -1;
i++;
//asm("nop");
}
return 0;
}
//-----------------------------------------------------------------------------
// DESC: Wait until fack signal is up
//-----------------------------------------------------------------------------
static inline int fic_comm_wait_fack_up() {
time_t t1, t2;
time(&t1);
while (GET_GPIO_PIN(RP_FACK) == 0) {
time(&t2);
if (t2 - t1 > COMM_TIMEOUT) {
PErr("Communication timeout");
return -1;
}
}
return 0;
}
static inline int fic_comm_wait_fack_up_notimeout() {
int i = 0;
while (GET_GPIO_PIN(RP_FACK) == 0) {
if (i > COMM_TIMEOUT_NUM) return -1;
i++;
//asm("nop");
}
return 0;
}
//-----------------------------------------------------------------------------
// DESC: Wait until freq signal is down
//-----------------------------------------------------------------------------
static inline int fic_comm_wait_freq_down() {
time_t t1, t2;
time(&t1);
while (GET_GPIO_PIN(RP_FREQ) == 1) {
time(&t2);
if (t2 - t1 > COMM_TIMEOUT) {
PErr("Communication timeout");
return -1;
}
}
return 0;
}
static inline int fic_comm_wait_freq_down_notimeout() {
int i = 0;
while (GET_GPIO_PIN(RP_FREQ) == 1) {
if (i > COMM_TIMEOUT_NUM) return -1;
i++;
//asm("nop");
}
return 0;
}
//-----------------------------------------------------------------------------
// DESC: Wait until freq signal is up
//-----------------------------------------------------------------------------
static inline int fic_comm_wait_freq_up() {
time_t t1, t2;
time(&t1);
while (GET_GPIO_PIN(RP_FREQ) == 0) {
time(&t2);
if (t2 - t1 > COMM_TIMEOUT) {
PErr("Communication timeout");
return -1;
}
}
return 0;
}
static inline int fic_comm_wait_freq_up_notimeout() {
int i = 0;
while (GET_GPIO_PIN(RP_FREQ) == 0) {
if (i > COMM_TIMEOUT_NUM) return -1;
i++;
//asm("nop");
}
return 0;
}
//-----------------------------------------------------------------------------
// DESC: Send single word data
//-----------------------------------------------------------------------------
static inline int fic_comm_send(uint32_t bus) {
#ifndef FICMK2
// Clr RSTB and DATA bus
if (fic_clr_gpio(RP_PIN_RSTB |
RP_PIN_DATA7 | RP_PIN_DATA6 | RP_PIN_DATA5 | RP_PIN_DATA4) < 0) {
return -1;
}
#else
// Clr RSTB and DATA bus
if (fic_clr_gpio(RP_PIN_RSTB |
RP_PIN_DATA0 | RP_PIN_DATA1 | RP_PIN_DATA2 | RP_PIN_DATA3 |
RP_PIN_DATA4 | RP_PIN_DATA5 | RP_PIN_DATA6 | RP_PIN_DATA7 |
RP_PIN_DATA8 | RP_PIN_DATA9 | RP_PIN_DATA10 | RP_PIN_DATA11 |
RP_PIN_DATA12 | RP_PIN_DATA13 | RP_PIN_DATA14 | RP_PIN_DATA15) < 0) {
return -1;
}
#endif
if (fic_set_gpio(RP_PIN_RSTB | bus) < 0) {
return -1;
}
if (fic_comm_wait_fack_up() < 0) {
PErr("fic_comm_wait_fack_up failed");
return -1;
}
if (fic_clr_gpio(RP_PIN_RSTB) < 0) {
return -1;
}
if (fic_comm_wait_fack_down() < 0) {
PErr("fic_comm_wait_fack_down failed");
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// DESC: Send single word data (Fast ver)
//-----------------------------------------------------------------------------
static inline int fic_comm_send_fast(uint32_t bus) {
int ret = 0;
// Clr RSTB and DATA bus
#ifndef FICMK2
ret |= fic_clr_gpio(RP_PIN_RSTB | RP_PIN_DATA7 | RP_PIN_DATA6 |
RP_PIN_DATA5 | RP_PIN_DATA4);
#else
ret |= fic_clr_gpio(RP_PIN_RSTB |
RP_PIN_DATA0 | RP_PIN_DATA1 | RP_PIN_DATA2 | RP_PIN_DATA3 |
RP_PIN_DATA4 | RP_PIN_DATA5 | RP_PIN_DATA6 | RP_PIN_DATA7 |
RP_PIN_DATA8 | RP_PIN_DATA9 | RP_PIN_DATA10 | RP_PIN_DATA11 |
RP_PIN_DATA12 | RP_PIN_DATA13 | RP_PIN_DATA14 | RP_PIN_DATA15);
#endif
ret |= fic_set_gpio_fast(RP_PIN_RSTB | bus);
ret |= fic_comm_wait_fack_up_notimeout();
ret |= fic_clr_gpio_fast(RP_PIN_RSTB);
//ret |= fic_comm_wait_fack_down_notimeout(); // Omit check
return ret;
}
static inline int fic_comm_receive() {
if (fic_clr_gpio(RP_PIN_RSTB) < 0) return -1;
if (fic_set_gpio(RP_PIN_RSTB) < 0) return -1;
if (fic_comm_wait_fack_up() < 0) {
PErr("fic_comm_wait_fack_up failed");
return -1;
}
#ifndef FICMK2
uint8_t rcv = 0;
rcv = (GET_GPIO >> RP_DATA_LOW) & 0xff;
#else
uint16_t rcv = 0;
rcv = (GET_GPIO >> RP_DATA_LOW) & 0xffff;
#endif
if (fic_clr_gpio(RP_PIN_RSTB) < 0) return -1;
if (fic_comm_wait_fack_down() < 0) {
PErr("fic_comm_wait_fack_down failed");
return -1;
}
return (int) rcv;
}
static inline int fic_comm_receive_fast(uint32_t *rcv) {
int ret = 0;
ret |= fic_clr_gpio_fast(RP_PIN_RSTB);
ret |= fic_set_gpio_fast(RP_PIN_RSTB);
ret |= fic_comm_wait_fack_up_notimeout();
if (ret < 0) return ret;
#ifndef FICMK2
(GET_GPIO);
(GET_GPIO);
*rcv = (GET_GPIO >> RP_DATA_LOW) & 0xf;
#else
// Note: mk2's 16bit I/F is unstable. so read it twice then check.
(GET_GPIO);
(GET_GPIO);
(GET_GPIO);
*rcv = (GET_GPIO >> RP_DATA_LOW) & 0xffff;
#endif
ret |= fic_clr_gpio_fast(RP_PIN_RSTB);
// ret |= fic_comm_wait_fack_down(); // Omit check
return ret;
}
//static inline int fic_comm_receive_debug(uint32_t *rcv) {
// *rcv = 0;
//
// if (fic_clr_gpio(RP_PIN_RSTB) < 0) return -1;
// if (fic_set_gpio(RP_PIN_RSTB) < 0) return -1;
//
// if (fic_comm_wait_fack_up() < 0) {
// PErr\( "[libfic2][ERROR]: fic_comm_wait_fack_up failed at %s %s %d",
// __FILE__, __FUNCTION__, __LINE__);
// return -1;
// }
//
//#ifndef FICMK2
// *rcv = (GET_GPIO >> RP_DATA_LOW) & 0xff;
//#else
// *rcv = (GET_GPIO >> RP_DATA_LOW) & 0xffff;
//#endif
//
// if (fic_clr_gpio(RP_PIN_RSTB) < 0) return -1;
//
// if (fic_comm_wait_fack_down() < 0) {
// PErr\( "[libfic2][ERROR]: fic_comm_wait_fack_down failed at %s %s %d",
// __FILE__, __FUNCTION__, __LINE__);
// return -1;
// }
//
// return 0;
//}
int fic_comm_setaddr(uint16_t addr) {
#ifndef FICMK2
// Send 16bit high-high address
if (fic_comm_send(((addr & 0xf000) >> 12) << RP_DATA_LOW) < 0) return -1;
// Send 16bit high-low address
if (fic_comm_send(((addr & 0x0f00) >> 8) << RP_DATA_LOW) < 0) return -1;
// Send 16bit low-high address
if (fic_comm_send(((addr & 0x00f0) >> 4) << RP_DATA_LOW) < 0) return -1;
// Send 16bit low-low address
if (fic_comm_send(((addr & 0x000f) << RP_DATA_LOW)) < 0) return -1;
#else
// Send 16bit address
if (fic_comm_send((addr & 0xffff) << RP_DATA_LOW) < 0) return -1;
#endif
return 0;
}
//-----------------------------------------------------------------------------
// Write fic register via 4bit interface
//-----------------------------------------------------------------------------
inline int fic_write(uint16_t addr, uint16_t data) {
if (fic_comm_setup() < 0) return -1;
fic_comm_portdir(COMM_PORT_SND);
// RREQ assert
if (fic_set_gpio(RP_PIN_RREQ) < 0) return -1;
// Send command
if (fic_comm_send(COMM_CMD_WRITE << RP_DATA_LOW) < 0) return -1;
// Set address
if (fic_comm_setaddr(addr) < 0) return -1;
#ifndef FICMK2
// Send 4bit high data
if (fic_comm_send(((data & 0xf0) >> 4) << RP_DATA_LOW) < 0) return -1;
// Send 4bit low data
if (fic_comm_send((data & 0x0f) << RP_DATA_LOW) < 0) return -1;
#else
// Send 16bit data
if (fic_comm_send(data << RP_DATA_LOW) < 0) return -1;
#endif
// RREQ dessert
if (fic_clr_gpio(RP_PIN_RREQ | RP_PIN_RSTB | COMM_DATABUS_MASK) < 0) return -1;
if (fic_comm_wait_freq_down() < 0) return -1;
SET_ALL_INPUT;
return 0;
}
//-----------------------------------------------------------------------------
// Read fic register via 4bit interface
//-----------------------------------------------------------------------------
inline int fic_read(uint16_t addr) {
if (fic_comm_setup() < 0) return -1;
fic_comm_portdir(COMM_PORT_SND);
// RREQ assert
if (fic_set_gpio(RP_PIN_RREQ) < 0) return -1;
// Send command
if (fic_comm_send(COMM_CMD_READ << RP_DATA_LOW) < 0) return -1;
// Set address
if (fic_comm_setaddr(addr) < 0) return -1;
// Change port direction
fic_comm_portdir(COMM_PORT_RCV);
int i = 0;
#ifndef FICMK2
uint8_t rcv = 0;
// Receive 4bit high data
i = fic_comm_receive(); if (i < 0) return -1;
rcv = (i & 0x0f) << 4;
i = fic_comm_receive(); if (i < 0) return -1;
rcv |= i & 0x0f;
#else
uint16_t rcv = 0;
// Receive 16bit data
i = fic_comm_receive(); if (i < 0) return -1;
rcv = i;
#endif
// RREQ dessert
if (fic_clr_gpio(RP_PIN_RREQ) < 0) return -1;
if (fic_comm_wait_freq_down() < 0) return -1;
SET_ALL_INPUT;
return rcv;
}
//-----------------------------------------------------------------------------
// Transfer bytes via 4bit interface
// Modify 2019.11.30: Make the function send each 1B on *buf via 4bit interface
//-----------------------------------------------------------------------------
static inline int _fic_hls_comm_initiate(enum RASIO_CMD cmd) {
if (fic_comm_setup() < 0) return -1;
fic_comm_portdir(COMM_PORT_SND);
// RREQ assert
// PDebug("DEBUG: RREQ assert");
if (fic_set_gpio(RP_PIN_RREQ) < 0) return -1;
// if (fic_comm_wait_freq_up() < 0) return -1;
// Send command
// PDebug("DEBUG: Send command");
if (fic_comm_send(cmd << RP_DATA_LOW) < 0) return -1;
// Set address at HLS module entry point 0x1000
// PDebug("DEBUG: Send addr");
if (fic_comm_setaddr(COMM_ADDR_HLS) < 0) return -1;
// Change port direction if cmd is READ
if (cmd == COMM_CMD_READ) {
// Change port direction
fic_comm_portdir(COMM_PORT_RCV);
}
return 0;
}
static inline int _fic_hls_comm_terminate() {
// RREQ dessert
//if (fic_clr_gpio(RP_PIN_RREQ | RP_PIN_RSTB | COMM_DATABUS_MASK) < 0) return -1;
if (fic_clr_gpio(RP_PIN_RREQ | RP_PIN_RSTB) < 0) return -1;
if (fic_comm_wait_freq_down() < 0) return -1;
SET_ALL_INPUT;
return 0;
}
static inline int _fic_hls_send_bytes(uint8_t *data, size_t size) {
size_t i;
int ret = 0;
#ifndef FICMK2
// For mk1 board
for (i = 0; i < size; i++) {
ret |= fic_comm_send_fast(((*(data+i) & 0xf0) >> 4) << RP_DATA_LOW); // 4bit high
ret |= fic_comm_send_fast((*(data+i) & 0x0f) << RP_DATA_LOW); // 4bit low
if (i > 0 && (i % (1024*1024)) == 0) {
PInfo("%s Send %d bytes",__FUNCTION__, i);
}
}
PInfo("%s Send %d bytes",__FUNCTION__, i);
#else
// For mk2 board
// Transfer 2B data each time
if (size < 2) {
PErr("%d is too small (size should more than 2 byte", size);
return -1;
}
if (size % 2 != 0) {
PErr("%d is not 2B aligned size", size);
return -1;
}
for (i = 0; i < size; i+=2) {
ret |= fic_comm_send_fast((*(data+i) | *(data+i+1) << 8) << RP_DATA_LOW); // 8bit + 8bit
if ((i % (1024*1024)) == 0) {
PInfo("%s Send %d bytes",__FUNCTION__, i);
}
}
PInfo("%s Send %d bytes",__FUNCTION__, i);
#endif
if (ret < 0) {
PErr("fic_comm_send_bytes failed");
return -1;
}
return 0;
}
inline int fic_hls_send(uint8_t *data, size_t size) {
// Begin send to HLS
if (_fic_hls_comm_initiate(COMM_CMD_WRITE) < 0) return -1;
// Send Data
if (_fic_hls_send_bytes(data, size) < 0) return -1;
// End send to HLS
if (_fic_hls_comm_terminate() < 0) return -1;
return 0;
}
//-----------------------------------------------------------------------------
// Receive bytes via 4bit interface
// Modify 2019.11.30: Make the function receive each 1B on *buf via 4bit interface
//-----------------------------------------------------------------------------
static inline int _fic_hls_receive_bytes(uint8_t *data, size_t size) {
size_t i;
int ret = 0;
#ifndef FICMK2
// for mk1
// mk2 has 4bit word width = 0.5bytes at once
for (i = 0; i < size; i++) {
int rcv = 0;
uint32_t rvh, rvl;
// Receive Low 4bit
ret = fic_comm_receive_fast(&rvh);
if (ret < 0) break;
// Receive High 4bit
ret = fic_comm_receive_fast(&rvl);
if (ret < 0) break;
// For mk1 last 4bit is valid in rvh and rvl
*(data+i) = ((rvh << 4) | rvl) & 0xff;
if (i > 0 && (i % (1024*1024)) == 0) {
PInfo("%s Received %d bytes",__FUNCTION__, i);
}
}
PInfo("%s Received %d bytes",__FUNCTION__, i);
#else
// for mk2
// mk2 has 16bit word width = 2bytes at once
if (size < 2) {
PErr("%d is too small (size should more than 2 byte", size);
return -1;
}
if (size % 2 != 0) {
PErr("%d is must be 2B aligned size", size);
return -1;
}
for (i = 0; i < size; i+=2) {
uint32_t rv = 0;
// Receive 16bit
ret = fic_comm_receive_fast(&rv);
if (ret < 0) break;
// For mk2 received 2B at once
*(data+i) = (rv & 0x00ff) >> 0;
*(data+i+1) = (rv & 0xff00) >> 8;
if ((i % (1024*1024)) == 0) {
PInfo("%s Received %d bytes",__FUNCTION__, i);
}
}
PInfo("%s Received %d bytes",__FUNCTION__, i);
#endif
if (ret < 0) {
PErr("fic_comm_receive_bytes failed");
return -1;
}
return 0;
}
inline int fic_hls_receive(uint8_t *buf, size_t size) {
// Begin receive data from HLS
if (_fic_hls_comm_initiate(COMM_CMD_READ) < 0) return -1;
// Receive data
if(_fic_hls_receive_bytes(buf, size) < 0) return -1;
// Begin receive data from HLS
if (_fic_hls_comm_terminate() < 0) return -1;
return 0;
}
//-----------------------------------------------------------------------------
// Write data to DDR module (rasddr) via RPI 4bit interface
//-----------------------------------------------------------------------------
int fic_hls_write(uint8_t *data, size_t size, uint32_t addr, enum RASDDR_CMD ctrl) {
uint32_t cmd[3];
cmd[0] = ctrl;
cmd[1] = addr;
cmd[2] = size;
PDebug("addr = %08x, size = %d", addr, size);
// Initiate communication port
if (_fic_hls_comm_initiate(COMM_CMD_WRITE) < 0) {
PErr("Communication error");
return -1;
}
// Send cmd
if (_fic_hls_send_bytes((uint8_t *)&cmd, 12) < 0) {
PErr("Communication error");
return -1;
}
// Send data
if (_fic_hls_send_bytes(data, size) < 0) {
PErr("Communication error");
return -1;
}
// Terminate communication port
if (_fic_hls_comm_terminate() < 0) {
PErr("Communication error");
return -1;
}
return 0;
}
int fic_hls_ddr_write(uint8_t *data, size_t size, uint32_t addr) {
return fic_hls_write(data, size, addr, RASDDR_CMD_WRITE);
}
//-----------------------------------------------------------------------------
// Receive bytes via 4bit interface
//-----------------------------------------------------------------------------
int fic_hls_read(uint8_t *buf, size_t size, uint32_t addr, enum RASDDR_CMD ctrl) {
uint32_t cmd[3];
cmd[0] = ctrl;
cmd[1] = addr;
cmd[2] = size;
PDebug("DEBUG: addr = %08x, size = %d", addr, size);
// Send cmd to DDR module
if (fic_hls_send((uint8_t *)&cmd, 12) < 0) { // cmd
PErr("Communication error");
return -1;
}
// Initiate communication port
if (_fic_hls_comm_initiate(COMM_CMD_READ) < 0) {
PErr("Communication error");
return -1;
}
// Receive bytes from DDR module
if (_fic_hls_receive_bytes(buf, size) < 0) {
PErr("Communication error");
return -1;
}
// Terminate communication port
if (_fic_hls_comm_terminate() < 0) {
PErr("Communication error");
return -1;
}
// fic_hls_send((uint8_t *)&cmd, 12); // cmd
// fic_hls_receive(buf, size); // size of data
return 0;
}
int fic_hls_ddr_read(uint8_t *buf, size_t size, uint32_t addr) {
return fic_hls_read(buf, size, addr, RASDDR_CMD_READ);
}
//-----------------------------------------------------------------------------
// Receive bytes via 4bit interface
//-----------------------------------------------------------------------------
int fic_hls_ddr_debug(uint8_t *buf, size_t size, uint32_t addr) {
int ret = 0;
uint32_t cmd[3];
cmd[0] = RASDDR_CMD_DEBUG;
cmd[1] = addr;
cmd[2] = size;
ret = fic_hls_send((uint8_t *)&cmd, 12); // CMD
ret = fic_hls_receive(buf, 12); // size of data
if (ret < 0) return -1;
return 0;
}
//-----------------------------------------------------------------------------
// Selectmap x16 PIN init
//-----------------------------------------------------------------------------
int fic_prog_init_sm16() {
int i;
SET_ALL_INPUT;
for (i = 0; i <= GPIO_PIN_MAX; i++) {
#ifndef FICMK2
if (i == RP_PWOK || i == RP_INIT || i == RP_DONE || i == RP_G_CKSEL) {
SET_INPUT(i);
}
if (i == RP_PROG_B || i == RP_CSI_B || i == RP_RDWR_B) {
SET_INPUT(i);
SET_OUTPUT(i);
if (fic_set_gpio(0x01 << i) < 0) return -1; // Disabled
}
if (i == RP_CCLK || i == RP_CD0 || i == RP_CD1 || i == RP_CD2 ||
i == RP_CD3 || i == RP_CD4 || i == RP_CD5 || i == RP_CD6 ||
i == RP_CD7 || i == RP_CD8 || i == RP_CD9 || i == RP_CD10 ||
i == RP_CD11 || i == RP_CD12 || i == RP_CD13 || i == RP_CD14 ||
i == RP_CD15) {
SET_INPUT(i);
SET_OUTPUT(i);
if (fic_clr_gpio(0x01 << i) < 0) return -1; // Negate
}
#else
if (i == RP_PWOK || i == RP_INIT || i == RP_DONE) {
SET_INPUT(i);
}
if (i == RP_PROG_B || i == RP_CSI_B || i == RP_RDWR_B || i == RP_CFSEL) {
SET_INPUT(i);
SET_OUTPUT(i);
if (fic_set_gpio(0x01 << i) < 0) return -1; // Set PINS on
}
if (i == RP_CCLK || i == RP_CD0 || i == RP_CD1 || i == RP_CD2 ||
i == RP_CD3 || i == RP_CD4 || i == RP_CD5 || i == RP_CD6 ||
i == RP_CD7 || i == RP_CD8 || i == RP_CD9 || i == RP_CD10 ||
i == RP_CD11 || i == RP_CD12 || i == RP_CD13 || i == RP_CD14 ||
i == RP_CD15) {
SET_INPUT(i);
SET_OUTPUT(i);
if (fic_clr_gpio(0x01 << i) < 0) return -1; // Negate
}
#endif
}
#ifdef FICMK2
// Set bus switch to FPGA configuration mode
SET_OUTPUT(RP_CFSEL);
if (fic_set_gpio(RP_PIN_CFSEL) < 0) return -1; // Set CFG mode
PDebug("RP_CFSEL is set for FiC Mark2 board");
#endif
return 0;
}
//-----------------------------------------------------------------------------
// Selectmap x8 PIN init
//-----------------------------------------------------------------------------
int fic_prog_init_sm8() {
int i;
SET_ALL_INPUT;
for (i = 0; i <= GPIO_PIN_MAX; i++) {
#ifndef FICMK2
if (i == RP_PWOK || i == RP_INIT || i == RP_DONE || i == RP_G_CKSEL) {
SET_INPUT(i);
}
if (i == RP_PROG_B || i == RP_CSI_B || i == RP_RDWR_B ) {
SET_INPUT(i);
SET_OUTPUT(i);
if (fic_set_gpio(0x01 << i) < 0) return -1; // Disabled
}
if (i == RP_CCLK || i == RP_CD0 || i == RP_CD1 || i == RP_CD2 ||
i == RP_CD3 || i == RP_CD4 || i == RP_CD5 || i == RP_CD6 ||
i == RP_CD7) {
SET_INPUT(i);
SET_OUTPUT(i);
if (fic_clr_gpio(0x01 << i) < 0) return -1; // Negate
}
#else
if (i == RP_PWOK || i == RP_INIT || i == RP_DONE) {
SET_INPUT(i);
}
if (i == RP_PROG_B || i == RP_CSI_B || i == RP_RDWR_B || i == RP_CFSEL) {
SET_INPUT(i);
SET_OUTPUT(i);
if (fic_set_gpio(0x01 << i) < 0) return -1; // Set PINS on
}
if (i == RP_CCLK || i == RP_CD0 || i == RP_CD1 || i == RP_CD2 ||
i == RP_CD3 || i == RP_CD4 || i == RP_CD5 || i == RP_CD6 ||
i == RP_CD7) {
SET_INPUT(i);
SET_OUTPUT(i);
if (fic_clr_gpio(0x01 << i) < 0) return -1; // Negate
}
#endif
}
#ifdef FICMK2
// Set bus switch to FPGA configuration mode
SET_OUTPUT(RP_CFSEL);
if (fic_set_gpio(RP_PIN_CFSEL) < 0) return -1; // Set CFG mode
PDebug("RP_CFSEL is set for FiC Mark2 board");
#endif
return 0;
}
//-----------------------------------------------------------------------------
// FPGA Init
//-----------------------------------------------------------------------------
int fic_prog_init(enum PROG_MODE pm) {
#ifdef FICMK2
SET_OUTPUT(RP_CFSEL);
if (fic_set_gpio(RP_PIN_CFSEL) < 0) return -1; // Set CFG mode
PDebug("RP_CFSEL is set for FiC Mark2 board");
#endif
// Pin setup for FPGA Init
SET_INPUT(RP_PROG_B); SET_OUTPUT(RP_PROG_B);
SET_INPUT(RP_CSI_B); SET_OUTPUT(RP_CSI_B);
SET_INPUT(RP_RDWR_B); SET_OUTPUT(RP_RDWR_B);
// Set disabled pins
if (fic_set_gpio(RP_PIN_PROG_B | RP_PIN_CSI_B | RP_PIN_RDWR_B) < 0) return -1;
// Partial reconfiguration mode
if (pm == PM_PR) {
if (fic_clr_gpio(RP_PIN_CSI_B | RP_PIN_RDWR_B) < 0) return -1; // Assert
return 0;
}
// Do FPGA init sequence
if (fic_set_gpio(RP_PIN_PROG_B | RP_PIN_CSI_B | RP_PIN_RDWR_B) < 0) return -1;
if (fic_clr_gpio(RP_PIN_PROG_B | RP_PIN_CSI_B | RP_PIN_RDWR_B) < 0) return -1;
if (fic_set_gpio(RP_PIN_PROG_B) < 0) return -1;
while (GET_GPIO_PIN(RP_INIT) == 0) {
PDebug("Awaiting FPGA reset...");
(GET_GPIO);
usleep(1000);
}
if (GET_GPIO_PIN(RP_DONE) == 1) {
PDebug("FPGA reset failed");
return -1;
}
PDebug("FPGA reset success...");
return 0;
}
//-----------------------------------------------------------------------------
// Note: all gpio set/hold timing is very empirical for RPi3B
// You need adjust values if you change HW
//-----------------------------------------------------------------------------
static inline void _gpio_hold(uint32_t out) {
int i;