Skip to content

Commit b039442

Browse files
ycsincarlescufi
authored andcommitted
drivers: sensor: st: vbat: check for ADC nodes
The vbat driver requires the adc node to be enabled: ```c .adc = DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(inst)) ``` Update its Kconfig to depend on `DT_HAS_ST_STM32_ADC_ENABLED`, which is the `"st,stm32-adc"` compat that all ST ADC bindings include, this will guarantee that at least one ADC node is enabled, but not necessarily the ADC used by the vbat node. To make sure that it at least compiles, we init the `adc` pointer only if the specified ADC node is enabled, otherwise it will points to `NULL`. Finally, check if the `adc` points to `NULL` in `stm32_vbat_init`. We are not relying on the existing `device_is_ready` check because `DEVICE_DT_GET` will not return `NULL` if the ADC is enabled. `adc == NULL` means that the ADC node is not enabled in the devicetree. Signed-off-by: Yong Cong Sin <[email protected]>
1 parent a15cc70 commit b039442

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

drivers/sensor/st/stm32_vbat/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ config STM32_VBAT
77
bool "STM32 Vbat Sensor"
88
default y
99
depends on DT_HAS_ST_STM32_VBAT_ENABLED
10+
depends on DT_HAS_ST_STM32_ADC_ENABLED
1011
depends on SOC_FAMILY_STM32 && !SOC_SERIES_STM32F1X
1112
select ADC
1213
help

drivers/sensor/st/stm32_vbat/stm32_vbat.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ static int stm32_vbat_init(const struct device *dev)
101101

102102
k_mutex_init(&data->mutex);
103103

104+
if (data->adc == NULL) {
105+
LOG_ERR("ADC is not enabled");
106+
return -ENODEV;
107+
}
108+
104109
if (!device_is_ready(data->adc)) {
105110
LOG_ERR("Device %s is not ready", data->adc->name);
106111
return -ENODEV;
@@ -116,9 +121,13 @@ static int stm32_vbat_init(const struct device *dev)
116121
return 0;
117122
}
118123

124+
#define STM32_VBAT_GET_ADC_OR_NULL(inst) \
125+
COND_CODE_1(DT_NODE_HAS_STATUS(DT_INST_IO_CHANNELS_CTLR(inst), okay), \
126+
(DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(inst))), (NULL))
127+
119128
#define STM32_VBAT_DEFINE(inst) \
120129
static struct stm32_vbat_data stm32_vbat_dev_data_##inst = { \
121-
.adc = DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(inst)), \
130+
.adc = STM32_VBAT_GET_ADC_OR_NULL(inst), \
122131
.adc_base = (ADC_TypeDef *)DT_REG_ADDR(DT_INST_IO_CHANNELS_CTLR(0)), \
123132
.adc_cfg = { \
124133
.gain = ADC_GAIN_1, \
@@ -136,6 +145,6 @@ static int stm32_vbat_init(const struct device *dev)
136145
SENSOR_DEVICE_DT_INST_DEFINE(inst, stm32_vbat_init, NULL, \
137146
&stm32_vbat_dev_data_##inst, &stm32_vbat_dev_config_##inst, \
138147
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
139-
&stm32_vbat_driver_api); \
148+
&stm32_vbat_driver_api);
140149

141150
DT_INST_FOREACH_STATUS_OKAY(STM32_VBAT_DEFINE)

0 commit comments

Comments
 (0)