From 220d4d2ef4633e9a6334ca83f374d0c5c7cf2521 Mon Sep 17 00:00:00 2001 From: Morgan Diepart Date: Sun, 28 Jan 2024 22:44:14 +0100 Subject: [PATCH] Module17: added VBat measurement to ADC1 driver --- platform/mcu/STM32F4xx/drivers/adc_stm32.c | 15 ++++++++++++++- platform/targets/Module17/hwconfig.h | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/platform/mcu/STM32F4xx/drivers/adc_stm32.c b/platform/mcu/STM32F4xx/drivers/adc_stm32.c index 44c55936..d22f02ba 100644 --- a/platform/mcu/STM32F4xx/drivers/adc_stm32.c +++ b/platform/mcu/STM32F4xx/drivers/adc_stm32.c @@ -107,10 +107,23 @@ uint16_t adcStm32_sample(const struct Adc *adc, const uint32_t channel) ADC_TypeDef *pAdc = ((ADC_TypeDef *) adc->priv); + /* Channel 18 is Vbat, enable it if requested */ + if(channel == 18) + ADC123_COMMON->CCR |= ADC_CCR_VBATE; + pAdc->SQR3 = channel; pAdc->CR2 |= ADC_CR2_SWSTART; while((pAdc->SR & ADC_SR_EOC) == 0) ; - return pAdc->DR; + uint16_t value = pAdc->DR; + + /* Disconnect Vbat channel. Vbat has an internal x2 voltage divider */ + if(channel == 18) + { + value *= 2; + ADC123_COMMON->CCR &= ~ADC_CCR_VBATE; + } + + return value; } diff --git a/platform/targets/Module17/hwconfig.h b/platform/targets/Module17/hwconfig.h index 5010bd17..8bfac15c 100644 --- a/platform/targets/Module17/hwconfig.h +++ b/platform/targets/Module17/hwconfig.h @@ -32,6 +32,7 @@ enum AdcChannel { ADC_HWVER_CH = 3, ADC_HMI_HWVER_CH = 13, + ADC_VBAT_CH = 18 }; extern const struct i2cDevice i2c1;