sifive/
watchdog.rs

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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Watchdog driver.

use kernel::utilities::registers::interfaces::Writeable;
use kernel::utilities::registers::{register_bitfields, ReadWrite, WriteOnly};
use kernel::utilities::StaticRef;

#[repr(C)]
pub struct WatchdogRegisters {
    /// Watchdog Configuration Register
    wdogcfg: ReadWrite<u32, cfg::Register>,
    _reserved0: [u8; 4],
    /// Watchdog Counter Register
    wdogcount: ReadWrite<u32>,
    _reserved1: [u8; 4],
    /// Watchdog Scaled Counter Register
    wdogs: ReadWrite<u32>,
    _reserved2: [u8; 4],
    /// Watchdog Feed Register
    wdogfeed: ReadWrite<u32, feed::Register>,
    /// Watchdog Key Register
    wdogkey: WriteOnly<u32, key::Register>,
    /// Watchdog Compare Register
    wdogcmp: ReadWrite<u32>,
}

register_bitfields![u32,
    cfg [
        cmpip OFFSET(28) NUMBITS(1) [],
        encoreawake OFFSET(13) NUMBITS(1) [],
        enalways OFFSET(12) NUMBITS(1) [],
        zerocmp OFFSET(9) NUMBITS(1) [],
        rsten OFFSET(8) NUMBITS(1) [],
        scale OFFSET(0) NUMBITS(4) []
    ],
    key [
        key OFFSET(0) NUMBITS(32) []
    ],
    feed [
        feed OFFSET(0) NUMBITS(32) []
    ]
];

pub struct Watchdog {
    registers: StaticRef<WatchdogRegisters>,
}

impl Watchdog {
    pub const fn new(base: StaticRef<WatchdogRegisters>) -> Watchdog {
        Watchdog { registers: base }
    }

    fn unlock(&self) {
        self.registers.wdogkey.write(key::key.val(0x51F15E));
    }

    fn feed(&self) {
        self.unlock();
        self.registers.wdogfeed.write(feed::feed.val(0xD09F00D));
    }

    pub fn disable(&self) {
        self.unlock();
        self.registers.wdogcfg.write(
            cfg::scale.val(0)
                + cfg::rsten::CLEAR
                + cfg::zerocmp::CLEAR
                + cfg::enalways::CLEAR
                + cfg::encoreawake::CLEAR,
        );
        self.feed();
    }
}