This is an automated email from the ASF dual-hosted git repository.

acassis 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 ecaddbb0aa6 Use small lock to protect resources related to timers in 
arch risc-v, xtensa and tricore.
ecaddbb0aa6 is described below

commit ecaddbb0aa6883ad95663611e6b9ec18e6c31f41
Author: wangzhi16 <wangzh...@xiaomi.com>
AuthorDate: Mon Jan 20 16:40:43 2025 +0800

    Use small lock to protect resources related to timers in arch risc-v, 
xtensa and tricore.
    
    Signed-off-by: wangzhi16 <wangzh...@xiaomi.com>
---
 arch/risc-v/src/common/espressif/esp_hr_timer.c   | 13 ++--
 arch/risc-v/src/esp32c3-legacy/esp32c3_rt_timer.c | 81 +++++++++++++---------
 arch/tricore/src/common/tricore_systimer.c        | 12 ++--
 arch/xtensa/src/esp32/esp32_rt_timer.c            |  9 +--
 arch/xtensa/src/esp32s2/esp32s2_rt_timer.c        | 83 ++++++++++++++---------
 arch/xtensa/src/esp32s3/esp32s3_rt_timer.c        | 21 ++++--
 6 files changed, 136 insertions(+), 83 deletions(-)

diff --git a/arch/risc-v/src/common/espressif/esp_hr_timer.c 
b/arch/risc-v/src/common/espressif/esp_hr_timer.c
index 7f27e924a8b..3225a58564e 100644
--- a/arch/risc-v/src/common/espressif/esp_hr_timer.c
+++ b/arch/risc-v/src/common/espressif/esp_hr_timer.c
@@ -34,6 +34,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <sys/types.h>
+#include <sched.h>
 
 #include <nuttx/nuttx.h>
 #include <nuttx/irq.h>
@@ -218,7 +219,8 @@ static int IRAM_ATTR esp_hr_timer_isr(int irq, void 
*context, void *arg)
 
   systimer_ll_clear_alarm_int(priv->hal.dev, SYSTIMER_ALARM_ESPTIMER);
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
+  sched_lock();
 
   /* Check if there is a timer running */
 
@@ -288,7 +290,8 @@ static int IRAM_ATTR esp_hr_timer_isr(int irq, void 
*context, void *arg)
         }
     }
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
+  sched_unlock();
 
   return OK;
 }
@@ -710,7 +713,7 @@ void IRAM_ATTR esp_hr_timer_calibration(uint64_t time_us)
 
 int esp_hr_timer_init(void)
 {
-  struct esp_hr_timer_context_s *priv;
+  struct esp_hr_timer_context_s *priv = &g_hr_timer_context;
   int pid;
 
   if (g_hr_timer_initialized)
@@ -720,6 +723,8 @@ int esp_hr_timer_init(void)
       return OK;
     }
 
+  spin_lock_init(&priv->lock);
+
   pid  = kthread_create(CONFIG_ESPRESSIF_HR_TIMER_TASK_NAME,
                         CONFIG_ESPRESSIF_HR_TIMER_TASK_PRIORITY,
                         CONFIG_ESPRESSIF_HR_TIMER_TASK_STACK_SIZE,
@@ -732,8 +737,6 @@ int esp_hr_timer_init(void)
       return pid;
     }
 
-  priv = &g_hr_timer_context;
-
   list_initialize(&priv->runlist);
   list_initialize(&priv->toutlist);
 
diff --git a/arch/risc-v/src/esp32c3-legacy/esp32c3_rt_timer.c 
b/arch/risc-v/src/esp32c3-legacy/esp32c3_rt_timer.c
index c5f1651e2c7..100d3c025b4 100644
--- a/arch/risc-v/src/esp32c3-legacy/esp32c3_rt_timer.c
+++ b/arch/risc-v/src/esp32c3-legacy/esp32c3_rt_timer.c
@@ -34,9 +34,10 @@
 #include <assert.h>
 #include <errno.h>
 #include <debug.h>
+#include <sched.h>
 
 #include <nuttx/nuttx.h>
-#include <nuttx/irq.h>
+#include <nuttx/spinlock.h>
 #include <nuttx/kthread.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/semaphore.h>
@@ -76,6 +77,7 @@ struct esp32c3_rt_priv_s
   struct list_node runlist;
   struct list_node toutlist;
   struct esp32c3_tim_dev_s *timer;
