forked from corrados/edrumulus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedrumulus_hardware.cpp
635 lines (549 loc) · 29.1 KB
/
edrumulus_hardware.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
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
/******************************************************************************\
* Copyright (c) 2020-2022
* Author(s): Volker Fischer
******************************************************************************
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\******************************************************************************/
#include "edrumulus_hardware.h"
Edrumulus_hardware* edrumulus_hardware_pointer = nullptr;
Edrumulus_hardware::Edrumulus_hardware()
{
// global pointer to this class needed for static callback function
edrumulus_hardware_pointer = this;
}
// -----------------------------------------------------------------------------
// Teensy 4.0/4.1/3.6 ----------------------------------------------------------
// -----------------------------------------------------------------------------
#ifdef TEENSYDUINO
void Edrumulus_hardware::setup ( const int conf_Fs,
const int number_pads,
const int number_inputs[],
int analog_pin[][MAX_NUM_PAD_INPUTS] )
{
// set essential parameters
Fs = conf_Fs;
// create linear vectors containing the pin information for each pad and pad-input
total_number_inputs = 0; // we use it as a counter, too
for ( int i = 0; i < number_pads; i++ )
{
for ( int j = 0; j < number_inputs[i]; j++ )
{
// store pin number in vector
input_pin[total_number_inputs] = analog_pin[i][j];
total_number_inputs++;
}
}
#ifdef ARDUINO_TEENSY36 // Teensy 3.6 specific code
// A word about conversion time:
// conv_time = SFCAdder + Averages * (BCT + LSTAdder + HSCAdder)
// = 5 ADCK + 5 bus + 1 *(25 ADCK + 0 ADCK + 2 ADCK) = 32 ADCK + 5 bus
// = 13/7.5 MHz + 5/60 MHz = 4.35 us per conversion
// For 22 conversions, 22 * 4.35 us = 95.7 us which is still under 125 us.
// F_BUS is 60 MHz and F_ADC is max allowed 24 MHz in 12-bit mode and 12 MHz in
// 16-bit mode to stay within specs. F_ADC is derived from F_BUS with dividers
// 1, 2, 4, 8, or 16. F_BUS of 60 MHz offers 30, 15, and 7.5 MHz options.
// For 12-bit mode, 15 and 7.5 can work; for 16-bit mode, only 7.5 can work.
// The definition for F_BUS can be found in kinetics.h and it's a function of
// the F_CPU. For F_CPU = 180 MHz, F_BUS = 60 MHz. For F_CPU = 192 MHz, F_BUS = 48 MHz.
// In other words, overclocking the teensy will affect the conversion rate.
adc_obj.adc0->setResolution ( 16 ); // we want to get the full ADC resolution of the Teensy 3.6
adc_obj.adc0->setAveraging ( 1 );
adc_obj.adc0->setConversionSpeed ( ADC_CONVERSION_SPEED::HIGH_SPEED_16BITS );
adc_obj.adc0->setSamplingSpeed ( ADC_SAMPLING_SPEED::VERY_HIGH_SPEED );
adc_obj.adc1->setResolution ( 16 ); // we want to get the full ADC resolution of the Teensy 3.6
adc_obj.adc1->setAveraging ( 1 );
adc_obj.adc1->setConversionSpeed ( ADC_CONVERSION_SPEED::HIGH_SPEED_16BITS );
adc_obj.adc1->setSamplingSpeed ( ADC_SAMPLING_SPEED::VERY_HIGH_SPEED );
#else
// set the ADC properties: averaging 8 samples with high speed sampling gives
// us the best compromise between ADC speed and spike protection
adc_obj.adc0->setResolution ( 12 ); // we want to get the full ADC resolution of the Teensy 4.0
adc_obj.adc0->setAveraging ( 8 );
adc_obj.adc0->setConversionSpeed ( ADC_CONVERSION_SPEED::HIGH_SPEED );
adc_obj.adc0->setSamplingSpeed ( ADC_SAMPLING_SPEED::HIGH_SPEED );
adc_obj.adc1->setResolution ( 12 ); // we want to get the full ADC resolution of the Teensy 4.0
adc_obj.adc1->setAveraging ( 8 );
adc_obj.adc1->setConversionSpeed ( ADC_CONVERSION_SPEED::HIGH_SPEED );
adc_obj.adc1->setSamplingSpeed ( ADC_SAMPLING_SPEED::HIGH_SPEED );
#endif
#if defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41) // Teensy 4.0/4.1 specific code
// disable MIMXRT1062DVL6A "keeper" on all possible Teensy 4.0/4.1 ADC input pins
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_02 &= ~( 1 << 12 ); // A0
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_03 &= ~( 1 << 12 ); // A1
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_07 &= ~( 1 << 12 ); // A2
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_06 &= ~( 1 << 12 ); // A3
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_01 &= ~( 1 << 12 ); // A4
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_00 &= ~( 1 << 12 ); // A5
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_10 &= ~( 1 << 12 ); // A6
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_11 &= ~( 1 << 12 ); // A7
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_08 &= ~( 1 << 12 ); // A8
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_09 &= ~( 1 << 12 ); // A9
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B0_12 &= ~( 1 << 12 ); // A10
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B0_13 &= ~( 1 << 12 ); // A11
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_14 &= ~( 1 << 12 ); // A12
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_15 &= ~( 1 << 12 ); // A13
#endif
// initialize timer flag (semaphore)
timer_ready = false;
// prepare timer at a rate of given sampling rate
myTimer.begin ( on_timer, 1000000 / Fs ); // here we define the sampling rate (1 MHz / Fs)
}
void Edrumulus_hardware::write_setting ( const int pad_index,
const int address,
const byte value )
{
EEPROM.update ( pad_index * MAX_NUM_SET_PER_PAD + address, value );
}
byte Edrumulus_hardware::read_setting ( const int pad_index,
const int address )
{
return EEPROM.read ( pad_index * MAX_NUM_SET_PER_PAD + address );
}
void Edrumulus_hardware::on_timer()
{
// tell the main loop that a sample can be read by setting the flag (semaphore)
edrumulus_hardware_pointer->timer_ready = true;
}
void Edrumulus_hardware::capture_samples ( const int number_pads,
const int number_inputs[],
int analog_pin[][MAX_NUM_PAD_INPUTS],
int sample_org[][MAX_NUM_PAD_INPUTS] )
{
// wait for the timer to get the correct sampling rate when reading the analog value
while ( !timer_ready ) delayMicroseconds ( 5 );
timer_ready = false; // it is important to reset the flag here
// read the ADC samples
for ( int i = 0; i < total_number_inputs; i++ )
{
// pins 12 and 13 (and 22 for Teensy 3.6) are ADC1 only, pins 10 and 11 are ADC0 only
// note that pin 8 gave large spikes on ADC0 but seems to work ok with ADC1
if ( ( input_pin[i] == 8 ) || ( input_pin[i] == 12 ) || ( input_pin[i] == 13 ) || ( input_pin[i] == 22 ) )
{
input_sample[i] = adc_obj.adc1->analogRead ( input_pin[i] );
}
else
{
input_sample[i] = adc_obj.adc0->analogRead ( input_pin[i] );
}
}
// copy captured samples in pad buffer
int input_cnt = 0;
for ( int i = 0; i < number_pads; i++ )
{
for ( int j = 0; j < number_inputs[i]; j++ )
{
sample_org[i][j] = input_sample[input_cnt++];
}
}
}
#endif
// -----------------------------------------------------------------------------
// ESP32 Dual Core -------------------------------------------------------------
// -----------------------------------------------------------------------------
#ifdef ESP_PLATFORM
void Edrumulus_hardware::setup ( const int conf_Fs,
const int number_pads,
const int number_inputs[],
int analog_pin[][MAX_NUM_PAD_INPUTS] )
{
// set essential parameters
Fs = conf_Fs;
eeprom_settings.begin ( ( number_pads + 1 ) * MAX_NUM_SET_PER_PAD ); // "+ 1" for pad-independent global settings
// create linear vectors containing the pin/ADC information for each pad and pad-input
bool input_is_used[MAX_NUM_PADS * MAX_NUM_PAD_INPUTS];
int input_adc[MAX_NUM_PADS * MAX_NUM_PAD_INPUTS];
total_number_inputs = 0; // we use it as a counter, too
for ( int i = 0; i < number_pads; i++ )
{
for ( int j = 0; j < number_inputs[i]; j++ )
{
// store pin number in vector and identify ADC number for each pin
input_pin[total_number_inputs] = analog_pin[i][j];
input_adc[total_number_inputs] = ( digitalPinToAnalogChannel ( analog_pin[i][j] ) >= 10 ); // channel < 10 -> ADC1, channel >= 10 -> ADC2
input_is_used[total_number_inputs] = false; // initialization needed for ADC pairs identification
total_number_inputs++;
}
}
// find ADC pairs, i.e., one pin uses ADC1 and the other uses ADC2
num_pin_pairs = 0; // we use it as a counter, too
for ( int i = 0; i < total_number_inputs - 1; i++ )
{
if ( !input_is_used[i] )
{
for ( int j = total_number_inputs - 1; j > i; j-- )
{
// check for different ADCs: 0+0=0, 1+0=1 (pair), 0+1=1 (pair), 1+1=2
if ( !input_is_used[j] && ( input_adc[i] + input_adc[j] == 1 ) )
{
if ( input_adc[i] == 0 )
{
adc1_index[num_pin_pairs] = i;
adc2_index[num_pin_pairs] = j;
}
else
{
adc1_index[num_pin_pairs] = j;
adc2_index[num_pin_pairs] = i;
}
// create the mask bit needed for SENS_SAR1_EN_PAD and SENS_SAR2_EN_PAD
const int8_t channel_adc1 = digitalPinToAnalogChannel ( input_pin[adc1_index[num_pin_pairs]] );
const int8_t channel_adc2 = digitalPinToAnalogChannel ( input_pin[adc2_index[num_pin_pairs]] ) - 10;
channel_adc1_bitval[num_pin_pairs] = ( 1 << channel_adc1 );
channel_adc2_bitval[num_pin_pairs] = ( 1 << channel_adc2 );
num_pin_pairs++;
input_is_used[i] = true;
input_is_used[j] = true;
break;
}
}
}
}
// find remaining single pins which we cannot create an ADC pair with
num_pin_single = 0; // we use it as a counter, too
for ( int i = 0; i < total_number_inputs; i++ )
{
if ( !input_is_used[i] )
{
single_index[num_pin_single] = i;
num_pin_single++;
}
}
// prepare the ADC and analog GPIO inputs
init_my_analogRead();
// create timer semaphore
timer_semaphore = xSemaphoreCreateBinary();
// create task pinned to core 0 for creating the timer interrupt so that the
// timer function is not running in our working core 1
xTaskCreatePinnedToCore ( start_timer_core0_task, "start_timer_core0_task", 800, this, 1, NULL, 0 );
}
void Edrumulus_hardware::write_setting ( const int pad_index,
const int address,
const byte value )
{
setup_timer ( true ); // clear timer to avoid a crash during writing the EEPROM settings
eeprom_settings.write ( pad_index * MAX_NUM_SET_PER_PAD + address, value );
eeprom_settings.commit(); // needed to update actual flash memory, will only be written if different
setup_timer(); // restart timer
}
byte Edrumulus_hardware::read_setting ( const int pad_index,
const int address )
{
return eeprom_settings.read ( pad_index * MAX_NUM_SET_PER_PAD + address );
}
void Edrumulus_hardware::setup_timer ( const bool clear_timer )
{
if ( !clear_timer )
{
// prepare timer at a rate of given sampling rate
timer = timerBegin ( 0, 80, true ); // prescaler of 80 (i.e. below we have 1 MHz instead of 80 MHz)
timerAttachInterrupt ( timer, &on_timer, true );
timerAlarmWrite ( timer, 1000000 / Fs, true ); // here we define the sampling rate (1 MHz / Fs)
timerAlarmEnable ( timer );
}
else
{
// clear timer to release SPI resources for writing EEPROM data
timerAlarmDisable ( timer );
timerDetachInterrupt ( timer );
timerEnd ( timer );
}
}
void Edrumulus_hardware::start_timer_core0_task ( void* param )
{
reinterpret_cast<Edrumulus_hardware*> ( param )->setup_timer();
// tasks must not return: forever loop with delay to keep watchdog happy
for ( ; ; )
{
delay ( 1000 );
}
}
void IRAM_ATTR Edrumulus_hardware::on_timer()
{
// first read the ADC pairs samples
for ( int i = 0; i < edrumulus_hardware_pointer->num_pin_pairs; i++ )
{
edrumulus_hardware_pointer->my_analogRead_parallel (
edrumulus_hardware_pointer->channel_adc1_bitval[i],
edrumulus_hardware_pointer->channel_adc2_bitval[i],
edrumulus_hardware_pointer->input_sample[edrumulus_hardware_pointer->adc1_index[i]],
edrumulus_hardware_pointer->input_sample[edrumulus_hardware_pointer->adc2_index[i]] );
}
// second read the single ADC samples
for ( int i = 0; i < edrumulus_hardware_pointer->num_pin_single; i++ )
{
edrumulus_hardware_pointer->input_sample[edrumulus_hardware_pointer->single_index[i]] =
edrumulus_hardware_pointer->my_analogRead ( edrumulus_hardware_pointer->input_pin[edrumulus_hardware_pointer->single_index[i]] );
}
// tell the main loop that a sample can be processed by setting the semaphore
static BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR ( edrumulus_hardware_pointer->timer_semaphore, &xHigherPriorityTaskWoken );
if ( xHigherPriorityTaskWoken == pdTRUE )
{
portYIELD_FROM_ISR();
}
}
void Edrumulus_hardware::capture_samples ( const int number_pads,
const int number_inputs[],
int analog_pin[][MAX_NUM_PAD_INPUTS],
int sample_org[][MAX_NUM_PAD_INPUTS] )
{
// wait for the timer to get the correct sampling rate when reading the analog value
if ( xSemaphoreTake ( timer_semaphore, portMAX_DELAY ) == pdTRUE )
{
// copy captured samples in pad buffer
int input_cnt = 0;
for ( int i = 0; i < number_pads; i++ )
{
for ( int j = 0; j < number_inputs[i]; j++ )
{
sample_org[i][j] = input_sample[input_cnt++];
}
}
}
}
// Since arduino-esp32 library version 1.0.5, the analogRead was changed to use the IDF interface
// which made the analogRead function so slow that we cannot use that anymore for Edrumulus:
// https://github.com/espressif/arduino-esp32/issues/4973, https://github.com/espressif/arduino-esp32/pull/3377
// As a workaround, we had to write our own analogRead function.
void Edrumulus_hardware::init_my_analogRead()
{
// if the GIOP 25/26 are used, we have to set the DAC to 0 to get correct DC offset
// estimates and reduce the number of large spikes
dac_i2s_enable();
dac_output_enable ( DAC_CHANNEL_1 );
dac_output_voltage ( DAC_CHANNEL_1, 0 );
dac_output_disable ( DAC_CHANNEL_1 );
dac_output_enable ( DAC_CHANNEL_2 );
dac_output_voltage ( DAC_CHANNEL_2, 0 );
dac_output_disable ( DAC_CHANNEL_2 );
dac_i2s_disable();
// set attenuation of 11 dB
WRITE_PERI_REG ( SENS_SAR_ATTEN1_REG, 0x0FFFFFFFF );
WRITE_PERI_REG ( SENS_SAR_ATTEN2_REG, 0x0FFFFFFFF );
// set both ADCs to 12 bit resolution using 8 cycles and 1 sample
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL_REG, SENS_SAR1_SAMPLE_CYCLE, 8, SENS_SAR1_SAMPLE_CYCLE_S ); // cycles
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL2_REG, SENS_SAR2_SAMPLE_CYCLE, 8, SENS_SAR2_SAMPLE_CYCLE_S );
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL_REG, SENS_SAR1_SAMPLE_NUM, 0, SENS_SAR1_SAMPLE_NUM_S ); // # samples
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL2_REG, SENS_SAR2_SAMPLE_NUM, 0, SENS_SAR2_SAMPLE_NUM_S );
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL_REG, SENS_SAR1_CLK_DIV, 1, SENS_SAR1_CLK_DIV_S ); // clock div
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL2_REG, SENS_SAR2_CLK_DIV, 1, SENS_SAR2_CLK_DIV_S );
SET_PERI_REG_BITS ( SENS_SAR_START_FORCE_REG, SENS_SAR1_BIT_WIDTH, 3, SENS_SAR1_BIT_WIDTH_S ); // width
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL_REG, SENS_SAR1_SAMPLE_BIT, 3, SENS_SAR1_SAMPLE_BIT_S );
SET_PERI_REG_BITS ( SENS_SAR_START_FORCE_REG, SENS_SAR2_BIT_WIDTH, 3, SENS_SAR2_BIT_WIDTH_S );
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL2_REG, SENS_SAR2_SAMPLE_BIT, 3, SENS_SAR2_SAMPLE_BIT_S );
// some other initializations
SET_PERI_REG_MASK ( SENS_SAR_READ_CTRL_REG, SENS_SAR1_DATA_INV );
SET_PERI_REG_MASK ( SENS_SAR_READ_CTRL2_REG, SENS_SAR2_DATA_INV );
SET_PERI_REG_MASK ( SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_FORCE_M ); // SAR ADC1 controller (in RTC) is started by SW
SET_PERI_REG_MASK ( SENS_SAR_MEAS_START1_REG, SENS_SAR1_EN_PAD_FORCE_M ); // SAR ADC1 pad enable bitmap is controlled by SW
SET_PERI_REG_MASK ( SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_FORCE_M ); // SAR ADC2 controller (in RTC) is started by SW
SET_PERI_REG_MASK ( SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD_FORCE_M ); // SAR ADC2 pad enable bitmap is controlled by SW
CLEAR_PERI_REG_MASK ( SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR_M ); // force XPD_SAR=0, use XPD_FSM
SET_PERI_REG_BITS ( SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_AMP, 0x2, SENS_FORCE_XPD_AMP_S ); // force XPD_AMP=0
CLEAR_PERI_REG_MASK ( SENS_SAR_MEAS_CTRL_REG, 0xfff << SENS_AMP_RST_FB_FSM_S ); // clear FSM
SET_PERI_REG_BITS ( SENS_SAR_MEAS_WAIT1_REG, SENS_SAR_AMP_WAIT1, 0x1, SENS_SAR_AMP_WAIT1_S );
SET_PERI_REG_BITS ( SENS_SAR_MEAS_WAIT1_REG, SENS_SAR_AMP_WAIT2, 0x1, SENS_SAR_AMP_WAIT2_S );
SET_PERI_REG_BITS ( SENS_SAR_MEAS_WAIT2_REG, SENS_SAR_AMP_WAIT3, 0x1, SENS_SAR_AMP_WAIT3_S );
while ( GET_PERI_REG_BITS2 ( SENS_SAR_SLAVE_ADDR1_REG, 0x7, SENS_MEAS_STATUS_S ) != 0 );
// configure all pins to analog read
for ( int i = 0; i < total_number_inputs; i++ )
{
pinMode ( input_pin[i], ANALOG );
}
}
uint16_t Edrumulus_hardware::my_analogRead ( const uint8_t pin )
{
const int8_t channel = digitalPinToAnalogChannel ( pin );
if ( channel > 9 )
{
const int8_t channel_modified = channel - 10;
CLEAR_PERI_REG_MASK ( SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_SAR_M );
SET_PERI_REG_BITS ( SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD, ( 1 << channel_modified ), SENS_SAR2_EN_PAD_S );
SET_PERI_REG_MASK ( SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_SAR_M );
while ( GET_PERI_REG_MASK ( SENS_SAR_MEAS_START2_REG, SENS_MEAS2_DONE_SAR ) == 0 );
return GET_PERI_REG_BITS2 ( SENS_SAR_MEAS_START2_REG, SENS_MEAS2_DATA_SAR, SENS_MEAS2_DATA_SAR_S );
}
else
{
CLEAR_PERI_REG_MASK ( SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_SAR_M );
SET_PERI_REG_BITS ( SENS_SAR_MEAS_START1_REG, SENS_SAR1_EN_PAD, ( 1 << channel ), SENS_SAR1_EN_PAD_S );
SET_PERI_REG_MASK ( SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_SAR_M );
while ( GET_PERI_REG_MASK ( SENS_SAR_MEAS_START1_REG, SENS_MEAS1_DONE_SAR ) == 0 );
return GET_PERI_REG_BITS2 ( SENS_SAR_MEAS_START1_REG, SENS_MEAS1_DATA_SAR, SENS_MEAS1_DATA_SAR_S );
}
}
void Edrumulus_hardware::my_analogRead_parallel ( const uint32_t channel_adc1_bitval,
const uint32_t channel_adc2_bitval,
uint16_t& out_adc1,
uint16_t& out_adc2 )
{
// start ADC1
CLEAR_PERI_REG_MASK ( SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_SAR_M );
SET_PERI_REG_BITS ( SENS_SAR_MEAS_START1_REG, SENS_SAR1_EN_PAD, channel_adc1_bitval, SENS_SAR1_EN_PAD_S );
SET_PERI_REG_MASK ( SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_SAR_M );
// start ADC2
CLEAR_PERI_REG_MASK ( SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_SAR_M );
SET_PERI_REG_BITS ( SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD, channel_adc2_bitval, SENS_SAR2_EN_PAD_S );
SET_PERI_REG_MASK ( SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_SAR_M );
// wait for ADC1 and read value
while ( GET_PERI_REG_MASK ( SENS_SAR_MEAS_START1_REG, SENS_MEAS1_DONE_SAR ) == 0 );
out_adc1 = GET_PERI_REG_BITS2 ( SENS_SAR_MEAS_START1_REG, SENS_MEAS1_DATA_SAR, SENS_MEAS1_DATA_SAR_S );
// wait for ADC2 and read value
while ( GET_PERI_REG_MASK ( SENS_SAR_MEAS_START2_REG, SENS_MEAS2_DONE_SAR ) == 0 );
out_adc2 = GET_PERI_REG_BITS2 ( SENS_SAR_MEAS_START2_REG, SENS_MEAS2_DATA_SAR, SENS_MEAS2_DATA_SAR_S );
}
#endif
// -----------------------------------------------------------------------------
// Common hardware functions ---------------------------------------------------
// -----------------------------------------------------------------------------
float Edrumulus_hardware::cancel_ADC_spikes ( const float input,
const int pad_index,
const int input_channel_index,
const int level )
{
// remove single/dual sample spikes by checking if right before and right after the
// detected spike(s) we only have noise and no useful signal (since the ESP32 spikes
// mostly are on just one or two sample(s))
const int max_peak_threshold = 150; // maximum assumed ESP32 spike amplitude
float return_value = prev_input4[pad_index][input_channel_index]; // normal return value in case no spike was detected
const float input_abs = abs ( input );
Espikestate input_state = ST_OTHER; // initialization value, might be overwritten
if ( input_abs < ADC_MAX_NOISE_AMPL )
{
input_state = ST_NOISE;
}
else if ( ( input < max_peak_threshold ) && ( input > 0 ) )
{
input_state = ST_SPIKE_HIGH;
}
else if ( ( input > -max_peak_threshold ) && ( input < 0 ) )
{
input_state = ST_SPIKE_LOW;
}
// check for single high spike sample case
if ( ( ( prev5_input_state[pad_index][input_channel_index] == ST_NOISE ) || ( prev5_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) ) &&
( prev4_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) &&
( ( prev3_input_state[pad_index][input_channel_index] == ST_NOISE ) || ( prev3_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) ) )
{
return_value = 0.0f; // remove single spike
}
// check for single low spike sample case
if ( ( ( prev5_input_state[pad_index][input_channel_index] == ST_NOISE ) || ( prev5_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) ) &&
( prev4_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) &&
( ( prev3_input_state[pad_index][input_channel_index] == ST_NOISE ) || ( prev3_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) ) )
{
return_value = 0.0f; // remove single spike
}
if ( level >= 2 )
{
// check for two sample spike case
if ( ( ( prev5_input_state[pad_index][input_channel_index] == ST_NOISE ) || ( prev5_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) ) &&
( prev4_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) &&
( prev3_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) &&
( ( prev2_input_state[pad_index][input_channel_index] == ST_NOISE ) || ( prev2_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) ) )
{
return_value = 0.0f; // remove two sample spike
prev_input3[pad_index][input_channel_index] = 0.0f; // remove two sample spike
}
// check for two sample low spike case
if ( ( ( prev5_input_state[pad_index][input_channel_index] == ST_NOISE ) || ( prev5_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) ) &&
( prev4_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) &&
( prev3_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) &&
( ( prev2_input_state[pad_index][input_channel_index] == ST_NOISE ) || ( prev2_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) ) )
{
return_value = 0.0f; // remove two sample spike
prev_input3[pad_index][input_channel_index] = 0.0f; // remove two sample spike
}
}
if ( level >= 3 )
{
// check for three sample high spike case
if ( ( ( prev5_input_state[pad_index][input_channel_index] == ST_NOISE ) || ( prev5_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) ) &&
( prev4_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) &&
( prev3_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) &&
( prev2_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) &&
( ( prev1_input_state[pad_index][input_channel_index] == ST_NOISE ) || ( prev1_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) ) )
{
return_value = 0.0f; // remove three sample spike
prev_input3[pad_index][input_channel_index] = 0.0f; // remove three sample spike
prev_input2[pad_index][input_channel_index] = 0.0f; // remove three sample spike
}
// check for three sample low spike case
if ( ( ( prev5_input_state[pad_index][input_channel_index] == ST_NOISE ) || ( prev5_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) ) &&
( prev4_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) &&
( prev3_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) &&
( prev2_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) &&
( ( prev1_input_state[pad_index][input_channel_index] == ST_NOISE ) || ( prev1_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) ) )
{
return_value = 0.0f; // remove three sample spike
prev_input3[pad_index][input_channel_index] = 0.0f; // remove three sample spike
prev_input2[pad_index][input_channel_index] = 0.0f; // remove three sample spike
}
}
if ( level >= 4 )
{
// check for four sample high spike case
if ( ( ( prev5_input_state[pad_index][input_channel_index] == ST_NOISE ) || ( prev5_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) ) &&
( prev4_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) &&
( prev3_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) &&
( prev2_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) &&
( prev1_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) &&
( ( input_state == ST_NOISE ) || ( input_state == ST_SPIKE_LOW ) ) )
{
return_value = 0.0f; // remove four sample spike
prev_input3[pad_index][input_channel_index] = 0.0f; // remove four sample spike
prev_input2[pad_index][input_channel_index] = 0.0f; // remove four sample spike
prev_input1[pad_index][input_channel_index] = 0.0f; // remove four sample spike
}
// check for four sample low spike case
if ( ( ( prev5_input_state[pad_index][input_channel_index] == ST_NOISE ) || ( prev5_input_state[pad_index][input_channel_index] == ST_SPIKE_HIGH ) ) &&
( prev4_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) &&
( prev3_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) &&
( prev2_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) &&
( prev1_input_state[pad_index][input_channel_index] == ST_SPIKE_LOW ) &&
( ( input_state == ST_NOISE ) || ( input_state == ST_SPIKE_HIGH ) ) )
{
return_value = 0.0f; // remove four sample spike
prev_input3[pad_index][input_channel_index] = 0.0f; // remove four sample spike
prev_input2[pad_index][input_channel_index] = 0.0f; // remove four sample spike
prev_input1[pad_index][input_channel_index] = 0.0f; // remove four sample spike
}
}
// update five-step input signal memory where we store the last five states of
// the input signal and four previous untouched input samples
prev5_input_state[pad_index][input_channel_index] = prev4_input_state[pad_index][input_channel_index];
prev4_input_state[pad_index][input_channel_index] = prev3_input_state[pad_index][input_channel_index];
prev3_input_state[pad_index][input_channel_index] = prev2_input_state[pad_index][input_channel_index];
prev2_input_state[pad_index][input_channel_index] = prev1_input_state[pad_index][input_channel_index];
prev_input4[pad_index][input_channel_index] = prev_input3[pad_index][input_channel_index];
prev_input3[pad_index][input_channel_index] = prev_input2[pad_index][input_channel_index];
prev_input2[pad_index][input_channel_index] = prev_input1[pad_index][input_channel_index];
// adjust the latency of the algorithm according to the spike cancellation
// level, i.e., the higher the level, the higher the latency
if ( level >= 3 )
{
prev1_input_state[pad_index][input_channel_index] = input_state;
prev_input1[pad_index][input_channel_index] = input;
}
else if ( level >= 2 )
{
prev2_input_state[pad_index][input_channel_index] = input_state;
prev_input2[pad_index][input_channel_index] = input;
}
else
{
prev3_input_state[pad_index][input_channel_index] = input_state;
prev_input3[pad_index][input_channel_index] = input;
}
return return_value;
}