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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//! # External Oscillator Peripheral
//!

use crate::gpio::gpioa::{PTB6, PTB7};
use crate::pac::OSC;
use core::marker::PhantomData;

/// Custom Error Types
pub enum Error {
    /// Value was not valid for the function it was passed into.
    InvalidValue,

    /// Pin has already been returned. Can't give it again
    NoPin,
}

// Common state-types

/// Peripheral doesn't care about this type.
///
/// It will be set to something useful later.
pub struct DontCare;

/// Clock feature is disabled
pub struct Stopped;

/// Clock feature is Enabled, but is Disabled on entry to Stop Mode.
pub struct Running;

/// Clock feature is always Enabled, even in Stop mode.
pub struct Unstoppable;

/// External Oscillator Peripheral
///
/// Oscillator is Stopped (Disabled) by default.
pub struct Osc<Status, OscType, Range, Gain, ExtalPinState, XtalPinState> {
    _status: PhantomData<Status>,
    _osc_type: PhantomData<OscType>,
    _range: PhantomData<Range>,
    _gain: PhantomData<Gain>,
    extal_pin: Option<PTB7<ExtalPinState>>,
    xtal_pin: Option<PTB6<XtalPinState>>,
}

/// Grabs ownership of OSC from the PAC
pub trait OSCExt {
    /// This module's state struct
    type Osc;

    /// grab the peripheral from the PAC and return the state struct
    fn split(self) -> Self::Osc;
}

impl OSCExt for OSC {
    type Osc = Osc<Stopped, ExtClock, LowRange, VariableGain, DontCare, DontCare>;
    fn split(self) -> Osc<Stopped, ExtClock, LowRange, VariableGain, DontCare, DontCare> {
        Osc {
            _status: PhantomData,
            _osc_type: PhantomData, //ExtClock is default, but don't care.
            _range: PhantomData,    // LowRange is default, but don't care.
            _gain: PhantomData,     // Variable (low power) is default, but don't care.
            extal_pin: None,
            xtal_pin: None,
        }
    }
}

/// External Clock used.
pub struct ExtClock;

/// External Oscillator or Resonator used
pub struct ExtOsc;

/// External Oscillator set for low range (roughly 32kHZ).
pub struct LowRange;

/// External oscillator set for high range (4-20MHz).
pub struct HighRange;

/// Oscillator module uses Variable gain to save power
pub struct VariableGain;

/// Oscillator module uses High Gain to get rail-to-rail oscillations.
pub struct HighGain;

impl<OscType, Range, Gain, ExtalPinState, XtalPinState>
    Osc<Stopped, OscType, Range, Gain, ExtalPinState, XtalPinState>
{
    /// Start-up the oscillator. Blocks until the oscillator is running.
    pub fn into_running(
        self,
        extal_pin: PTB7<ExtalPinState>,
    ) -> Osc<Running, OscType, Range, Gain, ExtalPinState, XtalPinState> {
        let osc = unsafe { &(*OSC::ptr()) };

        // start the oscillator
        osc.cr.modify(|_, w| w.oscen().set_bit());

        // block until it's stable
        while osc.cr.read().oscinit().is_0() {
            cortex_m::asm::nop();
        }

        Osc {
            _status: PhantomData,
            _osc_type: PhantomData,
            _range: PhantomData,
            _gain: PhantomData,
            extal_pin: Some(extal_pin),
            xtal_pin: self.xtal_pin,
        }
    }

    /// Returns the pin used for the EXTAL input
    ///
    /// @TODO what is the state of the pin after decoupling from OSC
    /// peripheral?
    pub fn release_extal_pin(
        self,
    ) -> (
        Result<PTB7<ExtalPinState>, Error>,
        Osc<Stopped, OscType, Range, Gain, DontCare, XtalPinState>,
    ) {
        (
            self.extal_pin.ok_or(Error::NoPin),
            Osc {
                _status: PhantomData,
                _osc_type: PhantomData,
                _range: PhantomData,
                _gain: PhantomData,
                extal_pin: None,
                xtal_pin: self.xtal_pin,
            },
        )
    }
}

