-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc.cpp
432 lines (316 loc) · 10.6 KB
/
adc.cpp
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
#include "Arduino.h"
#include "wiring_private.h"
#include "wiring_analog.h"
#include "board_wiring.h"
#include "adc.h"
uint32_t analogReadOversample(uint32_t pin, char samples);
uint32_t samplePinWithPinLow(uint32_t samplePin, uint32_t lowPin);
void samplePins(Sample *sample);
void samplePinsNew(Sample *sample);
void convert_to_ohms(Sample *in_adc, Sample *out_r);
/*
* \brief Reads the value from the specified analog pin.
*
* \param ulPin
*
* \return Read value from selected pin, if no error.
*/
uint32_t analogReadNew( uint32_t ulPin ) ;
/*
* \brief Set the resolution of analogRead return values. Default is 10 bits (range from 0 to 1023).
*
* \param res
*/
void analogReadResolutionNew(int res);
const char adc_bits = 12;
const char adc_bits_norm = 12;
//char adc_bits = 10;
//int adc_gain = 1;
const int adc_gain = 4;
//int adc_gain = 16;
const int adc_oversample = 1;
const char adc_norm_scale = 1ul<<(adc_bits_norm - adc_bits);
// adc read with pin low
int32_t calib_floor;
// adc read with pin 10 ohm to low
int32_t calib_10_delta;
#define CALIB_10 10
Sample adc = {0};
Sample resistance_avg = {0};
Sample resistance_max = {0};
#define ADC_SAMPLE_COUNT (4)
int current_adc_sample = 0;
Sample adc_samples[ADC_SAMPLE_COUNT];
Sample adc_avg;
Sample adc_max;
#ifndef _SAMD21_ADC_COMPONENT_
#error "SAMD21 only"
#endif
static int _readResolution = 10;
static int _ADCResolution = 10;
// Wait for synchronization of registers between the clock domains
static __inline__ void syncADC() __attribute__((always_inline, unused));
static void syncADC() {
while (ADC->STATUS.bit.SYNCBUSY == 1)
;
}
static inline uint32_t mapResolution(uint32_t value, uint32_t from, uint32_t to)
{
if (from == to) {
return value;
}
if (from > to) {
return value >> (from-to);
}
return value << (to-from);
}
void smooth_samples() {
adc_avg = (const Sample){ 0 };
adc_max = (const Sample){ 0 };
for (int i=0; i<ADC_SAMPLE_COUNT; i+=1) {
for (int j=0; j<SAMPLE_COMPONENT_COUNT; j+=1) {
sample_t v = adc_samples[i].component[j];
adc_avg.component[j] += v;
sample_t cur_max = adc_max.component[j];
if (v > cur_max) {
adc_max.component[j] = v;
}
}
}
for (int j=0; j<6; j+=1) {
adc_avg.component[j] /= ADC_SAMPLE_COUNT;
}
}
void check_floor(Sample *sample) {
sample_t new_floor = min(min(min(min(min(
sample->sAA1,
sample->sAB),
sample->sAC),
sample->sBB1),
sample->sBC),
sample->sCC1);
if (new_floor < calib_floor) {
Serial.printf("Floor change from %d to %d\n", calib_floor, new_floor);
calib_floor = new_floor;
}
}
void take_sample() {
Sample *s = &adc_samples[current_adc_sample];
samplePinsNew(s);
check_floor(s);
current_adc_sample = (current_adc_sample + 1) % ADC_SAMPLE_COUNT;
smooth_samples();
convert_to_ohms(&adc_avg, &resistance_avg);
convert_to_ohms(&adc_max, &resistance_max);
}
void analogSetup() {
analogReadResolutionNew(adc_bits);
//analogReference(AR_EXTERNAL);
analogReference(AR_DEFAULT);
if (adc_gain == 1) {
ADC->INPUTCTRL.bit.GAIN = ADC_INPUTCTRL_GAIN_1X_Val;
} else if (adc_gain == 2) {
ADC->INPUTCTRL.bit.GAIN = ADC_INPUTCTRL_GAIN_2X_Val;
} else if (adc_gain == 4) {
ADC->INPUTCTRL.bit.GAIN = ADC_INPUTCTRL_GAIN_4X_Val;
} else if (adc_gain == 8) {
ADC->INPUTCTRL.bit.GAIN = ADC_INPUTCTRL_GAIN_8X_Val;
} else if (adc_gain == 16) {
ADC->INPUTCTRL.bit.GAIN = ADC_INPUTCTRL_GAIN_16X_Val;
}
}
uint32_t analogReadOversample(uint32_t pin, char samples) {
uint32_t adc_read = 0;
for (int i=0; i < samples * adc_norm_scale ; ++i) {
adc_read += (analogReadNew(pin));
}
adc_read /= samples;
return adc_read;
}
uint32_t samplePinWithPinLow(uint32_t samplePin, uint32_t lowPin) {
pinMode(lowPin, OUTPUT);
digitalWrite(lowPin, LOW);
delay(1);
// Serial.printf("Set %d low \n", lowPin);
// Serial.printf("Readback %d %d \n", lowPin, digitalRead(lowPin));
uint32_t result = analogReadOversample(samplePin, adc_oversample);
pinMode(lowPin, INPUT);
return result;
}
void calibrateLevels() {
pinMode(PIN_TADrv, OUTPUT);
digitalWrite(PIN_TADrv, HIGH);
delay(1);
calib_floor = samplePinWithPinLow(PIN_TA_10, PIN_TA);
calib_10_delta = samplePinWithPinLow(PIN_TA, PIN_TA_10) - calib_floor;
pinMode(PIN_TADrv, INPUT);
pinMode(PIN_TA, INPUT);
pinMode(PIN_TA_10, INPUT);
}
/**
* Use current AA1 as 0.
*/
void external_calibration() {
calib_floor = adc_avg.sAA1;
}
void samplePins(Sample *sample) {
digitalWrite(PIN_TADrv, HIGH);
pinMode(PIN_TADrv, OUTPUT);
sample->sAA1 = samplePinWithPinLow(PIN_TA, PIN_TA1);
sample->sAB = samplePinWithPinLow(PIN_TA, PIN_TB);
sample->sAC = samplePinWithPinLow(PIN_TA, PIN_TC);
pinMode(PIN_TADrv, INPUT);
digitalWrite(PIN_TBDrv, HIGH);
pinMode(PIN_TBDrv, OUTPUT);
sample->sBB1 = samplePinWithPinLow(PIN_TB, PIN_TB1);
sample->sBC = samplePinWithPinLow(PIN_TB, PIN_TC);
pinMode(PIN_TBDrv, INPUT);
digitalWrite(PIN_TCDrv, HIGH);
pinMode(PIN_TCDrv, OUTPUT);
sample->sCC1 = samplePinWithPinLow(PIN_TC, PIN_TC1);
pinMode(PIN_TCDrv, INPUT);
}
void rawAdcEnable() {
/*
* Bit 1 ENABLE: Enable
* 0: The ADC is disabled.
* 1: The ADC is enabled.
* Due to synchronization, there is a delay from writing CTRLA.ENABLE until the peripheral is enabled/disabled. The
* value written to CTRL.ENABLE will read back immediately and the Synchronization Busy bit in the Status register
* (STATUS.SYNCBUSY) will be set. STATUS.SYNCBUSY will be cleared when the operation is complete.
*
* Before enabling the ADC, the asynchronous clock source must be selected and enabled, and the ADC reference must be
* configured. The first conversion after the reference is changed must not be used.
*/
syncADC();
ADC->CTRLA.bit.ENABLE = 0x01; // Enable ADC
syncADC();
}
void samplePinsNew(Sample *sample) {
rawAdcEnable();
// Samples from A
pinMode(PIN_TADrv, OUTPUT);
digitalWrite(PIN_TADrv, HIGH);
pinPeripheral(PIN_TA, PIO_ANALOG);
ADC->INPUTCTRL.bit.MUXPOS = g_APinDescription[PIN_TA].ulADCChannelNumber; // Selection for the positive ADC input
// Sample A-A1
pinMode(PIN_TA1, OUTPUT);
digitalWrite(PIN_TA1, LOW);
sample->sAA1 = analogReadNew(PIN_TA);
//////
// SAMD21 CODE
// Start conversion
syncADC();
ADC->SWTRIG.bit.START = 1;
// Waiting for the 1st conversion to complete
while (ADC->INTFLAG.bit.RESRDY == 0);
// Clear the Data Ready flag
ADC->INTFLAG.reg = ADC_INTFLAG_RESRDY;
// Start conversion again, since The first conversion after the reference is changed must not be used.
syncADC();
ADC->SWTRIG.bit.START = 1;
// Store the value
while (ADC->INTFLAG.bit.RESRDY == 0); // Waiting for conversion to complete
sample->sAA1 = mapResolution(ADC->RESULT.reg, _ADCResolution, _readResolution);
syncADC();
ADC->CTRLA.bit.ENABLE = 0x00; // Disable ADC
syncADC();
/////
pinMode(PIN_TA1, INPUT); // release
sample->sAB = samplePinWithPinLow(PIN_TA, PIN_TB);
sample->sAC = samplePinWithPinLow(PIN_TA, PIN_TC);
pinMode(PIN_TADrv, INPUT);
digitalWrite(PIN_TBDrv, HIGH);
pinMode(PIN_TBDrv, OUTPUT);
sample->sBB1 = samplePinWithPinLow(PIN_TB, PIN_TB1);
sample->sBC = samplePinWithPinLow(PIN_TB, PIN_TC);
pinMode(PIN_TBDrv, INPUT);
digitalWrite(PIN_TCDrv, HIGH);
pinMode(PIN_TCDrv, OUTPUT);
sample->sCC1 = samplePinWithPinLow(PIN_TC, PIN_TC1);
pinMode(PIN_TCDrv, INPUT);
}
int32_t covert_to_ohms(uint32_t sample) {
int32_t result = ((((int32_t)sample) - calib_floor) * CALIB_10 * 10) / (calib_10_delta);
if (result > 9999) {
result = 9999;
}
return result;
}
void convert_to_ohms(Sample *in_adc, Sample *out_r) {
out_r->sAA1 = covert_to_ohms(in_adc->sAA1);
out_r->sAB = covert_to_ohms(in_adc->sAB);
out_r->sAC = covert_to_ohms(in_adc->sAC);
out_r->sBB1 = covert_to_ohms(in_adc->sBB1);
out_r->sBC = covert_to_ohms(in_adc->sBC);
out_r->sCC1 = covert_to_ohms(in_adc->sCC1);
if (out_r->sBB1 > 9999) {
Serial.printf("raw adc.sBB1: %4d\n", in_adc->sBB1);
Serial.printf("raw r_BB1: %4d\n", out_r->sBB1);
Serial.printf("calib: %4d %4d\n", calib_floor, calib_10_delta);
}
}
// // Wait for synchronization of registers between the clock domains
// static __inline__ void syncTC_16(Tc* TCx) __attribute__((always_inline, unused));
// static void syncTC_16(Tc* TCx) {
// while (TCx->COUNT16.STATUS.bit.SYNCBUSY);
// }
// // Wait for synchronization of registers between the clock domains
// static __inline__ void syncTCC(Tcc* TCCx) __attribute__((always_inline, unused));
// static void syncTCC(Tcc* TCCx) {
// while (TCCx->SYNCBUSY.reg & TCC_SYNCBUSY_MASK);
// }
void analogReadResolutionNew(int res)
{
_readResolution = res;
if (res > 10) {
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_12BIT_Val;
_ADCResolution = 12;
} else if (res > 8) {
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_10BIT_Val;
_ADCResolution = 10;
} else {
ADC->CTRLB.bit.RESSEL = ADC_CTRLB_RESSEL_8BIT_Val;
_ADCResolution = 8;
}
syncADC();
}
uint32_t analogReadNew(uint32_t pin)
{
uint32_t valueRead = 0;
pinPeripheral(pin, PIO_ANALOG);
// SAMD21 CODE
syncADC();
ADC->INPUTCTRL.bit.MUXPOS = g_APinDescription[pin].ulADCChannelNumber; // Selection for the positive ADC input
// Control A
/*
* Bit 1 ENABLE: Enable
* 0: The ADC is disabled.
* 1: The ADC is enabled.
* Due to synchronization, there is a delay from writing CTRLA.ENABLE until the peripheral is enabled/disabled. The
* value written to CTRL.ENABLE will read back immediately and the Synchronization Busy bit in the Status register
* (STATUS.SYNCBUSY) will be set. STATUS.SYNCBUSY will be cleared when the operation is complete.
*
* Before enabling the ADC, the asynchronous clock source must be selected and enabled, and the ADC reference must be
* configured. The first conversion after the reference is changed must not be used.
*/
syncADC();
ADC->CTRLA.bit.ENABLE = 0x01; // Enable ADC
// Start conversion
syncADC();
ADC->SWTRIG.bit.START = 1;
// Waiting for the 1st conversion to complete
while (ADC->INTFLAG.bit.RESRDY == 0);
// Clear the Data Ready flag
ADC->INTFLAG.reg = ADC_INTFLAG_RESRDY;
// Start conversion again, since The first conversion after the reference is changed must not be used.
syncADC();
ADC->SWTRIG.bit.START = 1;
// Store the value
while (ADC->INTFLAG.bit.RESRDY == 0); // Waiting for conversion to complete
valueRead = ADC->RESULT.reg;
syncADC();
ADC->CTRLA.bit.ENABLE = 0x00; // Disable ADC
syncADC();
return mapResolution(valueRead, _ADCResolution, _readResolution);
}