-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathicoprog.cc
1828 lines (1506 loc) · 38.4 KB
/
icoprog.cc
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
/*
* IcoProg -- Programmer and Debug Tool for the IcoBoard
*
* Copyright (C) 2016 Clifford Wolf <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <assert.h>
#include <vector>
bool verbose = false;
bool ftdi_verbose = false;
bool got_error = false;
bool enable_prog_port = false;
bool enable_data_port = false;
// --------------------------------------------------------------------------------------------
#if !defined(USBMODE) && !defined(GPIOMODE)
# include <wiringPi.h>
# define RPI_ICE_CLK 14 // GP11 --
# define RPI_ICE_CDONE 16 // GP15 --
# define RPI_ICE_MOSI 12 // GP10 --
# define RPI_ICE_MISO 13 // GP09 --
# define LOAD_FROM_FLASH 25 // IGNORE -- PIN 33, GPIO.23
# define RPI_ICE_CRESET 6 // GP25 --
# define RPI_ICE_CS 11 // GP07 --
# define RPI_ICE_SELECT 24 // IGNORE --PIN 32, GPIO.26
# define RASPI_D8 0 // PIN 11, GPIO.0
# define RASPI_D7 1 // PIN 12, GPIO.1
# define RASPI_D6 3 // PIN 15, GPIO.3
# define RASPI_D5 4 // PIN 16, GPIO.4
# define RASPI_D4 12 // PIN 19, MOSI
# define RASPI_D3 13 // PIN 21, MISO
# define RASPI_D2 11 // PIN 26, CE1
# define RASPI_D1 24 // PIN 35, GPIO.24
# define RASPI_D0 27 // PIN 36, GPIO.27
# define RASPI_DIR 28 // PIN 38, GPIO.28
# define RASPI_CLK 29 // PIN 40, GPIO.29
void digitalSync(int usec_delay)
{
usleep(usec_delay);
}
#endif
// --------------------------------------------------------------------------------------------
#ifdef USBMODE
# include <ftdi.h>
// ADBUS0 0 BDBUS0 16
// ADBUS1 1 BDBUS1 17
// ADBUS2 2 BDBUS2 18
// ADBUS3 3 BDBUS3 19
// ADBUS4 4 BDBUS4 20
// ADBUS5 5 BDBUS5 21
// ADBUS6 6 BDBUS6 22
// ADBUS7 7 BDBUS7 23
// ACBUS0 8 BCBUS0 24
// ACBUS1 9 BCBUS1 25
// ACBUS2 10 BCBUS2 26
// ACBUS3 11 BCBUS3 27
// ACBUS4 12 BCBUS4 28
// ACBUS5 13 BCBUS5 29
// ACBUS6 14 BCBUS6 30
// ACBUS7 15 BCBUS7 31
# define RPI_ICE_CLK 16 // PIN 7
# define RPI_ICE_CDONE 22 // PIN 13
# define RPI_ICE_MOSI 17 // PIN 29
# define RPI_ICE_MISO 18 // PIN 31
# define LOAD_FROM_FLASH 21 // PIN 33
# define RPI_ICE_CRESET 23 // PIN 37
# define RPI_ICE_CS 20 // PIN 24
# define RPI_ICE_SELECT 19 // PIN 32
# define RASPI_D8 12 // PIN 11
# define RASPI_D7 14 // PIN 12
# define RASPI_D6 6 // PIN 15
# define RASPI_D5 7 // PIN 16
# define RASPI_D4 1 // PIN 19
# define RASPI_D3 2 // PIN 21
# define RASPI_D2 10 // PIN 26
# define RASPI_D1 3 // PIN 35
# define RASPI_D0 13 // PIN 36
# define RASPI_DIR 4 // PIN 38
# define RASPI_CLK 5 // PIN 40
# define INPUT 0
# define OUTPUT 1
# define LOW 0
# define HIGH 1
struct ftdi_context ftdia, ftdib;
uint32_t ftdistate_dir, ftdistate_val;
void my_ftdi_send(struct ftdi_context *ftdi, uint8_t *buffer, int len)
{
char interface = ftdi == &ftdia ? 'A' : ftdi == &ftdib ? 'B' : 'X';
if (ftdi_verbose) {
printf("ftdi-send-%c>", interface);
for (int i = 0; i < len; i++)
printf(" %02x", buffer[i]);
printf("\n");
}
while (len > 0) {
int rc = ftdi_write_data(ftdi, buffer, len);
if (rc <= 0) {
fprintf(stderr, "Communication error. (ftdi interface %c write rc=%d, %s)\n",
interface, rc, ftdi_get_error_string(ftdi));
exit(1);
}
buffer += rc;
len -= rc;
}
}
void my_ftdi_recv(struct ftdi_context *ftdi, uint8_t *buffer, int len)
{
char interface = ftdi == &ftdia ? 'A' : ftdi == &ftdib ? 'B' : 'X';
int retry_cnt = 0;
int offset = 0;
while (offset < len)
{
int rc = ftdi_read_data(ftdi, buffer + offset, len - offset);
if (rc == 0)
{
if (++retry_cnt > 10)
{
if (ftdi_verbose) {
printf("ftdi-partial-recv-%c>", interface);
for (int i = 0; i < offset; i++)
printf(" %02x", buffer[i]);
printf("\n");
}
fprintf(stderr, "FTDI interface %c read timeout after %d / %d bytes.\n",
interface, offset, len);
exit(1);
}
if (retry_cnt > 1)
fprintf(stderr, "FTDI interface %c read timeout after %d / %d bytes, keep waiting..\n",
interface, offset, len);
usleep(100000);
continue;
}
if (rc < 0) {
fprintf(stderr, "Communication error. (ftdi interface %c read rc=%d, %s)\n",
interface, rc, ftdi_get_error_string(ftdi));
exit(1);
}
retry_cnt = 0;
offset += rc;
}
if (ftdi_verbose) {
printf("ftdi-recv-%c>", interface);
for (int i = 0; i < len; i++)
printf(" %02x", buffer[i]);
printf("\n");
}
}
void my_ftdi_setup(struct ftdi_context *ftdi, enum ftdi_interface interface, const char *ifname)
{
ftdi_init(ftdi);
ftdi_set_interface(ftdi, interface);
if (ftdi_usb_open(ftdi, 0x0403, 0x6010)) {
fprintf(stderr, "Can't find IcoBoard USB baseboard (vedor_id 0x0403, device_id 0x6010, interface %s).\n", ifname);
exit(1);
}
if (ftdi_usb_reset(ftdi)) {
fprintf(stderr, "Failed to reset IcoBoard USB baseboard (interface %s).\n", ifname);
exit(1);
}
if (ftdi_usb_purge_buffers(ftdi)) {
fprintf(stderr, "Failed to purge buffers on IcoBoard USB baseboard (interface %s).\n", ifname);
exit(1);
}
/* 1 is the fastest polling, it means 1 kHz polling */
if (ftdi_set_latency_timer(ftdi, 1) < 0) {
fprintf(stderr, "Failed to set latency timer (%s).\n", ftdi_get_error_string(ftdi));
exit(1);
}
if (ftdi_set_bitmode(ftdi, 0xff, BITMODE_MPSSE) < 0) {
fprintf(stderr, "Failed set BITMODE_MPSSE on IcoBoard USB baseboard (interface %s).\n", ifname);
exit(1);
}
std::vector<uint8_t> cmd;
// enable clock prescale divide by 5
cmd.push_back(0x8b);
// 6 MHz = 0
// 1 MHz = 5
// 100 kHz = 59
// 10 kHz = 599
int clkdiv = 0;
cmd.push_back(0x86);
cmd.push_back(clkdiv & 255);
cmd.push_back(clkdiv >> 8);
my_ftdi_send(ftdi, &cmd.front(), cmd.size());
}
void my_ftdi_wr(int pin)
{
struct ftdi_context *ftdi = &ftdia;
uint32_t new_dir = ftdistate_dir;
uint32_t new_val = ftdistate_val;
uint8_t opcode = 0x80;
if (pin >= 16) {
pin -= 16;
ftdi = &ftdib;
new_dir >>= 16;
new_val >>= 16;
}
if (pin >= 8) {
pin -= 8;
opcode = 0x82;
new_dir >>= 8;
new_val >>= 8;
}
uint8_t cmd[3] = {opcode, uint8_t(new_val), uint8_t(new_dir)};
my_ftdi_send(ftdi, cmd, 3);
}
void my_ftdi_pinmode(int pin, bool pinmode)
{
if (pinmode)
ftdistate_dir |= 1 << pin;
else
ftdistate_dir &= ~(1 << pin);
my_ftdi_wr(pin);
}
void my_ftdi_pinwrite(int pin, bool pinvalue)
{
if (pinvalue)
ftdistate_val |= 1 << pin;
else
ftdistate_val &= ~(1 << pin);
my_ftdi_wr(pin);
}
bool my_ftdi_pinread(int pin)
{
struct ftdi_context *ftdi = &ftdia;
uint8_t opcode = 0x81;
if (pin >= 16) {
pin -= 16;
ftdi = &ftdib;
}
if (pin >= 8) {
pin -= 8;
opcode = 0x83;
}
uint8_t data;
my_ftdi_send(ftdi, &opcode, 1);
my_ftdi_recv(ftdi, &data, 1);
return (data & (1 << pin)) != 0;
}
void wiringPiSetup()
{
my_ftdi_setup(&ftdia, INTERFACE_A, "A");
my_ftdi_setup(&ftdib, INTERFACE_B, "B");
ftdistate_dir = 0;
ftdistate_val = 0;
}
void pinMode(int pin, int dir)
{
my_ftdi_pinmode(pin, dir != INPUT);
}
void digitalWrite(int pin, int val)
{
my_ftdi_pinwrite(pin, val != LOW);
}
int digitalRead(int pin)
{
return my_ftdi_pinread(pin) ? HIGH : LOW;
}
void digitalSync(int usec_delay)
{
uint8_t request = 0x81;
uint8_t response_a, response_b;
my_ftdi_send(&ftdia, &request, 1);
my_ftdi_recv(&ftdia, &response_a, 1);
my_ftdi_send(&ftdib, &request, 1);
my_ftdi_recv(&ftdib, &response_b, 1);
usleep(usec_delay);
}
#define PININFO(pin) pininfo(#pin, pin);
void pininfo(const char *name, int pin)
{
int dir = (ftdistate_dir >> pin) & 1;
int val = (ftdistate_val >> pin) & 1;
if (!dir) val = digitalRead(pin);
fprintf(stderr, "%-20s %d %s\n", name, val, dir ? "OUT" : "IN");
}
#endif /* USBMODE */
// --------------------------------------------------------------------------------------------
#ifdef GPIOMODE
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
// Pinout for UP-board:
// https://up-community.org/wiki/Pinout
# define RPI_ICE_CLK 4 // PIN 7
# define RPI_ICE_CDONE 27 // PIN 13
# define RPI_ICE_MOSI 5 // PIN 29
# define RPI_ICE_MISO 6 // PIN 31
# define LOAD_FROM_FLASH 13 // PIN 33
# define RPI_ICE_CRESET 26 // PIN 37
# define RPI_ICE_CS 8 // PIN 24
# define RPI_ICE_SELECT 12 // PIN 32
# define RASPI_D8 17 // PIN 11
# define RASPI_D7 18 // PIN 12
# define RASPI_D6 22 // PIN 15
# define RASPI_D5 23 // PIN 16
# define RASPI_D4 10 // PIN 19
# define RASPI_D3 9 // PIN 21
# define RASPI_D2 7 // PIN 26
# define RASPI_D1 19 // PIN 35
# define RASPI_D0 16 // PIN 36
# define RASPI_DIR 20 // PIN 38
# define RASPI_CLK 21 // PIN 40
# define INPUT 0
# define OUTPUT 1
# define LOW 0
# define HIGH 1
int gpio_direction[32];
int gpio_value[32];
int gpio_direction_fds[32];
int gpio_value_fds[32];
void wiringPiSetup()
{
for (int i = 0; i < 32; i++) {
gpio_direction[i] = -1;
gpio_value[i] = -1;
gpio_direction_fds[i] = -1;
gpio_value_fds[i] = -1;
}
}
void my_write(int fd, const void *buffer, int n)
{
int rc = write(fd, buffer, n);
if (rc != n) {
fprintf(stderr, "Unexpected GPIO write error.\n");
exit(1);
}
}
void pinSetup(int pin)
{
if (gpio_direction_fds[pin] < 0)
{
char buffer[4096];
int f, n, rc;
f = open("/sys/class/gpio/export", O_WRONLY);
n = snprintf(buffer, 4096, "%d", pin);
rc = write(f, buffer, n);
if (rc != n && (rc != -1 || errno != EBUSY)) {
fprintf(stderr, "Unexpected GPIO export error.\n");
exit(1);
}
close(f);
snprintf(buffer, 4096, "/sys/class/gpio/gpio%d/direction", pin);
gpio_direction_fds[pin] = open(buffer, O_WRONLY);
snprintf(buffer, 4096, "/sys/class/gpio/gpio%d/value", pin);
gpio_value_fds[pin] = open(buffer, O_RDWR);
my_write(gpio_direction_fds[pin], "in\n", 3);
gpio_direction[pin] = 0;
gpio_value[pin] = 0;
}
}
void pinMode(int pin, int dir)
{
pinSetup(pin);
if (dir == INPUT)
{
if (gpio_direction[pin] == 0)
return;
my_write(gpio_direction_fds[pin], "in\n", 3);
gpio_direction[pin] = 0;
}
else
{
if (gpio_direction[pin] == 1)
return;
if (gpio_value[pin])
my_write(gpio_direction_fds[pin], "high\n", 5);
else
my_write(gpio_direction_fds[pin], "low\n", 4);
gpio_direction[pin] = 1;
}
}
void digitalWrite(int pin, int val)
{
pinSetup(pin);
gpio_value[pin] = (val != LOW);
if (gpio_direction[pin] == 1) {
if (gpio_value[pin])
my_write(gpio_value_fds[pin], "1", 1);
else
my_write(gpio_value_fds[pin], "0", 1);
}
}
int digitalRead(int pin)
{
pinSetup(pin);
if (gpio_direction[pin] == 0)
{
char buffer[4096];
int n;
lseek(gpio_value_fds[pin], 0, SEEK_SET);
n = read(gpio_value_fds[pin], buffer, 4095);
if (n > 0)
gpio_value[pin] = buffer[0] != '0';
}
return gpio_value[pin] ? HIGH : LOW;;
}
void digitalSync(int usec_delay)
{
usleep(usec_delay);
}
#endif /* GPIOMODE */
// --------------------------------------------------------------------------------------------
bool send_zero = false;
bool recv_zero = false;
char current_send_recv_mode = 0;
int current_recv_ep = -1;
int last_recv_v = -1;
int last_recv_rep = 0;
void fpga_reset()
{
pinMode(RPI_ICE_CRESET, OUTPUT);
digitalWrite(RPI_ICE_CRESET, LOW);
digitalSync(2000);
digitalWrite(RPI_ICE_CRESET, HIGH);
digitalSync(500000);
if (digitalRead(RPI_ICE_CDONE) != HIGH) {
fprintf(stderr, "Warning: cdone is low\n");
got_error = true;
}
}
int get_time_ms()
{
static struct timespec spec_start;
static bool spec_start_initialized = false;
struct timespec spec_now;
clock_gettime(CLOCK_REALTIME, &spec_now);
if (!spec_start_initialized) {
spec_start = spec_now;
spec_start_initialized = true;
}
int s = spec_now.tv_sec - spec_start.tv_sec;
int ns = spec_now.tv_nsec - spec_start.tv_nsec;
return s*1000 + ns/1000000;
}
void prog_bitstream(bool reset_only = false)
{
assert(enable_prog_port);
pinMode(RPI_ICE_CLK, OUTPUT);
pinMode(RPI_ICE_MOSI, OUTPUT);
pinMode(LOAD_FROM_FLASH, OUTPUT);
pinMode(RPI_ICE_CRESET, OUTPUT);
pinMode(RPI_ICE_CS, OUTPUT);
pinMode(RPI_ICE_SELECT, OUTPUT);
fprintf(stderr, "reset..\n");
// enable reset
digitalWrite(RPI_ICE_CRESET, LOW);
// start clock high
digitalWrite(RPI_ICE_CLK, HIGH);
// select SRAM programming mode
digitalWrite(LOAD_FROM_FLASH, LOW);
digitalWrite(RPI_ICE_SELECT, LOW);
digitalWrite(RPI_ICE_CS, LOW);
digitalSync(100);
// release reset
digitalWrite(RPI_ICE_CRESET, HIGH);
digitalSync(2000);
fprintf(stderr, "cdone: %s\n", digitalRead(RPI_ICE_CDONE) == HIGH ? "high" : "low");
if (reset_only)
return;
fprintf(stderr, "programming..\n");
for (int i = 0; i < 8; i++) {
digitalWrite(RPI_ICE_CLK, LOW);
digitalWrite(RPI_ICE_CLK, HIGH);
}
#ifndef USBMODE
for (int k = 0;; k++)
{
int byte = getchar();
if (byte < 0)
break;
for (int i = 7; i >= 0; i--) {
digitalWrite(RPI_ICE_MOSI, ((byte >> i) & 1) ? HIGH : LOW);
digitalWrite(RPI_ICE_CLK, LOW);
digitalWrite(RPI_ICE_CLK, HIGH);
}
if (verbose && !(k % 1024) && k)
printf("%3d kB written.\n", k / 1024);
}
for (int i = 0; i < 49; i++) {
digitalWrite(RPI_ICE_CLK, LOW);
digitalWrite(RPI_ICE_CLK, HIGH);
}
#else
std::vector<uint8_t> data;
int sent_cnt = 0;
while (1)
{
int byte = getchar();
if (byte < 0)
break;
data.push_back(byte);
}
while (sent_cnt < int(data.size()))
{
uint8_t *buffer = &data[sent_cnt];
int len = int(data.size()) - sent_cnt;
if (len > 1024)
len = 1024;
uint8_t cmd[4] = {0x11, uint8_t(len-1), uint8_t((len-1) >> 8)};
my_ftdi_send(&ftdib, cmd, 3);
my_ftdi_send(&ftdib, buffer, len);
sent_cnt += len;
}
// send 49 additional dummy bits
uint8_t cmd[5] = {0x8f, 0x05, 0x00, 0x8e, 0x00};
my_ftdi_send(&ftdib, cmd, 5);
#endif
digitalSync(2000);
#if 0
for (int i = 2; i <= 512; i+=2) {
digitalSync(2000);
if (((i-1) & i) == 0)
fprintf(stderr, "cdone (after %3d ms): %s\n", i, digitalRead(RPI_ICE_CDONE) == HIGH ? "high" : "low");
}
#else
bool cdone_high = digitalRead(RPI_ICE_CDONE) == HIGH;
fprintf(stderr, "cdone: %s\n", cdone_high ? "high" : "low");
if (!cdone_high) got_error = true;
#endif
}
void spi_begin()
{
digitalWrite(RPI_ICE_CS, LOW);
// fprintf(stderr, "SPI_BEGIN\n");
}
void spi_end()
{
digitalWrite(RPI_ICE_CS, HIGH);
// fprintf(stderr, "SPI_END\n");
}
uint32_t spi_xfer(uint32_t data, int nbits = 8)
{
assert(enable_prog_port);
#ifndef USBMODE
uint32_t rdata = 0;
for (int i = nbits-1; i >= 0; i--)
{
digitalWrite(RPI_ICE_MOSI, (data & (1 << i)) ? HIGH : LOW);
digitalSync(1);
if (digitalRead(RPI_ICE_MISO) == HIGH)
rdata |= 1 << i;
digitalWrite(RPI_ICE_CLK, HIGH);
digitalWrite(RPI_ICE_CLK, LOW);
}
// fprintf(stderr, "SPI:%d %02x %02x\n", nbits, data, rdata);
return rdata;
#else
assert(nbits <= 8);
uint8_t cmd[3] = {0x33, uint8_t(nbits-1), uint8_t(data)};
uint8_t rdata;
my_ftdi_send(&ftdib, cmd, 3);
my_ftdi_recv(&ftdib, &rdata, 1);
// fprintf(stderr, "SPI:%d %02x %02x\n", nbits, data, rdata);
return rdata;
#endif
}
void flash_write_enable()
{
spi_begin();
spi_xfer(0x06);
spi_end();
}
void flash_bulk_erase()
{
spi_begin();
spi_xfer(0xc7);
spi_end();
}
void flash_erase_64kB(int addr)
{
spi_begin();
spi_xfer(0xd8);
spi_xfer(addr >> 16);
spi_xfer(addr >> 8);
spi_xfer(addr);
spi_end();
}
void flash_write(int addr, uint8_t *data, int n)
{
spi_begin();
spi_xfer(0x02);
spi_xfer(addr >> 16);
spi_xfer(addr >> 8);
spi_xfer(addr);
#ifndef USBMODE
while (n--)
spi_xfer(*(data++));
#else
assert(n <= 64*1024);
uint8_t cmd[3] = {0x11, uint8_t(n-1), uint8_t((n-1) >> 8)};
my_ftdi_send(&ftdib, cmd, 3);
my_ftdi_send(&ftdib, data, n);
#endif
spi_end();
}
void flash_read(int addr, uint8_t *data, int n)
{
spi_begin();
spi_xfer(0x03);
spi_xfer(addr >> 16);
spi_xfer(addr >> 8);
spi_xfer(addr);
#ifndef USBMODE
while (n--)
*(data++) = spi_xfer(0);
#else
assert(n <= 64*1024);
uint8_t cmd[3] = {0x24, uint8_t(n-1), uint8_t((n-1) >> 8)};
my_ftdi_send(&ftdib, cmd, 3);
my_ftdi_recv(&ftdib, data, n);
#endif
spi_end();
}
int flash_wait()
{
int ms_start = get_time_ms();
while (1)
{
spi_begin();
spi_xfer(0x05);
int status = spi_xfer(0);
spi_end();
if ((status & 0x01) == 0)
break;
digitalSync(1000);
}
return get_time_ms() - ms_start;
}
void prog_flasherase()
{
assert(enable_prog_port);
pinMode(RPI_ICE_CLK, OUTPUT);
pinMode(RPI_ICE_MOSI, OUTPUT);
pinMode(LOAD_FROM_FLASH, OUTPUT);
pinMode(RPI_ICE_CS, OUTPUT);
pinMode(RPI_ICE_SELECT, OUTPUT);
// connect flash to Raspi
digitalWrite(LOAD_FROM_FLASH, LOW);
digitalWrite(RPI_ICE_SELECT, HIGH);
digitalWrite(RPI_ICE_CS, HIGH);
digitalWrite(RPI_ICE_CLK, LOW);
digitalSync(100);
// power_up
spi_begin();
spi_xfer(0xab);
spi_end();
flash_write_enable();
flash_bulk_erase();
// power_down
spi_begin();
spi_xfer(0xb9);
spi_end();
}
void prog_flashmem(int pageoffset, bool erase_first_block)
{
assert(enable_prog_port);
pinMode(RPI_ICE_CLK, OUTPUT);
pinMode(RPI_ICE_MOSI, OUTPUT);
pinMode(LOAD_FROM_FLASH, OUTPUT);
pinMode(RPI_ICE_CS, OUTPUT);
pinMode(RPI_ICE_SELECT, OUTPUT);
// connect flash to Raspi
digitalWrite(LOAD_FROM_FLASH, LOW);
digitalWrite(RPI_ICE_SELECT, HIGH);
digitalWrite(RPI_ICE_CS, HIGH);
digitalWrite(RPI_ICE_CLK, LOW);
digitalSync(100);
// power_up
spi_begin();
spi_xfer(0xab);
spi_end();
// read flash id
spi_begin();
spi_xfer(0x9f);
fprintf(stderr, "flash id:");
for (int i = 0; i < 20; i++)
fprintf(stderr, " %02x", spi_xfer(0x00));
fprintf(stderr, "\n");
spi_end();
// load prog data into buffer
std::vector<uint8_t> prog_data;
if (erase_first_block)
{
prog_data.push_back(0);
}
else
{
while (1) {
int byte = getchar();
if (byte < 0)
break;
prog_data.push_back(byte);
}
fprintf(stderr, "writing %.2fkB..", double(prog_data.size()) / 1024);
}
int ms_timer = 0;
for (int addr = 0; addr < int(prog_data.size()); addr += 256)
{
if (addr % (64*1024) == 0)
{
if (erase_first_block) {
fprintf(stderr, "erasing 64kB sector @%06x..", addr);
} else {
fprintf(stderr, "\n%3d%% @%06x ", 100*addr/int(prog_data.size()), addr);
fprintf(stderr, "erasing 64kB sector..");
}
flash_write_enable();
flash_erase_64kB(addr + pageoffset * 0x10000);
ms_timer += flash_wait();
if (erase_first_block)
break;
}
if (addr % (32*256) == 0) {
fprintf(stderr, "\n%3d%% @%06x writing: ", 100*addr/int(prog_data.size()), addr);
}
int n = std::min(256, int(prog_data.size()) - addr);
uint8_t buffer[256];
for (int retry_count = 0; retry_count < 100; retry_count++)
{
flash_write_enable();
flash_write(addr + pageoffset * 0x10000, &prog_data[addr], n);
ms_timer += flash_wait();
flash_read(addr + pageoffset * 0x10000, buffer, n);
if (!memcmp(buffer, &prog_data[addr], n)) {
fprintf(stderr, "o");
goto written_ok;
}
fprintf(stderr, "X");
}
// restart erasing and writing this 64kB sector
addr -= addr % (64*1024);
addr -= 256;
written_ok:;
}
fprintf(stderr, "\n%stotal wait time: %d ms\n", erase_first_block ? "" : "100% ", ms_timer);
// power_down
spi_begin();
spi_xfer(0xb9);
spi_end();
}
void read_flashmem(int n)
{
assert(enable_prog_port);
pinMode(RPI_ICE_CLK, OUTPUT);
pinMode(RPI_ICE_MOSI, OUTPUT);
pinMode(LOAD_FROM_FLASH, OUTPUT);
pinMode(RPI_ICE_CS, OUTPUT);
pinMode(RPI_ICE_SELECT, OUTPUT);
// connect flash to Raspi
digitalWrite(LOAD_FROM_FLASH, LOW);
digitalWrite(RPI_ICE_SELECT, HIGH);
digitalWrite(RPI_ICE_CS, HIGH);
digitalWrite(RPI_ICE_CLK, LOW);
digitalSync(100);
// power_up
spi_begin();
spi_xfer(0xab);
spi_end();
// read flash id
spi_begin();
spi_xfer(0x9f);
fprintf(stderr, "flash id:");
for (int i = 0; i < 20; i++)
fprintf(stderr, " %02x", spi_xfer(0x00));
fprintf(stderr, "\n");
spi_end();
if (n > 0)
fprintf(stderr, "reading %.2fkB..\n", double(n) / 1024);
for (int addr = 0; addr < n; addr += 256) {
uint8_t buffer[256];
flash_read(addr, buffer, std::min(256, n - addr));
fwrite(buffer, std::min(256, n - addr), 1, stdout);
}
// power_down
spi_begin();
spi_xfer(0xb9);
spi_end();
}
void epsilon_sleep()
{
#ifndef USBMODE
for (int i = 0; i < 1000; i++)
asm volatile ("");
#endif
}
void send_word(int v)
{
assert(enable_data_port);
#ifndef USBMODE
if (current_send_recv_mode != 's')
{
digitalWrite(RASPI_DIR, HIGH);
epsilon_sleep();
pinMode(RASPI_D8, OUTPUT);