app: read NTC temperatures via sensor API

Replace the temperature calculation in daq.cpp by the DAQ NTC sensor
driver. Temperatures below -50 °C are still considered as a missing
sensor. A missing MOSFET temperature sensor keeps the previous value
instead of setting an implausible temperature.

The series resistor of the NTCs was specified as multiplier of the DAQ
channels before. It is moved to the pullup-ohm property of the new
ntc-fets and ntc-bat nodes and the gain of the DAQ channels is set to 1.

Note for out-of-tree boards: The temp-fets and temp-bat DAQ channels need
the labels daq_temp_fets and daq_temp_bat and a corresponding
libresolar,daq-ntc-thermistor node with the labels ntc_fets and ntc_bat.

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 6ec3c6f312
commit b90be36dbc
14 changed files with 125 additions and 46 deletions
+3
View File
@@ -18,6 +18,9 @@ CONFIG_HWINFO=y
CONFIG_CRC=y
# NTC temperature sensors
CONFIG_SENSOR=y
CONFIG_CONSOLE_SUBSYS=y
CONFIG_CONSOLE_GETCHAR=y
+4 -2
View File
@@ -19,11 +19,13 @@
#define LOAD_SWITCH_NODE DT_NODELABEL(load_switch)
#define USB_SWITCH_NODE DT_NODELABEL(usb_switch)
#define PWM_SWITCH_NODE DT_NODELABEL(pwm_switch)
#define NTC_FETS_NODE DT_NODELABEL(ntc_fets)
#define NTC_BAT_NODE DT_NODELABEL(ntc_bat)
#define BOARD_HAS_DCDC DT_NODE_HAS_STATUS_OKAY(HALF_BRIDGE_NODE)
#define BOARD_HAS_PWM_PORT DT_NODE_HAS_STATUS_OKAY(PWM_SWITCH_NODE)
#define BOARD_HAS_LOAD_OUTPUT DT_NODE_HAS_STATUS_OKAY(LOAD_SWITCH_NODE)
#define BOARD_HAS_USB_OUTPUT DT_NODE_HAS_STATUS_OKAY(USB_SWITCH_NODE)
#define BOARD_HAS_TEMP_FETS DT_NODE_EXISTS(DT_CHILD(DT_PATH(adc_inputs), temp_fets))
#define BOARD_HAS_TEMP_BAT DT_NODE_EXISTS(DT_CHILD(DT_PATH(adc_inputs), temp_bat))
#define BOARD_HAS_TEMP_FETS DT_NODE_HAS_STATUS_OKAY(NTC_FETS_NODE)
#define BOARD_HAS_TEMP_BAT DT_NODE_HAS_STATUS_OKAY(NTC_BAT_NODE)
#endif /* BOARD_H_ */
+30 -23
View File
@@ -6,17 +6,16 @@
#include "daq.h"
#include <zephyr/drivers/sensor.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(daq_app, CONFIG_DAQ_LOG_LEVEL);
#include <math.h> // log for thermistor calculation
#include "setup.h"
// typical value for Semitec 103AT-5 thermistor: 3435
#define NTC_BETA_VALUE 3435
// temperatures below this value are considered as invalid (e.g. sensor not connected)
#define NTC_TEMP_MIN (-50.0F)
// filter parameter c for additional battery voltage and current low-pass filter
// c = dt / (tau + dt) = 0.1s / (10s + 0.1s)
@@ -25,29 +24,34 @@ LOG_MODULE_REGISTER(daq_app, CONFIG_DAQ_LOG_LEVEL);
#define LV_TERMINAL_CURRENT_FILTER_CONST 0.0099F
#define PWM_CURRENT_FILTER_CONST 0.0625F // 1.5 seconds
#define ADC_GAIN(name) \
((float)DT_PROP(DT_CHILD(DAQ_NODE, name), multiplier) \
/ DT_PROP(DT_CHILD(DAQ_NODE, name), divider))
static const struct device *const daq = DAQ_DEV;
static inline float ntc_temp(int channel, float ntc_series_resistor)
#if BOARD_HAS_TEMP_BAT || BOARD_HAS_TEMP_FETS
/**
* Read temperature from an NTC sensor
*
* @returns 0 on success or negative errno if the temperature is not available or implausible
*/
static int ntc_temp(const struct device *sensor, float *temp)
{
/** \todo Improved (faster) temperature calculation:
https://www.embeddedrelated.com/showarticle/91.php
*/
struct sensor_value val;
int err;
int vref = daq_get_vref_mv(daq);
err = sensor_sample_fetch(sensor);
if (err != 0) {
return err;
}
// voltage read by ADC (mV)
float v_temp = daq_get_pin_voltage(daq, channel) * 1000;
err = sensor_channel_get(sensor, SENSOR_CHAN_AMBIENT_TEMP, &val);
if (err != 0) {
return err;
}
// resistance of NTC (Ohm)
float rts = ntc_series_resistor * v_temp / (vref - v_temp);
*temp = sensor_value_to_float(&val);
return 1.0f / (1.0f / (273.15f + 25.0f) + 1.0f / (float)NTC_BETA_VALUE * logf(rts / 10000.0f))
- 273.15f; // °C
return *temp > NTC_TEMP_MIN ? 0 : -ENODATA;
}
#endif
void calibrate_current_sensors()
{
@@ -173,10 +177,9 @@ void daq_update()
#endif
#if BOARD_HAS_TEMP_BAT
// battery temperature calculation
float bat_temp = ntc_temp(DAQ_CH(temp_bat), ADC_GAIN(temp_bat));
float bat_temp;
if (bat_temp > -50) {
if (ntc_temp(DEVICE_DT_GET(NTC_BAT_NODE), &bat_temp) == 0) {
// external sensor connected: take measured value
charger.bat_temperature = bat_temp;
charger.ext_temp_sensor = true;
@@ -189,7 +192,11 @@ void daq_update()
#endif
#if BOARD_HAS_TEMP_FETS
dcdc.temp_mosfets = ntc_temp(DAQ_CH(temp_fets), ADC_GAIN(temp_fets));
float fets_temp;
if (ntc_temp(DEVICE_DT_GET(NTC_FETS_NODE), &fets_temp) == 0) {
dcdc.temp_mosfets = fets_temp;
}
#endif
float internal_temp;
@@ -77,6 +77,12 @@
};
};
ntc_bat: ntc-bat {
compatible = "libresolar,daq-ntc-thermistor";
daq-channel = <&daq_temp_bat>;
pullup-ohm = <10000>;
};
adc-inputs {
compatible = "libresolar,stm32-daq";
@@ -107,9 +113,9 @@
divider = <150>; // 3*50
};
temp-bat {
daq_temp_bat: temp-bat {
io-channels = <&adc1 7>;
multiplier = <10000>;
multiplier = <1>;
divider = <1>;
};
@@ -67,6 +67,9 @@
};
};
/* no battery temperature sensor input */
/delete-node/ &ntc_bat;
&load_switch {
gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>;
};
@@ -65,6 +65,12 @@
};
};
ntc_fets: ntc-fets {
compatible = "libresolar,daq-ntc-thermistor";
daq-channel = <&daq_temp_fets>;
pullup-ohm = <10000>;
};
adc-inputs {
compatible = "libresolar,stm32-daq";
@@ -80,9 +86,9 @@
divider = <2200>;
};
temp-fets {
daq_temp_fets: temp-fets {
io-channels = <&adc1 11>;
multiplier = <10000>; // 10k NTC series resistor
multiplier = <1>;
divider = <1>;
};
@@ -58,6 +58,12 @@
};
};
ntc_fets: ntc-fets {
compatible = "libresolar,daq-ntc-thermistor";
daq-channel = <&daq_temp_fets>;
pullup-ohm = <10000>;
};
adc-inputs {
compatible = "libresolar,stm32-daq";
@@ -86,9 +92,9 @@
offset = <37414>; // 65536 / (1 + 8.2/120 + 8.2/12)
};
temp-fets {
daq_temp_fets: temp-fets {
io-channels = <&adc1 6>;
multiplier = <10000>; // 10k NTC series resistor
multiplier = <1>;
divider = <1>;
};
@@ -74,18 +74,30 @@
};
};
ntc_bat: ntc-bat {
compatible = "libresolar,daq-ntc-thermistor";
daq-channel = <&daq_temp_bat>;
pullup-ohm = <10000>;
};
ntc_fets: ntc-fets {
compatible = "libresolar,daq-ntc-thermistor";
daq-channel = <&daq_temp_fets>;
pullup-ohm = <10000>;
};
adc-inputs {
compatible = "libresolar,stm32-daq";
temp-bat {
daq_temp_bat: temp-bat {
io-channels = <&adc1 0>;
multiplier = <10000>; // 10k NTC series resistor
multiplier = <1>;
divider = <1>;
};
temp-fets {
daq_temp_fets: temp-fets {
io-channels = <&adc1 1>;
multiplier = <10000>; // 10k NTC series resistor
multiplier = <1>;
divider = <1>;
};
@@ -55,6 +55,18 @@
};
};
ntc_fets: ntc-fets {
compatible = "libresolar,daq-ntc-thermistor";
daq-channel = <&daq_temp_fets>;
pullup-ohm = <10000>;
};
ntc_bat: ntc-bat {
compatible = "libresolar,daq-ntc-thermistor";
daq-channel = <&daq_temp_bat>;
pullup-ohm = <10000>;
};
adc-inputs {
compatible = "libresolar,stm32-daq";
@@ -76,15 +88,15 @@
divider = <1>;
};
temp-fets {
daq_temp_fets: temp-fets {
io-channels = <&adc1 2>;
multiplier = <10000>; // 10k NTC series resistor
multiplier = <1>;
divider = <1>;
};
temp-bat {
daq_temp_bat: temp-bat {
io-channels = <&adc1 14>;
multiplier = <10000>; // 10k NTC series resistor
multiplier = <1>;
divider = <1>;
};
@@ -83,6 +83,12 @@
};
};
ntc_bat: ntc-bat {
compatible = "libresolar,daq-ntc-thermistor";
daq-channel = <&daq_temp_bat>;
pullup-ohm = <8200>;
};
adc-inputs {
compatible = "libresolar,stm32-daq";
@@ -114,9 +120,9 @@
divider = <136>; // 2*68
};
temp-bat {
daq_temp_bat: temp-bat {
io-channels = <&adc1 7>;
multiplier = <8200>; // 8.2k NTC series resistor
multiplier = <1>;
divider = <1>;
};
@@ -62,6 +62,12 @@
};
};
ntc_fets: ntc-fets {
compatible = "libresolar,daq-ntc-thermistor";
daq-channel = <&daq_temp_fets>;
pullup-ohm = <10000>;
};
adc-inputs {
compatible = "libresolar,stm32-daq";
@@ -91,9 +97,9 @@
divider = <2200>;
};
temp-fets {
daq_temp_fets: temp-fets {
io-channels = <&adc1 6>;
multiplier = <10000>; // 10k NTC series resistor
multiplier = <1>;
divider = <1>;
};
+3 -1
View File
@@ -178,7 +178,9 @@ Move all remaining hardware access from `app/src` into Zephyr drivers.
- [x] STM32 timer backend (`libresolar,stm32-pwm-power-switch`) for PWM charge controllers
(replaces `pwm_switch_driver.c`), needs hardware test
- [x] Emulated backend (`libresolar,power-switch-emul`) for tests on native_sim
- [ ] NTC thermistors via Zephyr sensor API (replaces `ntc_temp()` in `daq.cpp`)
- [x] NTC thermistors via Zephyr sensor API (replaces `ntc_temp()` in `daq.cpp`)
- [x] `libresolar,daq-ntc-thermistor` sensor driver using the filtered DAQ readings, as
Zephyr's NTC driver requires exclusive ADC access
- [ ] Bootloader entry and SoC-specific setup out of the application (`hardware.cpp`,
`setup.cpp`, `mcu.h`)
+8 -2
View File
@@ -62,6 +62,12 @@
#io-channel-cells = <1>;
};
ntc_fets: ntc-fets {
compatible = "libresolar,daq-ntc-thermistor";
daq-channel = <&daq_temp_fets>;
pullup-ohm = <10000>;
};
adc-inputs {
compatible = "libresolar,daq-emul";
@@ -84,9 +90,9 @@
offset = <37414>; // 65536 / (1 + 8.2/120 + 8.2/12)
};
temp-fets {
daq_temp_fets: temp-fets {
io-channels = <&adc1 11>;
multiplier = <10000>; // 10k NTC series resistor
multiplier = <1>;
divider = <1>;
};
+2
View File
@@ -16,6 +16,8 @@ CONFIG_REBOOT=y
CONFIG_HWINFO=y
CONFIG_SENSOR=y
CONFIG_CONSOLE_SUBSYS=y
CONFIG_LOG=y