From cc66334014785d811c95f28dc90b35d502d6c8dd Mon Sep 17 00:00:00 2001 From: morris Date: Fri, 14 Aug 2020 11:36:58 +0800 Subject: [PATCH] esp_timer: initial support on esp32-s3 --- components/esp32s3/ld/esp32s3.peripherals.ld | 1 + components/esp_timer/src/ets_timer_legacy.c | 2 + components/esp_timer/test/test_ets_timer.c | 2 + .../hal/esp32s3/include/hal/systimer_ll.h | 73 +++++++++++-------- components/hal/esp32s3/systimer_hal.c | 7 +- 5 files changed, 53 insertions(+), 32 deletions(-) diff --git a/components/esp32s3/ld/esp32s3.peripherals.ld b/components/esp32s3/ld/esp32s3.peripherals.ld index 7d56b730e1..68815bb27f 100644 --- a/components/esp32s3/ld/esp32s3.peripherals.ld +++ b/components/esp32s3/ld/esp32s3.peripherals.ld @@ -21,6 +21,7 @@ PROVIDE ( LEDC = 0x60019000 ); PROVIDE ( MCP = 0x600c3000 ); PROVIDE ( TIMERG0 = 0x6001F000 ); PROVIDE ( TIMERG1 = 0x60020000 ); +PROVIDE ( SYS_TIMER = 0x60023000 ); PROVIDE ( GPSPI2 = 0x60024000 ); PROVIDE ( GPSPI3 = 0x60025000 ); PROVIDE ( SYSCON = 0x60026000 ); diff --git a/components/esp_timer/src/ets_timer_legacy.c b/components/esp_timer/src/ets_timer_legacy.c index 14d5f02a7d..cec5525337 100644 --- a/components/esp_timer/src/ets_timer_legacy.c +++ b/components/esp_timer/src/ets_timer_legacy.c @@ -34,6 +34,8 @@ #include "esp32/rom/ets_sys.h" // for ETSTimer type #elif CONFIG_IDF_TARGET_ESP32S2 #include "esp32s2/rom/ets_sys.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/rom/ets_sys.h" #endif /* We abuse 'timer_arg' field of ETSTimer structure to hold a pointer to esp_timer */ diff --git a/components/esp_timer/test/test_ets_timer.c b/components/esp_timer/test/test_ets_timer.c index 5d7bf34d57..75f92e830d 100644 --- a/components/esp_timer/test/test_ets_timer.c +++ b/components/esp_timer/test/test_ets_timer.c @@ -11,6 +11,8 @@ #include "esp32/rom/ets_sys.h" // for ETSTimer type #elif CONFIG_IDF_TARGET_ESP32S2 #include "esp32s2/rom/ets_sys.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/rom/ets_sys.h" #endif TEST_CASE("ets_timer produces correct delay", "[ets_timer]") diff --git a/components/hal/esp32s3/include/hal/systimer_ll.h b/components/hal/esp32s3/include/hal/systimer_ll.h index 21ee0dabcc..0da3a4a16e 100644 --- a/components/hal/esp32s3/include/hal/systimer_ll.h +++ b/components/hal/esp32s3/include/hal/systimer_ll.h @@ -37,15 +37,13 @@ __attribute__((always_inline)) static inline void systimer_ll_enable_counter(uin REG_SET_BIT(SYS_TIMER_SYSTIMER_CONF_REG, 1 << (30 - counter_id)); } -__attribute__((always_inline)) static inline void systimer_ll_apply_counter_value(uint32_t counter_id) +__attribute__((always_inline)) static inline void systimer_ll_counter_can_stall_by_cpu(uint32_t counter_id, uint32_t cpu_id, bool can) { - REG_SET_BIT(SYS_TIMER_SYSTIMER_UNIT0_OP_REG + 4 * counter_id, 1 << 31); -} - -__attribute__((always_inline)) static inline void systimer_ll_load_counter_value(uint32_t counter_id, uint64_t value) -{ - REG_WRITE(SYS_TIMER_SYSTIMER_UNIT0_LOAD_LO_REG + 8 * counter_id, value & 0xFFFFFFFF); - REG_WRITE(SYS_TIMER_SYSTIMER_UNIT0_LOAD_HI_REG, value >> 32); + if (can) { + REG_SET_BIT(SYS_TIMER_SYSTIMER_CONF_REG, 1 << ((28 - counter_id * 2) - cpu_id)); + } else { + REG_CLR_BIT(SYS_TIMER_SYSTIMER_CONF_REG, 1 << ((28 - counter_id * 2) - cpu_id)); + } } __attribute__((always_inline)) static inline void systimer_ll_counter_snapshot(uint32_t counter_id) @@ -58,6 +56,12 @@ __attribute__((always_inline)) static inline bool systimer_ll_is_counter_value_v return REG_GET_BIT(SYS_TIMER_SYSTIMER_UNIT0_OP_REG + 4 * counter_id, 1 << 29); } +__attribute__((always_inline)) static inline void systimer_ll_set_counter_value(uint32_t counter_id, uint64_t value) +{ + REG_WRITE(SYS_TIMER_SYSTIMER_UNIT0_LOAD_LO_REG + 8 * counter_id, value & 0xFFFFFFFF); + REG_WRITE(SYS_TIMER_SYSTIMER_UNIT0_LOAD_HI_REG, (value >> 32) & 0xFFFFF); +} + __attribute__((always_inline)) static inline uint32_t systimer_ll_get_counter_value_low(uint32_t counter_id) { return REG_READ(SYS_TIMER_SYSTIMER_UNIT0_VALUE_LO_REG + 8 * counter_id); @@ -68,13 +72,17 @@ __attribute__((always_inline)) static inline uint32_t systimer_ll_get_counter_va return REG_READ(SYS_TIMER_SYSTIMER_UNIT0_VALUE_HI_REG + 8 * counter_id); } +__attribute__((always_inline)) static inline void systimer_ll_apply_counter_value(uint32_t counter_id) +{ + REG_SET_BIT(SYS_TIMER_SYSTIMER_UNIT0_LOAD_REG + 4 * counter_id, SYS_TIMER_TIMER_UNIT0_LOAD); +} + /*******************alarm*************************/ __attribute__((always_inline)) static inline void systimer_ll_set_alarm_value(uint32_t alarm_id, uint64_t value) { REG_WRITE(SYS_TIMER_SYSTIMER_TARGET0_LO_REG + alarm_id * 8, value & 0xFFFFFFFF); - REG_WRITE(SYS_TIMER_SYSTIMER_TARGET0_HI_REG + alarm_id * 8, value >> 32); - REG_WRITE(SYS_TIMER_SYSTIMER_COMP0_LOAD_REG + alarm_id * 4, SYS_TIMER_TIMER_COMP0_LOAD); + REG_WRITE(SYS_TIMER_SYSTIMER_TARGET0_HI_REG + alarm_id * 8, (value >> 32) & 0xFFFFF); } __attribute__((always_inline)) static inline uint64_t systimer_ll_get_alarm_value(uint32_t alarm_id) @@ -83,6 +91,31 @@ __attribute__((always_inline)) static inline uint64_t systimer_ll_get_alarm_valu | REG_READ(SYS_TIMER_SYSTIMER_TARGET0_LO_REG + alarm_id * 8); } +__attribute__((always_inline)) static inline void systimer_ll_connect_alarm_counter(uint32_t alarm_id, uint32_t counter_id) +{ + REG_SET_FIELD(SYS_TIMER_SYSTIMER_TARGET0_CONF_REG + 4 * alarm_id, SYS_TIMER_TARGET0_TIMER_UNIT_SEL, counter_id); +} + +__attribute__((always_inline)) static inline void systimer_ll_enable_alarm_oneshot(uint32_t alarm_id) +{ + REG_CLR_BIT(SYS_TIMER_SYSTIMER_TARGET0_CONF_REG + alarm_id * 4, SYS_TIMER_TARGET0_PERIOD_MODE); +} + +__attribute__((always_inline)) static inline void systimer_ll_enable_alarm_period(uint32_t alarm_id) +{ + REG_SET_BIT(SYS_TIMER_SYSTIMER_TARGET0_CONF_REG + alarm_id * 4, SYS_TIMER_TARGET0_PERIOD_MODE); +} + +__attribute__((always_inline)) static inline void systimer_ll_set_alarm_period(uint32_t alarm_id, uint32_t period) +{ + REG_SET_FIELD(SYS_TIMER_SYSTIMER_TARGET0_CONF_REG + alarm_id * 4, SYS_TIMER_TARGET0_PERIOD, period); +} + +__attribute__((always_inline)) static inline void systimer_ll_apply_alarm_value(uint32_t alarm_id) +{ + REG_SET_BIT(SYS_TIMER_SYSTIMER_COMP0_LOAD_REG + alarm_id * 4, SYS_TIMER_TIMER_COMP0_LOAD); +} + __attribute__((always_inline)) static inline void systimer_ll_disable_alarm(uint32_t alarm_id) { REG_CLR_BIT(SYS_TIMER_SYSTIMER_CONF_REG, 1 << (24 - alarm_id)); @@ -93,26 +126,6 @@ __attribute__((always_inline)) static inline void systimer_ll_enable_alarm(uint3 REG_SET_BIT(SYS_TIMER_SYSTIMER_CONF_REG, 1 << (24 - alarm_id)); } -__attribute__((always_inline)) static inline void systimer_ll_enable_alarm_oneshot(uint32_t alarm_id) -{ - REG_CLR_BIT(SYS_TIMER_SYSTIMER_TARGET0_CONF_REG + alarm_id * 4, SYS_TIMER_TARGET0_PERIOD_MODE_M); -} - -__attribute__((always_inline)) static inline void systimer_ll_enable_alarm_period(uint32_t alarm_id) -{ - REG_SET_BIT(SYS_TIMER_SYSTIMER_TARGET0_CONF_REG + alarm_id * 4, SYS_TIMER_TARGET0_PERIOD_MODE_M); -} - -__attribute__((always_inline)) static inline void systimer_ll_set_alarm_period(uint32_t alarm_id, uint32_t period) -{ - REG_SET_FIELD(SYS_TIMER_SYSTIMER_TARGET0_CONF_REG + alarm_id * 4, SYS_TIMER_TARGET0_PERIOD, period); -} - -__attribute__((always_inline)) static inline void systimer_ll_connect_alarm_counter(uint32_t alarm_id, uint32_t counter_id) -{ - REG_SET_FIELD(SYS_TIMER_SYSTIMER_TARGET0_CONF_REG + 4 * alarm_id, SYS_TIMER_TARGET0_TIMER_UNIT_SEL, counter_id); -} - /*******************interrupt*************************/ __attribute__((always_inline)) static inline void systimer_ll_enable_alarm_int(uint32_t alarm_id) diff --git a/components/hal/esp32s3/systimer_hal.c b/components/hal/esp32s3/systimer_hal.c index fef0e4d939..1f615f8e4f 100644 --- a/components/hal/esp32s3/systimer_hal.c +++ b/components/hal/esp32s3/systimer_hal.c @@ -17,8 +17,9 @@ #include "hal/systimer_ll.h" #include "hal/systimer_types.h" #include "soc/systimer_caps.h" +#include "hal/clk_gate_ll.h" -#define SYSTIMER_TICKS_PER_US (16) // Number of timer ticks per microsecond +#define SYSTIMER_TICKS_PER_US (16) // Systimer clock source is fixed to 16MHz uint64_t systimer_hal_get_counter_value(systimer_counter_id_t counter_id) { @@ -56,6 +57,7 @@ void systimer_hal_set_alarm_value(systimer_alarm_id_t alarm_id, uint64_t timesta systimer_counter_value_t alarm = { .val = timestamp * SYSTIMER_TICKS_PER_US}; systimer_ll_disable_alarm(alarm_id); systimer_ll_set_alarm_value(alarm_id, alarm.val); + systimer_ll_apply_alarm_value(alarm_id); systimer_ll_enable_alarm(alarm_id); } @@ -78,7 +80,7 @@ void systimer_hal_on_apb_freq_update(uint32_t apb_ticks_per_us) void systimer_hal_counter_value_advance(systimer_counter_id_t counter_id, int64_t time_us) { systimer_counter_value_t new_count = { .val = systimer_hal_get_counter_value(counter_id) + time_us * SYSTIMER_TICKS_PER_US }; - systimer_ll_load_counter_value(counter_id, new_count.val); + systimer_ll_set_counter_value(counter_id, new_count.val); systimer_ll_apply_counter_value(counter_id); } @@ -89,6 +91,7 @@ void systimer_hal_enable_counter(systimer_counter_id_t counter_id) void systimer_hal_init(void) { + periph_ll_enable_clk_clear_rst(PERIPH_SYSTIMER_MODULE); systimer_ll_enable_clock(); }