Skip to content

Commit

Permalink
drivers/timers/watchdog: add watchdog timer notifier chain
Browse files Browse the repository at this point in the history
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

Change-Id: I8dcfb2e25354aa5699b976934e9c6503c2a82e55
Signed-off-by: yaojiaqi <[email protected]>
  • Loading branch information
Jiaqi-YP7 committed Dec 31, 2024
1 parent c55c251 commit a1abcd3
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 0 deletions.
4 changes: 4 additions & 0 deletions drivers/timers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ if(CONFIG_WATCHDOG)
list(APPEND SRCS watchdog.c)
endif()

if(CONFIG_WATCHDOG_TIMEOUT_NOTIFIER)
list(APPEND SRCS watchdog_notifier.c)
endif()

if(CONFIG_TIMER)
list(APPEND SRCS timer.c)
endif()
Expand Down
8 changes: 8 additions & 0 deletions drivers/timers/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,14 @@ menuconfig WATCHDOG_AUTOMONITOR

if WATCHDOG_AUTOMONITOR

config WATCHDOG_TIMEOUT_NOTIFIER
bool "Enable watchdog timeout notifier"
default n
---help---
The watchdog timeout notifier chain mechanism supports users registering
custom callback functions, which will be called when the watchdog timer
managed by Auto-monitor times out.

choice
prompt "Auto-monitor keepalive by"
default WATCHDOG_AUTOMONITOR_BY_WDOG
Expand Down
6 changes: 6 additions & 0 deletions drivers/timers/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ ifeq ($(CONFIG_WATCHDOG),y)
TMRVPATH = :timers
endif

ifeq ($(CONFIG_WATCHDOG_TIMEOUT_NOTIFIER),y)
CSRCS += watchdog_notifier.c
TMRDEPPATH = --dep-path timers
TMRVPATH = :timers
endif

ifeq ($(CONFIG_TIMER),y)
CSRCS += timer.c
TMRDEPPATH = --dep-path timers
Expand Down
29 changes: 29 additions & 0 deletions drivers/timers/watchdog.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@
# endif
#endif

#if defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_ONESHOT)
#define WATCHDOG_NOTIFIER_ACTION WATCHDOG_KEEPALIVE_BY_ONESHOT
#elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_TIMER)
#define WATCHDOG_NOTIFIER_ACTION WATCHDOG_KEEPALIVE_BY_TIMER
#elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_WDOG)
#define WATCHDOG_NOTIFIER_ACTION WATCHDOG_KEEPALIVE_BY_WDOG
#elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_WORKER)
#define WATCHDOG_NOTIFIER_ACTION WATCHDOG_KEEPALIVE_BY_WORKER
#elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_CAPTURE)
#define WATCHDOG_NOTIFIER_ACTION WATCHDOG_KEEPALIVE_BY_CAPTURE
#elif defined(CONFIG_WATCHDOG_AUTOMONITOR_BY_IDLE)
#define WATCHDOG_NOTIFIER_ACTION WATCHDOG_KEEPALIVE_BY_IDLE
#endif
/****************************************************************************
* Private Type Definitions
****************************************************************************/
Expand Down Expand Up @@ -699,6 +712,22 @@ static int wdog_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: watchdog_automonitor_timeout
*
* Description:
* This function can be called in the watchdog timeout interrupt handler.
* If so, callbacks on the watchdog timer notify chain are called when the
* watchdog timer times out.
*
****************************************************************************/
#ifdef CONFIG_WATCHDOG_TIMEOUT_NOTIFIER
void watchdog_automonitor_timeout(void)
{
watchdog_notifier_call_chain(WATCHDOG_NOTIFIER_ACTION, NULL);
}
#endif /* CONFIG_WATCHDOG_TIMEOUT_NOTIFIER */

/****************************************************************************
* Name: watchdog_register
*
Expand Down
89 changes: 89 additions & 0 deletions drivers/timers/watchdog_notifier.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/****************************************************************************
* drivers/timers/watchdog_notifier.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/arch.h>
#include <nuttx/notifier.h>

#include <sys/types.h>

/****************************************************************************
* Private Data
****************************************************************************/

static ATOMIC_NOTIFIER_HEAD(g_watchdog_notifier_list);

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: watchdog_notifier_chain_register
*
* Description:
* Add notifier to the watchdog notifier chain
*
* Input Parameters:
* nb - New entry in notifier chain
*
****************************************************************************/

void watchdog_notifier_chain_register(FAR struct notifier_block *nb)
{
atomic_notifier_chain_register(&g_watchdog_notifier_list, nb);
}

