1 /* Copyright 2019 SiFive, Inc */
2 /* SPDX-License-Identifier: Apache-2.0 */
4 #ifndef METAL__WATCHDOG_H
5 #define METAL__WATCHDOG_H
10 * @brief API for configuring watchdog timers
13 #include <metal/interrupt.h>
15 struct metal_watchdog;
18 * @brief List of watchdog timer count behaviors
20 enum metal_watchdog_run_option {
21 METAL_WATCHDOG_STOP = 0, /*!< Stop the watchdog */
22 METAL_WATCHDOG_RUN_ALWAYS, /*!< Run the watchdog continuously, even during sleep */
23 METAL_WATCHDOG_RUN_AWAKE, /*!< Run the watchdog only while the CPU is awake */
27 * @brief List of behaviors when a watchdog triggers
29 enum metal_watchdog_result {
30 METAL_WATCHDOG_NO_RESULT = 0, /*!< When the watchdog triggers, do nothing */
31 METAL_WATCHDOG_INTERRUPT, /*!< When the watchdog triggers, fire an interrupt */
32 METAL_WATCHDOG_FULL_RESET, /*!< When the watchdog triggers, cause a full system reset */
36 struct metal_watchdog_vtable {
37 int (*feed)(const struct metal_watchdog *const wdog);
38 long int (*get_rate)(const struct metal_watchdog *const wdog);
39 long int (*set_rate)(const struct metal_watchdog *const wdog, const long int rate);
40 long int (*get_timeout)(const struct metal_watchdog *const wdog);
41 long int (*set_timeout)(const struct metal_watchdog *const wdog, const long int timeout);
42 int (*set_result)(const struct metal_watchdog *const wdog,
43 const enum metal_watchdog_result result);
44 int (*run)(const struct metal_watchdog *const wdog,
45 const enum metal_watchdog_run_option option);
46 struct metal_interrupt *(*get_interrupt)(const struct metal_watchdog *const wdog);
47 int (*get_interrupt_id)(const struct metal_watchdog *const wdog);
48 int (*clear_interrupt)(const struct metal_watchdog *const wdog);
52 * @brief Handle for a Watchdog Timer
54 struct metal_watchdog {
55 const struct metal_watchdog_vtable *vtable;
59 * @brief Feed the watchdog timer
61 inline int metal_watchdog_feed(const struct metal_watchdog *const wdog)
63 return wdog->vtable->feed(wdog);
67 * @brief Get the rate of the watchdog timer in Hz
69 * @return the rate of the watchdog timer
71 inline long int metal_watchdog_get_rate(const struct metal_watchdog *const wdog)
73 return wdog->vtable->get_rate(wdog);
77 * @brief Set the rate of the watchdog timer in Hz
79 * There is no guarantee that the new rate will match the requested rate.
81 * @return the new rate of the watchdog timer
83 inline long int metal_watchdog_set_rate(const struct metal_watchdog *const wdog, const long int rate)
85 return wdog->vtable->set_rate(wdog, rate);
89 * @brief Get the timeout of the watchdog timer
91 * @return the watchdog timeout value
93 inline long int metal_watchdog_get_timeout(const struct metal_watchdog *const wdog)
95 return wdog->vtable->get_timeout(wdog);
99 * @brief Set the timeout of the watchdog timer
101 * The set rate will be the minimimum of the requested and maximum supported rates.
103 * @return the new watchdog timeout value
105 inline long int metal_watchdog_set_timeout(const struct metal_watchdog *const wdog, const long int timeout)
107 return wdog->vtable->set_timeout(wdog, timeout);
111 * @brief Sets the result behavior of a watchdog timer timeout
113 * @return 0 if the requested result behavior is supported
115 inline int metal_watchdog_set_result(const struct metal_watchdog *const wdog,
116 const enum metal_watchdog_result result)
118 return wdog->vtable->set_result(wdog, result);
122 * @brief Set the run behavior of the watchdog
124 * Used to enable/disable the watchdog timer
126 * @return 0 if the watchdog was successfully started/stopped
128 inline int metal_watchdog_run(const struct metal_watchdog *const wdog,
129 const enum metal_watchdog_run_option option)
131 return wdog->vtable->run(wdog, option);
135 * @brief Get the interrupt controller for the watchdog interrupt
137 inline struct metal_interrupt *metal_watchdog_get_interrupt(const struct metal_watchdog *const wdog)
139 return wdog->vtable->get_interrupt(wdog);
143 * @Brief Get the interrupt id for the watchdog interrupt
145 inline int metal_watchdog_get_interrupt_id(const struct metal_watchdog *const wdog)
147 return wdog->vtable->get_interrupt_id(wdog);
151 * @brief Clear the watchdog interrupt
153 inline int metal_watchdog_clear_interrupt(const struct metal_watchdog *const wdog)
155 return wdog->vtable->clear_interrupt(wdog);
159 * @brief Get a watchdog handle
161 struct metal_watchdog *metal_watchdog_get_device(const int index);
163 #endif /* METAL__WATCHDOG_H */