From 0ce4d8b2da3edbedc7ff737e5175d080ab16bf35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Bj=C3=B6rnsson?= Date: Wed, 6 Jul 2022 16:18:25 +0200 Subject: [PATCH] drivers: sensor: stm32_vbat: Add multi-instance support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move driver to use DT_INST_FOREACH_STATUS_OKAY to add multi-instance support. Signed-off-by: Benjamin Björnsson --- drivers/sensor/stm32_vbat/stm32_vbat.c | 38 +++++++++++++++----------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/drivers/sensor/stm32_vbat/stm32_vbat.c b/drivers/sensor/stm32_vbat/stm32_vbat.c index 2a6f3b48ec2..23f4f7fc9c5 100644 --- a/drivers/sensor/stm32_vbat/stm32_vbat.c +++ b/drivers/sensor/stm32_vbat/stm32_vbat.c @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -106,20 +107,25 @@ static int stm32_vbat_init(const struct device *dev) return 0; } -static struct stm32_vbat_config stm32_vbat_dev_config = { - .ratio = DT_INST_PROP(0, ratio), -}; +#define STM32_VBAT_DEFINE(inst) \ + static struct stm32_vbat_data stm32_vbat_dev_data_##inst = { \ + .adc = DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(inst)), \ + .adc_cfg = { \ + .gain = ADC_GAIN_1, \ + .reference = ADC_REF_INTERNAL, \ + .acquisition_time = ADC_ACQ_TIME_MAX, \ + .channel_id = DT_INST_IO_CHANNELS_INPUT(inst), \ + .differential = 0, \ + }, \ + }; \ + \ + static const struct stm32_vbat_config stm32_vbat_dev_config_##inst = { \ + .ratio = DT_INST_PROP(inst, ratio), \ + }; \ + \ + DEVICE_DT_INST_DEFINE(inst, stm32_vbat_init, NULL, \ + &stm32_vbat_dev_data_##inst, &stm32_vbat_dev_config_##inst, \ + POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \ + &stm32_vbat_driver_api); \ -static struct stm32_vbat_data stm32_vbat_dev_data = { - .adc = DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(0)), - .adc_cfg = { - .gain = ADC_GAIN_1, - .reference = ADC_REF_INTERNAL, - .acquisition_time = ADC_ACQ_TIME_MAX, - .channel_id = DT_INST_IO_CHANNELS_INPUT(0), - .differential = 0, - }, -}; - -DEVICE_DT_INST_DEFINE(0, stm32_vbat_init, NULL, &stm32_vbat_dev_data, &stm32_vbat_dev_config, - POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &stm32_vbat_driver_api); +DT_INST_FOREACH_STATUS_OKAY(STM32_VBAT_DEFINE)