Compare commits

...

3 commits

Author SHA1 Message Date
João Victor
d1018cd1ed
Merge b9f353e31e into 43797ea6cc 2025-01-12 11:17:07 +08:00
yaojiaqi
43797ea6cc 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 <yaojiaqi@lixiang.com>
2025-01-12 11:15:42 +08:00
joaoVLAndrade
b9f353e31e [PATCH] stm32 timers: add set/get prescaler operations to ll_ops.
add get period operation to ll_ops.
2025-01-09 17:23:07 -03:00
5 changed files with 226 additions and 32 deletions

View file

@ -333,6 +333,10 @@ static int stm32_tim_setmode(struct stm32_tim_dev_s *dev,
stm32_tim_mode_t mode);
static int stm32_tim_setclock(struct stm32_tim_dev_s *dev,
uint32_t freq);
static uint32_t stm32_tim_getprescaler(struct stm32_tim_dev_s *dev);
static void stm32_tim_setprescaler(struct stm32_tim_dev_s *dev,
uint32_t prescaler);
static uint32_t stm32_tim_getperiod(struct stm32_tim_dev_s *dev);
static void stm32_tim_setperiod(struct stm32_tim_dev_s *dev,
uint32_t period);
static uint32_t stm32_tim_getcounter(struct stm32_tim_dev_s *dev);
@ -360,22 +364,25 @@ static int stm32_tim_checkint(struct stm32_tim_dev_s *dev, int source);
static const struct stm32_tim_ops_s stm32_tim_ops =
{
.enable = stm32_tim_enable,
.disable = stm32_tim_disable,
.setmode = stm32_tim_setmode,
.setclock = stm32_tim_setclock,
.setperiod = stm32_tim_setperiod,
.getcounter = stm32_tim_getcounter,
.setcounter = stm32_tim_setcounter,
.getwidth = stm32_tim_getwidth,
.setchannel = stm32_tim_setchannel,
.setcompare = stm32_tim_setcompare,
.getcapture = stm32_tim_getcapture,
.setisr = stm32_tim_setisr,
.enableint = stm32_tim_enableint,
.disableint = stm32_tim_disableint,
.ackint = stm32_tim_ackint,
.checkint = stm32_tim_checkint,
.enable = stm32_tim_enable,
.disable = stm32_tim_disable,
.setmode = stm32_tim_setmode,
.setclock = stm32_tim_setclock,
.setprescaler = stm32_tim_setprescaler,
.getprescaler = stm32_tim_getprescaler,
.getperiod = stm32_tim_getperiod,
.setperiod = stm32_tim_setperiod,
.getcounter = stm32_tim_getcounter,
.setcounter = stm32_tim_setcounter,
.getwidth = stm32_tim_getwidth,
.setchannel = stm32_tim_setchannel,
.setcompare = stm32_tim_setcompare,
.getcapture = stm32_tim_getcapture,
.setisr = stm32_tim_setisr,
.enableint = stm32_tim_enableint,
.disableint = stm32_tim_disableint,
.ackint = stm32_tim_ackint,
.checkint = stm32_tim_checkint,
};
#ifdef CONFIG_STM32_TIM1
@ -899,6 +906,41 @@ static int stm32_tim_setclock(struct stm32_tim_dev_s *dev, uint32_t freq)
return prescaler;
}
/****************************************************************************
* Name: stm32_tim_getprescaler
****************************************************************************/
static uint32_t stm32_tim_getprescaler(struct stm32_tim_dev_s *dev)
{
DEBUGASSERT(dev != NULL);
return stm32_tim_getwidth(dev) > 16 ?
stm32_getreg32(dev, STM32_GTIM_PSC_OFFSET) :
(uint32_t)stm32_getreg16(dev, STM32_GTIM_PSC_OFFSET);
}
/****************************************************************************
* Name: stm32_tim_setprescaler
****************************************************************************/
static void stm32_tim_setprescaler(struct stm32_tim_dev_s *dev,
uint32_t prescaler)
{
DEBUGASSERT(dev != NULL);
stm32_putreg32(dev, STM32_GTIM_PSC_OFFSET, prescaler);
}
/****************************************************************************
* Name: stm32_tim_getperiod
****************************************************************************/
static uint32_t stm32_tim_getperiod(struct stm32_tim_dev_s *dev)
{
DEBUGASSERT(dev != NULL);
return stm32_tim_getwidth(dev) > 16 ?
stm32_getreg32(dev, STM32_GTIM_ARR_OFFSET) :
(uint32_t)stm32_getreg16(dev, STM32_GTIM_ARR_OFFSET);
}
/****************************************************************************
* Name: stm32_tim_setperiod
****************************************************************************/

View file

