Files
MuditaOS/module-sys/SystemWatchdog/SystemWatchdog.cpp
Borys Jelenski d9ae779a37 [EGD-5503] Add watchdog implementation
The system watchdog monitors whether there is message traffic
on the Bus. If no message was sent for an extended period of time,
a reset will occur. It should also protect against system-wide hangs.

On Linux, watchdog is simulated by a FreeRTOS task that will call exit
on timeout.
2021-02-19 11:32:12 +01:00

76 lines
1.8 KiB
C++

// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include "SystemWatchdog.hpp"
#include "Service/Common.hpp"
#include "critical.hpp"
#include "ticks.hpp"
#include "bsp/watchdog/watchdog.hpp"
#include "log/log.hpp"
namespace sys
{
using namespace cpp_freertos;
static constexpr uint16_t stackDepthWords = 256;
SystemWatchdog::SystemWatchdog()
: Thread(threadName, stackDepthWords, static_cast<UBaseType_t>(ServicePriority::High))
{}
SystemWatchdog &SystemWatchdog::getInstance()
{
static SystemWatchdog watchdog;
return watchdog;
}
bool SystemWatchdog::init()
{
#ifdef DISABLE_WATCHDOG
return true;
#else
if (!bsp::watchdog::init(watchdogTimeoutPeriod)) {
return false;
}
bsp::watchdog::refresh();
if (!Start()) {
return false;
}
refresh();
return true;
#endif
}
void SystemWatchdog::refresh()
{
#ifndef DISABLE_WATCHDOG
// Critical section not required (atomic 32-bit writes)
lastRefreshTimestamp = Ticks::GetTicks();
#endif
}
void SystemWatchdog::Run()
{
while (true) {
vTaskDelay(checkPeriod);
if (timeout_occurred) {
continue;
}
// Critical section not required (atomic 32-bit reads)
if (Ticks::GetTicks() - lastRefreshTimestamp >= refreshTimeoutPeriod) {
// Allow HW watchdog timeout to occur
timeout_occurred = true;
LOG_FATAL("!!! System watchdog timeout, system will be resetted soon !!!");
}
else {
bsp::watchdog::refresh();
}
}
}
} // namespace sys