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
//! API for the micro-tick timer (UTICK)
//!
//! The entry point to this API is [`UTICK`].
//!
//! The UTICK peripheral is described in the user manual, chapter 26.
//! It is driven by the FRO 1Mhz clock and has a microsecond resolution.
//!
//! # Examples: led.rs, led_utick.rs

// TODO: move this to drivers section,
// possibly merge with ctimers when they're implemented

use core::convert::Infallible;
use embedded_hal::timer;
use nb;
use void::Void;

use crate::{
    raw,
    peripherals::{
        syscon,
    },
    typestates::{
        init_state,
        ClocksSupportUtickToken,
    },
};

crate::wrap_stateful_peripheral!(Utick, UTICK0);

pub type EnabledUtick = Utick<init_state::Enabled>;

impl<State> Utick<State> {
    pub fn enabled(
        mut self,
        syscon: &mut syscon::Syscon,
        _clocktree_token: &ClocksSupportUtickToken,
    ) -> EnabledUtick {
        syscon.enable_clock(&mut self.raw);
        syscon.reset(&mut self.raw);

        Utick {
            raw: self.raw,
            _state: init_state::Enabled(()),
        }
    }

    pub fn disabled(mut self, syscon: &mut syscon::Syscon) -> Utick<init_state::Disabled> {
        syscon.disable_clock(&mut self.raw);

        Utick {
            raw: self.raw,
            _state: init_state::Disabled,
        }
    }
}

// TODO: This does not feel like it belongs here.

impl timer::Cancel for EnabledUtick {
    type Error = Infallible;

    fn cancel(&mut self) -> Result<(), Self::Error> {
        // A value of 0 stops the timer.
        self.raw.ctrl.write(|w| unsafe { w.delayval().bits(0) });
        Ok(())
    }
}

// TODO: also implement Periodic for UTICK
impl timer::CountDown for EnabledUtick {
    type Time = u32;

    fn start<T>(&mut self, timeout: T)
    where
        T: Into<Self::Time>,
    {
        // The delay will be equal to DELAYVAL + 1 periods of the timer clock.
        // The minimum usable value is 1, for a delay of 2 timer clocks. A value of 0 stops the timer.
        let time = timeout.into();
        // Maybe remove again? Empirically, nothing much happens when
        // writing 1 to `delayval`.
        assert!(time >= 2);
        self.raw
            .ctrl
            .write(|w| unsafe { w.delayval().bits(time - 1) });
        // So... this seems a bit unsafe (what if time is 2?)
        // But: without it, in --release builds the timer behaves erratically.
        // The UM says this on the topic: "Note that the Micro-tick Timer operates from a different
        // (typically slower) clock than the CPU and bus systems.  This means there may be a
        // synchronization delay when accessing Micro-tick Timer registers."
        while self.raw.stat.read().active().bit_is_clear() {}
    }

    fn wait(&mut self) -> nb::Result<(), Void> {
        if self.raw.stat.read().active().bit_is_clear() {
            return Ok(());
        }

        Err(nb::Error::WouldBlock)
    }
}

// TODO: Either get rid of `nb` or get rid of this
impl EnabledUtick {
    pub fn blocking_wait(&mut self) {
        while self.raw.stat.read().active().bit_is_set() {}
    }
}