+  spinlock_t lock;
 };
 
 /****************************************************************************
@@ -109,18 +111,15 @@ static struct esp32c3_rt_priv_s g_rt_priv =
  *
  ****************************************************************************/
 
-static void start_rt_timer(struct rt_timer_s *timer,
-                           uint64_t timeout,
-                           bool repeat)
+static void start_rt_timer_nolock(struct rt_timer_s *timer,
+                                  uint64_t timeout,
+                                  bool repeat)
 {
-  irqstate_t flags;
   struct rt_timer_s *p;
   bool inserted = false;
   uint64_t counter;
   struct esp32c3_rt_priv_s *priv = &g_rt_priv;
 
-  flags = enter_critical_section();
-
   /* Only idle timer can be started */
 
   if (timer->state == RT_TIMER_IDLE)
@@ -178,8 +177,18 @@ static void start_rt_timer(struct rt_timer_s *timer,
           ESP32C3_TIM_SETALRM(priv->timer, true);
         }
     }
+}
+
+static void start_rt_timer(struct rt_timer_s *timer,
+                           uint64_t timeout,
+                           bool repeat)
+{
+  irqstate_t flags;
+  struct esp32c3_rt_priv_s *priv = &g_rt_priv;
 
-  leave_critical_section(flags);
+  flags = spin_lock_irqsave(&priv->lock);
+  start_rt_timer_nolock(timer, timeout, repeat);
+  spin_unlock_irqrestore(&priv->lock, flags);
 }
 
 /****************************************************************************
@@ -197,16 +206,13 @@ static void start_rt_timer(struct rt_timer_s *timer,
  *
  ****************************************************************************/
 
-static void stop_rt_timer(struct rt_timer_s *timer)
+static void stop_rt_timer_nolock(struct rt_timer_s *timer)
 {
-  irqstate_t flags;
   bool ishead;
   struct rt_timer_s *next_timer;
   uint64_t alarm;
   struct esp32c3_rt_priv_s *priv = &g_rt_priv;
 
-  flags = enter_critical_section();
-
   /* "start" function can set the timer's repeat flag, and "stop" function
    * should remove this flag.
    */
@@ -253,8 +259,16 @@ static void stop_rt_timer(struct rt_timer_s *timer)
             }
         }
     }
+}
+
+static void stop_rt_timer(struct rt_timer_s *timer)
+{
+  irqstate_t flags;
+  struct esp32c3_rt_priv_s *priv = &g_rt_priv;
 
-  leave_critical_section(flags);
+  flags = spin_lock_irqsave(&priv->lock);
+  stop_rt_timer_nolock(timer);
+  spin_unlock_irqrestore(&priv->lock, flags);
 }
 
 /****************************************************************************
@@ -280,11 +294,12 @@ static void delete_rt_timer(struct rt_timer_s *timer)
 
   struct esp32c3_rt_priv_s *priv = &g_rt_priv;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
+  sched_lock();
 
   if (timer->state == RT_TIMER_READY)
     {
-      stop_rt_timer(timer);
+      stop_rt_timer_nolock(timer);
     }
   else if (timer->state == RT_TIMER_TIMEOUT)
     {
@@ -307,7 +322,8 @@ static void delete_rt_timer(struct rt_timer_s *timer)
     }
 
 exit:
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
+  sched_unlock();
 }
 
 /****************************************************************************
@@ -345,7 +361,7 @@ static int rt_timer_thread(int argc, char *argv[])
           ASSERT(0);
         }
 
-      flags = enter_critical_section();
+      flags = spin_lock_irqsave(&priv->lock);
 
       /* Process all the timers in list */
 
@@ -368,7 +384,7 @@ static int rt_timer_thread(int argc, char *argv[])
 
           timer->state = RT_TIMER_IDLE;
 
-          leave_critical_section(flags);
+          spin_unlock_irqrestore(&priv->lock, flags);
 
           if (raw_state == RT_TIMER_TIMEOUT)
             {
@@ -381,7 +397,7 @@ static int rt_timer_thread(int argc, char *argv[])
 
           /* Enter critical section for next scanning list */
 
-          flags = enter_critical_section();
+          flags = spin_lock_irqsave(&priv->lock);
 
           if (raw_state == RT_TIMER_TIMEOUT)
             {
@@ -389,12 +405,12 @@ static int rt_timer_thread(int argc, char *argv[])
 
               if (timer->flags & RT_TIMER_REPEAT)
                 {
-                  start_rt_timer(timer, timer->timeout, true);
+                  start_rt_timer_nolock(timer, timer->timeout, true);
                 }
             }
         }
 
