]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1_FreedomStudio/freedom-metal/metal/watchdog.h
Update RISCC-V-RV32-SiFive_HiFive1_FreedomStudio project to latest tools and metal...
[freertos] / FreeRTOS / Demo / RISC-V_RV32_SiFive_HiFive1_FreedomStudio / freedom-metal / metal / watchdog.h
1 /* Copyright 2019 SiFive, Inc */
2 /* SPDX-License-Identifier: Apache-2.0 */
3
4 #ifndef METAL__WATCHDOG_H
5 #define METAL__WATCHDOG_H
6
7 /*!
8  * @file watchdog.h
9  *
10  * @brief API for configuring watchdog timers
11  */
12
13 #include <metal/interrupt.h>
14
15 struct metal_watchdog;
16
17 /*!
18  * @brief List of watchdog timer count behaviors
19  */
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 */
24 };
25
26 /*!
27  * @brief List of behaviors when a watchdog triggers
28  */
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 */
33 };
34
35
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);
49 };
50
51 /*!
52  * @brief Handle for a Watchdog Timer
53  */
54 struct metal_watchdog {
55   const struct metal_watchdog_vtable *vtable;
56 };
57
58 /*!
59  * @brief Feed the watchdog timer
60  */
61 inline int metal_watchdog_feed(const struct metal_watchdog *const wdog)
62 {
63   return wdog->vtable->feed(wdog);
64 }
65
66 /*!
67  * @brief Get the rate of the watchdog timer in Hz
68  *
69  * @return the rate of the watchdog timer
70  */
71 inline long int metal_watchdog_get_rate(const struct metal_watchdog *const wdog)
72 {
73   return wdog->vtable->get_rate(wdog);
74 }
75
76 /*!
77  * @brief Set the rate of the watchdog timer in Hz
78  *
79  * There is no guarantee that the new rate will match the requested rate.
80  *
81  * @return the new rate of the watchdog timer
82  */
83 inline long int metal_watchdog_set_rate(const struct metal_watchdog *const wdog, const long int rate)
84 {
85   return wdog->vtable->set_rate(wdog, rate);
86 }
87
88 /*!
89  * @brief Get the timeout of the watchdog timer
90  *
91  * @return the watchdog timeout value
92  */
93 inline long int metal_watchdog_get_timeout(const struct metal_watchdog *const wdog)
94 {
95   return wdog->vtable->get_timeout(wdog);
96 }
97
98 /*!
99  * @brief Set the timeout of the watchdog timer
100  *
101  * The set rate will be the minimimum of the requested and maximum supported rates.
102  *
103  * @return the new watchdog timeout value
104  */
105 inline long int metal_watchdog_set_timeout(const struct metal_watchdog *const wdog, const long int timeout)
106 {
107   return wdog->vtable->set_timeout(wdog, timeout);
108 }
109
110 /*!
111  * @brief Sets the result behavior of a watchdog timer timeout
112  *
113  * @return 0 if the requested result behavior is supported
114  */
115 inline int metal_watchdog_set_result(const struct metal_watchdog *const wdog,
116                                      const enum metal_watchdog_result result)
117 {
118   return wdog->vtable->set_result(wdog, result);
119 }
120
121 /*!
122  * @brief Set the run behavior of the watchdog
123  *
124  * Used to enable/disable the watchdog timer
125  *
126  * @return 0 if the watchdog was successfully started/stopped
127  */
128 inline int metal_watchdog_run(const struct metal_watchdog *const wdog,
129                               const enum metal_watchdog_run_option option)
130 {
131   return wdog->vtable->run(wdog, option);
132 }
133
134 /*!
135  * @brief Get the interrupt controller for the watchdog interrupt
136  */
137 inline struct metal_interrupt *metal_watchdog_get_interrupt(const struct metal_watchdog *const wdog)
138 {
139   return wdog->vtable->get_interrupt(wdog);
140 }
141
142 /*!
143  * @Brief Get the interrupt id for the watchdog interrupt
144  */
145 inline int metal_watchdog_get_interrupt_id(const struct metal_watchdog *const wdog)
146 {
147   return wdog->vtable->get_interrupt_id(wdog);
148 }
149
150 /*!
151  * @brief Clear the watchdog interrupt
152  */
153 inline int metal_watchdog_clear_interrupt(const struct metal_watchdog *const wdog)
154 {
155   return wdog->vtable->clear_interrupt(wdog);
156 }
157
158 /*!
159  * @brief Get a watchdog handle
160  */
161 struct metal_watchdog *metal_watchdog_get_device(const int index);
162
163 #endif /* METAL__WATCHDOG_H */