Clock Tree

The clock subsystem of ESP32-S2 is used to source and distribute system/module clocks from a range of root clocks. The clock tree driver maintains the basic functionality of the system clock and the intricate relationship among module clocks.

This document starts with the introduction to root and module clocks. Then it covers the clock tree APIs that users can call to monitor the status of the module clocks at runtime.

Introduction

This section lists definitions of the ESP32-S2’s supported root clocks and module clocks. These definitions are commonly used in the driver configuration, to help user select a proper source clock for the peripheral.

Root Clocks

Root clocks generate reliable clock signals. These clock signals then pass through various gates, muxes, dividers, or multipliers to become the clock sources for every functional module: the CPU core(s), WIFI, BT, the RTC, and the peripherals.

ESP32-S2’s root clocks are listed in soc_root_clk_t:

  • Internal 8MHz RC Oscillator (RC_FAST)

    This RC oscillator generates a ~8.5MHz clock signal output as the RC_FAST_CLK.

    The ~8.5MHz signal output is also passed into a configurable divider, which by default divides the input clock frequency by 256, to generate a RC_FAST_D256_CLK.

    The exact frequency of RC_FAST_CLK can be computed in runtime through calibration on the RC_FAST_D256_CLK.

  • External 40MHz Crystal (XTAL)

  • Internal 90kHz RC Oscillator (RC_SLOW)

    This RC oscillator generates a ~90kHz clock signal output as the RC_SLOW_CLK. The exact frequency of this clock can be computed in runtime through calibration.

  • External 32kHz Crystal - optional (XTAL32K)

    The clock source for this XTAL32K_CLK can be either a 32kHz crystal connecting to the XTAL_32K_P and XTAL_32K_N pins or a 32kHz clock signal generated by an external circuit. The external signal must be connected to the XTAL_32K_P pin.

    XTAL32K_CLK can also be calibrated to get its exact frequency.

Typically, the frequency of the signal generated from a RC oscillator circuit is less accurate and more sensitive to environment comparing to the signal generated from a crystal. ESP32-S2 provides several clock source options for the RTC_SLOW_CLK, and users can make the choice based on the requirements for system time accuracy and power consumption (refer to RTC 定时器时钟源 for more details).

Module Clocks

ESP32-S2’s available module clocks are listed in soc_module_clk_t. Each module clock has a unique ID. You can get more information on each clock by checking the documented enum value.

API Usage

The clock tree driver provides an all-in-one API to get the frequency of the module clocks, esp_clk_tree_src_get_freq_hz(). Users can call this function at any moment, with specifying the clock name (soc_module_clk_t) and the desired degree of precision of the returned frequency value (esp_clk_tree_src_freq_precision_t).

API Reference

Header File

Macros

SOC_CLK_RC_FAST_FREQ_APPROX

Approximate RC_FAST_CLK frequency in Hz

SOC_CLK_RC_SLOW_FREQ_APPROX

Approximate RC_SLOW_CLK frequency in Hz

SOC_CLK_RC_FAST_D256_FREQ_APPROX

Approximate RC_FAST_D256_CLK frequency in Hz

SOC_CLK_XTAL32K_FREQ_APPROX

Approximate XTAL32K_CLK frequency in Hz

SOC_GPTIMER_CLKS

Array initializer for all supported clock sources of GPTimer.

The following code can be used to iterate all possible clocks:

soc_periph_gptimer_clk_src_t gptimer_clks[] = (soc_periph_gptimer_clk_src_t)SOC_GPTIMER_CLKS;
for (size_t i = 0; i< sizeof(gptimer_clks) / sizeof(gptimer_clks[0]); i++) {
    soc_periph_gptimer_clk_src_t clk = gptimer_clks[i];
    // Test GPTimer with the clock `clk`
}

SOC_LCD_CLKS

Array initializer for all supported clock sources of LCD.

SOC_RMT_CLKS

Array initializer for all supported clock sources of RMT.

SOC_TEMP_SENSOR_CLKS

Array initializer for all supported clock sources of Temperature Sensor.

SOC_I2S_CLKS

Array initializer for all supported clock sources of I2S.

SOC_I2C_CLKS

Array initializer for all supported clock sources of I2C.

SOC_SPI_CLKS

Array initializer for all supported clock sources of SPI.

SOC_SDM_CLKS

Array initializer for all supported clock sources of SDM.

SOC_GLITCH_FILTER_CLKS

Array initializer for all supported clock sources of Glitch Filter.

SOC_DAC_DIGI_CLKS

Array initializer for all supported clock sources of DAC digital controller.

SOC_DAC_COSINE_CLKS

