| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- /*
- * SPDX-FileCopyrightText: 2017-2021 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 "soc/spinlock.h"
- #include "esp_timer.h"
- #include "esp_timer_impl.h"
- #include "esp_private/startup_internal.h"
- #include "esp_private/esp_timer_private.h"
- #include "esp_private/system_internal.h"
- #if CONFIG_IDF_TARGET_ESP32
- #include "esp32/rtc.h"
- #elif CONFIG_IDF_TARGET_ESP32S2
- #include "esp32s2/rtc.h"
- #elif CONFIG_IDF_TARGET_ESP32S3
- #include "esp32s3/rtc.h"
- #elif CONFIG_IDF_TARGET_ESP32C3
- #include "esp32c3/rtc.h"
- #endif
- #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_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
- } flags_t;
- struct esp_timer {
- uint64_t alarm;
- uint64_t period:56;
- 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);
- static esp_err_t timer_remove(esp_timer_handle_t timer);
- static bool timer_armed(esp_timer_handle_t timer);
- static void timer_list_lock(void);
- static void timer_list_unlock(void);
- #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
- __attribute__((unused)) static const char* TAG = "esp_timer";
- // list of currently armed timers
- static LIST_HEAD(esp_timer_list, esp_timer) s_timers =
- LIST_HEAD_INITIALIZER(s_timers);
- #if WITH_PROFILING
- // list of unarmed timers, used only to be able to dump statistics about
- // all the timers
- static LIST_HEAD(esp_inactive_timer_list, esp_timer) s_inactive_timers =
- 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 = portMUX_INITIALIZER_UNLOCKED;
- 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) {
- return ESP_ERR_INVALID_ARG;
- }
- esp_timer_handle_t result = (esp_timer_handle_t) calloc(1, sizeof(*result));
- if (result == NULL) {
- return ESP_ERR_NO_MEM;
- }
- result->callback = args->callback;
- result->arg = args->arg;
- result->flags = (args->dispatch_method ? FL_DISPATCH_METHOD : 0) |
- (args->skip_unhandled_events ? FL_SKIP_UNHANDLED_EVENTS : 0);
- #if WITH_PROFILING
- result->name = args->name;
- timer_insert_inactive(result);
- #endif
- *out_handle = result;
- return ESP_OK;
- }
- esp_err_t IRAM_ATTR esp_timer_start_once(esp_timer_handle_t timer, uint64_t timeout_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 = 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;
- }
- esp_err_t IRAM_ATTR esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period_us)
- {
- if (timer == NULL) {
- return ESP_ERR_INVALID_ARG;
- }
- if (!is_initialized() || timer_armed(timer)) {
- return ESP_ERR_INVALID_STATE;
- }
- timer_list_lock();
- period_us = MAX(period_us, esp_timer_impl_get_min_period_us());
- timer->alarm = esp_timer_get_time() + period_us;
- timer->period = period_us;
- #if WITH_PROFILING
- timer->times_armed++;
- timer->times_skipped = 0;
- #endif
- esp_err_t err = timer_insert(timer);
- timer_list_unlock();
- return err;
- }
- esp_err_t IRAM_ATTR esp_timer_stop(esp_timer_handle_t timer)
- {
- if (timer == NULL) {
- return ESP_ERR_INVALID_ARG;
- }
- if (!is_initialized() || !timer_armed(timer)) {
- return ESP_ERR_INVALID_STATE;
- }
- return timer_remove(timer);
- }
- esp_err_t esp_timer_delete(esp_timer_handle_t timer)
- {
- if (timer == NULL) {
- return ESP_ERR_INVALID_ARG;
- }
- if (timer_armed(timer)) {
- return ESP_ERR_INVALID_STATE;
- }
- timer_list_lock();
- timer->event_id = EVENT_ID_DELETE_TIMER;
- timer->alarm = esp_timer_get_time();
- timer->period = 0;
- timer_insert(timer);
- timer_list_unlock();
- return ESP_OK;
- }
- static IRAM_ATTR esp_err_t timer_insert(esp_timer_handle_t timer)
- {
- #if WITH_PROFILING
- timer_remove_inactive(timer);
- #endif
- esp_timer_handle_t it, last = NULL;
- if (LIST_FIRST(&s_timers) == NULL) {
- LIST_INSERT_HEAD(&s_timers, timer, list_entry);
- } else {
- LIST_FOREACH(it, &s_timers, 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 (timer == LIST_FIRST(&s_timers)) {
- esp_timer_impl_set_alarm(timer->alarm);
- }
- return ESP_OK;
- }
- static IRAM_ATTR esp_err_t timer_remove(esp_timer_handle_t timer)
- {
- timer_list_lock();
- LIST_REMOVE(timer, list_entry);
- timer->alarm = 0;
- timer->period = 0;
- #if WITH_PROFILING
- timer_insert_inactive(timer);
- #endif
- timer_list_unlock();
- return ESP_OK;
- }
- #if WITH_PROFILING
- static 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.
- */
- timer_list_lock();
- esp_timer_handle_t head = LIST_FIRST(&s_inactive_timers);
- if (head == NULL) {
- LIST_INSERT_HEAD(&s_inactive_timers, 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);
- }
- timer_list_unlock();
- }
- static IRAM_ATTR void timer_remove_inactive(esp_timer_handle_t timer)
- {
- timer_list_lock();
- LIST_REMOVE(timer, list_entry);
- timer_list_unlock();
- }
- #endif // WITH_PROFILING
- static IRAM_ATTR bool timer_armed(esp_timer_handle_t timer)
- {
- return timer->alarm > 0;
- }
- static IRAM_ATTR void timer_list_lock(void)
- {
- portENTER_CRITICAL_SAFE(&s_timer_lock);
- }
- static IRAM_ATTR void timer_list_unlock(void)
- {
- portEXIT_CRITICAL_SAFE(&s_timer_lock);
- }
- static void timer_process_alarm(esp_timer_dispatch_t dispatch_method)
- {
- /* unused, provision to allow running callbacks from ISR */
- (void) dispatch_method;
- timer_list_lock();
- esp_timer_handle_t it;
- while (1) {
- it = LIST_FIRST(&s_timers);
- int64_t now = esp_timer_impl_get_time();
- if (it == NULL || it->alarm > now) {
- break;
- }
- LIST_REMOVE(it, list_entry);
- if (it->event_id == EVENT_ID_DELETE_TIMER) {
- free(it);
- it = NULL;
- } else {
- 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);
- } 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();
- (*callback)(arg);
- timer_list_lock();
- #if WITH_PROFILING
- it->times_triggered++;
- it->total_callback_run_time += esp_timer_impl_get_time() - callback_start;
- #endif
- }
- }
- if (it) {
- esp_timer_impl_set_alarm(it->alarm);
- }
- timer_list_unlock();
- }
- static void timer_task(void* arg)
- {
- while (true){
- ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
- // all deferred events are processed at a time
- timer_process_alarm(ESP_TIMER_TASK);
- }
- }
- static void IRAM_ATTR timer_alarm_handler(void* arg)
- {
- BaseType_t xHigherPriorityTaskWoken = pdFALSE;
- vTaskNotifyGiveFromISR(s_timer_task, &xHigherPriorityTaskWoken);
- if (xHigherPriorityTaskWoken == pdTRUE) {
- portYIELD_FROM_ISR();
- }
- }
- static IRAM_ATTR inline bool is_initialized(void)
- {
- return s_timer_task != NULL;
- }
- 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;
- }
- esp_err_t esp_timer_init(void)
- {
- esp_err_t err;
- if (is_initialized()) {
- return ESP_ERR_INVALID_STATE;
- }
- int ret = xTaskCreatePinnedToCore(&timer_task, "esp_timer",
- ESP_TASK_TIMER_STACK, NULL, ESP_TASK_TIMER_PRIO, &s_timer_task, PRO_CPU_NUM);
- if (ret != pdPASS) {
- err = ESP_ERR_NO_MEM;
- goto out;
- }
- err = esp_timer_impl_init(&timer_alarm_handler);
- if (err != ESP_OK) {
- goto out;
- }
- return ESP_OK;
- out:
- if (s_timer_task) {
- vTaskDelete(s_timer_task);
- s_timer_task = NULL;
- }
- return ESP_ERR_NO_MEM;
- }
- esp_err_t esp_timer_deinit(void)
- {
- if (!is_initialized()) {
- return ESP_ERR_INVALID_STATE;
- }
- /* Check if there are any active timers */
- if (!LIST_EMPTY(&s_timers)) {
- 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
- if (!LIST_EMPTY(&s_inactive_timers)) {
- return ESP_ERR_INVALID_STATE;
- }
- #endif
- esp_timer_impl_deinit();
- vTaskDelete(s_timer_task);
- s_timer_task = NULL;
- 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, "%-12s ", t->name);
- } else {
- cb = snprintf(*dst, *dst_size, "timer@%p ", t);
- }
- cb += snprintf(*dst + cb, *dst_size + cb, "%12lld %12lld %9d %9d %6d %12lld\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 90
- #else
- size_t cb = snprintf(*dst, *dst_size, "timer@%p %12lld %12lld\n", t, (uint64_t)t->period, t->alarm);
- #define TIMER_INFO_LINE_LEN 46
- #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;
- timer_list_lock();
- LIST_FOREACH(it, &s_timers, list_entry) {
- ++timer_count;
- }
- #if WITH_PROFILING
- LIST_FOREACH(it, &s_inactive_timers, list_entry) {
- ++timer_count;
- }
- #endif
- timer_list_unlock();
- /* 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 */
- timer_list_lock();
- char* pos = print_buf;
- LIST_FOREACH(it, &s_timers, list_entry) {
- print_timer_info(it, &pos, &buf_size);
- }
- #if WITH_PROFILING
- LIST_FOREACH(it, &s_inactive_timers, list_entry) {
- print_timer_info(it, &pos, &buf_size);
- }
- #endif
- timer_list_unlock();
- /* Print the buffer */
- fputs(print_buf, stream);
- free(print_buf);
- return ESP_OK;
- }
- int64_t IRAM_ATTR esp_timer_get_next_alarm(void)
- {
- int64_t next_alarm = INT64_MAX;
- timer_list_lock();
- esp_timer_handle_t it = LIST_FIRST(&s_timers);
- if (it) {
- next_alarm = it->alarm;
- }
- timer_list_unlock();
- return next_alarm;
- }
- bool esp_timer_is_active(esp_timer_handle_t timer)
- {
- return timer_armed(timer);
- }
|