impl<OscType, Range, Gain, ExtalPinState, XtalPinState>
    Osc<Running, OscType, Range, Gain, ExtalPinState, XtalPinState>
{
    /// Allow the OSC peripheral to continue running when the system goes into
    /// STOP mode.
    pub fn into_unstoppable(
        self,
    ) -> Osc<Unstoppable, OscType, Range, Gain, ExtalPinState, XtalPinState> {
        unsafe {
            (*OSC::ptr()).cr.modify(|_, w| w.oscsten().set_bit());
        }
        Osc {
            _status: PhantomData,
            _osc_type: PhantomData,
            _range: PhantomData,
            _gain: PhantomData,
            extal_pin: self.extal_pin,
            xtal_pin: self.xtal_pin,
        }
    }

    /// Stop (disable) the OSC peripheral
    ///
    /// @TODO think of ways to not ruin someones day if they are using OSC_OUT
    /// as a reference clock.
    pub fn into_stopped(self) -> Osc<Stopped, OscType, Range, Gain, ExtalPinState, XtalPinState> {
        let osc = unsafe { &(*OSC::ptr()) };
        osc.cr.modify(|_, w| w.oscen().clear_bit());
        Osc {
            _status: PhantomData,
            _osc_type: PhantomData,
            _range: PhantomData,
            _gain: PhantomData,
            extal_pin: self.extal_pin,
            xtal_pin: self.xtal_pin,
        }
    }
}

impl<OscType, Range, Gain, ExtalPinState, XtalPinState>
    Osc<Unstoppable, OscType, Range, Gain, ExtalPinState, XtalPinState>
{
    /// Allow the OSC peripheral to be stopped when the system goes into STOP
    /// mode.
    pub fn into_running(self) -> Osc<Running, OscType, Range, Gain, ExtalPinState, XtalPinState> {
        unsafe {
            (*OSC::ptr()).cr.modify(|_, w| w.oscsten().clear_bit());
        }
        Osc {
            _status: PhantomData,
            _osc_type: PhantomData,
            _range: PhantomData,
            _gain: PhantomData,
            extal_pin: self.extal_pin,
            xtal_pin: self.xtal_pin,
        }
    }

    /// Stop (disable) the OSC peripheral
    ///
    /// @TODO think of ways to not ruin someones day if they are using OSC_OUT
    /// as a reference clock.
    pub fn into_stopped(self) -> Osc<Stopped, OscType, Range, Gain, ExtalPinState, XtalPinState> {
        let osc = unsafe { &(*OSC::ptr()) };
        osc.cr.modify(|_, w| w.oscen().clear_bit());
        Osc {
            _status: PhantomData,
            _osc_type: PhantomData,
            _range: PhantomData,
            _gain: PhantomData,
            extal_pin: self.extal_pin,
            xtal_pin: self.xtal_pin,
        }
    }
}

impl<Status, Range, Gain, ExtalPinState, XtalPinState>
    Osc<Status, ExtOsc, Range, Gain, ExtalPinState, XtalPinState>
{
    /// Change to External Clock mode (not resonator or crystal)
    ///
    /// Consumes Pin PTB
    pub fn into_ext_clock(self) -> Osc<Status, ExtClock, Range, Gain, ExtalPinState, XtalPinState> {
        unsafe {
            (*OSC::ptr()).cr.modify(|_, w| w.oscos()._0());
        }
        Osc {
            _status: PhantomData,
            _osc_type: PhantomData,
            _range: PhantomData,
            _gain: PhantomData,
            extal_pin: self.extal_pin,
            xtal_pin: self.xtal_pin,
        }
    }
}