-      leave_critical_section(flags);
+      spin_unlock_irqrestore(&priv->lock, flags);
     }
 
   return 0;
@@ -430,7 +446,8 @@ static int rt_timer_isr(int irq, void *context, void *arg)
 
   ESP32C3_TIM_ACKINT(priv->timer);
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
+  sched_lock();
 
   /* Check if there is a timer running */
 
@@ -489,7 +506,8 @@ static int rt_timer_isr(int irq, void *context, void *arg)
         }
     }
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
+  sched_unlock();
 
   return 0;
 }
@@ -645,7 +663,7 @@ uint64_t IRAM_ATTR rt_timer_get_alarm(void)
   struct esp32c3_rt_priv_s *priv = &g_rt_priv;
   uint64_t alarm_value = 0;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
 
   ESP32C3_TIM_GETCTR(priv->timer, &counter);
   counter = CYCLES_TO_USEC(counter);
@@ -661,7 +679,7 @@ uint64_t IRAM_ATTR rt_timer_get_alarm(void)
       alarm_value -= counter;
     }
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 
   return alarm_value;
 }
@@ -686,13 +704,13 @@ void IRAM_ATTR rt_timer_calibration(uint64_t time_us)
   struct esp32c3_rt_priv_s *priv = &g_rt_priv;
   irqstate_t flags;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
   ESP32C3_TIM_GETCTR(priv->timer, &counter);
   counter = CYCLES_TO_USEC(counter);
   counter += time_us;
   ESP32C3_TIM_SETCTR(priv->timer, USEC_TO_CYCLES(counter));
   ESP32C3_TIM_RLD_NOW(priv->timer);
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 }
 
 /****************************************************************************
@@ -715,6 +733,7 @@ int esp32c3_rt_timer_init(void)
   irqstate_t flags;
   struct esp32c3_rt_priv_s *priv = &g_rt_priv;
 
+  spin_lock_init(&priv->lock);
   priv->timer = esp32c3_tim_init(ESP32C3_RT_TIMER);
   if (priv->timer == NULL)
     {
@@ -739,7 +758,7 @@ int esp32c3_rt_timer_init(void)
 
   priv->pid = (pid_t)pid;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
 
   /* ESP32-C3 hardware timer configuration:
    * 1 count = 1/16 us
@@ -756,7 +775,7 @@ int esp32c3_rt_timer_init(void)
   ESP32C3_TIM_ENABLEINT(priv->timer);
   ESP32C3_TIM_START(priv->timer);
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 
   return 0;
 }
@@ -780,7 +799,7 @@ void esp32c3_rt_timer_deinit(void)
   irqstate_t flags;
   struct esp32c3_rt_priv_s *priv = &g_rt_priv;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
 
   ESP32C3_TIM_STOP(priv->timer);
   ESP32C3_TIM_DISABLEINT(priv->timer);
@@ -788,7 +807,7 @@ void esp32c3_rt_timer_deinit(void)
   esp32c3_tim_deinit(priv->timer);
   priv->timer = NULL;
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 
   if (priv->pid != INVALID_PROCESS_ID)
     {
diff --git a/arch/tricore/src/common/tricore_systimer.c 
b/arch/tricore/src/common/tricore_systimer.c
index 4c9f933b074..84482bb7146 100644
--- a/arch/tricore/src/common/tricore_systimer.c
+++ b/arch/tricore/src/common/tricore_systimer.c
@@ -24,7 +24,7 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/irq.h>
+#include <nuttx/spinlock.h>
 #include <nuttx/kmalloc.h>
 
 #include <nuttx/timers/oneshot.h>
@@ -51,6 +51,7 @@ struct tricore_systimer_lowerhalf_s
   uint64_t                   alarm;
   oneshot_callback_t         callback;
   void                       *arg;
+  spinlock_t                 lock;
 };
 
 /****************************************************************************
@@ -94,11 +95,11 @@ tricore_systimer_get_time(struct 
tricore_systimer_lowerhalf_s *priv)
   irqstate_t flags;
   uint64_t ticks;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
 
   ticks = IfxStm_get(priv->tbase);
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 
   return ticks;
 }
@@ -109,11 +110,11 @@ tricore_systimer_set_timecmp(struct 
tricore_systimer_lowerhalf_s *priv,
 {
   irqstate_t flags;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
 
   IfxStm_updateCompare(priv->tbase, IfxStm_Comparator_0, value);
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 }
 
 /****************************************************************************
@@ -315,6 +316,7 @@ tricore_systimer_initialize(volatile void *tbase, int irq, 
uint64_t freq)
 
   priv->tbase = tbase;
   priv->freq  = freq;
+  spin_lock_init(&priv->lock);
 
   IfxStm_setCompareControl(tbase,
       IfxStm_Comparator_0,
diff --git a/arch/xtensa/src/esp32/esp32_rt_timer.c 
b/arch/xtensa/src/esp32/esp32_rt_timer.c
index e923e5c3598..d6d6d74ff12 100644
--- a/arch/xtensa/src/esp32/esp32_rt_timer.c
+++ b/arch/xtensa/src/esp32/esp32_rt_timer.c
@@ -263,7 +263,7 @@ static void delete_rt_timer(struct rt_timer_s *timer)
   irqstate_t flags;
   struct esp32_rt_priv_s *priv = &g_rt_priv;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
 
   if (timer->state == RT_TIMER_READY)
     {
@@ -282,7 +282,7 @@ static void delete_rt_timer(struct rt_timer_s *timer)
   timer->state = RT_TIMER_DELETE;
 
 exit:
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 }
 
 /****************************************************************************
@@ -684,6 +684,7 @@ int esp32_rt_timer_init(void)
   struct esp32_tim_dev_s *tim;
   struct esp32_rt_priv_s *priv = &g_rt_priv;
 
+  spin_lock_init(&priv->lock);
   tim = esp32_tim_init(ESP32_RT_TIMER);
   if (!tim)
     {
@@ -709,7 +710,7 @@ int esp32_rt_timer_init(void)
   priv->timer = tim;
   priv->pid   = (pid_t)pid;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
 
   /* ESP32 hardware timer configuration:
    *   - 1 counter = 1us
@@ -727,7 +728,7 @@ int esp32_rt_timer_init(void)
 
   ESP32_TIM_START(tim);
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 
   return 0;
 }
diff --git a/arch/xtensa/src/esp32s2/esp32s2_rt_timer.c 
b/arch/xtensa/src/esp32s2/esp32s2_rt_timer.c
index ce26aec5e74..d2db31421a1 100644
--- a/arch/xtensa/src/esp32s2/esp32s2_rt_timer.c
+++ b/arch/xtensa/src/esp32s2/esp32s2_rt_timer.c
@@ -32,9 +32,10 @@
 #include <assert.h>
 #include <errno.h>
 #include <debug.h>
+#include <sched.h>
 
 #include <nuttx/nuttx.h>
-#include <nuttx/irq.h>
+#include <nuttx/spinlock.h>
 #include <nuttx/kthread.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/semaphore.h>
@@ -80,6 +81,7 @@ struct esp32s2_rt_priv_s
   struct list_node runlist;
   struct list_node toutlist;
   struct esp32s2_tim_dev_s *timer;
+  spinlock_t lock;
 };
 
 /****************************************************************************
@@ -114,18 +116,15 @@ static struct esp32s2_rt_priv_s g_rt_priv =
  *
  ****************************************************************************/
 
