2017-08-07 16:21:19 -04:00
|
|
|
// Copyright 2017 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2018-03-29 23:39:42 -04:00
|
|
|
#include <string.h>
|
global: move the soc component out of the common list
This MR removes the common dependency from every IDF components to the SOC component.
Currently, in the ``idf_functions.cmake`` script, we include the header path of SOC component by default for all components.
But for better code organization (or maybe also benifits to the compiling speed), we may remove the dependency to SOC components for most components except the driver and kernel related components.
In CMAKE, we have two kinds of header visibilities (set by include path visibility):
(Assume component A --(depends on)--> B, B is the current component)
1. public (``COMPONENT_ADD_INCLUDEDIRS``): means this path is visible to other depending components (A) (visible to A and B)
2. private (``COMPONENT_PRIV_INCLUDEDIRS``): means this path is only visible to source files inside the component (visible to B only)
and we have two kinds of depending ways:
(Assume component A --(depends on)--> B --(depends on)--> C, B is the current component)
1. public (```COMPONENT_REQUIRES```): means B can access to public include path of C. All other components rely on you (A) will also be available for the public headers. (visible to A, B)
2. private (``COMPONENT_PRIV_REQUIRES``): means B can access to public include path of C, but don't propagate this relation to other components (A). (visible to B)
1. remove the common requirement in ``idf_functions.cmake``, this makes the SOC components invisible to all other components by default.
2. if a component (for example, DRIVER) really needs the dependency to SOC, add a private dependency to SOC for it.
3. some other components that don't really depends on the SOC may still meet some errors saying "can't find header soc/...", this is because it's depended component (DRIVER) incorrectly include the header of SOC in its public headers. Moving all this kind of #include into source files, or private headers
4. Fix the include requirements for some file which miss sufficient #include directives. (Previously they include some headers by the long long long header include link)
This is a breaking change. Previous code may depends on the long include chain.
You may need to include the following headers for some files after this commit:
- soc/soc.h
- soc/soc_memory_layout.h
- driver/gpio.h
- esp_sleep.h
The major broken include chain includes:
1. esp_system.h no longer includes esp_sleep.h. The latter includes driver/gpio.h and driver/touch_pad.h.
2. ets_sys.h no longer includes soc/soc.h
3. freertos/portmacro.h no longer includes soc/soc_memory_layout.h
some peripheral headers no longer includes their hw related headers, e.g. rom/gpio.h no longer includes soc/gpio_pins.h and soc/gpio_reg.h
BREAKING CHANGE
2019-04-03 01:17:38 -04:00
|
|
|
#include "soc/soc.h"
|
2017-08-07 16:21:19 -04:00
|
|
|
#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"
|
2020-11-05 23:00:07 -05:00
|
|
|
#include "freertos/semphr.h"
|
2020-07-14 02:46:13 -04:00
|
|
|
#include "soc/spinlock.h"
|
2020-02-06 01:00:18 -05:00
|
|
|
#include "esp_timer.h"
|
|
|
|
#include "esp_timer_impl.h"
|
2020-07-14 02:46:13 -04:00
|
|
|
|
|
|
|
#include "esp_private/startup_internal.h"
|
|
|
|
#include "esp_private/esp_timer_private.h"
|
2020-08-11 05:49:48 -04:00
|
|
|
#include "esp_private/system_internal.h"
|
2020-07-14 02:46:13 -04:00
|
|
|
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
#include "esp32/rtc.h"
|
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
#include "esp32s2/rtc.h"
|
2020-10-20 02:09:32 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
#include "esp32s3/rtc.h"
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
#include "esp32c3/rtc.h"
|
2020-07-14 02:46:13 -04:00
|
|
|
#endif
|
|
|
|
|
2017-08-07 16:21:19 -04:00
|
|
|
#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
|
2019-03-14 05:29:32 -04:00
|
|
|
#include "sys/queue.h"
|
2017-08-07 16:21:19 -04:00
|
|
|
|
2019-05-20 07:07:28 -04:00
|
|
|
#define EVENT_ID_DELETE_TIMER 0xF0DE1E1E
|
|
|
|
|
2020-11-13 03:46:21 -05:00
|
|
|
typedef enum {
|
2020-02-10 08:45:09 -05:00
|
|
|
FL_ISR_DISPATCH_METHOD = (1 << 0), //!< 0=Callback is called from timer task, 1=Callback is called from timer ISR
|
2020-11-13 03:46:21 -05:00
|
|
|
FL_SKIP_UNHANDLED_EVENTS = (1 << 1), //!< 0=NOT skip unhandled events for periodic timers, 1=Skip unhandled events for periodic timers
|
|
|
|
} flags_t;
|
|
|
|
|
2017-08-07 16:21:19 -04:00
|
|
|
struct esp_timer {
|
|
|
|
uint64_t alarm;
|
2020-11-13 03:46:21 -05:00
|
|
|
uint64_t period:56;
|
|
|
|
flags_t flags:8;
|
2019-05-20 07:07:28 -04:00
|
|
|
union {
|
|
|
|
esp_timer_cb_t callback;
|
|
|
|
uint32_t event_id;
|
|
|
|
};
|
2017-08-07 16:21:19 -04:00
|
|
|
void* arg;
|
|
|
|
#if WITH_PROFILING
|
|
|
|
const char* name;
|
|
|
|
size_t times_triggered;
|
|
|
|
size_t times_armed;
|
2020-11-13 03:46:21 -05:00
|
|
|
size_t times_skipped;
|
2017-08-07 16:21:19 -04:00
|
|
|
uint64_t total_callback_run_time;
|
|
|
|
#endif // WITH_PROFILING
|
|
|
|
LIST_ENTRY(esp_timer) list_entry;
|
|
|
|
};
|
|
|
|
|
2020-08-18 04:15:53 -04:00
|
|
|
static inline bool is_initialized(void);
|
2020-02-10 08:45:09 -05:00
|
|
|
static esp_err_t timer_insert(esp_timer_handle_t timer, bool without_update_alarm);
|
2017-08-07 16:21:19 -04:00
|
|
|
static esp_err_t timer_remove(esp_timer_handle_t timer);
|
|
|
|
static bool timer_armed(esp_timer_handle_t timer);
|
2020-02-10 08:45:09 -05:00
|
|
|
static void timer_list_lock(esp_timer_dispatch_t timer_type);
|
|
|
|
static void timer_list_unlock(esp_timer_dispatch_t timer_type);
|
2017-08-07 16:21:19 -04:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
2020-10-26 06:07:34 -04:00
|
|
|
__attribute__((unused)) static const char* TAG = "esp_timer";
|
2017-08-07 16:21:19 -04:00
|
|
|
|
2020-02-10 08:45:09 -05:00
|
|
|
// 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)
|
|
|
|
};
|
2017-08-07 16:21:19 -04:00
|
|
|
#if WITH_PROFILING
|
2020-02-10 08:45:09 -05:00
|
|
|
// 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)
|
|
|
|
};
|
2017-08-07 16:21:19 -04:00
|
|
|
#endif
|
|
|
|
// task used to dispatch timer callbacks
|
|
|
|
static TaskHandle_t s_timer_task;
|
2018-03-29 23:39:42 -04:00
|
|
|
|
2019-05-20 07:07:28 -04:00
|
|
|
// lock protecting s_timers, s_inactive_timers
|
2020-02-10 08:45:09 -05:00
|
|
|
static portMUX_TYPE s_timer_lock[ESP_TIMER_MAX] = {
|
|
|
|
[0 ... (ESP_TIMER_MAX - 1)] = portMUX_INITIALIZER_UNLOCKED
|
|
|
|
};
|
2017-08-07 16:21:19 -04:00
|
|
|
|
2020-02-10 08:45:09 -05:00
|
|
|
#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
|
2017-08-07 16:21:19 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-02-10 08:45:09 -05:00
|
|
|
if (args == NULL || args->callback == NULL || out_handle == NULL ||
|
|
|
|
args->dispatch_method < 0 || args->dispatch_method >= ESP_TIMER_MAX) {
|
2017-08-07 16:21:19 -04:00
|
|
|
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;
|
2020-02-10 08:45:09 -05:00
|
|
|
result->flags = (args->dispatch_method ? FL_ISR_DISPATCH_METHOD : 0) |
|
2020-11-13 03:46:21 -05:00
|
|
|
(args->skip_unhandled_events ? FL_SKIP_UNHANDLED_EVENTS : 0);
|
2017-08-07 16:21:19 -04:00
|
|
|
#if WITH_PROFILING
|
|
|
|
result->name = args->name;
|
2020-02-10 08:45:09 -05:00
|
|
|
esp_timer_dispatch_t dispatch_method = result->flags & FL_ISR_DISPATCH_METHOD;
|
|
|
|
timer_list_lock(dispatch_method);
|
2017-08-07 16:21:19 -04:00
|
|
|
timer_insert_inactive(result);
|
2020-02-10 08:45:09 -05:00
|
|
|
timer_list_unlock(dispatch_method);
|
2017-08-07 16:21:19 -04:00
|
|
|
#endif
|
|
|
|
*out_handle = result;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-16 07:16:20 -04:00
|
|
|
esp_err_t IRAM_ATTR esp_timer_start_once(esp_timer_handle_t timer, uint64_t timeout_us)
|
2017-08-07 16:21:19 -04:00
|
|
|
{
|
2019-08-15 23:00:21 -04:00
|
|
|
if (timer == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
if (!is_initialized() || timer_armed(timer)) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2020-02-10 08:45:09 -05:00
|
|
|
int64_t alarm = esp_timer_get_time() + timeout_us;
|
|
|
|
esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD;
|
|
|
|
timer_list_lock(dispatch_method);
|
|
|
|
timer->alarm = alarm;
|
2017-08-07 16:21:19 -04:00
|
|
|
timer->period = 0;
|
|
|
|
#if WITH_PROFILING
|
|
|
|
timer->times_armed++;
|
|
|
|
#endif
|
2020-02-10 08:45:09 -05:00
|
|
|
esp_err_t err = timer_insert(timer, false);
|
|
|
|
timer_list_unlock(dispatch_method);
|
2019-09-06 08:17:25 -04:00
|
|
|
return err;
|
2017-08-07 16:21:19 -04:00
|
|
|
}
|
|
|
|
|
2017-10-16 07:16:20 -04:00
|
|
|
esp_err_t IRAM_ATTR esp_timer_start_periodic(esp_timer_handle_t timer, uint64_t period_us)
|
2017-08-07 16:21:19 -04:00
|
|
|
{
|
2019-08-15 23:00:21 -04:00
|
|
|
if (timer == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
if (!is_initialized() || timer_armed(timer)) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
period_us = MAX(period_us, esp_timer_impl_get_min_period_us());
|
2020-02-10 08:45:09 -05:00
|
|
|
int64_t alarm = esp_timer_get_time() + period_us;
|
|
|
|
esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD;
|
|
|
|
timer_list_lock(dispatch_method);
|
|
|
|
timer->alarm = alarm;
|
2017-08-07 16:21:19 -04:00
|
|
|
timer->period = period_us;
|
|
|
|
#if WITH_PROFILING
|
|
|
|
timer->times_armed++;
|
2020-11-13 03:46:21 -05:00
|
|
|
timer->times_skipped = 0;
|
2017-08-07 16:21:19 -04:00
|
|
|
#endif
|
2020-02-10 08:45:09 -05:00
|
|
|
esp_err_t err = timer_insert(timer, false);
|
|
|
|
timer_list_unlock(dispatch_method);
|
2019-09-06 08:17:25 -04:00
|
|
|
return err;
|
2017-08-07 16:21:19 -04:00
|
|
|
}
|
|
|
|
|
2017-10-16 07:16:20 -04:00
|
|
|
esp_err_t IRAM_ATTR esp_timer_stop(esp_timer_handle_t timer)
|
2017-08-07 16:21:19 -04:00
|
|
|
{
|
2019-08-15 23:00:21 -04:00
|
|
|
if (timer == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
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)
|
|
|
|
{
|
2018-12-19 02:53:50 -05:00
|
|
|
if (timer == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
if (timer_armed(timer)) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2020-02-10 08:45:09 -05:00
|
|
|
// 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.
|
|
|
|
int64_t alarm = esp_timer_get_time();
|
|
|
|
timer_list_lock(ESP_TIMER_TASK);
|
|
|
|
timer->flags &= ~FL_ISR_DISPATCH_METHOD;
|
2019-05-20 07:07:28 -04:00
|
|
|
timer->event_id = EVENT_ID_DELETE_TIMER;
|
2020-02-10 08:45:09 -05:00
|
|
|
timer->alarm = alarm;
|
2019-05-20 07:07:28 -04:00
|
|
|
timer->period = 0;
|
2020-02-10 08:45:09 -05:00
|
|
|
timer_insert(timer, false);
|
|
|
|
timer_list_unlock(ESP_TIMER_TASK);
|
2017-08-07 16:21:19 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2020-02-10 08:45:09 -05:00
|
|
|
static IRAM_ATTR esp_err_t timer_insert(esp_timer_handle_t timer, bool without_update_alarm)
|
2017-08-07 16:21:19 -04:00
|
|
|
{
|
|
|
|
#if WITH_PROFILING
|
|
|
|
timer_remove_inactive(timer);
|
|
|
|
#endif
|
|
|
|
esp_timer_handle_t it, last = NULL;
|
2020-02-10 08:45:09 -05:00
|
|
|
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);
|
2017-08-07 16:21:19 -04:00
|
|
|
} else {
|
2020-02-10 08:45:09 -05:00
|
|
|
LIST_FOREACH(it, &s_timers[dispatch_method], list_entry) {
|
2017-08-07 16:21:19 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 08:45:09 -05:00
|
|
|
if (without_update_alarm == false && timer == LIST_FIRST(&s_timers[dispatch_method])) {
|
|
|
|
esp_timer_impl_set_alarm_id(timer->alarm, dispatch_method);
|
2017-08-07 16:21:19 -04:00
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-16 07:16:20 -04:00
|
|
|
static IRAM_ATTR esp_err_t timer_remove(esp_timer_handle_t timer)
|
2017-08-07 16:21:19 -04:00
|
|
|
{
|
2020-02-10 08:45:09 -05:00
|
|
|
esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD;
|
|
|
|
timer_list_lock(dispatch_method);
|
|
|
|
esp_timer_handle_t first_timer = LIST_FIRST(&s_timers[dispatch_method]);
|
2017-08-07 16:21:19 -04:00
|
|
|
LIST_REMOVE(timer, list_entry);
|
|
|
|
timer->alarm = 0;
|
|
|
|
timer->period = 0;
|
2020-02-10 08:45:09 -05:00
|
|
|
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);
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
#if WITH_PROFILING
|
|
|
|
timer_insert_inactive(timer);
|
|
|
|
#endif
|
2020-02-10 08:45:09 -05:00
|
|
|
timer_list_unlock(dispatch_method);
|
2017-08-07 16:21:19 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if WITH_PROFILING
|
|
|
|
|
2017-10-16 07:16:20 -04:00
|
|
|
static IRAM_ATTR void timer_insert_inactive(esp_timer_handle_t timer)
|
2017-08-07 16:21:19 -04:00
|
|
|
{
|
|
|
|
/* May be locked or not, depending on where this is called from.
|
|
|
|
* Lock recursively.
|
|
|
|
*/
|
2020-02-10 08:45:09 -05:00
|
|
|
esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD;
|
|
|
|
esp_timer_handle_t head = LIST_FIRST(&s_inactive_timers[dispatch_method]);
|
2017-08-07 16:21:19 -04:00
|
|
|
if (head == NULL) {
|
2020-02-10 08:45:09 -05:00
|
|
|
LIST_INSERT_HEAD(&s_inactive_timers[dispatch_method], timer, list_entry);
|
2017-08-07 16:21:19 -04:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 07:16:20 -04:00
|
|
|
static IRAM_ATTR void timer_remove_inactive(esp_timer_handle_t timer)
|
2017-08-07 16:21:19 -04:00
|
|
|
{
|
|
|
|
LIST_REMOVE(timer, list_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // WITH_PROFILING
|
|
|
|
|
2017-10-16 07:16:20 -04:00
|
|
|
static IRAM_ATTR bool timer_armed(esp_timer_handle_t timer)
|
2017-08-07 16:21:19 -04:00
|
|
|
{
|
|
|
|
return timer->alarm > 0;
|
|
|
|
}
|
|
|
|
|
2020-02-10 08:45:09 -05:00
|
|
|
static IRAM_ATTR void timer_list_lock(esp_timer_dispatch_t timer_type)
|
2017-08-07 16:21:19 -04:00
|
|
|
{
|
2020-02-10 08:45:09 -05:00
|
|
|
portENTER_CRITICAL_SAFE(&s_timer_lock[timer_type]);
|
2017-08-07 16:21:19 -04:00
|
|
|
}
|
|
|
|
|
2020-02-10 08:45:09 -05:00
|
|
|
static IRAM_ATTR void timer_list_unlock(esp_timer_dispatch_t timer_type)
|
2017-08-07 16:21:19 -04:00
|
|
|
{
|
2020-02-10 08:45:09 -05:00
|
|
|
portEXIT_CRITICAL_SAFE(&s_timer_lock[timer_type]);
|
2017-08-07 16:21:19 -04:00
|
|
|
}
|
|
|
|
|
2020-02-10 08:45:09 -05:00
|
|
|
#ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
|
|
|
static 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
|
2017-08-07 16:21:19 -04:00
|
|
|
{
|
2020-02-10 08:45:09 -05:00
|
|
|
timer_list_lock(dispatch_method);
|
|
|
|
bool processed = false;
|
2020-11-13 03:46:21 -05:00
|
|
|
esp_timer_handle_t it;
|
|
|
|
while (1) {
|
2020-02-10 08:45:09 -05:00
|
|
|
it = LIST_FIRST(&s_timers[dispatch_method]);
|
2020-11-13 03:46:21 -05:00
|
|
|
int64_t now = esp_timer_impl_get_time();
|
|
|
|
if (it == NULL || it->alarm > now) {
|
|
|
|
break;
|
|
|
|
}
|
2020-02-10 08:45:09 -05:00
|
|
|
processed = true;
|
2017-08-07 16:21:19 -04:00
|
|
|
LIST_REMOVE(it, list_entry);
|
2019-05-20 07:07:28 -04:00
|
|
|
if (it->event_id == EVENT_ID_DELETE_TIMER) {
|
2020-02-10 08:45:09 -05:00
|
|
|
// 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.
|
2019-05-20 07:07:28 -04:00
|
|
|
free(it);
|
2020-11-13 03:46:21 -05:00
|
|
|
it = NULL;
|
2017-08-07 16:21:19 -04:00
|
|
|
} else {
|
2020-11-13 03:46:21 -05:00
|
|
|
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;
|
2017-08-07 16:21:19 -04:00
|
|
|
#if WITH_PROFILING
|
2020-11-13 03:46:21 -05:00
|
|
|
it->times_skipped += skipped;
|
2017-08-07 16:21:19 -04:00
|
|
|
#endif
|
2020-11-13 03:46:21 -05:00
|
|
|
} else {
|
|
|
|
it->alarm += it->period;
|
|
|
|
}
|
2020-02-10 08:45:09 -05:00
|
|
|
timer_insert(it, true);
|
2020-11-13 03:46:21 -05:00
|
|
|
} else {
|
|
|
|
it->alarm = 0;
|
|
|
|
#if WITH_PROFILING
|
|
|
|
timer_insert_inactive(it);
|
|
|
|
#endif
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
#if WITH_PROFILING
|
2020-11-13 03:46:21 -05:00
|
|
|
uint64_t callback_start = now;
|
2017-08-07 16:21:19 -04:00
|
|
|
#endif
|
2020-11-13 03:46:21 -05:00
|
|
|
esp_timer_cb_t callback = it->callback;
|
|
|
|
void* arg = it->arg;
|
2020-02-10 08:45:09 -05:00
|
|
|
timer_list_unlock(dispatch_method);
|
2020-11-13 03:46:21 -05:00
|
|
|
(*callback)(arg);
|
2020-02-10 08:45:09 -05:00
|
|
|
timer_list_lock(dispatch_method);
|
2017-08-07 16:21:19 -04:00
|
|
|
#if WITH_PROFILING
|
2020-11-13 03:46:21 -05:00
|
|
|
it->times_triggered++;
|
|
|
|
it->total_callback_run_time += esp_timer_impl_get_time() - callback_start;
|
2017-08-07 16:21:19 -04:00
|
|
|
#endif
|
2020-11-13 03:46:21 -05:00
|
|
|
}
|
2020-02-10 08:45:09 -05:00
|
|
|
} // while(1)
|
2020-11-13 03:46:21 -05:00
|
|
|
if (it) {
|
2020-02-10 08:45:09 -05:00
|
|
|
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);
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
}
|
2020-02-10 08:45:09 -05:00
|
|
|
timer_list_unlock(dispatch_method);
|
|
|
|
return processed;
|
2017-08-07 16:21:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void timer_task(void* arg)
|
|
|
|
{
|
|
|
|
while (true){
|
2020-10-26 06:07:34 -04:00
|
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
|
|
// all deferred events are processed at a time
|
2017-08-07 16:21:19 -04:00
|
|
|
timer_process_alarm(ESP_TIMER_TASK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 08:45:09 -05:00
|
|
|
#ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
|
|
|
IRAM_ATTR void esp_timer_isr_dispatch_need_yield(void)
|
|
|
|
{
|
|
|
|
assert(xPortInIsrContext());
|
|
|
|
s_isr_dispatch_need_yield = pdTRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-08-07 16:21:19 -04:00
|
|
|
static void IRAM_ATTR timer_alarm_handler(void* arg)
|
|
|
|
{
|
2020-10-26 06:07:34 -04:00
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
2020-02-10 08:45:09 -05:00
|
|
|
bool isr_timers_processed = false;
|
|
|
|
|
|
|
|
#ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
|
|
|
// 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);
|
|
|
|
}
|
2020-10-26 06:07:34 -04:00
|
|
|
if (xHigherPriorityTaskWoken == pdTRUE) {
|
2017-08-07 16:21:19 -04:00
|
|
|
portYIELD_FROM_ISR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 04:15:53 -04:00
|
|
|
static IRAM_ATTR inline bool is_initialized(void)
|
2017-08-07 16:21:19 -04:00
|
|
|
{
|
|
|
|
return s_timer_task != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_timer_init(void)
|
|
|
|
{
|
2018-12-19 02:53:50 -05:00
|
|
|
esp_err_t err;
|
2017-08-07 16:21:19 -04:00
|
|
|
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) {
|
2018-12-19 02:53:50 -05:00
|
|
|
err = ESP_ERR_NO_MEM;
|
|
|
|
goto out;
|
2017-08-07 16:21:19 -04:00
|
|
|
}
|
|
|
|
|
2018-12-19 02:53:50 -05:00
|
|
|
err = esp_timer_impl_init(&timer_alarm_handler);
|
2017-08-07 16:21:19 -04:00
|
|
|
if (err != ESP_OK) {
|
2018-12-19 02:53:50 -05:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-10-20 02:09:32 -04:00
|
|
|
#if CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
|
2021-03-08 10:01:40 -05:00
|
|
|
esp_timer_impl_init_system_time();
|
2020-07-14 02:46:13 -04:00
|
|
|
#endif
|
2020-06-23 04:46:06 -04:00
|
|
|
|
2018-12-19 02:53:50 -05:00
|
|
|
return ESP_OK;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (s_timer_task) {
|
2017-08-07 16:21:19 -04:00
|
|
|
vTaskDelete(s_timer_task);
|
|
|
|
s_timer_task = NULL;
|
2018-12-19 02:53:50 -05:00
|
|
|
}
|
2020-10-26 06:07:34 -04:00
|
|
|
|
2018-12-19 02:53:50 -05:00
|
|
|
return ESP_ERR_NO_MEM;
|
2017-08-07 16:21:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_timer_deinit(void)
|
|
|
|
{
|
|
|
|
if (!is_initialized()) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if there are any active timers */
|
2020-02-10 08:45:09 -05:00
|
|
|
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;
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We can only check if there are any timers which are not deleted if
|
|
|
|
* profiling is enabled.
|
|
|
|
*/
|
|
|
|
#if WITH_PROFILING
|
2020-02-10 08:45:09 -05:00
|
|
|
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;
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
}
|
|
|
|
#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
|
2020-11-13 03:46:21 -05:00
|
|
|
size_t cb;
|
|
|
|
// name is optional, might be missed.
|
|
|
|
if (t->name) {
|
2021-01-18 10:55:11 -05:00
|
|
|
cb = snprintf(*dst, *dst_size, "%-20.20s ", t->name);
|
2020-11-13 03:46:21 -05:00
|
|
|
} else {
|
2021-01-18 10:55:11 -05:00
|
|
|
cb = snprintf(*dst, *dst_size, "timer@%-10p ", t);
|
2020-11-13 03:46:21 -05:00
|
|
|
}
|
2021-01-18 10:55:11 -05:00
|
|
|
cb += snprintf(*dst + cb, *dst_size + cb, "%-10lld %-12lld %-12d %-12d %-12d %-12lld\n",
|
2020-11-13 03:46:21 -05:00
|
|
|
(uint64_t)t->period, t->alarm, t->times_armed,
|
|
|
|
t->times_triggered, t->times_skipped, t->total_callback_run_time);
|
2017-08-07 16:21:19 -04:00
|
|
|
/* keep this in sync with the format string, used in esp_timer_dump */
|
2020-11-13 03:46:21 -05:00
|
|
|
#define TIMER_INFO_LINE_LEN 90
|
2017-08-07 16:21:19 -04:00
|
|
|
#else
|
2021-01-18 10:55:11 -05:00
|
|
|
size_t cb = snprintf(*dst, *dst_size, "timer@%-14p %-10lld %-12lld\n", t, (uint64_t)t->period, t->alarm);
|
2017-08-07 16:21:19 -04:00
|
|
|
#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;
|
2020-02-10 08:45:09 -05:00
|
|
|
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;
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
#if WITH_PROFILING
|
2020-02-10 08:45:09 -05:00
|
|
|
LIST_FOREACH(it, &s_inactive_timers[dispatch_method], list_entry) {
|
|
|
|
++timer_count;
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
#endif
|
2020-02-10 08:45:09 -05:00
|
|
|
timer_list_unlock(dispatch_method);
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
|
|
|
|
/* 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;
|
2020-02-10 08:45:09 -05:00
|
|
|
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);
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
#if WITH_PROFILING
|
2020-02-10 08:45:09 -05:00
|
|
|
LIST_FOREACH(it, &s_inactive_timers[dispatch_method], list_entry) {
|
|
|
|
print_timer_info(it, &pos, &buf_size);
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
#endif
|
2020-02-10 08:45:09 -05:00
|
|
|
timer_list_unlock(dispatch_method);
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
|
2021-02-24 03:08:17 -05:00
|
|
|
if (stream != NULL) {
|
|
|
|
fprintf(stream, "Timer stats:\n");
|
2021-01-18 10:55:11 -05:00
|
|
|
#if WITH_PROFILING
|
2021-02-24 03:08:17 -05:00
|
|
|
fprintf(stream, "%-20s %-10s %-12s %-12s %-12s %-12s %-12s\n",
|
|
|
|
"Name", "Period", "Alarm", "Times_armed", "Times_trigg", "Times_skip", "Cb_exec_time");
|
2021-01-18 10:55:11 -05:00
|
|
|
#else
|
2021-02-24 03:08:17 -05:00
|
|
|
fprintf(stream, "%-20s %-10s %-12s\n", "Name", "Period", "Alarm");
|
2021-01-18 10:55:11 -05:00
|
|
|
#endif
|
|
|
|
|
2021-02-24 03:08:17 -05:00
|
|
|
/* Print the buffer */
|
2020-02-10 08:45:09 -05:00
|
|
|
fputs(print_buf, stream);
|
|
|
|
}
|
2017-08-07 16:21:19 -04:00
|
|
|
|
|
|
|
free(print_buf);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
int64_t IRAM_ATTR esp_timer_get_next_alarm(void)
|
2018-04-03 06:12:47 -04:00
|
|
|
{
|
|
|
|
int64_t next_alarm = INT64_MAX;
|
2020-02-10 08:45:09 -05:00
|
|
|
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);
|
2018-04-03 06:12:47 -04:00
|
|
|
}
|
|
|
|
return next_alarm;
|
2020-07-14 02:46:13 -04:00
|
|
|
}
|