You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2026/04/08 01:52:45 UTC
(nuttx) branch master updated: arch/risc-v: migrate hr timer backend to HAL esp_timer adapter
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 98875f005d1 arch/risc-v: migrate hr timer backend to HAL esp_timer adapter
98875f005d1 is described below
commit 98875f005d122e8763c7f0556ff5b75c912cca2a
Author: Tiago Medicci Serrano <ti...@espressif.com>
AuthorDate: Mon Mar 30 15:36:18 2026 -0300
arch/risc-v: migrate hr timer backend to HAL esp_timer adapter
Align RISC-V timer integration with Xtensa by replacing the
legacy systimer-thread backend with esp_timer adapter layers.
Rename local GPTIMER files to avoid header ambiguity, and
update board/build integration plus sdkconfig timer task
options for HAL compatibility.
Signed-off-by: Tiago Medicci Serrano <ti...@espressif.com>
---
arch/risc-v/src/common/espressif/CMakeLists.txt | 10 +-
arch/risc-v/src/common/espressif/Kconfig | 20 -
arch/risc-v/src/common/espressif/Make.defs | 8 +-
.../src/common/espressif/esp_ets_timer_legacy.c | 3 +-
.../espressif/{esp_timer.c => esp_gptimer.c} | 4 +-
.../espressif/{esp_timer.h => esp_gptimer.h} | 8 +-
arch/risc-v/src/common/espressif/esp_hr_timer.c | 919 ---------------------
arch/risc-v/src/common/espressif/esp_hr_timer.h | 292 +------
arch/risc-v/src/common/espressif/esp_rtc.c | 2 +
.../src/common/espressif/esp_timer_adapter.c | 494 +++++++++++
.../{esp_hr_timer.h => esp_timer_adapter.h} | 120 +--
arch/risc-v/src/common/espressif/esp_wifi_utils.c | 1 -
arch/risc-v/src/esp32c3/hal_esp32c3.cmake | 11 +-
arch/risc-v/src/esp32c3/hal_esp32c3.mk | 5 +
arch/risc-v/src/esp32c6/hal_esp32c6.cmake | 10 +
arch/risc-v/src/esp32c6/hal_esp32c6.mk | 5 +
arch/risc-v/src/esp32h2/hal_esp32h2.cmake | 10 +
arch/risc-v/src/esp32h2/hal_esp32h2.mk | 5 +
arch/risc-v/src/esp32p4/hal_esp32p4.cmake | 6 +
arch/risc-v/src/esp32p4/hal_esp32p4.mk | 6 +
.../esp32c3/esp32-c3-zero/src/esp32c3_bringup.c | 2 +-
.../esp32c3/esp32c3-devkit/src/esp32c3_bringup.c | 2 +-
.../esp32c3/esp32c3-xiao/src/esp32c3_bringup.c | 2 +-
.../esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c | 2 +-
.../esp32c6/esp32c6-devkitm/src/esp32c6_bringup.c | 2 +-
.../esp32c6/esp32c6-xiao/src/esp32c6_bringup.c | 2 +-
.../esp32h2/esp32h2-devkit/src/esp32h2_bringup.c | 2 +-
.../src/esp32p4_bringup.c | 2 +-
28 files changed, 640 insertions(+), 1315 deletions(-)
diff --git a/arch/risc-v/src/common/espressif/CMakeLists.txt b/arch/risc-v/src/common/espressif/CMakeLists.txt
index ed18f13a1d4..de756276bf5 100644
--- a/arch/risc-v/src/common/espressif/CMakeLists.txt
+++ b/arch/risc-v/src/common/espressif/CMakeLists.txt
@@ -49,7 +49,7 @@ if(CONFIG_DEV_RANDOM OR CONFIG_DEV_URANDOM_ARCH)
endif()
if(CONFIG_TIMER)
- list(APPEND SRCS esp_timer.c)
+ list(APPEND SRCS esp_gptimer.c)
endif()
if(CONFIG_ONESHOT)
@@ -61,7 +61,7 @@ if(CONFIG_RTC)
endif()
if(CONFIG_ESPRESSIF_HR_TIMER)
- list(APPEND SRCS esp_hr_timer.c esp_ets_timer_legacy.c)
+ list(APPEND SRCS esp_timer_adapter.c esp_ets_timer_legacy.c)
endif()
if(CONFIG_ESPRESSIF_EFUSE)
@@ -199,7 +199,7 @@ if(DEFINED ENV{ESP_HAL_3RDPARTY_VERSION})
CACHE STRING "ESP HAL 3rdparty version")
else()
set(ESP_HAL_3RDPARTY_VERSION
- 41b8c5f12063f6cce5796ce50b5eb4bd2ceeeb12
+ 5d8324708f55d5a927329553cdceebf7eafbd8c6
CACHE STRING "ESP HAL 3rdparty version")
endif()
@@ -348,9 +348,7 @@ target_compile_options(
# --undefined survives (otherwise ld sees the second symbol as an input file).
set(_esp_startup_u_opts "SHELL:-u esp_system_include_startup_funcs")
-if(CONFIG_PM)
- list(APPEND _esp_startup_u_opts "SHELL:-u esp_timer_init_include_func")
-endif()
+list(APPEND _esp_startup_u_opts "SHELL:-u esp_timer_init_include_func")
if(CONFIG_ESPRESSIF_EFUSE)
list(APPEND _esp_startup_u_opts "SHELL:-u esp_efuse_startup_include_func")
diff --git a/arch/risc-v/src/common/espressif/Kconfig b/arch/risc-v/src/common/espressif/Kconfig
index 4d9d6320a5a..4285a109a26 100644
--- a/arch/risc-v/src/common/espressif/Kconfig
+++ b/arch/risc-v/src/common/espressif/Kconfig
@@ -3473,26 +3473,6 @@ config ESP_MCPWM_TEST_LOOPBACK
endmenu # MCPWM Configuration
-menu "High Resolution Timer"
- depends on ESPRESSIF_HR_TIMER
-
-config ESPRESSIF_HR_TIMER_TASK_NAME
- string "High Resolution Timer task name"
- default "hr_timer"
-
-config ESPRESSIF_HR_TIMER_TASK_PRIORITY
- int "High Resolution Timer task priority"
- default 223
- ---help---
- Priority level of the High Resolution Timer task.
- Must be lower than the SCHED_HPWORKPRIORITY.
-
-config ESPRESSIF_HR_TIMER_TASK_STACK_SIZE
- int "High Resolution Timer task stack size"
- default 2048
-
-endmenu # High Resolution Timer
-
menu "Brownout Detector Configuration"
depends on ESPRESSIF_BROWNOUT_DET
diff --git a/arch/risc-v/src/common/espressif/Make.defs b/arch/risc-v/src/common/espressif/Make.defs
index 84c7ae7b30d..660b7042088 100644
--- a/arch/risc-v/src/common/espressif/Make.defs
+++ b/arch/risc-v/src/common/espressif/Make.defs
@@ -54,7 +54,7 @@ ifneq ($(CONFIG_DEV_RANDOM)$(CONFIG_DEV_URANDOM_ARCH),)
endif
ifeq ($(CONFIG_TIMER),y)
- CHIP_CSRCS += esp_timer.c
+ CHIP_CSRCS += esp_gptimer.c
endif
ifeq ($(CONFIG_ONESHOT),y)
@@ -66,7 +66,7 @@ ifeq ($(CONFIG_RTC),y)
endif
ifeq ($(CONFIG_ESPRESSIF_HR_TIMER),y)
- CHIP_CSRCS += esp_hr_timer.c esp_ets_timer_legacy.c
+ CHIP_CSRCS += esp_timer_adapter.c esp_ets_timer_legacy.c
endif
ifeq ($(CONFIG_ESPRESSIF_EFUSE),y)
@@ -191,12 +191,12 @@ ifeq ($(CONFIG_PM),y)
CHIP_CSRCS += esp_pm_initialize.c
endif
CHIP_CSRCS += esp_pm.c
- LDFLAGS += -u esp_timer_init_include_func
endif
# Required for initialization hooks on common code
LDFLAGS += -u esp_system_include_startup_funcs
+LDFLAGS += -u esp_timer_init_include_func
ifeq ($(CONFIG_ESPRESSIF_EFUSE),y)
LDFLAGS += -u esp_efuse_startup_include_func
@@ -210,7 +210,7 @@ endif
ESP_HAL_3RDPARTY_REPO = esp-hal-3rdparty
ifndef ESP_HAL_3RDPARTY_VERSION
- ESP_HAL_3RDPARTY_VERSION = 41b8c5f12063f6cce5796ce50b5eb4bd2ceeeb12
+ ESP_HAL_3RDPARTY_VERSION = 5d8324708f55d5a927329553cdceebf7eafbd8c6
endif
ifndef ESP_HAL_3RDPARTY_URL
diff --git a/arch/risc-v/src/common/espressif/esp_ets_timer_legacy.c b/arch/risc-v/src/common/espressif/esp_ets_timer_legacy.c
index 06a2b67c9c7..c892926185b 100644
--- a/arch/risc-v/src/common/espressif/esp_ets_timer_legacy.c
+++ b/arch/risc-v/src/common/espressif/esp_ets_timer_legacy.c
@@ -37,7 +37,6 @@
#include <stdbool.h>
#include "sdkconfig.h"
-#include "esp_timer.h"
#include "rom/ets_sys.h"
#include "esp_attr.h"
@@ -134,6 +133,8 @@ void ets_timer_setfn(ets_timer *ptimer,
hr_timer_args.arg = parg;
hr_timer_args.callback = pfunction;
+ hr_timer_args.name = "ets_timer";
+ hr_timer_args.skip_unhandled_events = false;
ret = esp_hr_timer_create(&hr_timer_args, &hr_timers_p);
diff --git a/arch/risc-v/src/common/espressif/esp_timer.c b/arch/risc-v/src/common/espressif/esp_gptimer.c
similarity index 99%
rename from arch/risc-v/src/common/espressif/esp_timer.c
rename to arch/risc-v/src/common/espressif/esp_gptimer.c
index 969f3ca44b2..a7d000f3c82 100644
--- a/arch/risc-v/src/common/espressif/esp_timer.c
+++ b/arch/risc-v/src/common/espressif/esp_gptimer.c
@@ -1,5 +1,5 @@
/****************************************************************************
- * arch/risc-v/src/common/espressif/esp_timer.c
+ * arch/risc-v/src/common/espressif/esp_gptimer.c
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -36,7 +36,7 @@
#include <nuttx/lib/lib.h>
#include "esp_irq.h"
-#include "esp_timer.h"
+#include "espressif/esp_gptimer.h"
#include "esp_clk_tree.h"
#include "esp_attr.h"
diff --git a/arch/risc-v/src/common/espressif/esp_timer.h b/arch/risc-v/src/common/espressif/esp_gptimer.h
similarity index 89%
rename from arch/risc-v/src/common/espressif/esp_timer.h
rename to arch/risc-v/src/common/espressif/esp_gptimer.h
index 53db6359785..c54f403b9ee 100644
--- a/arch/risc-v/src/common/espressif/esp_timer.h
+++ b/arch/risc-v/src/common/espressif/esp_gptimer.h
@@ -1,5 +1,5 @@
/****************************************************************************
- * arch/risc-v/src/common/espressif/esp_timer.h
+ * arch/risc-v/src/common/espressif/esp_gptimer.h
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -20,8 +20,8 @@
*
****************************************************************************/
-#ifndef __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_TIMER_H
-#define __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_TIMER_H
+#ifndef __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_GPTIMER_H
+#define __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_GPTIMER_H
/****************************************************************************
* Included Files
@@ -52,4 +52,4 @@
int esp_timer_initialize(int timer_id);
-#endif /* __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_TIMER_H */
+#endif /* __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_GPTIMER_H */
diff --git a/arch/risc-v/src/common/espressif/esp_hr_timer.c b/arch/risc-v/src/common/espressif/esp_hr_timer.c
deleted file mode 100644
index c620eadaf6d..00000000000
--- a/arch/risc-v/src/common/espressif/esp_hr_timer.c
+++ /dev/null
@@ -1,919 +0,0 @@
-/****************************************************************************
- * arch/risc-v/src/common/espressif/esp_hr_timer.c
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this args for additional information regarding copyright ownership. The
- * ASF licenses this file to you 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.
- *
- ****************************************************************************/
-
-/****************************************************************************
- * Included Files
- ****************************************************************************/
-
-#include <nuttx/config.h>
-
-#include <assert.h>
-#include <nuttx/debug.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <stdbool.h>
-#include <stdint.h>
-#include <string.h>
-#include <sys/types.h>
-
-#include <nuttx/nuttx.h>
-#include <nuttx/irq.h>
-#include <nuttx/kmalloc.h>
-#include <nuttx/kthread.h>
-#include <nuttx/semaphore.h>
-#include <nuttx/spinlock.h>
-
-#include "esp_irq.h"
-#include "esp_hr_timer.h"
-
-#include "esp_attr.h"
-#include "hal/systimer_hal.h"
-#include "hal/systimer_ll.h"
-#include "periph_ctrl.h"
-#include "systimer.h"
-
-/****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
-
-#if SOC_SYSTIMER_INT_LEVEL
-# define SYSTIMER_TRIGGER_TYPE ESP_IRQ_TRIGGER_LEVEL
-#else
-# define SYSTIMER_TRIGGER_TYPE ESP_IRQ_TRIGGER_EDGE
-#endif /* SOC_SYSTIMER_INT_LEVEL */
-
-#ifdef CONFIG_SCHED_HPWORK
-static_assert(CONFIG_ESPRESSIF_HR_TIMER_TASK_PRIORITY <
- CONFIG_SCHED_HPWORKPRIORITY,
- "High Resolution Timer task priority should be smaller than"
- " High priority worker thread");
-#endif
-
-/****************************************************************************
- * Private Types
- ****************************************************************************/
-
-struct esp_hr_timer_context_s
-{
- pid_t pid;
- sem_t toutsem;
- struct list_node runlist;
- struct list_node toutlist;
- spinlock_t lock;
- systimer_hal_context_t hal;
-};
-
-/****************************************************************************
- * Private Function Prototypes
- ****************************************************************************/
-
-static int esp_hr_timer_thread(int argc, char *argv[]);
-static int esp_hr_timer_isr(int irq, void *context, void *arg);
-
-/****************************************************************************
- * Private Data
- ****************************************************************************/
-
-static struct esp_hr_timer_context_s g_hr_timer_context =
-{
- .pid = INVALID_PROCESS_ID,
- .toutsem = SEM_INITIALIZER(0),
-};
-
-static bool g_hr_timer_initialized;
-static uint8_t g_hr_timer_lock_count;
-static irqstate_t g_hr_timer_lock_flags;
-
-/****************************************************************************
- * Private Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: esp_hr_timer_thread
- *
- * Description:
- * HR Timer working thread: Waits for a timeout semaphore, scans
- * the timeout list and processes all the timers in the list.
- *
- * Input Parameters:
- * argc - Not used.
- * argv - Not used.
- *
- * Returned Value:
- * Zero (OK) is returned on success; a negated errno value is returned on
- * any failure.
- *
- ****************************************************************************/
-
-IRAM_ATTR static int esp_hr_timer_thread(int argc, char *argv[])
-{
- struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
-
- while (true)
- {
- /* Waiting for all timers to time out */
-
- VERIFY(nxsem_wait_uninterruptible(&priv->toutsem));
-
- irqstate_t flags = spin_lock_irqsave(&priv->lock);
-
- /* Process all the timers in list */
-
- while (!list_is_empty(&priv->toutlist))
- {
- struct esp_hr_timer_s *timer;
- enum esp_hr_timer_state_e raw_state;
-
- /* Get the first timer in the list */
-
- timer = container_of(priv->toutlist.next,
- struct esp_hr_timer_s, list);
-
- /* Cache the raw state to decide how to deal with this timer */
-
- raw_state = timer->state;
-
- /* Delete the timer from the list */
-
- list_delete(&timer->list);
-
- /* Set timer's state to idle so it can be restarted by the user. */
-
- timer->state = HR_TIMER_IDLE;
-
- spin_unlock_irqrestore(&priv->lock, flags);
-
- if (raw_state == HR_TIMER_TIMEOUT)
- {
- timer->callback(timer->arg);
- }
- else if (raw_state == HR_TIMER_DELETE)
- {
- kmm_free(timer);
- }
-
- /* Enter critical section for next scanning list */
-
- flags = spin_lock_irqsave(&priv->lock);
-
- if (raw_state == HR_TIMER_TIMEOUT)
- {
- /* Check if the timer is in "repeat" mode */
-
- if (timer->flags & HR_TIMER_REPEAT)
- {
- esp_hr_timer_start(timer, timer->timeout, true);
- }
- }
- }
-
- spin_unlock_irqrestore(&priv->lock, flags);
- }
-
- return 0;
-}
-
-/****************************************************************************
- * Name: esp_hr_timer_isr
- *
- * Description:
- * Handler to be executed by the Systimer ISR.
- *
- * Input Parameters:
- * irq - IRQ associated to that interrupt.
- * context - Interrupt register state save info.
- * arg - A pointer to the argument provided when the interrupt
- * was registered.
- *
- * Returned Value:
- * Zero (OK) is returned on success; a negated errno value is returned on
- * any failure.
- *
- ****************************************************************************/
-
-static int IRAM_ATTR esp_hr_timer_isr(int irq, void *context, void *arg)
-{
- struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
- bool wake = false;
- irqstate_t flags;
-
- /* Clear interrupt register status */
-
- systimer_ll_clear_alarm_int(priv->hal.dev, SYSTIMER_ALARM_ESPTIMER);
-
- flags = enter_critical_section();
-
- /* Check if there is a timer running */
-
- if (!list_is_empty(&priv->runlist))
- {
- struct esp_hr_timer_s *timer;
- uint64_t counter;
-
- /* When stop/delete timer, in the same time the hardware timer
- * interrupt triggers, function "stop/delete" remove the timer
- * from running list, so the 1st timer is not which triggers.
- */
-
- timer = container_of(priv->runlist.next, struct esp_hr_timer_s, list);
- counter = systimer_hal_get_time(&priv->hal, SYSTIMER_COUNTER_ESPTIMER);
- if (timer->alarm <= counter)
- {
- /* Remove the first timer from the running list and add it to
- * the timeout list.
- *
- * Set the timer's state to be HR_TIMER_TIMEOUT to avoid
- * other operations.
- */
-
- list_delete(&timer->list);
- timer->state = HR_TIMER_TIMEOUT;
- list_add_after(&priv->toutlist, &timer->list);
- wake = true;
-
- /* Check if there is a timer running */
-
- if (!list_is_empty(&priv->runlist))
- {
- /* Reset hardware timer alarm with next timer's alarm value */
-
- timer = container_of(priv->runlist.next,
- struct esp_hr_timer_s, list);
-
- systimer_counter_value_t systimer_alarm =
- {
- .val = priv->hal.us_to_ticks(timer->alarm),
- };
-
- systimer_ll_enable_alarm(priv->hal.dev,
- SYSTIMER_ALARM_ESPTIMER, false);
- systimer_ll_set_alarm_target(priv->hal.dev,
- SYSTIMER_ALARM_ESPTIMER,
- systimer_alarm.val);
- systimer_ll_apply_alarm_value(priv->hal.dev,
- SYSTIMER_ALARM_ESPTIMER);
- }
- }
-
- /* If there is a timer in the list, the alarm should be enabled */
-
- systimer_ll_enable_alarm(priv->hal.dev, SYSTIMER_ALARM_ESPTIMER, true);
- }
-
- if (wake)
- {
- /* Wake up the thread to process timed-out timers */
-
- int ret = nxsem_post(&priv->toutsem);
- if (ret < 0)
- {
- tmrerr("Failed to post sem ret=%d\n", ret);
- return ret;
- }
- }
-
- leave_critical_section(flags);
-
- return OK;
-}
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: esp_hr_timer_create
- *
- * Description:
- * Create a High Resolution Timer from the provided arguments.
- *
- * Input Parameters:
- * args - HR Timer creation arguments.
- *
- * Output Parameters:
- * timer_handle - HR Timer handle pointer.
- *
- * Returned Value:
- * Zero (OK) is returned on success; a negated errno value is returned on
- * any failure.
- *
- ****************************************************************************/
-
-int IRAM_ATTR esp_hr_timer_create(const struct esp_hr_timer_args_s *args,
- struct esp_hr_timer_s **timer_handle)
-{
- struct esp_hr_timer_s *timer;
-
- timer = kmm_malloc(sizeof(*timer));
- if (timer == NULL)
- {
- tmrerr("Failed to allocate %d bytes\n", sizeof(*timer));
-
- return -ENOMEM;
- }
-
- timer->callback = args->callback;
- timer->arg = args->arg;
- timer->flags = HR_TIMER_NOFLAGS;
- timer->state = HR_TIMER_IDLE;
- list_initialize(&timer->list);
-
- *timer_handle = timer;
-
- return OK;
-}
-
-/****************************************************************************
- * Name: esp_hr_timer_start
- *
- * Description:
- * Start the High Resolution Timer.
- *
- * Input Parameters:
- * timer - HR Timer pointer.
- * timeout - Timeout value.
- * repeat - Repeat mode (true: enabled, false: disabled).
- *
- * Returned Value:
- * OK on success; ERROR on failure.
- *
- ****************************************************************************/
-
-int IRAM_ATTR esp_hr_timer_start(struct esp_hr_timer_s *timer,
- uint64_t timeout,
- bool repeat)
-{
- struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
- bool inserted = false;
- uint64_t counter;
- struct esp_hr_timer_s *p;
- irqstate_t flags = spin_lock_irqsave(&priv->lock);
- int ret = ERROR;
-
- if (timer == NULL)
- {
- return ret;
- }
-
- /* Only idle timer can be started */
-
- if (timer->state != HR_TIMER_IDLE)
- {
- ret = esp_hr_timer_stop_nolock(timer);
- if (ret != OK)
- {
- return ret;
- }
- }
-
- /* Calculate the timer's alarm value */
-
- counter = systimer_hal_get_time(&priv->hal, SYSTIMER_COUNTER_ESPTIMER);
- timer->timeout = timeout;
- timer->alarm = timer->timeout + counter;
-
- if (repeat)
- {
- timer->flags |= HR_TIMER_REPEAT;
- }
- else
- {
- timer->flags &= ~HR_TIMER_REPEAT;
- }
-
- /* Scan the timer list and insert the new timer into previous
- * node of timer whose alarm value is larger than new one
- */
-
- list_for_every_entry(&priv->runlist, p, struct esp_hr_timer_s, list)
- {
- if (p->alarm > timer->alarm)
- {
- list_add_before(&p->list, &timer->list);
- inserted = true;
- break;
- }
- }
-
- /* If we didn't find a larger one, insert the new timer at the tail
- * of the list.
- */
-
- if (!inserted)
- {
- list_add_tail(&priv->runlist, &timer->list);
- }
-
- timer->state = HR_TIMER_READY;
-
- /* If this timer is at the head of the list */
-
- if (timer == container_of(priv->runlist.next,
- struct esp_hr_timer_s, list))
- {
- /* Reset the hardware timer alarm */
-
- systimer_hal_set_alarm_target(&priv->hal, SYSTIMER_ALARM_ESPTIMER,
- timer->alarm);
- }
-
- spin_unlock_irqrestore(&priv->lock, flags);
- return OK;
-}
-
-/****************************************************************************
- * Name: esp_hr_timer_start_once
- *
- * Description:
- * Start the High Resolution Timer with one shot mode.
- *
- * Input Parameters:
- * timer - HR Timer pointer.
- * timeout - Timeout value.
- *
- * Returned Value:
- * OK on success; ERROR on failure.
- *
- ****************************************************************************/
-
-int esp_hr_timer_start_once(struct esp_hr_timer_s *timer, uint64_t timeout)
-{
- int ret = esp_hr_timer_start(timer, timeout, false);
- return ret;
-}
-
-/****************************************************************************
- * Name: esp_hr_timer_start_periodic
- *
- * Description:
- * Start the High Resolution Timer with periodic mode.
- *
- * Input Parameters:
- * timer - HR Timer pointer.
- * timeout - Timeout value.
- *
- * Returned Value:
- * OK on success; ERROR on failure.
- *
- ****************************************************************************/
-
-int esp_hr_timer_start_periodic(struct esp_hr_timer_s *timer,
- uint64_t timeout)
-{
- int ret = esp_hr_timer_start(timer, timeout, true);
- return ret;
-}
-
-/****************************************************************************
- * Name: esp_hr_timer_stop
- *
- * Description:
- * Stop the High Resolution Timer.
- *
- * Input Parameters:
- * timer - HR Timer pointer.
- *
- * Returned Value:
- * OK on success, ERROR on failure.
- *
- ****************************************************************************/
-
-int IRAM_ATTR esp_hr_timer_stop_nolock(struct esp_hr_timer_s *timer)
-{
- struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
-
- if (timer == NULL)
- {
- return ERROR;
- }
-
- /* "start" function can set the timer's repeat flag, and "stop" function
- * should remove this flag.
- */
-
- timer->flags &= ~HR_TIMER_REPEAT;
-
- /* Only timers in "ready" state can be stopped */
-
- if (timer->state == HR_TIMER_READY)
- {
- bool ishead;
-
- /* Check if the timer is at the head of the list */
-
- if (timer == container_of(priv->runlist.next,
- struct esp_hr_timer_s, list))
- {
- ishead = true;
- }
- else
- {
- ishead = false;
- }
-
- list_delete(&timer->list);
- timer->state = HR_TIMER_IDLE;
-
- /* If the timer is at the head of the list */
-
- if (ishead)
- {
- if (!list_is_empty(&priv->runlist))
- {
- struct esp_hr_timer_s *next_timer;
-
- /* Set the value from the next timer as the new hardware timer
- * alarm value.
- */
-
- next_timer = container_of(priv->runlist.next,
- struct esp_hr_timer_s,
- list);
-
- systimer_hal_set_alarm_target(&priv->hal,
- SYSTIMER_ALARM_ESPTIMER,
- next_timer->alarm);
- }
- }
- }
- else if (timer->state == HR_TIMER_TIMEOUT)
- {
- /* If the timer is in the timeout list, remove it from the list,
- * execute its callback function and set its state to idle.
- */
-
- DEBUGASSERT(!list_is_empty(&priv->toutlist));
-
- list_delete(&timer->list);
- timer->state = HR_TIMER_IDLE;
- timer->callback(timer->arg);
- }
-
- return OK;
-}
-
-int IRAM_ATTR esp_hr_timer_stop(struct esp_hr_timer_s *timer)
-{
- int ret = ERROR;
- struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
-
- irqstate_t flags = spin_lock_irqsave(&priv->lock);
- if (timer == NULL)
- {
- return ret;
- }
-
- ret = esp_hr_timer_stop_nolock(timer);
- spin_unlock_irqrestore(&priv->lock, flags);
- return ret;
-}
-
-/****************************************************************************
- * Name: esp_hr_timer_delete
- *
- * Description:
- * Stop and delete the High Resolution Timer.
- *
- * Input Parameters:
- * timer - HR Timer pointer.
- *
- * Returned Value:
- * OK on success; ERROR on failure.
- *
- ****************************************************************************/
-
-int esp_hr_timer_delete(struct esp_hr_timer_s *timer)
-{
- int ret = ERROR;
- irqstate_t flags;
-
- struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
-
- flags = spin_lock_irqsave(&priv->lock);
-
- if (timer->state == HR_TIMER_READY)
- {
- esp_hr_timer_stop_nolock(timer);
- }
- else if (timer->state == HR_TIMER_TIMEOUT)
- {
- list_delete(&timer->list);
- }
- else if (timer->state == HR_TIMER_DELETE)
- {
- spin_unlock_irqrestore(&priv->lock, flags);
- return ret;
- }
-
- list_add_after(&priv->toutlist, &timer->list);
- timer->state = HR_TIMER_DELETE;
- spin_unlock_irqrestore(&priv->lock, flags);
-
- /* Wake up the thread to process deleted timers */
-
- ret = nxsem_post(&priv->toutsem);
- if (ret < 0)
- {
- tmrerr("Failed to post sem ret=%d\n", ret);
- }
-
- return ret;
-}
-
-/****************************************************************************
- * Name: esp_hr_timer_time_us
- *
- * Description:
- * Get time of the High Resolution Timer in microseconds.
- *
- * Input Parameters:
- * None.
- *
- * Returned Value:
- * Time of the HR Timer in microseconds.
- *
- ****************************************************************************/
-
-uint64_t IRAM_ATTR esp_hr_timer_time_us(void)
-{
- struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
-
- return systimer_hal_get_time(&priv->hal, SYSTIMER_COUNTER_ESPTIMER);
-}
-
-/****************************************************************************
- * Name: esp_hr_timer_get_alarm
- *
- * Description:
- * Get the timestamp when the next timeout is expected to occur.
- *
- * Input Parameters:
- * None.
- *
- * Returned Value:
- * Timestamp of the nearest timer event in microseconds.
- *
- ****************************************************************************/
-
-uint64_t IRAM_ATTR esp_hr_timer_get_alarm(void)
-{
- struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
- uint64_t counter;
- uint64_t alarm_value;
-
- irqstate_t flags = spin_lock_irqsave(&priv->lock);
-
- counter = systimer_hal_get_time(&priv->hal, SYSTIMER_COUNTER_ESPTIMER);
- alarm_value = systimer_hal_get_alarm_value(&priv->hal,
- SYSTIMER_ALARM_ESPTIMER);
- alarm_value = priv->hal.ticks_to_us(alarm_value);
-
- if (alarm_value <= counter)
- {
- alarm_value = 0;
- }
- else
- {
- alarm_value -= counter;
- }
-
- spin_unlock_irqrestore(&priv->lock, flags);
-
- return alarm_value;
-}
-
-/****************************************************************************
- * Name: esp_hr_timer_calibration
- *
- * Description:
- * Adjust current High Resolution Timer by a certain value.
- *
- * Input Parameters:
- * time_us - Adjustment to apply to the HR Timer in microseconds.
- *
- * Returned Value:
- * None.
- *
- ****************************************************************************/
-
-void IRAM_ATTR esp_hr_timer_calibration(uint64_t time_us)
-{
- struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
-
- irqstate_t flags = spin_lock_irqsave(&priv->lock);
- systimer_hal_counter_value_advance(&priv->hal, SYSTIMER_COUNTER_ESPTIMER,
- time_us);
- spin_unlock_irqrestore(&priv->lock, flags);
-}
-
-/****************************************************************************
- * Name: esp_hr_timer_set
- *
- * Description:
- * Set the High Resolution Timer counter value to a specific timestamp.
- *
- * Input Parameters:
- * new_us - New counter value in microseconds.
- *
- * Returned Value:
- * None.
- *
- ****************************************************************************/
-
-void IRAM_ATTR esp_hr_timer_set(uint64_t new_us)
-{
- struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
-
- irqstate_t flags = spin_lock_irqsave(&priv->lock);
- systimer_counter_value_t new_count =
- {
- .val = priv->hal.us_to_ticks(new_us),
- };
-
- systimer_ll_set_counter_value(priv->hal.dev, SYSTIMER_COUNTER_ESPTIMER,
- new_count.val);
- systimer_ll_apply_counter_value(priv->hal.dev, SYSTIMER_COUNTER_ESPTIMER);
- spin_unlock_irqrestore(&priv->lock, flags);
-}
-
-/****************************************************************************
- * Name: esp_hr_timer_lock
- *
- * Description:
- * Acquire the High Resolution Timer lock to protect access to hardware
- * registers. Must be paired with esp_hr_timer_unlock().
- * This function supports recursive locking.
- *
- * Input Parameters:
- * None.
- *
- * Returned Value:
- * None.
- *
- ****************************************************************************/
-
-void IRAM_ATTR esp_hr_timer_lock(void)
-{
- struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
- irqstate_t flags;
-
- if (g_hr_timer_lock_count == 0)
- {
- flags = spin_lock_irqsave(&priv->lock);
-
- g_hr_timer_lock_flags = flags;
- }
-
- g_hr_timer_lock_count++;
-}
-
-/****************************************************************************
- * Name: esp_hr_timer_unlock
- *
- * Description:
- * Release the High Resolution Timer lock. Must be paired with
- * esp_hr_timer_lock().
- * This function supports recursive unlocking.
- *
- * Input Parameters:
- * None.
- *
- * Returned Value:
- * None.
- *
- ****************************************************************************/
-
-void IRAM_ATTR esp_hr_timer_unlock(void)
-{
- struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
-
- g_hr_timer_lock_count--;
-
- if (g_hr_timer_lock_count == 0)
- {
- spin_unlock_irqrestore(&priv->lock, g_hr_timer_lock_flags);
- }
-}
-
-/****************************************************************************
- * Name: esp_hr_timer_init
- *
- * Description:
- * Initialize High Resolution Timer.
- *
- * Input Parameters:
- * None.
- *
- * Returned Value:
- * Zero (OK) is returned on success; a negated errno value is returned on
- * any failure.
- *
- ****************************************************************************/
-
-int esp_hr_timer_init(void)
-{
- struct esp_hr_timer_context_s *priv;
- int pid;
-
- if (g_hr_timer_initialized)
- {
- tmrinfo("HR Timer already initialized, skipping...\n");
-
- return OK;
- }
-
- pid = kthread_create(CONFIG_ESPRESSIF_HR_TIMER_TASK_NAME,
- CONFIG_ESPRESSIF_HR_TIMER_TASK_PRIORITY,
- CONFIG_ESPRESSIF_HR_TIMER_TASK_STACK_SIZE,
- esp_hr_timer_thread,
- NULL);
- if (pid < 0)
- {
- tmrerr("Failed to create HR Timer task=%d\n", pid);
-
- return pid;
- }
-
- priv = &g_hr_timer_context;
-
- list_initialize(&priv->runlist);
- list_initialize(&priv->toutlist);
-
- priv->pid = (pid_t)pid;
-
- {
- irqstate_t flags = spin_lock_irqsave(&priv->lock);
-
- PERIPH_RCC_ACQUIRE_ATOMIC(PERIPH_SYSTIMER_MODULE, ref_count)
- {
- if (ref_count == 0)
- {
- systimer_ll_enable_bus_clock(true);
- systimer_ll_reset_register();
- }
- }
-
- systimer_hal_init(&priv->hal);
- systimer_hal_tick_rate_ops_t ops =
- {
- .ticks_to_us = systimer_ticks_to_us,
- .us_to_ticks = systimer_us_to_ticks,
- };
-
- systimer_hal_set_tick_rate_ops(&priv->hal, &ops);
- systimer_ll_set_counter_value(priv->hal.dev,
- SYSTIMER_COUNTER_ESPTIMER,
- 0);
- systimer_ll_apply_counter_value(priv->hal.dev,
- SYSTIMER_COUNTER_ESPTIMER);
-
-#if !SOC_SYSTIMER_FIXED_DIVIDER
- ASSERT(esp_clk_xtal_freq() == (40 * 1000000));
-
- systimer_hal_set_steps_per_tick(&priv->hal, 0, 2); /* For XTAL */
- systimer_hal_set_steps_per_tick(&priv->hal, 1, 1); /* For PLL */
-#endif
-
- systimer_hal_enable_alarm_int(&priv->hal, SYSTIMER_ALARM_ESPTIMER);
- systimer_hal_enable_counter(&priv->hal, SYSTIMER_COUNTER_ESPTIMER);
- systimer_hal_select_alarm_mode(&priv->hal, SYSTIMER_ALARM_ESPTIMER,
- SYSTIMER_ALARM_MODE_ONESHOT);
- systimer_hal_connect_alarm_counter(&priv->hal, SYSTIMER_ALARM_ESPTIMER,
- SYSTIMER_COUNTER_ESPTIMER);
-
- spin_unlock_irqrestore(&priv->lock, flags);
- }
-
- esp_setup_irq(ETS_SYSTIMER_TARGET2_INTR_SOURCE,
- ESP_IRQ_PRIORITY_DEFAULT,
- SYSTIMER_TRIGGER_TYPE,
- esp_hr_timer_isr,
- NULL);
-
- /* Enable the allocated CPU interrupt */
-
- up_enable_irq(ESP_SOURCE2IRQ(ETS_SYSTIMER_TARGET2_INTR_SOURCE));
-
- g_hr_timer_initialized = true;
-
- return OK;
-}
diff --git a/arch/risc-v/src/common/espressif/esp_hr_timer.h b/arch/risc-v/src/common/espressif/esp_hr_timer.h
index 35dea08eb0e..893dede9d13 100644
--- a/arch/risc-v/src/common/espressif/esp_hr_timer.h
+++ b/arch/risc-v/src/common/espressif/esp_hr_timer.h
@@ -27,296 +27,8 @@
* Included Files
****************************************************************************/
-#include <nuttx/config.h>
+/* Compatibility wrapper for the ESP-HAL timer adapter. */
-#include <stdint.h>
-
-#include <nuttx/list.h>
-
-/****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
-
-#define HR_TIMER_NOFLAGS (0) /* Timer supports no feature */
-#define HR_TIMER_REPEAT (1 << 0) /* Timer supports repeat mode */
-
-/****************************************************************************
- * Public Types
- ****************************************************************************/
-
-/* HR Timer state */
-
-enum esp_hr_timer_state_e
-{
- HR_TIMER_IDLE, /* Timer is not counting */
- HR_TIMER_READY, /* Timer is counting */
- HR_TIMER_TIMEOUT, /* Timer timed out */
- HR_TIMER_DELETE /* Timer is to be delete */
-};
-
-/* HR Timer data structure */
-
-struct esp_hr_timer_s
-{
- uint64_t timeout; /* Timeout value */
- uint64_t alarm; /* Timeout period */
- void (*callback)(void *arg); /* Callback function */
- void *arg; /* Private data */
- uint16_t flags; /* Supported features */
- enum esp_hr_timer_state_e state; /* Timer state */
- struct list_node list; /* Working list */
-};
-
-/* HR Timer creation arguments data structure */
-
-struct esp_hr_timer_args_s
-{
- void (*callback)(void *arg); /* Callback function */
- void *arg; /* Private data */
- const char *name; /* Timer name, used in esp_timer_dump function */
- bool skip_unhandled_events; /* Skip unhandled events for periodic timers */
-};
-
-#undef EXTERN
-#if defined(__cplusplus)
-#define EXTERN extern "C"
-extern "C"
-{
-#else
-#define EXTERN extern
-#endif
-
-/****************************************************************************
- * Public Function Prototypes
- ****************************************************************************/
-
-/****************************************************************************
- * Name: esp_hr_timer_create
- *
- * Description:
- * Create a High Resolution Timer from the provided arguments.
- *
- * Input Parameters:
- * args - HR Timer creation arguments.
- *
- * Output Parameters:
- * timer_handle - HR Timer handle pointer.
- *
- * Returned Value:
- * Zero (OK) is returned on success; a negated errno value is returned on
- * any failure.
- *
- ****************************************************************************/
-
-int esp_hr_timer_create(const struct esp_hr_timer_args_s *args,
- struct esp_hr_timer_s **timer_handle);
-
-/****************************************************************************
- * Name: esp_hr_timer_start
- *
- * Description:
- * Start the High Resolution Timer.
- *
- * Input Parameters:
- * timer - HR Timer pointer.
- * timeout - Timeout value.
- * repeat - Repeat mode (true: enabled, false: disabled).
- *
- * Returned Value:
- * OK on success; ERROR on failure.
- *
- ****************************************************************************/
-
-int esp_hr_timer_start(struct esp_hr_timer_s *timer,
- uint64_t timeout,
- bool repeat);
-
-/****************************************************************************
- * Name: esp_hr_timer_start_once
- *
- * Description:
- * Start the High Resolution Timer with one shot mode.
- *
- * Input Parameters:
- * timer - HR Timer pointer.
- * timeout - Timeout value.
- *
- * Returned Value:
- * OK on success; ERROR on failure.
- *
- ****************************************************************************/
-
-int esp_hr_timer_start_once(struct esp_hr_timer_s *timer, uint64_t timeout);
-
-/****************************************************************************
- * Name: esp_hr_timer_start_periodic
- *
- * Description:
- * Start the High Resolution Timer with periodic mode.
- *
- * Input Parameters:
- * timer - HR Timer pointer.
- * timeout - Timeout value.
- *
- * Returned Value:
- * OK on success; ERROR on failure.
- *
- ****************************************************************************/
-
-int esp_hr_timer_start_periodic(struct esp_hr_timer_s *timer,
- uint64_t timeout);
-
-/****************************************************************************
- * Name: esp_hr_timer_stop
- *
- * Description:
- * Stop the High Resolution Timer.
- *
- * Input Parameters:
- * timer - HR Timer pointer.
- *
- * Returned Value:
- * OK on success; ERROR on failure.
- *
- ****************************************************************************/
-
-int esp_hr_timer_stop(struct esp_hr_timer_s *timer);
-int esp_hr_timer_stop_nolock(struct esp_hr_timer_s *timer);
-
-/****************************************************************************
- * Name: esp_hr_timer_delete
- *
- * Description:
- * Stop and delete the High Resolution Timer.
- *
- * Input Parameters:
- * timer - HR Timer pointer.
- *
- * Returned Value:
- * OK on success; ERROR on failure.
- *
- ****************************************************************************/
-
-int esp_hr_timer_delete(struct esp_hr_timer_s *timer);
-
-/****************************************************************************
- * Name: esp_hr_timer_time_us
- *
- * Description:
- * Get time of the High Resolution Timer in microseconds.
- *
- * Input Parameters:
- * None.
- *
- * Returned Value:
- * Time of the HR Timer in microseconds.
- *
- ****************************************************************************/
-
-uint64_t esp_hr_timer_time_us(void);
-
-/****************************************************************************
- * Name: esp_hr_timer_get_alarm
- *
- * Description:
- * Get the timestamp when the next timeout is expected to occur.
- *
- * Input Parameters:
- * None.
- *
- * Returned Value:
- * Timestamp of the nearest timer event in microseconds.
- *
- ****************************************************************************/
-
-uint64_t esp_hr_timer_get_alarm(void);
-
-/****************************************************************************
- * Name: esp_hr_timer_calibration
- *
- * Description:
- * Adjust current High Resolution Timer by a certain value.
- *
- * Input Parameters:
- * time_us - Adjustment to apply to the HR Timer in microseconds.
- *
- * Returned Value:
- * None.
- *
- ****************************************************************************/
-
-void esp_hr_timer_calibration(uint64_t time_us);
-
-/****************************************************************************
- * Name: esp_hr_timer_set
- *
- * Description:
- * Set the High Resolution Timer counter value to a specific timestamp.
- *
- * Input Parameters:
- * new_us - New counter value in microseconds.
- *
- * Returned Value:
- * None.
- *
- ****************************************************************************/
-
-void esp_hr_timer_set(uint64_t new_us);
-
-/****************************************************************************
- * Name: esp_hr_timer_lock
- *
- * Description:
- * Acquire the High Resolution Timer lock to protect access to hardware
- * registers. Must be paired with esp_hr_timer_unlock().
- *
- * Input Parameters:
- * None.
- *
- * Returned Value:
- * None.
- *
- ****************************************************************************/
-
-void esp_hr_timer_lock(void);
-
-/****************************************************************************
- * Name: esp_hr_timer_unlock
- *
- * Description:
- * Release the High Resolution Timer lock. Must be paired with
- * esp_hr_timer_lock().
- *
- * Input Parameters:
- * None.
- *
- * Returned Value:
- * None.
- *
- ****************************************************************************/
-
-void esp_hr_timer_unlock(void);
-
-/****************************************************************************
- * Name: esp_hr_timer_init
- *
- * Description:
- * Initialize High Resolution Timer.
- *
- * Input Parameters:
- * None.
- *
- * Returned Value:
- * Zero (OK) is returned on success; a negated errno value is returned on
- * any failure.
- *
- ****************************************************************************/
-
-int esp_hr_timer_init(void);
-
-#ifdef __cplusplus
-}
-#endif
-#undef EXTERN
+#include "esp_timer_adapter.h"
#endif /* __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_HR_TIMER_H */
diff --git a/arch/risc-v/src/common/espressif/esp_rtc.c b/arch/risc-v/src/common/espressif/esp_rtc.c
index 3c774056ad5..6c36b1ff77b 100644
--- a/arch/risc-v/src/common/espressif/esp_rtc.c
+++ b/arch/risc-v/src/common/espressif/esp_rtc.c
@@ -445,6 +445,8 @@ static int esp_rtc_setalarm_nolock(struct rtc_lowerhalf_s *lower,
cbinfo->index = id;
hr_timer_args.arg = cbinfo;
hr_timer_args.callback = rtc_hr_timer_cb;
+ hr_timer_args.name = "rtc_alarm";
+ hr_timer_args.skip_unhandled_events = false;
ret = esp_hr_timer_create(&hr_timer_args, &cbinfo->alarm_hdl);
if (ret < 0)
diff --git a/arch/risc-v/src/common/espressif/esp_timer_adapter.c b/arch/risc-v/src/common/espressif/esp_timer_adapter.c
new file mode 100644
index 00000000000..e224b2e2665
--- /dev/null
+++ b/arch/risc-v/src/common/espressif/esp_timer_adapter.c
@@ -0,0 +1,494 @@
+/****************************************************************************
+ * arch/risc-v/src/common/espressif/esp_timer_adapter.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/spinlock.h>
+
+#include "esp_timer.h"
+#include "esp_timer_adapter.h"
+
+#include "esp_private/esp_timer_private.h"
+#include "esp_timer_impl.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* HR timer data structure wrapping HAL esp_timer (internal definition) */
+
+struct esp_hr_timer_s
+{
+ esp_timer_handle_t hal_timer; /* HAL timer handle */
+ uint64_t timeout; /* Timeout value */
+ uint64_t alarm; /* Timeout period */
+ void (*callback)(void *arg); /* Callback function */
+ void *arg; /* Private data */
+ uint16_t flags; /* Supported features */
+ enum esp_hr_timer_state_e state; /* Timer state */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static void esp_hr_timer_callback_wrapper(void *arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static spinlock_t g_hr_timer_lock = SP_UNLOCKED;
+static bool g_hr_timer_initialized = false;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp_hr_timer_callback_wrapper
+ *
+ * Description:
+ * Adapter callback used by esp_timer to dispatch to the upper-layer timer
+ * callback and update the timer state.
+ *
+ * Input Parameters:
+ * arg - Pointer to struct esp_hr_timer_s.
+ *
+ * Returned Value:
+ * None.
+ *
+ ****************************************************************************/
+
+static void esp_hr_timer_callback_wrapper(void *arg)
+{
+ struct esp_hr_timer_s *timer = (struct esp_hr_timer_s *)arg;
+
+ if (timer && timer->callback)
+ {
+ timer->state = HR_TIMER_TIMEOUT;
+ timer->callback(timer->arg);
+ }
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp_hr_timer_create
+ *
+ * Description:
+ * Create a high-resolution timer instance and initialize it from the
+ * provided arguments.
+ *
+ * Input Parameters:
+ * args - Timer creation arguments.
+ * timer_handle - Location where the created timer handle is returned.
+ *
+ * Returned Value:
+ * OK on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int esp_hr_timer_create(const struct esp_hr_timer_args_s *args,
+ struct esp_hr_timer_s **timer_handle)
+{
+ struct esp_hr_timer_s *timer;
+ esp_timer_create_args_t hal_args;
+ esp_err_t ret;
+
+ if (args == NULL || timer_handle == NULL || args->callback == NULL)
+ {
+ return -EINVAL;
+ }
+
+ timer = kmm_zalloc(sizeof(struct esp_hr_timer_s));
+ if (timer == NULL)
+ {
+ return -ENOMEM;
+ }
+
+ timer->callback = args->callback;
+ timer->arg = args->arg;
+ timer->flags = HR_TIMER_NOFLAGS;
+ timer->state = HR_TIMER_IDLE;
+
+ hal_args.callback = esp_hr_timer_callback_wrapper;
+ hal_args.arg = timer;
+ hal_args.dispatch_method = ESP_TIMER_TASK;
+ hal_args.name = args->name ? args->name : "nuttx_hr";
+ hal_args.skip_unhandled_events = args->skip_unhandled_events;
+
+ ret = esp_timer_create(&hal_args, &timer->hal_timer);
+ if (ret != ESP_OK)
+ {
+ kmm_free(timer);
+ return -EINVAL;
+ }
+
+ *timer_handle = timer;
+ return OK;
+}
+
+/****************************************************************************
+ * Name: esp_hr_timer_start
+ *
+ * Description:
+ * Start a high-resolution timer in one-shot or periodic mode.
+ *
+ * Input Parameters:
+ * timer - Timer instance to start.
+ * timeout - Timeout period in microseconds.
+ * repeat - True for periodic mode, false for one-shot mode.
+ *
+ * Returned Value:
+ * OK on success; ERROR on failure.
+ *
+ ****************************************************************************/
+
+int esp_hr_timer_start(struct esp_hr_timer_s *timer,
+ uint64_t timeout,
+ bool repeat)
+{
+ esp_err_t ret;
+
+ if (timer == NULL)
+ {
+ return ERROR;
+ }
+
+ timer->timeout = timeout;
+
+ if (repeat)
+ {
+ timer->flags |= HR_TIMER_REPEAT;
+ ret = esp_timer_start_periodic(timer->hal_timer, timeout);
+ }
+ else
+ {
+ timer->flags &= ~HR_TIMER_REPEAT;
+ ret = esp_timer_start_once(timer->hal_timer, timeout);
+ }
+
+ if (ret == ESP_OK)
+ {
+ timer->state = HR_TIMER_READY;
+ return OK;
+ }
+
+ return ERROR;
+}
+
+/****************************************************************************
+ * Name: esp_hr_timer_start_once
+ *
+ * Description:
+ * Start a high-resolution timer in one-shot mode.
+ *
+ * Input Parameters:
+ * timer - Timer instance to start.
+ * timeout - One-shot timeout in microseconds.
+ *
+ * Returned Value:
+ * OK on success; ERROR on failure.
+ *
+ ****************************************************************************/
+
+int esp_hr_timer_start_once(struct esp_hr_timer_s *timer, uint64_t timeout)
+{
+ return esp_hr_timer_start(timer, timeout, false);
+}
+
+/****************************************************************************
+ * Name: esp_hr_timer_start_periodic
+ *
+ * Description:
+ * Start a high-resolution timer in periodic mode.
+ *
+ * Input Parameters:
+ * timer - Timer instance to start.
+ * timeout - Period interval in microseconds.
+ *
+ * Returned Value:
+ * OK on success; ERROR on failure.
+ *
+ ****************************************************************************/
+
+int esp_hr_timer_start_periodic(struct esp_hr_timer_s *timer,
+ uint64_t timeout)
+{
+ return esp_hr_timer_start(timer, timeout, true);
+}
+
+/****************************************************************************
+ * Name: esp_hr_timer_stop
+ *
+ * Description:
+ * Stop a running high-resolution timer.
+ *
+ * Input Parameters:
+ * timer - Timer instance to stop.
+ *
+ * Returned Value:
+ * OK on success; ERROR on failure.
+ *
+ ****************************************************************************/
+
+int esp_hr_timer_stop(struct esp_hr_timer_s *timer)
+{
+ esp_err_t ret;
+
+ if (timer == NULL)
+ {
+ return ERROR;
+ }
+
+ ret = esp_timer_stop(timer->hal_timer);
+ if (ret == ESP_OK)
+ {
+ timer->state = HR_TIMER_IDLE;
+ return OK;
+ }
+
+ return ERROR;
+}
+
+/****************************************************************************
+ * Name: esp_hr_timer_stop_nolock
+ *
+ * Description:
+ * Stop a running high-resolution timer without taking any adapter lock.
+ *
+ * Input Parameters:
+ * timer - Timer instance to stop.
+ *
+ * Returned Value:
+ * OK on success; ERROR on failure.
+ *
+ ****************************************************************************/
+
+int esp_hr_timer_stop_nolock(struct esp_hr_timer_s *timer)
+{
+ /* esp_timer already handles its own locking */
+
+ return esp_hr_timer_stop(timer);
+}
+
+/****************************************************************************
+ * Name: esp_hr_timer_delete
+ *
+ * Description:
+ * Delete a timer instance and release associated resources.
+ *
+ * Input Parameters:
+ * timer - Timer instance to delete.
+ *
+ * Returned Value:
+ * OK on success; ERROR on failure.
+ *
+ ****************************************************************************/
+
+int esp_hr_timer_delete(struct esp_hr_timer_s *timer)
+{
+ esp_err_t ret;
+
+ if (timer == NULL)
+ {
+ return ERROR;
+ }
+
+ timer->state = HR_TIMER_DELETE;
+
+ ret = esp_timer_delete(timer->hal_timer);
+ if (ret == ESP_OK)
+ {
+ kmm_free(timer);
+ return OK;
+ }
+
+ return ERROR;
+}
+
+/****************************************************************************
+ * Name: esp_hr_timer_time_us
+ *
+ * Description:
+ * Return the current high-resolution timer time in microseconds.
+ *
+ * Input Parameters:
+ * None.
+ *
+ * Returned Value:
+ * Current timer time in microseconds.
+ *
+ ****************************************************************************/
+
+uint64_t esp_hr_timer_time_us(void)
+{
+ return esp_timer_impl_get_time();
+}
+
+/****************************************************************************
+ * Name: esp_hr_timer_get_alarm
+ *
+ * Description:
+ * Return the timestamp of the next scheduled timer alarm.
+ *
+ * Input Parameters:
+ * None.
+ *
+ * Returned Value:
+ * Absolute time in microseconds for the next alarm.
+ *
+ ****************************************************************************/
+
+uint64_t esp_hr_timer_get_alarm(void)
+{
+ return esp_timer_get_next_alarm();
+}
+
+/****************************************************************************
+ * Name: esp_hr_timer_calibration
+ *
+ * Description:
+ * Apply a timer calibration adjustment.
+ * The HAL esp_timer backend does not support calibration, so this is a
+ * no-op.
+ *
+ * Input Parameters:
+ * time_us - Calibration adjustment in microseconds.
+ *
+ * Returned Value:
+ * None.
+ *
+ ****************************************************************************/
+
+void esp_hr_timer_calibration(uint64_t time_us)
+{
+ /* HAL esp_timer does not support calibration */
+
+ UNUSED(time_us);
+}
+
+/****************************************************************************
+ * Name: esp_hr_timer_set
+ *
+ * Description:
+ * Set the high-resolution timer counter to a specific timestamp.
+ *
+ * Input Parameters:
+ * new_us - New timer value in microseconds.
+ *
+ * Returned Value:
+ * None.
+ *
+ ****************************************************************************/
+
+void esp_hr_timer_set(uint64_t new_us)
+{
+ /* Use HAL function to set timer */
+
+ esp_timer_private_set(new_us);
+}
+
+/****************************************************************************
+ * Name: esp_hr_timer_lock
+ *
+ * Description:
+ * Acquire the adapter lock used to serialize timer operations.
+ *
+ * Input Parameters:
+ * None.
+ *
+ * Returned Value:
+ * None.
+ *
+ ****************************************************************************/
+
+void esp_hr_timer_lock(void)
+{
+ spin_lock(&g_hr_timer_lock);
+}
+
+/****************************************************************************
+ * Name: esp_hr_timer_unlock
+ *
+ * Description:
+ * Release the adapter lock used to serialize timer operations.
+ *
+ * Input Parameters:
+ * None.
+ *
+ * Returned Value:
+ * None.
+ *
+ ****************************************************************************/
+
+void esp_hr_timer_unlock(void)
+{
+ spin_unlock(&g_hr_timer_lock);
+}
+
+/****************************************************************************
+ * Name: esp_hr_timer_init
+ *
+ * Description:
+ * Initialize the timer adapter and the underlying ESP timer subsystem.
+ *
+ * Input Parameters:
+ * None.
+ *
+ * Returned Value:
+ * OK on success; ERROR on failure.
+ *
+ ****************************************************************************/
+
+int esp_hr_timer_init(void)
+{
+ esp_err_t ret;
+
+ if (g_hr_timer_initialized)
+ {
+ return OK;
+ }
+
+ /* Initialize the timer subsystem */
+
+ ret = esp_timer_init();
+ if (ret == ESP_OK)
+ {
+ g_hr_timer_initialized = true;
+ return OK;
+ }
+
+ return ERROR;
+}
diff --git a/arch/risc-v/src/common/espressif/esp_hr_timer.h b/arch/risc-v/src/common/espressif/esp_timer_adapter.h
similarity index 71%
copy from arch/risc-v/src/common/espressif/esp_hr_timer.h
copy to arch/risc-v/src/common/espressif/esp_timer_adapter.h
index 35dea08eb0e..dda94485ab9 100644
--- a/arch/risc-v/src/common/espressif/esp_hr_timer.h
+++ b/arch/risc-v/src/common/espressif/esp_timer_adapter.h
@@ -1,5 +1,5 @@
/****************************************************************************
- * arch/risc-v/src/common/espressif/esp_hr_timer.h
+ * arch/risc-v/src/common/espressif/esp_timer_adapter.h
*
* SPDX-License-Identifier: Apache-2.0
*
@@ -20,8 +20,8 @@
*
****************************************************************************/
-#ifndef __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_HR_TIMER_H
-#define __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_HR_TIMER_H
+#ifndef __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_TIMER_ADAPTER_H
+#define __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_TIMER_ADAPTER_H
/****************************************************************************
* Included Files
@@ -29,10 +29,9 @@
#include <nuttx/config.h>
+#include <stdbool.h>
#include <stdint.h>
-#include <nuttx/list.h>
-
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@@ -44,28 +43,19 @@
* Public Types
****************************************************************************/
-/* HR Timer state */
+/* HR Timer state (simplified for adapter) */
enum esp_hr_timer_state_e
{
HR_TIMER_IDLE, /* Timer is not counting */
HR_TIMER_READY, /* Timer is counting */
HR_TIMER_TIMEOUT, /* Timer timed out */
- HR_TIMER_DELETE /* Timer is to be delete */
+ HR_TIMER_DELETE /* Timer is to be deleted */
};
-/* HR Timer data structure */
+/* Forward declaration - opaque type */
-struct esp_hr_timer_s
-{
- uint64_t timeout; /* Timeout value */
- uint64_t alarm; /* Timeout period */
- void (*callback)(void *arg); /* Callback function */
- void *arg; /* Private data */
- uint16_t flags; /* Supported features */
- enum esp_hr_timer_state_e state; /* Timer state */
- struct list_node list; /* Working list */
-};
+struct esp_hr_timer_s;
/* HR Timer creation arguments data structure */
@@ -73,7 +63,7 @@ struct esp_hr_timer_args_s
{
void (*callback)(void *arg); /* Callback function */
void *arg; /* Private data */
- const char *name; /* Timer name, used in esp_timer_dump function */
+ const char *name; /* Timer name */
bool skip_unhandled_events; /* Skip unhandled events for periodic timers */
};
@@ -94,17 +84,15 @@ extern "C"
* Name: esp_hr_timer_create
*
* Description:
- * Create a High Resolution Timer from the provided arguments.
+ * Create a high-resolution timer instance and initialize it from the
+ * provided arguments.
*
* Input Parameters:
- * args - HR Timer creation arguments.
- *
- * Output Parameters:
- * timer_handle - HR Timer handle pointer.
+ * args - Timer creation arguments.
+ * timer_handle - Location where the created timer handle is returned.
*
* Returned Value:
- * Zero (OK) is returned on success; a negated errno value is returned on
- * any failure.
+ * OK on success; a negated errno value on failure.
*
****************************************************************************/
@@ -115,12 +103,12 @@ int esp_hr_timer_create(const struct esp_hr_timer_args_s *args,
* Name: esp_hr_timer_start
*
* Description:
- * Start the High Resolution Timer.
+ * Start a high-resolution timer in one-shot or periodic mode.
*
* Input Parameters:
- * timer - HR Timer pointer.
- * timeout - Timeout value.
- * repeat - Repeat mode (true: enabled, false: disabled).
+ * timer - Timer instance to start.
+ * timeout - Timeout period in microseconds.
+ * repeat - True for periodic mode, false for one-shot mode.
*
* Returned Value:
* OK on success; ERROR on failure.
@@ -128,18 +116,18 @@ int esp_hr_timer_create(const struct esp_hr_timer_args_s *args,
****************************************************************************/
int esp_hr_timer_start(struct esp_hr_timer_s *timer,
- uint64_t timeout,
- bool repeat);
+ uint64_t timeout,
+ bool repeat);
/****************************************************************************
* Name: esp_hr_timer_start_once
*
* Description:
- * Start the High Resolution Timer with one shot mode.
+ * Start a high-resolution timer in one-shot mode.
*
* Input Parameters:
- * timer - HR Timer pointer.
- * timeout - Timeout value.
+ * timer - Timer instance to start.
+ * timeout - One-shot timeout in microseconds.
*
* Returned Value:
* OK on success; ERROR on failure.
@@ -152,11 +140,11 @@ int esp_hr_timer_start_once(struct esp_hr_timer_s *timer, uint64_t timeout);
* Name: esp_hr_timer_start_periodic
*
* Description:
- * Start the High Resolution Timer with periodic mode.
+ * Start a high-resolution timer in periodic mode.
*
* Input Parameters:
- * timer - HR Timer pointer.
- * timeout - Timeout value.
+ * timer - Timer instance to start.
+ * timeout - Period interval in microseconds.
*
* Returned Value:
* OK on success; ERROR on failure.
@@ -164,16 +152,16 @@ int esp_hr_timer_start_once(struct esp_hr_timer_s *timer, uint64_t timeout);
****************************************************************************/
int esp_hr_timer_start_periodic(struct esp_hr_timer_s *timer,
- uint64_t timeout);
+ uint64_t timeout);
/****************************************************************************
* Name: esp_hr_timer_stop
*
* Description:
- * Stop the High Resolution Timer.
+ * Stop a running high-resolution timer.
*
* Input Parameters:
- * timer - HR Timer pointer.
+ * timer - Timer instance to stop.
*
* Returned Value:
* OK on success; ERROR on failure.
@@ -181,16 +169,31 @@ int esp_hr_timer_start_periodic(struct esp_hr_timer_s *timer,
****************************************************************************/
int esp_hr_timer_stop(struct esp_hr_timer_s *timer);
+
+/****************************************************************************
+ * Name: esp_hr_timer_stop_nolock
+ *
+ * Description:
+ * Stop a running high-resolution timer without taking any adapter lock.
+ *
+ * Input Parameters:
+ * timer - Timer instance to stop.
+ *
+ * Returned Value:
+ * OK on success; ERROR on failure.
+ *
+ ****************************************************************************/
+
int esp_hr_timer_stop_nolock(struct esp_hr_timer_s *timer);
/****************************************************************************
* Name: esp_hr_timer_delete
*
* Description:
- * Stop and delete the High Resolution Timer.
+ * Delete a timer instance and release associated resources.
*
* Input Parameters:
- * timer - HR Timer pointer.
+ * timer - Timer instance to delete.
*
* Returned Value:
* OK on success; ERROR on failure.
@@ -203,13 +206,13 @@ int esp_hr_timer_delete(struct esp_hr_timer_s *timer);
* Name: esp_hr_timer_time_us
*
* Description:
- * Get time of the High Resolution Timer in microseconds.
+ * Return the current high-resolution timer time in microseconds.
*
* Input Parameters:
* None.
*
* Returned Value:
- * Time of the HR Timer in microseconds.
+ * Current timer time in microseconds.
*
****************************************************************************/
@@ -219,13 +222,13 @@ uint64_t esp_hr_timer_time_us(void);
* Name: esp_hr_timer_get_alarm
*
* Description:
- * Get the timestamp when the next timeout is expected to occur.
+ * Return the timestamp of the next scheduled timer alarm.
*
* Input Parameters:
* None.
*
* Returned Value:
- * Timestamp of the nearest timer event in microseconds.
+ * Absolute time in microseconds for the next alarm.
*
****************************************************************************/
@@ -235,10 +238,12 @@ uint64_t esp_hr_timer_get_alarm(void);
* Name: esp_hr_timer_calibration
*
* Description:
- * Adjust current High Resolution Timer by a certain value.
+ * Apply a timer calibration adjustment.
+ * The ESP timer backend does not support calibration, so this operation
+ * is a no-op.
*
* Input Parameters:
- * time_us - Adjustment to apply to the HR Timer in microseconds.
+ * time_us - Calibration adjustment in microseconds.
*
* Returned Value:
* None.
@@ -251,10 +256,10 @@ void esp_hr_timer_calibration(uint64_t time_us);
* Name: esp_hr_timer_set
*
* Description:
- * Set the High Resolution Timer counter value to a specific timestamp.
+ * Set the high-resolution timer counter to a specific timestamp.
*
* Input Parameters:
- * new_us - New counter value in microseconds.
+ * new_us - New timer value in microseconds.
*
* Returned Value:
* None.
@@ -267,8 +272,7 @@ void esp_hr_timer_set(uint64_t new_us);
* Name: esp_hr_timer_lock
*
* Description:
- * Acquire the High Resolution Timer lock to protect access to hardware
- * registers. Must be paired with esp_hr_timer_unlock().
+ * Acquire the adapter lock used to serialize timer operations.
*
* Input Parameters:
* None.
@@ -284,8 +288,7 @@ void esp_hr_timer_lock(void);
* Name: esp_hr_timer_unlock
*
* Description:
- * Release the High Resolution Timer lock. Must be paired with
- * esp_hr_timer_lock().
+ * Release the adapter lock used to serialize timer operations.
*
* Input Parameters:
* None.
@@ -301,14 +304,13 @@ void esp_hr_timer_unlock(void);
* Name: esp_hr_timer_init
*
* Description:
- * Initialize High Resolution Timer.
+ * Initialize the timer adapter and the underlying ESP timer subsystem.
*
* Input Parameters:
* None.
*
* Returned Value:
- * Zero (OK) is returned on success; a negated errno value is returned on
- * any failure.
+ * OK on success; ERROR on failure.
*
****************************************************************************/
@@ -319,4 +321,4 @@ int esp_hr_timer_init(void);
#endif
#undef EXTERN
-#endif /* __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_HR_TIMER_H */
+#endif /* __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_TIMER_ADAPTER_H */
diff --git a/arch/risc-v/src/common/espressif/esp_wifi_utils.c b/arch/risc-v/src/common/espressif/esp_wifi_utils.c
index c41b65bcf69..f71f0e77ddc 100644
--- a/arch/risc-v/src/common/espressif/esp_wifi_utils.c
+++ b/arch/risc-v/src/common/espressif/esp_wifi_utils.c
@@ -35,7 +35,6 @@
#include <nuttx/kmalloc.h>
#include <nuttx/wireless/wireless.h>
-#include "esp_timer.h"
#include "esp_wifi_utils.h"
#include "esp_wlan_netdev.h"
diff --git a/arch/risc-v/src/esp32c3/hal_esp32c3.cmake b/arch/risc-v/src/esp32c3/hal_esp32c3.cmake
index 398232de84d..c537ba0263d 100644
--- a/arch/risc-v/src/esp32c3/hal_esp32c3.cmake
+++ b/arch/risc-v/src/esp32c3/hal_esp32c3.cmake
@@ -339,7 +339,16 @@ list(
${ESP_HAL_3RDPARTY_REPO}/components/esp_system/port/soc/${CHIP_SERIES}/system_internal.c
)
-# ESP Timer sources (not in CHIP_CSRCS in hal_esp32c3.mk - removed to match
+# ESP Timer
+list(
+ APPEND
+ HAL_SRCS
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer_init.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer_impl_systimer.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/system_time.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer_impl_common.c)
+
# Make) HAL sources (paths from hal_esp32c3.mk: esp_hal_* and hal components)
list(
APPEND
diff --git a/arch/risc-v/src/esp32c3/hal_esp32c3.mk b/arch/risc-v/src/esp32c3/hal_esp32c3.mk
index a5dd89601f5..bfd250f5596 100644
--- a/arch/risc-v/src/esp32c3/hal_esp32c3.mk
+++ b/arch/risc-v/src/esp32c3/hal_esp32c3.mk
@@ -238,6 +238,11 @@ CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)startup.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)startup_funcs.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)system_time.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer_init.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer_impl_systimer.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)system_time.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer_impl_common.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)port$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)clk.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)port$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)system_internal.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hal_ana_conv$(DELIM)adc_hal_common.c
diff --git a/arch/risc-v/src/esp32c6/hal_esp32c6.cmake b/arch/risc-v/src/esp32c6/hal_esp32c6.cmake
index 337642cedce..009462aca60 100644
--- a/arch/risc-v/src/esp32c6/hal_esp32c6.cmake
+++ b/arch/risc-v/src/esp32c6/hal_esp32c6.cmake
@@ -346,6 +346,16 @@ list(
${ESP_HAL_3RDPARTY_REPO}/components/esp_system/port/soc/${CHIP_SERIES}/system_internal.c
)
+# ESP Timer
+list(
+ APPEND
+ HAL_SRCS
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer_init.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer_impl_systimer.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/system_time.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer_impl_common.c)
+
# HAL components
list(
APPEND
diff --git a/arch/risc-v/src/esp32c6/hal_esp32c6.mk b/arch/risc-v/src/esp32c6/hal_esp32c6.mk
index fe0a60cbc5b..6b424c69153 100644
--- a/arch/risc-v/src/esp32c6/hal_esp32c6.mk
+++ b/arch/risc-v/src/esp32c6/hal_esp32c6.mk
@@ -264,6 +264,11 @@ CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)startup.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)startup_funcs.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)system_time.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer_init.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer_impl_systimer.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)system_time.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer_impl_common.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)port$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)clk.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)port$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)system_internal.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hal_ana_conv$(DELIM)adc_hal_common.c
diff --git a/arch/risc-v/src/esp32h2/hal_esp32h2.cmake b/arch/risc-v/src/esp32h2/hal_esp32h2.cmake
index b7049a238d1..d3e0ae94762 100644
--- a/arch/risc-v/src/esp32h2/hal_esp32h2.cmake
+++ b/arch/risc-v/src/esp32h2/hal_esp32h2.cmake
@@ -333,6 +333,16 @@ list(
${ESP_HAL_3RDPARTY_REPO}/components/esp_system/port/soc/${CHIP_SERIES}/system_internal.c
)
+# ESP Timer
+list(
+ APPEND
+ HAL_SRCS
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer_init.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer_impl_systimer.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/system_time.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer_impl_common.c)
+
# HAL components
list(
APPEND
diff --git a/arch/risc-v/src/esp32h2/hal_esp32h2.mk b/arch/risc-v/src/esp32h2/hal_esp32h2.mk
index d7763da5a9a..dcbc41f1cab 100644
--- a/arch/risc-v/src/esp32h2/hal_esp32h2.mk
+++ b/arch/risc-v/src/esp32h2/hal_esp32h2.mk
@@ -245,6 +245,11 @@ CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)startup.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)startup_funcs.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)system_time.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer_init.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer_impl_systimer.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)system_time.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer_impl_common.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)port$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)clk.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)port$(DELIM)soc$(DELIM)$(CHIP_SERIES)$(DELIM)system_internal.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hal_ana_conv$(DELIM)adc_hal_common.c
diff --git a/arch/risc-v/src/esp32p4/hal_esp32p4.cmake b/arch/risc-v/src/esp32p4/hal_esp32p4.cmake
index 7b4e5dd0c9c..93e492540a7 100644
--- a/arch/risc-v/src/esp32p4/hal_esp32p4.cmake
+++ b/arch/risc-v/src/esp32p4/hal_esp32p4.cmake
@@ -128,6 +128,7 @@ set(ESP32P4_INCLUDES
${ESP_HAL_3RDPARTY_REPO}/components/esp_system/port/include/private
${ESP_HAL_3RDPARTY_REPO}/components/esp_system/port/public_compat
${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/include
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/private_include
${ESP_HAL_3RDPARTY_REPO}/components/hal/${CHIP_SERIES}/include
${ESP_HAL_3RDPARTY_REPO}/components/hal/include
${ESP_HAL_3RDPARTY_REPO}/components/hal/platform_port/include
@@ -342,6 +343,11 @@ list(
${ESP_HAL_3RDPARTY_REPO}/components/esp_system/startup_funcs.c
${ESP_HAL_3RDPARTY_REPO}/components/esp_system/startup.c
${ESP_HAL_3RDPARTY_REPO}/components/esp_system/system_time.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer_init.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer_impl_systimer.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/system_time.c
+ ${ESP_HAL_3RDPARTY_REPO}/components/esp_timer/src/esp_timer_impl_common.c
${ESP_HAL_3RDPARTY_REPO}/components/hal/${CHIP_SERIES}/efuse_hal.c
${ESP_HAL_3RDPARTY_REPO}/components/hal/cache_hal.c
${ESP_HAL_3RDPARTY_REPO}/components/hal/efuse_hal.c
diff --git a/arch/risc-v/src/esp32p4/hal_esp32p4.mk b/arch/risc-v/src/esp32p4/hal_esp32p4.mk
index 7a7c24c88c6..eb78122edb7 100644
--- a/arch/risc-v/src/esp32p4/hal_esp32p4.mk
+++ b/arch/risc-v/src/esp32p4/hal_esp32p4.mk
@@ -118,6 +118,7 @@ INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY
INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)port$(DELIM)include$(DELIM)private
INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)port$(DELIM)public_compat
INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)include
+INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)private_include
INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)$(CHIP_SERIES)$(DELIM)include
INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)include
INCLUDES += $(INCDIR_PREFIX)$(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)platform_port$(DELIM)include
@@ -300,6 +301,11 @@ CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)startup_funcs.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)startup.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_system$(DELIM)system_time.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer_init.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer_impl_systimer.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)system_time.c
+CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_timer$(DELIM)src$(DELIM)esp_timer_impl_common.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)$(CHIP_SERIES)$(DELIM)efuse_hal.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)cache_hal.c
CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)hal$(DELIM)efuse_hal.c
diff --git a/boards/risc-v/esp32c3/esp32-c3-zero/src/esp32c3_bringup.c b/boards/risc-v/esp32c3/esp32-c3-zero/src/esp32c3_bringup.c
index 96c49797ed6..373fffa97a3 100644
--- a/boards/risc-v/esp32c3/esp32-c3-zero/src/esp32c3_bringup.c
+++ b/boards/risc-v/esp32c3/esp32-c3-zero/src/esp32c3_bringup.c
@@ -53,7 +53,7 @@
#endif
#ifdef CONFIG_TIMER
-# include "espressif/esp_timer.h"
+# include "espressif/esp_gptimer.h"
#endif
#ifdef CONFIG_ONESHOT
diff --git a/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_bringup.c b/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_bringup.c
index 5fedd6d7d0d..3b7b5e7db69 100644
--- a/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_bringup.c
+++ b/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_bringup.c
@@ -52,7 +52,7 @@
#endif
#ifdef CONFIG_TIMER
-# include "espressif/esp_timer.h"
+# include "espressif/esp_gptimer.h"
#endif
#ifdef CONFIG_ONESHOT
diff --git a/boards/risc-v/esp32c3/esp32c3-xiao/src/esp32c3_bringup.c b/boards/risc-v/esp32c3/esp32c3-xiao/src/esp32c3_bringup.c
index 10b78bf61e4..7fbfa7a43fa 100644
--- a/boards/risc-v/esp32c3/esp32c3-xiao/src/esp32c3_bringup.c
+++ b/boards/risc-v/esp32c3/esp32c3-xiao/src/esp32c3_bringup.c
@@ -48,7 +48,7 @@
#endif
#ifdef CONFIG_TIMER
-# include "espressif/esp_timer.h"
+# include "espressif/esp_gptimer.h"
#endif
#ifdef CONFIG_ONESHOT
diff --git a/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c b/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c
index bf0717cd367..f3bac90e185 100644
--- a/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c
+++ b/boards/risc-v/esp32c6/esp32c6-devkitc/src/esp32c6_bringup.c
@@ -52,7 +52,7 @@
#endif
#ifdef CONFIG_TIMER
-# include "espressif/esp_timer.h"
+# include "espressif/esp_gptimer.h"
#endif
#ifdef CONFIG_ONESHOT
diff --git a/boards/risc-v/esp32c6/esp32c6-devkitm/src/esp32c6_bringup.c b/boards/risc-v/esp32c6/esp32c6-devkitm/src/esp32c6_bringup.c
index 4e4d630abd5..9b18ce9a64f 100644
--- a/boards/risc-v/esp32c6/esp32c6-devkitm/src/esp32c6_bringup.c
+++ b/boards/risc-v/esp32c6/esp32c6-devkitm/src/esp32c6_bringup.c
@@ -48,7 +48,7 @@
#endif
#ifdef CONFIG_TIMER
-# include "espressif/esp_timer.h"
+# include "espressif/esp_gptimer.h"
#endif
#ifdef CONFIG_ONESHOT
diff --git a/boards/risc-v/esp32c6/esp32c6-xiao/src/esp32c6_bringup.c b/boards/risc-v/esp32c6/esp32c6-xiao/src/esp32c6_bringup.c
index d57455df38b..b6c5e5f214d 100644
--- a/boards/risc-v/esp32c6/esp32c6-xiao/src/esp32c6_bringup.c
+++ b/boards/risc-v/esp32c6/esp32c6-xiao/src/esp32c6_bringup.c
@@ -50,7 +50,7 @@
#endif
#ifdef CONFIG_TIMER
-# include "espressif/esp_timer.h"
+# include "espressif/esp_gptimer.h"
#endif
#ifdef CONFIG_ONESHOT
diff --git a/boards/risc-v/esp32h2/esp32h2-devkit/src/esp32h2_bringup.c b/boards/risc-v/esp32h2/esp32h2-devkit/src/esp32h2_bringup.c
index 697ac3a270a..ba670579ac5 100644
--- a/boards/risc-v/esp32h2/esp32h2-devkit/src/esp32h2_bringup.c
+++ b/boards/risc-v/esp32h2/esp32h2-devkit/src/esp32h2_bringup.c
@@ -48,7 +48,7 @@
#endif
#ifdef CONFIG_TIMER
-# include "espressif/esp_timer.h"
+# include "espressif/esp_gptimer.h"
#endif
#ifdef CONFIG_ONESHOT
diff --git a/boards/risc-v/esp32p4/esp32p4-function-ev-board/src/esp32p4_bringup.c b/boards/risc-v/esp32p4/esp32p4-function-ev-board/src/esp32p4_bringup.c
index d3c3a8ae82e..5b6b1cf2265 100644
--- a/boards/risc-v/esp32p4/esp32p4-function-ev-board/src/esp32p4_bringup.c
+++ b/boards/risc-v/esp32p4/esp32p4-function-ev-board/src/esp32p4_bringup.c
@@ -48,7 +48,7 @@
#endif
#ifdef CONFIG_TIMER
-# include "espressif/esp_timer.h"
+# include "espressif/esp_gptimer.h"
#endif
#ifdef CONFIG_ONESHOT