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

View File

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

View File

@ -36,30 +36,6 @@ namespace mbed {
bool Watchdog::_running = false; bool Watchdog::_running = false;
static const uint32_t elapsed_ms = MBED_CONF_TARGET_WATCHDOG_TIMEOUT / 2; 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() bool Watchdog::start()
{ {
watchdog_status_t sts; watchdog_status_t sts;
@ -121,12 +97,12 @@ bool Watchdog::is_running() const
return _running; return _running;
} }
uint32_t Watchdog::get_timeout() uint32_t Watchdog::get_timeout() const
{ {
return hal_watchdog_get_reload_value(); 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(); const watchdog_features_t features = hal_watchdog_get_platform_features();
return features.max_timeout; return features.max_timeout;

View File

@ -33,12 +33,12 @@ namespace mbed {
/** \addtogroup drivers */ /** \addtogroup drivers */
/** Hardware system timer that will reset the system in the case of system failures or /** 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: * Example:
* @code * @code
* *
* Watchdog watchdog(); * Watchdog& watchdog = Watchdog::get_instance();
* watchdog.start(); * watchdog.start();
* *
* while (true) { * while (true) {
@ -50,12 +50,17 @@ namespace mbed {
class Watchdog : private NonCopyable<Watchdog> { class Watchdog : private NonCopyable<Watchdog> {
public: public:
/** Constructor configured with timeout /** 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.
*/ */
Watchdog(); static Watchdog &get_instance()
~Watchdog(); {
public: // 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 /** Start the watchdog timer
* *
@ -100,6 +105,9 @@ public:
bool is_running() const; bool is_running() const;
private: private:
Watchdog();
~Watchdog();
static void kick(); static void kick();
static uint32_t _elapsed_ms; static uint32_t _elapsed_ms;
static bool _running; static bool _running;