Array initializer for all supported clock sources of DAC cosine wave generator.

SOC_TWAI_CLKS

Array initializer for all supported clock sources of TWAI.

SOC_ADC_DIGI_CLKS

Array initializer for all supported clock sources of ADC digital controller.

SOC_ADC_RTC_CLKS

Array initializer for all supported clock sources of ADC RTC controller.

SOC_MWDT_CLKS

Array initializer for all supported clock sources of MWDT.

SOC_LEDC_CLKS

Array initializer for all supported clock sources of LEDC.

Enumerations

enum soc_root_clk_t

Root clock.

Values:

enumerator SOC_ROOT_CLK_INT_RC_FAST

Internal 8MHz RC oscillator

enumerator SOC_ROOT_CLK_INT_RC_SLOW

Internal 90kHz RC oscillator

enumerator SOC_ROOT_CLK_EXT_XTAL

External 40MHz crystal

enumerator SOC_ROOT_CLK_EXT_XTAL32K

External 32kHz crystal/clock signal

enum soc_cpu_clk_src_t

CPU_CLK mux inputs, which are the supported clock sources for the CPU_CLK.

备注

Enum values are matched with the register field values on purpose

Values:

enumerator SOC_CPU_CLK_SRC_XTAL

Select XTAL_CLK as CPU_CLK source

enumerator SOC_CPU_CLK_SRC_PLL

Select PLL_CLK as CPU_CLK source (PLL_CLK is the output of 40MHz crystal oscillator frequency multiplier, can be 480MHz or 320MHz)

enumerator SOC_CPU_CLK_SRC_RC_FAST

Select RC_FAST_CLK as CPU_CLK source

enumerator SOC_CPU_CLK_SRC_APLL

Select APLL_CLK as CPU_CLK source

enumerator SOC_CPU_CLK_SRC_INVALID

Invalid CPU_CLK source

enum soc_rtc_slow_clk_src_t

RTC_SLOW_CLK mux inputs, which are the supported clock sources for the RTC_SLOW_CLK.

备注

Enum values are matched with the register field values on purpose

Values:

enumerator SOC_RTC_SLOW_CLK_SRC_RC_SLOW

Select RC_SLOW_CLK as RTC_SLOW_CLK source

enumerator SOC_RTC_SLOW_CLK_SRC_XTAL32K

Select XTAL32K_CLK as RTC_SLOW_CLK source

enumerator SOC_RTC_SLOW_CLK_SRC_RC_FAST_D256

Select RC_FAST_D256_CLK (referred as FOSC_DIV or 8m_d256/8md256 in TRM and reg. description) as RTC_SLOW_CLK source

enumerator SOC_RTC_SLOW_CLK_SRC_INVALID

Invalid RTC_SLOW_CLK source

enum soc_rtc_fast_clk_src_t

RTC_FAST_CLK mux inputs, which are the supported clock sources for the RTC_FAST_CLK.

备注

Enum values are matched with the register field values on purpose

Values:

enumerator SOC_RTC_FAST_CLK_SRC_XTAL_D4

Select XTAL_D4_CLK (may referred as XTAL_CLK_DIV_4) as RTC_FAST_CLK source

enumerator SOC_RTC_FAST_CLK_SRC_XTAL_DIV

Alias name for SOC_RTC_FAST_CLK_SRC_XTAL_D4

enumerator SOC_RTC_FAST_CLK_SRC_RC_FAST

Select RC_FAST_CLK as RTC_FAST_CLK source

enumerator SOC_RTC_FAST_CLK_SRC_INVALID

Invalid RTC_FAST_CLK source

enum soc_module_clk_t

Supported clock sources for modules (CPU, peripherals, RTC, etc.)

备注

enum starts from 1, to save 0 for special purpose

Values:

enumerator SOC_MOD_CLK_CPU

CPU_CLK can be sourced from XTAL, PLL, RC_FAST, or APLL by configuring soc_cpu_clk_src_t

enumerator SOC_MOD_CLK_RTC_FAST

RTC_FAST_CLK can be sourced from XTAL_D4 or RC_FAST by configuring soc_rtc_fast_clk_src_t

enumerator SOC_MOD_CLK_RTC_SLOW

RTC_SLOW_CLK can be sourced from RC_SLOW, XTAL32K, or RC_FAST_D256 by configuring soc_rtc_slow_clk_src_t

enumerator SOC_MOD_CLK_APB

APB_CLK is highly dependent on the CPU_CLK source

enumerator SOC_MOD_CLK_PLL_F160M

PLL_F160M_CLK is derived from PLL, and has a fixed frequency of 160MHz

