From b90be36dbcb1865e0fcfe274c8be4c0a40408b62 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] app: read NTC temperatures via sensor API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- app/prj.conf | 3 ++ app/src/board.h | 6 ++- app/src/daq.cpp | 53 +++++++++++-------- .../mppt_1210_hus/mppt_1210_hus.dts | 10 +++- .../mppt_1210_hus/mppt_1210_hus_0_4_0.overlay | 3 ++ .../libresolar/mppt_2420_hc/mppt_2420_hc.dts | 10 +++- .../mppt_2420_hpx/mppt_2420_hpx.dts | 10 +++- .../libresolar/mppt_2420_lc/mppt_2420_lc.dts | 20 +++++-- .../libresolar/mppt_2420_rc/mppt_2420_rc.dts | 20 +++++-- .../libresolar/pwm_2420_lus/pwm_2420_lus.dts | 10 +++- .../boost_4820/spinvert_boost_4820.dts | 10 +++- docs/architecture-migration-plan.md | 4 +- tests/app/boards/native_sim.overlay | 10 +++- tests/app/prj.conf | 2 + 14 files changed, 125 insertions(+), 46 deletions(-) diff --git a/app/prj.conf b/app/prj.conf index 1bd2229..3c6a0ed 100644 --- a/app/prj.conf +++ b/app/prj.conf @@ -18,6 +18,9 @@ CONFIG_HWINFO=y CONFIG_CRC=y +# NTC temperature sensors +CONFIG_SENSOR=y + CONFIG_CONSOLE_SUBSYS=y CONFIG_CONSOLE_GETCHAR=y diff --git a/app/src/board.h b/app/src/board.h index 74794b0..a801cae 100644 --- a/app/src/board.h +++ b/app/src/board.h @@ -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_ */ diff --git a/app/src/daq.cpp b/app/src/daq.cpp index 4a57a9a..075ce8d 100644 --- a/app/src/daq.cpp +++ b/app/src/daq.cpp @@ -6,17 +6,16 @@ #include "daq.h" +#include #include #include LOG_MODULE_REGISTER(daq_app, CONFIG_DAQ_LOG_LEVEL); -#include // 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; diff --git a/boards/libresolar/mppt_1210_hus/mppt_1210_hus.dts b/boards/libresolar/mppt_1210_hus/mppt_1210_hus.dts index bef6ae9..e44dc11 100644 --- a/boards/libresolar/mppt_1210_hus/mppt_1210_hus.dts +++ b/boards/libresolar/mppt_1210_hus/mppt_1210_hus.dts @@ -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>; }; diff --git a/boards/libresolar/mppt_1210_hus/mppt_1210_hus_0_4_0.overlay b/boards/libresolar/mppt_1210_hus/mppt_1210_hus_0_4_0.overlay index b2a65fc..0743bee 100644 --- a/boards/libresolar/mppt_1210_hus/mppt_1210_hus_0_4_0.overlay +++ b/boards/libresolar/mppt_1210_hus/mppt_1210_hus_0_4_0.overlay @@ -67,6 +67,9 @@ }; }; +/* no battery temperature sensor input */ +/delete-node/ &ntc_bat; + &load_switch { gpios = <&gpioc 13 GPIO_ACTIVE_HIGH>; }; diff --git a/boards/libresolar/mppt_2420_hc/mppt_2420_hc.dts b/boards/libresolar/mppt_2420_hc/mppt_2420_hc.dts index 57c4d37..81f6169 100644 --- a/boards/libresolar/mppt_2420_hc/mppt_2420_hc.dts +++ b/boards/libresolar/mppt_2420_hc/mppt_2420_hc.dts @@ -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>; }; diff --git a/boards/libresolar/mppt_2420_hpx/mppt_2420_hpx.dts b/boards/libresolar/mppt_2420_hpx/mppt_2420_hpx.dts index 57e38f1..b9c4bbb 100644 --- a/boards/libresolar/mppt_2420_hpx/mppt_2420_hpx.dts +++ b/boards/libresolar/mppt_2420_hpx/mppt_2420_hpx.dts @@ -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>; }; diff --git a/boards/libresolar/mppt_2420_lc/mppt_2420_lc.dts b/boards/libresolar/mppt_2420_lc/mppt_2420_lc.dts index f08c123..ea6c302 100644 --- a/boards/libresolar/mppt_2420_lc/mppt_2420_lc.dts +++ b/boards/libresolar/mppt_2420_lc/mppt_2420_lc.dts @@ -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>; }; diff --git a/boards/libresolar/mppt_2420_rc/mppt_2420_rc.dts b/boards/libresolar/mppt_2420_rc/mppt_2420_rc.dts index 3718079..9662de6 100644 --- a/boards/libresolar/mppt_2420_rc/mppt_2420_rc.dts +++ b/boards/libresolar/mppt_2420_rc/mppt_2420_rc.dts @@ -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>; }; diff --git a/boards/libresolar/pwm_2420_lus/pwm_2420_lus.dts b/boards/libresolar/pwm_2420_lus/pwm_2420_lus.dts index d364305..25fd942 100644 --- a/boards/libresolar/pwm_2420_lus/pwm_2420_lus.dts +++ b/boards/libresolar/pwm_2420_lus/pwm_2420_lus.dts @@ -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>; }; diff --git a/boards/spinvert/boost_4820/spinvert_boost_4820.dts b/boards/spinvert/boost_4820/spinvert_boost_4820.dts index a165f1a..55da32b 100644 --- a/boards/spinvert/boost_4820/spinvert_boost_4820.dts +++ b/boards/spinvert/boost_4820/spinvert_boost_4820.dts @@ -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>; }; diff --git a/docs/architecture-migration-plan.md b/docs/architecture-migration-plan.md index ed95be2..6029416 100644 --- a/docs/architecture-migration-plan.md +++ b/docs/architecture-migration-plan.md @@ -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`) diff --git a/tests/app/boards/native_sim.overlay b/tests/app/boards/native_sim.overlay index bde67e0..9553cbb 100644 --- a/tests/app/boards/native_sim.overlay +++ b/tests/app/boards/native_sim.overlay @@ -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>; }; diff --git a/tests/app/prj.conf b/tests/app/prj.conf index 6d04a77..897c528 100644 --- a/tests/app/prj.conf +++ b/tests/app/prj.conf @@ -16,6 +16,8 @@ CONFIG_REBOOT=y CONFIG_HWINFO=y +CONFIG_SENSOR=y + CONFIG_CONSOLE_SUBSYS=y CONFIG_LOG=y