Verify ADCInput buffer sizes legal for 3 and 4 inputs (#2996)

Related to #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.
This commit is contained in:
Earle F. Philhower, III 2025-06-16 12:29:12 -07:00 committed by GitHub
parent 1df647a104
commit 878271e122
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -23,6 +23,7 @@
#include <Arduino.h> #include <Arduino.h>
#include "ADCInput.h" #include "ADCInput.h"
#include <hardware/adc.h> #include <hardware/adc.h>
#include <debug_internal.h>
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) { 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) {
_running = false; _running = false;
@ -122,6 +123,18 @@ bool ADCInput::begin() {
adc_gpio_init(pin); adc_gpio_init(pin);
} }
} }
// Make sure the bufferWords is a multiple of the natural size.
// For 1 and 2 inputs there will never be a misalignment on overflow
// 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
// Data = [ADC0:ADC1, ADC2:ADC0, ADC1:ADC2], [ADC0:ADC1, ADC2:ADC0, ADC1:ADC2,ADC3], ...
// For 4 inputs we need to have a multiple of 2 words, same reason. See #2991
// Data = [ADC0:ADC1, ADC2:ADC3] [ADC0:ADC1, ADC2:ADC3]
if (((cnt == 3) && (_bufferWords % 3)) || ((cnt == 4) && (_bufferWords % 2))) {
DEBUGV("ADCInput: bufferWords needs to be a multiple of 3 for 3 inputs, 2 for 4 inputs\n");
return false;
}
adc_set_round_robin(_pinMask); adc_set_round_robin(_pinMask);
adc_fifo_setup(true, true, 1, false, false); adc_fifo_setup(true, true, 1, false, false);