mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
refactor(esp_timer): Remove duplicate code and move it to a common file
This commit is contained in:
parent
bfcad8d8a2
commit
5693e0188b
@ -46,13 +46,11 @@ entries:
|
||||
archive: libesp_timer.a
|
||||
entries:
|
||||
if PM_SLP_IRAM_OPT = y:
|
||||
esp_timer_impl_common:esp_timer_impl_lock (noflash)
|
||||
esp_timer_impl_common:esp_timer_impl_unlock (noflash)
|
||||
if ESP_TIMER_IMPL_TG0_LAC = y:
|
||||
esp_timer_impl_lac:esp_timer_impl_lock (noflash)
|
||||
esp_timer_impl_lac:esp_timer_impl_unlock (noflash)
|
||||
esp_timer_impl_lac:esp_timer_impl_advance (noflash)
|
||||
elif ESP_TIMER_IMPL_SYSTIMER = y:
|
||||
esp_timer_impl_systimer:esp_timer_impl_lock (noflash)
|
||||
esp_timer_impl_systimer:esp_timer_impl_unlock (noflash)
|
||||
esp_timer_impl_systimer:esp_timer_impl_advance (noflash)
|
||||
|
||||
[mapping:newlib_pm]
|
||||
|
@ -2,7 +2,8 @@ idf_build_get_property(target IDF_TARGET)
|
||||
|
||||
set(srcs "src/esp_timer.c"
|
||||
"src/ets_timer_legacy.c"
|
||||
"src/system_time.c")
|
||||
"src/system_time.c"
|
||||
"src/esp_timer_impl_common.c")
|
||||
|
||||
if(CONFIG_ESP_TIMER_IMPL_TG0_LAC)
|
||||
list(APPEND srcs "src/esp_timer_impl_lac.c")
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@ -142,3 +142,12 @@ uint64_t esp_timer_impl_get_alarm_reg(void);
|
||||
*/
|
||||
void esp_timer_impl_init_system_time(void);
|
||||
#endif
|
||||
|
||||
#if CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
|
||||
/**
|
||||
* @brief Set the next alarm if there is such an alarm in the cached array.
|
||||
*
|
||||
* @note Available only when CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD is enabled.
|
||||
*/
|
||||
void esp_timer_impl_try_to_set_next_alarm(void);
|
||||
#endif
|
||||
|
@ -492,6 +492,7 @@ static void IRAM_ATTR timer_alarm_handler(void* arg)
|
||||
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;
|
||||
|
70
components/esp_timer/src/esp_timer_impl_common.c
Normal file
70
components/esp_timer/src/esp_timer_impl_common.c
Normal file
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023 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);
|
||||
}
|
||||
|
||||
void esp_timer_private_lock(void) __attribute__((alias("esp_timer_impl_lock")));
|
||||
void esp_timer_private_unlock(void) __attribute__((alias("esp_timer_impl_unlock")));
|
||||
|
||||
void 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 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 IRAM_ATTR esp_timer_impl_get_min_period_us(void)
|
||||
{
|
||||
return 50;
|
||||
}
|
@ -99,20 +99,10 @@ static intr_handle_t s_timer_interrupt_handle[ISR_HANDLERS] = { NULL };
|
||||
static intr_handler_t s_alarm_handler = NULL;
|
||||
|
||||
/* Spinlock used to protect access to the hardware registers. */
|
||||
portMUX_TYPE s_time_update_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||
extern portMUX_TYPE s_time_update_lock;
|
||||
|
||||
/* Alarm values to generate interrupt on match */
|
||||
static 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);
|
||||
}
|
||||
extern uint64_t timestamp_id[2];
|
||||
|
||||
uint64_t IRAM_ATTR esp_timer_impl_get_counter_reg(void)
|
||||
{
|
||||
@ -182,47 +172,12 @@ void IRAM_ATTR esp_timer_impl_set_alarm_id(uint64_t timestamp, unsigned alarm_id
|
||||
portEXIT_CRITICAL_SAFE(&s_time_update_lock);
|
||||
}
|
||||
|
||||
void 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
|
||||
static void IRAM_ATTR 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);
|
||||
}
|
||||
#else
|
||||
#define try_to_set_next_alarm()
|
||||
#endif
|
||||
|
||||
static void IRAM_ATTR timer_alarm_isr(void *arg)
|
||||
{
|
||||
#if ISR_HANDLERS == 1
|
||||
/* Clear interrupt status */
|
||||
REG_WRITE(INT_CLR_REG, TIMG_LACT_INT_CLR);
|
||||
|
||||
try_to_set_next_alarm();
|
||||
|
||||
/* Call the upper layer handler */
|
||||
(*s_alarm_handler)(arg);
|
||||
#else
|
||||
@ -243,7 +198,6 @@ static void IRAM_ATTR timer_alarm_isr(void *arg)
|
||||
REG_WRITE(INT_CLR_REG, TIMG_LACT_INT_CLR);
|
||||
portEXIT_CRITICAL_ISR(&s_time_update_lock);
|
||||
|
||||
try_to_set_next_alarm();
|
||||
(*s_alarm_handler)(arg);
|
||||
|
||||
portENTER_CRITICAL_ISR(&s_time_update_lock);
|
||||
@ -367,12 +321,6 @@ void esp_timer_impl_deinit(void)
|
||||
s_alarm_handler = NULL;
|
||||
}
|
||||
|
||||
/* FIXME: This value is safe for 80MHz APB frequency, should be modified to depend on clock frequency. */
|
||||
uint64_t IRAM_ATTR esp_timer_impl_get_min_period_us(void)
|
||||
{
|
||||
return 50;
|
||||
}
|
||||
|
||||
uint64_t esp_timer_impl_get_alarm_reg(void)
|
||||
{
|
||||
portENTER_CRITICAL_SAFE(&s_time_update_lock);
|
||||
@ -387,5 +335,3 @@ uint64_t esp_timer_impl_get_alarm_reg(void)
|
||||
void esp_timer_private_update_apb_freq(uint32_t apb_ticks_per_us) __attribute__((alias("esp_timer_impl_update_apb_freq")));
|
||||
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")));
|
||||
void esp_timer_private_lock(void) __attribute__((alias("esp_timer_impl_lock")));
|
||||
void esp_timer_private_unlock(void) __attribute__((alias("esp_timer_impl_unlock")));
|
||||
|
@ -55,20 +55,10 @@ static intr_handler_t s_alarm_handler = NULL;
|
||||
static systimer_hal_context_t systimer_hal;
|
||||
|
||||
/* Spinlock used to protect access to the hardware registers. */
|
||||
static portMUX_TYPE s_time_update_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||
extern portMUX_TYPE s_time_update_lock;
|
||||
|
||||
/* Alarm values to generate interrupt on match */
|
||||
static 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);
|
||||
}
|
||||
extern uint64_t timestamp_id[2];
|
||||
|
||||
uint64_t IRAM_ATTR esp_timer_impl_get_counter_reg(void)
|
||||
{
|
||||
@ -94,46 +84,11 @@ void IRAM_ATTR esp_timer_impl_set_alarm_id(uint64_t timestamp, unsigned alarm_id
|
||||
portEXIT_CRITICAL_SAFE(&s_time_update_lock);
|
||||
}
|
||||
|
||||
void 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
|
||||
static void IRAM_ATTR 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);
|
||||
}
|
||||
#else
|
||||
#define try_to_set_next_alarm()
|
||||
#endif
|
||||
|
||||
static void 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);
|
||||
|
||||
try_to_set_next_alarm();
|
||||
/* Call the upper layer handler */
|
||||
(*s_alarm_handler)(arg);
|
||||
#else
|
||||
@ -154,7 +109,6 @@ static void IRAM_ATTR timer_alarm_isr(void *arg)
|
||||
systimer_ll_clear_alarm_int(systimer_hal.dev, SYSTIMER_ALARM_ESPTIMER);
|
||||
portEXIT_CRITICAL_ISR(&s_time_update_lock);
|
||||
|
||||
try_to_set_next_alarm();
|
||||
(*s_alarm_handler)(arg);
|
||||
|
||||
portENTER_CRITICAL_ISR(&s_time_update_lock);
|
||||
@ -278,11 +232,6 @@ void esp_timer_impl_deinit(void)
|
||||
s_alarm_handler = NULL;
|
||||
}
|
||||
|
||||
uint64_t IRAM_ATTR esp_timer_impl_get_min_period_us(void)
|
||||
{
|
||||
return 50;
|
||||
}
|
||||
|
||||
uint64_t esp_timer_impl_get_alarm_reg(void)
|
||||
{
|
||||
portENTER_CRITICAL_SAFE(&s_time_update_lock);
|
||||
@ -294,5 +243,3 @@ uint64_t esp_timer_impl_get_alarm_reg(void)
|
||||
void esp_timer_private_update_apb_freq(uint32_t apb_ticks_per_us) __attribute__((alias("esp_timer_impl_update_apb_freq")));
|
||||
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")));
|
||||
void esp_timer_private_lock(void) __attribute__((alias("esp_timer_impl_lock")));
|
||||
void esp_timer_private_unlock(void) __attribute__((alias("esp_timer_impl_unlock")));
|
||||
|
Loading…
Reference in New Issue
Block a user