Page 1 of 2

100microsecond timer in esp32

Posted: Mon Apr 24, 2017 5:27 pm
by Abhiram
Dear All,

I am new to esp32 wanted to know how I can have a timer for 100 microseconds. An interrupt is to be generated at the lapse of 100microseconds
Should I use LEDC example or HW timer example or does FreeRtos API supports this. From my study so far I could not find freertos support for microseconds resolution.
Kindly let me know which one I should be using for accurate time period.

Thanks for help.

Re: 100microsecond timer in esp32

Posted: Tue Apr 25, 2017 2:14 am
by ESP_Sprite
You can probably use any of the timer sources for this... one of the timer groups, the LED PWM controller timers, maybe even the built-in Xtensa timers if you do not need clock switching... For what exactly do you require this, however? It might be that using a dedicated hardware peripheral is more suited to your needs.

Re: 100microsecond timer in esp32

Posted: Tue Apr 25, 2017 10:37 am
by Abhiram
I tried using Xtensa Timers , using xos.h and xos_timer.h. There are compilation errors and xos_clock_freq undefined error. Any config changes required for using Xtensa Timer APIs. I had set esp32 clock freq as 80MZ.

Re: 100microsecond timer in esp32

Posted: Tue Apr 25, 2017 11:07 am
by ESP_igrr
You can use the following code for the TG0 timer:

Code: Select all

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

static intr_handle_t s_timer_handle;

static void timer_isr(void* arg)
{
    TIMERG0.int_clr_timers.t0 = 1;
    TIMERG0.hw_timer[0].config.alarm_en = 1;

    // your code, runs in the interrupt
}

void init_timer(int timer_period_us)
{
    timer_config_t config = {
            .alarm_en = true,
            .counter_en = false,
            .intr_type = TIMER_INTR_LEVEL,
            .counter_dir = TIMER_COUNT_UP,
            .auto_reload = true,
            .divider = 80   /* 1 us per tick */
    };
    
    timer_init(TIMER_GROUP_0, TIMER_0, &config);
    timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
    timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, timer_period_us);
    timer_enable_intr(TIMER_GROUP_0, TIMER_0);
    timer_isr_register(TIMER_GROUP_0, TIMER_0, &timer_isr, NULL, 0, &s_timer_handle);

    timer_start(TIMER_GROUP_0, TIMER_0);
}

void app_main()
{
    init_timer(100);
}
Also check the timer group example: https://github.com/espressif/esp-idf/bl ... ple_main.c

Re: 100microsecond timer in esp32

Posted: Wed May 03, 2017 4:43 am
by rajkumar patel
hi Abhiram,

have you got any update or info regarding feasibility of xtensa(xos) timers from here or from other sources?
i'm also looking forward to use xtensa timers.

thanks.
regards.

Re: 100microsecond timer in esp32

Posted: Wed May 03, 2017 6:25 am
by rajkumar patel
Abhiram wrote:I tried using Xtensa Timers , using xos.h and xos_timer.h. There are compilation errors and xos_clock_freq undefined error. Any config changes required for using Xtensa Timer APIs. I had set esp32 clock freq as 80MZ.
hey Abhiram,

have a look at this,
xos.JPG
snap of errors which i've encountered while compiling an application which uses xos_timer's APIs
xos.JPG (76.35 KiB) Viewed 24043 times

i've also encountered the same linking error but along with some more undefined references to xos_timer functions. but i'm not clear about which lib file to add and where to add. do u have any idea regarding this?

regards.

Re: 100microsecond timer in esp32

Posted: Mon May 15, 2017 9:55 am
by Abhiram
Hi Rajkumar ,
No I gave up using Xtensa timers. no Clarity yet on those lines

Re: 100microsecond timer in esp32

Posted: Mon May 15, 2017 1:19 pm
by rajkumar patel
let's hope someone will come up with some practicality details of these timers. :|

Re: 100microsecond timer in esp32

Posted: Mon May 15, 2017 2:38 pm
by ESP_igrr
You can refer to FreeRTOS port layer code for an example of how these timers are used (portasm.S). In C, RSR and WSR macros can be used to read and write CCOUNT and CCOMPAREx registers.

xos functions mentioned above can not be used in IDF because IDF doesn't include corresponding libraries or source files. The header files are still present for some historical reason.

I do suggest trying the hardware timers (TimerGroup peripherals) instead of Xtensa timers, as these are documented in the TRM and there is an example illustrating their use in ESP-IDF.

Re: 100microsecond timer in esp32

Posted: Wed May 24, 2017 1:26 pm
by rajkumar patel
Hi ESP_igrr,

that's true, all i was using for reference was header file of that xtensa(xos)timers. i didn't thought of missing libraries as this sort of case is very rare. thanks for shading light on this.
i tried hardware timer feasibility and that works just fine. but microsecond resolution test i haven't performed yet.

regards.