Mistake on this page?
Report an issue in GitHub or email us
Watchdog.h
1 /*
2  * Copyright (c) 2018 Arm Limited and affiliates.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef MBED_WATCHDOG_H
19 #define MBED_WATCHDOG_H
20 
21 #ifdef DEVICE_WATCHDOG
22 
23 #include "platform/mbed_error.h"
24 #include "platform/mbed_assert.h"
25 #include "platform/mbed_critical.h"
26 #include "hal/watchdog_api.h"
27 #include "platform/NonCopyable.h"
28 #include <cstdio>
29 
30 namespace mbed {
31 /**
32  * \defgroup drivers_Watchdog Watchdog class
33  * \ingroup drivers-public-api
34  * @{
35  */
36 
37 /** A hardware watchdog timer that resets the system in the case of system
38  * failures or malfunctions. If you fail to refresh the Watchdog timer periodically,
39  * it resets the system after a set period of time.
40  *
41  * There is only one instance of the Watchdog class in the system, which directly maps to the hardware.
42  * Use Watchdog::get_instance to obtain a reference.
43  *
44  * Watchdog::start initializes a system timer with a time period specified in
45  * @a timeout param. This timer counts down and triggers a system reset
46  * when it wraps. To prevent the system reset, you must periodically kick or refresh
47  * the timer by calling Watchdog::kick, which resets the countdown
48  * to the initial value.
49  *
50  * Example:
51  * @code
52  * Watchdog &watchdog = Watchdog::get_instance();
53  * watchdog.start(500);
54  *
55  * while (true) {
56  // kick watchdog regularly within provided timeout
57  watchdog.kick();
58  // Application code
59  * }
60  * @endcode
61  *
62  * @note Synchronization level: Interrupt safe
63  */
64 class Watchdog : private NonCopyable<Watchdog> {
65 public:
66 
67  /** Get a reference to the single Watchdog instance in the system.
68  *
69  * @return A reference to the single Watchdog instance present in the system.
70  */
72  {
73  // Use this implementation of singleton (Meyer's) rather than the one that allocates
74  // the instance on the heap because it ensures destruction at program end (preventing warnings
75  // from memory checking tools, such as valgrind).
76  static Watchdog instance;
77  return instance;
78  }
79 
80  /** Start the Watchdog timer with the maximum timeout value available for
81  * the target.
82  *
83  * @note The timeout is set to a value returned by Watchdog::get_max_timeout.
84  *
85  * If the Watchdog timer is already running, this function does nothing.
86  *
87  * @return true if the Watchdog timer was started successfully;
88  * false if the Watchdog timer was not started or if the Watchdog
89  * timer is already running.
90  */
91  bool start();
92 
93  /** Start the Watchdog timer.
94  *
95  * @note Asset that the timeout param is supported by the target
96  * (0 < timeout <= Watchdog::get_max_timeout).
97  *
98  * @param timeout Watchdog timeout in milliseconds.
99  *
100  * @return true if the Watchdog timer was started successfully;
101  * false if Watchdog timer was not started or if setting
102  * a new watchdog timeout was not possible.
103  */
104  bool start(uint32_t timeout);
105 
106  /** Stop the Watchdog timer.
107  *
108  * Calling this function disables a running Watchdog
109  * peripheral if the platform supports it.
110  *
111  * @return true if the Watchdog timer was successfully stopped;
112  * false if the Watchdog timer cannot be disabled on this platform
113  * or if the Watchdog timer has not been started.
114  */
115  bool stop();
116 
117  /** Get the Watchdog timer refresh value.
118  *
119  * This function returns the refresh timeout of the watchdog peripheral.
120  *
121  * @return Reload value for the Watchdog timer in milliseconds.
122  */
123  uint32_t get_timeout() const;
124 
125  /** Get the maximum Watchdog refresh value for this platform.
126  *
127  * @return Maximum reload value supported by the Watchdog timer for this
128  * platform in milliseconds.
129  */
130  uint32_t get_max_timeout() const;
131 
132  /** Check if the Watchdog timer is already running.
133  *
134  * @return true if the Watchdog timer is running and
135  * false otherwise.
136  */
137  bool is_running() const;
138 
139  /** Refresh the Watchdog timer.
140  *
141  * Call this function periodically before the Watchdog times out.
142  * Otherwise, the system resets.
143  *
144  * If the Watchdog timer is not running, this function does nothing.
145  */
146  void kick();
147 
148 private:
149  Watchdog();
150  ~Watchdog();
151 
152  bool _running;
153 };
154 
155 /** @}*/
156 
157 } // namespace mbed
158 
159 #endif // DEVICE_WATCHDOG
160 #endif // MBED_WATCHDOG_H
bool start()
Start the Watchdog timer with the maximum timeout value available for the target. ...
static Watchdog & get_instance()
Get a reference to the single Watchdog instance in the system.
Definition: Watchdog.h:71
bool is_running() const
Check if the Watchdog timer is already running.
A hardware watchdog timer that resets the system in the case of system failures or malfunctions...
Definition: Watchdog.h:64
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
uint32_t get_timeout() const
Get the Watchdog timer refresh value.
void kick()
Refresh the Watchdog timer.
uint32_t get_max_timeout() const
Get the maximum Watchdog refresh value for this platform.
bool stop()
Stop the Watchdog timer.
Definition: ATHandler.h:46
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.