Skip to content

Commit f1808b2

Browse files
adding some error checking in adc begin()
1 parent 9bd88c0 commit f1808b2

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/AdvancedADC.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,22 @@ int AdvancedADC::begin(uint32_t resolution, uint32_t sample_rate, size_t n_sampl
184184
descr->dmabuf[1] = descr->pool->allocate();
185185

186186
// Init and config DMA.
187-
hal_dma_config(&descr->dma, descr->dma_irqn, DMA_PERIPH_TO_MEMORY);
187+
if (hal_dma_config(&descr->dma, descr->dma_irqn, DMA_PERIPH_TO_MEMORY) < 0) return 0;
188188

189189
// Init and config ADC.
190-
hal_adc_config(&descr->adc, ADC_RES_LUT[resolution], descr->tim_trig, adc_pins, n_channels);
190+
if (hal_adc_config(&descr->adc, ADC_RES_LUT[resolution], descr->tim_trig, adc_pins, n_channels) < 0) return 0;
191191

192192
// Link DMA handle to ADC handle, and start the ADC.
193193
__HAL_LINKDMA(&descr->adc, DMA_Handle, descr->dma);
194-
HAL_ADC_Start_DMA(&descr->adc, (uint32_t *) descr->dmabuf[0]->data(), descr->dmabuf[0]->size());
194+
if (HAL_ADC_Start_DMA(&descr->adc, (uint32_t *) descr->dmabuf[0]->data(), descr->dmabuf[0]->size()) != HAL_OK) return 0;
195195

196196
// Re/enable DMA double buffer mode.
197197
hal_dma_enable_dbm(&descr->dma, descr->dmabuf[0]->data(), descr->dmabuf[1]->data());
198198

199199
// Init, config and start the ADC timer.
200200
hal_tim_config(&descr->tim, sample_rate);
201-
HAL_TIM_Base_Start(&descr->tim);
201+
if (HAL_TIM_Base_Start(&descr->tim) != HAL_OK) return 0;
202+
202203
return 1;
203204
}
204205

src/HALConfig.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,10 @@ int hal_adc_config(ADC_HandleTypeDef *adc, uint32_t resolution, uint32_t trigger
195195
adc->Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
196196
adc->Init.ConversionDataManagement = ADC_CONVERSIONDATA_DMA_CIRCULAR;
197197

198-
HAL_ADC_Init(adc);
199-
HAL_ADCEx_Calibration_Start(adc, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
198+
if (HAL_ADC_Init(adc) != HAL_OK
199+
|| HAL_ADCEx_Calibration_Start(adc, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK) {
200+
return -1;
201+
}
200202

201203
ADC_ChannelConfTypeDef sConfig = {0};
202204
sConfig.Offset = 0;
@@ -209,7 +211,7 @@ int hal_adc_config(ADC_HandleTypeDef *adc, uint32_t resolution, uint32_t trigger
209211
uint32_t channel = STM_PIN_CHANNEL(function);
210212
sConfig.Rank = ADC_RANK_LUT[rank];
211213
sConfig.Channel = __HAL_ADC_DECIMAL_NB_TO_CHANNEL(channel);
212-
HAL_ADC_ConfigChannel(adc, &sConfig);
214+
if (HAL_ADC_ConfigChannel(adc, &sConfig) != HAL_OK) return -1;
213215
}
214216

215217
return 0;

0 commit comments

Comments
 (0)