mbed-os/drivers/Watchdog.cpp

108 lines
2.5 KiB
C++
Raw Normal View History

2019-06-26 08:33:48 +00:00
/*
* Copyright (c) 2018-2019 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.
*/
#ifdef DEVICE_WATCHDOG
#include "Watchdog.h"
#define MS_TO_US(x) ((x) * 1000) //macro to convert millisecond to microsecond
2017-11-28 14:00:56 +00:00
namespace mbed {
static const uint32_t elapsed_ms = MBED_CONF_TARGET_WATCHDOG_TIMEOUT / 2;
Watchdog::Watchdog() : _running(false)
{
}
2019-06-27 15:26:29 +00:00
~Watchdog::Watchdog()
{
}
bool Watchdog::start()
2017-11-28 14:00:56 +00:00
{
watchdog_status_t sts;
MBED_ASSERT(MBED_CONF_TARGET_WATCHDOG_TIMEOUT < get_max_timeout());
core_util_critical_section_enter();
if (_running) {
core_util_critical_section_exit();
return false;
}
watchdog_config_t config;
config.timeout_ms = MBED_CONF_TARGET_WATCHDOG_TIMEOUT;
sts = hal_watchdog_init(&config);
if (sts == WATCHDOG_STATUS_OK) {
_running = true;
}
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);
}
return _running;
2017-11-28 14:00:56 +00:00
}
bool Watchdog::stop()
2017-11-28 14:00:56 +00:00
{
watchdog_status_t sts;
bool msts = true;
core_util_critical_section_enter();
if (_running) {
sts = hal_watchdog_stop();
if (sts != WATCHDOG_STATUS_OK) {
msts = false;
} else {
_ticker->detach();
_running = false;
}
} else {
msts = false;
}
core_util_critical_section_exit();
return msts;
}
void Watchdog::kick()
{
core_util_critical_section_enter();
hal_watchdog_kick();
core_util_critical_section_exit();
2017-11-28 14:00:56 +00:00
}
bool Watchdog::is_running() const
{
return _running;
}
2017-11-28 14:00:56 +00:00
uint32_t Watchdog::get_timeout() const
2017-11-28 14:00:56 +00:00
{
return hal_watchdog_get_reload_value();
2017-11-28 14:00:56 +00:00
}
uint32_t Watchdog::get_max_timeout() const
2017-11-28 14:00:56 +00:00
{
const watchdog_features_t features = hal_watchdog_get_platform_features();
return features.max_timeout;
2017-11-28 14:00:56 +00:00
}
} // namespace mbed
2017-11-28 14:00:56 +00:00
#endif // DEVICE_WATCHDOG