Skip to content

Commit 535b881

Browse files
committed
Fix problem with ADC reads and multiple channels
1 parent 2547928 commit 535b881

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

stmhal/adc.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ typedef struct _pyb_obj_adc_t {
8080
ADC_HandleTypeDef handle;
8181
} pyb_obj_adc_t;
8282

83-
void adc_init_single(pyb_obj_adc_t *adc_obj) {
83+
STATIC void adc_init_single(pyb_obj_adc_t *adc_obj) {
8484
if (!IS_ADC_CHANNEL(adc_obj->channel)) {
8585
return;
8686
}
@@ -114,18 +114,20 @@ void adc_init_single(pyb_obj_adc_t *adc_obj) {
114114
adcHandle->Init.EOCSelection = DISABLE;
115115

116116
HAL_ADC_Init(adcHandle);
117+
}
117118

119+
STATIC void adc_config_channel(pyb_obj_adc_t *adc_obj) {
118120
ADC_ChannelConfTypeDef sConfig;
119121

120122
sConfig.Channel = adc_obj->channel;
121123
sConfig.Rank = 1;
122124
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
123125
sConfig.Offset = 0;
124126

125-
HAL_ADC_ConfigChannel(adcHandle, &sConfig);
127+
HAL_ADC_ConfigChannel(&adc_obj->handle, &sConfig);
126128
}
127129

128-
uint32_t adc_read_channel(ADC_HandleTypeDef *adcHandle) {
130+
STATIC uint32_t adc_read_channel(ADC_HandleTypeDef *adcHandle) {
129131
uint32_t rawValue = 0;
130132

131133
HAL_ADC_Start(adcHandle);
@@ -193,6 +195,7 @@ STATIC mp_obj_t adc_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_
193195
STATIC mp_obj_t adc_read(mp_obj_t self_in) {
194196
pyb_obj_adc_t *self = self_in;
195197

198+
adc_config_channel(self);
196199
uint32_t data = adc_read_channel(&self->handle);
197200
return mp_obj_new_int(data);
198201
}
@@ -226,6 +229,7 @@ STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_
226229

227230
// This uses the timer in polling mode to do the sampling
228231
// We could use DMA, but then we can't convert the values correctly for the buffer
232+
adc_config_channel(self);
229233
for (uint index = 0; index < bufinfo.len; index++) {
230234
// Wait for the timer to trigger
231235
while (__HAL_TIM_GET_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE) == RESET) {

0 commit comments

Comments
 (0)