-static void start_rt_timer(struct rt_timer_s *timer,
-                           uint64_t timeout,
-                           bool repeat)
+static void start_rt_timer_nolock(struct rt_timer_s *timer,
+                                  uint64_t timeout,
+                                  bool repeat)
 {
-  irqstate_t flags;
   struct rt_timer_s *temp_p;
   bool inserted = false;
   uint64_t counter;
   struct esp32s2_rt_priv_s *priv = &g_rt_priv;
 
-  flags = enter_critical_section();
-
   /* Only idle timer can be started */
 
   if (timer->state == RT_TIMER_IDLE)
@@ -188,8 +187,20 @@ static void start_rt_timer(struct rt_timer_s *timer,
       tmrwarn("WARN: Timer not in idle mode.\n"\
              "Only idle timer can be started!\n");
     }
+}
+
+static void start_rt_timer(struct rt_timer_s *timer,
+                           uint64_t timeout,
+                           bool repeat)
+{
+  irqstate_t flags;
+  struct esp32s2_rt_priv_s *priv = &g_rt_priv;
 
-  leave_critical_section(flags);
+  flags = spin_lock_irqsave(&priv->lock);
+  sched_lock();
+  start_rt_timer_nolock(timer, timeout, repeat);
+  spin_unlock_irqrestore(&priv->lock, flags);
+  sched_unlock();
 }
 
 /****************************************************************************
@@ -207,16 +218,13 @@ static void start_rt_timer(struct rt_timer_s *timer,
  *
  ****************************************************************************/
 