enumerator SOC_MOD_CLK_XTAL32K

XTAL32K_CLK comes from the external 32kHz crystal, passing a clock gating to the peripherals

enumerator SOC_MOD_CLK_RC_FAST

RC_FAST_CLK comes from the internal 8MHz rc oscillator, passing a clock gating to the peripherals

enumerator SOC_MOD_CLK_RC_FAST_D256

RC_FAST_D256_CLK is derived from the internal 8MHz rc oscillator, divided by 256, and passing a clock gating to the peripherals

enumerator SOC_MOD_CLK_XTAL

XTAL_CLK comes from the external 40MHz crystal

enumerator SOC_MOD_CLK_REF_TICK

REF_TICK is derived from XTAL or RC_FAST via a divider, it has a fixed frequency of 1MHz by default

enumerator SOC_MOD_CLK_APLL

APLL is sourced from PLL, and its frequency is configurable through APLL configuration registers

enumerator SOC_MOD_CLK_TEMP_SENSOR

TEMP_SENSOR_CLK comes directly from the internal 8MHz rc oscillator

enumerator SOC_MOD_CLK_INVALID

Indication of the end of the available module clock sources

enum soc_periph_systimer_clk_src_t

Type of SYSTIMER clock source.

Values:

enumerator SYSTIMER_CLK_SRC_XTAL

SYSTIMER source clock is XTAL

enumerator SYSTIMER_CLK_SRC_DEFAULT

SYSTIMER source clock default choice is XTAL

enum soc_periph_gptimer_clk_src_t

Type of GPTimer clock source.

Values:

enumerator GPTIMER_CLK_SRC_APB

Select APB as the source clock

enumerator GPTIMER_CLK_SRC_XTAL

Select XTAL as the source clock

enumerator GPTIMER_CLK_SRC_DEFAULT

Select APB as the default choice

enum soc_periph_tg_clk_src_legacy_t

Type of Timer Group clock source, reserved for the legacy timer group driver.

Values:

enumerator TIMER_SRC_CLK_APB

Timer group source clock is APB

enumerator TIMER_SRC_CLK_XTAL

Timer group source clock is XTAL

enumerator TIMER_SRC_CLK_DEFAULT

Timer group source clock default choice is APB

enum soc_periph_lcd_clk_src_t

Type of LCD clock source.

Values:

enumerator LCD_CLK_SRC_PLL160M

Select PLL_F160M as the source clock

enumerator LCD_CLK_SRC_DEFAULT

Select PLL_F160M as the default choice

enum soc_periph_rmt_clk_src_t

Type of RMT clock source.

Values:

enumerator RMT_CLK_SRC_APB

Select APB as the source clock

enumerator RMT_CLK_SRC_REF_TICK

Select REF_TICK as the source clock

enumerator RMT_CLK_SRC_DEFAULT

Select APB as the default choice

enum soc_periph_rmt_clk_src_legacy_t

Type of RMT clock source, reserved for the legacy RMT driver.

Values:

enumerator RMT_BASECLK_APB

RMT source clock is APB CLK

enumerator RMT_BASECLK_REF

RMT source clock is REF_TICK

enumerator RMT_BASECLK_DEFAULT

RMT source clock default choice is APB

enum soc_periph_temperature_sensor_clk_src_t

Type of Temp Sensor clock source.

Values:

enumerator TEMPERATURE_SENSOR_CLK_SRC_RC_FAST

Select RC_FAST as the source clock

enumerator TEMPERATURE_SENSOR_CLK_SRC_DEFAULT

Select RC_FAST as the default choice

enum soc_periph_uart_clk_src_legacy_t

Type of UART clock source, reserved for the legacy UART driver.

Values:

enumerator UART_SCLK_APB

UART source clock is APB CLK

enumerator UART_SCLK_REF_TICK

UART source clock is REF_TICK

enumerator UART_SCLK_DEFAULT

UART source clock default choice is APB

enum soc_periph_i2s_clk_src_t

I2S clock source enum.

Values:

enumerator I2S_CLK_SRC_DEFAULT

Select PLL_F160M as the default source clock

enumerator I2S_CLK_SRC_PLL_160M

Select PLL_F160M as the source clock

enumerator I2S_CLK_SRC_APLL

Select APLL as the source clock

enum soc_periph_i2c_clk_src_t

Type of I2C clock source.

Values:

enumerator I2C_CLK_SRC_APB
enumerator I2C_CLK_SRC_REF_TICK
enumerator I2C_CLK_SRC_DEFAULT
enum soc_periph_spi_clk_src_t

Type of SPI clock source.

Values:

