diff --git a/arch/arm/src/armv7-a/arm_timer.c b/arch/arm/src/armv7-a/arm_timer.c index 81da492a9b6c6..9d99152e3e1bc 100644 --- a/arch/arm/src/armv7-a/arm_timer.c +++ b/arch/arm/src/armv7-a/arm_timer.c @@ -63,8 +63,6 @@ struct arm_timer_lowerhalf_s { struct oneshot_lowerhalf_s lh; /* Lower half operations */ uint32_t freq; /* Timer working clock frequency(Hz) */ - oneshot_callback_t callback; /* Current user interrupt callback */ - void *arg; /* Argument passed to upper half callback */ /* which cpu timer is running, -1 indicate timer stoppd */ @@ -78,7 +76,6 @@ struct arm_timer_lowerhalf_s static int arm_timer_maxdelay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int arm_timer_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int arm_timer_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -170,7 +167,6 @@ static int arm_timer_maxdelay(struct oneshot_lowerhalf_s *lower_, } static int arm_timer_start(struct oneshot_lowerhalf_s *lower_, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct arm_timer_lowerhalf_s *lower = @@ -180,9 +176,6 @@ static int arm_timer_start(struct oneshot_lowerhalf_s *lower_, flags = up_irq_save(); - lower->callback = callback; - lower->arg = arg; - lower->running = this_cpu(); count = sec_to_count(ts->tv_sec, lower->freq) + @@ -206,8 +199,6 @@ static int arm_timer_cancel(struct oneshot_lowerhalf_s *lower_, flags = up_irq_save(); - lower->callback = NULL; - lower->arg = NULL; lower->running = -1; arm_timer_phy_set_irq_mask(true); @@ -234,23 +225,14 @@ static int arm_timer_current(struct oneshot_lowerhalf_s *lower_, static int arm_timer_interrupt(int irq, void *context, void *arg) { struct arm_timer_lowerhalf_s *lower = arg; - oneshot_callback_t callback; - void *cbarg; DEBUGASSERT(lower != NULL); arm_timer_phy_set_irq_mask(true); - if (lower->callback != NULL && lower->running == this_cpu()) + if (lower->running == this_cpu()) { - callback = lower->callback; - cbarg = lower->arg; - lower->callback = NULL; - lower->arg = NULL; - - /* Then perform the callback */ - - callback(&lower->lh, cbarg); + oneshot_process_callback(&lower->lh); } return 0; diff --git a/arch/arm/src/armv7-r/arm_timer.c b/arch/arm/src/armv7-r/arm_timer.c index c97f3b7ec7bad..433c6a21fac98 100644 --- a/arch/arm/src/armv7-r/arm_timer.c +++ b/arch/arm/src/armv7-r/arm_timer.c @@ -62,8 +62,6 @@ struct arm_timer_lowerhalf_s { struct oneshot_lowerhalf_s lh; /* Lower half operations */ uint32_t freq; /* Timer working clock frequency(Hz) */ - oneshot_callback_t callback; /* Current user interrupt callback */ - void *arg; /* Argument passed to upper half callback */ /* which cpu timer is running, -1 indicate timer stoppd */ @@ -77,7 +75,6 @@ struct arm_timer_lowerhalf_s static int arm_timer_maxdelay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int arm_timer_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int arm_timer_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -163,7 +160,6 @@ static int arm_timer_maxdelay(struct oneshot_lowerhalf_s *lower_, } static int arm_timer_start(struct oneshot_lowerhalf_s *lower_, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct arm_timer_lowerhalf_s *lower = @@ -173,9 +169,6 @@ static int arm_timer_start(struct oneshot_lowerhalf_s *lower_, flags = up_irq_save(); - lower->callback = callback; - lower->arg = arg; - lower->running = this_cpu(); count = sec_to_count(ts->tv_sec, arm_timer_get_freq()) + @@ -199,8 +192,6 @@ static int arm_timer_cancel(struct oneshot_lowerhalf_s *lower_, flags = up_irq_save(); - lower->callback = NULL; - lower->arg = NULL; lower->running = -1; arm_timer_phy_set_irq_mask(true); @@ -225,23 +216,13 @@ static int arm_timer_current(struct oneshot_lowerhalf_s *lower_, static int arm_timer_interrupt(int irq, void *context, void *arg) { struct arm_timer_lowerhalf_s *lower = arg; - oneshot_callback_t callback; - void *cbarg; - DEBUGASSERT(lower != NULL); arm_timer_phy_set_irq_mask(true); - if (lower->callback != NULL && lower->running == this_cpu()) + if (lower->running == this_cpu()) { - callback = lower->callback; - cbarg = lower->arg; - lower->callback = NULL; - lower->arg = NULL; - - /* Then perform the callback */ - - callback(&lower->lh, cbarg); + oneshot_process_callback(&lower->lh); } return 0; diff --git a/arch/arm/src/armv8-r/arm_timer.c b/arch/arm/src/armv8-r/arm_timer.c index 3fa0638596b76..79f4d752f55bd 100644 --- a/arch/arm/src/armv8-r/arm_timer.c +++ b/arch/arm/src/armv8-r/arm_timer.c @@ -60,7 +60,7 @@ # define GIC_IRQ_PHY_TIMER GIC_IRQ_NONSEC_PHY_TIMER #endif -#define ARM_ARCH_TIMER_IRQ GIC_IRQ_VIRT_TIMER +#define ARM_ARCH_TIMER_IRQ GIC_IRQ_PHY_TIMER #define ARM_ARCH_TIMER_PRIO IRQ_DEFAULT_PRIORITY #define ARM_ARCH_TIMER_FLAGS IRQ_TYPE_LEVEL @@ -80,9 +80,7 @@ struct arm_oneshot_lowerhalf_s /* Private lower half data follows */ - void *arg; /* Argument that is passed to the handler */ - uint64_t cycle_per_tick; /* cycle per tick */ - oneshot_callback_t callback; /* Internal handler that receives callback */ + uint32_t frequency; /* Frequency */ }; /**************************************************************************** @@ -146,18 +144,15 @@ static int arm_arch_timer_compare_isr(int irq, void *regs, void *arg) arm_timer_phy_set_irq_mask(true); - if (priv->callback) - { - /* Then perform the callback */ + /* Then perform the callback */ - priv->callback(&priv->lh, priv->arg); - } + oneshot_process_callback(&priv->lh); return OK; } /**************************************************************************** - * Name: arm_tick_max_delay + * Name: arm_max_delay * * Description: * Determine the maximum delay of the one-shot timer (in microseconds) @@ -166,7 +161,7 @@ static int arm_arch_timer_compare_isr(int irq, void *regs, void *arg) * lower An instance of the lower-half oneshot state structure. This * structure must have been previously initialized via a call to * oneshot_initialize(); - * ticks The location in which to return the maximum delay. + * ts The location in which to return the maximum delay. * * Returned Value: * Zero (OK) is returned on success; a negated errno value is returned @@ -174,18 +169,23 @@ static int arm_arch_timer_compare_isr(int irq, void *regs, void *arg) * ****************************************************************************/ -static int arm_tick_max_delay(struct oneshot_lowerhalf_s *lower, - clock_t *ticks) +static int arm_max_delay(struct oneshot_lowerhalf_s *lower, + struct timespec *ts) { - DEBUGASSERT(ticks != NULL); + struct arm_oneshot_lowerhalf_s *priv = + (struct arm_oneshot_lowerhalf_s *)lower; + uint32_t freq = priv->frequency; + + DEBUGASSERT(ts != NULL); - *ticks = (clock_t)UINT64_MAX; + ts->tv_sec = UINT64_MAX / freq; + ts->tv_nsec = UINT64_MAX % freq * NSEC_PER_SEC / freq; return OK; } /**************************************************************************** - * Name: arm_tick_cancel + * Name: arm_cancel * * Description: * Cancel the oneshot timer and return the time remaining on the timer. @@ -197,7 +197,7 @@ static int arm_tick_max_delay(struct oneshot_lowerhalf_s *lower, * lower Caller allocated instance of the oneshot state structure. This * structure must have been previously initialized via a call to * oneshot_initialize(); - * ticks The location in which to return the time remaining on the + * ts The location in which to return the time remaining on the * oneshot timer. * * Returned Value: @@ -207,13 +207,13 @@ static int arm_tick_max_delay(struct oneshot_lowerhalf_s *lower, * ****************************************************************************/ -static int arm_tick_cancel(struct oneshot_lowerhalf_s *lower, - clock_t *ticks) +static int arm_cancel(struct oneshot_lowerhalf_s *lower, + struct timespec *ts) { struct arm_oneshot_lowerhalf_s *priv = (struct arm_oneshot_lowerhalf_s *)lower; - DEBUGASSERT(priv != NULL && ticks != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); /* Disable int */ @@ -223,7 +223,7 @@ static int arm_tick_cancel(struct oneshot_lowerhalf_s *lower, } /**************************************************************************** - * Name: arm_tick_start + * Name: arm_start * * Description: * Start the oneshot timer @@ -234,7 +234,7 @@ static int arm_tick_cancel(struct oneshot_lowerhalf_s *lower, * oneshot_initialize(); * handler The function to call when when the oneshot timer expires. * arg An opaque argument that will accompany the callback. - * ticks Provides the duration of the one shot timer. + * ts Provides the duration of the one shot timer. * * Returned Value: * Zero (OK) is returned on success; a negated errno value is returned @@ -242,24 +242,23 @@ static int arm_tick_cancel(struct oneshot_lowerhalf_s *lower, * ****************************************************************************/ -static int arm_tick_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, - clock_t ticks) +static int arm_start(struct oneshot_lowerhalf_s *lower, + const struct timespec *ts) { + uint64_t count; struct arm_oneshot_lowerhalf_s *priv = (struct arm_oneshot_lowerhalf_s *)lower; + uint64_t freq = priv->frequency; - DEBUGASSERT(priv != NULL && callback != NULL); - - /* Save the new handler and its argument */ - - priv->callback = callback; - priv->arg = arg; + DEBUGASSERT(priv && ts); /* Set the timeout */ - arm_timer_phy_set_absolute(arm_timer_phy_count() + - priv->cycle_per_tick * ticks); + count = arm_timer_phy_count(); + count += (uint64_t)ts->tv_sec * freq + + (uint64_t)ts->tv_nsec * freq / NSEC_PER_SEC; + + arm_timer_phy_set_absolute(count); /* Try to unmask the timer irq in timer controller * in case of arm_tick_cancel is called. @@ -271,7 +270,7 @@ static int arm_tick_start(struct oneshot_lowerhalf_s *lower, } /**************************************************************************** - * Name: arm_tick_current + * Name: arm_current * * Description: * Get the current time. @@ -280,7 +279,7 @@ static int arm_tick_start(struct oneshot_lowerhalf_s *lower, * lower Caller allocated instance of the oneshot state structure. This * structure must have been previously initialized via a call to * oneshot_initialize(); - * ticks The location in which to return the current time. + * ts The location in which to return the current time. * * Returned Value: * Zero (OK) is returned on success, a negated errno value is returned on @@ -288,15 +287,21 @@ static int arm_tick_start(struct oneshot_lowerhalf_s *lower, * ****************************************************************************/ -static int arm_tick_current(struct oneshot_lowerhalf_s *lower, - clock_t *ticks) +static int arm_current(struct oneshot_lowerhalf_s *lower, + struct timespec *ts) { + uint64_t count; + uint32_t freq; struct arm_oneshot_lowerhalf_s *priv = (struct arm_oneshot_lowerhalf_s *)lower; - DEBUGASSERT(ticks != NULL); + DEBUGASSERT(ts != NULL); - *ticks = arm_timer_phy_count() / priv->cycle_per_tick; + freq = priv->frequency; + count = arm_timer_phy_count(); + + ts->tv_sec = count / freq; + ts->tv_nsec = (count % freq) * NSEC_PER_SEC / freq; return OK; } @@ -307,10 +312,10 @@ static int arm_tick_current(struct oneshot_lowerhalf_s *lower, static const struct oneshot_operations_s g_oneshot_ops = { - .tick_start = arm_tick_start, - .tick_current = arm_tick_current, - .tick_max_delay = arm_tick_max_delay, - .tick_cancel = arm_tick_cancel, + .start = arm_start, + .current = arm_current, + .max_delay = arm_max_delay, + .cancel = arm_cancel, }; /**************************************************************************** @@ -346,9 +351,10 @@ static struct oneshot_lowerhalf_s *arm_oneshot_initialize(void) /* Initialize the lower-half driver structure */ + DEBUGASSERT(arm_timer_get_freq() <= UINT32_MAX); + priv->lh.ops = &g_oneshot_ops; - priv->cycle_per_tick = arm_timer_get_freq() / TICK_PER_SEC; - tmrinfo("cycle_per_tick %" PRIu64 "\n", priv->cycle_per_tick); + priv->frequency = arm_timer_get_freq(); /* Attach handler */ @@ -411,7 +417,7 @@ void up_timer_initialize(void) * timer interrupt ****************************************************************************/ -void arm_timer_secondary_init(void) +void arm_timer_secondary_init(unsigned int freq) { #ifdef CONFIG_SCHED_TICKLESS tmrinfo("arm_arch_timer_secondary_init\n"); diff --git a/arch/arm/src/sam34/sam4cm_oneshot_lowerhalf.c b/arch/arm/src/sam34/sam4cm_oneshot_lowerhalf.c index 7b63804f9ce5a..78427367cd0b5 100644 --- a/arch/arm/src/sam34/sam4cm_oneshot_lowerhalf.c +++ b/arch/arm/src/sam34/sam4cm_oneshot_lowerhalf.c @@ -58,8 +58,6 @@ struct sam_oneshot_lowerhalf_s /* Private lower half data follows */ struct sam_oneshot_s oneshot; /* SAM-specific oneshot state */ - oneshot_callback_t callback; /* internal handler that receives callback */ - void *arg; /* Argument that is passed to the handler */ }; /**************************************************************************** @@ -71,7 +69,6 @@ static void sam_oneshot_handler(void *arg); static int sam_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int sam_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int sam_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -112,8 +109,6 @@ static void sam_oneshot_handler(void *arg) { struct sam_oneshot_lowerhalf_s *priv = (struct sam_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *cbarg; DEBUGASSERT(priv != NULL); @@ -121,21 +116,7 @@ static void sam_oneshot_handler(void *arg) * sam_cancel? */ - if (priv->callback) - { - /* Sample and nullify BEFORE executing callback (in case the callback - * restarts the oneshot). - */ - - callback = priv->callback; - cbarg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cbarg); - } + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -199,7 +180,6 @@ static int sam_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int sam_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct sam_oneshot_lowerhalf_s *priv = @@ -207,15 +187,13 @@ static int sam_start(struct oneshot_lowerhalf_s *lower, irqstate_t flags; int ret; - DEBUGASSERT(priv != NULL && callback != NULL && ts != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); /* Save the callback information and start the timer */ - flags = enter_critical_section(); - priv->callback = callback; - priv->arg = arg; - ret = sam_oneshot_start(&priv->oneshot, NULL, - sam_oneshot_handler, priv, ts); + flags = enter_critical_section(); + ret = sam_oneshot_start(&priv->oneshot, NULL, + sam_oneshot_handler, priv, ts); leave_critical_section(flags); if (ret < 0) @@ -262,10 +240,8 @@ static int sam_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = enter_critical_section(); - ret = sam_oneshot_cancel(&priv->oneshot, NULL, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = enter_critical_section(); + ret = sam_oneshot_cancel(&priv->oneshot, NULL, ts); leave_critical_section(flags); if (ret < 0) diff --git a/arch/arm/src/sama5/sam_oneshot_lowerhalf.c b/arch/arm/src/sama5/sam_oneshot_lowerhalf.c index 6ce17a5b05403..e937eb7d48f3c 100644 --- a/arch/arm/src/sama5/sam_oneshot_lowerhalf.c +++ b/arch/arm/src/sama5/sam_oneshot_lowerhalf.c @@ -60,8 +60,6 @@ struct sam_oneshot_lowerhalf_s /* Private lower half data follows */ struct sam_oneshot_s oneshot; /* SAM-specific oneshot state */ - oneshot_callback_t callback; /* internal handler that receives callback */ - void *arg; /* Argument that is passed to the handler */ }; /**************************************************************************** @@ -73,7 +71,6 @@ static void sam_oneshot_handler(void *arg); static int sam_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int sam_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int sam_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -114,8 +111,6 @@ static void sam_oneshot_handler(void *arg) { struct sam_oneshot_lowerhalf_s *priv = (struct sam_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *cbarg; DEBUGASSERT(priv != NULL); @@ -123,21 +118,7 @@ static void sam_oneshot_handler(void *arg) * sam_cancel? */ - if (priv->callback) - { - /* Sample and nullify BEFORE executing callback (in case the callback - * restarts the oneshot). - */ - - callback = priv->callback; - cbarg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cbarg); - } + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -201,7 +182,6 @@ static int sam_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int sam_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct sam_oneshot_lowerhalf_s *priv = @@ -209,15 +189,13 @@ static int sam_start(struct oneshot_lowerhalf_s *lower, irqstate_t flags; int ret; - DEBUGASSERT(priv != NULL && callback != NULL && ts != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); /* Save the callback information and start the timer */ - flags = enter_critical_section(); - priv->callback = callback; - priv->arg = arg; - ret = sam_oneshot_start(&priv->oneshot, NULL, - sam_oneshot_handler, priv, ts); + flags = enter_critical_section(); + ret = sam_oneshot_start(&priv->oneshot, NULL, + sam_oneshot_handler, priv, ts); leave_critical_section(flags); if (ret < 0) @@ -264,10 +242,8 @@ static int sam_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = enter_critical_section(); - ret = sam_oneshot_cancel(&priv->oneshot, NULL, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = enter_critical_section(); + ret = sam_oneshot_cancel(&priv->oneshot, NULL, ts); leave_critical_section(flags); if (ret < 0) diff --git a/arch/arm/src/samd5e5/sam_oneshot_lowerhalf.c b/arch/arm/src/samd5e5/sam_oneshot_lowerhalf.c index c41675769b1fd..8df89f6b5d980 100644 --- a/arch/arm/src/samd5e5/sam_oneshot_lowerhalf.c +++ b/arch/arm/src/samd5e5/sam_oneshot_lowerhalf.c @@ -58,8 +58,6 @@ struct sam_oneshot_lowerhalf_s /* Private lower half data follows */ struct sam_oneshot_s oneshot; /* SAM-specific oneshot state */ - oneshot_callback_t callback; /* internal handler that receives callback */ - void *arg; /* Argument that is passed to the handler */ }; /**************************************************************************** @@ -71,7 +69,6 @@ static void sam_oneshot_handler(void *arg); static int sam_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int sam_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int sam_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -112,8 +109,6 @@ static void sam_oneshot_handler(void *arg) { struct sam_oneshot_lowerhalf_s *priv = (struct sam_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *cbarg; DEBUGASSERT(priv != NULL); @@ -121,21 +116,7 @@ static void sam_oneshot_handler(void *arg) * sam_cancel? */ - if (priv->callback) - { - /* Sample and nullify BEFORE executing callback (in case the callback - * restarts the oneshot). - */ - - callback = priv->callback; - cbarg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cbarg); - } + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -199,7 +180,6 @@ static int sam_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int sam_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct sam_oneshot_lowerhalf_s *priv = @@ -207,15 +187,13 @@ static int sam_start(struct oneshot_lowerhalf_s *lower, irqstate_t flags; int ret; - DEBUGASSERT(priv != NULL && callback != NULL && ts != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); /* Save the callback information and start the timer */ - flags = enter_critical_section(); - priv->callback = callback; - priv->arg = arg; - ret = sam_oneshot_start(&priv->oneshot, NULL, - sam_oneshot_handler, priv, ts); + flags = enter_critical_section(); + ret = sam_oneshot_start(&priv->oneshot, NULL, + sam_oneshot_handler, priv, ts); leave_critical_section(flags); if (ret < 0) @@ -262,10 +240,8 @@ static int sam_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = enter_critical_section(); - ret = sam_oneshot_cancel(&priv->oneshot, NULL, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = enter_critical_section(); + ret = sam_oneshot_cancel(&priv->oneshot, NULL, ts); leave_critical_section(flags); if (ret < 0) diff --git a/arch/arm/src/samv7/sam_oneshot_lowerhalf.c b/arch/arm/src/samv7/sam_oneshot_lowerhalf.c index dc736a3ee5404..60362290bedf3 100644 --- a/arch/arm/src/samv7/sam_oneshot_lowerhalf.c +++ b/arch/arm/src/samv7/sam_oneshot_lowerhalf.c @@ -58,8 +58,6 @@ struct sam_oneshot_lowerhalf_s /* Private lower half data follows */ struct sam_oneshot_s oneshot; /* SAMV7-specific oneshot state */ - oneshot_callback_t callback; /* internal handler that receives callback */ - void *arg; /* Argument that is passed to the handler */ }; /**************************************************************************** @@ -71,7 +69,6 @@ static void sam_oneshot_handler(void *arg); static int sam_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int sam_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int sam_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -112,8 +109,6 @@ static void sam_oneshot_handler(void *arg) { struct sam_oneshot_lowerhalf_s *priv = (struct sam_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *cbarg; DEBUGASSERT(priv != NULL); @@ -121,21 +116,7 @@ static void sam_oneshot_handler(void *arg) * sam_cancel? */ - if (priv->callback) - { - /* Sample and nullify BEFORE executing callback (in case the callback - * restarts the oneshot). - */ - - callback = priv->callback; - cbarg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cbarg); - } + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -199,7 +180,6 @@ static int sam_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int sam_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct sam_oneshot_lowerhalf_s *priv = @@ -207,15 +187,13 @@ static int sam_start(struct oneshot_lowerhalf_s *lower, irqstate_t flags; int ret; - DEBUGASSERT(priv != NULL && callback != NULL && ts != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); /* Save the callback information and start the timer */ - flags = enter_critical_section(); - priv->callback = callback; - priv->arg = arg; - ret = sam_oneshot_start(&priv->oneshot, NULL, - sam_oneshot_handler, priv, ts); + flags = enter_critical_section(); + ret = sam_oneshot_start(&priv->oneshot, NULL, + sam_oneshot_handler, priv, ts); leave_critical_section(flags); if (ret < 0) @@ -262,10 +240,8 @@ static int sam_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = enter_critical_section(); - ret = sam_oneshot_cancel(&priv->oneshot, NULL, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = enter_critical_section(); + ret = sam_oneshot_cancel(&priv->oneshot, NULL, ts); leave_critical_section(flags); if (ret < 0) diff --git a/arch/arm/src/stm32/stm32_oneshot_lowerhalf.c b/arch/arm/src/stm32/stm32_oneshot_lowerhalf.c index f0b6d2ccf9518..a6afcd2a94414 100644 --- a/arch/arm/src/stm32/stm32_oneshot_lowerhalf.c +++ b/arch/arm/src/stm32/stm32_oneshot_lowerhalf.c @@ -57,8 +57,6 @@ struct stm32_oneshot_lowerhalf_s /* Private lower half data follows */ struct stm32_oneshot_s oneshot; /* STM32-specific oneshot state */ - oneshot_callback_t callback; /* Internal handler that receives callback */ - void *arg; /* Argument that is passed to the handler */ }; /**************************************************************************** @@ -70,7 +68,6 @@ static void stm32_oneshot_handler(void *arg); static int stm32_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int stm32_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int stm32_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -111,8 +108,6 @@ static void stm32_oneshot_handler(void *arg) { struct stm32_oneshot_lowerhalf_s *priv = (struct stm32_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *cbarg; DEBUGASSERT(priv != NULL); @@ -120,21 +115,7 @@ static void stm32_oneshot_handler(void *arg) * stm32_cancel? */ - if (priv->callback) - { - /* Sample and nullify BEFORE executing callback (in case the callback - * restarts the oneshot). - */ - - callback = priv->callback; - cbarg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cbarg); - } + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -198,7 +179,6 @@ static int stm32_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int stm32_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct stm32_oneshot_lowerhalf_s *priv = @@ -206,15 +186,13 @@ static int stm32_start(struct oneshot_lowerhalf_s *lower, irqstate_t flags; int ret; - DEBUGASSERT(priv != NULL && callback != NULL && ts != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); /* Save the callback information and start the timer */ - flags = enter_critical_section(); - priv->callback = callback; - priv->arg = arg; - ret = stm32_oneshot_start(&priv->oneshot, - stm32_oneshot_handler, priv, ts); + flags = enter_critical_section(); + ret = stm32_oneshot_start(&priv->oneshot, + stm32_oneshot_handler, priv, ts); leave_critical_section(flags); if (ret < 0) @@ -261,10 +239,8 @@ static int stm32_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = enter_critical_section(); - ret = stm32_oneshot_cancel(&priv->oneshot, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = enter_critical_section(); + ret = stm32_oneshot_cancel(&priv->oneshot, ts); leave_critical_section(flags); if (ret < 0) diff --git a/arch/arm/src/stm32h7/stm32_oneshot_lowerhalf.c b/arch/arm/src/stm32h7/stm32_oneshot_lowerhalf.c index 545f4ebdcf355..da8112d52ba6e 100644 --- a/arch/arm/src/stm32h7/stm32_oneshot_lowerhalf.c +++ b/arch/arm/src/stm32h7/stm32_oneshot_lowerhalf.c @@ -57,8 +57,6 @@ struct stm32_oneshot_lowerhalf_s /* Private lower half data follows */ struct stm32_oneshot_s oneshot; /* STM32-specific oneshot state */ - oneshot_callback_t callback; /* Internal handler that receives callback */ - void *arg; /* Argument that is passed to the handler */ }; /**************************************************************************** @@ -70,7 +68,6 @@ static void stm32_oneshot_handler(void *arg); static int stm32_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int stm32_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int stm32_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -111,8 +108,6 @@ static void stm32_oneshot_handler(void *arg) { struct stm32_oneshot_lowerhalf_s *priv = (struct stm32_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *cbarg; DEBUGASSERT(priv != NULL); @@ -120,21 +115,7 @@ static void stm32_oneshot_handler(void *arg) * stm32_cancel? */ - if (priv->callback) - { - /* Sample and nullify BEFORE executing callback (in case the callback - * restarts the oneshot). - */ - - callback = priv->callback; - cbarg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cbarg); - } + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -198,7 +179,6 @@ static int stm32_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int stm32_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct stm32_oneshot_lowerhalf_s *priv = @@ -206,15 +186,13 @@ static int stm32_start(struct oneshot_lowerhalf_s *lower, irqstate_t flags; int ret; - DEBUGASSERT(priv != NULL && callback != NULL && ts != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); /* Save the callback information and start the timer */ - flags = enter_critical_section(); - priv->callback = callback; - priv->arg = arg; - ret = stm32_oneshot_start(&priv->oneshot, - stm32_oneshot_handler, priv, ts); + flags = enter_critical_section(); + ret = stm32_oneshot_start(&priv->oneshot, + stm32_oneshot_handler, priv, ts); leave_critical_section(flags); if (ret < 0) @@ -261,10 +239,8 @@ static int stm32_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = enter_critical_section(); - ret = stm32_oneshot_cancel(&priv->oneshot, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = enter_critical_section(); + ret = stm32_oneshot_cancel(&priv->oneshot, ts); leave_critical_section(flags); if (ret < 0) diff --git a/arch/arm/src/stm32l4/stm32l4_oneshot_lowerhalf.c b/arch/arm/src/stm32l4/stm32l4_oneshot_lowerhalf.c index 61e75d04ffa77..63d31d39f10a4 100644 --- a/arch/arm/src/stm32l4/stm32l4_oneshot_lowerhalf.c +++ b/arch/arm/src/stm32l4/stm32l4_oneshot_lowerhalf.c @@ -58,8 +58,6 @@ struct stm32l4_oneshot_lowerhalf_s /* Private lower half data follows */ struct stm32l4_oneshot_s oneshot; /* STM32-specific oneshot state */ - oneshot_callback_t callback; /* internal handler that receives callback */ - void *arg; /* Argument that is passed to the handler */ }; /**************************************************************************** @@ -71,7 +69,6 @@ static void stm32l4_oneshot_handler(void *arg); static int stm32l4_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int stm32l4_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int stm32l4_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -112,8 +109,6 @@ static void stm32l4_oneshot_handler(void *arg) { struct stm32l4_oneshot_lowerhalf_s *priv = (struct stm32l4_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *cbarg; DEBUGASSERT(priv != NULL); @@ -121,21 +116,7 @@ static void stm32l4_oneshot_handler(void *arg) * stm32l4_cancel? */ - if (priv->callback) - { - /* Sample and nullify BEFORE executing callback (in case the callback - * restarts the oneshot). - */ - - callback = priv->callback; - cbarg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cbarg); - } + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -199,7 +180,6 @@ static int stm32l4_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int stm32l4_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct stm32l4_oneshot_lowerhalf_s *priv = @@ -207,15 +187,13 @@ static int stm32l4_start(struct oneshot_lowerhalf_s *lower, irqstate_t flags; int ret; - DEBUGASSERT(priv != NULL && callback != NULL && ts != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); /* Save the callback information and start the timer */ - flags = enter_critical_section(); - priv->callback = callback; - priv->arg = arg; - ret = stm32l4_oneshot_start(&priv->oneshot, - stm32l4_oneshot_handler, priv, ts); + flags = enter_critical_section(); + ret = stm32l4_oneshot_start(&priv->oneshot, + stm32l4_oneshot_handler, priv, ts); leave_critical_section(flags); if (ret < 0) @@ -262,10 +240,8 @@ static int stm32l4_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = enter_critical_section(); - ret = stm32l4_oneshot_cancel(&priv->oneshot, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = enter_critical_section(); + ret = stm32l4_oneshot_cancel(&priv->oneshot, ts); leave_critical_section(flags); if (ret < 0) diff --git a/arch/arm/src/stm32wb/stm32wb_oneshot_lowerhalf.c b/arch/arm/src/stm32wb/stm32wb_oneshot_lowerhalf.c index 2df58ee753dbd..b1d3bebdfcdd3 100644 --- a/arch/arm/src/stm32wb/stm32wb_oneshot_lowerhalf.c +++ b/arch/arm/src/stm32wb/stm32wb_oneshot_lowerhalf.c @@ -58,8 +58,6 @@ struct stm32wb_oneshot_lowerhalf_s /* Private lower half data follows */ struct stm32wb_oneshot_s oneshot; /* STM32-specific oneshot state */ - oneshot_callback_t callback; /* internal handler that receives callback */ - void *arg; /* Argument that is passed to the handler */ }; /**************************************************************************** @@ -71,7 +69,6 @@ static void stm32wb_oneshot_handler(void *arg); static int stm32wb_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int stm32wb_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int stm32wb_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -112,8 +109,6 @@ static void stm32wb_oneshot_handler(void *arg) { struct stm32wb_oneshot_lowerhalf_s *priv = (struct stm32wb_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *cbarg; DEBUGASSERT(priv != NULL); @@ -121,21 +116,7 @@ static void stm32wb_oneshot_handler(void *arg) * stm32wb_cancel? */ - if (priv->callback) - { - /* Sample and nullify BEFORE executing callback (in case the callback - * restarts the oneshot). - */ - - callback = priv->callback; - cbarg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cbarg); - } + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -199,23 +180,20 @@ static int stm32wb_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int stm32wb_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, - const struct timespec *ts) + const struct timespec *ts) { struct stm32wb_oneshot_lowerhalf_s *priv = (struct stm32wb_oneshot_lowerhalf_s *)lower; irqstate_t flags; int ret; - DEBUGASSERT(priv != NULL && callback != NULL && ts != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); /* Save the callback information and start the timer */ - flags = enter_critical_section(); - priv->callback = callback; - priv->arg = arg; - ret = stm32wb_oneshot_start(&priv->oneshot, - stm32wb_oneshot_handler, priv, ts); + flags = enter_critical_section(); + ret = stm32wb_oneshot_start(&priv->oneshot, + stm32wb_oneshot_handler, priv, ts); leave_critical_section(flags); if (ret < 0) @@ -262,10 +240,8 @@ static int stm32wb_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = enter_critical_section(); - ret = stm32wb_oneshot_cancel(&priv->oneshot, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = enter_critical_section(); + ret = stm32wb_oneshot_cancel(&priv->oneshot, ts); leave_critical_section(flags); if (ret < 0) diff --git a/arch/arm64/src/common/arm64_arch_timer.c b/arch/arm64/src/common/arm64_arch_timer.c index 3aac9b1615ff1..5c1bd0d89a460 100644 --- a/arch/arm64/src/common/arm64_arch_timer.c +++ b/arch/arm64/src/common/arm64_arch_timer.c @@ -69,9 +69,7 @@ struct arm64_oneshot_lowerhalf_s /* Private lower half data follows */ - void *arg; /* Argument that is passed to the handler */ uint64_t frequency; /* Frequency in cycle per second */ - oneshot_callback_t callback; /* Internal handler that receives callback */ /* which cpu timer is running, -1 indicate timer stoppd */ @@ -89,38 +87,14 @@ static inline void arm64_arch_timer_set_compare(uint64_t value) static inline void arm64_arch_timer_enable(bool enable) { - uint64_t value; - - value = read_sysreg(cntv_ctl_el0); - - if (enable) - { - value |= CNTV_CTL_ENABLE_BIT; - } - else - { - value &= ~CNTV_CTL_ENABLE_BIT; - } - - write_sysreg(value, cntv_ctl_el0); + modify_sysreg(enable ? CNTV_CTL_ENABLE_BIT : 0u, + CNTV_CTL_ENABLE_BIT, cntv_ctl_el0); } static inline void arm64_arch_timer_set_irq_mask(bool mask) { - uint64_t value; - - value = read_sysreg(cntv_ctl_el0); - - if (mask) - { - value |= CNTV_CTL_IMASK_BIT; - } - else - { - value &= ~CNTV_CTL_IMASK_BIT; - } - - write_sysreg(value, cntv_ctl_el0); + modify_sysreg(mask ? CNTV_CTL_IMASK_BIT : 0u, + CNTV_CTL_IMASK_BIT, cntv_ctl_el0); } static inline uint64_t arm64_arch_timer_count(void) @@ -133,45 +107,6 @@ static inline uint64_t arm64_arch_timer_get_cntfrq(void) return read_sysreg(cntfrq_el0); } -static inline uint64_t arm64_arch_cnt2tick(uint64_t count, uint64_t freq) -{ - uint64_t sec; - uint64_t ticks; - - /* In case of count * TICK_PER_SEC overflow. - * We should divide the count into two parts: - * The second part and sub-second part. - */ - - sec = count / freq; - ticks = sec * TICK_PER_SEC; - - /* Here we convert the sub-second part to ticks. */ - - count -= sec * freq; - ticks += count * TICK_PER_SEC / freq; - - return ticks; -} - -static inline uint64_t arm64_arch_tick2cnt(uint64_t ticks, uint64_t freq) -{ - uint64_t count; - uint64_t sec; - - /* First we convert the second part to count. */ - - sec = div_const(ticks, TICK_PER_SEC); - count = sec * freq; - - /* Here we convert the sub-second part to count. */ - - ticks -= sec * TICK_PER_SEC; - count += div_const(ticks * freq, TICK_PER_SEC); - - return count; -} - /**************************************************************************** * Private Functions ****************************************************************************/ @@ -199,11 +134,11 @@ static int arm64_arch_timer_compare_isr(int irq, void *regs, void *arg) arm64_arch_timer_set_irq_mask(true); - if (priv->callback && priv->running == this_cpu()) + if (priv->running == this_cpu()) { /* Then perform the callback */ - priv->callback(&priv->lh, priv->arg); + oneshot_process_callback(&priv->lh); } return OK; @@ -227,12 +162,15 @@ static int arm64_arch_timer_compare_isr(int irq, void *regs, void *arg) * ****************************************************************************/ -static int arm64_tick_max_delay(struct oneshot_lowerhalf_s *lower, - clock_t *ticks) +static int arm64_max_delay(struct oneshot_lowerhalf_s *lower, + struct timespec *ts) { - DEBUGASSERT(ticks != NULL); + uint64_t freq = arm64_arch_timer_get_cntfrq(); - *ticks = (clock_t)UINT32_MAX; + DEBUGASSERT(ts != NULL); + + ts->tv_sec = UINT64_MAX / freq; + ts->tv_nsec = UINT64_MAX % freq * NSEC_PER_SEC / freq; return OK; } @@ -260,13 +198,13 @@ static int arm64_tick_max_delay(struct oneshot_lowerhalf_s *lower, * ****************************************************************************/ -static int arm64_tick_cancel(struct oneshot_lowerhalf_s *lower, - clock_t *ticks) +static int arm64_cancel(struct oneshot_lowerhalf_s *lower, + struct timespec *ts) { struct arm64_oneshot_lowerhalf_s *priv = (struct arm64_oneshot_lowerhalf_s *)lower; - DEBUGASSERT(priv != NULL && ticks != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); /* Disable int */ @@ -296,35 +234,23 @@ static int arm64_tick_cancel(struct oneshot_lowerhalf_s *lower, * ****************************************************************************/ -static int arm64_tick_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, - clock_t ticks) +static int arm64_start(struct oneshot_lowerhalf_s *lower, + const struct timespec *ts) { - uint64_t next_cnt; - uint64_t next_tick; + uint64_t count; struct arm64_oneshot_lowerhalf_s *priv = (struct arm64_oneshot_lowerhalf_s *)lower; uint64_t freq = priv->frequency; - DEBUGASSERT(priv != NULL && callback != NULL); - - /* Save the new handler and its argument */ - - priv->callback = callback; - priv->arg = arg; + DEBUGASSERT(priv && ts); priv->running = this_cpu(); - /* Align the timer count to the tick boundary. - * Notice that this is just a work-around. We should pass both - * the current system ticks and the delay ticks as input parameters. - * But we only have the delay tick here due to the oneshot interface. - */ - - next_tick = arm64_arch_cnt2tick(arm64_arch_timer_count(), freq) + ticks; - next_cnt = arm64_arch_tick2cnt(next_tick, freq); + count = arm64_arch_timer_count(); + count += (uint64_t)ts->tv_sec * freq + + (uint64_t)ts->tv_nsec * freq / NSEC_PER_SEC; - arm64_arch_timer_set_compare(next_cnt); + arm64_arch_timer_set_compare(count); arm64_arch_timer_set_irq_mask(false); @@ -345,18 +271,21 @@ static int arm64_tick_start(struct oneshot_lowerhalf_s *lower, * ****************************************************************************/ -static int arm64_tick_current(struct oneshot_lowerhalf_s *lower, - clock_t *ticks) +static int arm64_current(struct oneshot_lowerhalf_s *lower, + struct timespec *ts) { uint64_t count; + uint64_t freq; struct arm64_oneshot_lowerhalf_s *priv = (struct arm64_oneshot_lowerhalf_s *)lower; - DEBUGASSERT(ticks != NULL); + DEBUGASSERT(ts != NULL); + freq = priv->frequency; count = arm64_arch_timer_count(); - *ticks = arm64_arch_cnt2tick(count, priv->frequency); + ts->tv_sec = count / freq; + ts->tv_nsec = (count % freq) * NSEC_PER_SEC / freq; return OK; } @@ -389,10 +318,10 @@ static void arm64_oneshot_initialize_per_cpu(void) static const struct oneshot_operations_s g_oneshot_ops = { - .tick_start = arm64_tick_start, - .tick_current = arm64_tick_current, - .tick_max_delay = arm64_tick_max_delay, - .tick_cancel = arm64_tick_cancel, + .start = arm64_start, + .current = arm64_current, + .max_delay = arm64_max_delay, + .cancel = arm64_cancel, }; /**************************************************************************** diff --git a/arch/mips/src/pic32mz/pic32mz_oneshot_lowerhalf.c b/arch/mips/src/pic32mz/pic32mz_oneshot_lowerhalf.c index da88ae9ee7e29..8a7d8dd3a92a2 100644 --- a/arch/mips/src/pic32mz/pic32mz_oneshot_lowerhalf.c +++ b/arch/mips/src/pic32mz/pic32mz_oneshot_lowerhalf.c @@ -56,8 +56,6 @@ struct pic32mz_oneshot_lowerhalf_s /* Private lower half data follows */ struct pic32mz_oneshot_s oneshot; /* PIC32MZ-specific oneshot state */ - oneshot_callback_t callback; /* Handler that receives callback */ - void *arg; /* Argument passed to the handler */ }; /**************************************************************************** @@ -69,7 +67,6 @@ static void pic32mz_oneshot_handler(void *arg); static int pic32mz_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int pic32mz_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int pic32mz_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -110,8 +107,6 @@ static void pic32mz_oneshot_handler(void *arg) { struct pic32mz_oneshot_lowerhalf_s *priv = (struct pic32mz_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *cbarg; DEBUGASSERT(priv != NULL); @@ -119,21 +114,7 @@ static void pic32mz_oneshot_handler(void *arg) * pic32mz_cancel? */ - if (priv->callback) - { - /* Sample and nullify BEFORE executing callback (in case the callback - * restarts the oneshot). - */ - - callback = priv->callback; - cbarg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cbarg); - } + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -200,7 +181,6 @@ static int pic32mz_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int pic32mz_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct pic32mz_oneshot_lowerhalf_s *priv = @@ -208,16 +188,13 @@ static int pic32mz_start(struct oneshot_lowerhalf_s *lower, irqstate_t flags; int ret; - DEBUGASSERT(priv != NULL && callback != NULL && ts != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); /* Save the callback information and start the timer */ - flags = enter_critical_section(); - priv->callback = callback; - priv->arg = arg; - ret = pic32mz_oneshot_start(&priv->oneshot, - pic32mz_oneshot_handler, - priv, ts); + flags = enter_critical_section(); + ret = pic32mz_oneshot_start(&priv->oneshot, + pic32mz_oneshot_handler, priv, ts); leave_critical_section(flags); if (ret < 0) @@ -264,10 +241,8 @@ static int pic32mz_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = enter_critical_section(); - ret = pic32mz_oneshot_cancel(&priv->oneshot, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = enter_critical_section(); + ret = pic32mz_oneshot_cancel(&priv->oneshot, ts); leave_critical_section(flags); if (ret < 0) diff --git a/arch/risc-v/src/bl602/bl602_oneshot_lowerhalf.c b/arch/risc-v/src/bl602/bl602_oneshot_lowerhalf.c index 53d49c2c1d065..4e7ee8b8c2678 100644 --- a/arch/risc-v/src/bl602/bl602_oneshot_lowerhalf.c +++ b/arch/risc-v/src/bl602/bl602_oneshot_lowerhalf.c @@ -72,8 +72,6 @@ struct bl602_oneshot_lowerhalf_s /* Private lower half data follows */ - oneshot_callback_t callback; /* Internal handler that receives callback */ - void * arg; /* Argument that is passed to the handler */ uint8_t tim; /* timer tim 0,1 */ uint8_t irq; /* IRQ associated with this timer */ bool started; /* True: Timer has been started */ @@ -86,8 +84,6 @@ struct bl602_oneshot_lowerhalf_s static int bl602_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec * ts); static int bl602_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, - void * arg, const struct timespec * ts); static int bl602_cancel(struct oneshot_lowerhalf_s *lower, struct timespec * ts); @@ -129,9 +125,6 @@ static int bl602_oneshot_handler(int irq, void *context, void *arg) struct bl602_oneshot_lowerhalf_s *priv = (struct bl602_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void * cbarg; - /* Clear Interrupt Bits */ uint32_t int_id; @@ -156,13 +149,7 @@ static int bl602_oneshot_handler(int irq, void *context, void *arg) if ((int_id & TIMER_TMSR2_TMSR_0) != 0) { putreg32(ticr_val | TIMER_TICR2_TCLR_0, ticr_addr); - callback = priv->callback; - cbarg = priv->arg; - - if (callback) - { - callback(&priv->lh, cbarg); - } + oneshot_process_callback(&priv->lh); } /* Comparator 1 match interrupt */ @@ -240,8 +227,6 @@ static int bl602_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int bl602_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, - void * arg, const struct timespec * ts) { struct bl602_oneshot_lowerhalf_s *priv = @@ -249,7 +234,7 @@ static int bl602_start(struct oneshot_lowerhalf_s *lower, irqstate_t flags; uint64_t usec; - DEBUGASSERT(priv != NULL && callback != NULL && ts != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); if (priv->started == true) { @@ -261,9 +246,7 @@ static int bl602_start(struct oneshot_lowerhalf_s *lower, /* Save the callback information and start the timer */ - flags = enter_critical_section(); - priv->callback = callback; - priv->arg = arg; + flags = enter_critical_section(); /* Express the delay in microseconds */ @@ -328,8 +311,6 @@ static int bl602_cancel(struct oneshot_lowerhalf_s *lower, priv->started = false; up_disable_irq(priv->irq); bl602_timer_intmask(priv->tim, TIMER_INT_COMP_0, 1); - priv->callback = NULL; - priv->arg = NULL; leave_critical_section(flags); return OK; diff --git a/arch/risc-v/src/common/espressif/esp_oneshot.c b/arch/risc-v/src/common/espressif/esp_oneshot.c index 909ca0ee67807..75809226b32b4 100644 --- a/arch/risc-v/src/common/espressif/esp_oneshot.c +++ b/arch/risc-v/src/common/espressif/esp_oneshot.c @@ -80,8 +80,6 @@ struct esp_oneshot_lowerhalf_s { struct oneshot_lowerhalf_s lh; /* Lower half instance */ timer_hal_context_t hal; /* HAL context */ - oneshot_callback_t callback; /* Current user interrupt callback */ - void *arg; /* Argument passed to upper half callback */ uint16_t resolution; /* Timer resolution in microseconds */ bool running; /* True: the timer is running */ }; @@ -99,8 +97,6 @@ static int esp_oneshot_isr(int irq, void *context, void *arg); static int esp_oneshot_maxdelay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int esp_oneshot_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, - void *arg, const struct timespec *ts); static int esp_oneshot_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -129,8 +125,6 @@ static struct esp_oneshot_lowerhalf_s g_oneshot_lowerhalf = { .ops = &g_oneshot_ops, }, - .callback = NULL, - .arg = NULL }; /**************************************************************************** @@ -197,8 +191,6 @@ static int esp_oneshot_maxdelay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int esp_oneshot_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, - void *arg, const struct timespec *ts) { struct esp_oneshot_lowerhalf_s *priv = @@ -206,11 +198,9 @@ static int esp_oneshot_start(struct oneshot_lowerhalf_s *lower, uint64_t timeout_us; DEBUGASSERT(priv != NULL); - DEBUGASSERT(callback != NULL); DEBUGASSERT(ts != NULL); - tmrinfo("callback=%p arg=%p, ts=(%lu, %ld)\n", - callback, arg, (unsigned long)ts->tv_sec, ts->tv_nsec); + tmrinfo("ts=(%lu, %ld)\n", (unsigned long)ts->tv_sec, ts->tv_nsec); if (priv->running) { @@ -223,11 +213,6 @@ static int esp_oneshot_start(struct oneshot_lowerhalf_s *lower, esp_oneshot_cancel(lower, NULL); } - /* Save the new callback and its argument */ - - priv->callback = callback; - priv->arg = arg; - /* Retrieve the duration from timespec in microsecond */ timeout_us = (uint64_t)ts->tv_sec * USEC_PER_SEC + @@ -278,11 +263,8 @@ static int esp_oneshot_start(struct oneshot_lowerhalf_s *lower, /* Configure callback, in case a handler was provided before */ - if (priv->callback != NULL) - { - timer_ll_enable_intr(hal->dev, TIMER_LL_EVENT_ALARM(hal->timer_id), - true); - } + timer_ll_enable_intr(hal->dev, TIMER_LL_EVENT_ALARM(hal->timer_id), + true); /* Finally, start the timer */ @@ -368,9 +350,7 @@ static int esp_oneshot_cancel(struct oneshot_lowerhalf_s *lower, ts->tv_nsec = remaining_us * NSEC_PER_USEC; } - priv->running = false; - priv->callback = NULL; - priv->arg = NULL; + priv->running = false; return OK; } @@ -441,8 +421,6 @@ IRAM_ATTR static int esp_oneshot_isr(int irq, void *context, void *arg) { struct esp_oneshot_lowerhalf_s *priv = (struct esp_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *callback_arg; timer_hal_context_t *hal = &(priv->hal); uint32_t intr_status = timer_ll_get_intr_status(hal->dev); @@ -460,16 +438,9 @@ IRAM_ATTR static int esp_oneshot_isr(int irq, void *context, void *arg) priv->running = false; - /* Forward the event, clearing out any vestiges */ - - callback = priv->callback; - callback_arg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - /* Call the callback */ - callback(&priv->lh, callback_arg); + oneshot_process_callback(&priv->lh); return OK; } @@ -510,8 +481,6 @@ struct oneshot_lowerhalf_s *oneshot_initialize(int chan, uint16_t resolution) /* Initialize the elements of lower half state structure */ - lower->callback = NULL; - lower->arg = NULL; lower->resolution = resolution; lower->running = false; diff --git a/arch/risc-v/src/common/riscv_mtimer.c b/arch/risc-v/src/common/riscv_mtimer.c index 52c6fab3122e5..4e1a632ad02e5 100644 --- a/arch/risc-v/src/common/riscv_mtimer.c +++ b/arch/risc-v/src/common/riscv_mtimer.c @@ -46,8 +46,6 @@ struct riscv_mtimer_lowerhalf_s uintreg_t mtimecmp; uint64_t freq; uint64_t alarm; - oneshot_callback_t callback; - void *arg; }; /**************************************************************************** @@ -57,23 +55,12 @@ struct riscv_mtimer_lowerhalf_s static int riscv_mtimer_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int riscv_mtimer_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int riscv_mtimer_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int riscv_mtimer_current(struct oneshot_lowerhalf_s *lower, struct timespec *ts); -static int riscv_mtimer_tick_max_delay(struct oneshot_lowerhalf_s *lower, - clock_t *ticks); -static int riscv_mtimer_tick_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, - clock_t ticks); -static int riscv_mtimer_tick_cancel(struct oneshot_lowerhalf_s *lower, - clock_t *ticks); -static int riscv_mtimer_tick_current(struct oneshot_lowerhalf_s *lower, - clock_t *ticks); - /**************************************************************************** * Private Data ****************************************************************************/ @@ -84,10 +71,6 @@ static const struct oneshot_operations_s g_riscv_mtimer_ops = .start = riscv_mtimer_start, .cancel = riscv_mtimer_cancel, .current = riscv_mtimer_current, - .tick_start = riscv_mtimer_tick_start, - .tick_current = riscv_mtimer_tick_current, - .tick_max_delay = riscv_mtimer_tick_max_delay, - .tick_cancel = riscv_mtimer_tick_cancel, }; /**************************************************************************** @@ -132,32 +115,6 @@ static int riscv_mtimer_max_delay(struct oneshot_lowerhalf_s *lower, return 0; } -/**************************************************************************** - * Name: riscv_mtimer_tick_max_delay - * - * Description: - * Determine the maximum delay of the one-shot timer - * - * Input Parameters: - * lower An instance of the lower-half oneshot state structure. This - * structure must have been previously initialized via a call to - * oneshot_initialize(); - * ticks The location in which to return the maximum delay. - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -static int riscv_mtimer_tick_max_delay(struct oneshot_lowerhalf_s *lower, - clock_t *ticks) -{ - *ticks = (clock_t)UINT64_MAX; - - return 0; -} - /**************************************************************************** * Name: riscv_mtimer_start * @@ -179,7 +136,6 @@ static int riscv_mtimer_tick_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int riscv_mtimer_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct riscv_mtimer_lowerhalf_s *priv = @@ -194,56 +150,7 @@ static int riscv_mtimer_start(struct oneshot_lowerhalf_s *lower, alarm = mtime + ts->tv_sec * priv->freq + ts->tv_nsec * priv->freq / NSEC_PER_SEC; - priv->alarm = alarm; - priv->callback = callback; - priv->arg = arg; - - riscv_mtimer_set_mtimecmp(priv, priv->alarm); - - up_irq_restore(flags); - return 0; -} - -/**************************************************************************** - * Name: riscv_mtimer_tick_start - * - * Description: - * Start the oneshot timer - * - * Input Parameters: - * lower An instance of the lower-half oneshot state structure. This - * structure must have been previously initialized via a call to - * oneshot_initialize(); - * handler The function to call when when the oneshot timer expires. - * arg An opaque argument that will accompany the callback. - * ticks Provides the duration of the one shot timer. - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -static int riscv_mtimer_tick_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, - clock_t ticks) -{ - struct riscv_mtimer_lowerhalf_s *priv = - (struct riscv_mtimer_lowerhalf_s *)lower; - irqstate_t flags; - uint64_t mtime; - clock_t current; - uint64_t alarm; - - flags = up_irq_save(); - - mtime = riscv_mtimer_get_mtime(priv); - current = mtime * TICK_PER_SEC / priv->freq; - alarm = (current + ticks) * priv->freq / TICK_PER_SEC; - - priv->alarm = alarm; - priv->callback = callback; - priv->arg = arg; + priv->alarm = alarm; riscv_mtimer_set_mtimecmp(priv, priv->alarm); @@ -297,62 +204,7 @@ static int riscv_mtimer_cancel(struct oneshot_lowerhalf_s *lower, ts->tv_sec = nsec / NSEC_PER_SEC; ts->tv_nsec = nsec % NSEC_PER_SEC; - priv->alarm = 0; - priv->callback = NULL; - priv->arg = NULL; - - up_irq_restore(flags); - - return 0; -} - -/**************************************************************************** - * Name: riscv_mtimer_tick_cancel - * - * Description: - * Cancel the oneshot timer and return the time remaining on the timer. - * - * NOTE: This function may execute at a high rate with no timer running (as - * when pre-emption is enabled and disabled). - * - * Input Parameters: - * lower Caller allocated instance of the oneshot state structure. This - * structure must have been previously initialized via a call to - * oneshot_initialize(); - * ticks The location in which to return the time remaining on the - * oneshot timer. A time of zero is returned if the timer is - * not running. - * - * Returned Value: - * Zero (OK) is returned on success. A call to up_timer_cancel() when - * the timer is not active should also return success; a negated errno - * value is returned on any failure. - * - ****************************************************************************/ - -static int riscv_mtimer_tick_cancel(struct oneshot_lowerhalf_s *lower, - clock_t *ticks) -{ - struct riscv_mtimer_lowerhalf_s *priv = - (struct riscv_mtimer_lowerhalf_s *)lower; - uint64_t mtime; - uint64_t alarm; - irqstate_t flags; - - flags = up_irq_save(); - - alarm = priv->alarm; - - mtime = riscv_mtimer_get_mtime(priv); - - riscv_mtimer_set_mtimecmp(priv, UINT64_MAX); - - *ticks = alarm * TICK_PER_SEC / priv->freq - - mtime * TICK_PER_SEC / priv->freq; - - priv->alarm = 0; - priv->callback = NULL; - priv->arg = NULL; + priv->alarm = 0; up_irq_restore(flags); @@ -397,45 +249,11 @@ static int riscv_mtimer_interrupt(int irq, void *context, void *arg) { struct riscv_mtimer_lowerhalf_s *priv = arg; - if (priv->callback != NULL) - { - priv->callback(&priv->lower, priv->arg); - } + oneshot_process_callback(&priv->lower); return 0; } -/**************************************************************************** - * Name: riscv_mtimer_tick_current - * - * Description: - * Get the current time. - * - * Input Parameters: - * lower Caller allocated instance of the oneshot state structure. This - * structure must have been previously initialized via a call to - * oneshot_initialize(); - * ticks The location in which to return the current time. A time of zero - * is returned for the initialization moment. - * - * Returned Value: - * Zero (OK) is returned on success, a negated errno value is returned on - * any failure. - * - ****************************************************************************/ - -static int riscv_mtimer_tick_current(struct oneshot_lowerhalf_s *lower, - clock_t *ticks) -{ - struct riscv_mtimer_lowerhalf_s *priv = - (struct riscv_mtimer_lowerhalf_s *)lower; - uint64_t mtime = riscv_mtimer_get_mtime(priv); - - *ticks = mtime * TICK_PER_SEC / priv->freq; - - return OK; -} - /**************************************************************************** * Public Functions ****************************************************************************/ diff --git a/arch/risc-v/src/esp32c3-legacy/esp32c3_oneshot_lowerhalf.c b/arch/risc-v/src/esp32c3-legacy/esp32c3_oneshot_lowerhalf.c index 817cedbd70a59..6a620fbd570b2 100644 --- a/arch/risc-v/src/esp32c3-legacy/esp32c3_oneshot_lowerhalf.c +++ b/arch/risc-v/src/esp32c3-legacy/esp32c3_oneshot_lowerhalf.c @@ -58,8 +58,6 @@ struct esp32c3_oneshot_lowerhalf_s struct oneshot_lowerhalf_s lh; /* Lower half instance */ struct esp32c3_oneshot_s oneshot; /* ESP32-C3-specific oneshot state */ - oneshot_callback_t callback; /* Upper half Interrupt callback */ - void *arg; /* Argument passed to handler */ uint16_t resolution; }; @@ -74,8 +72,6 @@ static void esp32c3_oneshot_lh_handler(void *arg); static int oneshot_lh_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int oneshot_lh_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, - void *arg, const struct timespec *ts); static int oneshot_lh_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -116,11 +112,8 @@ static void esp32c3_oneshot_lh_handler(void *arg) { struct esp32c3_oneshot_lowerhalf_s *priv = (struct esp32c3_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *cb_arg; DEBUGASSERT(priv != NULL); - DEBUGASSERT(priv->callback != NULL); tmrinfo("Oneshot LH handler triggered\n"); @@ -128,14 +121,7 @@ static void esp32c3_oneshot_lh_handler(void *arg) * restarts the oneshot). */ - callback = priv->callback; - cb_arg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cb_arg); + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -199,8 +185,6 @@ static int oneshot_lh_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int oneshot_lh_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, - void *arg, const struct timespec *ts) { struct esp32c3_oneshot_lowerhalf_s *priv = @@ -209,19 +193,13 @@ static int oneshot_lh_start(struct oneshot_lowerhalf_s *lower, irqstate_t flags; DEBUGASSERT(priv != NULL); - DEBUGASSERT(callback != NULL); - DEBUGASSERT(arg != NULL); DEBUGASSERT(ts != NULL); /* Save the callback information and start the timer */ - flags = enter_critical_section(); - priv->callback = callback; - priv->arg = arg; - ret = esp32c3_oneshot_start(&priv->oneshot, - esp32c3_oneshot_lh_handler, - priv, - ts); + flags = enter_critical_section(); + ret = esp32c3_oneshot_start(&priv->oneshot, esp32c3_oneshot_lh_handler, + priv, ts); leave_critical_section(flags); if (ret < 0) @@ -268,10 +246,8 @@ static int oneshot_lh_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = enter_critical_section(); - ret = esp32c3_oneshot_cancel(&priv->oneshot, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = enter_critical_section(); + ret = esp32c3_oneshot_cancel(&priv->oneshot, ts); leave_critical_section(flags); if (ret < 0) @@ -358,8 +334,6 @@ struct oneshot_lowerhalf_s *oneshot_initialize(int chan, } priv->lh.ops = &g_esp32c3_timer_ops; /* Pointer to the LH operations */ - priv->callback = NULL; /* No callback yet */ - priv->arg = NULL; /* No arg yet */ priv->resolution = resolution; /* Configured resolution */ /* Initialize esp32c3_oneshot_s structure */ diff --git a/arch/sim/src/sim/sim_oneshot.c b/arch/sim/src/sim/sim_oneshot.c index 96e8594169919..02b787f853fb4 100644 --- a/arch/sim/src/sim/sim_oneshot.c +++ b/arch/sim/src/sim/sim_oneshot.c @@ -62,9 +62,7 @@ struct sim_oneshot_lowerhalf_s sq_entry_t link; struct timespec alarm; - - oneshot_callback_t callback; /* internal handler that receives callback */ - void *arg; /* Argument that is passed to the handler */ + int running; }; /**************************************************************************** @@ -76,7 +74,6 @@ static void sim_process_tick(sq_entry_t *entry); static int sim_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int sim_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int sim_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -223,8 +220,6 @@ static void sim_process_tick(sq_entry_t *entry) { struct sim_oneshot_lowerhalf_s *priv = container_of(entry, struct sim_oneshot_lowerhalf_s, link); - oneshot_callback_t callback; - void *cbarg; DEBUGASSERT(priv != NULL); @@ -238,20 +233,10 @@ static void sim_process_tick(sq_entry_t *entry) sim_reset_alarm(&priv->alarm); - if (priv->callback) + if (priv->running == 1) { - /* Sample and nullify BEFORE executing callback (in case the callback - * restarts the oneshot). - */ - - callback = priv->callback; - cbarg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cbarg); + priv->running = 0; + oneshot_process_callback(&priv->lh); } } @@ -304,23 +289,21 @@ static int sim_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int sim_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct sim_oneshot_lowerhalf_s *priv = (struct sim_oneshot_lowerhalf_s *)lower; + struct timespec current; irqstate_t flags; - DEBUGASSERT(priv != NULL && callback != NULL && ts != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); flags = enter_critical_section(); - clock_ticks2time(&priv->alarm, - host_gettime(false) / NSEC_PER_TICK + - clock_time2ticks(ts)); + priv->running = 1; - priv->callback = callback; - priv->arg = arg; + sim_timer_current(¤t); + clock_timespec_add(¤t, ts, &priv->alarm); sim_update_hosttimer(); @@ -374,9 +357,6 @@ static int sim_cancel(struct oneshot_lowerhalf_s *lower, sim_reset_alarm(&priv->alarm); sim_update_hosttimer(); - priv->callback = NULL; - priv->arg = NULL; - leave_critical_section(flags); return OK; @@ -474,6 +454,8 @@ struct oneshot_lowerhalf_s *oneshot_initialize(int chan, /* Initialize the lower-half driver structure */ + priv->running = 0; + sq_addlast(&priv->link, &g_oneshot_list); priv->lh.ops = &g_oneshot_ops; diff --git a/arch/sparc/src/bm3803/bm3803_oneshot_lowerhalf.c b/arch/sparc/src/bm3803/bm3803_oneshot_lowerhalf.c index 5f1bd1f311836..d3a7cb5ed5292 100644 --- a/arch/sparc/src/bm3803/bm3803_oneshot_lowerhalf.c +++ b/arch/sparc/src/bm3803/bm3803_oneshot_lowerhalf.c @@ -60,8 +60,6 @@ struct bm3803_oneshot_lowerhalf_s /* STM32-specific oneshot state */ struct bm3803_oneshot_s oneshot; - oneshot_callback_t callback; /* internal handler that receives callback */ - void *arg; /* Argument that is passed to the handler */ }; /**************************************************************************** @@ -73,8 +71,7 @@ static void bm3803_oneshot_handler(void *arg); static int bm3803_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int bm3803_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, - const struct timespec *ts); + const struct timespec *ts); static int bm3803_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -114,8 +111,6 @@ static void bm3803_oneshot_handler(void *arg) { struct bm3803_oneshot_lowerhalf_s *priv = (struct bm3803_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *cbarg; DEBUGASSERT(priv != NULL); @@ -123,21 +118,7 @@ static void bm3803_oneshot_handler(void *arg) * bm3803_cancel? */ - if (priv->callback) - { - /* Sample and nullify BEFORE executing callback (in case the callback - * restarts the oneshot). - */ - - callback = priv->callback; - cbarg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cbarg); - } + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -201,23 +182,20 @@ static int bm3803_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int bm3803_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, - const struct timespec *ts) + const struct timespec *ts) { struct bm3803_oneshot_lowerhalf_s *priv = (struct bm3803_oneshot_lowerhalf_s *)lower; irqstate_t flags; int ret; - DEBUGASSERT(priv != NULL && callback != NULL && ts != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); /* Save the callback information and start the timer */ - flags = enter_critical_section(); - priv->callback = callback; - priv->arg = arg; - ret = bm3803_oneshot_start(&priv->oneshot, - bm3803_oneshot_handler, priv, ts); + flags = enter_critical_section(); + ret = bm3803_oneshot_start(&priv->oneshot, + bm3803_oneshot_handler, priv, ts); leave_critical_section(flags); if (ret < 0) @@ -264,10 +242,8 @@ static int bm3803_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = enter_critical_section(); - ret = bm3803_oneshot_cancel(&priv->oneshot, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = enter_critical_section(); + ret = bm3803_oneshot_cancel(&priv->oneshot, ts); leave_critical_section(flags); if (ret < 0) diff --git a/arch/tricore/src/common/tricore_systimer.c b/arch/tricore/src/common/tricore_systimer.c index 70ad40629cffb..c3d574dc92efc 100644 --- a/arch/tricore/src/common/tricore_systimer.c +++ b/arch/tricore/src/common/tricore_systimer.c @@ -49,8 +49,6 @@ struct tricore_systimer_lowerhalf_s volatile void *tbase; uint64_t freq; uint64_t alarm; - oneshot_callback_t callback; - void *arg; spinlock_t lock; }; @@ -61,16 +59,11 @@ struct tricore_systimer_lowerhalf_s static int tricore_systimer_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int tricore_systimer_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int tricore_systimer_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int tricore_systimer_current(struct oneshot_lowerhalf_s *lower, struct timespec *ts); -static int -tricore_systimer_tick_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, - clock_t ticks); /**************************************************************************** * Private Data @@ -82,7 +75,6 @@ static const struct oneshot_operations_s g_tricore_systimer_ops = .start = tricore_systimer_start, .cancel = tricore_systimer_cancel, .current = tricore_systimer_current, - .tick_start = tricore_systimer_tick_start, }; static struct tricore_systimer_lowerhalf_s g_systimer_lower = @@ -170,7 +162,6 @@ static int tricore_systimer_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int tricore_systimer_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct tricore_systimer_lowerhalf_s *priv = @@ -184,9 +175,6 @@ static int tricore_systimer_start(struct oneshot_lowerhalf_s *lower, priv->alarm = UINT64_MAX; } - priv->callback = callback; - priv->arg = arg; - tricore_systimer_set_timecmp(priv, priv->alarm); return 0; } @@ -239,9 +227,7 @@ static int tricore_systimer_cancel(struct oneshot_lowerhalf_s *lower, ts->tv_nsec = 0; } - priv->alarm = 0; - priv->callback = NULL; - priv->arg = NULL; + priv->alarm = 0; return 0; } @@ -279,48 +265,6 @@ static int tricore_systimer_current(struct oneshot_lowerhalf_s *lower, return 0; } -/**************************************************************************** - * Name: tricore_systimer_tick_start - * - * Description: - * Start the oneshot timer - * - * Input Parameters: - * lower An instance of the lower-half oneshot state structure. This - * structure must have been previously initialized via a call to - * oneshot_initialize(); - * handler The function to call when when the oneshot timer expires. - * arg An opaque argument that will accompany the callback. - * ticks Provides the duration of the one shot timer. - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -static int -tricore_systimer_tick_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, - clock_t ticks) -{ - struct tricore_systimer_lowerhalf_s *priv = - (struct tricore_systimer_lowerhalf_s *)lower; - uint64_t mtime = tricore_systimer_get_time(priv); - - priv->alarm = mtime + priv->freq * ticks / TICK_PER_SEC; - if (priv->alarm < mtime) - { - priv->alarm = UINT64_MAX; - } - - priv->callback = callback; - priv->arg = arg; - - tricore_systimer_set_timecmp(priv, priv->alarm); - return 0; -} - /**************************************************************************** * Name: tricore_systimer_interrupt * @@ -335,10 +279,7 @@ static int tricore_systimer_interrupt(int irq, void *context, void *arg) struct tricore_systimer_lowerhalf_s *priv = arg; tricore_systimer_set_timecmp(priv, UINT64_MAX); - if (priv->callback != NULL) - { - priv->callback(&priv->lower, priv->arg); - } + oneshot_process_callback(&priv->lower); return 0; } diff --git a/arch/x86_64/src/intel64/intel64_oneshot_lower.c b/arch/x86_64/src/intel64/intel64_oneshot_lower.c index f82aace3e9843..bb8e55d23058d 100644 --- a/arch/x86_64/src/intel64/intel64_oneshot_lower.c +++ b/arch/x86_64/src/intel64/intel64_oneshot_lower.c @@ -49,8 +49,6 @@ struct intel64_oneshot_lowerhalf_s { struct oneshot_lowerhalf_s lh; struct intel64_oneshot_s oneshot; - oneshot_callback_t callback; - void *arg; }; /**************************************************************************** @@ -62,21 +60,11 @@ static void intel64_oneshot_handler(void *arg); static int intel64_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int intel64_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, - const struct timespec *ts); + const struct timespec *ts); static int intel64_cancel(struct oneshot_lowerhalf_s *lower, - struct timespec *ts); + struct timespec *ts); static int intel64_current(struct oneshot_lowerhalf_s *lower, struct timespec *ts); -static int intel64_tick_max_delay(struct oneshot_lowerhalf_s *lower, - clock_t *ticks); -static int intel64_tick_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, - clock_t ticks); -static int intel64_tick_cancel(struct oneshot_lowerhalf_s *lower, - clock_t *ticks); -static int intel64_tick_current(struct oneshot_lowerhalf_s *lower, - clock_t *ticks); /**************************************************************************** * Private Data @@ -89,11 +77,7 @@ static const struct oneshot_operations_s g_oneshot_ops = .max_delay = intel64_max_delay, .start = intel64_start, .cancel = intel64_cancel, - .current = intel64_current, - .tick_max_delay = intel64_tick_max_delay, - .tick_start = intel64_tick_start, - .tick_cancel = intel64_tick_cancel, - .tick_current = intel64_tick_current, + .current = intel64_current }; static spinlock_t g_oneshotlow_spin; @@ -120,8 +104,6 @@ static spinlock_t g_oneshotlow_spin; static void intel64_oneshot_handler(void *arg) { struct intel64_oneshot_lowerhalf_s *priv = arg; - oneshot_callback_t callback; - void *cbarg; DEBUGASSERT(priv != NULL); @@ -129,21 +111,7 @@ static void intel64_oneshot_handler(void *arg) * intel64_cancel? */ - if (priv->callback) - { - /* Sample and nullify BEFORE executing callback (in case the callback - * restarts the oneshot). - */ - - callback = priv->callback; - cbarg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cbarg); - } + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -208,7 +176,6 @@ static int intel64_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int intel64_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct intel64_oneshot_lowerhalf_s *priv = @@ -216,16 +183,13 @@ static int intel64_start(struct oneshot_lowerhalf_s *lower, irqstate_t flags; int ret; - DEBUGASSERT(priv != NULL && callback != NULL && ts != NULL); + DEBUGASSERT(priv != NULL && ts != NULL); /* Save the callback information and start the timer */ - flags = spin_lock_irqsave(&g_oneshotlow_spin); - priv->callback = callback; - priv->arg = arg; - ret = intel64_oneshot_start(&priv->oneshot, - intel64_oneshot_handler, - priv, ts); + flags = spin_lock_irqsave(&g_oneshotlow_spin); + ret = intel64_oneshot_start(&priv->oneshot, intel64_oneshot_handler, + priv, ts); spin_unlock_irqrestore(&g_oneshotlow_spin, flags); if (ret < 0) @@ -272,10 +236,8 @@ static int intel64_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = spin_lock_irqsave(&g_oneshotlow_spin); - ret = intel64_oneshot_cancel(&priv->oneshot, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = spin_lock_irqsave(&g_oneshotlow_spin); + ret = intel64_oneshot_cancel(&priv->oneshot, ts); spin_unlock_irqrestore(&g_oneshotlow_spin, flags); if (ret < 0) @@ -321,143 +283,6 @@ static int intel64_current(struct oneshot_lowerhalf_s *lower, return OK; } -/**************************************************************************** - * Name: intel64_tick_max_delay - * - * Description: - * Determine the maximum delay of the one-shot timer (in microseconds) - * - * Input Parameters: - * lower An instance of the lower-half oneshot state structure. This - * structure must have been previously initialized via a call to - * oneshot_initialize(); - * ticks The location in which to return the maximum delay. - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -static int intel64_tick_max_delay(struct oneshot_lowerhalf_s *lower, - clock_t *ticks) -{ - struct timespec ts; - int ret; - - ret = intel64_max_delay(lower, &ts); - - /* Convert time to ticks */ - - *ticks = clock_time2ticks(&ts); - - return ret; -} - -/**************************************************************************** - * Name: intel64_tick_start - * - * Description: - * Start the oneshot timer - * - * Input Parameters: - * lower An instance of the lower-half oneshot state structure. This - * structure must have been previously initialized via a call to - * oneshot_initialize(); - * handler The function to call when when the oneshot timer expires. - * arg An opaque argument that will accompany the callback. - * ticks Provides the duration of the one shot timer. - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -static int intel64_tick_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, - clock_t ticks) -{ - struct timespec ts; - - /* Convert ticks to time */ - - clock_ticks2time(&ts, ticks); - - return intel64_start(lower, callback, arg, &ts); -} - -/**************************************************************************** - * Name: intel64_tick_cancel - * - * Description: - * Cancel the oneshot timer and return the time remaining on the timer. - * - * NOTE: This function may execute at a high rate with no timer running (as - * when pre-emption is enabled and disabled). - * - * Input Parameters: - * lower Caller allocated instance of the oneshot state structure. This - * structure must have been previously initialized via a call to - * oneshot_initialize(); - * ticks The location in which to return the time remaining on the - * oneshot timer. - * - * Returned Value: - * Zero (OK) is returned on success. A call to up_timer_cancel() when - * the timer is not active should also return success; a negated errno - * value is returned on any failure. - * - ****************************************************************************/ - -static int intel64_tick_cancel(struct oneshot_lowerhalf_s *lower, - clock_t *ticks) -{ - struct timespec ts; - int ret; - - ret = intel64_cancel(lower, &ts); - - /* Convert time to ticks */ - - *ticks = clock_time2ticks(&ts); - - return ret; -} - -/**************************************************************************** - * Name: intel64_tick_current - * - * Description: - * Get the current time. - * - * Input Parameters: - * lower Caller allocated instance of the oneshot state structure. This - * structure must have been previously initialized via a call to - * oneshot_initialize(); - * ticks The location in which to return the current time. - * - * Returned Value: - * Zero (OK) is returned on success, a negated errno value is returned on - * any failure. - * - ****************************************************************************/ - -static int intel64_tick_current(struct oneshot_lowerhalf_s *lower, - clock_t *ticks) -{ - struct timespec ts; - int ret; - - ret = intel64_current(lower, &ts); - - /* Convert time to ticks */ - - *ticks = clock_time2ticks(&ts); - - return ret; -} - /**************************************************************************** * Public Functions ****************************************************************************/ diff --git a/arch/xtensa/src/common/xtensa_oneshot.c b/arch/xtensa/src/common/xtensa_oneshot.c index 100500349e62e..c3bf1b151c833 100644 --- a/arch/xtensa/src/common/xtensa_oneshot.c +++ b/arch/xtensa/src/common/xtensa_oneshot.c @@ -47,8 +47,6 @@ struct xoneshot_lowerhalf_s { struct oneshot_lowerhalf_s lh; /* Lower half operations */ uint32_t freq; /* Timer working clock frequency(Hz) */ - oneshot_callback_t callback; /* Current user interrupt callback */ - void *arg; /* Argument passed to upper half callback */ uint32_t irq; }; @@ -59,7 +57,6 @@ struct xoneshot_lowerhalf_s static int xtensa_oneshot_maxdelay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int xtensa_oneshot_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, void *arg, const struct timespec *ts); static int xtensa_oneshot_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -95,7 +92,6 @@ static inline uint64_t sec_to_count(uint32_t sec, uint32_t freq) } static int xtensa_oneshot_start(struct oneshot_lowerhalf_s *lower_, - oneshot_callback_t callback, void *arg, const struct timespec *ts) { struct xoneshot_lowerhalf_s *lower = @@ -105,9 +101,6 @@ static int xtensa_oneshot_start(struct oneshot_lowerhalf_s *lower_, flags = enter_critical_section(); - lower->callback = callback; - lower->arg = arg; - count = sec_to_count((uint64_t)ts->tv_sec, lower->freq) + nsec_to_count((uint64_t)ts->tv_nsec, lower->freq); @@ -130,9 +123,6 @@ static int xtensa_oneshot_cancel(struct oneshot_lowerhalf_s *lower_, flags = enter_critical_section(); - lower->callback = NULL; - lower->arg = NULL; - up_disable_irq(lower->irq); leave_critical_section(flags); @@ -157,22 +147,10 @@ static int xtensa_oneshot_maxdelay(struct oneshot_lowerhalf_s *lower_, static int xtensa_oneshot_interrupt(int irq, void *context, void *arg) { struct xoneshot_lowerhalf_s *lower = arg; - oneshot_callback_t callback; - void *cbarg; DEBUGASSERT(lower != NULL); - if (lower->callback != NULL) - { - callback = lower->callback; - cbarg = lower->arg; - lower->callback = NULL; - lower->arg = NULL; - - /* Then perform the callback */ - - callback(&lower->lh, cbarg); - } + oneshot_process_callback(&lower->lh); return 0; } diff --git a/arch/xtensa/src/esp32/esp32_oneshot_lowerhalf.c b/arch/xtensa/src/esp32/esp32_oneshot_lowerhalf.c index b98f0758323e4..4adba56bf95ee 100644 --- a/arch/xtensa/src/esp32/esp32_oneshot_lowerhalf.c +++ b/arch/xtensa/src/esp32/esp32_oneshot_lowerhalf.c @@ -58,8 +58,6 @@ struct esp32_oneshot_lowerhalf_s struct oneshot_lowerhalf_s lh; /* Lower half instance */ struct esp32_oneshot_s oneshot; /* ESP32-specific oneshot state */ - oneshot_callback_t callback; /* Upper half Interrupt callback */ - void *arg; /* Argument passed to handler */ uint16_t resolution; /* Timer's resulation in uS */ spinlock_t lock; /* Device specific lock */ }; @@ -75,8 +73,6 @@ static void esp32_oneshot_lh_handler(void *arg); static int esp32_max_lh_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int esp32_lh_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, - void *arg, const struct timespec *ts); static int esp32_lh_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -117,26 +113,12 @@ static void esp32_oneshot_lh_handler(void *arg) { struct esp32_oneshot_lowerhalf_s *priv = (struct esp32_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *cb_arg; DEBUGASSERT(priv != NULL); - DEBUGASSERT(priv->callback != NULL); tmrinfo("Oneshot LH handler triggered\n"); - /* Sample and nullify BEFORE executing callback (in case the callback - * restarts the oneshot). - */ - - callback = priv->callback; - cb_arg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cb_arg); + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -200,8 +182,6 @@ static int esp32_max_lh_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int esp32_lh_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, - void *arg, const struct timespec *ts) { struct esp32_oneshot_lowerhalf_s *priv = @@ -210,16 +190,13 @@ static int esp32_lh_start(struct oneshot_lowerhalf_s *lower, irqstate_t flags; DEBUGASSERT(priv != NULL); - DEBUGASSERT(callback != NULL); DEBUGASSERT(ts != NULL); /* Save the callback information and start the timer */ - flags = spin_lock_irqsave(&priv->lock); - priv->callback = callback; - priv->arg = arg; - ret = esp32_oneshot_start(&priv->oneshot, - esp32_oneshot_lh_handler, priv, ts); + flags = spin_lock_irqsave(&priv->lock); + ret = esp32_oneshot_start(&priv->oneshot, + esp32_oneshot_lh_handler, priv, ts); spin_unlock_irqrestore(&priv->lock, flags); if (ret < 0) @@ -266,10 +243,8 @@ static int esp32_lh_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = spin_lock_irqsave(&priv->lock); - ret = esp32_oneshot_cancel(&priv->oneshot, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = spin_lock_irqsave(&priv->lock); + ret = esp32_oneshot_cancel(&priv->oneshot, ts); spin_unlock_irqrestore(&priv->lock, flags); if (ret < 0) @@ -356,8 +331,6 @@ struct oneshot_lowerhalf_s *oneshot_initialize(int chan, } priv->lh.ops = &g_esp32_timer_ops; /* Pointer to the LH operations */ - priv->callback = NULL; /* No callback yet */ - priv->arg = NULL; /* No arg yet */ priv->resolution = resolution; /* Configured resolution */ /* Initialize esp32_oneshot_s structure */ diff --git a/arch/xtensa/src/esp32s2/esp32s2_oneshot_lowerhalf.c b/arch/xtensa/src/esp32s2/esp32s2_oneshot_lowerhalf.c index 0c1fe3929cf23..3f52825dbfdef 100644 --- a/arch/xtensa/src/esp32s2/esp32s2_oneshot_lowerhalf.c +++ b/arch/xtensa/src/esp32s2/esp32s2_oneshot_lowerhalf.c @@ -56,8 +56,6 @@ struct esp32s2_oneshot_lowerhalf_s struct oneshot_lowerhalf_s lh; /* Lower half instance */ struct esp32s2_oneshot_s oneshot; /* ESP32-S2-specific oneshot state */ - oneshot_callback_t callback; /* Upper half Interrupt callback */ - void *arg; /* Argument passed to handler */ uint16_t resolution; }; @@ -72,8 +70,6 @@ static void esp32s2_oneshot_lh_handler(void *arg); static int oneshot_lh_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int oneshot_lh_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, - void *arg, const struct timespec *ts); static int oneshot_lh_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -114,11 +110,8 @@ static void esp32s2_oneshot_lh_handler(void *arg) { struct esp32s2_oneshot_lowerhalf_s *priv = (struct esp32s2_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *cb_arg; DEBUGASSERT(priv != NULL); - DEBUGASSERT(priv->callback != NULL); tmrinfo("Oneshot LH handler triggered\n"); @@ -126,14 +119,7 @@ static void esp32s2_oneshot_lh_handler(void *arg) * restarts the oneshot). */ - callback = priv->callback; - cb_arg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cb_arg); + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -197,8 +183,6 @@ static int oneshot_lh_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int oneshot_lh_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, - void *arg, const struct timespec *ts) { struct esp32s2_oneshot_lowerhalf_s *priv = @@ -207,19 +191,13 @@ static int oneshot_lh_start(struct oneshot_lowerhalf_s *lower, irqstate_t flags; DEBUGASSERT(priv != NULL); - DEBUGASSERT(callback != NULL); - DEBUGASSERT(arg != NULL); DEBUGASSERT(ts != NULL); /* Save the callback information and start the timer */ - flags = enter_critical_section(); - priv->callback = callback; - priv->arg = arg; - ret = esp32s2_oneshot_start(&priv->oneshot, - esp32s2_oneshot_lh_handler, - priv, - ts); + flags = enter_critical_section(); + ret = esp32s2_oneshot_start(&priv->oneshot, esp32s2_oneshot_lh_handler, + priv, ts); leave_critical_section(flags); if (ret < 0) @@ -266,10 +244,8 @@ static int oneshot_lh_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = enter_critical_section(); - ret = esp32s2_oneshot_cancel(&priv->oneshot, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = enter_critical_section(); + ret = esp32s2_oneshot_cancel(&priv->oneshot, ts); leave_critical_section(flags); if (ret < 0) @@ -356,8 +332,6 @@ struct oneshot_lowerhalf_s *oneshot_initialize(int chan, } priv->lh.ops = &g_esp32s2_timer_ops; /* Pointer to the LH operations */ - priv->callback = NULL; /* No callback yet */ - priv->arg = NULL; /* No arg yet */ priv->resolution = resolution; /* Configured resolution */ /* Initialize esp32s2_oneshot_s structure */ diff --git a/arch/xtensa/src/esp32s3/esp32s3_oneshot_lowerhalf.c b/arch/xtensa/src/esp32s3/esp32s3_oneshot_lowerhalf.c index af621f26f37d9..53d4481ffe036 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_oneshot_lowerhalf.c +++ b/arch/xtensa/src/esp32s3/esp32s3_oneshot_lowerhalf.c @@ -57,8 +57,6 @@ struct esp32s3_oneshot_lowerhalf_s struct oneshot_lowerhalf_s lh; /* Lower-half instance */ struct esp32s3_oneshot_s oneshot; /* ESP32-S3-specific oneshot state */ - oneshot_callback_t callback; /* Upper-half Interrupt callback */ - void *arg; /* Argument passed to handler */ uint16_t resolution; /* Timer's resolution in microseconds */ spinlock_t lock; /* Device-specific lock */ }; @@ -74,8 +72,6 @@ static void oneshot_lh_handler(void *arg); static int oneshot_lh_max_delay(struct oneshot_lowerhalf_s *lower, struct timespec *ts); static int oneshot_lh_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, - void *arg, const struct timespec *ts); static int oneshot_lh_cancel(struct oneshot_lowerhalf_s *lower, struct timespec *ts); @@ -116,11 +112,8 @@ static void oneshot_lh_handler(void *arg) { struct esp32s3_oneshot_lowerhalf_s *priv = (struct esp32s3_oneshot_lowerhalf_s *)arg; - oneshot_callback_t callback; - void *cb_arg; DEBUGASSERT(priv != NULL); - DEBUGASSERT(priv->callback != NULL); tmrinfo("Oneshot LH handler triggered\n"); @@ -128,14 +121,7 @@ static void oneshot_lh_handler(void *arg) * restarts the oneshot). */ - callback = priv->callback; - cb_arg = priv->arg; - priv->callback = NULL; - priv->arg = NULL; - - /* Then perform the callback */ - - callback(&priv->lh, cb_arg); + oneshot_process_callback(&priv->lh); } /**************************************************************************** @@ -197,8 +183,6 @@ static int oneshot_lh_max_delay(struct oneshot_lowerhalf_s *lower, ****************************************************************************/ static int oneshot_lh_start(struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, - void *arg, const struct timespec *ts) { struct esp32s3_oneshot_lowerhalf_s *priv = @@ -207,16 +191,13 @@ static int oneshot_lh_start(struct oneshot_lowerhalf_s *lower, irqstate_t flags; DEBUGASSERT(priv != NULL); - DEBUGASSERT(callback != NULL); DEBUGASSERT(ts != NULL); /* Save the callback information and start the timer */ - flags = spin_lock_irqsave(&priv->lock); - priv->callback = callback; - priv->arg = arg; - ret = esp32s3_oneshot_start(&priv->oneshot, oneshot_lh_handler, - priv, ts); + flags = spin_lock_irqsave(&priv->lock); + ret = esp32s3_oneshot_start(&priv->oneshot, oneshot_lh_handler, + priv, ts); spin_unlock_irqrestore(&priv->lock, flags); if (ret < 0) @@ -263,10 +244,8 @@ static int oneshot_lh_cancel(struct oneshot_lowerhalf_s *lower, /* Cancel the timer */ - flags = spin_lock_irqsave(&priv->lock); - ret = esp32s3_oneshot_cancel(&priv->oneshot, ts); - priv->callback = NULL; - priv->arg = NULL; + flags = spin_lock_irqsave(&priv->lock); + ret = esp32s3_oneshot_cancel(&priv->oneshot, ts); spin_unlock_irqrestore(&priv->lock, flags); if (ret < 0) @@ -352,8 +331,6 @@ struct oneshot_lowerhalf_s *oneshot_initialize(int chan, uint16_t resolution) } priv->lh.ops = &g_esp32s3_timer_ops; /* Pointer to the LH operations */ - priv->callback = NULL; /* No callback yet */ - priv->arg = NULL; /* No arg yet */ priv->resolution = resolution; /* Configured resolution */ /* Initialize esp32s3_oneshot_s structure */ diff --git a/drivers/audio/tone.c b/drivers/audio/tone.c index 5465258d75ff5..93247fe57b354 100644 --- a/drivers/audio/tone.c +++ b/drivers/audio/tone.c @@ -390,7 +390,7 @@ static void next_note(FAR struct tone_upperhalf_s *upper) ts.tv_sec = (time_t) sec; ts.tv_nsec = (unsigned long)nsec; - ONESHOT_START(upper->oneshot, oneshot_callback, upper, &ts); + ONESHOT_START(upper->oneshot, &ts); g_silence_length = 0; return; @@ -524,7 +524,7 @@ static void next_note(FAR struct tone_upperhalf_s *upper) ts.tv_sec = (time_t) sec; ts.tv_nsec = (unsigned long)nsec; - ONESHOT_START(upper->oneshot, oneshot_callback, upper, &ts); + ONESHOT_START(upper->oneshot, &ts); return; /* Change tempo */ @@ -567,7 +567,7 @@ static void next_note(FAR struct tone_upperhalf_s *upper) ts.tv_sec = (time_t) sec; ts.tv_nsec = (unsigned long)nsec; - ONESHOT_START(upper->oneshot, oneshot_callback, upper, &ts); + ONESHOT_START(upper->oneshot, &ts); return; } @@ -651,7 +651,7 @@ static void next_note(FAR struct tone_upperhalf_s *upper) /* And arrange a callback when the note should stop */ - ONESHOT_START(upper->oneshot, oneshot_callback, upper, &ts); + ONESHOT_START(upper->oneshot, &ts); return; /* Tune looks bad (unexpected EOF, bad character, etc.) */ @@ -958,6 +958,9 @@ int tone_register(FAR const char *path, FAR struct pwm_lowerhalf_s *tone, upper->channel = (uint8_t)channel; #endif + upper->oneshot->callback = oneshot_callback; + upper->oneshot->arg = upper; + /* Register the PWM device */ audinfo("Registering %s\n", path); diff --git a/drivers/timers/arch_alarm.c b/drivers/timers/arch_alarm.c index fe37816c4f425..09adbdb45dae3 100644 --- a/drivers/timers/arch_alarm.c +++ b/drivers/timers/arch_alarm.c @@ -59,7 +59,7 @@ static void oneshot_callback(FAR struct oneshot_lowerhalf_s *lower, * atomically w. respect to a HW timer */ - ONESHOT_TICK_START(g_oneshot_lower, oneshot_callback, NULL, 1); + ONESHOT_TICK_START(g_oneshot_lower, 1); /* It is always an error if this progresses more than 1 tick at a time. * That would break any timer based on wdog; such timers might timeout @@ -90,12 +90,15 @@ void up_alarm_set_lowerhalf(FAR struct oneshot_lowerhalf_s *lower) g_oneshot_lower = lower; + lower->callback = oneshot_callback; + lower->arg = lower; + #ifdef CONFIG_SCHED_TICKLESS ONESHOT_TICK_MAX_DELAY(g_oneshot_lower, &ticks); g_oneshot_maxticks = ticks < UINT32_MAX ? ticks : UINT32_MAX; #else ONESHOT_TICK_CURRENT(g_oneshot_lower, &g_current_tick); - ONESHOT_TICK_START(g_oneshot_lower, oneshot_callback, NULL, 1); + ONESHOT_TICK_START(g_oneshot_lower, 1); #endif } @@ -269,8 +272,7 @@ int weak_function up_alarm_tick_start(clock_t ticks) delta = 0; } - ret = ONESHOT_TICK_START(g_oneshot_lower, oneshot_callback, - NULL, delta); + ret = ONESHOT_TICK_START(g_oneshot_lower, delta); } return ret; diff --git a/drivers/timers/goldfish_timer.c b/drivers/timers/goldfish_timer.c index 05b2a8575e714..5e8d1a2d74601 100644 --- a/drivers/timers/goldfish_timer.c +++ b/drivers/timers/goldfish_timer.c @@ -64,8 +64,6 @@ struct goldfish_timer_lowerhalf_s { struct oneshot_lowerhalf_s lh; /* Lower half operations */ - oneshot_callback_t callback; /* Current user interrupt callback */ - FAR void *arg; /* Argument passed to upper half callback */ uintptr_t base; /* Base address of registers */ spinlock_t lock; /* Lock for interrupt handling */ }; @@ -77,8 +75,6 @@ struct goldfish_timer_lowerhalf_s static int goldfish_timer_maxdelay(FAR struct oneshot_lowerhalf_s *lower, FAR struct timespec *ts); static int goldfish_timer_start(FAR struct oneshot_lowerhalf_s *lower, - FAR oneshot_callback_t callback, - FAR void *arg, FAR const struct timespec *ts); static int goldfish_timer_cancel(FAR struct oneshot_lowerhalf_s *lower, FAR struct timespec *ts); @@ -111,8 +107,6 @@ static int goldfish_timer_maxdelay(FAR struct oneshot_lowerhalf_s *lower_, } static int goldfish_timer_start(FAR struct oneshot_lowerhalf_s *lower_, - FAR oneshot_callback_t callback, - FAR void *arg, FAR const struct timespec *ts) { FAR struct goldfish_timer_lowerhalf_s *lower = @@ -126,9 +120,6 @@ static int goldfish_timer_start(FAR struct oneshot_lowerhalf_s *lower_, flags = spin_lock_irqsave(&lower->lock); - lower->callback = callback; - lower->arg = arg; - nsec = ts->tv_sec * 1000000000 + ts->tv_nsec; l32 = getreg32(lower->base + GOLDFISH_TIMER_TIME_LOW); h32 = getreg32(lower->base + GOLDFISH_TIMER_TIME_HIGH); @@ -154,9 +145,6 @@ static int goldfish_timer_cancel(FAR struct oneshot_lowerhalf_s *lower_, flags = spin_lock_irqsave(&lower->lock); - lower->callback = NULL; - lower->arg = NULL; - putreg32(0, lower->base + GOLDFISH_TIMER_IRQ_ENABLED); putreg32(1, lower->base + GOLDFISH_TIMER_CLEAR_ALARM); @@ -196,9 +184,7 @@ static int goldfish_timer_interrupt(int irq, FAR void *arg) { FAR struct goldfish_timer_lowerhalf_s *lower = arg; - oneshot_callback_t callback = NULL; irqstate_t flags; - void *cbarg; DEBUGASSERT(lower != NULL); @@ -207,22 +193,11 @@ static int goldfish_timer_interrupt(int irq, putreg32(1, lower->base + GOLDFISH_TIMER_CLEAR_ALARM); putreg32(1, lower->base + GOLDFISH_TIMER_CLEAR_INTERRUPT); - if (lower->callback != NULL) - { - callback = lower->callback; - cbarg = lower->arg; - lower->callback = NULL; - lower->arg = NULL; - } - spin_unlock_irqrestore(&lower->lock, flags); /* Then perform the callback */ - if (callback) - { - callback(&lower->lh, cbarg); - } + oneshot_process_callback(&lower->lh); return 0; } diff --git a/drivers/timers/oneshot.c b/drivers/timers/oneshot.c index 605baccf9f645..9c1204fa8c045 100644 --- a/drivers/timers/oneshot.c +++ b/drivers/timers/oneshot.c @@ -215,8 +215,7 @@ static int oneshot_ioctl(FAR struct file *filep, int cmd, unsigned long arg) /* Start the oneshot timer */ - ret = ONESHOT_START(priv->od_lower, oneshot_callback, priv, - &start->ts); + ret = ONESHOT_START(priv->od_lower, &start->ts); } break; @@ -311,6 +310,10 @@ int oneshot_register(FAR const char *devname, /* Initialize the new oneshot timer driver instance */ priv->od_lower = lower; + + lower->callback = oneshot_callback; + lower->arg = lower; + nxmutex_init(&priv->od_lock); /* And register the oneshot timer driver */ diff --git a/drivers/timers/watchdog.c b/drivers/timers/watchdog.c index 0f964805e6824..7ec6d33c43223 100644 --- a/drivers/timers/watchdog.c +++ b/drivers/timers/watchdog.c @@ -187,7 +187,7 @@ watchdog_automonitor_oneshot(FAR struct oneshot_lowerhalf_s *oneshot, }; lower->ops->keepalive(lower); - ONESHOT_START(oneshot, watchdog_automonitor_oneshot, upper, &ts); + ONESHOT_START(oneshot, &ts); } } #elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_TIMER) @@ -275,7 +275,7 @@ watchdog_automonitor_start(FAR struct watchdog_upperhalf_s *upper) }; upper->oneshot = oneshot; - ONESHOT_START(oneshot, watchdog_automonitor_oneshot, upper, &ts); + ONESHOT_START(oneshot, &ts); #elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_TIMER) upper->timer = timer; timer->ops->setcallback(timer, watchdog_automonitor_timer, upper); @@ -848,6 +848,8 @@ FAR void *watchdog_register(FAR const char *path, } #if defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_ONESHOT) + upper->oneshot->callback = watchdog_automonitor_oneshot; + upper->oneshot->arg = upper; watchdog_automonitor_start(upper, oneshot); #elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_TIMER) watchdog_automonitor_start(upper, timer); diff --git a/include/nuttx/timers/oneshot.h b/include/nuttx/timers/oneshot.h index a9d09d8ed167b..642bd138f202b 100644 --- a/include/nuttx/timers/oneshot.h +++ b/include/nuttx/timers/oneshot.h @@ -95,10 +95,8 @@ * ****************************************************************************/ -#define ONESHOT_MAX_DELAY(l,t) \ - ((l)->ops->max_delay ? (l)->ops->max_delay(l,t) : oneshot_max_delay(l,t)) -#define ONESHOT_TICK_MAX_DELAY(l,t) \ - ((l)->ops->tick_max_delay ? (l)->ops->tick_max_delay(l,t) : oneshot_tick_max_delay(l,t)) +#define ONESHOT_MAX_DELAY(l,t) (l)->ops->max_delay(l,t) +#define ONESHOT_TICK_MAX_DELAY(l,t) oneshot_tick_max_delay(l,t) /**************************************************************************** * Name: ONESHOT_START @@ -120,10 +118,8 @@ * ****************************************************************************/ -#define ONESHOT_START(l,h,a,t) \ - ((l)->ops->start ? (l)->ops->start(l,h,a,t) : oneshot_start(l,h,a,t)) -#define ONESHOT_TICK_START(l,h,a,t) \ - ((l)->ops->tick_start ? (l)->ops->tick_start(l,h,a,t) : oneshot_tick_start(l,h,a,t)) +#define ONESHOT_START(l,t) (l)->ops->start(l,t) +#define ONESHOT_TICK_START(l,t) oneshot_tick_start(l,t) /**************************************************************************** * Name: ONESHOT_CANCEL @@ -149,10 +145,8 @@ * ****************************************************************************/ -#define ONESHOT_CANCEL(l,t) \ - ((l)->ops->cancel ? (l)->ops->cancel(l,t) : oneshot_cancel(l,t)) -#define ONESHOT_TICK_CANCEL(l,t) \ - ((l)->ops->tick_cancel ? (l)->ops->tick_cancel(l,t) : oneshot_tick_cancel(l,t)) +#define ONESHOT_CANCEL(l,t) (l)->ops->cancel(l,t) +#define ONESHOT_TICK_CANCEL(l,t) oneshot_tick_cancel(l,t) /**************************************************************************** * Name: ONESHOT_CURRENT @@ -173,10 +167,8 @@ * ****************************************************************************/ -#define ONESHOT_CURRENT(l,t) \ - ((l)->ops->current ? (l)->ops->current(l,t) : oneshot_current(l,t)) -#define ONESHOT_TICK_CURRENT(l,t) \ - ((l)->ops->tick_current ? (l)->ops->tick_current(l,t) : oneshot_tick_current(l,t)) +#define ONESHOT_CURRENT(l,t) (l)->ops->current(l,t) +#define ONESHOT_TICK_CURRENT(l,t) oneshot_tick_current(l,t) /**************************************************************************** * Public Types @@ -202,21 +194,11 @@ struct oneshot_operations_s CODE int (*max_delay)(FAR struct oneshot_lowerhalf_s *lower, FAR struct timespec *ts); CODE int (*start)(FAR struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, FAR void *arg, FAR const struct timespec *ts); CODE int (*cancel)(FAR struct oneshot_lowerhalf_s *lower, FAR struct timespec *ts); CODE int (*current)(FAR struct oneshot_lowerhalf_s *lower, FAR struct timespec *ts); - CODE int (*tick_max_delay)(FAR struct oneshot_lowerhalf_s *lower, - FAR clock_t *ticks); - CODE int (*tick_start)(FAR struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, FAR void *arg, - clock_t ticks); - CODE int (*tick_cancel)(FAR struct oneshot_lowerhalf_s *lower, - FAR clock_t *ticks); - CODE int (*tick_current)(FAR struct oneshot_lowerhalf_s *lower, - FAR clock_t *ticks); }; /* This structure describes the state of the oneshot timer lower-half @@ -231,6 +213,9 @@ struct oneshot_lowerhalf_s FAR const struct oneshot_operations_s *ops; + FAR oneshot_callback_t callback; + FAR void *arg; + /* Private lower half data may follow */ }; @@ -259,78 +244,9 @@ extern "C" #endif /**************************************************************************** - * Public Function Prototypes + * Inline Functions ****************************************************************************/ -static inline -int oneshot_max_delay(FAR struct oneshot_lowerhalf_s *lower, - FAR struct timespec *ts) -{ - clock_t tick; - int ret; - - if (lower->ops->tick_max_delay == NULL) - { - return -ENOTSUP; - } - - ret = lower->ops->tick_max_delay(lower, &tick); - clock_ticks2time(ts, tick); - return ret; -} - -static inline -int oneshot_start(FAR struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, FAR void *arg, - FAR const struct timespec *ts) -{ - clock_t tick; - - if (lower->ops->tick_start == NULL) - { - return -ENOTSUP; - } - - tick = clock_time2ticks(ts); - return lower->ops->tick_start(lower, callback, arg, tick); -} - -static inline -int oneshot_cancel(FAR struct oneshot_lowerhalf_s *lower, - FAR struct timespec *ts) -{ - clock_t tick; - int ret; - - if (lower->ops->tick_cancel == NULL) - { - return -ENOTSUP; - } - - ret = lower->ops->tick_cancel(lower, &tick); - clock_ticks2time(ts, tick); - - return ret; -} - -static inline -int oneshot_current(FAR struct oneshot_lowerhalf_s *lower, - FAR struct timespec *ts) -{ - clock_t tick; - int ret; - - if (lower->ops->tick_current == NULL) - { - return -ENOTSUP; - } - - ret = lower->ops->tick_current(lower, &tick); - clock_ticks2time(ts, tick); - - return ret; -} - static inline int oneshot_tick_max_delay(FAR struct oneshot_lowerhalf_s *lower, FAR clock_t *ticks) @@ -350,7 +266,6 @@ int oneshot_tick_max_delay(FAR struct oneshot_lowerhalf_s *lower, static inline int oneshot_tick_start(FAR struct oneshot_lowerhalf_s *lower, - oneshot_callback_t callback, FAR void *arg, clock_t ticks) { struct timespec ts; @@ -361,7 +276,7 @@ int oneshot_tick_start(FAR struct oneshot_lowerhalf_s *lower, } clock_ticks2time(&ts, ticks); - return lower->ops->start(lower, callback, arg, &ts); + return lower->ops->start(lower, &ts); } static inline @@ -400,6 +315,19 @@ int oneshot_tick_current(FAR struct oneshot_lowerhalf_s *lower, return ret; } +static inline_function +void oneshot_process_callback(FAR struct oneshot_lowerhalf_s *lower) +{ + if (lower->callback) + { + lower->callback(lower, lower->arg); + } +} + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + /**************************************************************************** * Name: oneshot_initialize * diff --git a/sched/sched/sched_cpuload_oneshot.c b/sched/sched/sched_cpuload_oneshot.c index cb87fa97a8e46..608a14b74bd2a 100644 --- a/sched/sched/sched_cpuload_oneshot.c +++ b/sched/sched/sched_cpuload_oneshot.c @@ -199,8 +199,7 @@ static void nxsched_oneshot_start(void) ts.tv_sec = secs; ts.tv_nsec = 1000 * usecs; - DEBUGVERIFY(ONESHOT_START(g_sched_oneshot.oneshot, - nxsched_oneshot_callback, NULL, &ts)); + DEBUGVERIFY(ONESHOT_START(g_sched_oneshot.oneshot, &ts)); } /**************************************************************************** @@ -328,6 +327,8 @@ void nxsched_oneshot_extclk(FAR struct oneshot_lowerhalf_s *lower) /* Then start the oneshot */ g_sched_oneshot.oneshot = lower; + lower->callback = nxsched_oneshot_callback; + lower->arg = NULL; nxsched_oneshot_start(); } #endif