diff options
author | Damien George <damien.p.george@gmail.com> | 2016-11-30 12:20:23 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2016-11-30 12:58:54 +1100 |
commit | c19a395caceb37a29a4ca74a05858cdaf94d3347 (patch) | |
tree | d7a895c3572df581bbdb502adefc172aefb8486d /stmhal | |
parent | 390ce86a30b83626d420873b63b2749680875c10 (diff) | |
download | micropython-c19a395caceb37a29a4ca74a05858cdaf94d3347.tar.gz micropython-c19a395caceb37a29a4ca74a05858cdaf94d3347.zip |
stmhal/adc: Make channel "16" always map to the temperature sensor.
The temperature sensor on F4 and F7 MCUs is mostly, but not always, on
channel 16. To retain compatibility across all these MCUs this patch
maps the user-facing channel 16 to the internal temperature sensor.
Diffstat (limited to 'stmhal')
-rw-r--r-- | stmhal/adc.c | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/stmhal/adc.c b/stmhal/adc.c index 70f0be1f82..9858190b43 100644 --- a/stmhal/adc.c +++ b/stmhal/adc.c @@ -115,6 +115,18 @@ typedef struct _pyb_obj_adc_t { ADC_HandleTypeDef handle; } pyb_obj_adc_t; +// convert user-facing channel number into internal channel number +static inline uint32_t adc_get_internal_channel(uint32_t channel) { + #if defined(MCU_SERIES_F4) || defined(MCU_SERIES_F7) + // on F4 and F7 MCUs we want channel 16 to always be the TEMPSENSOR + // (on some MCUs ADC_CHANNEL_TEMPSENSOR=16, on others it doesn't) + if (channel == 16) { + channel = ADC_CHANNEL_TEMPSENSOR; + } + #endif + return channel; +} + STATIC bool is_adcx_channel(int channel) { #if defined(MCU_SERIES_F4) || defined(MCU_SERIES_F7) return IS_ADC_CHANNEL(channel); @@ -273,7 +285,7 @@ STATIC mp_obj_t adc_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uin uint32_t channel; if (MP_OBJ_IS_INT(pin_obj)) { - channel = mp_obj_get_int(pin_obj); + channel = adc_get_internal_channel(mp_obj_get_int(pin_obj)); } else { const pin_obj_t *pin = pin_find(pin_obj); if ((pin->adc_num & PIN_ADC1) == 0) { @@ -605,7 +617,7 @@ STATIC mp_obj_t adc_all_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp STATIC mp_obj_t adc_all_read_channel(mp_obj_t self_in, mp_obj_t channel) { pyb_adc_all_obj_t *self = self_in; - uint32_t chan = mp_obj_get_int(channel); + uint32_t chan = adc_get_internal_channel(mp_obj_get_int(channel)); uint32_t data = adc_config_and_read_channel(&self->handle, chan); return mp_obj_new_int(data); } |