diff --git a/drivers/VirtualWatchdog.cpp b/drivers/VirtualWatchdog.cpp index 66891459f1..73212d6658 100644 --- a/drivers/VirtualWatchdog.cpp +++ b/drivers/VirtualWatchdog.cpp @@ -31,7 +31,7 @@ VirtualWatchdog::VirtualWatchdog(uint32_t timeout, const char *const str): _name _max_timeout = timeout; // start watchdog Watchdog& watchdog = Watchdog::get_instance(); - watchdog.start(); + watchdog.start(&VirtualWatchdog::process, WatchdogManager::elapsed_ms); } VirtualWatchdog::~VirtualWatchdog() @@ -97,14 +97,14 @@ void VirtualWatchdog::remove_from_list() } } -void VirtualWatchdog::process(uint32_t elapsed_ms) +void VirtualWatchdog::process() { VirtualWatchdog *cur_ptr = _first; while (cur_ptr != NULL) { if (cur_ptr->_current_count > cur_ptr->_max_timeout) { system_reset(); } else { - cur_ptr->_current_count += elapsed_ms; + cur_ptr->_current_count += WatchdogManager::elapsed_ms; } cur_ptr = cur_ptr->_next; } diff --git a/drivers/VirtualWatchdog.h b/drivers/VirtualWatchdog.h index f0a6c51d94..2a05bf68d7 100644 --- a/drivers/VirtualWatchdog.h +++ b/drivers/VirtualWatchdog.h @@ -25,6 +25,7 @@ #include "platform/mbed_critical.h" #include "platform/mbed_power_mgmt.h" #include "mbed_assert.h" +#include "WatchdogManager.h" namespace mbed { @@ -94,7 +95,7 @@ public: * * Otherwise, the system resets. */ - static void process(uint32_t elapsed_ms); + static void process(); protected : /** Use add_to_list to store the registered user in the list. diff --git a/drivers/Watchdog.cpp b/drivers/Watchdog.cpp index 9b59dadb00..1078ba90a4 100644 --- a/drivers/Watchdog.cpp +++ b/drivers/Watchdog.cpp @@ -16,24 +16,24 @@ */ #ifdef DEVICE_WATCHDOG -#include "Watchdog.h" +#include "drivers/Watchdog.h" +#include "drivers/WatchdogManager.h" #define MS_TO_US(x) ((x) * 1000) //macro to convert millisecond to microsecond namespace mbed { -static const uint32_t elapsed_ms = MBED_CONF_TARGET_WATCHDOG_TIMEOUT / 2; - -Watchdog::Watchdog() : _running(false) +Watchdog::Watchdog() : _running(false), _callback(NULL) { } -~Watchdog::Watchdog() +Watchdog::~Watchdog() { } -bool Watchdog::start() +bool Watchdog::start(Callback func, uint32_t timeout) { + _callback = func; watchdog_status_t sts; MBED_ASSERT(MBED_CONF_TARGET_WATCHDOG_TIMEOUT < get_max_timeout()); core_util_critical_section_enter(); @@ -49,8 +49,8 @@ bool Watchdog::start() } core_util_critical_section_exit(); if (_running) { - us_timestamp_t timeout = (MS_TO_US(((elapsed_ms <= 0) ? 1 : elapsed_ms))); - _ticker->attach_us(callback(&Watchdog::get_instance(), &Watchdog::kick), timeout); + us_timestamp_t ticker_timeout = (MS_TO_US(((timeout <= 0) ? 1 : timeout))); + WatchdogManager::attach(callback(this, &Watchdog::kick), ticker_timeout); } return _running; } @@ -66,8 +66,9 @@ bool Watchdog::stop() if (sts != WATCHDOG_STATUS_OK) { msts = false; } else { - _ticker->detach(); + WatchdogManager::detach(); _running = false; + _callback = NULL; } } else { @@ -82,6 +83,9 @@ void Watchdog::kick() core_util_critical_section_enter(); hal_watchdog_kick(); core_util_critical_section_exit(); + if (_callback) { + _callback(); + } } diff --git a/drivers/Watchdog.h b/drivers/Watchdog.h index 9845a272b2..1aa1f51cd3 100644 --- a/drivers/Watchdog.h +++ b/drivers/Watchdog.h @@ -27,6 +27,7 @@ #include "platform/NonCopyable.h" #include "platform/SingletonPtr.h" #include "drivers/LowPowerTicker.h" +#include "drivers/WatchdogManager.h" #include namespace mbed { @@ -69,7 +70,7 @@ public: * successfully. assert if one of the input parameters is out of range for the current platform. * false if watchdog timer was not started */ - bool start(); + bool start(Callback func = NULL, uint32_t timeout = WatchdogManager::elapsed_ms); /** Stops the watchdog timer * @@ -104,22 +105,13 @@ public: */ bool is_running() const; + void kick(); private: Watchdog(); ~Watchdog(); - void kick(); - bool _running; -#if DEVICE_LPTICKER - /** Create singleton instance of LowPowerTicker for watchdog periodic call back of kick. - */ - SingletonPtr _ticker; -#else - /** Create singleton instance of Ticker for watchdog periodic call back of kick. - */ - SingletonPtr _ticker; -#endif + Callback _callback; }; } // namespace mbed diff --git a/drivers/WatchdogManager.cpp b/drivers/WatchdogManager.cpp new file mode 100644 index 0000000000..bfc2eba7fa --- /dev/null +++ b/drivers/WatchdogManager.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2018-2019 Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * 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. + */ +#ifdef DEVICE_WATCHDOG + +#include "WatchdogManager.h" + +namespace mbed { + +#if DEVICE_LPTICKER + /** Create singleton instance of LowPowerTicker for watchdog periodic call back of kick. + */ + SingletonPtr _ticker; +#else + /** Create singleton instance of Ticker for watchdog periodic call back of kick. + */ + SingletonPtr _ticker; +#endif + +void WatchdogManager::attach(Callback func, us_timestamp_t timeout) +{ + _ticker->attach_us(func, timeout); +} + +void WatchdogManager::detach() +{ + _ticker->detach(); +} + +} // namespace mbed + + +#endif // DEVICE_WATCHDOG diff --git a/drivers/WatchdogManager.h b/drivers/WatchdogManager.h new file mode 100644 index 0000000000..40db8a0c65 --- /dev/null +++ b/drivers/WatchdogManager.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2019 Arm Limited and affiliates. + * SPDX-License-Identifier: Apache-2.0 + * + * 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_MANAGER_H +#define MBED_WATCHDOG_MANAGER_H + +#include "platform/SingletonPtr.h" +#include "drivers/LowPowerTicker.h" +#include "platform/Callback.h" +#include "hal/ticker_api.h" + +#include + + +namespace mbed { + +/** \addtogroup drivers */ + +class WatchdogManager { +public: + static void attach(Callback func, us_timestamp_t timeout); + static void detach(); + // static uint32_t get_elapsed(); + static const uint32_t elapsed_ms = MBED_CONF_TARGET_WATCHDOG_TIMEOUT / 2; +private: +#if DEVICE_LPTICKER + /** Create singleton instance of LowPowerTicker for watchdog periodic call back of kick. + */ + static SingletonPtr _ticker; +#else + /** Create singleton instance of Ticker for watchdog periodic call back of kick. + */ + static SingletonPtr _ticker; +#endif +}; + +} // namespace mbed + +#endif // MBED_WATCHDOG_MANAGER_H