@@ -470,3 +470,41 @@ func initADCClock() {
470470 esp .APB_SARADC .SetCTRL_SARADC_SAR_CLK_DIV (1 )
471471 esp .APB_SARADC .SetCLKM_CONF_CLK_EN (1 )
472472}
473+
474+ // ReadTemperature reads the on-chip temperature sensor (TSENS) and returns
475+ // a value in millicelsius (°C × 1000). Uses the default measurement range
476+ // (offset = 0, approximately −10 °C to 80 °C, ±3 °C accuracy).
477+ //
478+ // The conversion uses the same formula as ESP-IDF with no eFuse calibration:
479+ //
480+ // T = (0.4386 × raw − 20.52) °C
481+ func ReadTemperature () int32 {
482+ // Enable TSENS peripheral clock.
483+ esp .SENS .SetSAR_PERI_CLK_GATE_CONF_TSENS_CLK_EN (1 )
484+
485+ // Set clock divider to default (6).
486+ esp .SENS .SetSAR_TSENS_CTRL_SAR_TSENS_CLK_DIV (6 )
487+
488+ // Power up the temperature sensor.
489+ esp .SENS .SetSAR_TSENS_CTRL_SAR_TSENS_POWER_UP_FORCE (1 )
490+ esp .SENS .SetSAR_TSENS_CTRL2_SAR_TSENS_XPD_FORCE (1 )
491+ esp .SENS .SetSAR_TSENS_CTRL_SAR_TSENS_POWER_UP (1 )
492+
493+ // Trigger a conversion.
494+ esp .SENS .SetSAR_TSENS_CTRL_SAR_TSENS_DUMP_OUT (1 )
495+
496+ // Wait for data ready.
497+ for esp .SENS .GetSAR_TSENS_CTRL_SAR_TSENS_READY () == 0 {
498+ }
499+
500+ // Read the 8-bit raw value.
501+ raw := int32 (esp .SENS .GetSAR_TSENS_CTRL_SAR_TSENS_OUT ())
502+
503+ // Stop the conversion.
504+ esp .SENS .SetSAR_TSENS_CTRL_SAR_TSENS_DUMP_OUT (0 )
505+
506+ // Convert to millicelsius using the ESP-IDF integer formula (offset=0):
507+ // T_celsius = (4386 * raw - 205200) / 10000
508+ // T_millicelsius = (4386 * raw - 205200) / 10
509+ return (4386 * raw - 205200 ) / 10
510+ }
0 commit comments