Skip to content

Commit

Permalink
platform/technic_hub: init all adc channel params
Browse files Browse the repository at this point in the history
The STM32L4Cube HAL was updated via upstream MicroPython and contains
significant changes to the ADC code. Apparently, this code is less
forgiving of zero-values for certain parameters. This fixes the issue
by ensuring that we initialize all parameters to a valid value.

Fixes: pybricks/support#665
  • Loading branch information
dlech committed Jun 11, 2022
1 parent 4fc6f9d commit 2cf9bca
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

### Fixed
- Fixed motor not stopping at the end of `run_until_stalled` ([support#662]).
- Fixed incorrect battery current reading on Technic hub ([support#665]).

[support#662]: https://github.com/pybricks/support/issues/662
[support#665]: https://github.com/pybricks/support/issues/665

## [3.2.0b1]

Expand Down
25 changes: 16 additions & 9 deletions lib/pbio/platform/technic_hub/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,22 @@ const uint32_t MSIRangeTable[12] = {
};

void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) {
GPIO_InitTypeDef gpio_init = { };
ADC_ChannelConfTypeDef adc_ch_config = { };
GPIO_InitTypeDef gpio_init = {
.Pin = 0, // variable
.Mode = GPIO_MODE_ANALOG_ADC_CONTROL,
.Pull = GPIO_NOPULL,
.Speed = GPIO_SPEED_FREQ_LOW,
.Alternate = 0, // not used
};

ADC_ChannelConfTypeDef adc_ch_config = {
.Channel = 0, // variable
.Rank = 0, // variable
.SamplingTime = ADC_SAMPLETIME_640CYCLES_5,
.SingleDiff = ADC_SINGLE_ENDED,
.OffsetNumber = ADC_OFFSET_NONE,
.Offset = 0, // not used
};

// clocks are enabled in SystemInit
assert_param(__HAL_RCC_TIM6_IS_CLK_ENABLED());
Expand All @@ -592,32 +606,25 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) {
// PC1, battery voltage

gpio_init.Pin = GPIO_PIN_1;
gpio_init.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
gpio_init.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &gpio_init);

adc_ch_config.Channel = ADC_CHANNEL_2;
adc_ch_config.Rank = ADC_REGULAR_RANK_1;
adc_ch_config.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
HAL_ADC_ConfigChannel(hadc, &adc_ch_config);

// PC2, battery current

gpio_init.Pin = GPIO_PIN_2;
gpio_init.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
gpio_init.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &gpio_init);

adc_ch_config.Channel = ADC_CHANNEL_3;
adc_ch_config.Rank = ADC_REGULAR_RANK_2;
adc_ch_config.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
HAL_ADC_ConfigChannel(hadc, &adc_ch_config);

// internal, temperature

adc_ch_config.Channel = ADC_CHANNEL_TEMPSENSOR;
adc_ch_config.Rank = ADC_REGULAR_RANK_3;
adc_ch_config.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
HAL_ADC_ConfigChannel(hadc, &adc_ch_config);
}

Expand Down
12 changes: 12 additions & 0 deletions pybricks/common/pb_type_battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ STATIC mp_obj_t battery_current(void) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(battery_current_obj, battery_current);

#include "stm32l4xx_hal.h"

STATIC mp_obj_t battery_reg(void) {
mp_obj_t adc1[2];
adc1[0] = mp_obj_new_bytes((const byte*)ADC1, sizeof(ADC_TypeDef));
adc1[1] = mp_obj_new_bytes((const byte*)ADC1_COMMON, sizeof(ADC_Common_TypeDef));

return mp_obj_new_tuple(MP_ARRAY_SIZE(adc1), adc1);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(battery_reg_obj, battery_reg);

#if !PYBRICKS_HUB_MOVEHUB

STATIC mp_obj_t battery_type(void) {
Expand Down Expand Up @@ -63,6 +74,7 @@ STATIC const mp_rom_map_elem_t battery_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_battery) },
{ MP_ROM_QSTR(MP_QSTR_voltage), MP_ROM_PTR(&battery_voltage_obj) },
{ MP_ROM_QSTR(MP_QSTR_current), MP_ROM_PTR(&battery_current_obj) },
{ MP_ROM_QSTR(MP_QSTR_reg), MP_ROM_PTR(&battery_reg_obj) },
#if !PYBRICKS_HUB_MOVEHUB
{ MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&battery_type_obj) },
{ MP_ROM_QSTR(MP_QSTR_temperature), MP_ROM_PTR(&battery_temperature_obj) },
Expand Down

0 comments on commit 2cf9bca

Please sign in to comment.