Page 1 of 1

Set an alarm with the same time base as returned by esp_timer_get_time()

Posted: Fri Nov 15, 2019 4:19 pm
by AloyseTech
Hi,

How can I set an alarm to a given timestamp in microseconds with the same base as esp_timer_get_time() ?
It seems that esp_timer_get_time does not use timer group so they do not share the same time base.

Here is a pseudo code of what I need:

Code: Select all

void app_main()
{
    execAt = postParam(esp_timer_get_time()); //return a timestamp relative to the input at which an event must be triggered
    configure_alarm_at(execAt, handler);
}

void IRAM handler()
{
    execute();
}

Re: Set an alarm with the same time base as returned by esp_timer_get_time()

Posted: Fri Nov 15, 2019 6:51 pm
by AloyseTech
The esp_timer common components has a function called esp_timer_start_once(timer, relative_time_from_now)

It could be great to have a similar API called esp_timer_start_alarm(timer, absolute_time)

Implementation would be :

Code: Select all

esp_err_t IRAM_ATTR esp_timer_start_alarm(esp_timer_handle_t timer, uint64_t timestamp_us)
{
    if (timer == NULL) {
        return ESP_ERR_INVALID_ARG;
    }
    if (!is_initialized() || timer_armed(timer)) {
        return ESP_ERR_INVALID_STATE;
    }
    timer_list_lock();
    timer->alarm = timestamp_us; // start_once use esp_timer_get_time() + timeout_us
    timer->period = 0;
#if WITH_PROFILING
    timer->times_armed++;
#endif
    esp_err_t err = timer_insert(timer);
    timer_list_unlock();
    return err;
}