Mbed OS Reference
Loading...
Searching...
No Matches
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#include <chrono>
30
31namespace mbed {
32/**
33 * \defgroup drivers_Watchdog Watchdog class
34 * \ingroup drivers-public-api
35 * @{
36 */
37
38/** A hardware watchdog timer that resets the system in the case of system
39 * failures or malfunctions. If you fail to refresh the Watchdog timer periodically,
40 * it resets the system after a set period of time.
41 *
42 * There is only one instance of the Watchdog class in the system, which directly maps to the hardware.
43 * Use Watchdog::get_instance to obtain a reference.
44 *
45 * Watchdog::start initializes a system timer with a time period specified in
46 * @a timeout param. This timer counts down and triggers a system reset
47 * when it wraps. To prevent the system reset, you must periodically kick or refresh
48 * the timer by calling Watchdog::kick, which resets the countdown
49 * to the initial value.
50 *
51 * Example:
52 * @code
53 * Watchdog &watchdog = Watchdog::get_instance();
54 * watchdog.start(500);
55 *
56 * while (true) {
57 // kick watchdog regularly within provided timeout
58 watchdog.kick();
59 // Application code
60 * }
61 * @endcode
62 *
63 * @note The watchdog is still able to count down and time out when the device is in sleep and deep sleep mode.
64 * However, on some devices (Kinetis MCUs), if the watchdog times out during deep sleep mode, the timer will reset
65 * and count up to the timeout value again before resetting the system. This means that if your MCU may be
66 * in deep sleep mode when the timer times out, allow for up to double the configured watchdog time before timeout.
67 *
68 * @note Synchronization level: Interrupt safe
69 */
70class Watchdog : private NonCopyable<Watchdog> {
71public:
72
73 /** Get a reference to the single Watchdog instance in the system.
74 *
75 * @return A reference to the single Watchdog instance present in the system.
76 */
78 {
79 // Use this implementation of singleton (Meyer's) rather than the one that allocates
80 // the instance on the heap because it ensures destruction at program end (preventing warnings
81 // from memory checking tools, such as valgrind).
82 static Watchdog instance;
83 return instance;
84 }
85
86 /** Start the Watchdog timer with the maximum timeout value available for
87 * the target.
88 *
89 * @note The timeout is set to a value returned by Watchdog::get_max_timeout.
90 *
91 * If the Watchdog timer is already running, this function does nothing.
92 *
93 * @return true if the Watchdog timer was started successfully;
94 * false if the Watchdog timer was not started or if the Watchdog
95 * timer is already running.
96 */
97 bool start();
98
99 /** Start the Watchdog timer.
100 *
101 * @note Asserts that the timeout param is supported by the target
102 * (0 < timeout <= Watchdog::get_max_timeout).
103 *
104 * @param timeout Watchdog timeout in milliseconds.
105 *
106 * @return true if the Watchdog timer was started successfully;
107 * false if Watchdog timer was not started or if setting
108 * a new watchdog timeout was not possible.
109 */
110 bool start(uint32_t timeout);
111
112 /** Start the Watchdog timer.
113 *
114 * @note Asserts that the timeout param is supported by the target
115 * (0 < timeout <= Watchdog::get_max_timeout).
116 *
117 * @param timeout Watchdog timeout in chrono milliseconds.
118 *
119 * @return true if the Watchdog timer was started successfully;
120 * false if Watchdog timer was not started or if setting
121 * a new watchdog timeout was not possible.
122 */
123 bool start(std::chrono::milliseconds timeout)
124 {
125 return start(timeout.count());
126 }
127
128 /** Stop the Watchdog timer.
129 *
130 * Calling this function disables a running Watchdog
131 * peripheral if the platform supports it.
132 *
133 * @return true if the Watchdog timer was successfully stopped;
134 * false if the Watchdog timer cannot be disabled on this platform
135 * or if the Watchdog timer has not been started.
136 */
137 bool stop();
138
139 /** Get the Watchdog timer refresh value.
140 *
141 * This function returns the refresh timeout of the watchdog peripheral.
142 *
143 * @return Reload value for the Watchdog timer in milliseconds.
144 */
145 uint32_t get_timeout() const;
146
147 /** Get the maximum Watchdog refresh value for this platform.
148 *
149 * @return Maximum reload value supported by the Watchdog timer for this
150 * platform in milliseconds.
151 */
152 uint32_t get_max_timeout() const;
153
154 /** Check if the Watchdog timer is already running.
155 *
156 * @return true if the Watchdog timer is running and
157 * false otherwise.
158 */
159 bool is_running() const;
160
161 /** Refresh the Watchdog timer.
162 *
163 * Call this function periodically before the Watchdog times out.
164 * Otherwise, the system resets.
165 *
166 * If the Watchdog timer is not running, this function does nothing.
167 */
168 void kick();
169
170private:
171 Watchdog();
172 ~Watchdog();
173
174 bool _running;
175};
176
177/** @}*/
178
179} // namespace mbed
180
181#endif // DEVICE_WATCHDOG
182#endif // MBED_WATCHDOG_H
Prevents generation of copy constructor and copy assignment operator in derived classes.
A hardware watchdog timer that resets the system in the case of system failures or malfunctions.
Definition Watchdog.h:70
bool start(uint32_t timeout)
Start the Watchdog timer.
bool is_running() const
Check if the Watchdog timer is already running.
uint32_t get_max_timeout() const
Get the maximum Watchdog refresh value for this platform.
bool stop()
Stop the Watchdog timer.
void kick()
Refresh the Watchdog timer.
static Watchdog & get_instance()
Get a reference to the single Watchdog instance in the system.
Definition Watchdog.h:77
bool start()
Start the Watchdog timer with the maximum timeout value available for the target.
uint32_t get_timeout() const
Get the Watchdog timer refresh value.
bool start(std::chrono::milliseconds timeout)
Start the Watchdog timer.
Definition Watchdog.h:123