~aleteoryx/muditaos

ref: e35fca8524dfe78c3076073d20bedad897e876a7 muditaos/module-sys/SystemWatchdog/SystemWatchdog.cpp -rw-r--r-- 2.2 KiB
e35fca85 — Lefucjusz [MOS-1064] Fix no input language selected for French/Spanish 1 year, 9 months 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <SystemWatchdog/SystemWatchdog.hpp>
#include "system/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();

        enableRunLoop = true;
        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 (enableRunLoop) {
            Delay(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 reset soon!");
            }
            else {
                bsp::watchdog::refresh();
            }
        }

        // notify caller of deinit()
        taskEndedSem.Give();
    }

    void SystemWatchdog::deinit()
    {
#ifndef DISABLE_WATCHDOG
        enableRunLoop = false;
        xTaskAbortDelay(GetHandle());
        if (!taskEndedSem.Take(closurePeriod)) {
            LOG_ERROR("Watchdog thread was not gently closed, killing");
        }
#endif
    }
} // namespace sys