Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 17 23:23:45 2019 +0000
Revision:
0:5b88d5760320
Child:
1:9db0e321a9f4
mbed-os5 only for TYBLE16

Who changed what in which revision?

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