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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Clock peripheral driver, nRF52
//!
//! Based on Phil Levis clock driver for nRF51
//!
//! HFCLK - High Frequency Clock:
//!
//! * 64 MHz internal oscillator (HFINT)
//! * 64 MHz crystal oscillator, using 32 MHz external crystal (HFXO)
//! * The HFXO must be running to use the RADIO, NFC module or the calibration mechanism
//!   associated with the 32.768 kHz RC oscillator.
//!
//! LFCLK - Low Frequency Clock Source:
//!
//! * 32.768 kHz RC oscillator (LFRC)
//! * 32.768 kHz crystal oscillator (LFXO)
//! * 32.768 kHz synthesized from HFCLK (LFSYNT)
//!

use kernel::utilities::cells::OptionalCell;
use kernel::utilities::registers::interfaces::{Readable, Writeable};
use kernel::utilities::registers::{
    register_bitfields, register_structs, ReadOnly, ReadWrite, WriteOnly,
};
use kernel::utilities::StaticRef;

register_structs! {
    ClockRegisters {
        (0x000 => tasks_hfclkstart: WriteOnly<u32, Control::Register>),
        (0x004 => tasks_hfclkstop: WriteOnly<u32, Control::Register>),
        (0x008 => tasks_lfclkstart: ReadWrite<u32, Control::Register>),
        (0x00C => tasks_lfclkstop: WriteOnly<u32, Control::Register>),
        (0x010 => tasks_cal: WriteOnly<u32, Control::Register>),
        (0x014 => tasks_ctstart: WriteOnly<u32, Control::Register>),
        (0x018 => tasks_ctstop: WriteOnly<u32, Control::Register>),
        (0x01C => _reserved1),
        (0x100 => events_hfclkstarted: ReadOnly<u32, Status::Register>),
        (0x104 => events_lfclkstarted: ReadOnly<u32, Status::Register>),
        (0x108 => _reserved2),
        (0x10C => events_done: ReadOnly<u32, Status::Register>),
        (0x110 => events_ctto: ReadOnly<u32, Status::Register>),
        (0x114 => _reserved3),
        (0x304 => intenset: ReadWrite<u32, Interrupt::Register>),
        (0x308 => intenclr: ReadWrite<u32, Interrupt::Register>),
        (0x30C => _reserved4),
        (0x408 => hfclkrun: ReadOnly<u32, Status::Register>),
        (0x40C => hfclkstat: ReadOnly<u32, HfClkStat::Register>),
        (0x410 => _reserved5),
        (0x414 => lfclkrun: ReadOnly<u32, Control::Register>),
        (0x418 => lfclkstat: ReadWrite<u32, LfClkStat::Register>),
        (0x41C => lfclksrccopy: ReadOnly<u32, LfClkSrcCopy::Register>),
        (0x420 => _reserved6),
        (0x518 => lfclksrc: ReadWrite<u32, LfClkSrc::Register>),
        (0x51C => _reserved7),
        (0x538 => ctiv: ReadWrite<u32, Ctiv::Register>),
        (0x53C => _reserved8),
        (0x55C => traceconfig: ReadWrite<u32, TraceConfig::Register>),
        (0x560 => @END),
    }
}

register_bitfields! [u32,
    Control [
        ENABLE OFFSET(0) NUMBITS(1)
    ],
    Status [
        READY OFFSET(0) NUMBITS(1)
    ],
    Interrupt [
        HFCLKSTARTED OFFSET(0) NUMBITS(1),
        LFCLKSTARTED OFFSET(1) NUMBITS(1),
        DONE OFFSET(3) NUMBITS(1),
        CTTO OFFSET(4) NUMBITS(1)
    ],
    HfClkStat [
        SRC OFFSET(0) NUMBITS(1) [
            RC = 0,
            XTAL = 1
        ],
        STATE OFFSET(16) NUMBITS(1) [
            RUNNING = 1
        ]
    ],
    LfClkStat [
        SRC OFFSET(0) NUMBITS(2) [
            RC = 0,
            XTAL = 1,
            SYNTH = 2
        ],
        STATE OFFSET(16) NUMBITS(1) [
            RUNNING = 1
        ]
    ],
    LfClkSrcCopy [
        SRC OFFSET(0) NUMBITS(2) [
            RC = 0,
            XTAL = 1,
            SYNTH = 2
        ]
    ],
    LfClkSrc [
        SRC OFFSET(0) NUMBITS(2) [
            RC = 0,
            XTAL = 1,
            SYNTH = 2
        ]
    ],
    Ctiv [
        CTIV OFFSET(0) NUMBITS(7) []
    ],
    TraceConfig [
        TracePortSpeed OFFSET(0) NUMBITS(2) [
            THIRTYTWO = 0,
            SIXTEEN = 1,
            EIGHT = 2,
            FOUR = 3
        ],
        TraceMux OFFSET(16) NUMBITS(2) [
            GPIO = 0,
            SERIAL = 1,
            PARALELL = 2
        ]
    ]
];

