Watchdog: use get_instance to access watchdog hw

pull/11023/head
Martin Kojtal 2019-06-27 14:10:35 +01:00
parent ce51d0da87
commit 6ab79c93d4
4 changed files with 23 additions and 49 deletions

View File

@ -17,11 +17,11 @@
#ifdef DEVICE_VIRTUAL_WATCHDOG
#include "VirtualWatchdog.h"
#include "Watchdog.h"
namespace mbed {
VirtualWatchdog *VirtualWatchdog::_first = NULL;
Watchdog VirtualWatchdog::*_watchdog = NULL;
VirtualWatchdog::VirtualWatchdog(uint32_t timeout, const char *const str): _name(str)
{
@ -29,23 +29,16 @@ VirtualWatchdog::VirtualWatchdog(uint32_t timeout, const char *const str): _name
_is_initialized = false;
_next = NULL;
_max_timeout = timeout;
// initialize watchdog, just once (driver Watchdog takes care of only one instance there)
if (_watchdog == NULL) {
_watchdog = new Watchdog();
_watchdog.start();
}
// start watchdog
Watchdog& watchdog = Watchdog::get_instance();
watchdog.start();
}
VirtualWatchdog::~VirtualWatchdog()
{
if (_is_initialized) {
stop();
// if we are the last VirtualWatchdog in the system, make sure to turn off hw watchdog
if (_first == NULL) {
_watchdog.stop();
delete _watchdog;
_watchdog = NULL;
}
// we do not need to stop hw watchdog, it's ticking by itself
}
}

View File

@ -26,8 +26,6 @@
#include "platform/mbed_power_mgmt.h"
#include "mbed_assert.h"
class Watchdog;
namespace mbed {
/** \addtogroup drivers */
@ -116,7 +114,6 @@ private:
bool _is_initialized; //To control start and stop functionality
static VirtualWatchdog *_first; //List to store the user/threads who called start
VirtualWatchdog *_next;
static Watchdog *_watchdog;
};
} // namespace mbed

View File

@ -36,30 +36,6 @@ namespace mbed {
bool Watchdog::_running = false;
static const uint32_t elapsed_ms = MBED_CONF_TARGET_WATCHDOG_TIMEOUT / 2;
Watchdog::Watchdog()
{
core_util_critical_section_enter();
if (_running) {
// error function does not return thus exit critical section and print an error
core_util_critical_section_exit();
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_OUT_OF_RESOURCES), "There's already watchdog in the system");
}
core_util_critical_section_exit();
}
Watchdog::~Watchdog()
{
core_util_critical_section_enter();
if (_running) {
if (stop()) {
// some targets might not support stop, thus keep running true in that case
_running = false;
}
}
core_util_critical_section_exit();
}
bool Watchdog::start()
{
watchdog_status_t sts;
@ -121,12 +97,12 @@ bool Watchdog::is_running() const
return _running;
}
uint32_t Watchdog::get_timeout()
uint32_t Watchdog::get_timeout() const
{
return hal_watchdog_get_reload_value();
}
uint32_t Watchdog::get_max_timeout()
uint32_t Watchdog::get_max_timeout() const
{
const watchdog_features_t features = hal_watchdog_get_platform_features();
return features.max_timeout;

View File

@ -33,12 +33,12 @@ namespace mbed {
/** \addtogroup drivers */
/** Hardware system timer that will reset the system in the case of system failures or
* malfunctions.
* malfunctions. There is only one instance in the system.
*
* Example:
* @code
*
* Watchdog watchdog();
* Watchdog& watchdog = Watchdog::get_instance();
* watchdog.start();
*
* while (true) {
@ -50,12 +50,17 @@ namespace mbed {
class Watchdog : private NonCopyable<Watchdog> {
public:
/** Constructor configured with timeout
*
*/
Watchdog();
~Watchdog();
public:
/** As Watchdog might not stop ever, there is just one instance - we use single instance.
* This ensures we keep Watchdog alive. To operate watchdog, use start/stop methods.
*/
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;
}
/** Start the watchdog timer
*
@ -100,6 +105,9 @@ public:
bool is_running() const;
private:
Watchdog();
~Watchdog();
static void kick();
static uint32_t _elapsed_ms;
static bool _running;