-static void stop_rt_timer(struct rt_timer_s *timer)
+static void stop_rt_timer_nolock(struct rt_timer_s *timer)
 {
-  irqstate_t flags;
   bool ishead;
   struct rt_timer_s *next_timer;
   uint64_t alarm;
   struct esp32s2_rt_priv_s *priv = &g_rt_priv;
 
-  flags = enter_critical_section();
-
   /* "start" function can set the timer's repeat flag, and "stop" function
    * should remove this flag.
    */
@@ -263,8 +271,16 @@ static void stop_rt_timer(struct rt_timer_s *timer)
             }
         }
     }
+}
+
+static void stop_rt_timer(struct rt_timer_s *timer)
+{
+  irqstate_t flags;
+  struct esp32s2_rt_priv_s *priv = &g_rt_priv;
 
-  leave_critical_section(flags);
+  flags = spin_lock_irqsave(&priv->lock);
+  stop_rt_timer_nolock(timer);
+  spin_unlock_irqrestore(&priv->lock, flags);
 }
 
 /****************************************************************************
@@ -289,11 +305,12 @@ static void delete_rt_timer(struct rt_timer_s *timer)
   irqstate_t flags;
   struct esp32s2_rt_priv_s *priv = &g_rt_priv;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
+  sched_lock();
 
   if (timer->state == RT_TIMER_READY)
     {
-      stop_rt_timer(timer);
+      stop_rt_timer_nolock(timer);
     }
   else if (timer->state == RT_TIMER_TIMEOUT)
     {
@@ -316,7 +333,8 @@ static void delete_rt_timer(struct rt_timer_s *timer)
     }
 
 exit:
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
+  sched_unlock();
 }
 
 /****************************************************************************
@@ -354,7 +372,7 @@ static int rt_timer_thread(int argc, char *argv[])
           ASSERT(0);
         }
 
-      flags = enter_critical_section();
+      flags = spin_lock_irqsave(&priv->lock);
 
       /* Process all the timers in list */
 
@@ -377,7 +395,7 @@ static int rt_timer_thread(int argc, char *argv[])
 
           timer->state = RT_TIMER_IDLE;
 
-          leave_critical_section(flags);
+          spin_unlock_irqrestore(&priv->lock, flags);
 
           if (raw_state == RT_TIMER_TIMEOUT)
             {
@@ -390,7 +408,7 @@ static int rt_timer_thread(int argc, char *argv[])
 
           /* Enter critical section for next scanning list */
 
-          flags = enter_critical_section();
+          flags = spin_lock_irqsave(&priv->lock);
 
           if (raw_state == RT_TIMER_TIMEOUT)
             {
@@ -398,12 +416,12 @@ static int rt_timer_thread(int argc, char *argv[])
 
               if (timer->flags & RT_TIMER_REPEAT)
                 {
-                  start_rt_timer(timer, timer->timeout, true);
+                  start_rt_timer_nolock(timer, timer->timeout, true);
                 }
             }
         }
 
-      leave_critical_section(flags);
+      spin_unlock_irqrestore(&priv->lock, flags);
     }
 
   return 0;
@@ -439,7 +457,8 @@ static int rt_timer_isr(int irq, void *context, void *arg)
 
   ESP32S2_TIM_ACKINT(priv->timer);
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
+  sched_lock();
 
   /* Check if there is a timer running */
 
@@ -498,7 +517,8 @@ static int rt_timer_isr(int irq, void *context, void *arg)
         }
     }
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
+  sched_unlock();
 
   return 0;
 }
@@ -654,7 +674,7 @@ uint64_t IRAM_ATTR rt_timer_get_alarm(void)
   struct esp32s2_rt_priv_s *priv = &g_rt_priv;
   uint64_t alarm_value = 0;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
 
   ESP32S2_TIM_GETCTR(priv->timer, &counter);
   counter = CYCLES_TO_USEC(counter);
@@ -670,7 +690,7 @@ uint64_t IRAM_ATTR rt_timer_get_alarm(void)
       alarm_value -= counter;
     }
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 
   return alarm_value;
 }
