From 5ee8ea7fc01ca02990480209a12c6159394ea859 Mon Sep 17 00:00:00 2001 From: Wikwoj0512 Date: Tue, 12 May 2026 01:45:42 +0200 Subject: [PATCH 1/2] [feature] added temperature reading to esp32s3 --- src/machine/machine_esp32s3_temperature.go | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/machine/machine_esp32s3_temperature.go diff --git a/src/machine/machine_esp32s3_temperature.go b/src/machine/machine_esp32s3_temperature.go new file mode 100644 index 0000000000..7b13cfd2e1 --- /dev/null +++ b/src/machine/machine_esp32s3_temperature.go @@ -0,0 +1,43 @@ +//go:build esp32s3 + +package machine + +import "device/esp" + +// ReadTemperature reads the on-chip temperature sensor (TSENS) and returns +// a value in millicelsius (°C × 1000). Uses the default measurement range +// (offset = 0, approximately −10 °C to 80 °C, ±3 °C accuracy). +// +// The conversion uses the same formula as ESP-IDF with no eFuse calibration: +// +// T = (0.4386 × raw − 20.52) °C +func ReadTemperature() int32 { + // Enable TSENS peripheral clock. + esp.SENS.SetSAR_PERI_CLK_GATE_CONF_TSENS_CLK_EN(1) + + // Set clock divider to default (6). + esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_CLK_DIV(6) + + // Power up the temperature sensor. + esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_POWER_UP_FORCE(1) + esp.SENS.SetSAR_TSENS_CTRL2_SAR_TSENS_XPD_FORCE(1) + esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_POWER_UP(1) + + // Trigger a conversion. + esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_DUMP_OUT(1) + + // Wait for data ready. + for esp.SENS.GetSAR_TSENS_CTRL_SAR_TSENS_READY() == 0 { + } + + // Read the 8-bit raw value. + raw := int32(esp.SENS.GetSAR_TSENS_CTRL_SAR_TSENS_OUT()) + + // Stop the conversion. + esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_DUMP_OUT(0) + + // Convert to millicelsius using the ESP-IDF integer formula (offset=0): + // T_celsius = (4386 * raw - 205200) / 10000 + // T_millicelsius = (4386 * raw - 205200) / 10 + return (4386*raw - 205200) / 10 +} From a7f60afcb7cd4e2238c007e86ec0226b71d80861 Mon Sep 17 00:00:00 2001 From: wikto Date: Sun, 7 Jun 2026 19:25:52 +0200 Subject: [PATCH 2/2] [feature] moved ReadTemeprature to machine_esp32s3.go --- src/machine/machine_esp32s3.go | 38 +++++++++++++++++++ src/machine/machine_esp32s3_temperature.go | 43 ---------------------- 2 files changed, 38 insertions(+), 43 deletions(-) delete mode 100644 src/machine/machine_esp32s3_temperature.go diff --git a/src/machine/machine_esp32s3.go b/src/machine/machine_esp32s3.go index e7582688f1..6c71e07cc8 100644 --- a/src/machine/machine_esp32s3.go +++ b/src/machine/machine_esp32s3.go @@ -470,3 +470,41 @@ func initADCClock() { esp.APB_SARADC.SetCTRL_SARADC_SAR_CLK_DIV(1) esp.APB_SARADC.SetCLKM_CONF_CLK_EN(1) } + +// ReadTemperature reads the on-chip temperature sensor (TSENS) and returns +// a value in millicelsius (°C × 1000). Uses the default measurement range +// (offset = 0, approximately −10 °C to 80 °C, ±3 °C accuracy). +// +// The conversion uses the same formula as ESP-IDF with no eFuse calibration: +// +// T = (0.4386 × raw − 20.52) °C +func ReadTemperature() int32 { + // Enable TSENS peripheral clock. + esp.SENS.SetSAR_PERI_CLK_GATE_CONF_TSENS_CLK_EN(1) + + // Set clock divider to default (6). + esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_CLK_DIV(6) + + // Power up the temperature sensor. + esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_POWER_UP_FORCE(1) + esp.SENS.SetSAR_TSENS_CTRL2_SAR_TSENS_XPD_FORCE(1) + esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_POWER_UP(1) + + // Trigger a conversion. + esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_DUMP_OUT(1) + + // Wait for data ready. + for esp.SENS.GetSAR_TSENS_CTRL_SAR_TSENS_READY() == 0 { + } + + // Read the 8-bit raw value. + raw := int32(esp.SENS.GetSAR_TSENS_CTRL_SAR_TSENS_OUT()) + + // Stop the conversion. + esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_DUMP_OUT(0) + + // Convert to millicelsius using the ESP-IDF integer formula (offset=0): + // T_celsius = (4386 * raw - 205200) / 10000 + // T_millicelsius = (4386 * raw - 205200) / 10 + return (4386*raw - 205200) / 10 +} diff --git a/src/machine/machine_esp32s3_temperature.go b/src/machine/machine_esp32s3_temperature.go deleted file mode 100644 index 7b13cfd2e1..0000000000 --- a/src/machine/machine_esp32s3_temperature.go +++ /dev/null @@ -1,43 +0,0 @@ -//go:build esp32s3 - -package machine - -import "device/esp" - -// ReadTemperature reads the on-chip temperature sensor (TSENS) and returns -// a value in millicelsius (°C × 1000). Uses the default measurement range -// (offset = 0, approximately −10 °C to 80 °C, ±3 °C accuracy). -// -// The conversion uses the same formula as ESP-IDF with no eFuse calibration: -// -// T = (0.4386 × raw − 20.52) °C -func ReadTemperature() int32 { - // Enable TSENS peripheral clock. - esp.SENS.SetSAR_PERI_CLK_GATE_CONF_TSENS_CLK_EN(1) - - // Set clock divider to default (6). - esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_CLK_DIV(6) - - // Power up the temperature sensor. - esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_POWER_UP_FORCE(1) - esp.SENS.SetSAR_TSENS_CTRL2_SAR_TSENS_XPD_FORCE(1) - esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_POWER_UP(1) - - // Trigger a conversion. - esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_DUMP_OUT(1) - - // Wait for data ready. - for esp.SENS.GetSAR_TSENS_CTRL_SAR_TSENS_READY() == 0 { - } - - // Read the 8-bit raw value. - raw := int32(esp.SENS.GetSAR_TSENS_CTRL_SAR_TSENS_OUT()) - - // Stop the conversion. - esp.SENS.SetSAR_TSENS_CTRL_SAR_TSENS_DUMP_OUT(0) - - // Convert to millicelsius using the ESP-IDF integer formula (offset=0): - // T_celsius = (4386 * raw - 205200) / 10000 - // T_millicelsius = (4386 * raw - 205200) / 10 - return (4386*raw - 205200) / 10 -}