espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.69k stars 7.29k forks source link

ESP32-H2 LEDC Output is too slow (IDFGH-13499) #14396

Open JonRobo opened 2 months ago

JonRobo commented 2 months ago

Answers checklist.

IDF version.

5.3.2

Espressif SoC revision.

ESP32-H2

Operating System used.

Windows

How did you build your project?

Command line with idf.py

If you are using Windows, please specify command line type.

CMD

Development Kit.

Custom ESP32-H2

Power Supply used.

USB

What is the expected behavior?

Output 200KHz Square Wave

What is the actual behavior?

Outputs a 13Hz Square Wave, verified by my oscilloscope

Steps to reproduce.

include

include "freertos/FreeRTOS.h"

include "freertos/task.h"

include "esp_log.h"

include "driver/adc.h"

include "driver/ledc.h"

include "driver/gpio.h"

include "esp_err.h"

define LEDC_TIMER LEDC_TIMER_0

define LEDC_MODE LEDC_LOW_SPEED_MODE

define LEDC_OUTPUT_IO (5) // Define the output GPIO

define LEDC_CHANNEL LEDC_CHANNEL_3

define LEDC_DUTY_RES LEDC_TIMER_1_BIT // Set duty resolution to 13 bits

define LEDC_DUTY (1 << (LEDC_DUTY_RES - 1))

define LEDC_FREQUENCY (200000)

define ADC1_CHANNEL (ADC1_CHANNEL_3) // GPIO 4 corresponds to ADC1_CHANNEL_3

static void ledc_init(void) { // Prepare and then apply the LEDC PWM timer configuration ledc_timer_config_t ledc_timer = { .speed_mode = LEDC_MODE, .duty_resolution = LEDC_DUTY_RES, .timer_num = LEDC_TIMER, .freq_hz = LEDC_FREQUENCY,
.clk_cfg = LEDC_USE_XTAL_CLK }; ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));

// Prepare and then apply the LEDC PWM channel configuration
ledc_channel_config_t ledc_channel = {
    .speed_mode     = LEDC_MODE,
    .channel        = LEDC_CHANNEL,
    .timer_sel      = LEDC_TIMER,
    .intr_type      = LEDC_INTR_DISABLE,
    .gpio_num       = LEDC_OUTPUT_IO,
    .duty           = LEDC_DUTY, // Set duty to 0%
    .hpoint         = 0
};
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));

uint32_t clk_freq = ledc_get_freq(ledc_timer.speed_mode, ledc_timer.timer_num);

ESP_LOGI("LEDC", "LEDC source clock frequency: %ld Hz", clk_freq);

}

void app_main(void) {

// Set the LEDC peripheral configuration
ledc_init();
// Set duty to 50%
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY));
// Update duty to apply the new value
//ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));

adc1_config_width(ADC_WIDTH_BIT_12);                // 12-bit ADC resolution
adc1_config_channel_atten(ADC1_CHANNEL, ADC_ATTEN_DB_0); // No attenuation

while (1)
{
    // Read the ADC value
    int adc_value = adc1_get_raw(ADC1_CHANNEL);
    ESP_LOGI("ADC", "ADC Value: %d", adc_value);

    // Delay for 100 ms
    vTaskDelay(pdMS_TO_TICKS(100));
}

}

Debug Logs.

No response

More Information.

image image

Trying to make a low power moisture sensor using the esp32-h2, however I cannot produce a 200KHz square wave using this board. I was able to using the dev kit + Arduino but for some reason my board is only giving me 13Hz. Could it be that the LEDC timer is defaulting to a one of the RTC timers somehow? Any help would be appreciated. Thank you

suda-morris commented 2 months ago

I can't reproduce the issue with your code, I can see a 200KHz 50% duty square wave. Have you tried with other GPIO?