~aleteoryx/muditaos

ref: 958ce4906c5386ad8aaa0e7e72697be7388ca9ef muditaos/module-bsp/board/rt1051/bsp/watchdog/watchdog.cpp -rw-r--r-- 1.5 KiB
958ce490 — Marcin Smoczyński [BH-897] Split module-sys 4 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "bsp/watchdog/watchdog.hpp"
extern "C"
{
#include "fsl_rtwdog.h"
}
#include <limits>

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

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

        rtwdog_config_t 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  = false;
        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      = false;
        config.enableWindowMode     = false;
        config.windowValue          = 0;
        config.timeoutValue         = static_cast<uint16_t>(timeoutValueTicks);
        RTWDOG_Init(RTWDOG, &config);

        return true;
    }

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