mbed-os/drivers/Watchdog.cpp

111 lines
2.5 KiB
C++
Raw Normal View History

2017-11-28 14:00:56 +00:00
/* mbed Microcontroller Library
2018-11-19 16:55:27 +00:00
* Copyright (c) 2018 ARM Limited
* 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"
namespace mbed {
Watchdog *Watchdog::_first = NULL;
Watchdog::Watchdog(uint32_t timeout, const char *const str): _name(str)
{
_current_count = 0;
_is_initialized = false;
_next = NULL;
_max_timeout = timeout;
}
void Watchdog::add_to_list()
2017-11-28 14:00:56 +00:00
{
this->_next = _first;
_first = this;
_is_initialized = true;
2017-11-28 14:00:56 +00:00
}
void Watchdog::start()
2017-11-28 14:00:56 +00:00
{
MBED_ASSERT(!_is_initialized);
core_util_critical_section_enter();
add_to_list();
core_util_critical_section_exit();
2017-11-28 14:00:56 +00:00
}
void Watchdog::kick()
2017-11-28 14:00:56 +00:00
{
MBED_ASSERT(_is_initialized);
core_util_critical_section_enter();
_current_count = 0;
core_util_critical_section_exit();
}
void Watchdog::stop()
{
MBED_ASSERT(_is_initialized);
core_util_critical_section_enter();
remove_from_list();
core_util_critical_section_exit();
2017-11-28 14:00:56 +00:00
}
void Watchdog::remove_from_list()
{
Watchdog *cur_ptr = _first,
*prev_ptr = NULL;
while (cur_ptr != NULL) {
if (cur_ptr == this) {
if (cur_ptr == _first) {
prev_ptr = _first;
_first = cur_ptr->_next;
prev_ptr->_next = NULL;
} else {
prev_ptr->_next = cur_ptr->_next;
cur_ptr->_next = NULL;
}
_is_initialized = false;
break;
} else {
prev_ptr = cur_ptr;
cur_ptr = cur_ptr->_next;
}
}
}
2017-11-28 14:00:56 +00:00
void Watchdog::process(uint32_t elapsed_ms)
2017-11-28 14:00:56 +00:00
{
Watchdog *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 = cur_ptr->_next;
}
2017-11-28 14:00:56 +00:00
}
Watchdog::~Watchdog()
2017-11-28 14:00:56 +00:00
{
if (_is_initialized) {
stop();
}
2017-11-28 14:00:56 +00:00
}
} // namespace mbed
#endif // DEVICE_WATCHDOG