set serial clock source to ref_tick for esp32 platform

This commit is contained in:
m1nl 2025-08-25 19:34:18 +02:00
parent 050b967d5f
commit ed27f91e94
4 changed files with 68 additions and 4 deletions

View File

@ -22,6 +22,7 @@
#include "target_specific.h"
#include <Preferences.h>
#include <driver/rtc_io.h>
#include <driver/uart.h>
#include <nvs.h>
#include <nvs_flash.h>
@ -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
}

View File

@ -58,6 +58,9 @@ Observable<esp_sleep_wakeup_cause_t> 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

View File

@ -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);

View File

@ -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