Skip to content

Commit 05a6c0f

Browse files
benjaminbjornssoncarlescufi
authored andcommitted
drivers: sensor: stm32_temp: Add multi-instance support
Move driver to use DT_INST_FOREACH_STATUS_OKAY to add multi-instance support. Signed-off-by: Benjamin Björnsson <[email protected]>
1 parent 7b6ca29 commit 05a6c0f

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

drivers/sensor/stm32_temp/stm32_temp.c

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include <zephyr/device.h>
9+
#include <zephyr/devicetree.h>
910
#include <zephyr/drivers/sensor.h>
1011
#include <zephyr/drivers/adc.h>
1112
#include <zephyr/logging/log.h>
@@ -134,30 +135,33 @@ static int stm32_temp_init(const struct device *dev)
134135
return 0;
135136
}
136137

137-
static const struct stm32_temp_config stm32_temp_dev_config = {
138-
#if HAS_CALIBRATION
139-
.cal1_addr = (uint16_t *)DT_INST_PROP(0, ts_cal1_addr),
140-
.cal2_addr = (uint16_t *)DT_INST_PROP(0, ts_cal2_addr),
141-
.cal1_temp = DT_INST_PROP(0, ts_cal1_temp),
142-
.cal2_temp = DT_INST_PROP(0, ts_cal2_temp),
143-
.cal_vrefanalog = DT_INST_PROP(0, ts_cal_vrefanalog),
144-
#else
145-
.avgslope = DT_INST_PROP(0, avgslope),
146-
.v25_mv = DT_INST_PROP(0, v25),
147-
.is_ntc = DT_INST_PROP(0, ntc)
148-
#endif
149-
};
150-
151-
static struct stm32_temp_data stm32_temp_dev_data = {
152-
.adc = DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(0)),
153-
.adc_cfg = {
154-
.gain = ADC_GAIN_1,
155-
.reference = ADC_REF_INTERNAL,
156-
.acquisition_time = ADC_ACQ_TIME_MAX,
157-
.channel_id = DT_INST_IO_CHANNELS_INPUT(0),
158-
.differential = 0
159-
},
160-
};
161-
162-
DEVICE_DT_INST_DEFINE(0, stm32_temp_init, NULL, &stm32_temp_dev_data, &stm32_temp_dev_config,
163-
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &stm32_temp_driver_api);
138+
#define STM32_TEMP_DEFINE(inst) \
139+
static struct stm32_temp_data stm32_temp_dev_data_##inst = { \
140+
.adc = DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(inst)), \
141+
.adc_cfg = { \
142+
.gain = ADC_GAIN_1, \
143+
.reference = ADC_REF_INTERNAL, \
144+
.acquisition_time = ADC_ACQ_TIME_MAX, \
145+
.channel_id = DT_INST_IO_CHANNELS_INPUT(inst), \
146+
.differential = 0 \
147+
}, \
148+
}; \
149+
\
150+
static const struct stm32_temp_config stm32_temp_dev_config_##inst = { \
151+
COND_CODE_1(HAS_CALIBRATION, \
152+
(.cal1_addr = (uint16_t *)DT_INST_PROP(inst, ts_cal1_addr), \
153+
.cal2_addr = (uint16_t *)DT_INST_PROP(inst, ts_cal2_addr), \
154+
.cal1_temp = DT_INST_PROP(inst, ts_cal1_temp), \
155+
.cal2_temp = DT_INST_PROP(inst, ts_cal2_temp), \
156+
.cal_vrefanalog = DT_INST_PROP(inst, ts_cal_vrefanalog),), \
157+
(.avgslope = DT_INST_PROP(inst, avgslope), \
158+
.v25_mv = DT_INST_PROP(inst, v25), \
159+
.is_ntc = DT_INST_PROP(inst, ntc))) \
160+
}; \
161+
\
162+
DEVICE_DT_INST_DEFINE(inst, stm32_temp_init, NULL, \
163+
&stm32_temp_dev_data_##inst, &stm32_temp_dev_config_##inst, \
164+
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
165+
&stm32_temp_driver_api); \
166+
167+
DT_INST_FOREACH_STATUS_OKAY(STM32_TEMP_DEFINE)

0 commit comments

Comments
 (0)