sched/wdog: support for periodic wdog.

This commit added support for periodic wdog.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit is contained in:
ouyangxiangzhen 2024-11-21 16:14:35 +08:00 • committed by Alan C. Assis
parent e28c0b4ef1
commit 51399a76d9
2 changed files with 126 additions and 1 deletions

View file

@ -31,7 +31,6 @@
#include <nuttx/compiler.h>
#include <nuttx/clock.h>
#include <nuttx/irq.h>
#include <stdint.h>
/****************************************************************************
@ -90,6 +89,13 @@ struct wdog_s
clock_t expired; /* Timer associated with the absoulute time */
};
struct wdog_period_s
{
struct wdog_s wdog; /* Watchdog */
clock_t period; /* Period time in ticks */
wdentry_t func; /* Wrapped function to execute when delay expires */
};
/****************************************************************************
* Pubic Function Prototypes
****************************************************************************/
@ -279,6 +285,34 @@ static inline int wd_start_realtime(FAR struct wdog_s *wdog,
#endif
}
/****************************************************************************
* Name: wd_start_period
*
* Description:
* This function periodically adds a watchdog timer to the active timer.
*
* Input Parameters:
* wdog - Pointer of the periodic watchdog.
* delay - Delayed time in system ticks.
* period - Period in system ticks.
* wdentry - Function to call on timeout.
* arg - Parameter to pass to wdentry.
*
* NOTE: The parameter must be of type wdparm_t.
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is return to
* indicate the nature of any failure.
*
* Assumptions:
* The watchdog routine runs in the context of the timer interrupt handler
* and is subject to all ISR restrictions.
*
****************************************************************************/
int wd_start_period(FAR struct wdog_period_s *wdog, sclock_t delay,
clock_t period, wdentry_t wdentry, wdparm_t arg);
/****************************************************************************
* Name: wd_cancel
*
@ -297,6 +331,26 @@ static inline int wd_start_realtime(FAR struct wdog_s *wdog,
int wd_cancel(FAR struct wdog_s *wdog);
/****************************************************************************
* Name: wd_cancel_period
*
* Description:
* This function cancels a currently running periodic watchdog timer.
*
* Input Parameters:
* wdog_period - Pointer of the periodic watchdog.
*
* Returned Value:
* Zero (OK) is returned on success; A negated errno value is returned to
* indicate the nature of any failure.
*
****************************************************************************/
static inline int wd_cancel_period(FAR struct wdog_period_s *wdog_period)
{
return wd_cancel(&wdog_period->wdog);
}
/****************************************************************************
* Name: wd_gettime
*

View file

@ -94,6 +94,35 @@ static unsigned int g_wdtimernested;
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: wdentry_period
*
* Description:
* Periodic watchdog timer callback function.
*
* Input Parameters:
* arg - The argument of the wdog callback.
*
* Returned Value:
* None
*
****************************************************************************/
static void wdentry_period(wdparm_t arg)
{
FAR struct wdog_period_s *wdperiod = (FAR struct wdog_period_s *)arg;
wdperiod->func(wdperiod->wdog.arg);
/* Since we set `ticks++` at `wd_start_abstick`,
* we need to use `expired - 1` here to avoid time drift.
*/
wd_start_abstick(&wdperiod->wdog,
wdperiod->wdog.expired + wdperiod->period - 1,
wdentry_period, wdperiod->wdog.arg);
}
/****************************************************************************
* Name: wd_expiration
*
@ -151,6 +180,9 @@ static inline_function void wd_expiration(clock_t ticks)
arg = wdog->arg;
wdog->func = NULL;
arg = func != wdentry_period ? wdog->arg :
(wdparm_t)list_container_of(wdog, struct wdog_period_s, wdog);
/* Execute the watchdog function */
up_setpicbase(wdog->picbase);
@ -396,6 +428,45 @@ int wd_start(FAR struct wdog_s *wdog, sclock_t delay,
wdentry, arg);
}
/****************************************************************************
* Name: wd_start_period
*
* Description:
* This function periodically adds a watchdog timer to the active timer.
*
* Input Parameters:
* wdog - Pointer of the periodic watchdog.
* delay - Delayed time in system ticks.
* period - Period in system ticks.
* wdentry - Function to call on timeout.
* arg - Parameter to pass to wdentry.
*
* NOTE: The parameter must be of type wdparm_t.
*
* Returned Value:
* Zero (OK) is returned on success; a negated errno value is return to
* indicate the nature of any failure.
*
* Assumptions:
* The watchdog routine runs in the context of the timer interrupt handler
* and is subject to all ISR restrictions.
*
****************************************************************************/
int wd_start_period(FAR struct wdog_period_s *wdog, sclock_t delay,
clock_t period, wdentry_t wdentry, wdparm_t arg)
{
if (!wdog || !period || !wdentry)
{
return -EINVAL;
}
wdog->func = wdentry;
wdog->period = period;
return wd_start(&wdog->wdog, delay, wdentry_period, arg);
}
/****************************************************************************
* Name: wd_timer
*