impl<Status, Range, Gain, ExtalPinState, XtalPinState>
    Osc<Status, ExtClock, Range, Gain, ExtalPinState, XtalPinState>
{
    /// Change to External oscillator / resonator mode
    ///
    /// Consumes Pins PTB6:PTB7
    pub fn into_ext_osc(self) -> Osc<Status, ExtOsc, Range, Gain, ExtalPinState, XtalPinState> {
        unsafe { (*OSC::ptr()).cr.modify(|_, w| w.oscos()._1()) }
        Osc {
            _status: PhantomData,
            _osc_type: PhantomData,
            _range: PhantomData,
            _gain: PhantomData,
            extal_pin: self.extal_pin,
            xtal_pin: self.xtal_pin,
        }
    }

    /// Returns the pin used for the XTAL input.
    ///
    /// This pin is only needed for the external oscillator / resonator mode.
    ///
    /// @TODO what is the state of the pin after decoupling from OSC
    /// peripheral?
    pub fn release_xtal_pin(
        self,
    ) -> (
        Result<PTB6<XtalPinState>, Error>,
        Osc<Status, ExtClock, Range, Gain, ExtalPinState, DontCare>,
    ) {
        (
            self.xtal_pin.ok_or(Error::NoPin),
            Osc {
                _status: PhantomData,
                _osc_type: PhantomData,
                _range: PhantomData,
                _gain: PhantomData,
                extal_pin: self.extal_pin,
                xtal_pin: None,
            },
        )
    }
}

// Osc Module is stopped
impl<OscType, Gain, ExtalPinState, XtalPinState>
    Osc<Stopped, OscType, LowRange, Gain, ExtalPinState, XtalPinState>
{
    /// Set to high range.
    ///
    /// 4-20MHz input. Range must be set while the Osc module is disabled.
    pub fn into_high_range(
        self,
    ) -> Osc<Stopped, OscType, HighRange, Gain, ExtalPinState, XtalPinState> {
        unsafe {
            (*OSC::ptr()).cr.modify(|_, w| w.range().set_bit());
        }
        Osc {
            _status: PhantomData,
            _osc_type: PhantomData,
            _range: PhantomData,
            _gain: PhantomData,
            extal_pin: self.extal_pin,
            xtal_pin: self.xtal_pin,
        }
    }
}

// Osc Module is stopped
impl<OscType, Gain, ExtalPinState, XtalPinState>
    Osc<Stopped, OscType, HighRange, Gain, ExtalPinState, XtalPinState>
{
    /// Set to low range.
    ///
    /// Roughly 32kHZ. Range must be set while the Osc module is disabled.
    pub fn into_low_range(
        self,
    ) -> Osc<Stopped, OscType, HighRange, Gain, ExtalPinState, XtalPinState> {
        unsafe {
            (*OSC::ptr()).cr.modify(|_, w| w.range().clear_bit());
        }
        Osc {
            _status: PhantomData,
            _osc_type: PhantomData,
            _range: PhantomData,
            _gain: PhantomData,
            extal_pin: self.extal_pin,
            xtal_pin: self.xtal_pin,
        }
    }
}

// I'm pretty sure that there's no harm in changing this in External clock
// mode, if so then change generic OscType to ExtOsc
impl<Status, OscType, Range, ExtalPinState, XtalPinState>
    Osc<Status, OscType, Range, HighGain, ExtalPinState, XtalPinState>
{
    /// Set the Oscillator to use Variable Gain.
    ///
    /// This mode adjusts the oscillator gain to get an acceptable amplitude,
    /// minimizing power used.
    pub fn into_variable_gain(
        self,
    ) -> Osc<Status, OscType, Range, VariableGain, ExtalPinState, XtalPinState> {
        unsafe {
            (*OSC::ptr()).cr.modify(|_, w| w.hgo().clear_bit());
        };
        Osc {
            _status: PhantomData,
            _osc_type: PhantomData,
            _range: PhantomData,
            _gain: PhantomData,
            extal_pin: self.extal_pin,
            xtal_pin: self.xtal_pin,
        }
    }
}

impl<Status, OscType, Range, ExtalPinState, XtalPinState>
    Osc<Status, OscType, Range, HighGain, ExtalPinState, XtalPinState>
{
    /// Set the Oscillator to use High Gain
    ///
    /// This mode attempts to get rail-to-rail output from the oscillator or
    /// resonator.
    pub fn into_high_gain(
        self,
    ) -> Osc<Status, OscType, Range, HighGain, ExtalPinState, XtalPinState> {
        unsafe {
            (*OSC::ptr()).cr.modify(|_, w| w.hgo().set_bit());
        };
        Osc {
            _status: PhantomData,
            _osc_type: PhantomData,
            _range: PhantomData,
            _gain: PhantomData,
            extal_pin: self.extal_pin,
            xtal_pin: self.xtal_pin,
        }
    }
}