src.nth.io/

summaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorLuke Hoersten <[email protected]>2026-07-15 19:18:32 -0500
committerLuke Hoersten <[email protected]>2026-07-15 19:18:32 -0500
commitf59cea3be9eed8d63fa44d4fd360c9c68e9318f6 (patch)
tree699a0b554d04600520505c9e5bf835f89663af9f /main
parent659da7f36473a21494693f031a39fb6bb977b90a (diff)
temp: report on-die temperature in /state and the info screen
New chip_temp module wraps the ESP32-P4 TSENS driver (20-100C range for best accuracy in the warm band a PoE + 200MHz-PSRAM device lives in). /state gains temp_c (0.1C resolution, omitted when the sensor is unavailable); the long-press info overlay gains a temp line (lowercase c suffix — the local 8x8 font has no uppercase C). Junction temperature, ~10-20C above ambient under load.
Diffstat (limited to 'main')
-rw-r--r--main/CMakeLists.txt6
-rw-r--r--main/app_main.c2
-rw-r--r--main/chip_temp.c40
-rw-r--r--main/chip_temp.h12
-rw-r--r--main/http_api.c9
-rw-r--r--main/local_screens.c4
6 files changed, 70 insertions, 3 deletions
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt
index dbb4d4b..2c17544 100644
--- a/main/CMakeLists.txt
+++ b/main/CMakeLists.txt
@@ -2,7 +2,7 @@ idf_component_register(
SRC_DIRS "."
INCLUDE_DIRS "."
REQUIRES app_update driver esp_app_format esp_driver_i2c
- esp_driver_jpeg esp_eth esp_event esp_http_client
- esp_http_server esp_hw_support esp_lcd esp_netif esp_pm
- esp_timer hal json mdns nvs_flash
+ esp_driver_jpeg esp_driver_tsens esp_eth esp_event
+ esp_http_client esp_http_server esp_hw_support esp_lcd
+ esp_netif esp_pm esp_timer hal json mdns nvs_flash
)
diff --git a/main/app_main.c b/main/app_main.c
index fa63e26..f1aae13 100644
--- a/main/app_main.c
+++ b/main/app_main.c
@@ -1,3 +1,4 @@
+#include "chip_temp.h"
#include "display.h"
#include "http_api.h"
#include "jpeg_decoder.h"
@@ -42,6 +43,7 @@ void app_main(void)
viewport_state_init();
nvs_config_load(); // apply persisted config over defaults (best-effort)
+ chip_temp_init(); // best-effort; readers get NAN if unavailable
// PROJECT_VER comes from CMakeLists.txt — git short hash + dirty
// marker. Logged in the very first line so any captured boot log
// tells us exactly which build is running.
diff --git a/main/chip_temp.c b/main/chip_temp.c
new file mode 100644
index 0000000..1530fc5
--- /dev/null
+++ b/main/chip_temp.c
@@ -0,0 +1,40 @@
+#include "chip_temp.h"
+
+#include <math.h>
+
+#include "driver/temperature_sensor.h"
+#include "esp_log.h"
+
+static const char *TAG = "temp";
+
+static temperature_sensor_handle_t s_tsens;
+
+esp_err_t chip_temp_init(void)
+{
+ // 20-100°C range: best accuracy band for a device that idles warm
+ // (PoE + PSRAM at 200 MHz) and whose interesting failures are hot.
+ temperature_sensor_config_t cfg = TEMPERATURE_SENSOR_CONFIG_DEFAULT(20, 100);
+ esp_err_t err = temperature_sensor_install(&cfg, &s_tsens);
+ if (err != ESP_OK) {
+ ESP_LOGW(TAG, "install failed: %s", esp_err_to_name(err));
+ return err;
+ }
+ err = temperature_sensor_enable(s_tsens);
+ if (err != ESP_OK) {
+ ESP_LOGW(TAG, "enable failed: %s", esp_err_to_name(err));
+ temperature_sensor_uninstall(s_tsens);
+ s_tsens = NULL;
+ return err;
+ }
+ float t = chip_temp_read();
+ ESP_LOGI(TAG, "on-die sensor up, %.1f°C at boot", (double)t);
+ return ESP_OK;
+}
+
+float chip_temp_read(void)
+{
+ if (!s_tsens) return NAN;
+ float celsius = NAN;
+ if (temperature_sensor_get_celsius(s_tsens, &celsius) != ESP_OK) return NAN;
+ return celsius;
+}
diff --git a/main/chip_temp.h b/main/chip_temp.h
new file mode 100644
index 0000000..ef3e792
--- /dev/null
+++ b/main/chip_temp.h
@@ -0,0 +1,12 @@
+#pragma once
+
+#include "esp_err.h"
+
+// On-die temperature sensor (ESP32-P4 TSENS). Init once at boot; reads are
+// cheap (~µs) and safe from any task. Note this is junction temperature —
+// expect ~10-20°C above ambient under load; useful for trending and
+// thermal-throttle debugging, not room temperature.
+esp_err_t chip_temp_init(void);
+
+// Degrees Celsius, or NAN if the sensor isn't initialized / read failed.
+float chip_temp_read(void);
diff --git a/main/http_api.c b/main/http_api.c
index b9eade0..5e84db6 100644
--- a/main/http_api.c
+++ b/main/http_api.c
@@ -1,6 +1,7 @@
#include "http_api.h"
#include <inttypes.h>
+#include <math.h>
#include <stdio.h>
#include <string.h>
@@ -17,6 +18,7 @@
#include "esp_system.h"
#include "esp_timer.h"
+#include "chip_temp.h"
#include "display.h"
#include "jpeg_decoder.h"
#include "mdns_service.h"
@@ -78,6 +80,13 @@ static esp_err_t state_get_handler(httpd_req_t *req)
// prevented (would-have-torn count under double buffering).
cJSON_AddNumberToObject(root, "tear_guard_engaged",
(double)display_tear_guard_engaged());
+ // On-die junction temperature (°C, ~10-20° above ambient under
+ // load). Omitted entirely if the sensor is unavailable.
+ float temp_c = chip_temp_read();
+ if (!isnan(temp_c)) {
+ cJSON_AddNumberToObject(root, "temp_c",
+ round((double)temp_c * 10.0) / 10.0);
+ }
viewport_state_unlock();
diff --git a/main/local_screens.c b/main/local_screens.c
index 5c72f26..751ac91 100644
--- a/main/local_screens.c
+++ b/main/local_screens.c
@@ -1,11 +1,13 @@
#include "local_screens.h"
+#include <math.h>
#include <string.h>
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "esp_timer.h"
+#include "chip_temp.h"
#include "display.h"
#include "net_eth.h"
#include "viewport_state.h"
@@ -310,6 +312,8 @@ esp_err_t local_screens_show_info(void)
ADD("errs %llu", (unsigned long long)(decode_err + post_err));
ADD("heap %s", heap_str);
ADD("psram %s", psram_str);
+ float temp_c = chip_temp_read(); // on-die junction temp; skip if n/a
+ if (!isnan(temp_c)) ADD("temp %.1fc", (double)temp_c);
#undef ADD