@ -40,22 +40,25 @@
/* Helpers ******************************************************************/
#define STM32_TIM_SETMODE(d,mode) ((d)->ops->setmode(d,mode))
#define STM32_TIM_SETCLOCK(d,freq) ((d)->ops->setclock(d,freq))
#define STM32_TIM_SETPERIOD(d,period) ((d)->ops->setperiod(d,period))
#define STM32_TIM_GETCOUNTER(d) ((d)->ops->getcounter(d))
#define STM32_TIM_SETCOUNTER(d,c) ((d)->ops->setcounter(d,c))
#define STM32_TIM_GETWIDTH(d) ((d)->ops->getwidth(d))
#define STM32_TIM_SETCHANNEL(d,ch,mode) ((d)->ops->setchannel(d,ch,mode))
#define STM32_TIM_SETCOMPARE(d,ch,comp) ((d)->ops->setcompare(d,ch,comp))
#define STM32_TIM_GETCAPTURE(d,ch) ((d)->ops->getcapture(d,ch))
#define STM32_TIM_SETISR(d,hnd,arg,s) ((d)->ops->setisr(d,hnd,arg,s))
#define STM32_TIM_ENABLEINT(d,s) ((d)->ops->enableint(d,s))
#define STM32_TIM_DISABLEINT(d,s) ((d)->ops->disableint(d,s))
#define STM32_TIM_ACKINT(d,s) ((d)->ops->ackint(d,s))
#define STM32_TIM_CHECKINT(d,s) ((d)->ops->checkint(d,s))
#define STM32_TIM_ENABLE(d) ((d)->ops->enable(d))
#define STM32_TIM_DISABLE(d) ((d)->ops->disable(d))
#define STM32_TIM_SETMODE(d,mode) ((d)->ops->setmode(d,mode))
#define STM32_TIM_SETCLOCK(d,freq) ((d)->ops->setclock(d,freq))
#define STM32_TIM_GETPRESCALER(d) ((d)->ops->getprescaler(d))
#define STM32_TIM_SETPRESCALER(d,p) ((d)->ops->setprescaler(d,p))
#define STM32_TIM_GETPERIOD(d) ((d)->ops->getperiod(d))
#define STM32_TIM_SETPERIOD(d,p) ((d)->ops->setperiod(d,p))
#define STM32_TIM_GETCOUNTER(d) ((d)->ops->getcounter(d))
#define STM32_TIM_SETCOUNTER(d,c) ((d)->ops->setcounter(d,c))
#define STM32_TIM_GETWIDTH(d) ((d)->ops->getwidth(d))
#define STM32_TIM_SETCHANNEL(d,ch,mode) ((d)->ops->setchannel(d,ch,mode))
#define STM32_TIM_SETCOMPARE(d,ch,comp) ((d)->ops->setcompare(d,ch,comp))
#define STM32_TIM_GETCAPTURE(d,ch) ((d)->ops->getcapture(d,ch))
#define STM32_TIM_SETISR(d,hnd,arg,s) ((d)->ops->setisr(d,hnd,arg,s))
#define STM32_TIM_ENABLEINT(d,s) ((d)->ops->enableint(d,s))
#define STM32_TIM_DISABLEINT(d,s) ((d)->ops->disableint(d,s))
#define STM32_TIM_ACKINT(d,s) ((d)->ops->ackint(d,s))
#define STM32_TIM_CHECKINT(d,s) ((d)->ops->checkint(d,s))
#define STM32_TIM_ENABLE(d) ((d)->ops->enable(d))
#define STM32_TIM_DISABLE(d) ((d)->ops->disable(d))
/****************************************************************************
* Public Types
@ -159,6 +162,9 @@ struct stm32_tim_ops_s
void (*disable)(struct stm32_tim_dev_s *dev);
int (*setmode)(struct stm32_tim_dev_s *dev, stm32_tim_mode_t mode);
int (*setclock)(struct stm32_tim_dev_s *dev, uint32_t freq);
uint32_t (*getprescaler)(struct stm32_tim_dev_s *dev);
void (*setprescaler)(struct stm32_tim_dev_s *dev, uint32_t prescaler);
uint32_t (*getperiod)(struct stm32_tim_dev_s *dev);
void (*setperiod)(struct stm32_tim_dev_s *dev, uint32_t period);
uint32_t (*getcounter)(struct stm32_tim_dev_s *dev);
void (*setcounter)(struct stm32_tim_dev_s *dev, uint32_t count);

View file

@ -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

View file

@ -71,6 +71,20 @@
# 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
****************************************************************************/
@ -135,6 +149,10 @@ static const struct file_operations g_wdogops =
wdog_ioctl, /* ioctl */
};
#ifdef CONFIG_WATCHDOG_TIMEOUT_NOTIFIER
static ATOMIC_NOTIFIER_HEAD(g_watchdog_notifier_list);
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
@ -699,6 +717,55 @@ static int wdog_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
* Public Functions
****************************************************************************/
#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)
{
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_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)
{
atomic_notifier_call_chain(&g_watchdog_notifier_list, action, data);
}
#endif /* CONFIG_WATCHDOG_TIMEOUT_NOTIFIER */
/****************************************************************************
* Name: watchdog_register
*

View file

@ -31,6 +31,7 @@
#include <nuttx/compiler.h>
#include <nuttx/irq.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/notifier.h>
#ifdef CONFIG_WATCHDOG
@ -88,6 +89,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
****************************************************************************/
@ -197,6 +227,47 @@ 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_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
*