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 Synchronization level: Interrupt safe
64 */
65class Watchdog : private NonCopyable<Watchdog> {
66public:
67
68 /** Get a reference to the single Watchdog instance in the system.
69 *
70 * @return A reference to the single Watchdog instance present in the system.
71 */
73 {
74 // Use this implementation of singleton (Meyer's) rather than the one that allocates
75 // the instance on the heap because it ensures destruction at program end (preventing warnings
76 // from memory checking tools, such as valgrind).
77 static Watchdog instance;
78 return instance;
79 }
80
81 /** Start the Watchdog timer with the maximum timeout value available for
82 * the target.
83 *
84 * @note The timeout is set to a value returned by Watchdog::get_max_timeout.
85 *
86 * If the Watchdog timer is already running, this function does nothing.
87 *
88 * @return true if the Watchdog timer was started successfully;
89 * false if the Watchdog timer was not started or if the Watchdog
90 * timer is already running.
91 */
92 bool start();
93
94 /** Start the Watchdog timer.
95 *
96 * @note Asserts that the timeout param is supported by the target
97 * (0 < timeout <= Watchdog::get_max_timeout).
98 *
99 * @param timeout Watchdog timeout in milliseconds.
100 *
101 * @return true if the Watchdog timer was started successfully;
102 * false if Watchdog timer was not started or if setting
103 * a new watchdog timeout was not possible.
104 */
105 bool start(uint32_t timeout);
106
107 /** Start the Watchdog timer.
108 *
109 * @note Asserts that the timeout param is supported by the target
110 * (0 < timeout <= Watchdog::get_max_timeout).
111 *
112 * @param timeout Watchdog timeout in chrono milliseconds.
113 *
114 * @return true if the Watchdog timer was started successfully;
115 * false if Watchdog timer was not started or if setting
116 * a new watchdog timeout was not possible.
117 */
118 bool start(std::chrono::milliseconds timeout)
119 {
120 return start(timeout.count());
121 }
122
123 /** Stop the Watchdog timer.
124 *
125 * Calling this function disables a running Watchdog
126 * peripheral if the platform supports it.
127 *
128 * @return true if the Watchdog timer was successfully stopped;
129 * false if the Watchdog timer cannot be disabled on this platform
130 * or if the Watchdog timer has not been started.
131 */
132 bool stop();
133
134 /** Get the Watchdog timer refresh value.
135 *
136 * This function returns the refresh timeout of the watchdog peripheral.
137 *
138 * @return Reload value for the Watchdog timer in milliseconds.
139 */
140 uint32_t get_timeout() const;
141
142 /** Get the maximum Watchdog refresh value for this platform.
143 *
144 * @return Maximum reload value supported by the Watchdog timer for this
145 * platform in milliseconds.
146 */
147 uint32_t get_max_timeout() const;
148
149 /** Check if the Watchdog timer is already running.
150 *
151 * @return true if the Watchdog timer is running and
152 * false otherwise.
153 */
154 bool is_running() const;
155
156 /** Refresh the Watchdog timer.
157 *
158 * Call this function periodically before the Watchdog times out.
159 * Otherwise, the system resets.
160 *
161 * If the Watchdog timer is not running, this function does nothing.
162 */
163 void kick();
164
165private:
166 Watchdog();
167 ~Watchdog();
168
169 bool _running;
170};
171
172/** @}*/
173
174} // namespace mbed
175
176#endif // DEVICE_WATCHDOG
177#endif // MBED_WATCHDOG_H
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
A hardware watchdog timer that resets the system in the case of system failures or malfunctions.
Definition: Watchdog.h:65
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:72
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:118