~aleteoryx/muditaos

muditaos/module-bsp/board/rt1051/bsp/watchdog/watchdog.cpp -rw-r--r-- 1.6 KiB
a405cad6Aleteoryx trim readme 2 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "bsp/watchdog/watchdog.hpp"
#include <fsl_rtwdog.h>
#include <limits>
#include <cstdint>

namespace bsp::watchdog
{
    bool init(unsigned timeoutMs)
    {
        // 32.768kHz source clock divided by 256 prescaler
        constexpr unsigned clockFreqHz = 32768 / 256;

        const unsigned timeoutValueTicks = timeoutMs * clockFreqHz / 1000;
        if (timeoutValueTicks > std::numeric_limits<std::uint16_t>::max()) {
            return false;
        }

        rtwdog_config_t config;
        RTWDOG_GetDefaultConfig(&config);
        config.enableRtwdog         = true;
        config.clockSource          = kRTWDOG_ClockSource1;            // LPO_CLK clock (32.768kHz)
        config.prescaler            = kRTWDOG_ClockPrescalerDivide256; // 256 prescaler (effectively 128Hz clock)
        config.workMode.enableWait  = true;                            // Keep RTWDOG enabled in WFI
        config.workMode.enableStop  = false;
        config.workMode.enableDebug = false; // If true, RTWDOG will run when target is halted
        config.testMode             = kRTWDOG_TestModeDisabled;
        config.enableUpdate         = true;
        config.enableInterrupt      = true;
        config.enableWindowMode     = false;
        config.windowValue          = 0;
        config.timeoutValue         = static_cast<std::uint16_t>(timeoutValueTicks);
        RTWDOG_Init(RTWDOG, &config);

        return true;
    }

    void refresh()
    {
        RTWDOG_Refresh(RTWDOG);
    }
} // namespace bsp::watchdog