From 798fb80e222f7896011e8b93e38dcb98f2dbcab9 Mon Sep 17 00:00:00 2001 From: Rick Sorensen Date: Tue, 26 Dec 2023 12:57:46 -0600 Subject: [PATCH 1/2] esp32/modesp32.c: Add mcu_temperature() function for C3/S2/S3 devices. For ESP32C3/S2/S3 IDFv5 exposes new internal temperature API which is different than base ESP32, IDFv4. Thanks to @robert-hh for cleaner code and testing sensor capability in these devices. See discussion #10443 Signed-off-by: Rick Sorensen --- docs/esp32/quickref.rst | 12 +++++++++++- ports/esp32/modesp32.c | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index 08df25b055..8c019ce647 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -18,6 +18,9 @@ working with this board it may be useful to get an overview of the microcontroll general.rst tutorial/index.rst +Note that there are several varieties of ESP32- ESP32, ESP32C3, ESP32S2, ESP32S3 supported by MicroPython, +with some differences in functionality between them. + Installing MicroPython ---------------------- @@ -58,12 +61,19 @@ The :mod:`esp32` module:: import esp32 esp32.raw_temperature() # read the internal temperature of the MCU, in Fahrenheit - esp32.ULP() # access to the Ultra-Low-Power Co-processor + esp32.ULP() # access to the Ultra-Low-Power Co-processor, not on ESP32C3 Note that the temperature sensor in the ESP32 will typically read higher than ambient due to the IC getting warm while it runs. This effect can be minimised by reading the temperature sensor immediately after waking up from sleep. +ESP32C3, ESP32S2, and ESP32S3 also have an internal sensor available. +It is implemented a bit differently than the ESP32 and returns +temperature in Celsius:: + + esp32.mcu_temperature() # read the internal temperature of the MCU, in Celsius + + Networking ---------- diff --git a/ports/esp32/modesp32.c b/ports/esp32/modesp32.c index ef3ad0b76d..131f817d3e 100644 --- a/ports/esp32/modesp32.c +++ b/ports/esp32/modesp32.c @@ -168,6 +168,25 @@ STATIC mp_obj_t esp32_raw_temperature(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32_raw_temperature_obj, esp32_raw_temperature); +#else +// IDF 5 exposes new internal temperature interface, and the ESP32C3/S2/S3 +// now have calibrated temperature settings in 5 discrete ranges. +#include "driver/temperature_sensor.h" + +STATIC mp_obj_t esp32_mcu_temperature(void) { + static temperature_sensor_handle_t temp_sensor = NULL; + float tvalue; + if (temp_sensor == NULL) { + temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80); + ESP_ERROR_CHECK(temperature_sensor_install(&temp_sensor_config, &temp_sensor)); + } + ESP_ERROR_CHECK(temperature_sensor_enable(temp_sensor)); + ESP_ERROR_CHECK(temperature_sensor_get_celsius(temp_sensor, &tvalue)); + ESP_ERROR_CHECK(temperature_sensor_disable(temp_sensor)); + return mp_obj_new_int((int)(tvalue + 0.5)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32_mcu_temperature_obj, esp32_mcu_temperature); + #endif STATIC mp_obj_t esp32_idf_heap_info(const mp_obj_t cap_in) { @@ -202,6 +221,8 @@ STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_gpio_deep_sleep_hold), MP_ROM_PTR(&esp32_gpio_deep_sleep_hold_obj) }, #if CONFIG_IDF_TARGET_ESP32 { MP_ROM_QSTR(MP_QSTR_raw_temperature), MP_ROM_PTR(&esp32_raw_temperature_obj) }, + #else + { MP_ROM_QSTR(MP_QSTR_mcu_temperature), MP_ROM_PTR(&esp32_mcu_temperature_obj) }, #endif { MP_ROM_QSTR(MP_QSTR_idf_heap_info), MP_ROM_PTR(&esp32_idf_heap_info_obj) }, From 3832098338148c23200b55da6a8d05de1496d9c9 Mon Sep 17 00:00:00 2001 From: Rick Sorensen Date: Tue, 12 Mar 2024 12:20:25 -0500 Subject: [PATCH 2/2] modesp32.c: Remove use of STATIC macro per PR #13763. Signed-off-by: Rick Sorensen --- ports/esp32/modesp32.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/esp32/modesp32.c b/ports/esp32/modesp32.c index 146e6e9e82..33cb08e704 100644 --- a/ports/esp32/modesp32.c +++ b/ports/esp32/modesp32.c @@ -173,7 +173,7 @@ static MP_DEFINE_CONST_FUN_OBJ_0(esp32_raw_temperature_obj, esp32_raw_temperatur // now have calibrated temperature settings in 5 discrete ranges. #include "driver/temperature_sensor.h" -STATIC mp_obj_t esp32_mcu_temperature(void) { +static mp_obj_t esp32_mcu_temperature(void) { static temperature_sensor_handle_t temp_sensor = NULL; float tvalue; if (temp_sensor == NULL) { @@ -185,7 +185,7 @@ STATIC mp_obj_t esp32_mcu_temperature(void) { ESP_ERROR_CHECK(temperature_sensor_disable(temp_sensor)); return mp_obj_new_int((int)(tvalue + 0.5)); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32_mcu_temperature_obj, esp32_mcu_temperature); +static MP_DEFINE_CONST_FUN_OBJ_0(esp32_mcu_temperature_obj, esp32_mcu_temperature); #endif