Use of either LEDC or general timers for very short (<2us) pulses

Dwagner6
Posts: 2
Joined: Sat Nov 04, 2023 5:38 pm

Use of either LEDC or general timers for very short (<2us) pulses

Postby Dwagner6 » Sat Nov 04, 2023 5:47 pm

Hello!

I have been working with an ESP32-S2 board to create a pretty simple, very low duty cycle (1kHz 0.1% duty or less) pulser. The application for this is to drive a circuit that creates very short high current pulses for testing laser diodes.

The LEDC peripheral works great for creating these pulses, even down to 500ns or less, and the pulse width is easily adjustable on the fly. My issue is that I need something like an interrupt on the falling edge of the pulse to trigger an ADC reading from a current sensor in the circuit (that part of the circuit works fine). I’m having trouble determining if this is possible with the LEDC peripheral.

Anyone able to help me with this? Or is a better solution to implement this using the general purpose timers? My experience is pretty limited when it comes to C and ESP-IDF, so I have not been able to determine for myself what the best path to follow is.

Thanks for any insight.

Maxzillian
Posts: 8
Joined: Fri Apr 01, 2022 3:06 pm

Re: Use of either LEDC or general timers for very short (<2us) pulses

Postby Maxzillian » Mon Nov 06, 2023 4:57 pm

You might be able to utilize what I've done here to configure the pin as an input-and-output. At that point maybe you can configure a falling-edge interrupt as you would on any GPIO input?

viewtopic.php?f=19&t=13347#p122786

Edit: Funny enough, I just had to cross this bridge. After following what I proposed in the linked post, you can set set up an interrupt on the pin:

Code: Select all

gpio_install_isr_service(0); // no error check so we don't assert if the service is already running
ESP_ERROR_CHECK(gpio_isr_handler_add(GPIO_NUM_33, (gpio_isr_t)pin_change, this));
ESP_ERROR_CHECK(gpio_set_intr_type(GPIO_NUM_33, GPIO_INTR_NEGEDGE));
ESP_ERROR_CHECK(gpio_intr_enable(GPIO_NUM_33));
Define your callback function in your header file:

Code: Select all

static void IRAM_ATTR pin_change(void * pData);
And then finally the callback function itself:

Code: Select all

void IRAM_ATTR pin_change(void * pData) {
    ets_printf("Pin change!\n");
}
edit: It occurred to me that this is the Arduino flavor of the forum. The above example is written for the ESP-IDF, but the concept is all the same.

Dwagner6
Posts: 2
Joined: Sat Nov 04, 2023 5:38 pm

Re: Use of either LEDC or general timers for very short (<2us) pulses

Postby Dwagner6 » Tue Nov 07, 2023 12:53 pm

This is really helpful, and works! Thank you! The Arduino-world attachInterrupt() works just as well for falling edge on the LEDC output pin. Even on a <500ns pulse width.

Thanks again.

Who is online

Users browsing this forum: No registered users and 97 guests