@@ -695,13 +715,13 @@ void IRAM_ATTR rt_timer_calibration(uint64_t time_us)
   struct esp32s2_rt_priv_s *priv = &g_rt_priv;
   irqstate_t flags;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
   ESP32S2_TIM_GETCTR(priv->timer, &counter);
   counter = CYCLES_TO_USEC(counter);
   counter += time_us;
   ESP32S2_TIM_SETCTR(priv->timer, USEC_TO_CYCLES(counter));
   ESP32S2_TIM_RLD_NOW(priv->timer);
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 }
 
 /****************************************************************************
@@ -728,6 +748,7 @@ int esp32s2_rt_timer_init(void)
   uint16_t pre;
   uint16_t ticks;
 
+  spin_lock_init(&priv->lock);
   tim = esp32s2_tim_init(SYSTIMER_COMP0);
 
   if (tim == NULL)
@@ -754,7 +775,7 @@ int esp32s2_rt_timer_init(void)
   priv->pid = (pid_t)pid;
   priv->timer = tim;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
 
   /* ESP32-S2 hardware timer configuration, acc. TRM V1.0
    * Systimer is clocked by APB_CLK.
@@ -798,7 +819,7 @@ int esp32s2_rt_timer_init(void)
   ESP32S2_TIM_SETISR(priv->timer, rt_timer_isr, NULL);
   ESP32S2_TIM_ENABLEINT(priv->timer);
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 
   return 0;
 }
@@ -822,14 +843,14 @@ void esp32s2_rt_timer_deinit(void)
   irqstate_t flags;
   struct esp32s2_rt_priv_s *priv = &g_rt_priv;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
 
   ESP32S2_TIM_DISABLEINT(priv->timer);
   ESP32S2_TIM_SETISR(priv->timer, NULL, NULL);
   esp32s2_tim_deinit(priv->timer);
   priv->timer = NULL;
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 
   if (priv->pid != INVALID_PROCESS_ID)
     {
diff --git a/arch/xtensa/src/esp32s3/esp32s3_rt_timer.c 
b/arch/xtensa/src/esp32s3/esp32s3_rt_timer.c
index 7762dfabafb..917e8e12c79 100644
--- a/arch/xtensa/src/esp32s3/esp32s3_rt_timer.c
+++ b/arch/xtensa/src/esp32s3/esp32s3_rt_timer.c
@@ -32,9 +32,10 @@
 #include <stdint.h>
 #include <string.h>
 #include <sys/types.h>
+#include <sched.h>
 
 #include <nuttx/nuttx.h>
-#include <nuttx/irq.h>
+#include <nuttx/spinlock.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/kthread.h>
 #include <nuttx/semaphore.h>
@@ -611,7 +612,8 @@ static int rt_timer_isr(int irq, void *context, void *arg)
 
   modifyreg32(SYSTIMER_INT_CLR_REG, 0, SYSTIMER_TARGET2_INT_CLR);
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
+  sched_lock();
 
   /* Check if there is a timer running */
 
@@ -672,7 +674,8 @@ static int rt_timer_isr(int irq, void *context, void *arg)
         }
     }
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
+  sched_unlock();
 
   return OK;
 }
@@ -805,7 +808,8 @@ void esp32s3_rt_timer_delete(struct rt_timer_s *timer)
   irqstate_t flags;
   struct esp32s3_rt_priv_s *priv = &g_rt_priv;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
+  sched_lock();
 
   if (timer->state == RT_TIMER_READY)
     {
@@ -832,7 +836,8 @@ void esp32s3_rt_timer_delete(struct rt_timer_s *timer)
     }
 
 exit:
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
+  sched_unlock();
 }
 
 /****************************************************************************
@@ -949,6 +954,8 @@ int esp32s3_rt_timer_init(void)
   irqstate_t flags;
   struct esp32s3_rt_priv_s *priv = &g_rt_priv;
 
+  spin_lock_init(&priv->lock);
+
   pid = kthread_create(RT_TIMER_TASK_NAME,
                        RT_TIMER_TASK_PRIORITY,
                        RT_TIMER_TASK_STACK_SIZE,
@@ -965,7 +972,7 @@ int esp32s3_rt_timer_init(void)
 
   priv->pid = (pid_t)pid;
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
 
   /* ESP32-S3 hardware timer configuration:
    * 1 count = 1/16 us
@@ -1007,7 +1014,7 @@ int esp32s3_rt_timer_init(void)
 
   modifyreg32(SYSTIMER_CONF_REG, 0, SYSTIMER_TIMER_UNIT1_WORK_EN);
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 
   return OK;
 }

Reply via email to