-
Notifications
You must be signed in to change notification settings - Fork 8
/
triggerbox.ino
835 lines (707 loc) · 21.2 KB
/
triggerbox.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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
triggerbox - trigger box for arduino
====================================
This file contains the source code for the firmware that runs on the
Arduino device. There are two notable things that happen here.
Regular hardware trigger pulse generation
-----------------------------------------
We need to send trigger pulses at a regular interval.
Clock synchronization support
-----------------------------
See the file synchronization.md for information about the theory of
operation. In this firmware, we need to respond to clock requests with
our current clock value. We use the device's timer1 as our official
clock.
*/
#include <Arduino.h>
#include <EEPROM.h>
#include <SPI.h>
// --- start UDEV library ------
/* This UDEV code is included in this file so the whole firmware can be
distributed as a single Arduino file without having to install libraries. It
was originally written by John Stowers. It was downloaded from
https://github.com/strawlab/arduino-udev/tree/f111cc92158d1016ec330455f6811bb8a65aa836/firmware/UDEV.
It is licensed under the MIT licence.
*/
typedef enum {
ID_NOP = 0x00,
ID_WRITTEN = 0x01,
ID_READ = 0x02,
ID_FAIL_CRC = 0x03,
ID_FAIL_READ = 0x04
} udev_state_t;
class UDEV {
public:
UDEV(HardwareSerial &serial, int pinLed = 13, int eepromBase = 0x0, int maxLen = 8);
udev_state_t read_and_process(float block = 0, int pin_led = -1, int delay=50);
udev_state_t process(char cmd, char value);
void serial_handshake(float block = 5);
void begin(void);
private:
HardwareSerial &_serial;
int _pinLed;
int _eepromBase;
int _maxLen;
};
UDEV::UDEV(HardwareSerial &serial, int pinLed, int eepromBase, int maxLen) :
_serial(serial),
_pinLed(pinLed),
_eepromBase(eepromBase),
_maxLen(maxLen)
{
}
udev_state_t UDEV::read_and_process(float block, int pin_led, int del) {
udev_state_t ok = ID_NOP;
//convert block to a millisecond counter
int ms = block*1000;
int c = -1;
while (true) {
if (pin_led >= 0)
digitalWrite(pin_led, 0x01 ^ digitalRead(pin_led));
if (!_serial)
goto pause;
if (!_serial.available())
goto pause;
c = _serial.read();
if ((c != -1) && ((char)c == 'N'))
break;
pause:
if (ms <= 0)
break;
delay(del);
ms -= del;
}
char cmd = c;
if ((c != -1) && (cmd == 'N')) {
while (!_serial.available());
char val = _serial.read();
ok = process(cmd, val);
}
return ok;
}
static byte CRC8(byte *data, byte len) {
byte crc = 0x00;
while (len--) {
byte extract = *data++;
for (byte i = 8; i; i--) {
byte sum = (crc ^ extract) & 0x01;
crc >>= 1;
if (sum) {
crc ^= 0x8C;
}
extract >>= 1;
}
}
return crc;
}
udev_state_t UDEV::process(char cmd, char value) {
udev_state_t ok = ID_NOP;
char name[_maxLen];
memset(&name,0,_maxLen);
if (cmd=='N') {
ok = ID_FAIL_READ;
if (value=='=') {
//read the string
for (int i=0; i<_maxLen; i++) {
while( !_serial.available() );
char c = _serial.read();
name[i] = c;
}
//read the crc
char crc[2] = {0,0};
while( !_serial.available() );
crc[0] = _serial.read();
while( !_serial.available() );
crc[1] = _serial.read();
byte crca = strtoul(crc,NULL,16);
byte crcb = CRC8((byte *)name,_maxLen);
//if the crc is ok write to the eeprom
if (crca == crcb) {
for (int j=0; j<_maxLen; j++) {
EEPROM.write( _eepromBase + j, name[j]);
}
ok = ID_WRITTEN;
} else {
ok = ID_FAIL_CRC;
}
} else if (value=='?') {
for (int j=0; j<_maxLen; j++) {
char c = EEPROM.read( _eepromBase + j);
name[j] = c;
_serial.write(c);
}
unsigned char crc = CRC8((byte *)name,_maxLen);
_serial.print(crc,HEX);
ok = ID_READ;
}
}
return ok;
}
void UDEV::serial_handshake(float block) {
udev_state_t ok = read_and_process(block, _pinLed, 20);
digitalWrite(_pinLed, ok != ID_READ);
delay(500);
_serial.flush();
_serial.end();
}
void UDEV::begin(void) {
pinMode(_pinLed, OUTPUT);
_serial.begin(9600);
delay(50);
}
// ----------------------- end UDEV library
#define WITH_AOUT
#ifdef WITH_AOUT
// ----------------------- start MCP4822 library
/* This MCP4822 code is included in this file so the whole firmware
can be distributed as a single Arduino file without having to
install libraries. It was originally written by Will Dickson, IO
Rodeo Inc. It was downloaded from
https://bitbucket.org/iorodeo/iorodeo_arduino_libs . It is licensed
under the Apache 2.0 licence.
*/
#define MCP4822_NUMCHAN 2
enum MCP4822_DAC_CHAN
{
MCP4822_DAC_A,
MCP4822_DAC_B
};
class MCP4822
{
private:
int cs;
int ldac;
int gain[MCP4822_NUMCHAN];
int getCmdWrd(int dac, int value);
public:
MCP4822();
MCP4822(int csPin, int ldacPin);
void setValue(int dac, int value);
void setValue_A(int value);
void setValue_B(int value);
void setValue_AB(int value_A, int value_B);
void setGain2X(int dac);
void setGain2X_A();
void setGain2X_B();
void setGain2X_AB();
void setGain1X(int dac);
void setGain1X_A();
void setGain1X_B();
void setGain1X_AB();
void off_AB();
void off_A();
void off_B();
void off(int dac);
};
// ----------------------------------------------------------------------------
// max4822.cpp
//
// Provides an SPI based interface to the MCP4822 dual voltage output digital
// to analog converter.
//
// Author: Will Dickson, IO Rodeo Inc.
// ----------------------------------------------------------------------------
#define ACTIVE 0b0001000000000000
#define SHUTDOWN 0b0000000000000000
#define GAIN_2X 0b0000000000000000
#define GAIN_1X 0b0010000000000000
#define DAC_A_WRITE 0b0000000000000000
#define DAC_B_WRITE 0b1000000000000000
MCP4822::MCP4822()
{
}
// ----------------------------------------------------------------------------
// MCP4822::MCP4822
//
// Constructor
// ----------------------------------------------------------------------------
MCP4822::MCP4822(int csPin, int ldacPin)
{
// Configure chip select and latch pins
cs = csPin;
ldac = ldacPin;
pinMode(cs, OUTPUT);
pinMode(ldac, OUTPUT);
digitalWrite(cs, HIGH);
digitalWrite(ldac, HIGH);
// Set to default configuration
setGain2X_AB();
}
// ---------------------------------------------------------------------------
// MCP4822::getCmdWrd
//
// Gets command work for writing value to given channel
// ---------------------------------------------------------------------------
int MCP4822::getCmdWrd(int dac, int value)
{
int cmdWrd = 0;
switch (dac)
{
case MCP4822_DAC_A:
cmdWrd = DAC_A_WRITE;
break;
case MCP4822_DAC_B:
cmdWrd = DAC_B_WRITE;
break;
default:
return 0;
}
cmdWrd |= gain[dac];
cmdWrd |= ACTIVE;
cmdWrd |= (0b0000111111111111 & value);
return cmdWrd;
}
// ----------------------------------------------------------------------------
// MCP4822::setValue
//
// Set the output value of the given dac channel
// ----------------------------------------------------------------------------
void MCP4822::setValue(int dac, int value)
{
int cmdWrd;
uint8_t byte0;
uint8_t byte1;
// Enable SPI communications
digitalWrite(cs, LOW);
// Send command word
cmdWrd = getCmdWrd(dac, value);
byte0 = cmdWrd >> 8;
byte1 = cmdWrd & 0b0000000011111111;
SPI.transfer(byte0);
SPI.transfer(byte1);
// Disable SPI communications
digitalWrite(cs, HIGH);
// Latch value
digitalWrite(ldac, LOW);
digitalWrite(ldac, HIGH);
}
// ---------------------------------------------------------------------------
// MCP4822::setValue_A
//
// Set the output value of dac A to the given value
// ---------------------------------------------------------------------------
void MCP4822::setValue_A(int value)
{
setValue(MCP4822_DAC_A, value);
}
// ----------------------------------------------------------------------------
// MCP4822::setValue_B
//
// Set the output value of dac B to the given value
// ----------------------------------------------------------------------------
void MCP4822::setValue_B(int value)
{
setValue(MCP4822_DAC_B, value);
}
// ---------------------------------------------------------------------------
// MCP4822::setValue_AB
//
// Set the output value of dac A and B to the given values. Latch them in at
// the same time.
// ---------------------------------------------------------------------------
void MCP4822::setValue_AB(int value_A, int value_B)
{
int cmdWrd;
uint8_t byte0;
uint8_t byte1;
// Enable SPI communications and send command word
digitalWrite(cs, LOW);
// Send command word for DAC A
cmdWrd = getCmdWrd(MCP4822_DAC_A, value_A);
byte0 = cmdWrd >> 8;
byte1 = cmdWrd & 0b0000000011111111;
SPI.transfer(byte0);
SPI.transfer(byte1);
// Toggle CS
digitalWrite(cs, HIGH);
digitalWrite(cs, LOW);
// Send command word for DAC A
cmdWrd = getCmdWrd(MCP4822_DAC_B, value_B);
byte0 = cmdWrd >> 8;
byte1 = cmdWrd & 0b0000000011111111;
SPI.transfer(byte0);
SPI.transfer(byte1);
// Disable SPI communications
digitalWrite(cs, HIGH);
// Latch value
digitalWrite(ldac, LOW);
digitalWrite(ldac, HIGH);
}
// ---------------------------------------------------------------------------
// MCP4822::setGain2X
//
// Set the gain of the given channel to 2X
// ---------------------------------------------------------------------------
void MCP4822::setGain2X(int dac)
{
if ((dac == MCP4822_DAC_A) || (dac == MCP4822_DAC_B))
{
gain[dac] = GAIN_2X;
}
}
// ----------------------------------------------------------------------------
// MCP4822::setGain2X_A
//
// Set the gain of dac A to 2X
// ----------------------------------------------------------------------------
void MCP4822::setGain2X_A()
{
setGain2X(MCP4822_DAC_A);
}
// ----------------------------------------------------------------------------
// MCP4822::setGain2X_B
//
// Set the gain of dac B to 2X
// ----------------------------------------------------------------------------
void MCP4822::setGain2X_B()
{
setGain2X(MCP4822_DAC_B);
}
// ----------------------------------------------------------------------------
// MCP4822::setGain2X_AB
//
// Set the gain of dac A and B to 2X
// ----------------------------------------------------------------------------
void MCP4822::setGain2X_AB()
{
setGain2X_A();
setGain2X_B();
}
// ---------------------------------------------------------------------------
// MCP4822::setGain1X
//
// Set the gain of the given channel to 1X
// ----------------------------------------------------------------------------
void MCP4822::setGain1X(int dac)
{
if ((dac == MCP4822_DAC_A) || (dac == MCP4822_DAC_B))
{
gain[dac] = GAIN_1X;
}
}
// ----------------------------------------------------------------------------
// MCP4822::setGain1X_A
//
// Set the gain of dac A to 1X
// ----------------------------------------------------------------------------
void MCP4822::setGain1X_A()
{
setGain1X(MCP4822_DAC_A);
}
// ----------------------------------------------------------------------------
// MCP4822::setGain1X_B
//
// Set the gain of dac B to 1X
// ----------------------------------------------------------------------------
void MCP4822::setGain1X_B()
{
setGain1X(MCP4822_DAC_B);
}
// ----------------------------------------------------------------------------
// MCP4822::setGain1X_AB
//
// Set the gain of dac A and B to 1X
// ----------------------------------------------------------------------------
void MCP4822::setGain1X_AB()
{
setGain1X_A();
setGain1X_B();
}
// ----------------------------------------------------------------------------
// MCP4822::off
//
// Turn off the given dac
// ----------------------------------------------------------------------------
void MCP4822::off(int dac)
{
int cmdWrd = 0;
uint8_t byte0;
uint8_t byte1;
// Create shutdown cmd word.
switch (dac)
{
case MCP4822_DAC_A:
cmdWrd = DAC_A_WRITE;
break;
case MCP4822_DAC_B:
cmdWrd = DAC_B_WRITE;
break;
default:
return;
}
cmdWrd |= SHUTDOWN;
// Enable SPI communications
digitalWrite(cs, LOW);
// Send command word
byte0 = cmdWrd >> 8;
byte1 = cmdWrd & 0b0000000011111111;
SPI.transfer(byte0);
SPI.transfer(byte1);
// Disable SPI communications
digitalWrite(cs, HIGH);
}
// ----------------------------------------------------------------------------
// MCP4822::off_A
//
// Turn off dac A
// ----------------------------------------------------------------------------
void MCP4822::off_A()
{
off(MCP4822_DAC_A);
}
// ----------------------------------------------------------------------------
// MCP4822::off_B
//
// Turn off dac B
// ----------------------------------------------------------------------------
void MCP4822::off_B()
{
off(MCP4822_DAC_B);
}
// ----------------------------------------------------------------------------
// MCP4822::off_AB
//
// Turn off dac A and B
// ----------------------------------------------------------------------------
void MCP4822::off_AB()
{
off_A();
off_B();
}
#endif // WITH_AOUT
// ----------------------- end MCP4822 library
typedef uint32_t pulsenumber_dtype; /* 2**32 @100Hz = 497 days */
struct timed_sample
{
uint8_t value; /* value of arbitrary data */
pulsenumber_dtype pulsenumber;
uint16_t ticks;
};
// Pin numbers -----------------------------------------------------------------
const unsigned short TrigPin = 9;
const unsigned short LEDPin = 2;
#ifdef WITH_AOUT
const unsigned short AOUT_CS = 10;
const unsigned short AOUT_LDAC = 7;
#endif
// Global variables ------------------------------------------------------------
volatile pulsenumber_dtype pulsenumber = 0;
// ---- set TCCR1B ----------
// high bits = 0,0,0
// WGM13, WGM12 = 1,1
// CS1 = 0,1,0 (starts timer1 CS=8) (clock select)
uint8_t tccr1b_when_running = 0x18; //stopped. started in setup()
#ifdef WITH_AOUT
MCP4822 analogOut = MCP4822(AOUT_CS, AOUT_LDAC);
#endif
UDEV udev(Serial);
// Interrupt service routine for timer1 compare -------------------------------
ISR(TIMER1_COMPA_vect)
{
pulsenumber++;
}
// Setup timer1 ----------------------------------------------------------------
void setup_timer1(uint8_t tccr1b, uint16_t icr1)
{
tccr1b_when_running = tccr1b;
cli();
// Set output compare to generate trigger pulse width
OCR1A = 0x03e8;
// Set TOP
ICR1 = icr1;
// ---- set TCCR1A ----------
// set Compare Output Mode for Fast PWM, with TOP in ICR1
// COM1A1:0 = 1,0 clear OC1A on compare match
// COM1B1:0 = 1,0 clear OC1B on compare match
// WGM11, WGM10 = 1,0
TCCR1A = 0xAA;
TCCR1B = tccr1b;
TIMSK1 |= (1 << OCIE1A);
sei();
}
// Standard arduino setup function ---------------------------------------------
void setup()
{
udev.begin();
udev.serial_handshake(); //blocks for 5 seconds by default
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV8);
SPI.begin();
#ifdef WITH_AOUT
analogOut.setGain2X_AB();
#endif
pinMode(LEDPin, OUTPUT);
pinMode(TrigPin, OUTPUT);
// start serial port at 115200 bps:
Serial.begin(115200);
digitalWrite(LEDPin, HIGH);
// start at 25fps
setup_timer1(0x1B, 0x2710);
}
// Send data with our simple protocol to the host computer ---------------------
static char Serial_read_blocking()
{
while (Serial.available() == 0)
{
delay(10);
}
return Serial.read();
}
static inline void send_data_string(String *s, const char header)
{
uint8_t N = s->length();
Serial.write(header);
Serial.write(N); // payload size
uint8_t chksum = 0;
char next_byte;
for (uint8_t i = 0; i < N; i++)
{
next_byte = s->charAt(i);
chksum += next_byte;
Serial.write(next_byte);
}
Serial.write(chksum);
}
static inline void send_data(const struct timed_sample samp, const char header)
{
/* This commented-out version does the same as below but with more
overhead. Nevertheless, it can be used to verify the
correctness of the send_data_string() function.
const char * buf;
buf = (const char*)&(samp);
String out_buf="";
for (uint8_t i=0; i< sizeof(timed_sample); i++) {
out_buf += buf[i];
}
send_data_string(&out_buf, header);
*/
const char *buf;
Serial.write(header);
Serial.write((char)sizeof(struct timed_sample)); // payload size
buf = (const char *)&(samp);
uint8_t chksum = 0;
for (uint8_t i = 0; i < sizeof(struct timed_sample); i++)
{
chksum += buf[i];
Serial.write(buf[i]);
}
Serial.write(chksum);
}
// Standard Arduino loop function. This gets repeatedly called at high rate ----
void loop()
{
if (Serial.available() >= 2)
{
static char cmd;
static char value;
cmd = Serial.read();
value = Serial.read();
if (cmd == 'P')
{
// timestamp query
static struct timed_sample timestamp_request;
timestamp_request.value = value;
uint8_t SaveSREG_ = SREG; // save interrupt flag
cli(); // disable interrupts
timestamp_request.pulsenumber = pulsenumber;
timestamp_request.ticks = TCNT1;
SREG = SaveSREG_; // restore interrupt flags
send_data(timestamp_request, 'P');
}
else if (cmd == 'V')
{
// version request
static struct timed_sample version_request;
version_request.value = 14;
uint8_t SaveSREG_ = SREG; // save interrupt flag
cli(); // disable interrupts
version_request.pulsenumber = pulsenumber;
version_request.ticks = TCNT1;
SREG = SaveSREG_; // restore interrupt flags
send_data(version_request, 'V');
digitalWrite(LEDPin, LOW);
}
else if (cmd == 'S')
{
// synchronization
if (value == '0')
{
// stop clock, reset pulsenumber
TCCR1B = 0x18;
TCNT1 = 0;
pulsenumber = 0;
digitalWrite(LEDPin, HIGH);
}
else if (value == '1')
{
// start clock
TCCR1B = tccr1b_when_running;
digitalWrite(LEDPin, LOW);
}
else if (value == '2')
{
// stop clock
TCCR1B = 0x18;
digitalWrite(LEDPin, HIGH);
}
}
else if (cmd == 'T')
{
// TOP value
uint8_t value0, value1;
char prescaler_key;
uint16_t new_icr1;
if (value == '=')
{
value0 = Serial_read_blocking();
value1 = Serial_read_blocking();
prescaler_key = Serial_read_blocking();
new_icr1 = ((uint16_t)value1 << 8) + value0;
ICR1 = new_icr1;
if (prescaler_key == '1')
{
// prescaler = 8
tccr1b_when_running = 0x1A;
}
else if (prescaler_key == '2')
{
// prescaler = 64
tccr1b_when_running = 0x1B;
}
}
}
else if (cmd == 'O')
{
// AOUT values
uint8_t aout0_0, aout0_1, aout1_0, aout1_1;
uint8_t aout_sequence;
int aout0, aout1;
if (value == '=')
{
aout0_0 = Serial_read_blocking();
aout0_1 = Serial_read_blocking();
aout0 = ((int)aout0_1 << 8) + aout0_0;
aout1_0 = Serial_read_blocking();
aout1_1 = Serial_read_blocking();
aout1 = ((int)aout1_1 << 8) + aout1_0;
#ifdef WITH_AOUT
analogOut.setValue_AB(aout0, aout1);
#endif
aout_sequence = Serial_read_blocking();
static struct timed_sample aout_confirm;
aout_confirm.value = aout_sequence;
uint8_t SaveSREG_ = SREG; // save interrupt flag
cli(); // disable interrupts
aout_confirm.pulsenumber = pulsenumber;
aout_confirm.ticks = TCNT1;
SREG = SaveSREG_; // restore interrupt flags
send_data(aout_confirm, 'O');
}
}
else if (cmd == 'N')
{
udev.process(cmd, value);
}
}
}