chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,895 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2017-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <string.h>
|
||||
#include "soc/soc.h"
|
||||
#include "esp_types.h"
|
||||
#include "esp_attr.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_task.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_timer_impl.h"
|
||||
#include "esp_compiler.h"
|
||||
#include "esp_private/startup_internal.h"
|
||||
#include "esp_private/esp_timer_private.h"
|
||||
#include "esp_private/system_internal.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#ifdef CONFIG_ESP_TIMER_PROFILING
|
||||
#define WITH_PROFILING 1
|
||||
#endif
|
||||
|
||||
#ifndef NDEBUG
|
||||
// Enable built-in checks in queue.h in debug builds
|
||||
#define INVARIANTS
|
||||
#endif
|
||||
#include "sys/queue.h"
|
||||
|
||||
#define EVENT_ID_DELETE_TIMER 0xF0DE1E1E
|
||||
|
||||
typedef enum {
|
||||
FL_ISR_DISPATCH_METHOD = (1 << 0), //!< 0=Callback is called from timer task, 1=Callback is called from timer ISR
|
||||
FL_SKIP_UNHANDLED_EVENTS = (1 << 1), //!< 0=NOT skip unhandled events for periodic timers, 1=Skip unhandled events for periodic timers
|
||||
FL_CALLBACK_IS_RUNNING = (1 << 2), //!< 0=Callback is NOT running, 1=Callback is running
|
||||
} flags_t;
|
||||
|
||||
struct esp_timer {
|
||||
uint64_t alarm;
|
||||
uint64_t period: 56;
|
||||
volatile flags_t flags: 8;
|
||||
union {
|
||||
esp_timer_cb_t callback;
|
||||
uint32_t event_id;
|
||||
};
|
||||
void* arg;
|
||||
#if WITH_PROFILING
|
||||
const char* name;
|
||||
size_t times_triggered;
|
||||
size_t times_armed;
|
||||
size_t times_skipped;
|
||||
uint64_t total_callback_run_time;
|
||||
#endif // WITH_PROFILING
|
||||
LIST_ENTRY(esp_timer) list_entry;
|
||||
};
|
||||
|
||||
static inline bool is_initialized(void);
|
||||
static esp_err_t timer_insert(esp_timer_handle_t timer, bool without_update_alarm);
|
||||
static void timer_remove(esp_timer_handle_t timer);
|
||||
static bool timer_armed(esp_timer_handle_t timer);
|
||||
static void timer_list_lock(esp_timer_dispatch_t timer_type);
|
||||
static void timer_list_unlock(esp_timer_dispatch_t timer_type);
|
||||
static esp_err_t timer_restart(esp_timer_handle_t timer, uint64_t timeout_us, uint64_t alarm_us);
|
||||
|
||||
#if WITH_PROFILING
|
||||
static void timer_insert_inactive(esp_timer_handle_t timer);
|
||||
static void timer_remove_inactive(esp_timer_handle_t timer);
|
||||
#endif // WITH_PROFILING
|
||||
|
||||
ESP_LOG_ATTR_TAG(TAG, "esp_timer");
|
||||
|
||||
// lists of currently armed timers for two dispatch methods: ISR and TASK
|
||||
static LIST_HEAD(esp_timer_list, esp_timer) s_timers[ESP_TIMER_MAX] = {
|
||||
[0 ...(ESP_TIMER_MAX - 1)] = LIST_HEAD_INITIALIZER(s_timers)
|
||||
};
|
||||
#if WITH_PROFILING
|
||||
// lists of unarmed timers for two dispatch methods: ISR and TASK,
|
||||
// used only to be able to dump statistics about all the timers
|
||||
static LIST_HEAD(esp_inactive_timer_list, esp_timer) s_inactive_timers[ESP_TIMER_MAX] = {
|
||||
[0 ...(ESP_TIMER_MAX - 1)] = LIST_HEAD_INITIALIZER(s_timers)
|
||||
};
|
||||
#endif
|
||||
// task used to dispatch timer callbacks
|
||||
static TaskHandle_t s_timer_task;
|
||||
|
||||
// lock protecting s_timers, s_inactive_timers
|
||||
static portMUX_TYPE s_timer_lock[ESP_TIMER_MAX] = {
|
||||
[0 ...(ESP_TIMER_MAX - 1)] = portMUX_INITIALIZER_UNLOCKED
|
||||
};
|
||||
|
||||
#ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
// For ISR dispatch method, a callback function of the timer may require a context switch
|
||||
static volatile BaseType_t s_isr_dispatch_need_yield = pdFALSE;
|
||||
#endif // CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
|
||||
esp_err_t esp_timer_create(const esp_timer_create_args_t* args,
|
||||
esp_timer_handle_t* out_handle)
|
||||
{
|
||||
if (!is_initialized()) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
if (args == NULL || args->callback == NULL || out_handle == NULL ||
|
||||
args->dispatch_method < 0 || args->dispatch_method >= ESP_TIMER_MAX) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
esp_timer_handle_t result = (esp_timer_handle_t) heap_caps_calloc(1, sizeof(*result), MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
|
||||
if (result == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
result->callback = args->callback;
|
||||
result->arg = args->arg;
|
||||
result->flags = (args->dispatch_method ? FL_ISR_DISPATCH_METHOD : 0) |
|
||||
(args->skip_unhandled_events ? FL_SKIP_UNHANDLED_EVENTS : 0);
|
||||
#if WITH_PROFILING
|
||||
result->name = args->name;
|
||||
esp_timer_dispatch_t dispatch_method = result->flags & FL_ISR_DISPATCH_METHOD;
|
||||
timer_list_lock(dispatch_method);
|
||||
timer_insert_inactive(result);
|
||||
timer_list_unlock(dispatch_method);
|
||||
#endif
|
||||
*out_handle = result;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* We have placed this function in IRAM to ensure consistency with the esp_timer API.
|
||||
* esp_timer_start_once, esp_timer_start_periodic and esp_timer_stop are in IRAM.
|
||||
* But actually in IDF esp_timer_restart is used only in one place, which requires keeping
|
||||
* in IRAM when PM_SLP_IRAM_OPT = y and ESP_TASK_WDT USE ESP_TIMER = y.
|
||||
*/
|
||||
esp_err_t ESP_TIMER_IRAM_ATTR esp_timer_restart(esp_timer_handle_t timer, uint64_t timeout_us)
|
||||
{
|
||||
return timer_restart(timer, timeout_us, 0);
|
||||
}
|
||||
|
||||
esp_err_t ESP_TIMER_IRAM_ATTR esp_timer_restart_at(esp_timer_handle_t timer, uint64_t period_us, uint64_t first_alarm_us)
|
||||
{
|
||||
const uint64_t min_overhead_us = esp_timer_impl_get_min_period_us();
|
||||
if (first_alarm_us + min_overhead_us < esp_timer_impl_get_time()) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
return timer_restart(timer, period_us, first_alarm_us);
|
||||
}
|
||||
|
||||
static esp_err_t ESP_TIMER_IRAM_ATTR timer_restart(esp_timer_handle_t timer, uint64_t timeout_us, uint64_t first_alarm_us)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
|
||||
if (timer == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (!is_initialized() || !timer_armed(timer)) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD;
|
||||
timer_list_lock(dispatch_method);
|
||||
|
||||
const int64_t now = esp_timer_impl_get_time();
|
||||
const uint64_t period = timer->period;
|
||||
|
||||
/* We need to remove the timer to the list of timers and reinsert it at
|
||||
* the right position. In fact, the timers are sorted by their alarm value
|
||||
* (earliest first) */
|
||||
timer_remove(timer);
|
||||
|
||||
/* Two cases here:
|
||||
* - if the alarm was a periodic one, i.e. `period` is not 0, the given timeout_us becomes the new period
|
||||
* - if the alarm was a one-shot one, i.e. `period` is 0, it remains non-periodic. */
|
||||
if (period != 0) {
|
||||
/* Remove function got rid of the alarm and period fields, restore them */
|
||||
const uint64_t min_period = esp_timer_impl_get_min_period_us();
|
||||
const uint64_t new_period = MAX(timeout_us, min_period);
|
||||
timer->alarm = (first_alarm_us != 0) ? first_alarm_us : now + new_period;
|
||||
timer->period = new_period;
|
||||
} else {
|
||||
/* The new one-shot alarm shall be triggered timeout_us after the current time */
|
||||
timer->alarm = (first_alarm_us != 0) ? first_alarm_us : now + timeout_us;
|
||||
timer->period = 0;
|
||||
}
|
||||
ret = timer_insert(timer, false);
|
||||
|
||||
timer_list_unlock(dispatch_method);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t ESP_TIMER_IRAM_ATTR timer_init(esp_timer_handle_t timer, uint64_t period_us, uint64_t first_alarm_us)
|
||||
{
|
||||
if (timer == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (!is_initialized()) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD;
|
||||
esp_err_t err;
|
||||
|
||||
timer_list_lock(dispatch_method);
|
||||
|
||||
/* Check if the timer is armed once the list is locked.
|
||||
* Otherwise another task may arm the timer between the checks
|
||||
* and us locking the list, resulting in us inserting the
|
||||
* timer to s_timers a second time. This will create a loop
|
||||
* in s_timers. */
|
||||
if (timer_armed(timer)) {
|
||||
err = ESP_ERR_INVALID_STATE;
|
||||
} else {
|
||||
timer->alarm = first_alarm_us;
|
||||
timer->period = period_us;
|
||||
#if WITH_PROFILING
|
||||
timer->times_armed++;
|
||||
timer->times_skipped = 0;
|
||||
#endif
|
||||
err = timer_insert(timer, false);
|
||||
}
|
||||
timer_list_unlock(dispatch_method);
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t ESP_TIMER_IRAM_ATTR esp_timer_start_once(esp_timer_handle_t timer, uint64_t timeout_us)
|
||||
{
|
||||
return timer_init(timer, 0, esp_timer_get_time() + timeout_us);
|
||||
}
|
||||
|
||||
esp_err_t ESP_TIMER_IRAM_ATTR esp_timer_start_once_at(esp_timer_handle_t timer, uint64_t alarm_us)
|
||||
{
|
||||
const uint64_t min_overhead_us = esp_timer_impl_get_min_period_us();
|
||||
if (alarm_us + min_overhead_us < esp_timer_impl_get_time()) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
return timer_init(timer, 0, alarm_us);
|
||||
}
|
||||
|
||||
esp_err_t ESP_TIMER_IRAM_ATTR esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period_us)
|
||||
{
|
||||
uint64_t min_period = esp_timer_impl_get_min_period_us();
|
||||
period_us = MAX(period_us, min_period);
|
||||
return timer_init(timer, period_us, esp_timer_get_time() + period_us);
|
||||
}
|
||||
|
||||
esp_err_t ESP_TIMER_IRAM_ATTR esp_timer_start_periodic_at(esp_timer_handle_t timer, uint64_t period_us, uint64_t first_alarm_us)
|
||||
{
|
||||
const uint64_t min_overhead_us = esp_timer_impl_get_min_period_us();
|
||||
if (first_alarm_us + min_overhead_us < esp_timer_impl_get_time()) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
period_us = MAX(period_us, min_overhead_us);
|
||||
return timer_init(timer, period_us, first_alarm_us);
|
||||
}
|
||||
|
||||
esp_err_t ESP_TIMER_IRAM_ATTR esp_timer_stop(esp_timer_handle_t timer)
|
||||
{
|
||||
if (timer == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (!is_initialized()) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD;
|
||||
esp_err_t err = ESP_OK;
|
||||
|
||||
timer_list_lock(dispatch_method);
|
||||
|
||||
/* Check if the timer is armed once the list is locked to avoid a data race */
|
||||
if (!timer_armed(timer)) {
|
||||
err = ESP_ERR_INVALID_STATE;
|
||||
} else {
|
||||
timer_remove(timer);
|
||||
}
|
||||
timer_list_unlock(dispatch_method);
|
||||
return err;
|
||||
}
|
||||
|
||||
static inline bool is_callback_running(esp_timer_handle_t timer, esp_timer_dispatch_t dispatch_method)
|
||||
{
|
||||
timer_list_lock(dispatch_method);
|
||||
bool callback_running = (timer != NULL) && (timer->flags & FL_CALLBACK_IS_RUNNING) != 0;
|
||||
timer_list_unlock(dispatch_method);
|
||||
return callback_running;
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_stop_blocking(esp_timer_handle_t timer, uint32_t timeout_ticks)
|
||||
{
|
||||
if (timer == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (!is_initialized()) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD;
|
||||
|
||||
timer_list_lock(dispatch_method);
|
||||
|
||||
bool callback_running = (timer->flags & FL_CALLBACK_IS_RUNNING) != 0;
|
||||
/* Check if the timer is armed once the list is locked to avoid a data race */
|
||||
if (timer_armed(timer)) {
|
||||
timer_remove(timer);
|
||||
}
|
||||
|
||||
timer_list_unlock(dispatch_method);
|
||||
|
||||
if (callback_running) {
|
||||
// timer_process_alarm() releases the timer list lock while executing the callback.
|
||||
// So it is possible that timer is disarmed but its callback is still running.
|
||||
// To guarantee that the callback will not run after esp_timer_stop_blocking(),
|
||||
// we need to wait for the callback to complete here.
|
||||
|
||||
// In ISR context: do not wait to avoid blocking
|
||||
if (xPortInIsrContext()) {
|
||||
return ESP_ERR_NOT_FINISHED;
|
||||
}
|
||||
|
||||
if (xTaskGetCurrentTaskHandle() == s_timer_task) {
|
||||
// Called from the esp_timer task context (i.e., the callback owner is a TASK-dispatch timer).
|
||||
// Concurrency model:
|
||||
// - TASK-dispatch callbacks are executed by a single esp_timer task and are strictly serialized.
|
||||
// Therefore, only one TASK callback can be running at any time.
|
||||
// - If CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD is enabled,
|
||||
// ISR-dispatch callbacks and TASK-dispatch callbacks may be running at the same time.
|
||||
if (dispatch_method == ESP_TIMER_TASK) {
|
||||
// Only one ESP_TIMER_TASK callback can be running at any time.
|
||||
// So we are stopping the timer from its own callback context:
|
||||
// the callback will complete naturally after this function returns.
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Stopping a running ISR-dispatch timer from a foreign callback:
|
||||
// we cannot wait for ISR context to complete, and blocking the esp_timer task would
|
||||
// stall TASK-dispatch callbacks. Report that the timer callback is still running.
|
||||
return ESP_ERR_NOT_FINISHED;
|
||||
}
|
||||
|
||||
TickType_t start_time = xTaskGetTickCount();
|
||||
while (is_callback_running(timer, dispatch_method)) {
|
||||
if (timeout_ticks != (uint32_t) portMAX_DELAY) {
|
||||
TickType_t elapsed = xTaskGetTickCount() - start_time;
|
||||
if (elapsed >= timeout_ticks) {
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
}
|
||||
vTaskDelay(1);
|
||||
}
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_delete(esp_timer_handle_t timer)
|
||||
{
|
||||
if (timer == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
int64_t alarm = esp_timer_get_time();
|
||||
esp_err_t err;
|
||||
timer_list_lock(ESP_TIMER_TASK);
|
||||
|
||||
/* Check if the timer is armed once the list is locked to avoid a data race */
|
||||
if (timer_armed(timer)) {
|
||||
err = ESP_ERR_INVALID_STATE;
|
||||
} else {
|
||||
// A case for the timer with ESP_TIMER_ISR:
|
||||
// This ISR timer was removed from the ISR list in esp_timer_stop() or in timer_process_alarm() -> LIST_REMOVE(it, list_entry)
|
||||
// and here this timer will be added to another the TASK list, see below.
|
||||
// We do this because we want to free memory of the timer in a task context instead of an isr context.
|
||||
timer->flags &= ~FL_ISR_DISPATCH_METHOD;
|
||||
timer->event_id = EVENT_ID_DELETE_TIMER;
|
||||
timer->alarm = alarm;
|
||||
timer->period = 0;
|
||||
err = timer_insert(timer, false);
|
||||
}
|
||||
timer_list_unlock(ESP_TIMER_TASK);
|
||||
return err;
|
||||
}
|
||||
|
||||
static ESP_TIMER_IRAM_ATTR esp_err_t timer_insert(esp_timer_handle_t timer, bool without_update_alarm)
|
||||
{
|
||||
#if WITH_PROFILING
|
||||
timer_remove_inactive(timer);
|
||||
#endif
|
||||
esp_timer_handle_t it, last = NULL;
|
||||
esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD;
|
||||
if (LIST_FIRST(&s_timers[dispatch_method]) == NULL) {
|
||||
LIST_INSERT_HEAD(&s_timers[dispatch_method], timer, list_entry);
|
||||
} else {
|
||||
LIST_FOREACH(it, &s_timers[dispatch_method], list_entry) {
|
||||
if (timer->alarm < it->alarm) {
|
||||
LIST_INSERT_BEFORE(it, timer, list_entry);
|
||||
break;
|
||||
}
|
||||
last = it;
|
||||
}
|
||||
if (it == NULL) {
|
||||
assert(last);
|
||||
LIST_INSERT_AFTER(last, timer, list_entry);
|
||||
}
|
||||
}
|
||||
if (without_update_alarm == false && timer == LIST_FIRST(&s_timers[dispatch_method])) {
|
||||
esp_timer_impl_set_alarm_id(timer->alarm, dispatch_method);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// It should be always called with the timer list locked
|
||||
static ESP_TIMER_IRAM_ATTR void timer_remove(esp_timer_handle_t timer)
|
||||
{
|
||||
esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD;
|
||||
esp_timer_handle_t first_timer = LIST_FIRST(&s_timers[dispatch_method]);
|
||||
LIST_REMOVE(timer, list_entry);
|
||||
timer->alarm = 0;
|
||||
timer->period = 0;
|
||||
if (timer == first_timer) { // if this timer was the first in the list.
|
||||
uint64_t next_timestamp = UINT64_MAX;
|
||||
first_timer = LIST_FIRST(&s_timers[dispatch_method]);
|
||||
if (first_timer) { // if after removing the timer from the list, this list is not empty.
|
||||
next_timestamp = first_timer->alarm;
|
||||
}
|
||||
esp_timer_impl_set_alarm_id(next_timestamp, dispatch_method);
|
||||
}
|
||||
#if WITH_PROFILING
|
||||
timer_insert_inactive(timer);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if WITH_PROFILING
|
||||
|
||||
static ESP_TIMER_IRAM_ATTR void timer_insert_inactive(esp_timer_handle_t timer)
|
||||
{
|
||||
/* May be locked or not, depending on where this is called from.
|
||||
* Lock recursively.
|
||||
*/
|
||||
esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD;
|
||||
esp_timer_handle_t head = LIST_FIRST(&s_inactive_timers[dispatch_method]);
|
||||
if (head == NULL) {
|
||||
LIST_INSERT_HEAD(&s_inactive_timers[dispatch_method], timer, list_entry);
|
||||
} else {
|
||||
/* Insert as head element as this is the fastest thing to do.
|
||||
* Removal is O(1) anyway.
|
||||
*/
|
||||
LIST_INSERT_BEFORE(head, timer, list_entry);
|
||||
}
|
||||
}
|
||||
|
||||
static ESP_TIMER_IRAM_ATTR void timer_remove_inactive(esp_timer_handle_t timer)
|
||||
{
|
||||
LIST_REMOVE(timer, list_entry);
|
||||
}
|
||||
|
||||
#endif // WITH_PROFILING
|
||||
|
||||
static ESP_TIMER_IRAM_ATTR bool timer_armed(esp_timer_handle_t timer)
|
||||
{
|
||||
return timer->alarm > 0;
|
||||
}
|
||||
|
||||
static ESP_TIMER_IRAM_ATTR void timer_list_lock(esp_timer_dispatch_t timer_type)
|
||||
{
|
||||
portENTER_CRITICAL_SAFE(&s_timer_lock[timer_type]);
|
||||
}
|
||||
|
||||
static ESP_TIMER_IRAM_ATTR void timer_list_unlock(esp_timer_dispatch_t timer_type)
|
||||
{
|
||||
portEXIT_CRITICAL_SAFE(&s_timer_lock[timer_type]);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
static ESP_TIMER_IRAM_ATTR bool timer_process_alarm(esp_timer_dispatch_t dispatch_method)
|
||||
#else
|
||||
static bool timer_process_alarm(esp_timer_dispatch_t dispatch_method)
|
||||
#endif
|
||||
{
|
||||
timer_list_lock(dispatch_method);
|
||||
bool processed = false;
|
||||
esp_timer_handle_t it;
|
||||
while (1) {
|
||||
it = LIST_FIRST(&s_timers[dispatch_method]);
|
||||
int64_t now = esp_timer_impl_get_time();
|
||||
ESP_COMPILER_DIAGNOSTIC_PUSH_IGNORE("-Wanalyzer-use-after-free") // False-positive detection. TODO GCC-366
|
||||
if (it == NULL || it->alarm > now) {
|
||||
break;
|
||||
}
|
||||
ESP_COMPILER_DIAGNOSTIC_POP("-Wanalyzer-use-after-free")
|
||||
processed = true;
|
||||
LIST_REMOVE(it, list_entry);
|
||||
if (it->event_id == EVENT_ID_DELETE_TIMER) {
|
||||
// It is handled only by ESP_TIMER_TASK (see esp_timer_delete()).
|
||||
// All the ESP_TIMER_ISR timers which should be deleted are moved by esp_timer_delete() to the ESP_TIMER_TASK list.
|
||||
// We want to free memory of the timer in a task context instead of an isr context.
|
||||
free(it);
|
||||
it = NULL;
|
||||
} else {
|
||||
it->flags |= FL_CALLBACK_IS_RUNNING;
|
||||
if (it->period > 0) {
|
||||
int skipped = (now - it->alarm) / it->period;
|
||||
if ((it->flags & FL_SKIP_UNHANDLED_EVENTS) && (skipped > 1)) {
|
||||
it->alarm = now + it->period;
|
||||
#if WITH_PROFILING
|
||||
it->times_skipped += skipped;
|
||||
#endif
|
||||
} else {
|
||||
it->alarm += it->period;
|
||||
}
|
||||
timer_insert(it, true);
|
||||
} else {
|
||||
it->alarm = 0;
|
||||
#if WITH_PROFILING
|
||||
timer_insert_inactive(it);
|
||||
#endif
|
||||
}
|
||||
#if WITH_PROFILING
|
||||
uint64_t callback_start = now;
|
||||
#endif
|
||||
esp_timer_cb_t callback = it->callback;
|
||||
void* arg = it->arg;
|
||||
timer_list_unlock(dispatch_method);
|
||||
(*callback)(arg);
|
||||
timer_list_lock(dispatch_method);
|
||||
it->flags &= ~FL_CALLBACK_IS_RUNNING;
|
||||
#if WITH_PROFILING
|
||||
it->times_triggered++;
|
||||
it->total_callback_run_time += esp_timer_impl_get_time() - callback_start;
|
||||
#endif
|
||||
}
|
||||
} // while(1)
|
||||
if (it) {
|
||||
if (dispatch_method == ESP_TIMER_TASK || (dispatch_method != ESP_TIMER_TASK && processed == true)) {
|
||||
esp_timer_impl_set_alarm_id(it->alarm, dispatch_method);
|
||||
}
|
||||
} else {
|
||||
if (processed) {
|
||||
esp_timer_impl_set_alarm_id(UINT64_MAX, dispatch_method);
|
||||
}
|
||||
}
|
||||
timer_list_unlock(dispatch_method);
|
||||
return processed;
|
||||
}
|
||||
|
||||
static void timer_task(void* arg)
|
||||
{
|
||||
while (true) {
|
||||
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
||||
// all deferred events are processed at a time
|
||||
#if CONFIG_ESP_TIMER_IMPL_LINUX && CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
esp_timer_impl_try_to_set_next_alarm();
|
||||
if (timer_process_alarm(ESP_TIMER_ISR)) {
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
timer_process_alarm(ESP_TIMER_TASK);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
ESP_TIMER_IRAM_ATTR void esp_timer_isr_dispatch_need_yield(void)
|
||||
{
|
||||
#ifndef CONFIG_ESP_TIMER_IMPL_LINUX
|
||||
assert(xPortInIsrContext());
|
||||
#endif
|
||||
s_isr_dispatch_need_yield = pdTRUE;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_ESP_TIMER_IMPL_LINUX
|
||||
static void ESP_TIMER_IRAM_ATTR timer_alarm_handler(void* arg)
|
||||
{
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
bool isr_timers_processed = false;
|
||||
|
||||
#ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
esp_timer_impl_try_to_set_next_alarm();
|
||||
// process timers with ISR dispatch method
|
||||
isr_timers_processed = timer_process_alarm(ESP_TIMER_ISR);
|
||||
xHigherPriorityTaskWoken = s_isr_dispatch_need_yield;
|
||||
s_isr_dispatch_need_yield = pdFALSE;
|
||||
#endif
|
||||
|
||||
if (isr_timers_processed == false) {
|
||||
vTaskNotifyGiveFromISR(s_timer_task, &xHigherPriorityTaskWoken);
|
||||
}
|
||||
if (xHigherPriorityTaskWoken == pdTRUE) {
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
}
|
||||
#endif // !CONFIG_ESP_TIMER_IMPL_LINUX
|
||||
|
||||
static ESP_TIMER_IRAM_ATTR inline bool is_initialized(void)
|
||||
{
|
||||
return s_timer_task != NULL;
|
||||
}
|
||||
|
||||
TaskHandle_t esp_timer_impl_get_timer_task_handle(void)
|
||||
{
|
||||
return s_timer_task;
|
||||
}
|
||||
|
||||
static esp_err_t init_timer_task(void)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
if (is_initialized()) {
|
||||
ESP_EARLY_LOGE(TAG, "Task is already initialized");
|
||||
err = ESP_ERR_INVALID_STATE;
|
||||
} else {
|
||||
int ret = xTaskCreatePinnedToCore(
|
||||
&timer_task, "esp_timer",
|
||||
ESP_TASK_TIMER_STACK, NULL, ESP_TASK_TIMER_PRIO,
|
||||
&s_timer_task, CONFIG_ESP_TIMER_TASK_AFFINITY);
|
||||
if (ret != pdPASS) {
|
||||
ESP_EARLY_LOGE(TAG, "Not enough memory to create timer task");
|
||||
err = ESP_ERR_NO_MEM;
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
static void deinit_timer_task(void)
|
||||
{
|
||||
if (s_timer_task) {
|
||||
vTaskDelete(s_timer_task);
|
||||
s_timer_task = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_init(void)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
#ifndef CONFIG_ESP_TIMER_ISR_AFFINITY_NO_AFFINITY
|
||||
err = init_timer_task();
|
||||
#else
|
||||
/* This function will be run on all cores if CONFIG_ESP_TIMER_ISR_AFFINITY_NO_AFFINITY is enabled,
|
||||
* We do it that way because we need to allocate the timer ISR on MULTIPLE cores.
|
||||
* timer task will be created by CPU0.
|
||||
*/
|
||||
if (xPortGetCoreID() == 0) {
|
||||
err = init_timer_task();
|
||||
}
|
||||
#endif // CONFIG_ESP_TIMER_ISR_AFFINITY_NO_AFFINITY
|
||||
if (err == ESP_OK) {
|
||||
#ifndef CONFIG_ESP_TIMER_IMPL_LINUX
|
||||
err = esp_timer_impl_init(&timer_alarm_handler);
|
||||
#else
|
||||
err = esp_timer_impl_init(NULL);
|
||||
#endif
|
||||
if (err != ESP_OK) {
|
||||
ESP_EARLY_LOGE(TAG, "ISR init failed");
|
||||
deinit_timer_task();
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
#if CONFIG_ESP_TIMER_ISR_AFFINITY_CPU0
|
||||
#define ESP_TIMER_INIT_MASK BIT(0)
|
||||
#elif CONFIG_ESP_TIMER_ISR_AFFINITY_CPU1
|
||||
#define ESP_TIMER_INIT_MASK BIT(1)
|
||||
#elif CONFIG_ESP_TIMER_ISR_AFFINITY_NO_AFFINITY
|
||||
#define ESP_TIMER_INIT_MASK ESP_SYSTEM_INIT_ALL_CORES
|
||||
#endif // CONFIG_ESP_TIMER_ISR_AFFINITY_*
|
||||
|
||||
/*
|
||||
* This function initializes a task and ISR that esp_timer uses.
|
||||
*
|
||||
* We keep the esp_timer initialization function here to allow the linker
|
||||
* to automatically include esp_timer_init_os if other components call esp_timer APIs.
|
||||
* If no other code calls esp_timer APIs, then esp_timer_init_os will be skipped.
|
||||
*/
|
||||
ESP_SYSTEM_INIT_FN(esp_timer_init_os, SECONDARY, ESP_TIMER_INIT_MASK, 100)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
if (is_initialized()) {
|
||||
err = ESP_OK;
|
||||
} else {
|
||||
err = esp_timer_init();
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_deinit(void)
|
||||
{
|
||||
if (!is_initialized()) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
/* Check if there are any active timers */
|
||||
for (esp_timer_dispatch_t dispatch_method = ESP_TIMER_TASK; dispatch_method < ESP_TIMER_MAX; ++dispatch_method) {
|
||||
if (!LIST_EMPTY(&s_timers[dispatch_method])) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
}
|
||||
|
||||
/* We can only check if there are any timers which are not deleted if
|
||||
* profiling is enabled.
|
||||
*/
|
||||
#if WITH_PROFILING
|
||||
for (esp_timer_dispatch_t dispatch_method = ESP_TIMER_TASK; dispatch_method < ESP_TIMER_MAX; ++dispatch_method) {
|
||||
if (!LIST_EMPTY(&s_inactive_timers[dispatch_method])) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
esp_timer_impl_deinit();
|
||||
deinit_timer_task();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void print_timer_info(esp_timer_handle_t t, char** dst, size_t* dst_size)
|
||||
{
|
||||
#if WITH_PROFILING
|
||||
size_t cb;
|
||||
// name is optional, might be missed.
|
||||
if (t->name) {
|
||||
cb = snprintf(*dst, *dst_size, "%-20.20s ", t->name);
|
||||
} else {
|
||||
cb = snprintf(*dst, *dst_size, "timer@%-10p ", t);
|
||||
}
|
||||
|
||||
cb += snprintf(*dst + cb, *dst_size - cb, "%-10" PRIu64" %-12" PRIu64" %-12zu %-12zu %-12zu %-12" PRIu64"\n",
|
||||
(uint64_t)t->period, t->alarm, t->times_armed,
|
||||
t->times_triggered, t->times_skipped, t->total_callback_run_time);
|
||||
/* keep this in sync with the format string, used in esp_timer_dump */
|
||||
#define TIMER_INFO_LINE_LEN 103
|
||||
#else
|
||||
size_t cb = snprintf(*dst, *dst_size, "timer@%-14p %-10" PRIu64" %-12" PRIu64"\n", t, (uint64_t)t->period, t->alarm);
|
||||
#define TIMER_INFO_LINE_LEN 47
|
||||
#endif
|
||||
*dst += cb;
|
||||
*dst_size -= cb;
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_dump(FILE* stream)
|
||||
{
|
||||
/* Since timer lock is a critical section, we don't want to print directly
|
||||
* to stdout, since that may cause a deadlock if stdout is interrupt-driven
|
||||
* (via the UART driver). Allocate sufficiently large chunk of memory first,
|
||||
* print to it, then dump this memory to stdout.
|
||||
*/
|
||||
|
||||
esp_timer_handle_t it;
|
||||
|
||||
/* First count the number of timers */
|
||||
size_t timer_count = 0;
|
||||
for (esp_timer_dispatch_t dispatch_method = ESP_TIMER_TASK; dispatch_method < ESP_TIMER_MAX; ++dispatch_method) {
|
||||
timer_list_lock(dispatch_method);
|
||||
LIST_FOREACH(it, &s_timers[dispatch_method], list_entry) {
|
||||
++timer_count;
|
||||
}
|
||||
#if WITH_PROFILING
|
||||
LIST_FOREACH(it, &s_inactive_timers[dispatch_method], list_entry) {
|
||||
++timer_count;
|
||||
}
|
||||
#endif
|
||||
timer_list_unlock(dispatch_method);
|
||||
}
|
||||
|
||||
/* Allocate the memory for this number of timers. Since we have unlocked,
|
||||
* we may find that there are more timers. There's no bulletproof solution
|
||||
* for this (can't allocate from a critical section), but we allocate
|
||||
* slightly more and the output will be truncated if that is not enough.
|
||||
*/
|
||||
size_t buf_size = TIMER_INFO_LINE_LEN * (timer_count + 3);
|
||||
char* print_buf = calloc(1, buf_size + 1);
|
||||
if (print_buf == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
/* Print to the buffer */
|
||||
char* pos = print_buf;
|
||||
for (esp_timer_dispatch_t dispatch_method = ESP_TIMER_TASK; dispatch_method < ESP_TIMER_MAX; ++dispatch_method) {
|
||||
timer_list_lock(dispatch_method);
|
||||
LIST_FOREACH(it, &s_timers[dispatch_method], list_entry) {
|
||||
print_timer_info(it, &pos, &buf_size);
|
||||
}
|
||||
#if WITH_PROFILING
|
||||
LIST_FOREACH(it, &s_inactive_timers[dispatch_method], list_entry) {
|
||||
print_timer_info(it, &pos, &buf_size);
|
||||
}
|
||||
#endif
|
||||
timer_list_unlock(dispatch_method);
|
||||
}
|
||||
|
||||
if (stream != NULL) {
|
||||
fprintf(stream, "Timer stats:\n");
|
||||
#if WITH_PROFILING
|
||||
fprintf(stream, "%-20s %-10s %-12s %-12s %-12s %-12s %-12s\n",
|
||||
"Name", "Period", "Alarm", "Times_armed", "Times_trigg", "Times_skip", "Cb_exec_time");
|
||||
#else
|
||||
fprintf(stream, "%-20s %-10s %-12s\n", "Name", "Period", "Alarm");
|
||||
#endif
|
||||
|
||||
/* Print the buffer */
|
||||
fputs(print_buf, stream);
|
||||
}
|
||||
|
||||
free(print_buf);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
int64_t ESP_TIMER_IRAM_ATTR esp_timer_get_next_alarm(void)
|
||||
{
|
||||
int64_t next_alarm = INT64_MAX;
|
||||
for (esp_timer_dispatch_t dispatch_method = ESP_TIMER_TASK; dispatch_method < ESP_TIMER_MAX; ++dispatch_method) {
|
||||
timer_list_lock(dispatch_method);
|
||||
esp_timer_handle_t it = LIST_FIRST(&s_timers[dispatch_method]);
|
||||
if (it) {
|
||||
if (next_alarm > it->alarm) {
|
||||
next_alarm = it->alarm;
|
||||
}
|
||||
}
|
||||
timer_list_unlock(dispatch_method);
|
||||
}
|
||||
return next_alarm;
|
||||
}
|
||||
|
||||
int64_t ESP_TIMER_IRAM_ATTR esp_timer_get_next_alarm_for_wake_up(void)
|
||||
{
|
||||
int64_t next_alarm = INT64_MAX;
|
||||
for (esp_timer_dispatch_t dispatch_method = ESP_TIMER_TASK; dispatch_method < ESP_TIMER_MAX; ++dispatch_method) {
|
||||
timer_list_lock(dispatch_method);
|
||||
esp_timer_handle_t it = NULL;
|
||||
LIST_FOREACH(it, &s_timers[dispatch_method], list_entry) {
|
||||
// timers with the SKIP_UNHANDLED_EVENTS flag do not want to wake up CPU from a sleep mode.
|
||||
if ((it->flags & FL_SKIP_UNHANDLED_EVENTS) == 0) {
|
||||
if (next_alarm > it->alarm) {
|
||||
next_alarm = it->alarm;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
timer_list_unlock(dispatch_method);
|
||||
}
|
||||
return next_alarm;
|
||||
}
|
||||
|
||||
esp_err_t ESP_TIMER_IRAM_ATTR esp_timer_get_period(esp_timer_handle_t timer, uint64_t *period)
|
||||
{
|
||||
if (timer == NULL || period == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD;
|
||||
|
||||
timer_list_lock(dispatch_method);
|
||||
*period = timer->period;
|
||||
timer_list_unlock(dispatch_method);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t ESP_TIMER_IRAM_ATTR esp_timer_get_expiry_time(esp_timer_handle_t timer, uint64_t *expiry)
|
||||
{
|
||||
if (timer == NULL || expiry == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (timer->period > 0) {
|
||||
/* Return error for periodic timers */
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD;
|
||||
|
||||
timer_list_lock(dispatch_method);
|
||||
*expiry = timer->alarm;
|
||||
timer_list_unlock(dispatch_method);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
bool ESP_TIMER_IRAM_ATTR esp_timer_is_active(esp_timer_handle_t timer)
|
||||
{
|
||||
if (timer == NULL) {
|
||||
return false;
|
||||
}
|
||||
esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD;
|
||||
|
||||
timer_list_lock(dispatch_method);
|
||||
// Timer is active if it is armed or its callback is currently running
|
||||
// After esp_timer_stop() timer is disarmed, but its callback may still be running
|
||||
bool active = (timer_armed(timer) && timer->event_id != EVENT_ID_DELETE_TIMER)
|
||||
|| ((timer->flags & FL_CALLBACK_IS_RUNNING) != 0);
|
||||
timer_list_unlock(dispatch_method);
|
||||
|
||||
return active;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_timer.h"
|
||||
#include "soc/soc_etm_source.h"
|
||||
#include "esp_private/systimer.h"
|
||||
#include "esp_private/etm_interface.h"
|
||||
|
||||
#define ETM_MEM_ALLOC_CAPS MALLOC_CAP_DEFAULT
|
||||
|
||||
ESP_LOG_ATTR_TAG(TAG, "esptimer-etm");
|
||||
|
||||
static esp_err_t esp_timer_etm_event_del(esp_etm_event_t *event)
|
||||
{
|
||||
free(event);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_new_etm_alarm_event(esp_etm_event_handle_t *out_event)
|
||||
{
|
||||
esp_etm_event_t *event = NULL;
|
||||
esp_err_t ret = ESP_OK;
|
||||
ESP_GOTO_ON_FALSE(out_event, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
|
||||
event = heap_caps_calloc(1, sizeof(esp_etm_event_t), ETM_MEM_ALLOC_CAPS);
|
||||
ESP_GOTO_ON_FALSE(event, ESP_ERR_NO_MEM, err, TAG, "no memory for ETM event");
|
||||
|
||||
// fill the ETM event object
|
||||
uint32_t event_id = SYSTIMER_EVT_CNT_CMP0 + SYSTIMER_ALARM_ESPTIMER;
|
||||
event->event_id = event_id;
|
||||
event->trig_periph = ETM_TRIG_PERIPH_SYSTIMER;
|
||||
event->del = esp_timer_etm_event_del;
|
||||
*out_event = event;
|
||||
return ESP_OK;
|
||||
|
||||
err:
|
||||
if (event) {
|
||||
esp_timer_etm_event_del(event);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdatomic.h>
|
||||
#include "sys/param.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_timer_impl.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include <dispatch/dispatch.h>
|
||||
|
||||
static const char *TAG = "esp_timer_impl";
|
||||
|
||||
/* Alarm values to generate interrupt on match */
|
||||
extern uint64_t timestamp_id[2];
|
||||
|
||||
/* GCD queue and timer source used as "hardware timer" on macOS */
|
||||
static dispatch_queue_t s_queue = NULL;
|
||||
static dispatch_source_t s_timer_source = NULL;
|
||||
|
||||
/* Flag to track if timer is armed */
|
||||
static volatile bool s_timer_armed = false;
|
||||
static _Atomic int64_t s_time_offset_us;
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Time base */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
uint64_t esp_timer_impl_get_counter_reg(void)
|
||||
{
|
||||
return (uint64_t) esp_timer_impl_get_time();
|
||||
}
|
||||
|
||||
static int64_t get_monotonic_time_us(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (int64_t)ts.tv_sec * 1000000LL + ts.tv_nsec / 1000LL;
|
||||
}
|
||||
|
||||
int64_t esp_timer_impl_get_time(void)
|
||||
{
|
||||
return get_monotonic_time_us() + atomic_load_explicit(&s_time_offset_us, memory_order_relaxed);
|
||||
}
|
||||
|
||||
int64_t esp_timer_get_time(void)
|
||||
{
|
||||
return esp_timer_impl_get_time();
|
||||
}
|
||||
|
||||
static void timer_alarm_dispatch_handler(void *ctx)
|
||||
{
|
||||
(void) ctx;
|
||||
TaskHandle_t timer_task = esp_timer_impl_get_timer_task_handle();
|
||||
if (timer_task != NULL) {
|
||||
xTaskNotifyGive(timer_task);
|
||||
}
|
||||
}
|
||||
|
||||
static void setup_alarm(uint64_t alarm_us)
|
||||
{
|
||||
int64_t now = esp_timer_impl_get_time();
|
||||
int64_t delta_ns = (alarm_us > now) ? (alarm_us - now) * 1000LL : 0;
|
||||
|
||||
if (delta_ns < 50000LL) {
|
||||
delta_ns = 50000LL; /* Minimum 50us delay to avoid busy loop */
|
||||
}
|
||||
|
||||
dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, delta_ns);
|
||||
dispatch_source_set_timer(s_timer_source, when, 0, 0);
|
||||
|
||||
s_timer_armed = true;
|
||||
}
|
||||
|
||||
void esp_timer_impl_set_alarm_id(uint64_t timestamp_us, unsigned alarm_id)
|
||||
{
|
||||
esp_timer_impl_lock();
|
||||
timestamp_id[alarm_id] = timestamp_us;
|
||||
uint64_t min_alarm_us = MIN(timestamp_id[0], timestamp_id[1]);
|
||||
|
||||
if (min_alarm_us != UINT64_MAX) {
|
||||
setup_alarm(min_alarm_us);
|
||||
} else if (s_timer_armed) {
|
||||
dispatch_source_set_timer(s_timer_source, DISPATCH_TIME_FOREVER, 0, 0);
|
||||
s_timer_armed = false;
|
||||
}
|
||||
|
||||
esp_timer_impl_unlock();
|
||||
}
|
||||
|
||||
void esp_timer_impl_set(uint64_t new_us)
|
||||
{
|
||||
esp_timer_impl_lock();
|
||||
atomic_store_explicit(&s_time_offset_us, (int64_t)new_us - get_monotonic_time_us(), memory_order_relaxed);
|
||||
uint64_t min_alarm_us = MIN(timestamp_id[0], timestamp_id[1]);
|
||||
if (min_alarm_us != UINT64_MAX) {
|
||||
setup_alarm(min_alarm_us);
|
||||
}
|
||||
esp_timer_impl_unlock();
|
||||
}
|
||||
|
||||
void esp_timer_impl_advance(int64_t time_diff_us)
|
||||
{
|
||||
esp_timer_impl_lock();
|
||||
atomic_fetch_add_explicit(&s_time_offset_us, time_diff_us, memory_order_relaxed);
|
||||
uint64_t min_alarm_us = MIN(timestamp_id[0], timestamp_id[1]);
|
||||
if (min_alarm_us != UINT64_MAX) {
|
||||
setup_alarm(min_alarm_us);
|
||||
}
|
||||
esp_timer_impl_unlock();
|
||||
}
|
||||
|
||||
void esp_timer_private_set(uint64_t new_us)
|
||||
{
|
||||
esp_timer_impl_set(new_us);
|
||||
}
|
||||
|
||||
void esp_timer_private_advance(int64_t time_diff_us)
|
||||
{
|
||||
esp_timer_impl_advance(time_diff_us);
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_impl_early_init(void)
|
||||
{
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler)
|
||||
{
|
||||
(void) alarm_handler;
|
||||
|
||||
timestamp_id[0] = UINT64_MAX;
|
||||
timestamp_id[1] = UINT64_MAX;
|
||||
atomic_store_explicit(&s_time_offset_us, 0, memory_order_relaxed);
|
||||
|
||||
s_queue = dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0);
|
||||
if (s_queue == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to get GCD queue");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
s_timer_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, s_queue);
|
||||
if (s_timer_source == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to create GCD timer source");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
dispatch_set_context(s_timer_source, NULL);
|
||||
dispatch_source_set_event_handler_f(s_timer_source, timer_alarm_dispatch_handler);
|
||||
dispatch_resume(s_timer_source);
|
||||
|
||||
ESP_LOGI(TAG, "esp_timer initialized successfully");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void esp_timer_impl_deinit(void)
|
||||
{
|
||||
if (s_timer_source != NULL) {
|
||||
dispatch_source_cancel(s_timer_source);
|
||||
dispatch_release(s_timer_source);
|
||||
s_timer_source = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t esp_timer_impl_get_alarm_reg(void)
|
||||
{
|
||||
esp_timer_impl_lock();
|
||||
uint64_t min_alarm_us = MIN(timestamp_id[0], timestamp_id[1]);
|
||||
esp_timer_impl_unlock();
|
||||
return min_alarm_us;
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_timer_impl.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_task.h"
|
||||
#include "esp_attr.h"
|
||||
|
||||
/* Spinlock used to protect access to the hardware registers. */
|
||||
portMUX_TYPE s_time_update_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
/* Alarm values to generate interrupt on match
|
||||
* [0] - for ESP_TIMER_TASK alarms,
|
||||
* [1] - for ESP_TIMER_ISR alarms.
|
||||
*/
|
||||
uint64_t timestamp_id[2] = { UINT64_MAX, UINT64_MAX };
|
||||
|
||||
void esp_timer_impl_lock(void)
|
||||
{
|
||||
portENTER_CRITICAL(&s_time_update_lock);
|
||||
}
|
||||
|
||||
void esp_timer_impl_unlock(void)
|
||||
{
|
||||
portEXIT_CRITICAL(&s_time_update_lock);
|
||||
}
|
||||
|
||||
#ifndef CONFIG_IDF_TARGET_LINUX
|
||||
void esp_timer_private_lock(void) __attribute__((alias("esp_timer_impl_lock")));
|
||||
void esp_timer_private_unlock(void) __attribute__((alias("esp_timer_impl_unlock")));
|
||||
#else // CONFIG_IDF_TARGET_LINUX
|
||||
// Avoid using __attribute(alias) here since linux target builds on MacOS fail to compile.
|
||||
void esp_timer_private_lock(void)
|
||||
{
|
||||
esp_timer_impl_lock();
|
||||
}
|
||||
|
||||
void esp_timer_private_unlock(void)
|
||||
{
|
||||
esp_timer_impl_unlock();
|
||||
}
|
||||
#endif // CONFIG_IDF_TARGET_LINUX
|
||||
|
||||
void ESP_TIMER_IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp)
|
||||
{
|
||||
esp_timer_impl_set_alarm_id(timestamp, 0);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
void ESP_TIMER_IRAM_ATTR esp_timer_impl_try_to_set_next_alarm(void)
|
||||
{
|
||||
portENTER_CRITICAL_ISR(&s_time_update_lock);
|
||||
unsigned now_alarm_idx; // ISR is called due to this current alarm
|
||||
unsigned next_alarm_idx; // The following alarm after now_alarm_idx
|
||||
if (timestamp_id[0] < timestamp_id[1]) {
|
||||
now_alarm_idx = 0;
|
||||
next_alarm_idx = 1;
|
||||
} else {
|
||||
now_alarm_idx = 1;
|
||||
next_alarm_idx = 0;
|
||||
}
|
||||
|
||||
if (timestamp_id[next_alarm_idx] != UINT64_MAX) {
|
||||
// The following alarm is valid and can be used.
|
||||
// Remove the current alarm from consideration.
|
||||
esp_timer_impl_set_alarm_id(UINT64_MAX, now_alarm_idx);
|
||||
} else {
|
||||
// There is no the following alarm.
|
||||
// Remove the current alarm from consideration as well.
|
||||
timestamp_id[now_alarm_idx] = UINT64_MAX;
|
||||
}
|
||||
portEXIT_CRITICAL_ISR(&s_time_update_lock);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* FIXME: This value is safe for 80MHz APB frequency, should be modified to depend on clock frequency. */
|
||||
uint64_t ESP_TIMER_IRAM_ATTR esp_timer_impl_get_min_period_us(void)
|
||||
{
|
||||
return 50;
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include "sys/param.h"
|
||||
#include "esp_timer_impl.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_task.h"
|
||||
#include "esp_attr.h"
|
||||
#include "esp_intr_alloc.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_private/esp_clk.h"
|
||||
#include "esp_private/periph_ctrl.h"
|
||||
#include "esp_private/systimer.h"
|
||||
#include "soc/soc.h"
|
||||
#include "soc/timer_group_reg.h"
|
||||
#include "soc/rtc.h"
|
||||
#include "hal/lact_ll.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
|
||||
/**
|
||||
* @file esp_timer_lac.c
|
||||
* @brief Implementation of chip-specific part of esp_timer
|
||||
*
|
||||
* This implementation uses TG0 LAC timer of the ESP32. This timer is
|
||||
* a 64-bit up-counting timer, with a programmable compare value (called 'alarm'
|
||||
* hereafter). When the timer reaches compare value, interrupt is raised.
|
||||
* The timer can be configured to produce an edge or a level interrupt.
|
||||
*/
|
||||
|
||||
#if LACT_MODULE == 0
|
||||
#define INTR_SOURCE_LACT ETS_TG0_LACT_LEVEL_INTR_SOURCE
|
||||
#define PERIPH_LACT PERIPH_TIMG0_MODULE
|
||||
#elif LACT_MODULE == 1
|
||||
#define INTR_SOURCE_LACT ETS_TG1_LACT_LEVEL_INTR_SOURCE
|
||||
#define PERIPH_LACT PERIPH_TIMG1_MODULE
|
||||
#else
|
||||
#error "Incorrect the number of LACT module (only 0 or 1)"
|
||||
#endif
|
||||
|
||||
/* Shorter register names, used in this file */
|
||||
#define CONFIG_REG (TIMG_LACTCONFIG_REG(LACT_MODULE))
|
||||
#define RTC_STEP_REG (TIMG_LACTRTC_REG(LACT_MODULE))
|
||||
#define ALARM_LO_REG (TIMG_LACTALARMLO_REG(LACT_MODULE))
|
||||
#define ALARM_HI_REG (TIMG_LACTALARMHI_REG(LACT_MODULE))
|
||||
#define COUNT_LO_REG (TIMG_LACTLO_REG(LACT_MODULE))
|
||||
#define COUNT_HI_REG (TIMG_LACTHI_REG(LACT_MODULE))
|
||||
#define UPDATE_REG (TIMG_LACTUPDATE_REG(LACT_MODULE))
|
||||
#define LOAD_REG (TIMG_LACTLOAD_REG(LACT_MODULE))
|
||||
#define LOAD_LO_REG (TIMG_LACTLOADLO_REG(LACT_MODULE))
|
||||
#define LOAD_HI_REG (TIMG_LACTLOADHI_REG(LACT_MODULE))
|
||||
#define INT_ENA_REG (TIMG_INT_ENA_TIMERS_REG(LACT_MODULE))
|
||||
#define INT_ST_REG (TIMG_INT_ST_TIMERS_REG(LACT_MODULE))
|
||||
#define INT_CLR_REG (TIMG_INT_CLR_TIMERS_REG(LACT_MODULE))
|
||||
|
||||
/* Helper type to convert between a 64-bit value and a pair of 32-bit values without shifts and masks */
|
||||
typedef struct {
|
||||
union {
|
||||
struct {
|
||||
uint32_t lo;
|
||||
uint32_t hi;
|
||||
};
|
||||
uint64_t val;
|
||||
};
|
||||
} timer_64b_reg_t;
|
||||
|
||||
ESP_LOG_ATTR_TAG(TAG, "esp_timer_impl");
|
||||
|
||||
#define NOT_USED 0xBAD00FAD
|
||||
|
||||
/* Interrupt handle returned by the interrupt allocator */
|
||||
#ifdef CONFIG_ESP_TIMER_ISR_AFFINITY_NO_AFFINITY
|
||||
#define ISR_HANDLERS (CONFIG_FREERTOS_NUMBER_OF_CORES)
|
||||
#else
|
||||
#define ISR_HANDLERS (1)
|
||||
#endif
|
||||
static intr_handle_t s_timer_interrupt_handle[ISR_HANDLERS] = { NULL };
|
||||
|
||||
/* Function from the upper layer to be called when the interrupt happens.
|
||||
* Registered in esp_timer_impl_init.
|
||||
*/
|
||||
static intr_handler_t s_alarm_handler = NULL;
|
||||
|
||||
/* Spinlock used to protect access to the hardware registers. */
|
||||
extern portMUX_TYPE s_time_update_lock;
|
||||
|
||||
/* Alarm values to generate interrupt on match */
|
||||
extern uint64_t timestamp_id[2];
|
||||
|
||||
uint64_t ESP_TIMER_IRAM_ATTR esp_timer_impl_get_counter_reg(void)
|
||||
{
|
||||
uint32_t lo, hi;
|
||||
uint32_t lo_start = REG_READ(COUNT_LO_REG);
|
||||
uint32_t div = REG_GET_FIELD(CONFIG_REG, TIMG_LACT_DIVIDER);
|
||||
/* The peripheral doesn't have a bit to indicate that the update is done, so we poll the
|
||||
* lower 32 bit part of the counter until it changes, or a timeout expires.
|
||||
*/
|
||||
REG_WRITE(UPDATE_REG, 1);
|
||||
do {
|
||||
lo = REG_READ(COUNT_LO_REG);
|
||||
} while (lo == lo_start && div-- > 0);
|
||||
|
||||
/* Since this function is called without a critical section, verify that LO and HI
|
||||
* registers are consistent. That is, if an interrupt happens between reading LO and
|
||||
* HI registers, and esp_timer_impl_get_time is called from an ISR, then try to
|
||||
* detect this by the change in LO register value, and re-read both registers.
|
||||
*/
|
||||
do {
|
||||
lo_start = lo;
|
||||
hi = REG_READ(COUNT_HI_REG);
|
||||
lo = REG_READ(COUNT_LO_REG);
|
||||
} while (lo != lo_start);
|
||||
|
||||
timer_64b_reg_t result = {
|
||||
.lo = lo,
|
||||
.hi = hi
|
||||
};
|
||||
return result.val;
|
||||
}
|
||||
|
||||
int64_t ESP_TIMER_IRAM_ATTR esp_timer_impl_get_time(void)
|
||||
{
|
||||
return esp_timer_impl_get_counter_reg() / LACT_TICKS_PER_US;
|
||||
}
|
||||
|
||||
int64_t esp_timer_get_time(void) __attribute__((alias("esp_timer_impl_get_time")));
|
||||
|
||||
void ESP_TIMER_IRAM_ATTR esp_timer_impl_set_alarm_id(uint64_t timestamp, unsigned alarm_id)
|
||||
{
|
||||
assert(alarm_id < sizeof(timestamp_id) / sizeof(timestamp_id[0]));
|
||||
portENTER_CRITICAL_SAFE(&s_time_update_lock);
|
||||
timestamp_id[alarm_id] = timestamp;
|
||||
timestamp = MIN(timestamp_id[0], timestamp_id[1]);
|
||||
if (timestamp != UINT64_MAX) {
|
||||
int64_t offset = LACT_TICKS_PER_US * 2;
|
||||
uint64_t now_time = esp_timer_impl_get_counter_reg();
|
||||
timer_64b_reg_t alarm = { .val = MAX(timestamp * LACT_TICKS_PER_US, now_time + offset) };
|
||||
do {
|
||||
REG_CLR_BIT(CONFIG_REG, TIMG_LACT_ALARM_EN);
|
||||
REG_WRITE(ALARM_LO_REG, alarm.lo);
|
||||
REG_WRITE(ALARM_HI_REG, alarm.hi);
|
||||
REG_SET_BIT(CONFIG_REG, TIMG_LACT_ALARM_EN);
|
||||
now_time = esp_timer_impl_get_counter_reg();
|
||||
int64_t delta = (int64_t)alarm.val - (int64_t)now_time;
|
||||
if (delta <= 0 && REG_GET_FIELD(INT_ST_REG, TIMG_LACT_INT_ST) == 0) {
|
||||
// new alarm is less than the counter and the interrupt flag is not set
|
||||
offset += llabs(delta) + LACT_TICKS_PER_US * 2;
|
||||
alarm.val = now_time + offset;
|
||||
} else {
|
||||
// finish if either (alarm > counter) or the interrupt flag is already set.
|
||||
break;
|
||||
}
|
||||
} while (1);
|
||||
}
|
||||
portEXIT_CRITICAL_SAFE(&s_time_update_lock);
|
||||
}
|
||||
|
||||
static void ESP_TIMER_IRAM_ATTR timer_alarm_isr(void *arg)
|
||||
{
|
||||
#if ISR_HANDLERS == 1
|
||||
/* Clear interrupt status */
|
||||
REG_WRITE(INT_CLR_REG, TIMG_LACT_INT_CLR);
|
||||
|
||||
/* Call the upper layer handler */
|
||||
(*s_alarm_handler)(arg);
|
||||
#else
|
||||
static volatile uint32_t processed_by = NOT_USED;
|
||||
static volatile bool pending_alarm = false;
|
||||
/* CRITICAL section ensures the read/clear is atomic between cores */
|
||||
portENTER_CRITICAL_ISR(&s_time_update_lock);
|
||||
if (REG_GET_FIELD(INT_ST_REG, TIMG_LACT_INT_ST)) {
|
||||
// Clear interrupt status
|
||||
REG_WRITE(INT_CLR_REG, TIMG_LACT_INT_CLR);
|
||||
// Is the other core already processing a previous alarm?
|
||||
if (processed_by == NOT_USED) {
|
||||
// Current core is not processing an alarm yet
|
||||
processed_by = xPortGetCoreID();
|
||||
do {
|
||||
pending_alarm = false;
|
||||
// Clear interrupt status
|
||||
REG_WRITE(INT_CLR_REG, TIMG_LACT_INT_CLR);
|
||||
portEXIT_CRITICAL_ISR(&s_time_update_lock);
|
||||
|
||||
(*s_alarm_handler)(arg);
|
||||
|
||||
portENTER_CRITICAL_ISR(&s_time_update_lock);
|
||||
// Another alarm could have occurred while were handling the previous alarm.
|
||||
// Check if we need to call the s_alarm_handler again:
|
||||
// 1) if the alarm has already been fired, it helps to handle it immediately without an additional ISR call.
|
||||
// 2) handle pending alarm that was cleared by the other core in time when this core worked with the current alarm.
|
||||
} while (REG_GET_FIELD(INT_ST_REG, TIMG_LACT_INT_ST) || pending_alarm);
|
||||
processed_by = NOT_USED;
|
||||
} else {
|
||||
// Current core arrived at ISR but the other core is still handling a previous alarm.
|
||||
// Once we already cleared the ISR status we need to let the other core know that it was.
|
||||
// Set the flag to handle the current alarm by the other core later.
|
||||
pending_alarm = true;
|
||||
}
|
||||
}
|
||||
portEXIT_CRITICAL_ISR(&s_time_update_lock);
|
||||
#endif // ISR_HANDLERS != 1
|
||||
}
|
||||
|
||||
void esp_timer_impl_set(uint64_t new_us)
|
||||
{
|
||||
portENTER_CRITICAL(&s_time_update_lock);
|
||||
timer_64b_reg_t dst = { .val = new_us * LACT_TICKS_PER_US };
|
||||
REG_WRITE(LOAD_LO_REG, dst.lo);
|
||||
REG_WRITE(LOAD_HI_REG, dst.hi);
|
||||
REG_WRITE(LOAD_REG, 1);
|
||||
portEXIT_CRITICAL(&s_time_update_lock);
|
||||
}
|
||||
|
||||
void esp_timer_impl_advance(int64_t time_diff_us)
|
||||
{
|
||||
uint64_t now = esp_timer_impl_get_time();
|
||||
esp_timer_impl_set(now + time_diff_us);
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_impl_early_init(void)
|
||||
{
|
||||
PERIPH_RCC_ACQUIRE_ATOMIC(PERIPH_LACT, ref_count) {
|
||||
if (ref_count == 0) {
|
||||
timg_ll_enable_bus_clock(LACT_MODULE, true);
|
||||
timg_ll_reset_register(LACT_MODULE);
|
||||
}
|
||||
}
|
||||
|
||||
REG_WRITE(CONFIG_REG, 0);
|
||||
REG_WRITE(LOAD_LO_REG, 0);
|
||||
REG_WRITE(LOAD_HI_REG, 0);
|
||||
REG_WRITE(ALARM_LO_REG, UINT32_MAX);
|
||||
REG_WRITE(ALARM_HI_REG, UINT32_MAX);
|
||||
REG_WRITE(LOAD_REG, 1);
|
||||
REG_SET_BIT(INT_CLR_REG, TIMG_LACT_INT_CLR);
|
||||
REG_SET_FIELD(CONFIG_REG, TIMG_LACT_DIVIDER, APB_CLK_FREQ / 1000000 / LACT_TICKS_PER_US);
|
||||
REG_SET_BIT(CONFIG_REG, TIMG_LACT_INCREASE |
|
||||
TIMG_LACT_LEVEL_INT_EN |
|
||||
TIMG_LACT_EN);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler)
|
||||
{
|
||||
if (s_timer_interrupt_handle[(ISR_HANDLERS == 1) ? 0 : xPortGetCoreID()] != NULL) {
|
||||
ESP_EARLY_LOGE(TAG, "timer ISR is already initialized");
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
int isr_flags = ESP_INTR_FLAG_INTRDISABLED
|
||||
| ((1 << CONFIG_ESP_TIMER_INTERRUPT_LEVEL) & ESP_INTR_FLAG_LEVELMASK)
|
||||
#if CONFIG_ESP_TIMER_IN_IRAM
|
||||
| ESP_INTR_FLAG_IRAM
|
||||
#endif
|
||||
;
|
||||
|
||||
esp_err_t err = esp_intr_alloc(INTR_SOURCE_LACT, isr_flags,
|
||||
&timer_alarm_isr, NULL,
|
||||
&s_timer_interrupt_handle[(ISR_HANDLERS == 1) ? 0 : xPortGetCoreID()]);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_EARLY_LOGE(TAG, "Can not allocate ISR handler (0x%0x)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
if (s_alarm_handler == NULL) {
|
||||
s_alarm_handler = alarm_handler;
|
||||
/* In theory, this needs a shared spinlock with the timer group driver.
|
||||
* However since esp_timer_impl_init is called early at startup, this
|
||||
* will not cause issues in practice.
|
||||
*/
|
||||
REG_SET_BIT(INT_ENA_REG, TIMG_LACT_INT_ENA);
|
||||
portENTER_CRITICAL_SAFE(&s_time_update_lock);
|
||||
lact_ll_set_clock_prescale(LACT_LL_GET_HW(LACT_MODULE), esp_clk_apb_freq() / MHZ / LACT_TICKS_PER_US);
|
||||
portEXIT_CRITICAL_SAFE(&s_time_update_lock);
|
||||
// Set the step for the sleep mode when the timer will work
|
||||
// from a slow_clk frequency instead of the APB frequency.
|
||||
uint32_t slowclk_ticks_per_us = esp_clk_slowclk_cal_get() * LACT_TICKS_PER_US;
|
||||
REG_SET_FIELD(RTC_STEP_REG, TIMG_LACT_RTC_STEP_LEN, slowclk_ticks_per_us);
|
||||
}
|
||||
|
||||
err = esp_intr_enable(s_timer_interrupt_handle[(ISR_HANDLERS == 1) ? 0 : xPortGetCoreID()]);
|
||||
if (err != ESP_OK) {
|
||||
ESP_EARLY_LOGE(TAG, "Can not enable ISR (0x%0x)", err);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
void esp_timer_impl_deinit(void)
|
||||
{
|
||||
REG_WRITE(CONFIG_REG, 0);
|
||||
REG_SET_BIT(INT_CLR_REG, TIMG_LACT_INT_CLR);
|
||||
/* TODO: also clear TIMG_LACT_INT_ENA; however see the note in esp_timer_impl_init. */
|
||||
for (unsigned i = 0; i < ISR_HANDLERS; i++) {
|
||||
if (s_timer_interrupt_handle[i] != NULL) {
|
||||
esp_intr_disable(s_timer_interrupt_handle[i]);
|
||||
esp_intr_free(s_timer_interrupt_handle[i]);
|
||||
s_timer_interrupt_handle[i] = NULL;
|
||||
}
|
||||
}
|
||||
s_alarm_handler = NULL;
|
||||
PERIPH_RCC_RELEASE_ATOMIC(PERIPH_LACT, ref_count) {
|
||||
if (ref_count == 0) {
|
||||
timg_ll_enable_bus_clock(LACT_MODULE, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t esp_timer_impl_get_alarm_reg(void)
|
||||
{
|
||||
portENTER_CRITICAL_SAFE(&s_time_update_lock);
|
||||
timer_64b_reg_t alarm = {
|
||||
.lo = REG_READ(ALARM_LO_REG),
|
||||
.hi = REG_READ(ALARM_HI_REG)
|
||||
};
|
||||
portEXIT_CRITICAL_SAFE(&s_time_update_lock);
|
||||
return alarm.val;
|
||||
}
|
||||
|
||||
void esp_timer_private_set(uint64_t new_us) __attribute__((alias("esp_timer_impl_set")));
|
||||
void esp_timer_private_advance(int64_t time_diff_us) __attribute__((alias("esp_timer_impl_advance")));
|
||||
@@ -0,0 +1,383 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include <stdatomic.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <sys/eventfd.h>
|
||||
#include <sys/poll.h>
|
||||
#include <sys/prctl.h>
|
||||
|
||||
#include "sys/param.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_timer_impl.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char *TAG = "esp_timer_impl";
|
||||
|
||||
/*
|
||||
* Linux host backend for esp_timer.
|
||||
*
|
||||
* This implementation emulates a hardware alarm using Linux timerfd.
|
||||
*
|
||||
* The esp_timer time domain is:
|
||||
*
|
||||
* esp_time = CLOCK_MONOTONIC + s_time_offset_us
|
||||
*
|
||||
* Therefore, when esp_timer asks us to arm an alarm at deadline T,
|
||||
* the corresponding CLOCK_MONOTONIC absolute deadline is:
|
||||
*
|
||||
* mono_deadline = T - s_time_offset_us
|
||||
*
|
||||
* The alarm is programmed as an absolute CLOCK_MONOTONIC timerfd deadline
|
||||
* using timerfd_settime(..., TFD_TIMER_ABSTIME, ...).
|
||||
*
|
||||
* A dedicated pthread waits on:
|
||||
*
|
||||
* - timerfd: alarm expiration
|
||||
* - eventfd: shutdown notification
|
||||
*
|
||||
* When timerfd expires, the thread notifies the common esp_timer task with
|
||||
* xTaskNotifyGive(), preserving the common esp_timer callback flow.
|
||||
*
|
||||
* timerfd expiration -> alarm pthread wakeup -> xTaskNotifyGive() -> esp_timer task execution
|
||||
*/
|
||||
|
||||
/* Alarm values to generate interrupt on match */
|
||||
extern uint64_t timestamp_id[2];
|
||||
|
||||
/* Dedicated alarm thread used as the Linux "hardware timer". */
|
||||
static pthread_t s_alarm_thread;
|
||||
static bool s_alarm_thread_created;
|
||||
|
||||
/* Linux timer/event descriptors. */
|
||||
static int s_timer_fd = -1;
|
||||
static int s_shutdown_fd = -1;
|
||||
|
||||
/* Adjustable offset between CLOCK_MONOTONIC and esp_timer time. */
|
||||
static _Atomic int64_t s_time_offset_us;
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Time base */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
static int64_t get_monotonic_time_us(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (int64_t)ts.tv_sec * 1000000LL + ts.tv_nsec / 1000LL;
|
||||
}
|
||||
|
||||
uint64_t esp_timer_impl_get_counter_reg(void)
|
||||
{
|
||||
return (uint64_t) esp_timer_impl_get_time();
|
||||
}
|
||||
|
||||
int64_t esp_timer_impl_get_time(void)
|
||||
{
|
||||
return get_monotonic_time_us() + atomic_load_explicit(&s_time_offset_us, memory_order_relaxed);
|
||||
}
|
||||
|
||||
int64_t esp_timer_get_time(void)
|
||||
{
|
||||
return esp_timer_impl_get_time();
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* timerfd helpers */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
static void us_to_timespec_abs(int64_t us, struct timespec *ts)
|
||||
{
|
||||
if (us < 0) {
|
||||
us = 0;
|
||||
}
|
||||
|
||||
ts->tv_sec = us / 1000000LL;
|
||||
ts->tv_nsec = (us % 1000000LL) * 1000LL;
|
||||
}
|
||||
|
||||
static int64_t deadline_to_monotonic_us(uint64_t deadline_us)
|
||||
{
|
||||
int64_t offset_us = atomic_load_explicit(&s_time_offset_us, memory_order_relaxed);
|
||||
return (int64_t)deadline_us - offset_us;
|
||||
}
|
||||
|
||||
static esp_err_t program_timerfd(uint64_t deadline_us)
|
||||
{
|
||||
struct itimerspec its = { 0 };
|
||||
|
||||
if (s_timer_fd < 0) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (deadline_us == UINT64_MAX) {
|
||||
/*
|
||||
* Disarm timerfd.
|
||||
* For timerfd_settime(), zero it_value disarms the timer.
|
||||
*/
|
||||
if (timerfd_settime(s_timer_fd, TFD_TIMER_ABSTIME, &its, NULL) != 0) {
|
||||
ESP_LOGE(TAG, "timerfd disarm failed: %s", strerror(errno));
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
int64_t mono_deadline_us = deadline_to_monotonic_us(deadline_us);
|
||||
int64_t now_mono_us = get_monotonic_time_us();
|
||||
|
||||
if (mono_deadline_us <= now_mono_us) {
|
||||
// Using now + 1 us avoids zero it_value, because zero disarms timerfd.
|
||||
mono_deadline_us = now_mono_us + 1;
|
||||
}
|
||||
|
||||
us_to_timespec_abs(mono_deadline_us, &its.it_value);
|
||||
its.it_interval.tv_sec = 0;
|
||||
its.it_interval.tv_nsec = 0;
|
||||
|
||||
if (timerfd_settime(s_timer_fd, TFD_TIMER_ABSTIME, &its, NULL) != 0) {
|
||||
ESP_LOGE(TAG, "timerfd_settime failed: %s", strerror(errno));
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Alarm thread */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
static void *alarm_thread_func(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
|
||||
#ifdef PR_SET_TIMERSLACK
|
||||
// Set timer slack to 1 ns for this thread.
|
||||
// Linux coalesces nearby wakeups to save power; slack controls the allowed delay.
|
||||
// Default is 50 us. Setting it to 1 ns minimises timerfd wakeup jitter.
|
||||
prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
|
||||
#endif
|
||||
|
||||
struct pollfd fds[2] = {
|
||||
{
|
||||
.fd = s_timer_fd,
|
||||
.events = POLLIN,
|
||||
.revents = 0,
|
||||
},
|
||||
{
|
||||
.fd = s_shutdown_fd,
|
||||
.events = POLLIN,
|
||||
.revents = 0,
|
||||
},
|
||||
};
|
||||
|
||||
while (true) {
|
||||
int ret = poll(fds, 2, -1);
|
||||
if (ret < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ESP_LOGE(TAG, "alarm poll failed: %s", strerror(errno));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fds[1].revents & POLLIN) {
|
||||
uint64_t val;
|
||||
ssize_t n = read(s_shutdown_fd, &val, sizeof(val));
|
||||
(void)n;
|
||||
break;
|
||||
}
|
||||
|
||||
if (fds[0].revents & POLLIN) {
|
||||
uint64_t expirations;
|
||||
ssize_t n = read(s_timer_fd, &expirations, sizeof(expirations));
|
||||
if (n != sizeof(expirations)) {
|
||||
if (n < 0 && (errno == EINTR || errno == EAGAIN)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (n < 0) {
|
||||
ESP_LOGE(TAG, "timerfd read failed: %s", strerror(errno));
|
||||
} else {
|
||||
ESP_LOGE(TAG, "timerfd short read: %zd", n);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
TaskHandle_t timer_task = esp_timer_impl_get_timer_task_handle();
|
||||
if (timer_task != NULL) {
|
||||
xTaskNotifyGive(timer_task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Alarm programming API */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
void esp_timer_impl_set_alarm_id(uint64_t timestamp_us, unsigned alarm_id)
|
||||
{
|
||||
esp_timer_impl_lock();
|
||||
|
||||
if (alarm_id < 2) {
|
||||
timestamp_id[alarm_id] = timestamp_us;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Invalid alarm_id: %u", alarm_id);
|
||||
esp_timer_impl_unlock();
|
||||
return;
|
||||
}
|
||||
|
||||
uint64_t min_alarm_us = MIN(timestamp_id[0], timestamp_id[1]);
|
||||
program_timerfd(min_alarm_us);
|
||||
|
||||
esp_timer_impl_unlock();
|
||||
}
|
||||
|
||||
void esp_timer_impl_set(uint64_t new_us)
|
||||
{
|
||||
esp_timer_impl_lock();
|
||||
|
||||
atomic_store_explicit(&s_time_offset_us, (int64_t)new_us - get_monotonic_time_us(), memory_order_relaxed);
|
||||
|
||||
// Offset changed, so the same esp_timer deadline now maps to a different
|
||||
// CLOCK_MONOTONIC absolute deadline. Reprogram timerfd.
|
||||
uint64_t min_alarm_us = MIN(timestamp_id[0], timestamp_id[1]);
|
||||
program_timerfd(min_alarm_us);
|
||||
|
||||
esp_timer_impl_unlock();
|
||||
}
|
||||
|
||||
void esp_timer_impl_advance(int64_t time_diff_us)
|
||||
{
|
||||
esp_timer_impl_lock();
|
||||
|
||||
atomic_fetch_add_explicit(&s_time_offset_us, time_diff_us, memory_order_relaxed);
|
||||
|
||||
// Offset changed, so reprogram the host timer.
|
||||
uint64_t min_alarm_us = MIN(timestamp_id[0], timestamp_id[1]);
|
||||
program_timerfd(min_alarm_us);
|
||||
|
||||
esp_timer_impl_unlock();
|
||||
}
|
||||
|
||||
void esp_timer_private_set(uint64_t new_us)
|
||||
{
|
||||
esp_timer_impl_set(new_us);
|
||||
}
|
||||
|
||||
void esp_timer_private_advance(int64_t time_diff_us)
|
||||
{
|
||||
esp_timer_impl_advance(time_diff_us);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Init/deinit */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
esp_err_t esp_timer_impl_early_init(void)
|
||||
{
|
||||
// No initialization required to call esp_timer_impl_get_time().
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler)
|
||||
{
|
||||
(void)alarm_handler;
|
||||
|
||||
timestamp_id[0] = UINT64_MAX;
|
||||
timestamp_id[1] = UINT64_MAX;
|
||||
|
||||
atomic_store_explicit(&s_time_offset_us, 0, memory_order_relaxed);
|
||||
|
||||
s_timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
|
||||
if (s_timer_fd < 0) {
|
||||
ESP_LOGE(TAG, "timerfd_create failed: %s", strerror(errno));
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
s_shutdown_fd = eventfd(0, EFD_CLOEXEC);
|
||||
if (s_shutdown_fd < 0) {
|
||||
ESP_LOGE(TAG, "eventfd failed: %s", strerror(errno));
|
||||
close(s_timer_fd);
|
||||
s_timer_fd = -1;
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
int err = pthread_create(&s_alarm_thread, NULL, alarm_thread_func, NULL);
|
||||
if (err != 0) {
|
||||
ESP_LOGE(TAG, "Failed to create alarm thread: %s", strerror(err));
|
||||
|
||||
close(s_shutdown_fd);
|
||||
close(s_timer_fd);
|
||||
|
||||
s_shutdown_fd = -1;
|
||||
s_timer_fd = -1;
|
||||
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
s_alarm_thread_created = true;
|
||||
|
||||
ESP_LOGI(TAG, "esp_timer initialized successfully");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void esp_timer_impl_deinit(void)
|
||||
{
|
||||
if (!s_alarm_thread_created) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Disarm timer first, then wake alarm thread through eventfd.
|
||||
if (s_timer_fd >= 0) {
|
||||
struct itimerspec its = { 0 };
|
||||
(void)timerfd_settime(s_timer_fd, TFD_TIMER_ABSTIME, &its, NULL);
|
||||
}
|
||||
|
||||
if (s_shutdown_fd >= 0) {
|
||||
uint64_t one = 1;
|
||||
ssize_t n = write(s_shutdown_fd, &one, sizeof(one));
|
||||
(void)n;
|
||||
}
|
||||
|
||||
pthread_join(s_alarm_thread, NULL);
|
||||
|
||||
if (s_shutdown_fd >= 0) {
|
||||
close(s_shutdown_fd);
|
||||
s_shutdown_fd = -1;
|
||||
}
|
||||
|
||||
if (s_timer_fd >= 0) {
|
||||
close(s_timer_fd);
|
||||
s_timer_fd = -1;
|
||||
}
|
||||
|
||||
s_alarm_thread_created = false;
|
||||
}
|
||||
|
||||
uint64_t esp_timer_impl_get_alarm_reg(void)
|
||||
{
|
||||
esp_timer_impl_lock();
|
||||
uint64_t min_alarm_us = MIN(timestamp_id[0], timestamp_id[1]);
|
||||
esp_timer_impl_unlock();
|
||||
|
||||
return min_alarm_us;
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_timer_impl.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_attr.h"
|
||||
#include "esp_intr_alloc.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_compiler.h"
|
||||
#include "soc/periph_defs.h"
|
||||
#include "soc/soc_caps.h"
|
||||
#include "esp_private/esp_clk.h"
|
||||
#include "esp_private/systimer.h"
|
||||
#include "esp_private/periph_ctrl.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "hal/systimer_ll.h"
|
||||
#include "hal/systimer_types.h"
|
||||
#include "hal/systimer_hal.h"
|
||||
|
||||
/**
|
||||
* @file esp_timer_systimer.c
|
||||
* @brief Implementation of esp_timer using systimer.
|
||||
*
|
||||
* This timer is a 64-bit up-counting timer, with a programmable compare value (called 'alarm' hereafter).
|
||||
* When the timer reaches compare value, interrupt is raised.
|
||||
* The timer can be configured to produce an edge interrupt.
|
||||
*
|
||||
* @note systimer counter0 and alarm2 are adopted to implemented esp_timer
|
||||
*/
|
||||
|
||||
ESP_LOG_ATTR_TAG(TAG, "esp_timer_systimer");
|
||||
|
||||
#define NOT_USED 0xBAD00FAD
|
||||
|
||||
/* Interrupt handle returned by the interrupt allocator */
|
||||
#ifdef CONFIG_ESP_TIMER_ISR_AFFINITY_NO_AFFINITY
|
||||
#define ISR_HANDLERS (CONFIG_FREERTOS_NUMBER_OF_CORES)
|
||||
#else
|
||||
#define ISR_HANDLERS (1)
|
||||
#endif
|
||||
static intr_handle_t s_timer_interrupt_handle[ISR_HANDLERS] = { NULL };
|
||||
|
||||
/* Function from the upper layer to be called when the interrupt happens.
|
||||
* Registered in esp_timer_impl_init.
|
||||
*/
|
||||
static intr_handler_t s_alarm_handler = NULL;
|
||||
|
||||
/* Systimer HAL layer object */
|
||||
static systimer_hal_context_t systimer_hal;
|
||||
|
||||
/* Spinlock used to protect access to the hardware registers. */
|
||||
extern portMUX_TYPE s_time_update_lock;
|
||||
|
||||
/* Alarm values to generate interrupt on match */
|
||||
extern uint64_t timestamp_id[2];
|
||||
|
||||
uint64_t ESP_TIMER_IRAM_ATTR esp_timer_impl_get_counter_reg(void)
|
||||
{
|
||||
return systimer_hal_get_counter_value(&systimer_hal, SYSTIMER_COUNTER_ESPTIMER);
|
||||
}
|
||||
|
||||
int64_t ESP_TIMER_IRAM_ATTR esp_timer_impl_get_time(void)
|
||||
{
|
||||
// we hope the execution time of this function won't > 1us
|
||||
// thus, to save one function call, we didn't use the existing `systimer_hal_get_time`
|
||||
return systimer_hal.ticks_to_us(systimer_hal_get_counter_value(&systimer_hal, SYSTIMER_COUNTER_ESPTIMER));
|
||||
}
|
||||
|
||||
int64_t esp_timer_get_time(void) __attribute__((alias("esp_timer_impl_get_time")));
|
||||
|
||||
void ESP_TIMER_IRAM_ATTR esp_timer_impl_set_alarm_id(uint64_t timestamp, unsigned alarm_id)
|
||||
{
|
||||
assert(alarm_id < sizeof(timestamp_id) / sizeof(timestamp_id[0]));
|
||||
portENTER_CRITICAL_SAFE(&s_time_update_lock);
|
||||
timestamp_id[alarm_id] = timestamp;
|
||||
timestamp = MIN(timestamp_id[0], timestamp_id[1]);
|
||||
systimer_hal_set_alarm_target(&systimer_hal, SYSTIMER_ALARM_ESPTIMER, timestamp);
|
||||
portEXIT_CRITICAL_SAFE(&s_time_update_lock);
|
||||
}
|
||||
|
||||
static void ESP_TIMER_IRAM_ATTR timer_alarm_isr(void *arg)
|
||||
{
|
||||
#if ISR_HANDLERS == 1
|
||||
// clear the interrupt
|
||||
systimer_ll_clear_alarm_int(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER);
|
||||
/* Call the upper layer handler */
|
||||
(*s_alarm_handler)(arg);
|
||||
#else
|
||||
static volatile uint32_t processed_by = NOT_USED;
|
||||
static volatile bool pending_alarm = false;
|
||||
/* CRITICAL section ensures the read/clear is atomic between cores */
|
||||
portENTER_CRITICAL_ISR(&s_time_update_lock);
|
||||
if (systimer_ll_is_alarm_int_fired(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER)) {
|
||||
// Clear interrupt status
|
||||
systimer_ll_clear_alarm_int(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER);
|
||||
// Is the other core already processing a previous alarm?
|
||||
if (processed_by == NOT_USED) {
|
||||
// Current core is not processing an alarm yet
|
||||
processed_by = xPortGetCoreID();
|
||||
do {
|
||||
pending_alarm = false;
|
||||
// Clear interrupt status
|
||||
systimer_ll_clear_alarm_int(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER);
|
||||
portEXIT_CRITICAL_ISR(&s_time_update_lock);
|
||||
|
||||
(*s_alarm_handler)(arg);
|
||||
|
||||
portENTER_CRITICAL_ISR(&s_time_update_lock);
|
||||
// Another alarm could have occurred while were handling the previous alarm.
|
||||
// Check if we need to call the s_alarm_handler again:
|
||||
// 1) if the alarm has already been fired, it helps to handle it immediately without an additional ISR call.
|
||||
// 2) handle pending alarm that was cleared by the other core in time when this core worked with the current alarm.
|
||||
} while (systimer_ll_is_alarm_int_fired(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER) || pending_alarm);
|
||||
processed_by = NOT_USED;
|
||||
} else {
|
||||
// Current core arrived at ISR but the other core is still handling a previous alarm.
|
||||
// Once we already cleared the ISR status we need to let the other core know that it was.
|
||||
// Set the flag to handle the current alarm by the other core later.
|
||||
pending_alarm = true;
|
||||
}
|
||||
}
|
||||
portEXIT_CRITICAL_ISR(&s_time_update_lock);
|
||||
#endif // ISR_HANDLERS != 1
|
||||
}
|
||||
|
||||
void esp_timer_impl_set(uint64_t new_us)
|
||||
{
|
||||
portENTER_CRITICAL_SAFE(&s_time_update_lock);
|
||||
systimer_counter_value_t new_count = {
|
||||
.val = systimer_hal.us_to_ticks(new_us),
|
||||
};
|
||||
systimer_ll_set_counter_value(systimer_hal.dev, SYSTIMER_COUNTER_ESPTIMER, new_count.val);
|
||||
systimer_ll_apply_counter_value(systimer_hal.dev, SYSTIMER_COUNTER_ESPTIMER);
|
||||
portEXIT_CRITICAL_SAFE(&s_time_update_lock);
|
||||
}
|
||||
|
||||
void esp_timer_impl_advance(int64_t time_diff_us)
|
||||
{
|
||||
portENTER_CRITICAL_SAFE(&s_time_update_lock);
|
||||
systimer_hal_counter_value_advance(&systimer_hal, SYSTIMER_COUNTER_ESPTIMER, time_diff_us);
|
||||
portEXIT_CRITICAL_SAFE(&s_time_update_lock);
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_impl_early_init(void)
|
||||
{
|
||||
PERIPH_RCC_ACQUIRE_ATOMIC(PERIPH_SYSTIMER_MODULE, ref_count) {
|
||||
if (ref_count == 0) {
|
||||
systimer_ll_enable_bus_clock(true);
|
||||
systimer_ll_reset_register();
|
||||
systimer_ll_enable_sys_clock(true);
|
||||
}
|
||||
}
|
||||
systimer_hal_tick_rate_ops_t ops = {
|
||||
.ticks_to_us = systimer_ticks_to_us,
|
||||
.us_to_ticks = systimer_us_to_ticks,
|
||||
};
|
||||
systimer_hal_init(&systimer_hal);
|
||||
systimer_hal_set_tick_rate_ops(&systimer_hal, &ops);
|
||||
|
||||
#if !SYSTIMER_LL_FIXED_DIVIDER
|
||||
assert(esp_clk_xtal_freq() == (40 * 1000000) &&
|
||||
"update the step for xtal to support other XTAL:APB frequency ratios");
|
||||
systimer_hal_set_steps_per_tick(&systimer_hal, 0, 2); // for xtal
|
||||
systimer_hal_set_steps_per_tick(&systimer_hal, 1, 1); // for pll
|
||||
#endif
|
||||
|
||||
systimer_hal_enable_counter(&systimer_hal, SYSTIMER_COUNTER_ESPTIMER);
|
||||
systimer_hal_select_alarm_mode(&systimer_hal, SYSTIMER_ALARM_ESPTIMER, SYSTIMER_ALARM_MODE_ONESHOT);
|
||||
systimer_hal_connect_alarm_counter(&systimer_hal, SYSTIMER_ALARM_ESPTIMER, SYSTIMER_COUNTER_ESPTIMER);
|
||||
|
||||
for (unsigned cpuid = 0; cpuid < SOC_CPU_CORES_NUM; ++cpuid) {
|
||||
bool can_stall = (cpuid < portNUM_PROCESSORS);
|
||||
systimer_hal_counter_can_stall_by_cpu(&systimer_hal, SYSTIMER_COUNTER_ESPTIMER, cpuid, can_stall);
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler)
|
||||
{
|
||||
if (s_timer_interrupt_handle[(ISR_HANDLERS == 1) ? 0 : xPortGetCoreID()] != NULL) {
|
||||
ESP_EARLY_LOGE(TAG, "timer ISR is already initialized");
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
int isr_flags = ESP_INTR_FLAG_INTRDISABLED
|
||||
| ((1 << CONFIG_ESP_TIMER_INTERRUPT_LEVEL) & ESP_INTR_FLAG_LEVELMASK)
|
||||
#if !SYSTIMER_LL_INT_LEVEL
|
||||
| ESP_INTR_FLAG_EDGE
|
||||
#endif
|
||||
#if CONFIG_ESP_TIMER_IN_IRAM
|
||||
| ESP_INTR_FLAG_IRAM
|
||||
#endif
|
||||
;
|
||||
|
||||
esp_err_t err = esp_intr_alloc(ETS_SYSTIMER_TARGET2_INTR_SOURCE, isr_flags,
|
||||
&timer_alarm_isr, NULL,
|
||||
&s_timer_interrupt_handle[(ISR_HANDLERS == 1) ? 0 : xPortGetCoreID()]);
|
||||
if (err != ESP_OK) {
|
||||
ESP_EARLY_LOGE(TAG, "esp_intr_alloc failed (0x%x)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
if (s_alarm_handler == NULL) {
|
||||
s_alarm_handler = alarm_handler;
|
||||
/* TODO: if SYSTIMER is used for anything else, access to SYSTIMER_INT_ENA_REG has to be
|
||||
* protected by a shared spinlock. Since this code runs as part of early startup, this
|
||||
* is practically not an issue.
|
||||
*/
|
||||
systimer_hal_enable_alarm_int(&systimer_hal, SYSTIMER_ALARM_ESPTIMER);
|
||||
}
|
||||
|
||||
err = esp_intr_enable(s_timer_interrupt_handle[(ISR_HANDLERS == 1) ? 0 : xPortGetCoreID()]);
|
||||
if (err != ESP_OK) {
|
||||
ESP_EARLY_LOGE(TAG, "Can not enable ISR (0x%0x)", err);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
void esp_timer_impl_deinit(void)
|
||||
{
|
||||
systimer_ll_enable_alarm(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER, false);
|
||||
/* TODO: may need a spinlock, see the note related to SYSTIMER_INT_ENA_REG in systimer_hal_init */
|
||||
systimer_ll_enable_alarm_int(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER, false);
|
||||
for (unsigned i = 0; i < ISR_HANDLERS; i++) {
|
||||
if (s_timer_interrupt_handle[i] != NULL) {
|
||||
esp_intr_disable(s_timer_interrupt_handle[i]);
|
||||
esp_intr_free(s_timer_interrupt_handle[i]);
|
||||
s_timer_interrupt_handle[i] = NULL;
|
||||
}
|
||||
}
|
||||
s_alarm_handler = NULL;
|
||||
}
|
||||
|
||||
uint64_t esp_timer_impl_get_alarm_reg(void)
|
||||
{
|
||||
portENTER_CRITICAL_SAFE(&s_time_update_lock);
|
||||
uint64_t val = systimer_hal_get_alarm_value(&systimer_hal, SYSTIMER_ALARM_ESPTIMER);
|
||||
portEXIT_CRITICAL_SAFE(&s_time_update_lock);
|
||||
return val;
|
||||
}
|
||||
|
||||
void esp_timer_private_set(uint64_t new_us) __attribute__((alias("esp_timer_impl_set")));
|
||||
void esp_timer_private_advance(int64_t time_diff_us) __attribute__((alias("esp_timer_impl_advance")));
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_private/startup_internal.h"
|
||||
#include "esp_timer_impl.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
esp_err_t esp_timer_early_init(void)
|
||||
{
|
||||
esp_timer_impl_early_init();
|
||||
#if CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
|
||||
esp_timer_impl_init_system_time();
|
||||
#endif
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function starts a timer, which is used by esp_timer
|
||||
* to count time from the very start of the application.
|
||||
*
|
||||
* Another initialization function, esp_timer_init_nonos (which initializes ISR and task),
|
||||
* is called only if other code calls the esp_timer API.
|
||||
*/
|
||||
ESP_SYSTEM_INIT_FN(esp_timer_init_nonos, CORE, BIT(0), 101)
|
||||
{
|
||||
return esp_timer_early_init();
|
||||
}
|
||||
|
||||
void esp_timer_init_include_func(void)
|
||||
{
|
||||
// Hook to force the linker to include this file
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2010-2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/*
|
||||
* ets_timer module implements a set of legacy timer APIs which are
|
||||
* used by the WiFi driver. This is done on top of the newer esp_timer APIs.
|
||||
* Applications should not use ets_timer functions, as they may change without
|
||||
* notice.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "esp_types.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_attr.h"
|
||||
#include "esp_intr_alloc.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_timer_impl.h"
|
||||
|
||||
// for ETSTimer type
|
||||
#include "rom/ets_sys.h"
|
||||
|
||||
/* We abuse 'timer_arg' field of ETSTimer structure to hold a pointer to esp_timer */
|
||||
#define ESP_TIMER(p_ets_timer) ((esp_timer_handle_t) (p_ets_timer)->timer_arg)
|
||||
|
||||
/* We abuse 'timer_expire' field of ETSTimer structure to hold a magic value
|
||||
* signifying that the contents of the timer was zeroed out.
|
||||
*/
|
||||
#define TIMER_INITIALIZED_FIELD(p_ets_timer) ((p_ets_timer)->timer_expire)
|
||||
#define TIMER_INITIALIZED_VAL 0x12121212
|
||||
|
||||
static ESP_TIMER_IRAM_ATTR bool timer_initialized(ETSTimer *ptimer)
|
||||
{
|
||||
return TIMER_INITIALIZED_FIELD(ptimer) == TIMER_INITIALIZED_VAL;
|
||||
}
|
||||
|
||||
void ets_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg)
|
||||
{
|
||||
if (!timer_initialized(ptimer)) {
|
||||
memset(ptimer, 0, sizeof(*ptimer));
|
||||
TIMER_INITIALIZED_FIELD(ptimer) = TIMER_INITIALIZED_VAL;
|
||||
}
|
||||
|
||||
if (ESP_TIMER(ptimer) == NULL) {
|
||||
const esp_timer_create_args_t create_args = {
|
||||
.callback = pfunction,
|
||||
.arg = parg,
|
||||
.name = "ETSTimer",
|
||||
.dispatch_method = ESP_TIMER_TASK
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(esp_timer_create(&create_args, (esp_timer_handle_t*) & (ptimer->timer_arg)));
|
||||
}
|
||||
}
|
||||
|
||||
void ESP_TIMER_IRAM_ATTR ets_timer_arm_us(ETSTimer *ptimer, uint32_t time_us, bool repeat_flag)
|
||||
{
|
||||
assert(timer_initialized(ptimer));
|
||||
esp_timer_stop(ESP_TIMER(ptimer)); // no error check
|
||||
if (!repeat_flag) {
|
||||
ESP_ERROR_CHECK(esp_timer_start_once(ESP_TIMER(ptimer), time_us));
|
||||
} else {
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(ESP_TIMER(ptimer), time_us));
|
||||
}
|
||||
}
|
||||
|
||||
void ESP_TIMER_IRAM_ATTR ets_timer_arm(ETSTimer *ptimer, uint32_t time_ms, bool repeat_flag)
|
||||
{
|
||||
uint64_t time_us = 1000LL * (uint64_t) time_ms;
|
||||
assert(timer_initialized(ptimer));
|
||||
esp_timer_stop(ESP_TIMER(ptimer)); // no error check
|
||||
if (!repeat_flag) {
|
||||
ESP_ERROR_CHECK(esp_timer_start_once(ESP_TIMER(ptimer), time_us));
|
||||
} else {
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(ESP_TIMER(ptimer), time_us));
|
||||
}
|
||||
}
|
||||
|
||||
void ets_timer_done(ETSTimer *ptimer)
|
||||
{
|
||||
if (timer_initialized(ptimer)) {
|
||||
esp_timer_delete(ESP_TIMER(ptimer));
|
||||
ptimer->timer_arg = NULL;
|
||||
TIMER_INITIALIZED_FIELD(ptimer) = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void ESP_TIMER_IRAM_ATTR ets_timer_disarm(ETSTimer *ptimer)
|
||||
{
|
||||
if (timer_initialized(ptimer)) {
|
||||
esp_timer_stop(ESP_TIMER(ptimer));
|
||||
}
|
||||
}
|
||||
|
||||
void ets_timer_init(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ets_timer_deinit(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void os_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg) __attribute__((alias("ets_timer_setfn")));
|
||||
void os_timer_disarm(ETSTimer *ptimer) __attribute__((alias("ets_timer_disarm")));
|
||||
void os_timer_arm_us(ETSTimer *ptimer, uint32_t u_seconds, bool repeat_flag) __attribute__((alias("ets_timer_arm_us")));
|
||||
void os_timer_arm(ETSTimer *ptimer, uint32_t milliseconds, bool repeat_flag) __attribute__((alias("ets_timer_arm")));
|
||||
void os_timer_done(ETSTimer *ptimer) __attribute__((alias("ets_timer_done")));
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
// Provides strong definition for system time functions relied upon
|
||||
// by core components.
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#if CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
|
||||
#include "esp_timer.h"
|
||||
#include "esp_timer_impl.h"
|
||||
#include "esp_system.h"
|
||||
#ifndef CONFIG_IDF_TARGET_LINUX
|
||||
#include "esp_newlib.h"
|
||||
#endif
|
||||
|
||||
#include "esp_private/startup_internal.h"
|
||||
#include "esp_rtc_time.h"
|
||||
|
||||
// Correction for underlying timer to keep definition
|
||||
// of system time consistent.
|
||||
static int64_t s_correction_us = 0;
|
||||
|
||||
#if defined(CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER) && defined(CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER)
|
||||
ESP_SHUTDOWN_HANDLER_REGISTER(esp_sync_timekeeping_timers_shutdown, 100)
|
||||
{
|
||||
esp_sync_timekeeping_timers();
|
||||
return ESP_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
void esp_timer_impl_init_system_time(void)
|
||||
{
|
||||
#ifndef CONFIG_IDF_TARGET_LINUX
|
||||
s_correction_us = esp_rtc_get_time_us() - g_startup_time - esp_timer_impl_get_time();
|
||||
#endif // !CONFIG_IDF_TARGET_LINUX
|
||||
}
|
||||
|
||||
int64_t ESP_TIMER_IRAM_ATTR esp_system_get_time(void)
|
||||
{
|
||||
return esp_timer_get_time() + s_correction_us;
|
||||
}
|
||||
|
||||
uint32_t ESP_TIMER_IRAM_ATTR esp_system_get_time_resolution(void)
|
||||
{
|
||||
return 1000;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user