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 <noreply@anthropic.com>
This commit is contained in:
Martin Jäger
2026-11-01 14:47:03 +01:00
co-authored by Claude Opus 5.5
parent 465a7f9ef7
commit 6ec3c6f312
14 changed files with 407 additions and 0 deletions
+1
View File
@@ -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)
+1
View File
@@ -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
+4
View File
@@ -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)
+8
View File
@@ -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
+6
View File
@@ -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)
+11
View File
@@ -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.
+127
View File
@@ -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 <libresolar/drivers/daq.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/logging/log.h>
#include <math.h>
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)
@@ -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.
+8
View File
@@ -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)
@@ -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>;
};
};
@@ -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>;
};
};
+2
View File
@@ -0,0 +1,2 @@
CONFIG_ZTEST=y
CONFIG_SENSOR=y
+107
View File
@@ -0,0 +1,107 @@
/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <libresolar/drivers/daq.h>
#include <libresolar/drivers/daq_emul.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/ztest.h>
#include <math.h>
#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);
+10
View File
@@ -0,0 +1,10 @@
tests:
drivers.sensor.daq_ntc:
tags:
- drivers
- sensors
platform_allow:
- native_sim
- native_sim/native/64
integration_platforms:
- native_sim