/****************************************************************************
* Name: watchdog_notifier_chain_unregister
*
* Description:
* Remove notifier from the watchdog notifier chain
*
* Input Parameters:
* nb - Entry to remove from notifier chain
*
****************************************************************************/

void watchdog_notifier_chain_unregister(FAR struct notifier_block *nb)
{
atomic_notifier_chain_unregister(&g_watchdog_notifier_list, nb);
}

/****************************************************************************
* Name: watchdog_notifier_call_chain
*
* Description:
* Call functions in the watchdog notifier chain.
*
* Input Parameters:
* action - Value passed unmodified to notifier function
* data - Pointer passed unmodified to notifier function
*
****************************************************************************/

void watchdog_notifier_call_chain(unsigned long action, FAR void *data)
{
atomic_notifier_call_chain(&g_watchdog_notifier_list, action, data);
}
86 changes: 86 additions & 0 deletions include/nuttx/timers/watchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <nuttx/compiler.h>
#include <nuttx/irq.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/notifier.h>
#include <nuttx/sched.h>

#ifdef CONFIG_WATCHDOG

Expand Down Expand Up @@ -88,6 +90,35 @@
#define WDFLAGS_CAPTURE (1 << 2) /* 1=Call the user function when the
* watchdog timer expires */

/* Keepalive Actions ********************************************************/

/* According to the keepalive action specified by the Auto-monitor, callback
* functions registered on the watchdog notifier chain may take corresponding
* actions.
*
* These are detected and handled by the "upper half" watchdog timer driver.
*
* WATCHDOG_KEEPALIVE_BY_ONESHOT - The watchdog timer is keepalive by
* oneshot timer.
* WATCHDOG_KEEPALIVE_BY_TIMER - The watchdog timer is keepalive by
* timer.
* WATCHDOG_KEEPALIVE_BY_WDOG - The watchdog timer is keepalive by
* wdog.
* WATCHDOG_KEEPALIVE_BY_WORKER - The watchdog timer is keepalive by
* worker queue.
* WATCHDOG_KEEPALIVE_BY_CAPTURE - The watchdog timer is keepalive by
* capture.
* WATCHDOG_KEEPALIVE_BY_IDLE - The watchdog timer is keepalive by
* idle task.
*/

#define WATCHDOG_KEEPALIVE_BY_ONESHOT 0
#define WATCHDOG_KEEPALIVE_BY_TIMER 1
#define WATCHDOG_KEEPALIVE_BY_WDOG 2
#define WATCHDOG_KEEPALIVE_BY_WORKER 3
#define WATCHDOG_KEEPALIVE_BY_CAPTURE 4
#define WATCHDOG_KEEPALIVE_BY_IDLE 5

/****************************************************************************
* Public Types
****************************************************************************/
Expand Down Expand Up @@ -197,6 +228,61 @@ extern "C"
#define EXTERN extern
#endif

#ifdef CONFIG_WATCHDOG_TIMEOUT_NOTIFIER
/****************************************************************************
* Name: watchdog_notifier_chain_register
*
* Description:
* Add notifier to the watchdog notifier chain
*
* Input Parameters:
* nb - New entry in notifier chain
*
****************************************************************************/

void watchdog_notifier_chain_register(FAR struct notifier_block *nb);

/****************************************************************************
* Name: watchdog_notifier_chain_unregister
*
* Description:
* Remove notifier from the watchdog notifier chain
*
* Input Parameters:
* nb - Entry to remove from notifier chain
*
****************************************************************************/

void watchdog_notifier_chain_unregister(FAR struct notifier_block *nb);

/****************************************************************************
* Name: watchdog_notifier_call_chain
*
* Description:
* Call functions in the watchdog notifier chain.
*
* Input Parameters:
* action - Value passed unmodified to notifier function
* data - Pointer passed unmodified to notifier function
*
****************************************************************************/

void watchdog_notifier_call_chain(unsigned long action, FAR void *data);

/****************************************************************************
* Name: watchdog_automonitor_timeout
*
* Description:
* This function can be called in the watchdog timeout interrupt handler.
* If so, callbacks on the watchdog timer notify chain are called when the
* watchdog timer times out.
*
****************************************************************************/

void watchdog_automonitor_timeout(void);

#endif /* CONFIG_WATCHDOG_TIMEOUT_NOTIFIER */

/****************************************************************************
* Name: watchdog_register
*
Expand Down

0 comments on commit a1abcd3

Please sign in to comment.