Skip to content

Commit f1607b9

Browse files
Jiaqi-YP7xiaoxiang781216
authored andcommitted
drivers/timers/watchdog: add watchdog timer notifier chain
Add support for watchdog timer notifer chain so that users can customize the callback function when the watchdog timer times out which enabled by Auto-monitor Signed-off-by: yaojiaqi <[email protected]>
1 parent 91c71ed commit f1607b9

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

drivers/timers/Kconfig

+8
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,14 @@ menuconfig WATCHDOG_AUTOMONITOR
439439

440440
if WATCHDOG_AUTOMONITOR
441441

442+
config WATCHDOG_TIMEOUT_NOTIFIER
443+
bool "Enable watchdog timeout notifier"
444+
default n
445+
---help---
446+
The watchdog timeout notifier chain mechanism supports users registering
447+
custom callback functions, which will be called when the watchdog timer
448+
managed by Auto-monitor times out.
449+
442450
choice
443451
prompt "Auto-monitor keepalive by"
444452
default WATCHDOG_AUTOMONITOR_BY_WDOG

drivers/timers/watchdog.c

+67
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@
7171
# endif
7272
#endif
7373

74+
#if defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_ONESHOT)
75+
# define WATCHDOG_NOTIFIER_ACTION WATCHDOG_KEEPALIVE_BY_ONESHOT
76+
#elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_TIMER)
77+
# define WATCHDOG_NOTIFIER_ACTION WATCHDOG_KEEPALIVE_BY_TIMER
78+
#elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_WDOG)
79+
# define WATCHDOG_NOTIFIER_ACTION WATCHDOG_KEEPALIVE_BY_WDOG
80+
#elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_WORKER)
81+
# define WATCHDOG_NOTIFIER_ACTION WATCHDOG_KEEPALIVE_BY_WORKER
82+
#elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_CAPTURE)
83+
# define WATCHDOG_NOTIFIER_ACTION WATCHDOG_KEEPALIVE_BY_CAPTURE
84+
#elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_IDLE)
85+
# define WATCHDOG_NOTIFIER_ACTION WATCHDOG_KEEPALIVE_BY_IDLE
86+
#endif
87+
7488
/****************************************************************************
7589
* Private Type Definitions
7690
****************************************************************************/
@@ -135,6 +149,10 @@ static const struct file_operations g_wdogops =
135149
wdog_ioctl, /* ioctl */
136150
};
137151

152+
#ifdef CONFIG_WATCHDOG_TIMEOUT_NOTIFIER
153+
static ATOMIC_NOTIFIER_HEAD(g_watchdog_notifier_list);
154+
#endif
155+
138156
/****************************************************************************
139157
* Private Functions
140158
****************************************************************************/
@@ -699,6 +717,55 @@ static int wdog_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
699717
* Public Functions
700718
****************************************************************************/
701719

720+
#ifdef CONFIG_WATCHDOG_TIMEOUT_NOTIFIER
721+
/****************************************************************************
722+
* Name: watchdog_notifier_chain_register
723+
*
724+
* Description:
725+
* Add notifier to the watchdog notifier chain
726+
*
727+
* Input Parameters:
728+
* nb - New entry in notifier chain
729+
*
730+
****************************************************************************/
731+
732+
void watchdog_notifier_chain_register(FAR struct notifier_block *nb)
733+
{
734+
atomic_notifier_chain_register(&g_watchdog_notifier_list, nb);
735+
}
736+
737+
/****************************************************************************
738+
* Name: watchdog_notifier_chain_unregister
739+
*
740+
* Description:
741+
* Remove notifier from the watchdog notifier chain
742+
*
743+
* Input Parameters:
744+
* nb - Entry to remove from notifier chain
745+
*
746+
****************************************************************************/
747+
748+
void watchdog_notifier_chain_unregister(FAR struct notifier_block *nb)
749+
{
750+
atomic_notifier_chain_unregister(&g_watchdog_notifier_list, nb);
751+
}
752+
753+
/****************************************************************************
754+
* Name: watchdog_automonitor_timeout
755+
*
756+
* Description:
757+
* This function can be called in the watchdog timeout interrupt handler.
758+
* If so, callbacks on the watchdog timer notify chain are called when the
759+
* watchdog timer times out.
760+
*
761+
****************************************************************************/
762+
763+
void watchdog_automonitor_timeout(void)
764+
{
765+
atomic_notifier_call_chain(&g_watchdog_notifier_list, action, data);
766+
}
767+
#endif /* CONFIG_WATCHDOG_TIMEOUT_NOTIFIER */
768+
702769
/****************************************************************************
703770
* Name: watchdog_register
704771
*

include/nuttx/timers/watchdog.h

+71
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <nuttx/compiler.h>
3232
#include <nuttx/irq.h>
3333
#include <nuttx/fs/ioctl.h>
34+
#include <nuttx/notifier.h>
3435

3536
#ifdef CONFIG_WATCHDOG
3637

@@ -88,6 +89,35 @@
8889
#define WDFLAGS_CAPTURE (1 << 2) /* 1=Call the user function when the
8990
* watchdog timer expires */
9091

