Page 1 of 1

esp32 high resolution timer

Posted: Sun May 06, 2018 1:22 pm
by vinimac
Hello,

I am trying to use the esp32 high-resolution timers but I am not getting the right tick interval. I am calling esp_timer_start_periodic() with 10 microseconds but it is triggering at 50 microseconds. Am I configuring correctly?

Code: Select all


#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

#include <stddef.h>
#include "esp_intr_alloc.h"
#include "esp_attr.h"
#include "driver/timer.h"
#include "esp_timer.h"

/* Can run 'make menuconfig' to choose the GPIO to blink,
   or you can edit the following line and set a number here.
*/
#define BLINK_GPIO CONFIG_BLINK_GPIO

char state;

static void timer_func(void* arg)
{
    // your code, runs in the interrupt
    if (state) {
        state = 0;
        gpio_set_level(BLINK_GPIO, 1);
    } else {
        state = 1;
        gpio_set_level(BLINK_GPIO, 0);
    }
}

void init_timer(int timer_period_us)
{
    int64_t t_end;
    esp_timer_handle_t timer1;
    esp_timer_create_args_t args = {
            .callback = &timer_func,
            .arg = &t_end,
            .name = "timer1"
    };
    // ---
    gpio_pad_select_gpio(BLINK_GPIO);
    /* Set the GPIO as a push/pull output */
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
    //----
    esp_timer_create(&args, &timer1);
    //ref_clock_init();
    esp_timer_start_periodic(timer1, timer_period_us);

    while(1) {
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
    esp_timer_delete(timer1);
}

void app_main()
{
    init_timer(10); // 10uS
}

Re: esp32 high resolution timer

Posted: Sun May 06, 2018 2:30 pm
by ESP_igrr
Sorry, the minimal period limit is not mentioned in the docs. Will add a note...

https://github.com/espressif/esp-idf/bl ... p32.c#L374

https://github.com/espressif/esp-idf/bl ... mer.c#L138

If you were planning to use esp_timer to generate a waveform, then it might not be the right choice. There are a few tasks with priorities higher than that of esp_timer (like WiFi task), so while those tasks are running, esp_timer will not dispatch callbacks. Besides, running a timer every few tens of microseconds leaves nearly no time for the system to do other things. That's why the limitation on minimal period is there.

Other options might be to use RMT peripheral (if you need to generate waveforms) or to use Timer Group timers, attach the interrupt to CPU1 and make it a level 3 interrupt, and do GPIO writes from interrupt handler.

Re: esp32 high resolution timer

Posted: Sun May 06, 2018 10:14 pm
by vinimac
Hi igrr,

Thanks for the repply. My goal is to do high speed ADC capture using a ADC chip which has a 100Ksps sample rate.

Is there any example for attach the interrupt to CPU1?

Re: esp32 high resolution timer

Posted: Mon May 07, 2018 3:44 am
by kolban
Purely for my curiosity, what kind of application / sensing needs to sample an analog signal 100,000 times per second?

Re: esp32 high resolution timer

Posted: Mon May 07, 2018 4:05 am
by ESP_igrr
For such sample rate, it might make sense to use DMA functionality. I2S peripheral can, among other things, move data from ADC to RAM or from RAM to DAC. examples/peripherals/i2s_adc_dac has an example of that.