~aleteoryx/muditaos

ref: a21736e3a63927001e8cee6cee5b964e238bb8a9 muditaos/module-bsp/board/linux/watchdog/software_watchdog.cpp -rw-r--r-- 1.5 KiB
a21736e3 — Lefucjusz [MOS-1050] Add missing Germany HNI codes 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "software_watchdog.hpp"
#include <critical.hpp>
#include <ticks.hpp>
#include <system/Common.hpp>
#include <log/log.hpp>
#include <cstdlib>

namespace bsp::watchdog
{
    using namespace cpp_freertos;

    static constexpr uint16_t stackDepthWords = 256;

    SoftwareWatchdog::SoftwareWatchdog()
        : Thread("SW_Watchdog", stackDepthWords, static_cast<UBaseType_t>(sys::ServicePriority::Realtime))
    {}

    bool SoftwareWatchdog::init(TickType_t timeoutPeriodMs)
    {
#ifdef DISABLE_WATCHDOG
        return true;
#else
        timeoutPeriod      = pdMS_TO_TICKS(timeoutPeriodMs);
        const auto started = Start();

        if (started) {
            refresh();
        }

        return started;
#endif
    }

    void SoftwareWatchdog::refresh()
    {
#ifndef DISABLE_WATCHDOG
        // Critical section not required (atomic 32-bit writes)
        lastRefreshTimestamp = Ticks::GetTicks();
#endif
    }

    void SoftwareWatchdog::Run()
    {
        TickType_t lastTimeoutTimestamp = xTaskGetTickCount();

        while (true) {
            vTaskDelayUntil(&lastTimeoutTimestamp, timeoutPeriod);

            // Critical section not required (atomic 32-bit reads)
            if (lastTimeoutTimestamp - lastRefreshTimestamp >= timeoutPeriod) {
                LOG_FATAL("!!! Software watchdog timeout, exiting !!!");
                exit(EXIT_FAILURE);
            }
        }
    }
} // namespace bsp::watchdog