ESP32-C6: possible to route 40Mhz XTAL to gpio pin?

greengnu
Posts: 66
Joined: Wed May 08, 2019 8:45 pm

ESP32-C6: possible to route 40Mhz XTAL to gpio pin?

Postby greengnu » Thu Mar 12, 2026 10:51 pm

On the esp32-s3 I can route the 40Mhz clock to a pin with the following code:

Code: Select all

#include "driver/gpio.h"
#include "driver/ledc.h"
#include "hal/clk_tree_hal.h"
#include "soc/clkout_channel.h"
#include "soc/io_mux_reg.h"

void route_40mhz_xtal_to_gpio39_s3() {
    // 1. Set GPIO 39 as an output
    gpio_set_direction(GPIO_NUM_39, GPIO_MODE_OUTPUT);
    
    // 2. Set to maximum drive strength to keep the 40MHz edges sharp
    gpio_set_drive_capability(GPIO_NUM_39, GPIO_DRIVE_CAP_3);

    // 3. Configure the IO_MUX: 
    // On the ESP32-S3, 'Function 2' on GPIO 39 routes the physical pin to CLK_OUT3
    PIN_FUNC_SELECT(IO_MUX_GPIO39_REG, 2);

    // 4. Configure the clock tree to send the Main XTAL to CLKOUT Channel 3
    clk_hal_clock_output_setup(CLKOUT_SIG_XTAL, CLKOUT_CHANNEL_3);
}

void setup() {
    route_40mhz_xtal_to_gpio39_s3();
}

void loop() {
  delay(1000);
}
ESP32-C6 doesn't seem to have CLKOUT channels. Is there some other way to route the 40Mhz root clock to a gpio or is it only possible to create a PLL derived 40Mhz clock using LEDC?

User avatar
krzychb
Espressif staff
Espressif staff
Posts: 424
Joined: Sat Oct 01, 2016 9:05 am
Contact:

Re: ESP32-C6: possible to route 40Mhz XTAL to gpio pin?

Postby krzychb » Mon Mar 16, 2026 11:07 am

Hi greengnu,

There is an API esp_clock_output_start() to route clock sources, including the 40 MHz root clock, to a GPIO:

Code: Select all

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_check.h"
#include "esp_clock_output.h"
#include "esp_log.h"

#define XTAL_OUTPUT_GPIO GPIO_NUM_3

static const char *TAG = "pin_40mhz";
static esp_clock_output_mapping_handle_t s_clkout_handle;

static void route_40mhz_xtal_to_gpio(void)
{
    ESP_ERROR_CHECK(esp_clock_output_start(CLKOUT_SIG_XTAL, XTAL_OUTPUT_GPIO, &s_clkout_handle));
    ESP_LOGI(TAG, "Routing 40 MHz XTAL to GPIO%d", XTAL_OUTPUT_GPIO);
}

void app_main(void)
{
    route_40mhz_xtal_to_gpio();

    while (1) {
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}


Who is online

Users browsing this forum: Baidu [Spider], coccocbot and 4 guests