2019-06-26 08:33:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Arm Limited and affiliates.
|
2018-11-19 16:55:27 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-11-28 14:00:56 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MBED_WATCHDOG_H
|
|
|
|
#define MBED_WATCHDOG_H
|
|
|
|
|
|
|
|
#ifdef DEVICE_WATCHDOG
|
|
|
|
|
2019-06-28 10:06:06 +00:00
|
|
|
#include "platform/mbed_error.h"
|
|
|
|
#include "platform/mbed_assert.h"
|
2019-06-27 09:55:24 +00:00
|
|
|
#include "platform/mbed_critical.h"
|
2019-06-28 10:06:06 +00:00
|
|
|
#include "hal/watchdog_api.h"
|
2019-06-27 10:03:20 +00:00
|
|
|
#include "platform/NonCopyable.h"
|
2019-06-27 09:55:24 +00:00
|
|
|
#include <cstdio>
|
2019-03-13 11:30:11 +00:00
|
|
|
|
2019-01-25 11:41:23 +00:00
|
|
|
namespace mbed {
|
2019-01-03 12:33:26 +00:00
|
|
|
|
2017-11-28 14:00:56 +00:00
|
|
|
/** \addtogroup drivers */
|
2019-07-03 22:43:51 +00:00
|
|
|
/** A hardware watchdog timer that resets the system in the case of system
|
|
|
|
* failures or malfunctions. If you fail to refresh the Watchdog timer periodically,
|
|
|
|
* it resets the system after a set period of time.
|
2019-07-03 13:28:37 +00:00
|
|
|
*
|
2019-07-04 08:49:24 +00:00
|
|
|
* There is only one instance of the Watchdog class in the system, which directly maps to the hardware.
|
2019-07-03 22:43:51 +00:00
|
|
|
* Use Watchdog::get_instance to obtain a reference.
|
2019-07-03 13:28:37 +00:00
|
|
|
*
|
|
|
|
* Watchdog::start initializes a system timer with a time period specified in
|
|
|
|
* @a timeout param. This timer counts down and triggers a system reset
|
2019-07-03 22:43:51 +00:00
|
|
|
* when it wraps. To prevent the system reset, you must periodically kick or refresh
|
|
|
|
* the timer by calling Watchdog::kick, which resets the countdown
|
|
|
|
* to the initial value.
|
2017-11-28 14:00:56 +00:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* @code
|
2019-06-28 08:52:23 +00:00
|
|
|
* Watchdog &watchdog = Watchdog::get_instance();
|
2019-07-03 13:28:37 +00:00
|
|
|
* watchdog.start(500);
|
2017-11-28 14:00:56 +00:00
|
|
|
*
|
|
|
|
* while (true) {
|
2019-06-28 12:39:05 +00:00
|
|
|
// kick watchdog regularly within provided timeout
|
2019-06-28 11:54:21 +00:00
|
|
|
watchdog.kick();
|
|
|
|
// Application code
|
2017-11-28 14:00:56 +00:00
|
|
|
* }
|
|
|
|
* @endcode
|
2019-06-28 08:52:23 +00:00
|
|
|
*
|
|
|
|
* @note Synchronization level: Interrupt safe
|
2017-12-04 11:38:24 +00:00
|
|
|
* @ingroup drivers
|
2017-11-28 14:00:56 +00:00
|
|
|
*/
|
2019-06-27 09:55:24 +00:00
|
|
|
class Watchdog : private NonCopyable<Watchdog> {
|
2017-11-28 14:00:56 +00:00
|
|
|
public:
|
2019-03-13 11:30:11 +00:00
|
|
|
|
2019-07-03 13:28:37 +00:00
|
|
|
/** Get a reference to the single Watchdog instance in the system.
|
|
|
|
*
|
|
|
|
* @return A reference to the single Watchdog instance present in the system.
|
|
|
|
*/
|
2019-06-27 13:10:35 +00:00
|
|
|
static Watchdog &get_instance()
|
|
|
|
{
|
|
|
|
// Use this implementation of singleton (Meyer's) rather than the one that allocates
|
|
|
|
// the instance on the heap because it ensures destruction at program end (preventing warnings
|
|
|
|
// from memory checking tools, such as valgrind).
|
|
|
|
static Watchdog instance;
|
|
|
|
return instance;
|
|
|
|
}
|
2019-01-03 12:33:26 +00:00
|
|
|
|
2019-07-03 13:28:37 +00:00
|
|
|
/** Start the Watchdog timer with the maximum timeout value available for
|
|
|
|
* the target.
|
|
|
|
*
|
|
|
|
* @note The timeout is set to a value returned by Watchdog::get_max_timeout.
|
2019-07-02 09:13:03 +00:00
|
|
|
*
|
2019-07-03 22:43:51 +00:00
|
|
|
* If the Watchdog timer is already running, this function does nothing.
|
2019-07-02 09:13:03 +00:00
|
|
|
*
|
2019-07-03 22:43:51 +00:00
|
|
|
* @return true if the Watchdog timer was started successfully;
|
|
|
|
* false if the Watchdog timer was not started or if the Watchdog
|
|
|
|
* timer is already running.
|
2019-07-02 09:13:03 +00:00
|
|
|
*/
|
|
|
|
bool start();
|
|
|
|
|
2019-07-03 13:28:37 +00:00
|
|
|
/** Start the Watchdog timer.
|
2019-06-27 09:55:24 +00:00
|
|
|
*
|
2019-07-03 13:28:37 +00:00
|
|
|
* @note Asset that the timeout param is supported by the target
|
|
|
|
* (0 < timeout <= Watchdog::get_max_timeout).
|
Add SW Watchdog
-SW watchdog has interface name start(),stop(),kick() Sw watchdog internally has static list and shared across multiple instance of SW watchdog
- Sw watchdog initialize timeout value,unique string via constructor whenever threads created sw watchdog object
-Threads make sure pass proper timeout value,Unique string while creating the instance.
-start() called by components(BLE,WIFI etc.,),it adds the entry into static list with few details current count ,etc.,
-kick() called by registered components(BLE,WIFI etc.) to reset current count to zero.
-is_alive - interface API to mbed_watchdog_manager
-implementation optimization
2019-01-17 11:57:56 +00:00
|
|
|
*
|
2019-07-03 13:28:37 +00:00
|
|
|
* @param timeout Watchdog timeout in milliseconds.
|
|
|
|
*
|
2019-07-03 22:43:51 +00:00
|
|
|
* @return true if the Watchdog timer was started successfully;
|
|
|
|
* false if Watchdog timer was not started or if the Watchdog
|
|
|
|
* timer is already running.
|
Add SW Watchdog
-SW watchdog has interface name start(),stop(),kick() Sw watchdog internally has static list and shared across multiple instance of SW watchdog
- Sw watchdog initialize timeout value,unique string via constructor whenever threads created sw watchdog object
-Threads make sure pass proper timeout value,Unique string while creating the instance.
-start() called by components(BLE,WIFI etc.,),it adds the entry into static list with few details current count ,etc.,
-kick() called by registered components(BLE,WIFI etc.) to reset current count to zero.
-is_alive - interface API to mbed_watchdog_manager
-implementation optimization
2019-01-17 11:57:56 +00:00
|
|
|
*/
|
2019-07-02 09:13:03 +00:00
|
|
|
bool start(uint32_t timeout);
|
2019-01-03 12:33:26 +00:00
|
|
|
|
2019-07-03 13:28:37 +00:00
|
|
|
/** Stop the Watchdog timer.
|
2017-11-28 14:00:56 +00:00
|
|
|
*
|
2019-07-03 22:43:51 +00:00
|
|
|
* Calling this function disables a running Watchdog
|
|
|
|
* peripheral if the platform supports it.
|
2017-11-28 14:00:56 +00:00
|
|
|
*
|
2019-07-03 22:43:51 +00:00
|
|
|
* @return true if the Watchdog timer was successfully stopped;
|
|
|
|
* false if the Watchdog timer cannot be disabled on this platform
|
|
|
|
* or if the Watchdog timer has not been started.
|
2017-11-28 14:00:56 +00:00
|
|
|
*/
|
2019-06-27 09:55:24 +00:00
|
|
|
bool stop();
|
2017-11-28 14:00:56 +00:00
|
|
|
|
2019-07-03 13:28:37 +00:00
|
|
|
/** Get the Watchdog timer refresh value.
|
2017-11-28 14:00:56 +00:00
|
|
|
*
|
2019-07-03 13:28:37 +00:00
|
|
|
* This function returns the refresh timeout of the watchdog peripheral.
|
2017-11-28 14:00:56 +00:00
|
|
|
*
|
2019-07-03 13:28:37 +00:00
|
|
|
* @return Reload value for the Watchdog timer in milliseconds.
|
2017-11-28 14:00:56 +00:00
|
|
|
*/
|
2019-06-27 09:55:24 +00:00
|
|
|
uint32_t get_timeout() const;
|
2017-11-28 14:00:56 +00:00
|
|
|
|
2019-07-03 13:28:37 +00:00
|
|
|
/** Get the maximum Watchdog refresh value for this platform.
|
2019-01-25 11:41:23 +00:00
|
|
|
*
|
2019-07-03 13:28:37 +00:00
|
|
|
* @return Maximum reload value supported by the Watchdog timer for this
|
|
|
|
* platform in milliseconds.
|
2017-11-28 14:00:56 +00:00
|
|
|
*/
|
2019-06-27 09:55:24 +00:00
|
|
|
uint32_t get_max_timeout() const;
|
2019-01-03 12:33:26 +00:00
|
|
|
|
2019-07-03 22:43:51 +00:00
|
|
|
/** Check if the Watchdog timer is already running.
|
2019-06-27 09:55:24 +00:00
|
|
|
*
|
2019-07-03 22:43:51 +00:00
|
|
|
* @return true if the Watchdog timer is running and
|
|
|
|
* false otherwise.
|
2019-06-27 09:55:24 +00:00
|
|
|
*/
|
|
|
|
bool is_running() const;
|
2019-01-03 12:33:26 +00:00
|
|
|
|
2019-07-03 13:28:37 +00:00
|
|
|
/** Refresh the Watchdog timer.
|
|
|
|
*
|
|
|
|
* Call this function periodically before the Watchdog times out.
|
2019-07-03 22:43:51 +00:00
|
|
|
* Otherwise, the system resets.
|
2019-06-28 08:52:23 +00:00
|
|
|
*
|
2019-07-03 22:43:51 +00:00
|
|
|
* If the Watchdog timer is not running, this function does nothing.
|
2019-06-28 08:52:23 +00:00
|
|
|
*/
|
2019-06-27 16:11:30 +00:00
|
|
|
void kick();
|
2019-06-27 17:11:38 +00:00
|
|
|
|
2019-01-03 12:33:26 +00:00
|
|
|
private:
|
2019-06-27 13:10:35 +00:00
|
|
|
Watchdog();
|
|
|
|
~Watchdog();
|
|
|
|
|
2019-06-27 14:46:06 +00:00
|
|
|
bool _running;
|
2017-11-28 14:00:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mbed
|
|
|
|
|
|
|
|
#endif // DEVICE_WATCHDOG
|
|
|
|
#endif // MBED_WATCHDOG_H
|