enumerator SPI_CLK_SRC_DEFAULT

Select APB as SPI source clock

enumerator SPI_CLK_SRC_APB

Select XTAL as SPI source clock

enum soc_periph_sdm_clk_src_t

Sigma Delta Modulator clock source.

Values:

enumerator SDM_CLK_SRC_APB

Select APB as the source clock

enumerator SDM_CLK_SRC_DEFAULT

Select APB as the default clock choice

enum soc_periph_glitch_filter_clk_src_t

Glitch filter clock source.

Values:

enumerator GLITCH_FILTER_CLK_SRC_APB

Select APB clock as the source clock

enumerator GLITCH_FILTER_CLK_SRC_DEFAULT

Select APB clock as the default clock choice

enum soc_periph_dac_digi_clk_src_t

DAC digital controller clock source.

Values:

enumerator DAC_DIGI_CLK_SRC_APB

Select APB as the source clock

enumerator DAC_DIGI_CLK_SRC_APLL

Select APLL as the source clock

enumerator DAC_DIGI_CLK_SRC_DEFAULT

Select APB as the default source clock

enum soc_periph_dac_cosine_clk_src_t

DAC cosine wave generator clock source.

Values:

enumerator DAC_COSINE_CLK_SRC_RTC_FAST

Select RTC FAST as the source clock

enumerator DAC_COSINE_CLK_SRC_DEFAULT

Select RTC FAST as the default source clock

enum soc_periph_twai_clk_src_t

TWAI clock source.

Values:

enumerator TWAI_CLK_SRC_APB

Select APB as the source clock

enumerator TWAI_CLK_SRC_DEFAULT

Select APB as the default clock choice

enum soc_periph_adc_digi_clk_src_t

ADC digital controller clock source.

Values:

enumerator ADC_DIGI_CLK_SRC_APB

Select APB as the source clock

enumerator ADC_DIGI_CLK_SRC_APLL

Select APLL as the source clock

enumerator ADC_DIGI_CLK_SRC_DEFAULT

Select APB as the default clock choice

enum soc_periph_adc_rtc_clk_src_t

ADC RTC controller clock source.

Values:

enumerator ADC_RTC_CLK_SRC_RC_FAST

Select RC_FAST as the source clock

enumerator ADC_RTC_CLK_SRC_DEFAULT

Select RC_FAST as the default clock choice

enum soc_periph_mwdt_clk_src_t

MWDT clock source.

Values:

enumerator MWDT_CLK_SRC_APB

Select APB as the source clock

enumerator MWDT_CLK_SRC_DEFAULT

Select APB as the default clock choice

enum soc_periph_ledc_clk_src_legacy_t

Type of LEDC clock source, reserved for the legacy LEDC driver.

Values:

enumerator LEDC_AUTO_CLK

LEDC source clock will be automatically selected based on the giving resolution and duty parameter when init the timer

enumerator LEDC_USE_APB_CLK

Select APB as the source clock

enumerator LEDC_USE_RC_FAST_CLK

Select RC_FAST as the source clock

enumerator LEDC_USE_REF_TICK

Select REF_TICK as the source clock

enumerator LEDC_USE_XTAL_CLK

Select XTAL as the source clock

enumerator LEDC_USE_RTC8M_CLK

Alias of ‘LEDC_USE_RC_FAST_CLK’

Header File

Functions

esp_err_t esp_clk_tree_src_get_freq_hz(soc_module_clk_t clk_src, esp_clk_tree_src_freq_precision_t precision, uint32_t *freq_value)

Get frequency of module clock source.

参数
  • clk_src[in] Clock source available to modules, in soc_module_clk_t

  • precision[in] Degree of precision, one of esp_clk_tree_src_freq_precision_t values This arg only applies to the clock sources that their frequencies can vary: SOC_MOD_CLK_RTC_FAST, SOC_MOD_CLK_RTC_SLOW, SOC_MOD_CLK_RC_FAST, SOC_MOD_CLK_RC_FAST_D256, SOC_MOD_CLK_XTAL32K For other clock sources, this field is ignored.

  • freq_value[out] Frequency of the clock source, in Hz

返回

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_FAIL Calibration failed

Enumerations

enum esp_clk_tree_src_freq_precision_t

Degree of precision of frequency value to be returned by esp_clk_tree_src_get_freq_hz()

Values:

enumerator ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED
enumerator ESP_CLK_TREE_SRC_FREQ_PRECISION_APPROX
enumerator ESP_CLK_TREE_SRC_FREQ_PRECISION_EXACT
enumerator ESP_CLK_TREE_SRC_FREQ_PRECISION_INVALID