8000 Addition ABM checks in PWMAudio and ADCInput (#1530) · esphome/arduino-pico@5b76b06 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5b76b06

Browse files
Addition ABM checks in PWMAudio and ADCInput (earlephilhower#1530)
Handle the case where the DMA manager is unable to completely allocate needed resources (DMA channels or memory) and return `false` in ::begin()
1 parent d18f8dc commit 5b76b06

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

libraries/ADCInput/src/ADCInput.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ void ADCInput::onReceive(void(*fn)(void)) {
7979
}
8080

8181
bool ADCInput::begin() {
82+
if (_running) {
83+
return false;
84+
}
85+
8286
_running = true;
8387

8488
_isHolding = 0;
@@ -105,7 +109,11 @@ bool ADCInput::begin() {
105109
setFrequency(_freq);
106110

107111
_arb = new AudioBufferManager(_buffers, _bufferWords, 0, INPUT, DMA_SIZE_16);
108-
_arb->begin(DREQ_ADC, (volatile void*)&adc_hw->fifo);
112+
if (!_arb->begin(DREQ_ADC, (volatile void*)&adc_hw->fifo)) {
113+
delete _arb;
114+
_arb = nullptr;
115+
return false;
116+
}
109117
_arb->setCallback(_cb);
110118

111119
adc_fifo_drain();

libraries/PWMAudio/src/PWMAudio.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ void PWMAudio::onTransmit(void(*fn)(void)) {
100100
}
101101

102102
bool PWMAudio::begin() {
103+
if (_running) {
104+
return false;
105+
}
106+
103107
if (_stereo && (_pin & 1)) {
104108
// Illegal, need to have consecutive pins on the same PWM slice
105109
Serial.printf("ERROR: PWMAudio stereo mode requires pin be even\n");
@@ -118,7 +122,13 @@ bool PWMAudio::begin() {
118122
uint32_t ccAddr = PWM_BASE + PWM_CH0_CC_OFFSET + pwm_gpio_to_slice_num(_pin) * 20;
119123

120124
_arb = new AudioBufferManager(_buffers, _bufferWords, 0x80008000, OUTPUT, DMA_SIZE_32);
121-
_arb->begin(pwm_get_dreq(pwm_gpio_to_slice_num(_pin)), (volatile void*)ccAddr);
125+
if (!_arb->begin(pwm_get_dreq(pwm_gpio_to_slice_num(_pin)), (volatile void*)ccAddr)) {
126+
_running = false;
127+
delete _arb;
128+
_arb = nullptr;
129+
return false;
130+
}
131+
122132
_arb->setCallback(_cb);
123133

124134
return true;

0 commit comments

Comments
 (0)
0