diff --git a/src/platform/esp32/main-esp32.cpp b/src/platform/esp32/main-esp32.cpp index 7b6b9dcff..f47f0e1bb 100644 --- a/src/platform/esp32/main-esp32.cpp +++ b/src/platform/esp32/main-esp32.cpp @@ -22,6 +22,7 @@ #include "target_specific.h" #include #include +#include #include #include @@ -263,3 +264,51 @@ void cpuDeepSleep(uint32_t msecToWake) esp_sleep_enable_timer_wakeup(msecToWake * 1000ULL); // call expects usecs esp_deep_sleep_start(); // TBD mA sleep current (battery) } + +bool setSerialClockToRefTick(int uart_num) +{ +#if !SOC_UART_SUPPORT_REF_TICK + return false; +#else + uart_config_t uart_config; + uint32_t baudrate; + + if (uart_get_baudrate(uart_num, &baudrate) != ESP_OK) { + LOG_ERROR("Unable to get UART baudrate"); + return false; + } + + uart_config.baud_rate = (int)baudrate; + + if (uart_get_word_length(uart_num, &uart_config.data_bits) != ESP_OK) { + LOG_ERROR("Unable to get UART baudrate"); + return false; + } + if (uart_get_parity(uart_num, &uart_config.parity) != ESP_OK) { + LOG_ERROR("Unable to get UART baudrate"); + return false; + } + if (uart_get_stop_bits(uart_num, &uart_config.stop_bits) != ESP_OK) { + LOG_ERROR("Unable to get UART baudrate"); + return false; + } + if (uart_get_hw_flow_ctrl(uart_num, &uart_config.flow_ctrl) != ESP_OK) { + LOG_ERROR("Unable to get UART baudrate"); + return false; + } + + if (uart_config.flow_ctrl != UART_HW_FLOWCTRL_DISABLE) { + uart_config.rx_flow_ctrl_thresh = 122; + } + + uart_config.source_clk = UART_SCLK_REF_TICK; + + // Configure UART parameters + if (uart_param_config(uart_num, &uart_config) != ESP_OK) { + LOG_ERROR("Unable to get UART baudrate"); + return false; + } + + return true; +#endif +} diff --git a/src/sleep.cpp b/src/sleep.cpp index 7cfcece8a..70d431798 100644 --- a/src/sleep.cpp +++ b/src/sleep.cpp @@ -58,6 +58,9 @@ Observable notifyLightSleepEnd; esp_pm_lock_handle_t pmLightSleepLock; #endif +// this are imported symbol with target-specific implementation +bool setSerialClockToRefTick(int uart_num); + // restores GPIO function after sleep void gpioReset(void); // enables button wake-up interrupt @@ -68,6 +71,9 @@ void enableLoraInterrupt(void); bool shouldLoraWake(uint32_t msecToWake); #endif +// this are imported symbol with target-specific implementation +void cpuDeepSleep(uint32_t msecToWake); + // deep sleep support RTC_DATA_ATTR int bootCount = 0; @@ -427,6 +433,13 @@ void doLightSleep(uint32_t sleepMsec) void initLightSleep() { esp_err_t res; + bool dfsSupported = true; + +#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2) + if (dfsSupported) { + dfsSupported &= setSerialClockToRefTick(UART_NUM_0); + } +#endif #ifdef HAS_ESP32_PM_SUPPORT res = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "meshtastic", &pmLightSleepLock); @@ -437,7 +450,7 @@ void initLightSleep() esp_pm_config_esp32_t pm_config; pm_config.max_freq_mhz = 80; - pm_config.min_freq_mhz = 20; + pm_config.min_freq_mhz = dfsSupported ? 20 : pm_config.max_freq_mhz; #ifdef HAS_ESP32_DYNAMIC_LIGHT_SLEEP pm_config.light_sleep_enable = true; #else diff --git a/src/sleep.h b/src/sleep.h index 14ac890b2..ea2906c44 100644 --- a/src/sleep.h +++ b/src/sleep.h @@ -21,7 +21,7 @@ void doLightSleep(uint32_t msecToWake); // perform power on init that we do on each wake from deep sleep void initDeepSleep(); -void doDeepSleep(uint32_t msecToWake, bool skipPreflight, bool skipSaveNodeDb), cpuDeepSleep(uint32_t msecToWake); +void doDeepSleep(uint32_t msecToWake, bool skipPreflight, bool skipSaveNodeDb); void setCPUFast(bool on); diff --git a/variants/esp32/heltec_v2.1/platformio.ini b/variants/esp32/heltec_v2.1/platformio.ini index 763f9764c..74d991ce7 100644 --- a/variants/esp32/heltec_v2.1/platformio.ini +++ b/variants/esp32/heltec_v2.1/platformio.ini @@ -1,9 +1,11 @@ [env:heltec-v2_1] board_level = extra -;build_type = debug ; to make it possible to step through our jtag debugger +;build_type = debug ; to make it possible to step through our jtag debugger extends = esp32_base board = heltec_wifi_lora_32_V2 -build_flags = +platform_packages = + platformio/framework-arduinoespressif32 @ https://github.com/m1nl/arduino-esp32/archive/refs/tags/2.0.17+7b9546f7.tar.gz ; optimize IRAM usage for ESP32 platform +build_flags = ${esp32_base.build_flags} -D HELTEC_V2_1 -I variants/esp32/heltec_v2.1