diff --git a/arch/xtensa/src/esp32/esp32_tim_lowerhalf.c b/arch/xtensa/src/esp32/esp32_tim_lowerhalf.c index 30e9c2c97d8..0506b33bc0b 100644 --- a/arch/xtensa/src/esp32/esp32_tim_lowerhalf.c +++ b/arch/xtensa/src/esp32/esp32_tim_lowerhalf.c @@ -23,21 +23,24 @@ ****************************************************************************/ #include -#include -#include -#include + #include #include #include #include #include +#include + +#include +#include +#include #include "xtensa.h" #include "hardware/esp32_soc.h" -#include "esp32_tim.h" #include "esp32_clockconfig.h" +#include "esp32_tim.h" /**************************************************************************** * Pre-processor Definitions @@ -53,12 +56,13 @@ struct esp32_timer_lowerhalf_s { - const struct timer_ops_s *ops; /* Lower half operations */ - struct esp32_tim_dev_s *tim; /* esp32 timer driver */ - tccb_t callback; /* Current user interrupt callback */ - void *arg; /* Argument passed to upper half callback */ - bool started; /* True: Timer has been started */ - void *upper; /* Pointer to watchdog_upperhalf_s */ + const struct timer_ops_s *ops; /* Lower half operations */ + struct esp32_tim_dev_s *tim; /* esp32 timer driver */ + tccb_t callback; /* Current user interrupt callback */ + void *arg; /* Argument passed to upper half callback */ + bool started; /* True: Timer has been started */ + void *upper; /* Pointer to watchdog_upperhalf_s */ + spinlock_t lock; /* Device specific lock */ }; /**************************************************************************** @@ -247,9 +251,9 @@ static int esp32_timer_start(struct timer_lowerhalf_s *lower) if (priv->callback != NULL) { - flags = enter_critical_section(); + flags = spin_lock_irqsave(&priv->lock); ret = ESP32_TIM_SETISR(priv->tim, esp32_timer_handler, priv); - leave_critical_section(flags); + spin_unlock_irqrestore(&priv->lock, flags); if (ret != OK) { goto errout; @@ -300,9 +304,11 @@ static int esp32_timer_stop(struct timer_lowerhalf_s *lower) } ESP32_TIM_DISABLEINT(priv->tim); - flags = enter_critical_section(); + + flags = spin_lock_irqsave(&priv->lock); ret = ESP32_TIM_SETISR(priv->tim, NULL, NULL); - leave_critical_section(flags); + spin_unlock_irqrestore(&priv->lock, flags); + ESP32_TIM_STOP(priv->tim); priv->started = false; @@ -467,7 +473,7 @@ static void esp32_timer_setcallback(struct timer_lowerhalf_s *lower, priv->callback = callback; priv->arg = arg; - flags = enter_critical_section(); + flags = spin_lock_irqsave(&priv->lock); /* There is a user callback and the timer has already been started */ @@ -482,7 +488,7 @@ static void esp32_timer_setcallback(struct timer_lowerhalf_s *lower, ret = ESP32_TIM_SETISR(priv->tim, NULL, NULL); } - leave_critical_section(flags); + spin_unlock_irqrestore(&priv->lock, flags); assert(ret == OK); }