forked from siliconvalley4066/DLO-138-SPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.ino
More file actions
168 lines (129 loc) · 3.58 KB
/
io.ino
File metadata and controls
168 lines (129 loc) · 3.58 KB
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
int16_t trigLevel = 0;
// ------------------------
void initIO() {
// ------------------------
// set pin I/O direction
pinMode(BOARD_LED, OUTPUT);
pinMode(AN_CH1, INPUT_ANALOG);
pinMode(AN_CH2, INPUT_ANALOG);
pinMode(DG_CH1, INPUT_PULLDOWN);
pinMode(DG_CH2, INPUT_PULLDOWN);
pinMode(TRIGGER_IN, INPUT_PULLUP);
pinMode(AC_CH1, INPUT_PULLUP);
pinMode(AC_CH2, INPUT_PULLUP);
// calibrate the ADC channels at startup
adc_calibrate(ADC1);
adc_calibrate(ADC2);
setADC();
// start 1KHz square wave
pinMode(TEST_WAVE_PIN, PWM);
Timer3.setPeriod(1000);
pwmWrite(TEST_WAVE_PIN, Timer3.getOverflow() / 2);
DBG_PRINTLN("Test square wave started");
// input button and encoder
pinMode(ENCODER_SW, INPUT_PULLUP);
pinMode(ENCODER_A, INPUT_PULLUP);
pinMode(ENCODER_B, INPUT_PULLUP);
pinMode(BTN4, INPUT_PULLUP);
attachInterrupt(ENCODER_SW, readESwitchISR, FALLING);
attachInterrupt(BTN4, btn4ISR, CHANGE);
#ifdef USE_ENCODER
attachInterrupt(ENCODER_A, readEncoderISR, CHANGE);
attachInterrupt(ENCODER_B, readEncoderISR, CHANGE);
#else
attachInterrupt(ENCODER_A, readASwitchISR, FALLING);
attachInterrupt(ENCODER_B, readBSwitchISR, FALLING);
#endif
// init trigger level PWM
// start 20KHz square wave on trigger out reference and negative v gen
Timer4.setPeriod(50);
pinMode(TRIGGER_LEVEL, PWM);
pinMode(VGEN, PWM);
pwmWrite(VGEN, 700);
blinkLED();
// init scan timeout timer
initScanTimeout();
}
// ------------------------
void setADC() {
// ------------------------
int pinMapADCin1 = PIN_MAP[AN_CH1].adc_channel;
int pinMapADCin2 = PIN_MAP[AN_CH2].adc_channel;
// opamp is low impedance, set next fastest sampling
adc_set_sample_rate(ADC1, ADC_SMPR_7_5);
adc_set_sample_rate(ADC2, ADC_SMPR_7_5);
adc_set_reg_seqlen(ADC1, 1);
adc_set_reg_seqlen(ADC2, 1);
ADC1->regs->SQR3 = pinMapADCin1;
// set ADC1 continuous mode
ADC1->regs->CR2 |= ADC_CR2_CONT;
// set ADC2 in regular simultaneous mode
ADC1->regs->CR1 |= 0x60000;
ADC1->regs->CR2 |= ADC_CR2_SWSTART;
// set ADC2 continuous mode
ADC2->regs->CR2 |= ADC_CR2_CONT;
ADC2->regs->SQR3 = pinMapADCin2;
}
// ------------------------
void blinkLED() {
// ------------------------
LED_ON;
delay(10);
LED_OFF;
}
// ------------------------
void initScanTimeout() {
// ------------------------
Timer2.pause();
Timer2.setPeriod(100 * 1000); // 100msec
Timer2.setCount(0);
Timer2.attachInterrupt(TIMER_UPDATE_INTERRUPT, scanTimeoutISR);
Timer2.refresh();
}
// ------------------------
int16_t getTriggerLevel() {
// ------------------------
return trigLevel;
}
// ------------------------
void setTriggerLevel(int16_t tLvl) {
// ------------------------
trigLevel = tLvl;
trigger_ad = tLvl + 2048;
}
// ------------------------
void readInpSwitches() {
// ------------------------
static uint8_t couplingOld1, couplingOld2, rangeOld;
uint16_t cpl, pos1, pos2;
adc_reg_map *ADC1regs = ADC1->regs;
// check AC/DC switch 1
if (digitalRead(AC_CH1) == LOW) {
couplingPos1 = CPL_AC;
digitalWrite(OF_CH1, HIGH);
} else {
couplingPos1 = CPL_DC;
digitalWrite(OF_CH1, LOW);
}
// check AC/DC switch 2
if (digitalRead(AC_CH2) == LOW) {
couplingPos2 = CPL_AC;
digitalWrite(OF_CH2, HIGH);
} else {
couplingPos2 = CPL_DC;
digitalWrite(OF_CH2, LOW);
}
// check if switch position changed from previous snap
if(couplingPos1 != couplingOld1) {
couplingOld1 = couplingPos1;
repaintLabels();
}
if(couplingPos2 != couplingOld2) {
couplingOld2 = couplingPos2;
repaintLabels();
}
if(rangePos1 != rangeOld) {
rangeOld = rangePos1;
repaintLabels();
}
}