Fix problem with ADC reads and multiple channels

This commit is contained in:
Dave Hylands 2014-06-16 09:41:58 -07:00
parent 2547928148
commit 535b88133c

View File

@ -80,7 +80,7 @@ typedef struct _pyb_obj_adc_t {
ADC_HandleTypeDef handle; ADC_HandleTypeDef handle;
} pyb_obj_adc_t; } pyb_obj_adc_t;
void adc_init_single(pyb_obj_adc_t *adc_obj) { STATIC void adc_init_single(pyb_obj_adc_t *adc_obj) {
if (!IS_ADC_CHANNEL(adc_obj->channel)) { if (!IS_ADC_CHANNEL(adc_obj->channel)) {
return; return;
} }
@ -114,7 +114,9 @@ void adc_init_single(pyb_obj_adc_t *adc_obj) {
adcHandle->Init.EOCSelection = DISABLE; adcHandle->Init.EOCSelection = DISABLE;
HAL_ADC_Init(adcHandle); HAL_ADC_Init(adcHandle);
}
STATIC void adc_config_channel(pyb_obj_adc_t *adc_obj) {
ADC_ChannelConfTypeDef sConfig; ADC_ChannelConfTypeDef sConfig;
sConfig.Channel = adc_obj->channel; sConfig.Channel = adc_obj->channel;
@ -122,10 +124,10 @@ void adc_init_single(pyb_obj_adc_t *adc_obj) {
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES; sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
sConfig.Offset = 0; sConfig.Offset = 0;
HAL_ADC_ConfigChannel(adcHandle, &sConfig); HAL_ADC_ConfigChannel(&adc_obj->handle, &sConfig);
} }
uint32_t adc_read_channel(ADC_HandleTypeDef *adcHandle) { STATIC uint32_t adc_read_channel(ADC_HandleTypeDef *adcHandle) {
uint32_t rawValue = 0; uint32_t rawValue = 0;
HAL_ADC_Start(adcHandle); 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_
STATIC mp_obj_t adc_read(mp_obj_t self_in) { STATIC mp_obj_t adc_read(mp_obj_t self_in) {
pyb_obj_adc_t *self = self_in; pyb_obj_adc_t *self = self_in;
adc_config_channel(self);
uint32_t data = adc_read_channel(&self->handle); uint32_t data = adc_read_channel(&self->handle);
return mp_obj_new_int(data); return mp_obj_new_int(data);
} }
@ -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_
// This uses the timer in polling mode to do the sampling // This uses the timer in polling mode to do the sampling
// We could use DMA, but then we can't convert the values correctly for the buffer // We could use DMA, but then we can't convert the values correctly for the buffer
adc_config_channel(self);
for (uint index = 0; index < bufinfo.len; index++) { for (uint index = 0; index < bufinfo.len; index++) {
// Wait for the timer to trigger // Wait for the timer to trigger
while (__HAL_TIM_GET_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE) == RESET) { while (__HAL_TIM_GET_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE) == RESET) {