From 6ec3c6f312ab526d1da18c972052dff79d1770e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Sun, 1 Nov 2026 14:38:59 +0100 Subject: [PATCH] drivers: sensor: add NTC thermistor driver using DAQ readings Zephyr's NTC thermistor driver reads the ADC on demand, which is not possible while the ADC is continuously sampled by the DAQ driver. The new sensor driver calculates the temperature from the filtered reading of a DAQ channel using the beta equation. The DAQ channel is referenced via phandle, the pull-up resistor, the nominal resistance and the beta value are configured in the devicetree. Co-Authored-By: Claude Opus 5.5 --- drivers/CMakeLists.txt | 1 + drivers/Kconfig | 1 + drivers/sensor/CMakeLists.txt | 4 + drivers/sensor/Kconfig | 8 ++ drivers/sensor/daq_ntc/CMakeLists.txt | 6 + drivers/sensor/daq_ntc/Kconfig | 11 ++ drivers/sensor/daq_ntc/daq_ntc.c | 127 ++++++++++++++++++ .../sensor/libresolar,daq-ntc-thermistor.yaml | 54 ++++++++ tests/drivers/daq_ntc/CMakeLists.txt | 8 ++ .../drivers/daq_ntc/boards/native_sim.overlay | 34 +++++ .../boards/native_sim_native_64.overlay | 34 +++++ tests/drivers/daq_ntc/prj.conf | 2 + tests/drivers/daq_ntc/src/main.c | 107 +++++++++++++++ tests/drivers/daq_ntc/testcase.yaml | 10 ++ 14 files changed, 407 insertions(+) create mode 100644 drivers/sensor/CMakeLists.txt create mode 100644 drivers/sensor/Kconfig create mode 100644 drivers/sensor/daq_ntc/CMakeLists.txt create mode 100644 drivers/sensor/daq_ntc/Kconfig create mode 100644 drivers/sensor/daq_ntc/daq_ntc.c create mode 100644 dts/bindings/sensor/libresolar,daq-ntc-thermistor.yaml create mode 100644 tests/drivers/daq_ntc/CMakeLists.txt create mode 100644 tests/drivers/daq_ntc/boards/native_sim.overlay create mode 100644 tests/drivers/daq_ntc/boards/native_sim_native_64.overlay create mode 100644 tests/drivers/daq_ntc/prj.conf create mode 100644 tests/drivers/daq_ntc/src/main.c create mode 100644 tests/drivers/daq_ntc/testcase.yaml diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index 67784b5..e01f2b5 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -5,3 +5,4 @@ add_subdirectory_ifdef(CONFIG_DAQ daq) add_subdirectory_ifdef(CONFIG_HALF_BRIDGE half_bridge) add_subdirectory_ifdef(CONFIG_POWER_SWITCH power_switch) add_subdirectory_ifdef(CONFIG_LIBRESOLAR_STM32_TIM stm32_tim) +add_subdirectory(sensor) diff --git a/drivers/Kconfig b/drivers/Kconfig index f81bb61..158a0b1 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -6,6 +6,7 @@ menu "Libre Solar drivers" rsource "daq/Kconfig" rsource "half_bridge/Kconfig" rsource "power_switch/Kconfig" +rsource "sensor/Kconfig" rsource "stm32_tim/Kconfig" endmenu diff --git a/drivers/sensor/CMakeLists.txt b/drivers/sensor/CMakeLists.txt new file mode 100644 index 0000000..04283f6 --- /dev/null +++ b/drivers/sensor/CMakeLists.txt @@ -0,0 +1,4 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +add_subdirectory_ifdef(CONFIG_DAQ_NTC_THERMISTOR daq_ntc) diff --git a/drivers/sensor/Kconfig b/drivers/sensor/Kconfig new file mode 100644 index 0000000..67a8c2f --- /dev/null +++ b/drivers/sensor/Kconfig @@ -0,0 +1,8 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +if SENSOR + +rsource "daq_ntc/Kconfig" + +endif # SENSOR diff --git a/drivers/sensor/daq_ntc/CMakeLists.txt b/drivers/sensor/daq_ntc/CMakeLists.txt new file mode 100644 index 0000000..af8397e --- /dev/null +++ b/drivers/sensor/daq_ntc/CMakeLists.txt @@ -0,0 +1,6 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +zephyr_library() + +zephyr_library_sources(daq_ntc.c) diff --git a/drivers/sensor/daq_ntc/Kconfig b/drivers/sensor/daq_ntc/Kconfig new file mode 100644 index 0000000..4ae5b5d --- /dev/null +++ b/drivers/sensor/daq_ntc/Kconfig @@ -0,0 +1,11 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +config DAQ_NTC_THERMISTOR + bool "NTC thermistor measured via DAQ" + default y + depends on DT_HAS_LIBRESOLAR_DAQ_NTC_THERMISTOR_ENABLED + depends on DAQ + help + Enable driver for NTC thermistors measured via a channel of the data acquisition (DAQ) + driver. diff --git a/drivers/sensor/daq_ntc/daq_ntc.c b/drivers/sensor/daq_ntc/daq_ntc.c new file mode 100644 index 0000000..3b9c9f8 --- /dev/null +++ b/drivers/sensor/daq_ntc/daq_ntc.c @@ -0,0 +1,127 @@ +/* + * Copyright (c) The Libre Solar Project Contributors + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief NTC thermistor measured via a DAQ channel + * + * The NTC is connected between the ADC input and ground with a pull-up resistor to the ADC + * reference voltage. The temperature is calculated from the filtered DAQ reading using the beta + * equation. + * + * Zephyr's NTC thermistor driver can't be used, as the ADC is continuously sampled by the DAQ. + */ + +#define DT_DRV_COMPAT libresolar_daq_ntc_thermistor + +#include + +#include +#include +#include + +#include + +LOG_MODULE_REGISTER(daq_ntc, CONFIG_SENSOR_LOG_LEVEL); + +#define ZERO_CELSIUS_IN_KELVIN 273.15F +#define NOMINAL_TEMP_KELVIN (ZERO_CELSIUS_IN_KELVIN + 25.0F) + +struct daq_ntc_config +{ + const struct device *daq; + uint32_t pullup_ohm; + uint32_t nominal_ohm; + uint16_t beta; + uint8_t channel; +}; + +struct daq_ntc_data +{ + float temp; +}; + +static int daq_ntc_sample_fetch(const struct device *dev, enum sensor_channel chan) +{ + const struct daq_ntc_config *config = dev->config; + struct daq_ntc_data *data = dev->data; + + if (chan != SENSOR_CHAN_ALL && chan != SENSOR_CHAN_AMBIENT_TEMP) { + return -ENOTSUP; + } + + int vref_mv = daq_get_vref_mv(config->daq); + if (vref_mv < 0) { + return vref_mv; + } + + float vref = vref_mv / 1000.0F; + float v_ntc = daq_get_pin_voltage(config->daq, config->channel); + + if (v_ntc <= 0.0F || v_ntc >= vref) { + /* + * Temperature can't be calculated. Note that a missing NTC usually results in a reading + * slightly below the reference voltage and an implausibly low temperature instead. + */ + return -ENODATA; + } + + float r_ntc = config->pullup_ohm * v_ntc / (vref - v_ntc); + + data->temp = + 1.0F + / (1.0F / NOMINAL_TEMP_KELVIN + logf(r_ntc / config->nominal_ohm) / (float)config->beta) + - ZERO_CELSIUS_IN_KELVIN; + + return 0; +} + +static int daq_ntc_channel_get(const struct device *dev, enum sensor_channel chan, + struct sensor_value *val) +{ + struct daq_ntc_data *data = dev->data; + + if (chan != SENSOR_CHAN_AMBIENT_TEMP) { + return -ENOTSUP; + } + + return sensor_value_from_float(val, data->temp); +} + +static DEVICE_API(sensor, daq_ntc_api) = { + .sample_fetch = daq_ntc_sample_fetch, + .channel_get = daq_ntc_channel_get, +}; + +static int daq_ntc_init(const struct device *dev) +{ + const struct daq_ntc_config *config = dev->config; + + if (!device_is_ready(config->daq)) { + LOG_ERR("DAQ device not ready"); + return -ENODEV; + } + + return 0; +} + +#define DAQ_NTC_CHANNEL_NODE(n) DT_INST_PHANDLE(n, daq_channel) + +#define DAQ_NTC_INIT(n) \ + static const struct daq_ntc_config daq_ntc_config_##n = { \ + .daq = DEVICE_DT_GET(DT_PARENT(DAQ_NTC_CHANNEL_NODE(n))), \ + .pullup_ohm = DT_INST_PROP(n, pullup_ohm), \ + .nominal_ohm = DT_INST_PROP(n, nominal_ohm), \ + .beta = DT_INST_PROP(n, beta), \ + .channel = DT_NODE_CHILD_IDX(DAQ_NTC_CHANNEL_NODE(n)), \ + }; \ +\ + static struct daq_ntc_data daq_ntc_data_##n; \ +\ + SENSOR_DEVICE_DT_INST_DEFINE(n, daq_ntc_init, NULL, &daq_ntc_data_##n, &daq_ntc_config_##n, \ + POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &daq_ntc_api); + +DT_INST_FOREACH_STATUS_OKAY(DAQ_NTC_INIT) diff --git a/dts/bindings/sensor/libresolar,daq-ntc-thermistor.yaml b/dts/bindings/sensor/libresolar,daq-ntc-thermistor.yaml new file mode 100644 index 0000000..e505123 --- /dev/null +++ b/dts/bindings/sensor/libresolar,daq-ntc-thermistor.yaml @@ -0,0 +1,54 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +description: | + NTC thermistor measured via a DAQ channel + + The NTC is connected between the ADC input and ground with a pull-up resistor to the ADC + reference voltage. The temperature is calculated using the beta equation. + + Example: + + adc-inputs { + compatible = "libresolar,stm32-daq"; + + daq_temp_fets: temp-fets { + io-channels = <&adc1 11>; + multiplier = <1>; + divider = <1>; + }; + }; + + ntc_fets: ntc-fets { + compatible = "libresolar,daq-ntc-thermistor"; + daq-channel = <&daq_temp_fets>; + pullup-ohm = <10000>; + }; + +compatible: "libresolar,daq-ntc-thermistor" + +include: [sensor-device.yaml] + +properties: + daq-channel: + type: phandle + required: true + description: Child node of the DAQ device measuring the NTC voltage + + pullup-ohm: + type: int + required: true + description: Resistance of the pull-up resistor in ohms + + nominal-ohm: + type: int + default: 10000 + description: Resistance of the NTC at 25°C in ohms + + beta: + type: int + default: 3435 + description: | + Beta value of the NTC in kelvin + + The default value is valid for the Semitec 103AT-5 thermistor. diff --git a/tests/drivers/daq_ntc/CMakeLists.txt b/tests/drivers/daq_ntc/CMakeLists.txt new file mode 100644 index 0000000..650963e --- /dev/null +++ b/tests/drivers/daq_ntc/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright (c) The Libre Solar Project Contributors +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(daq_ntc) + +target_sources(app PRIVATE src/main.c) diff --git a/tests/drivers/daq_ntc/boards/native_sim.overlay b/tests/drivers/daq_ntc/boards/native_sim.overlay new file mode 100644 index 0000000..4f72d04 --- /dev/null +++ b/tests/drivers/daq_ntc/boards/native_sim.overlay @@ -0,0 +1,34 @@ +/* + * Copyright (c) The Libre Solar Project Contributors + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + test_adc: test-adc { + compatible = "zephyr,dummy-adc"; + #io-channel-cells = <1>; + }; + + daq: daq { + compatible = "libresolar,daq-emul"; + + v-in { + io-channels = <&test_adc 3>; + multiplier = <105600>; + divider = <5600>; + }; + + daq_temp: temp { + io-channels = <&test_adc 11>; + multiplier = <1>; + divider = <1>; + }; + }; + + ntc: ntc { + compatible = "libresolar,daq-ntc-thermistor"; + daq-channel = <&daq_temp>; + pullup-ohm = <10000>; + }; +}; diff --git a/tests/drivers/daq_ntc/boards/native_sim_native_64.overlay b/tests/drivers/daq_ntc/boards/native_sim_native_64.overlay new file mode 100644 index 0000000..4f72d04 --- /dev/null +++ b/tests/drivers/daq_ntc/boards/native_sim_native_64.overlay @@ -0,0 +1,34 @@ +/* + * Copyright (c) The Libre Solar Project Contributors + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + test_adc: test-adc { + compatible = "zephyr,dummy-adc"; + #io-channel-cells = <1>; + }; + + daq: daq { + compatible = "libresolar,daq-emul"; + + v-in { + io-channels = <&test_adc 3>; + multiplier = <105600>; + divider = <5600>; + }; + + daq_temp: temp { + io-channels = <&test_adc 11>; + multiplier = <1>; + divider = <1>; + }; + }; + + ntc: ntc { + compatible = "libresolar,daq-ntc-thermistor"; + daq-channel = <&daq_temp>; + pullup-ohm = <10000>; + }; +}; diff --git a/tests/drivers/daq_ntc/prj.conf b/tests/drivers/daq_ntc/prj.conf new file mode 100644 index 0000000..97305dd --- /dev/null +++ b/tests/drivers/daq_ntc/prj.conf @@ -0,0 +1,2 @@ +CONFIG_ZTEST=y +CONFIG_SENSOR=y diff --git a/tests/drivers/daq_ntc/src/main.c b/tests/drivers/daq_ntc/src/main.c new file mode 100644 index 0000000..b41bc67 --- /dev/null +++ b/tests/drivers/daq_ntc/src/main.c @@ -0,0 +1,107 @@ +/* + * Copyright (c) The Libre Solar Project Contributors + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include +#include +#include + +#include + +#define DAQ_NODE DT_NODELABEL(daq) + +static const struct device *const daq = DEVICE_DT_GET(DAQ_NODE); +static const struct device *const ntc = DEVICE_DT_GET(DT_NODELABEL(ntc)); + +static const int channel = DT_NODE_CHILD_IDX(DT_NODELABEL(daq_temp)); + +/* NTC resistance (beta = 3435, 10k at 25°C) */ +#define R_NTC_25DEGC 10000.0F +#define R_NTC_50DEGC 4101.0F +#define R_PULLUP 10000.0F + +static void set_ntc_resistance(float r_ntc) +{ + uint16_t raw = (uint16_t)(65536.0F * r_ntc / (r_ntc + R_PULLUP)); + + zassert_ok(daq_emul_set_raw(daq, channel, raw)); + daq_emul_preset_filtered(daq); +} + +static int read_temp(float *temp) +{ + struct sensor_value val; + int err = sensor_sample_fetch(ntc); + + if (err != 0) { + return err; + } + + zassert_ok(sensor_channel_get(ntc, SENSOR_CHAN_AMBIENT_TEMP, &val)); + *temp = sensor_value_to_float(&val); + + return 0; +} + +static void *daq_ntc_setup(void) +{ + zassert_true(device_is_ready(ntc)); + + return NULL; +} + +ZTEST(daq_ntc, test_nominal_temperature) +{ + float temp; + + set_ntc_resistance(R_NTC_25DEGC); + zassert_ok(read_temp(&temp)); + zassert_within(temp, 25.0F, 0.1F); +} + +ZTEST(daq_ntc, test_high_temperature) +{ + float temp; + + set_ntc_resistance(R_NTC_50DEGC); + zassert_ok(read_temp(&temp)); + zassert_within(temp, 50.0F, 0.2F); +} + +ZTEST(daq_ntc, test_not_connected) +{ + float temp; + + /* + * NTC missing: input pulled up to reference voltage, but the maximum raw reading is still + * one LSB below, resulting in an implausibly low temperature + */ + zassert_ok(daq_emul_set_raw(daq, channel, UINT16_MAX)); + daq_emul_preset_filtered(daq); + zassert_ok(read_temp(&temp)); + zassert_true(temp < -50.0F); +} + +ZTEST(daq_ntc, test_shorted) +{ + float temp; + + zassert_ok(daq_emul_set_raw(daq, channel, 0)); + daq_emul_preset_filtered(daq); + zassert_equal(read_temp(&temp), -ENODATA); +} + +ZTEST(daq_ntc, test_unsupported_channel) +{ + struct sensor_value val; + + zassert_equal(sensor_sample_fetch_chan(ntc, SENSOR_CHAN_VOLTAGE), -ENOTSUP); + zassert_equal(sensor_channel_get(ntc, SENSOR_CHAN_VOLTAGE, &val), -ENOTSUP); +} + +ZTEST_SUITE(daq_ntc, NULL, daq_ntc_setup, NULL, NULL, NULL); diff --git a/tests/drivers/daq_ntc/testcase.yaml b/tests/drivers/daq_ntc/testcase.yaml new file mode 100644 index 0000000..90321f7 --- /dev/null +++ b/tests/drivers/daq_ntc/testcase.yaml @@ -0,0 +1,10 @@ +tests: + drivers.sensor.daq_ntc: + tags: + - drivers + - sensors + platform_allow: + - native_sim + - native_sim/native/64 + integration_platforms: + - native_sim