High Frequency (MHz) clock output from ESP32-S3

BinaryPoet
Posts: 9
Joined: Thu Aug 31, 2023 1:01 pm

High Frequency (MHz) clock output from ESP32-S3

Postby BinaryPoet » Wed May 22, 2024 11:38 am

Hello,
I need to output a clock signal in the range 8-20Mhz from the IO15 of an ESP32-S3. I had this function previously on ESP32-WROOM using ledc but now discover that ESP32-S3 does not support LEDC_HIGH_SPEED_MODE and the maximum output frequency I get is 16KHz.
However there are peripheral (such as LCD panels) which requires MHz clocks. Is there a way to drive such clock from the internal clock tree to a device pin?

ESP_Sprite
Posts: 9764
Joined: Thu Nov 26, 2015 4:08 am

Re: High Frequency (MHz) clock output from ESP32-S3

Postby ESP_Sprite » Thu May 23, 2024 2:41 am

BinaryPoet wrote:
Wed May 22, 2024 11:38 am
ESP32-S3 does not support LEDC_HIGH_SPEED_MODE and the maximum output frequency I get is 16KHz.
First of all, HIGH_SPEED_MODE refers to the capability to reload the value register automatically; there's no difference in frequency capabilities between the high-speed and low-speed channels of the LEDC in that respect. The -S3 loses that distinction entirely because the peripheral was re-architected a bit: in effect, all -S3 channels are LEDC_HIGH_SPEED_MODE ones.

Aside from that, the -S3 LEDC should be capable of generating up to at least 20MHz, possibly 40MHz. Can you post how you're configuring that peripheral?

BinaryPoet
Posts: 9
Joined: Thu Aug 31, 2023 1:01 pm

Re: High Frequency (MHz) clock output from ESP32-S3

Postby BinaryPoet » Thu May 23, 2024 7:10 am

Thank you for your reply, ESP_Sprite.
ESP_Sprite wrote: Aside from that, the -S3 LEDC should be capable of generating up to at least 20MHz, possibly 40MHz. Can you post how you're configuring that peripheral?
This comforts me. Yesterday the frequency limit was 16KHz today I am not able to output more than 5KHz. The goal is 16MHz.

Here my code:

Code: Select all

#define STEPPER_CLK_FREQUENCY	(16000)
#define STEPPER_CLK_MODE 		LEDC_LOW_SPEED_MODE
#define STEPPER_CLK_TIMER 		LEDC_TIMER_0
#define STEPPER_CLK_RESOLUTION 	LEDC_TIMER_2_BIT
#define STEPPER_CLK_CHANNEL		LEDC_CHANNEL_1
#define STEPPER_CLK_OUTPUT_IO   (15)
#define STEPPER_DUTY			(2)

static void stepperCLK_init(void)
{
    // Prepare and then apply the LEDC PWM timer configuration
    ledc_timer_config_t stepperCLK_timer = {
        .speed_mode       = STEPPER_CLK_MODE,
        .timer_num        = STEPPER_CLK_TIMER,
        .duty_resolution  = LEDC_TIMER_2_BIT,
        .freq_hz          = STEPPER_CLK_FREQUENCY,  
        .clk_cfg          = LEDC_AUTO_CLK
    };
    ESP_ERROR_CHECK(ledc_timer_config(&stepperCLK_timer));

    // Prepare and then apply the LEDC PWM channel configuration
    ledc_channel_config_t ledc_channel_clkout = {
        .speed_mode     = STEPPER_CLK_MODE,
        .channel        = STEPPER_CLK_CHANNEL,
        .timer_sel      = STEPPER_CLK_TIMER,
        .intr_type      = LEDC_INTR_DISABLE,
        .gpio_num       = STEPPER_CLK_OUTPUT_IO,
        .duty           = 0, // Set duty to 0%
        .hpoint         = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel_clkout));
}

void StepperClkEnable(bool en)
{
	if (en)
	{
		ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, STEPPER_CLK_CHANNEL, STEPPER_DUTY));
	}
	else
	{
		ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, STEPPER_CLK_CHANNEL, 0));
	}
}
UPDATE: Now I am able to get 20MHz with the following changes:

Code: Select all

[b]#define STEPPER_CLK_FREQUENCY	(20*1000*1000)[/b]
#define STEPPER_CLK_MODE 		LEDC_LOW_SPEED_MODE
#define STEPPER_CLK_TIMER 		LEDC_TIMER_0
[b]#define STEPPER_CLK_RESOLUTION 	LEDC_TIMER_1_BIT[/b]
#define STEPPER_CLK_CHANNEL	LEDC_CHANNEL_1
#define STEPPER_CLK_OUTPUT_IO   (15)
[b]#define STEPPER_DUTY			(1)[/b]
and

Code: Select all

ledc_timer_config_t stepperCLK_timer = {
        .speed_mode       = STEPPER_CLK_MODE,
        .timer_num        = STEPPER_CLK_TIMER,
        .duty_resolution  = STEPPER_CLK_RESOLUTION,
        .freq_hz          = STEPPER_CLK_FREQUENCY,  
       [b] .clk_cfg          = LEDC_USE_XTAL_CLK[/b]
    };
20MHz should be OK. I tried to get 16MHz but I was not able go get a regular output since it looks impossible to set the duty cycle to 50% with only 1 bit resolution. Is there a way to output a regular 8MHz clock from LEDC_USE_RC_FAST_CLK?

ESP_Sprite
Posts: 9764
Joined: Thu Nov 26, 2015 4:08 am

Re: High Frequency (MHz) clock output from ESP32-S3

Postby ESP_Sprite » Fri May 24, 2024 2:48 am

BinaryPoet wrote:
Thu May 23, 2024 7:10 am
20MHz should be OK. I tried to get 16MHz but I was not able go get a regular output since it looks impossible to set the duty cycle to 50% with only 1 bit resolution. Is there a way to output a regular 8MHz clock from LEDC_USE_RC_FAST_CLK?
No, I think the fast RTC clock is 17.something MHz, so that doesn't divide cleanly into 8MHz. If you use the APB or Xtal clock as a source, you should be able to get 8MHz as both are multiples of that.

Who is online

Users browsing this forum: No registered users and 168 guests