-
Notifications
You must be signed in to change notification settings - Fork 1
/
radiantBoardManager.ino
executable file
·1000 lines (883 loc) · 30.4 KB
/
radiantBoardManager.ino
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 <PacketSerial.h>
#ifndef RADIANT_SAMPLE_RATE
#define RADIANT_SAMPLE_RATE 2400
#endif
#pragma message( "Sample Rate is " $RADIANT_SAMPLE_RATE " MHz");
#include <SPI.h>
// SPISettings for attenuator (0x24 write)
SPISettings settingsAtten(4000000, LSBFIRST, SPI_MODE0);
// SPISettings for ADF4351 (0x28 write)
SPISettings settingsSigGen(4000000, MSBFIRST, SPI_MODE0);
#include <Wire.h>
#define VER_MAJOR 0
#define VER_MINOR 2
#define VER_REV 19
#define VER_ENC ( ((VER_MAJOR & 0xF) << 12) | ((VER_MINOR & 0xF) << 8) | (VER_REV & 0xFF))
// these need to be automated, but it's a pain in the ass
#define DATE_MONTH 9
#define DATE_DAY 13
#define DATE_YEAR 23
#define DATE_ENC (((DATE_YEAR & 0x7F) << 9) | ((DATE_MONTH & 0xF) << 5) | (DATE_DAY & 0x1F))
const uint32_t ident = 'RDBM';
const uint32_t ver = ((DATE_ENC << 16) | (VER_ENC));
#ifdef _VARIANT_RADIANT_V3_
#warning "RadiantV3 build"
#define I2C_CLOCK 0x70
#define I2C_GPBASE 0x38
#define VARIANT_RADIANT 3
#endif
#ifdef _VARIANT_RADIANT_V2_
#warning "RadiantV2 build"
#define I2C_CLOCK 0x70
#define I2C_GPBASE 0x38
#define VARIANT_RADIANT 2
#endif
#ifdef _VARIANT_RADIANT_V1_
#warning "RadiantV1 build"
#define I2C_CLOCK 0x71
#define I2C_GPBASE 0x20
#define VARIANT_RADIANT 1
#endif
#define I2C_DAC_BASE 0x60
const uint8_t i2c_gp[7] = {
I2C_GPBASE | 0x0,
I2C_GPBASE | 0x4,
I2C_GPBASE | 0x2,
I2C_GPBASE | 0x6,
I2C_GPBASE | 0x1,
I2C_GPBASE | 0x5,
I2C_GPBASE | 0x3
};
const uint32_t samd_uid_addr[4] = { 0x0080A00C, 0x0080A040, 0x00800A044, 0x00800A048};
// NOTE PACKET FORMAT
// the 'raw' output packet format is
// addr0 addr1 addr2 (len or data)
// addr0 bit 7 indicates write (if 1) or read (if 0)
// addr0 bit 6 indicates board manager (if 1) or FPGA (if 0)
//
uint32_t control_reg = 0;
#define DSTART() while(!SerialUSB)
#define DPRINT(...) SerialUSB.print( __VA_ARGS__ )
#define DPRINTLN(...) SerialUSB.println( __VA_ARGS__ )
// Our peripherals are:
// SERCOM1 CB UART (PA30/PA31)
// The default SAMD21 Xplained Pro peripherals are:
// Serial (sercom3)
COBSPacketSerial cbIf;
COBSPacketSerial usbIf;
// This is really the OUTBOUND version
COBSPacketSerial fpIf;
bool usbActive = false;
// as of version 2.16, no longer read clocks from SPI flash but instead build it into the binary.
// It almost certainly takes up less space than the code to read from the SPI Flash and that's all it's needed for
#warning Using RADIANT_SAMPLE_RATE
#define STRINGIFY_HELPER(x) #x
#define STRINGIFY(x) STRINGIFY_HELPER(x)
#define CLOCK_PATH STRINGIFY(clocks/clocks_ RADIANT_SAMPLE_RATE.h)
#include CLOCK_PATH
uint8_t clockR(uint8_t addr) {
Wire.beginTransmission(I2C_CLOCK);
Wire.write(addr);
if (Wire.endTransmission() != 0) return 0;
Wire.requestFrom(I2C_CLOCK, 1);
if (!Wire.available()) return 0;
return Wire.read();
}
int clockRMW(uint8_t addr, uint8_t reg, uint8_t mask) {
uint8_t curval;
if (mask != 0xFF) {
Wire.beginTransmission(I2C_CLOCK);
Wire.write(addr);
if (Wire.endTransmission() != 0) return -1;
Wire.requestFrom(I2C_CLOCK, 1);
if (!Wire.available()) return -1;
curval = Wire.read();
curval &= ~mask;
curval = curval | (reg & mask);
} else {
curval = reg;
}
Wire.beginTransmission(I2C_CLOCK);
Wire.write(addr);
Wire.write(curval);
if (Wire.endTransmission() != 0) return -1;
return 0;
}
int clockConfigure(char *fn) {
char buf[6];
// start in page 0
clockRMW(0xFF, 0x0, 0xFF);
// disable outputs
clockRMW(230, 0x10, 0x10);
// pause LOL
clockRMW(241, 0x80, 0xFF);
// Run the configuration procedure as outlined in the included header file
for (int iaddr = 0; iaddr < NCLOCK_REGS; iaddr++)
{
uint8_t addr = CLOCK_REGS[iaddr][0];
uint8_t reg = CLOCK_REGS[iaddr][1];
uint8_t mask = CLOCK_REGS[iaddr][2];
clockRMW(addr, reg, mask);
}
// validate input clock status
while (clockR(218) & 0x4);
clockRMW(49, 0x00, 0x80);
clockRMW(246, 0x2, 0x2);
delay(25);
clockRMW(241, 0x65, 0xFF);
while (clockR(218) & 0x11);
uint8_t val = clockR(237);
val &= 0x3;
clockRMW(47, val, 0x3);
val = clockR(236);
clockRMW(46, val, 0xFF);
val = clockR(235);
clockRMW(45, val, 0xFF);
clockRMW(47, 0x14, 0xFC);
clockRMW(49, 0x80, 0x80);
clockRMW(230, 0x00, 0x10);
return 0;
}
//COBSPacketSerial bbSerial;
uint16_t myLoop;
#define BM_ERR_STARTUP_I2C 1
#define BM_ERR_STARTUP_PGV10 2
#define BM_ERR_STARTUP_PGV18 3
#define BM_ERR_STARTUP_PGV25 4
#define BM_ERR_STARTUP_PGV26 5
#define BM_ERR_STARTUP_PGV31 6
#define BM_ERR_STARTUP_CLOCK 7
#define BM_ERR_STARTUP_SPI 8
#define BM_ERR_STARTUP_FAT 9
const uint8_t diedieHeader[4] = { 0x5, 0xff, 0xff, 0xff };
void diedie(uint8_t errcode) {
// need to let the CB know, probably a repeating packet.
// talk to Cosmin about this
// all diedies happen before starting serial
Serial.begin(1000000);
while (1) {
for (uint8_t i=0;i<sizeof(diedieHeader);i++)
Serial.write(diedieHeader[i]);
Serial.write(errcode);
Serial.write((uint8_t) 0x0);
delay(50);
DPRINT("DIEDIE: ");
DPRINTLN(errcode);
digitalWrite(PIN_LED, LOW);
digitalWrite(PIN_LED2, HIGH);
delay(50);
digitalWrite(PIN_LED, HIGH);
digitalWrite(PIN_LED2, LOW);
}
}
// these guys are ALWAYS I/Os. The bootloader forces them.
#define GET_ENV10() (PORT->Group[PORTB].IN.reg & PORT_PB02)
#define GET_ENV18() (PORT->Group[PORTA].IN.reg & PORT_PA04)
#define GET_ENV25() (PORT->Group[PORTA].IN.reg & PORT_PA06)
#define GET_ENV26() (PORT->Group[PORTA].IN.reg & PORT_PA02)
#define GET_ENV31() (PORT->Group[PORTB].IN.reg & PORT_PB08)
#define SET_ENV10() PORT->Group[PORTB].OUTSET.reg = PORT_PB02
#define SET_ENV18() PORT->Group[PORTA].OUTSET.reg = PORT_PA04
#define SET_ENV25() PORT->Group[PORTA].OUTSET.reg = PORT_PA06
#define SET_ENV26() PORT->Group[PORTA].OUTSET.reg = PORT_PA02
#define SET_ENV31() PORT->Group[PORTB].OUTSET.reg = PORT_PB08
#define CLR_ENV10() PORT->Group[PORTB].OUTCLR.reg = PORT_PB02
#define CLR_ENV18() PORT->Group[PORTA].OUTCLR.reg = PORT_PA04
#define CLR_ENV25() PORT->Group[PORTA].OUTCLR.reg = PORT_PA06
#define CLR_ENV26() PORT->Group[PORTA].OUTCLR.reg = PORT_PA02
#define CLR_ENV31() PORT->Group[PORTB].OUTCLR.reg = PORT_PB08
// These are the powergood pins
#define PGV10 8
#define PGV18 14
#define PGV25 15
#define PGV26 12
#define PGV31 13
#define MGTDET 16
#define FPGA_DONE 25
#define SD_DETECT 23
#define BM_EN_10MHZ 22
#define BMGPIO2 33
#define JTAG_TDO 29
#define JTAG_TDI 30
#define JTAG_TMS 31
#define JTAG_TCK 32
#define FPGA_PROGB 21
// new features, disabled by default for now (can enable at compile time if you want)
//#define ENABLE_INTERRUPT_HISTORY
//#define ENABLE_TIMER_MODE
// control bits
#define CONTROL_FPGA_PROGRAM 0x100
#define CONTROL_JTAGEN 0x200
#define CONTROL_JTAG_TCK 0x10000
#define CONTROL_JTAG_TMS 0x20000
#define CONTROL_JTAG_TDI 0x40000
#define CONTROL_JTAG_TDO 0x80000
#define CONTROL_JTAG_MASK (CONTROL_JTAG_TCK | CONTROL_JTAG_TDI | CONTROL_JTAG_TMS)
#define CONTROL_COBS_FLUSH (0x1 << 31 )
#ifdef ENABLE_TIMER_MODE
int timer_mode_pin = BMGPIO2;
int in_timer_mode = 0;
void enterTimerMode();
#endif
void setupSerial();
#ifdef ENABLE_INTERRUPT_HISTORY
#define INTERRUPT_HISTORY_SIZE 16
struct
{
uint32_t counter;
uint32_t when;
union
{
struct
{
uint32_t when_high : 14; //top 14 bits of when
uint32_t pin : 6;
uint32_t state : 12;
} bits;
uint32_t u;
} payload;
} interrupt_history[INTERRUPT_HISTORY_SIZE];
volatile uint32_t interrupt_counter = 0;
volatile int must_fill_interrupt_times = 0;
// xmacro for interrupt pins
#define INTERRUPT_PINS \
X(PGV10) \
X(PGV25) \
X(PGV26) \
X(PGV18) \
X(PGV31) \
//hopefuly this isn't too much for an ISR...
#define X(pinnum) \
void interrupt_fn_##pinnum() \
{ \
must_fill_interrupt_times=1; \
uint32_t counter = (interrupt_counter++); \
int which = counter % INTERRUPT_HISTORY_SIZE; \
interrupt_history[which].when = 0; \
interrupt_history[which].payload.bits.when_high = 0; \
interrupt_history[which].payload.bits.pin = pinnum; \
interrupt_history[which].payload.bits.state = digitalRead(pinnum); \
interrupt_history[which].counter = counter; \
}
INTERRUPT_PINS
#undef X
#endif
void setup() {
// The powergoods all need pullups.
pinMode(PGV10, INPUT_PULLUP);
pinMode(PGV18, INPUT_PULLUP);
pinMode(PGV25, INPUT_PULLUP);
pinMode(PGV26, INPUT_PULLUP);
pinMode(PGV31, INPUT_PULLUP);
// other pullups
pinMode(MGTDET, INPUT_PULLUP);
// SD detect switch shorts G1<->P4 (SD_DETECT to VCC).
// So we pulldown here.
pinMode(SD_DETECT, INPUT_PULLDOWN);
// enable clock by default
digitalWrite(BM_EN_10MHZ, HIGH);
pinMode(BM_EN_10MHZ, OUTPUT);
// LEDs
digitalWrite(PIN_LED, LOW);
digitalWrite(PIN_LED2, LOW);
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_LED2, OUTPUT);
// analog read resolution
analogReadResolution(16);
SerialUSB.begin(9600);
// hold up 2 seconds to allow USB initialization
delay(2000);
DPRINTLN("RADIANT: Startup.");
////////////////////////////////////////////////////
//
// Start the Wire periph.
//
////////////////////////////////////////////////////
Wire.begin();
////////////////////////////////////////////////////
SPI1.begin();
// Re-convert the MISO pin back to an output (low)
// because VINMON never got connected (... whoops).
digitalWrite(PIN_SPI1_MISO, LOW);
pinMode(PIN_SPI1_MISO, OUTPUT);
////////////////////////////////////////////////////
// So how can we figure out if we've never been powered on before?
// Easy: just read the I2C GPIO registers for GP6, which is the *last stage* in the configuration.
//
// However, for debugging, we want an ALTERNATE way to force it.
// So what we do here is look to see if SerialUSB is *already open*. If it is, we go ahead and do
// everything *anyway*.
//
// So if you do *not* want initial board setup to occur (but still want to use SerialUSB, for example)
// go ahead and make sure to *close* the serial terminal for ~5 seconds after power on.
//
Wire.beginTransmission(i2c_gp[6]);
// Check the CONFIGURATION register.
Wire.write(0x3);
if (Wire.endTransmission() != 0) diedie(BM_ERR_STARTUP_I2C);
Wire.requestFrom(i2c_gp[6], 1);
if (Wire.available()) {
uint8_t c = Wire.read();
if (c == 0xFF || SerialUSB) {
// INITIAL POWERON STARTUP
//
// NOTE: In this version we start up everything automatically,
// rather than starting "quiet." Later we'll use one of the GPIOs
// to indicate "dude start low power plz"
//
DPRINTLN("RADIANT: Doing initial power-on sequence.");
// 1: Turn on the FPGA supplies, in sequence, waiting for powergood on each.
SET_ENV10();
// Wait 5 milliseconds: the soft-start ramp should only be 0.7 ms.
delay(5);
if (!digitalRead(PGV10)) diedie(BM_ERR_STARTUP_PGV10);
SET_ENV18();
// Wait 5 milliseconds: soft-start ramp is only 1 ms
delay(5);
if (!digitalRead(PGV18)) diedie(BM_ERR_STARTUP_PGV18);
SET_ENV25();
// Wait 5 milliseconds: soft start ramp is only 1 ms
delay(5);
if (!digitalRead(PGV25)) diedie(BM_ERR_STARTUP_PGV25);
// 2: Turn on the LAB4/trigger supplies...
SET_ENV26();
// Wait 5 milliseconds, soft start ramp is <1 ms
delay(5);
if (!digitalRead(PGV26)) diedie(BM_ERR_STARTUP_PGV26);
SET_ENV31();
delay(5);
if (!digitalRead(PGV31)) diedie(BM_ERR_STARTUP_PGV31);
// 3: Set up the clock.
if (clockConfigure("intclock25.dat")) diedie(BM_ERR_STARTUP_CLOCK);
// 4: Set up the LAB4 GPIOs. If we're a RADIANTv1 we need to enable pulls
// first.
for (unsigned int i=0;i<6;i++) {
#ifdef _VARIANT_RADIANT_V1_
Wire.beginTransmission(i2c_gp[i]);
Wire.write(0x4);
Wire.write(0xC0);
if (Wire.endTransmission() != 0) diedie(BM_ERR_STARTUP_I2C);
#endif
Wire.beginTransmission(i2c_gp[i]);
Wire.write(0x0);
if (Wire.endTransmission() != 0) diedie(BM_ERR_STARTUP_I2C);
Wire.requestFrom(i2c_gp[i], 1);
if (!Wire.available()) diedie(BM_ERR_STARTUP_I2C);
c = Wire.read();
// Copy bits 7 and 6 to bits 5 and 4.
c &= 0xC0;
c >>= 2;
// Light the green LED.
c |= 0x8;
Wire.beginTransmission(i2c_gp[i]);
Wire.write(0x1);
Wire.write(c);
if (Wire.endTransmission() != 0) diedie(BM_ERR_STARTUP_I2C);
Wire.beginTransmission(i2c_gp[i]);
Wire.write(0x3);
// Drive everything except the top bits.
Wire.write(0xC0);
if (Wire.endTransmission() != 0) diedie(BM_ERR_STARTUP_I2C);
}
// Step 5: Set up the signal generator GPIO.
// This one indicates that we're done.
// It also needs to be set up a little weird, since it's got complementary pairs.
// All outputs except bit 7.
// x010_1000 = 0x28
Wire.beginTransmission(i2c_gp[6]);
Wire.write(1);
Wire.write(0x28);
if (Wire.endTransmission() != 0) diedie(BM_ERR_STARTUP_I2C);
Wire.beginTransmission(i2c_gp[6]);
Wire.write(3);
#ifdef _VARIANT_RADIANT_V3_
Wire.write(0x0);
#else
Wire.write(0x80);
#endif
if (Wire.endTransmission() != 0) diedie(BM_ERR_STARTUP_I2C);
} else {
// NO INITIAL POWERON STARTUP
DPRINTLN("RADIANT: Skipping initial poweron.");
}
} else diedie(BM_ERR_STARTUP_I2C);
#ifdef ENABLE_INTERRUPT_HISTORY
// set up interrupts on PG pins
#define X(pin) attachInterrupt(digitalPinToInterrupt(pin), interrupt_fn_##pin, CHANGE);
INTERRUPT_PINS
#undef X
#endif
#ifdef ENABLE_TIMER_MODE
//check for timerMode
if (digitalRead(timer_mode_pin))
{
enterTimerMode();
//don't set up UART
}
else
{
setupSerial();
}
#else
setupSerial();
#endif
}
void setupSerial()
{
Serial.begin(1000000);
Serial1.begin(1000000);
// fp is the FPGA. Reset its handler.
for (uint8_t i=0;i<4;i++)
Serial1.write((byte)0x00);
cbIf.setStream(&Serial);
cbIf.setPacketHandler(&onCbPacketReceived);
usbIf.setStream(&SerialUSB);
usbIf.setPacketHandler(&onCbPacketReceived);
// No need to set handler, I'm never going to call update.
fpIf.setStream(&Serial1);
if (SerialUSB) usbActive = true;
DPRINTLN("RADIANT: startup complete.");
}
unsigned long time_now = 0;
uint16_t time_overflow_counter = 0;
int period = 1000;
bool gpioHigh = false;
void loop() {
#ifdef ENABLE_TIMER_MODE
if (in_timer_mode)
{
// go back to normal mode if the timer mode pin is down
if (!digitalRead(timer_mode_pin))
{
in_timer_mode = 0;
setupSerial();
}
return;
}
#endif
// Inbound packets.
cbIf.update();
if (usbActive) usbIf.update();
// Outbound bridge. Here we *can* avoid blocking: in *our* path we can't.
// Weird shit might happen if CB tries to throw packets freely in flight, so like,
// don't do that.
int fpAvailable = Serial1.available();
if (fpAvailable) {
int cbWriteSpace = Serial.availableForWrite();
if (usbActive) {
int usbWriteSpace = SerialUSB.availableForWrite();
if (usbWriteSpace < cbWriteSpace) cbWriteSpace = usbWriteSpace;
}
int toBridge = (cbWriteSpace < fpAvailable) ? cbWriteSpace : fpAvailable;
for (int i=0;i<toBridge;i++) {
uint8_t ch = Serial1.read();
Serial.write(ch);
if (usbActive) SerialUSB.write(ch);
}
}
unsigned long when = millis();
// poll every second to see if the USB's still there
if (when - time_now > period) {
if (SerialUSB) usbActive = true;
else usbActive = false;
if (when < time_now) time_overflow_counter++;
time_now = when;
if (gpioHigh) {
digitalWrite(PIN_LED, LOW);
gpioHigh = false;
} else {
digitalWrite(PIN_LED, HIGH);
gpioHigh = true;
}
}
#ifdef ENABLE_INTERRUPT_HISTORY
if (must_fill_interrupt_times)
{
must_fill_interrupt_times = 0; // there is a small race condition here, but I think it doesn't matter
// and there are no atomic ops on the Cortex-M0 anyway
for (int i = 0; i < INTERRUPT_HISTORY_SIZE; i++)
{
if (interrupt_history[i].payload.bits.pin && !interrupt_history[i].when && !interrupt_history[i].payload.bits.when_high)
interrupt_history[i].when = when;
interrupt_history[i].payload.bits.when_high = time_overflow_counter & 0x3fff;
}
}
#endif
}
uint32_t getStatus() {
uint32_t resp;
resp = 0;
if (digitalRead(FPGA_DONE)) resp |= 0x1;
if (digitalRead(MGTDET)) resp |= 0x2;
if (digitalRead(SD_DETECT)) resp |= 0x4;
if (digitalRead(PGV10)) resp |= 0x8;
if (digitalRead(PGV18)) resp |= 0x10;
if (digitalRead(PGV25)) resp |= 0x20;
if (digitalRead(PGV26)) resp |= 0x40;
if (digitalRead(PGV31)) resp |= 0x80;
// done for now
return resp;
}
uint8_t tempBuffer[256];
// I need to figure out a way to speed up the serial forwarding here.
// Editing the PacketSerial handler should work since we can tell
// if the packet's for us by either the first (if it's 01) or
// second byte (if the first byte's not 01, if the next byte
// does not have bit 6 set, it's not for us).
// That means the overall delay is only increased by 1-2 bytes.
//
// On the return side I shouldn't even be running the handler.
// Just forward every damn byte.
uint8_t zeroPacketCount = 0;
void onCbPacketReceived(const uint8_t *buffer, size_t size) {
// Zero-size packets are special.
if (!size) {
zeroPacketCount++;
// We do this at *three*, because the first one might've
// been eaten identifying a short packet.
// So imagine you write, and die. Then start up.
// First 0 you send kicks out attempting to parse a packet.
// You send 3 more.
// Then this trips and we reset.
if (zeroPacketCount == 3) {
Serial1.write((byte) 0x00);
Serial1.write((byte) 0x00);
Serial1.write((byte) 0x00);
Serial1.write((byte) 0x00);
}
return;
}
zeroPacketCount = 0;
// Packets have to be at least 4 bytes: addr addr addr data (on write).
if (size < 4) return;
if (!(buffer[0] & 0x40)) {
memcpy(tempBuffer, buffer, size);
// not for us
if (control_reg & 0x8) tempBuffer[0] |= 0x40;
fpIf.send(tempBuffer, size);
} else {
// interpret it, build response, send back
uint32_t addr;
bool wr;
if (buffer[0] & 0x80) wr = true; else wr = false;
// Build address. We ONLY work with 32-bit addresses here.
// CB board just... shouldn't send anything else.
addr = ((buffer[0] & 0x3F) << 16) |
(buffer[1] << 8) |
(buffer[2] & 0xFC);
addr >>= 2;
if (!wr) {
uint32_t rsp;
switch(addr) {
// ID
case 0: rsp = ident; break;
// VER
case 1: rsp = ver; break;
// STATUS
case 2: rsp = getStatus(); break;
case 3: rsp = control_reg;
if (control_reg & CONTROL_JTAGEN)
{
if (digitalRead(JTAG_TDO)) rsp |= CONTROL_JTAG_TDO;
}
break;
// Analogs
case 4: rsp = analogRead(A0); break;
case 5: rsp = analogRead(A1); break;
case 6: rsp = analogRead(A2); break;
case 7: rsp = analogRead(A3); break;
case 8: rsp = analogRead(A4); break;
// SPI output (no readback)
case 9: rsp = 0; break;
// SAMD ID
case 12:
case 13:
case 14:
case 15:
rsp = *((volatile uint32_t*)samd_uid_addr[addr-12]); break;
break;
case 16:
case 17:
case 18:
case 19:
case 20:
case 21:
case 22:
Wire.beginTransmission(i2c_gp[(addr-16)]);
// select the input register
Wire.write(0);
if (Wire.endTransmission() == 0) {
Wire.requestFrom(i2c_gp[(addr-16)], 1);
if (Wire.available()) rsp = Wire.read();
else rsp = 0xFFFFFFFF;
} else rsp = 0xFFFFFFFF;
break;
case 23:
rsp=VARIANT_RADIANT;
break;
// DACs don't have readback
case 58:
rsp = millis();
break;
case 59:
rsp = millis() < time_now ? time_overflow_counter+1 : time_overflow_counter;
break;
case 60:
rsp = RADIANT_SAMPLE_RATE;
break;
#ifdef ENABLE_INTERRUPT_HISTORY
case 64: case 67: case 70: case 73:
case 76: case 79: case 82: case 85:
case 88: case 91: case 94: case 97:
case 100: case 103: case 106: case 109:
rsp = interrupt_history[(addr-64)/3].counter;
break;
case 65: case 68: case 71: case 74:
case 77: case 80: case 83: case 86:
case 89: case 92: case 95: case 98:
case 101: case 104: case 107: case 110:
rsp = interrupt_history[(addr-64)/3].when;
break;
case 66: case 69: case 72: case 75:
case 78: case 81: case 84: case 87:
case 90: case 93: case 96: case 99:
case 102: case 105: case 108: case 111:
rsp = interrupt_history[(addr-64)/3].payload.u;
break;
#endif
default:
rsp = 0;
}
tempBuffer[0] = buffer[0];
tempBuffer[1] = buffer[1];
tempBuffer[2] = buffer[2];
tempBuffer[3] = rsp & 0xFF;
tempBuffer[4] = (rsp >> 8) & 0xFF;
tempBuffer[5] = (rsp >> 16) & 0xFF;
tempBuffer[6] = (rsp >> 24) & 0xFF;
cbIf.send(tempBuffer, 7);
if (usbActive) usbIf.send(tempBuffer, 7);
} else {
uint32_t val;
uint8_t quad;
uint8_t ch;
val = buffer[3];
if (size > 4) val |= buffer[4] << 8;
if (size > 5) val |= buffer[5] << 16;
if (size > 6) val |= buffer[6] << 24;
switch(addr) {
// 0, 1, and 2 are read-only
// Control is harder. For now I'm only capturing the burst bit.
// I'm not convinced I'm going to keep the "blow things up" bits here anyway.
// sigh, we need the 'blow things up' bits.
// OK, so here we go.
// Bit 8 = FPGA_PROGRAM (inverted FPGA_PROGRAM_B)
// Bit 9 = JTAGEN
// Bit 16 = TCK (when JTAGEN)
// Bit 17 = TMS (when JTAGEN)
// Bit 18 = TDI (when JTAGEN)
// Bit 19 = TDO (when JTAGEN)
// BIT 31 = FPGA COBS FLUSH
case 3: control_reg &= ~0x8;
control_reg |= val & 0x8;
// handle PROG_B. If it's currently set,
// check to see if we release. If it's desired to set,
// drive it.
if (control_reg & CONTROL_FPGA_PROGRAM) {
if (!(val & CONTROL_FPGA_PROGRAM)) {
control_reg &= ~CONTROL_FPGA_PROGRAM;
pinMode(FPGA_PROGB, INPUT);
}
} else if (val & CONTROL_FPGA_PROGRAM) {
digitalWrite(FPGA_PROGB, 0);
pinMode(FPGA_PROGB, OUTPUT);
control_reg |= CONTROL_FPGA_PROGRAM;
}
// handle JTAGEN
// check to see if we're running, but want to stop
if (control_reg & CONTROL_JTAGEN) {
if (!(val & CONTROL_JTAGEN)) {
// release
control_reg &= ~CONTROL_JTAGEN;
pinMode(JTAG_TMS, INPUT);
pinMode(JTAG_TDI, INPUT);
pinMode(JTAG_TCK, INPUT);
pinMode(JTAG_TDO, INPUT);
}
}
// check to see if we want to keep running
// update first, then claim
if (val & CONTROL_JTAGEN) {
// check to see if we need to acquire
if (!(control_reg & CONTROL_JTAGEN)) {
control_reg |= CONTROL_JTAGEN;
// yes, so drive outputs
pinMode(JTAG_TMS, OUTPUT);
pinMode(JTAG_TDI, OUTPUT);
pinMode(JTAG_TCK, OUTPUT);
pinMode(JTAG_TDO, INPUT_PULLUP);
}
if (val & CONTROL_JTAG_TMS) digitalWrite(JTAG_TMS, 1);
else digitalWrite(JTAG_TMS, 0);
if (val & CONTROL_JTAG_TDI) digitalWrite(JTAG_TDI, 1);
else digitalWrite(JTAG_TDI, 0);
if (val & CONTROL_JTAG_TCK) digitalWrite(JTAG_TCK, 1);
else digitalWrite(JTAG_TCK, 0);
// update
control_reg &= ~CONTROL_JTAG_MASK;
control_reg |= (val & CONTROL_JTAG_MASK);
}
if (val & CONTROL_COBS_FLUSH)
{
//no need to save this
for (uint8_t i=0;i<4;i++)
Serial1.write((byte)0x00);
}
break;
// 4-8 are read-only
// SPI write. Only the low byte is used.
// It's software's job to handle the latch enable, which is an I2C GPIO pin.
// Register 9 is a 16-bit LSB write (for attenuator).
// It's organized as (address)(data): so address is byte1, value is byte0.
// Register 10 is a 32-bit MSB write (for siggen).
// It's organized as writing the 32-bit value as is.
case 9: SPI1.beginTransaction(settingsAtten);
SPI1.transfer(val & 0xFF);
val >>= 8;
SPI1.transfer(val & 0xFF); break;
case 10: SPI1.beginTransaction(settingsSigGen);
// Need to invert the damn thing
SPI1.transfer((val>>24) & 0xFF);
SPI1.transfer((val>>16) & 0xFF);
SPI1.transfer((val>>8) & 0xFF);
SPI1.transfer(val & 0xFF); break;
case 16:
case 17:
case 18:
case 19:
case 20:
case 21:
case 22:
Wire.beginTransmission(i2c_gp[(addr-16)]);
// select output register
Wire.write(1);
Wire.write(val & 0xFF);
Wire.endTransmission();
break;
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
case 38:
case 39:
case 40:
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47:
case 48:
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
ch = (addr-32) % 4;
quad = (addr-32)/4;
ch = ch << 1;
// multi-write command
// byte 1: 0100 0 (ch) 0
// vref = 1, pd = 00, gx = 0
// byte 2: 1000 (val >> 8 & 0xF)
// byte 3: val & 0xFF
Wire.beginTransmission(I2C_DAC_BASE+quad+2);
Wire.write(0x40 | ch);
Wire.write(0x80 | ((val >> 8) & 0xF));
Wire.write(val & 0xFF);
Wire.endTransmission();
break;
case 56:
case 57:
val = val << 4;
Wire.beginTransmission(I2C_DAC_BASE + (addr - 56) );
// 2nd byte has command + powerdown stuff
// So now 010 00 00 0 = 0x40
// then (val >> 4) & 0xFF
// then (val << 4) & 0xF0
Wire.write(0x40);
Wire.write((val >> 8) & 0xFF);
Wire.write(val & 0xF0);
Wire.endTransmission();
break;
default: break;
}
tempBuffer[0] = buffer[0];
tempBuffer[1] = buffer[1];
tempBuffer[2] = buffer[2];
if (size > 7) tempBuffer[3] = 4;
else tempBuffer[3] = size - 3;
cbIf.send(tempBuffer, 4);
if (usbActive) usbIf.send(tempBuffer, 4);
}
}
}
//void onFpPacketReceived(const uint8_t *buffer, size_t size) {
// // not for us
// memcpy(tempBuffer, buffer, size);
// cbIf.send(tempBuffer, size);
// if (SerialUSB) usbIf.send(tempBuffer, size);
//}
#ifdef ENABLE_TIMER_MODE
// Just output a 1 Hz square wave instead of doing anything else, until
//based on https://shawnhymel.com/1710/arduino-zero-samd21-raw-pwm-using-cmsis/
void enterTimerMode()
{
// Enable and configure generic clock generator 4
GCLK->GENCTRL.reg = GCLK_GENCTRL_IDC | // Improve duty cycle
GCLK_GENCTRL_GENEN | // Enable generic clock gen
GCLK_GENCTRL_SRC_DFLL48M | // Select 48MHz as source
GCLK_GENCTRL_ID(4); // Select GCLK4
while (GCLK->STATUS.bit.SYNCBUSY); // Wait for synchronization
// Set clock divider of 48 to generic clock generator 4
GCLK->GENDIV.reg = GCLK_GENDIV_DIV(48) | // Divide 48 MHz by 48, so it's 1 MHz
GCLK_GENDIV_ID(4); // Apply to GCLK4 4
while (GCLK->STATUS.bit.SYNCBUSY); // Wait for synchronization
// Enable GCLK4 and connect it to and TCC2
GCLK->CLKCTRL.reg = GCLK_CLKCTRL_CLKEN | // Enable generic clock
GCLK_CLKCTRL_GEN_GCLK4 | // Select GCLK4
GCLK_CLKCTRL_ID_TCC2_TC3; // Feed GCLK4 to TCC2
while (GCLK->STATUS.bit.SYNCBUSY); // Wait for synchronization
// Divide counter by 1 giving 1 MHz (1 us) on each TCC2 tick
TCC2->CTRLA.reg |= TCC_CTRLA_PRESCALER(TCC_CTRLA_PRESCALER_DIV1_Val);
// Use "Normal PWM" (single-slope PWM): count up to PER, match on CC[n]
TCC2->WAVE.reg = TCC_WAVE_WAVEGEN_NPWM; // Select NPWM as waveform
while (TCC2->SYNCBUSY.bit.WAVE); // Wait for synchronization
int period = 1000000;
// Set the period (the number to count to (TOP) before resetting timer)
TCC2->PER.reg = period;
while (TCC2->SYNCBUSY.bit.PER);
// Set PWM signal to output 50% duty cycle
// n for CC[n] is determined by n = x % 4 where x is from WO[x]
TCC2->CC[0].reg = period / 2;
while (TCC2->SYNCBUSY.bit.CC2);
// Configure PA00 (D10 on Arduino Zero) to be output
PORT->Group[PORTA].DIRSET.reg = PORT_PA00; // Set pin as output
PORT->Group[PORTA].OUTCLR.reg = PORT_PA00; // Set pin to low
// Enable the port multiplexer for PA00
PORT->Group[PORTA].PINCFG[0].reg |= PORT_PINCFG_PMUXEN;
// Connect TCC2 timer to PA00. Function E is TCC2/WO[0] for PA00.
// Odd pin num (2*n + 1): use PMUXO
// Even pin num (2*n): use PMUXE
PORT->Group[PORTA].PMUX[0].reg = PORT_PMUX_PMUXE_E;
// Enable output (start PWM)
TCC2->CTRLA.reg |= (TCC_CTRLA_ENABLE);
while (TCC2->SYNCBUSY.bit.ENABLE); // Wait for synchronization
in_timer_mode = 1;
}
#endif