Use small lock to protect resources related to timers in arch risc-v, xtensa and tricore.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
This commit is contained in:
wangzhi16
2025-01-20 09:57:01 -03:00
committed by Alan C. Assis
parent 4b91d76869
commit ecaddbb0aa
6 changed files with 136 additions and 83 deletions
@@ -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);
File diff suppressed because it is too large Load Diff
+7 -5
View File
@@ -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,
+5 -4
View File
@@ -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;
}
File diff suppressed because it is too large Load Diff
+14 -7
View File
@@ -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;
}