Use small lock to protect timer related resources in arch ARM.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
This commit is contained in:
wangzhi16
2025-01-20 11:52:51 +08:00
committed by Xiang Xiao
parent e1285d73fa
commit 5f77da4c51
3 changed files with 39 additions and 24 deletions
+14 -8
View File
@@ -35,8 +35,10 @@
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <debug.h> #include <debug.h>
#include <sched.h>
#include <nuttx/irq.h> #include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <nuttx/timers/timer.h> #include <nuttx/timers/timer.h>
#include <arch/board/board.h> #include <arch/board/board.h>
#include <arch/chip/timer.h> #include <arch/chip/timer.h>
@@ -91,12 +93,13 @@ struct cxd56_lowerhalf_s
/* Private data */ /* Private data */
uint32_t base; /* Base address of the timer */ uint32_t base; /* Base address of the timer */
tccb_t callback; /* Current user interrupt callback */ tccb_t callback; /* Current user interrupt callback */
void *arg; /* Argument passed to upper half callback */ void *arg; /* Argument passed to upper half callback */
uint32_t timeout; /* The current timeout value (us) */ uint32_t timeout; /* The current timeout value (us) */
uint32_t clkticks; /* actual clock ticks for current interval */ uint32_t clkticks; /* actual clock ticks for current interval */
bool started; /* The timer has been started */ bool started; /* The timer has been started */
spinlock_t lock; /* Spinlock */
}; };
/**************************************************************************** /****************************************************************************
@@ -430,7 +433,8 @@ static void cxd56_setcallback(struct timer_lowerhalf_s *lower,
struct cxd56_lowerhalf_s *priv = (struct cxd56_lowerhalf_s *)lower; struct cxd56_lowerhalf_s *priv = (struct cxd56_lowerhalf_s *)lower;
irqstate_t flags; irqstate_t flags;
flags = enter_critical_section(); flags = spin_lock_irqsave(&priv->lock);
sched_lock();
DEBUGASSERT(priv); DEBUGASSERT(priv);
tmrinfo("Entry: callback=%p\n", callback); tmrinfo("Entry: callback=%p\n", callback);
@@ -440,7 +444,8 @@ static void cxd56_setcallback(struct timer_lowerhalf_s *lower,
priv->callback = callback; priv->callback = callback;
priv->arg = arg; priv->arg = arg;
leave_critical_section(flags); spin_unlock_irqrestore(&priv->lock, flags);
sched_unlock();
} }
/**************************************************************************** /****************************************************************************
@@ -559,6 +564,7 @@ void cxd56_timer_initialize(const char *devpath, int timer)
} }
priv->ops = &g_tmrops; priv->ops = &g_tmrops;
spin_lock_init(&priv->lock);
irq_attach(irq, cxd56_timer_interrupt, priv); irq_attach(irq, cxd56_timer_interrupt, priv);
+9 -6
View File
@@ -33,6 +33,7 @@
#include <debug.h> #include <debug.h>
#include <nuttx/irq.h> #include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <nuttx/arch.h> #include <nuttx/arch.h>
#include <nuttx/timers/pwm.h> #include <nuttx/timers/pwm.h>
#include <arch/board/board.h> #include <arch/board/board.h>
@@ -91,6 +92,7 @@ struct lpc17_40_timer_s
uint32_t pincfg; /* Output pin configuration */ uint32_t pincfg; /* Output pin configuration */
uint32_t pclk; /* The frequency of the peripheral clock uint32_t pclk; /* The frequency of the peripheral clock
* that drives the timer module. */ * that drives the timer module. */
spinlock_t lock; /* Spinlock */
}; };
/**************************************************************************** /****************************************************************************
@@ -154,6 +156,7 @@ static struct lpc17_40_timer_s g_pwm1dev =
.base = LPC17_40_TMR1_BASE, .base = LPC17_40_TMR1_BASE,
.pincfg = GPIO_MAT0p1_2, .pincfg = GPIO_MAT0p1_2,
.pclk = (0x1 << 12), .pclk = (0x1 << 12),
.lock = SP_UNLOCKED,
}; };
#endif #endif
@@ -266,7 +269,7 @@ static int timer_timer(struct lpc17_40_timer_s *priv,
irqstate_t flags; irqstate_t flags;
uint32_t regval; uint32_t regval;
flags = enter_critical_section(); flags = spin_lock_irqsave(&priv->lock);
putreg32(info->frequency, LPC17_40_TMR0_MR1); /* Set TIMER0 MR1 = number of counts */ putreg32(info->frequency, LPC17_40_TMR0_MR1); /* Set TIMER0 MR1 = number of counts */
putreg32(info->frequency, LPC17_40_TMR1_MR0); /* Set TIMER1 MR0 = number of counts */ putreg32(info->frequency, LPC17_40_TMR1_MR0); /* Set TIMER1 MR0 = number of counts */
@@ -274,7 +277,7 @@ static int timer_timer(struct lpc17_40_timer_s *priv,
putreg32(1, LPC17_40_TMR0_TCR); /* Start timer0 */ putreg32(1, LPC17_40_TMR0_TCR); /* Start timer0 */
putreg32(1, LPC17_40_TMR1_TCR); /* Start timer1 */ putreg32(1, LPC17_40_TMR1_TCR); /* Start timer1 */
leave_critical_section(flags); spin_unlock_irqrestore(&priv->lock, flags);
timer_dumpregs(priv, "After starting"); timer_dumpregs(priv, "After starting");
return OK; return OK;
} }
@@ -361,7 +364,7 @@ static int timer_setup(struct pwm_lowerhalf_s *dev)
irqstate_t flags; irqstate_t flags;
uint32_t regval; uint32_t regval;
flags = enter_critical_section(); flags = spin_lock_irqsave(&priv->lock);
/* Power on the timer peripherals */ /* Power on the timer peripherals */
@@ -423,7 +426,7 @@ static int timer_setup(struct pwm_lowerhalf_s *dev)
/* lpc17_40_configgpio(GPIO_MAT0p1_2); */ /* lpc17_40_configgpio(GPIO_MAT0p1_2); */
leave_critical_section(flags); spin_unlock_irqrestore(&priv->lock, flags);
pwm_dumpgpio(priv->pincfg, "TIMER setup"); pwm_dumpgpio(priv->pincfg, "TIMER setup");
return OK; return OK;
} }
@@ -511,7 +514,7 @@ static int timer_stop(struct pwm_lowerhalf_s *dev)
* to prevent any concurrent access to the reset register. * to prevent any concurrent access to the reset register.
*/ */
flags = enter_critical_section(); flags = spin_lock_irqsave(&priv->lock);
/* Disable further interrupts and stop the timer */ /* Disable further interrupts and stop the timer */
@@ -529,7 +532,7 @@ static int timer_stop(struct pwm_lowerhalf_s *dev)
* into a state where timer_start() can be called. * into a state where timer_start() can be called.
*/ */
leave_critical_section(flags); spin_unlock_irqrestore(&priv->lock, flags);
pwminfo("regaddr: %08x resetbit: %08x\n", regaddr, resetbit); pwminfo("regaddr: %08x resetbit: %08x\n", regaddr, resetbit);
timer_dumpregs(priv, "After stop"); timer_dumpregs(priv, "After stop");
+16 -10
View File
@@ -34,8 +34,10 @@
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <debug.h> #include <debug.h>
#include <sched.h>
#include <nuttx/irq.h> #include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <nuttx/timers/timer.h> #include <nuttx/timers/timer.h>
#include <arch/board/board.h> #include <arch/board/board.h>
@@ -78,14 +80,15 @@ struct lpc43_lowerhalf_s
/* Private data */ /* Private data */
uint32_t base; /* Base address of the timer */ uint32_t base; /* Base address of the timer */
tccb_t callback; /* Current user interrupt callback */ tccb_t callback; /* Current user interrupt callback */
void *arg; /* Argument passed to the callback function */ void *arg; /* Argument passed to the callback function */
uint32_t timeout; /* The current timeout value (us) */ uint32_t timeout; /* The current timeout value (us) */
uint32_t adjustment; /* time lost due to clock resolution truncation (us) */ uint32_t adjustment; /* time lost due to clock resolution truncation (us) */
uint32_t clkticks; /* actual clock ticks for current interval */ uint32_t clkticks; /* actual clock ticks for current interval */
bool started; /* The timer has been started */ bool started; /* The timer has been started */
uint16_t tmrid; /* Timer id */ uint16_t tmrid; /* Timer id */
spinlock_t lock; /* Spinlock */
}; };
/**************************************************************************** /****************************************************************************
@@ -632,7 +635,8 @@ static void lpc43_setcallback(struct timer_lowerhalf_s *lower,
struct lpc43_lowerhalf_s *priv = (struct lpc43_lowerhalf_s *)lower; struct lpc43_lowerhalf_s *priv = (struct lpc43_lowerhalf_s *)lower;
irqstate_t flags; irqstate_t flags;
flags = enter_critical_section(); flags = spin_lock_irqsave(&priv->lock);
sched_lock();
DEBUGASSERT(priv); DEBUGASSERT(priv);
tmrinfo("Entry: callback=%p\n", callback); tmrinfo("Entry: callback=%p\n", callback);
@@ -642,7 +646,8 @@ static void lpc43_setcallback(struct timer_lowerhalf_s *lower,
priv->callback = callback; priv->callback = callback;
priv->arg = arg; priv->arg = arg;
leave_critical_section(flags); spin_unlock_irqrestore(&priv->lock, flags);
sched_unlock();
} }
/**************************************************************************** /****************************************************************************
@@ -749,6 +754,7 @@ void lpc43_tmrinitialize(const char *devpath, int irq)
} }
priv->ops = &g_tmrops; priv->ops = &g_tmrops;
spin_lock_init(&priv->lock);
irq_attach(irq, lpc43_interrupt, NULL); irq_attach(irq, lpc43_interrupt, NULL);