92+
/* Keepalive Actions ********************************************************/
93+
94+
/* According to the keepalive action specified by the Auto-monitor, callback
95+
* functions registered on the watchdog notifier chain may take corresponding
96+
* actions.
97+
*
98+
* These are detected and handled by the "upper half" watchdog timer driver.
99+
*
100+
* WATCHDOG_KEEPALIVE_BY_ONESHOT - The watchdog timer is keepalive by
101+
* oneshot timer.
102+
* WATCHDOG_KEEPALIVE_BY_TIMER - The watchdog timer is keepalive by
103+
* timer.
104+
* WATCHDOG_KEEPALIVE_BY_WDOG - The watchdog timer is keepalive by
105+
* wdog.
106+
* WATCHDOG_KEEPALIVE_BY_WORKER - The watchdog timer is keepalive by
107+
* worker queue.
108+
* WATCHDOG_KEEPALIVE_BY_CAPTURE - The watchdog timer is keepalive by
109+
* capture.
110+
* WATCHDOG_KEEPALIVE_BY_IDLE - The watchdog timer is keepalive by
111+
* idle task.
112+
*/
113+
114+
#define WATCHDOG_KEEPALIVE_BY_ONESHOT 0
115+
#define WATCHDOG_KEEPALIVE_BY_TIMER 1
116+
#define WATCHDOG_KEEPALIVE_BY_WDOG 2
117+
#define WATCHDOG_KEEPALIVE_BY_WORKER 3
118+
#define WATCHDOG_KEEPALIVE_BY_CAPTURE 4
119+
#define WATCHDOG_KEEPALIVE_BY_IDLE 5
120+
91121
/****************************************************************************
92122
* Public Types
93123
****************************************************************************/
@@ -197,6 +227,47 @@ extern "C"
197227
#define EXTERN extern
198228
#endif
199229

230+
#ifdef CONFIG_WATCHDOG_TIMEOUT_NOTIFIER
231+
/****************************************************************************
232+
* Name: watchdog_notifier_chain_register
233+
*
234+
* Description:
235+
* Add notifier to the watchdog notifier chain
236+
*
237+
* Input Parameters:
238+
* nb - New entry in notifier chain
239+
*
240+
****************************************************************************/
241+
242+
void watchdog_notifier_chain_register(FAR struct notifier_block *nb);
243+
244+
/****************************************************************************
245+
* Name: watchdog_notifier_chain_unregister
246+
*
247+
* Description:
248+
* Remove notifier from the watchdog notifier chain
249+
*
250+
* Input Parameters:
251+
* nb - Entry to remove from notifier chain
252+
*
253+
****************************************************************************/
254+
255+
void watchdog_notifier_chain_unregister(FAR struct notifier_block *nb);
256+
257+
/****************************************************************************
258+
* Name: watchdog_automonitor_timeout
259+
*
260+
* Description:
261+
* This function can be called in the watchdog timeout interrupt handler.
262+
* If so, callbacks on the watchdog timer notify chain are called when the
263+
* watchdog timer times out.
264+
*
265+
****************************************************************************/
266+
267+
void watchdog_automonitor_timeout(void);
268+
269+
#endif /* CONFIG_WATCHDOG_TIMEOUT_NOTIFIER */
270+
200271
/****************************************************************************
201272
* Name: watchdog_register
202273
*

0 commit comments

Comments
 (0)