drivers: sensor: stm32_vref: handle disabled ADCs more gracefully

This commit modifies the STM32 internal voltage reference sensor
driver to handle erroneous usage more gracefully. More precisely:
  - driver no longer builds if no ADC node is enabled
  - fail builds with an explicit error message when the sensor
    is enabled but the corresponding ADC is not. This can only
    happen on STM32 series with more than one ADC (e.g., H7).

Signed-off-by: Mathieu Choplain <mathieu.choplain@st.com>
This commit is contained in:
Mathieu Choplain 2024-07-22 17:08:07 +02:00 committed by Carles Cufí
commit 1e0e3a874f
2 changed files with 20 additions and 0 deletions

View file

@ -6,6 +6,7 @@
config STM32_VREF
bool "STM32 VREF Sensor"
default y
depends on DT_HAS_ST_STM32_ADC_ENABLED
depends on DT_HAS_ST_STM32_VREF_ENABLED
depends on SOC_FAMILY_STM32 && !SOC_SERIES_STM32F1X
select ADC

View file

@ -150,6 +150,23 @@ static int stm32_vref_init(const struct device *dev)
return 0;
}
/**
* Verify that the ADC instance which this driver uses to measure internal
* voltage reference is enabled. On STM32 MCUs with more than one ADC, it is
* possible to compile this driver even if the ADC used for measurement is
* disabled. In such cases, fail build with an explicit error message.
*/
#if !DT_NODE_HAS_STATUS(DT_INST_IO_CHANNELS_CTLR(0), okay)
/* Use BUILD_ASSERT to get preprocessing on the message */
BUILD_ASSERT(0, "ADC '" DT_NODE_FULL_NAME(DT_INST_IO_CHANNELS_CTLR(0)) "' needed by "
"Vref sensor '" DT_NODE_FULL_NAME(DT_DRV_INST(0)) "' is not enabled");
/* To reduce noise in the compiler error log, do not attempt
* to instantiate device if the sensor's ADC is not enabled.
*/
#else
static struct stm32_vref_data stm32_vref_dev_data = {
.adc = DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(0)),
.adc_base = (ADC_TypeDef *)DT_REG_ADDR(DT_INST_IO_CHANNELS_CTLR(0)),
@ -167,3 +184,5 @@ static const struct stm32_vref_config stm32_vref_dev_config = {
SENSOR_DEVICE_DT_INST_DEFINE(0, stm32_vref_init, NULL, &stm32_vref_dev_data, &stm32_vref_dev_config,
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &stm32_vref_driver_api);
#endif /* !DT_NODE_HAS_STATUS(DT_INST_IO_CHANNELS_CTLR(0), okay) */