const CLOCK_BASE: StaticRef<ClockRegisters> =
    unsafe { StaticRef::new(0x40000000 as *const ClockRegisters) };

/// Interrupt sources
pub enum InterruptField {
    HFCLKSTARTED = 1 << 0,
    LFCLKSTARTED = 1 << 1,
    DONE = 1 << 3,
    CTTO = 1 << 4,
}

/// Low frequency clock source
pub enum LowClockSource {
    RC = 0,
    XTAL = 1,
    SYNTH = 2,
    MASK = 3,
}

/// High frequency clock source
pub enum HighClockSource {
    RC = 0,
    XTAL = 1,
}

/// Clock struct
pub struct Clock {
    registers: StaticRef<ClockRegisters>,
    client: OptionalCell<&'static dyn ClockClient>,
}

pub trait ClockClient {
    /// All clock interrupts are control signals, e.g., when
    /// a clock has started etc. We don't actually handle any
    /// of them for now, but keep this trait in place for if we
    /// do need to in the future.
    fn event(&self);
}

impl Clock {
    /// Constructor
    pub const fn new() -> Clock {
        Clock {
            registers: CLOCK_BASE,
            client: OptionalCell::empty(),
        }
    }

    /// Client for callbacks
    pub fn set_client(&self, client: &'static dyn ClockClient) {
        self.client.set(client);
    }

    /// Enable interrupt
    pub fn interrupt_enable(&self, interrupt: InterruptField) {
        // this is a little too verbose
        match interrupt {
            InterruptField::CTTO => self.registers.intenset.write(Interrupt::CTTO::SET),
            InterruptField::DONE => self.registers.intenset.write(Interrupt::DONE::SET),
            InterruptField::HFCLKSTARTED => {
                self.registers.intenset.write(Interrupt::HFCLKSTARTED::SET)
            }
            InterruptField::LFCLKSTARTED => {
                self.registers.intenset.write(Interrupt::LFCLKSTARTED::SET)
            }
        }
    }

    /// Disable interrupt
    pub fn interrupt_disable(&self, interrupt: InterruptField) {
        // this is a little too verbose
        match interrupt {
            InterruptField::CTTO => self.registers.intenset.write(Interrupt::CTTO::SET),
            InterruptField::DONE => self.registers.intenset.write(Interrupt::DONE::SET),
            InterruptField::HFCLKSTARTED => {
                self.registers.intenset.write(Interrupt::HFCLKSTARTED::SET)
            }
            InterruptField::LFCLKSTARTED => {
                self.registers.intenset.write(Interrupt::LFCLKSTARTED::SET)
            }
        }
    }

    /// Start the high frequency clock - specifically HFXO, and sets the high frequency
    /// clock source to HFXO
    pub fn high_start(&self) {
        self.registers.tasks_hfclkstart.write(Control::ENABLE::SET);
    }

    /// Stop the high frequency clock
    pub fn high_stop(&self) {
        self.registers.tasks_hfclkstop.write(Control::ENABLE::SET);
    }

    /// Check if the high frequency clock has started
    pub fn high_started(&self) -> bool {
        self.registers
            .events_hfclkstarted
            .matches_all(Status::READY.val(1))
    }

    /// Read clock source from the high frequency clock
    pub fn high_source(&self) -> HighClockSource {
        match self.registers.hfclkstat.read(HfClkStat::SRC) {
            0 => HighClockSource::RC,
            _ => HighClockSource::XTAL,
        }
    }

    /// Check if the high frequency clock is running
    pub fn high_running(&self) -> bool {
        self.registers
            .hfclkstat
            .matches_all(HfClkStat::STATE::RUNNING)
    }

    /// Start the low frequency clock
    pub fn low_start(&self) {
        self.registers.tasks_lfclkstart.write(Control::ENABLE::SET);
    }

    /// Stop the low frequency clock
    pub fn low_stop(&self) {
        self.registers.tasks_lfclkstop.write(Control::ENABLE::SET);
    }

    /// Check if the low frequency clock has started
    pub fn low_started(&self) -> bool {
        self.registers
            .events_lfclkstarted
            .matches_all(Status::READY::SET)
    }

    /// Read clock source from the low frequency clock
    pub fn low_source(&self) -> LowClockSource {
        match self.registers.lfclkstat.read(LfClkStat::SRC) {
            0b1 => LowClockSource::XTAL,
            0b10 => LowClockSource::SYNTH,
            _ => LowClockSource::RC,
        }
    }

    /// Check if the low frequency clock is running
    pub fn low_running(&self) -> bool {
        self.registers
            .lfclkstat
            .matches_all(LfClkStat::STATE::RUNNING)
    }

    /// Set low frequency clock source
    pub fn low_set_source(&self, clock_source: LowClockSource) {
        self.registers
            .lfclksrc
            .write(LfClkSrc::SRC.val(clock_source as u32));
    }
}