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
// This is a bit of a hodgepodge of more standard stm32-rs
// and less standard lpc-rs approaches.
//
// Will see what ends where and how.

// use embedded_hal::timer::CountDown;

// use crate::{
//     peripherals::{
//         utick::EnabledUtick,
//     },
// };

//////////////////////////////
// stm32-f4
//////////////////////////////
#[derive(PartialEq, PartialOrd, Clone, Copy, Debug)]
pub struct Hertz(pub u32);
#[derive(PartialEq, PartialOrd, Clone, Copy, Debug)]
pub struct Kilohertz(pub u32);
#[derive(PartialEq, PartialOrd, Clone, Copy, Debug)]
pub struct Megahertz(pub u32);

impl From<Kilohertz> for Hertz {
    fn from(khz: Kilohertz) -> Self {
        Hertz(1_000 * khz.0)
    }
}

impl From<Megahertz> for Kilohertz {
    fn from(mhz: Megahertz) -> Self {
        Kilohertz(1_000 * mhz.0)
    }
}

impl From<Megahertz> for Hertz {
    fn from(mhz: Megahertz) -> Self {
        Hertz(1_000_000 * mhz.0)
    }
}

#[derive(PartialEq, PartialOrd, Clone, Copy, Debug)]
pub struct Baud(pub u32);
#[derive(PartialEq, PartialOrd, Clone, Copy, Debug)]
pub struct Kilobaud(pub u32);
#[derive(PartialEq, PartialOrd, Clone, Copy, Debug)]
pub struct Megabaud(pub u32);

impl From<Kilobaud> for Baud {
    fn from(kbd: Kilobaud) -> Self {
        Baud(1_000 * kbd.0)
    }
}

impl From<Megabaud> for Kilobaud {
    fn from(mbd: Megabaud) -> Self {
        Kilobaud(1_000 * mbd.0)
    }
}

impl From<Megabaud> for Baud {
    fn from(mbd: Megabaud) -> Self {
        Baud(1_000_000 * mbd.0)
    }
}

pub trait U32Ext {
    /// Wrap in `Hertz`
    fn hz(self) -> Hertz;

    /// Wrap in `Kilohertz`
    fn khz(self) -> Kilohertz;

    /// Wrap in `Megahertz`
    fn mhz(self) -> Megahertz;

    /// Wrap in `Baud`
    fn bps(self) -> Baud;

    /// Wrap in `Kilobaud`
    fn kbps(self) -> Kilobaud;

    /// Wrap in `Megabaud`
    fn mbps(self) -> Megabaud;
}

impl U32Ext for u32 {
    fn hz(self) -> Hertz {
        Hertz(self)
    }

    fn khz(self) -> Kilohertz {
        Kilohertz(self)
    }

    fn mhz(self) -> Megahertz {
        Megahertz(self)
    }

    fn bps(self) -> Baud {
        Baud(self)
    }

    fn kbps(self) -> Kilobaud {
        Kilobaud(self)
    }

    fn mbps(self) -> Megabaud {
        Megabaud(self)
    }
}


////////////////////////////////////
//
// lpc-rs
//
// TODO: This stuff has way too
// many references and lifetimes...
//
////////////////////////////////////


//pub struct Ticks<'clock, C: 'clock> {
//    pub value: u32,

//    /// Reference to the clock
//    ///
//    /// Kept to prevent changes of the clock configuration.
//    pub clock: &'clock C,
//}

//impl<'clock, Clock> Clone for Ticks<'clock, Clock> {
//    fn clone(&self) -> Self {
//        Ticks {
//            value: self.value,
//            clock: self.clock,
//        }
//    }
//}

//impl<'clock, Clock> Copy for Ticks<'clock, Clock> {}

//pub trait Frequency {
//    /// This method must never return `0`.
//    fn hz(&self) -> u32;
//}

//pub mod clock {
//    // only meant for clocks
//    pub trait Enabled {}
//}

//pub trait Sleep<Clock>
//where
//    Clock: clock::Enabled,
//{
//    /// Puts the processor to sleep for the given number of ticks of the clock
//    fn sleep<'clock, T>(&mut self, ticks: T)
//    where
//        Clock: 'clock,
//        T: Into<Ticks<'clock, Clock>>;
//}

//pub struct Busy<'utick> {
//    utick: &'utick mut EnabledUtick,
//}

//impl<'utick> Busy<'utick> {
//    /// Prepare busy sleep mode
//    ///
//    /// Returns an instance of `sleep::Busy`, which implements [`Sleep`] and can
//    /// therefore be used to put the microcontroller to sleep.
//    ///
//    /// Requires a mutable reference to [`UTICK`]. The reference will be borrowed
//    /// for as long as the `sleep::Busy` instance exists, as it will be needed
//    /// to count down the time in every call to [`Sleep::sleep`].
//    pub fn wrap(utick: &'utick mut EnabledUtick) -> Self {
//        Busy { utick }
//    }
//}

//impl<'utick, Clock> Sleep<Clock> for Busy<'utick>
//where
//    Clock: clock::Enabled// + crate::peripherals::utick::Clock,
//{
//    fn sleep<'clock, T>(&mut self, ticks: T)
//    where
//        Clock: 'clock,
//        T: Into<Ticks<'clock, Clock>>,
//    {
//        let ticks: Ticks<Clock> = ticks.into();

//        // If we try to sleep for zero cycles, we'll never wake up again.
//        if ticks.value == 0 {
//            return;
//        }

//        self.utick.start(ticks.value);
//        while let Err(nb::Error::WouldBlock) = self.utick.wait() {
//            asm::nop();
//        }
//    }
//}

//// TODO-ish: there's also "Regular" sleep, which means entering a sleep
//// mode, and exiting on an interrupt.