Skip to content

Commit 878271e

Browse files
Verify ADCInput buffer sizes legal for 3 and 4 inputs (earlephilhower#2996)
Related to earlephilhower#2991, the ADCInput bufferWords needs to be a size that always has the 1st ADC sample at offset 0:0. For 1 and 2 inputs that's guaranteed. For 3 inputs we need a multiple of 3 words to ensure things always align in the case of overflow. For 4 inputs, a multiple of 2 words is needed.
1 parent 1df647a commit 878271e

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

libraries/ADCInput/src/ADCInput.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <Arduino.h>
2424
#include "ADCInput.h"
2525
#include <hardware/adc.h>
26+
#include <debug_internal.h>
2627

2728
ADCInput::ADCInput(pin_size_t p0, pin_size_t p1, pin_size_t p2, pin_size_t p3, pin_size_t p4, pin_size_t p5, pin_size_t p6, pin_size_t p7) {
2829
_running = false;
@@ -122,6 +123,18 @@ bool ADCInput::begin() {
122123
adc_gpio_init(pin);
123124
}
124125
}
126+
127+
// Make sure the bufferWords is a multiple of the natural size.
128+
// For 1 and 2 inputs there will never be a misalignment on overflow
129+
// For 3 inputs we need to have a multiple of 3 words to guarantee that every buffer[0] is ADC[0]. OTW we can slip on an overflow
130+
// Data = [ADC0:ADC1, ADC2:ADC0, ADC1:ADC2], [ADC0:ADC1, ADC2:ADC0, ADC1:ADC2,ADC3], ...
131+
// For 4 inputs we need to have a multiple of 2 words, same reason. See #2991
132+
// Data = [ADC0:ADC1, ADC2:ADC3] [ADC0:ADC1, ADC2:ADC3]
133+
if (((cnt == 3) && (_bufferWords % 3)) || ((cnt == 4) && (_bufferWords % 2))) {
134+
DEBUGV("ADCInput: bufferWords needs to be a multiple of 3 for 3 inputs, 2 for 4 inputs\n");
135+
return false;
136+
}
137+
125138
adc_set_round_robin(_pinMask);
126139
adc_fifo_setup(true, true, 1, false, false);
127140

0 commit comments

Comments
 (0)