100microsecond timer in esp32

Abhiram
Posts: 22
Joined: Mon Apr 24, 2017 5:09 pm

100microsecond timer in esp32

Postby Abhiram » Mon Apr 24, 2017 5:27 pm

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.

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

Re: 100microsecond timer in esp32

Postby ESP_Sprite » Tue Apr 25, 2017 2:14 am

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.

Abhiram
Posts: 22
Joined: Mon Apr 24, 2017 5:09 pm

Re: 100microsecond timer in esp32

Postby Abhiram » Tue Apr 25, 2017 10:37 am

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.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: 100microsecond timer in esp32

Postby ESP_igrr » Tue Apr 25, 2017 11:07 am

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

User avatar
rajkumar patel
Posts: 29
Joined: Mon Apr 10, 2017 12:43 pm
Location: india

Re: 100microsecond timer in esp32

Postby rajkumar patel » Wed May 03, 2017 4:43 am

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.
Regards,
Rajkumar M. Patel

User avatar
rajkumar patel
Posts: 29
Joined: Mon Apr 10, 2017 12:43 pm
Location: india

Re: 100microsecond timer in esp32

Postby rajkumar patel » Wed May 03, 2017 6:25 am

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 24037 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.
Regards,
Rajkumar M. Patel

Abhiram
Posts: 22
Joined: Mon Apr 24, 2017 5:09 pm

Re: 100microsecond timer in esp32

Postby Abhiram » Mon May 15, 2017 9:55 am

Hi Rajkumar ,
No I gave up using Xtensa timers. no Clarity yet on those lines

User avatar
rajkumar patel
Posts: 29
Joined: Mon Apr 10, 2017 12:43 pm
Location: india

Re: 100microsecond timer in esp32

Postby rajkumar patel » Mon May 15, 2017 1:19 pm

let's hope someone will come up with some practicality details of these timers. :|
Regards,
Rajkumar M. Patel

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: 100microsecond timer in esp32

Postby ESP_igrr » Mon May 15, 2017 2:38 pm

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.

User avatar
rajkumar patel
Posts: 29
Joined: Mon Apr 10, 2017 12:43 pm
Location: india

Re: 100microsecond timer in esp32

Postby rajkumar patel » Wed May 24, 2017 1:26 pm

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.
Regards,
Rajkumar M. Patel

Who is online

Users browsing this forum: Bing [Bot] and 88 guests