Skip to content

Commit ebcbbef

Browse files
committed
Get rid of lifetime-riddled Sleep stuff
1 parent 0d08d5e commit ebcbbef

12 files changed

+178
-532
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ core
1010
/jlink-fw-sw
1111
.idea/
1212
**/venv
13-
13+
**/sdk-drivers
14+
**/sdk-examples
15+
notes.txt

Makefile

-12
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@ fetch-docs:
3232
curl -s https://www.segger.com/downloads/jlink/UM08036 \
3333
-o ref/gdbextensions-jlink.pdf
3434

35-
#
36-
# Maintenance
37-
#
38-
39-
VERSION := $(shell grep version Cargo.toml|head -1|cut -d' ' -f 3|tr -d '"')
40-
version:
41-
echo $(VERSION)
42-
43-
tag:
44-
git tag -a $(VERSION) -m"v$(VERSION)"
45-
git push origin $(VERSION)
46-
4735
#
4836
# CI
4937
#

VERSION

-1
This file was deleted.

examples/led.rs examples/led_sleep.rs.deactivated

+8-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use lpc55s6x_hal as hal;
1212
#[entry]
1313
fn main() -> ! {
1414
let hal = hal::new();
15+
let anactrl = hal.anactrl;
16+
let pmc = hal.pmc;
1517
let mut syscon = hal.syscon;
1618
// dbg!(syscon.get_num_wait_states());
1719
let mut gpio = hal.gpio.enabled(&mut syscon);
@@ -39,18 +41,17 @@ fn main() -> ! {
3941
.into_gpio_pin(&mut iocon, &mut gpio)
4042
.into_output(hal::drivers::pins::Level::High); // start turned off
4143

42-
// let iocon = iocon.disabled(&mut syscon);
43-
// iocon.release();
44+
let clocks = hal::ClockRequirements::default()
45+
.configure(&mut anactrl, &mut pmc, &mut syscon)
46+
.unwrap();
4447

45-
let clock = hal::peripherals::syscon::Fro1MhzUtickClock::take()
46-
.unwrap()
47-
.enable(&mut syscon);
48+
let token = clocks.support_utick_token().unwrap();
4849

49-
let mut utick = hal.utick.enabled(&mut syscon, &clock);
50+
let mut utick = hal.utick.enabled(&mut syscon, &token);
5051

5152
let delay = hal::time::Ticks {
5253
value: 500_000,
53-
clock: &clock,
54+
// clock: &clock,
5455
}; // 500 ms = 0.5 s
5556

5657
let mut sleep = hal::time::Busy::wrap(&mut utick);

examples/led_utick.rs

+30-33
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,47 @@
44
// extern crate panic_semihosting; // 4004 bytes
55
extern crate panic_halt; // 672 bytes
66

7-
// use cortex_m::asm;
87
use cortex_m_rt::entry;
9-
// use nb::block;
108

11-
use hal::{drivers::pins::Level, prelude::*};
129
use lpc55s6x_hal as hal;
10+
use hal::{
11+
drivers::pins::Level,
12+
prelude::*,
13+
};
1314

14-
// macro_rules! kitt {
15-
// ($( $led:ident ),+ ) => ({
16-
// $led.set_low().unwrap();
17-
// utick.start(1_000_000u32);
18-
// utick.blocking_wait();
19-
// $led.set_high().unwrap();
20-
// }, *);
21-
// }
15+
macro_rules! kitt {
16+
($utick:ident, $($led:ident),+ ) => ($(
17+
18+
// low = on
19+
$led.set_low().unwrap();
20+
21+
$utick.start(1_000_000u32);
22+
$utick.blocking_wait();
23+
24+
// high = off
25+
$led.set_high().unwrap();
26+
27+
)*);
28+
}
2229

2330
#[entry]
2431
fn main() -> ! {
2532
let hal = hal::new();
33+
34+
let mut anactrl = hal.anactrl;
35+
let mut pmc = hal.pmc;
2636
let mut syscon = hal.syscon;
37+
38+
let clocks = hal::ClockRequirements::default()
39+
.configure(&mut anactrl, &mut pmc, &mut syscon)
40+
.unwrap();
41+
42+
let token = clocks.support_utick_token().unwrap();
43+
let mut utick = hal.utick.enabled(&mut syscon, &token);
44+
2745
let mut gpio = hal.gpio.enabled(&mut syscon);
2846
let mut iocon = hal.iocon.enabled(&mut syscon);
2947
let pins = hal::Pins::take().unwrap();
30-
let fro1mhz = hal::peripherals::syscon::Fro1MhzUtickClock::take()
31-
.unwrap()
32-
.enable(&mut syscon);
33-
let mut utick = hal.utick.enabled(&mut syscon, &fro1mhz);
34-
// let (utick, fro1mhz) = utick.disabled(&mut syscon);
35-
// let mut utick = utick.enabled(&mut syscon, fro1mhz);
3648

3749
// R = pio1_6
3850
// G = pio1_7
@@ -54,21 +66,6 @@ fn main() -> ! {
5466
.into_output(Level::High);
5567

5668
loop {
57-
// kitt!(red, green, blue);
58-
red.set_low().unwrap();
59-
utick.start(1_000_000u32);
60-
// block!(utick.wait()).unwrap();
61-
utick.blocking_wait();
62-
red.set_high().unwrap();
63-
64-
green.set_low().unwrap();
65-
utick.start(1_000_000u32);
66-
utick.blocking_wait();
67-
green.set_high().unwrap();
68-
69-
blue.set_low().unwrap();
70-
utick.start(1_000_000u32);
71-
utick.blocking_wait();
72-
blue.set_high().unwrap();
69+
kitt!(utick, red, green, blue);
7370
}
7471
}

src/drivers/clocks.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::typestates::{
1313
// clock_state,
1414
ClocksSupportFlexcommToken,
1515
ClocksSupportUsbfsToken,
16+
ClocksSupportUtickToken,
1617
};
1718
use crate::{
1819
peripherals::{
@@ -63,6 +64,10 @@ impl Clocks {
6364
None
6465
}
6566
}
67+
68+
pub fn support_utick_token(&self) -> Option<ClocksSupportUtickToken> {
69+
Some(ClocksSupportUtickToken{__: ()})
70+
}
6671
}
6772

6873
/// Output of Pll is: M/(2NP) times input
@@ -184,9 +189,14 @@ impl ClockRequirements {
184189
// unsafe { pmc.raw.pdruncfgclr0.write(|w| w.bits(1u32 << 5)) };
185190
// but it's hidden in UM, so let's assume this is always cleared
186191

187-
// turn on both 12mhz and 96mhz clocks
192+
// turn on 1mhz, 12mhz and 96mhz clocks
188193
anactrl.raw.fro192m_ctrl.modify(|_, w| w.ena_96mhzclk().enable());
189194
anactrl.raw.fro192m_ctrl.modify(|_, w| w.ena_12mhzclk().enable());
195+
// TODO: not clear what the difference of these two is; eg. are both needed?
196+
syscon.raw.clock_ctrl.modify(|_, w| w
197+
.fro1mhz_clk_ena().enable()
198+
.fro1mhz_utick_ena().enable()
199+
);
190200

191201
let (main_clock, sys_divider) = match freq {
192202
freq if freq <= 12.mhz() && 12 % freq.0 == 0 => {

src/peripherals/syscon.rs

+7-69
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
// use cortex_m_semihosting::dbg;
2020

2121
// use crate::raw;
22-
use crate::{
23-
time::{
24-
self,
25-
clock,
26-
},
27-
typestates::init_state,
28-
};
22+
// use crate::{
23+
// time::{
24+
// self,
25+
// clock,
26+
// },
27+
// typestates::init_state,
28+
// };
2929

3030
crate::wrap_always_on_peripheral!(Syscon, SYSCON);
3131

@@ -309,65 +309,3 @@ impl_reset_control!(raw::UTICK0, utick_rst, presetctrl1);
309309

310310
impl_reset_control!(raw::USBFSH, usb0_hostm_rst, usb0_hosts_rst, presetctrl2);
311311

312-
static mut FRO1MHZUTICKCLOCK_TAKEN: bool = false;
313-
314-
pub struct Fro1MhzUtickClock<State = init_state::Disabled> {
315-
_state: State,
316-
}
317-
318-
impl Fro1MhzUtickClock<init_state::Disabled> {
319-
pub fn take() -> Option<Self> {
320-
if unsafe { FRO1MHZUTICKCLOCK_TAKEN } {
321-
None
322-
} else {
323-
Some(unsafe {
324-
FRO1MHZUTICKCLOCK_TAKEN = true;
325-
Fro1MhzUtickClock::steal()
326-
})
327-
}
328-
}
329-
330-
pub fn release(self) {
331-
unsafe { FRO1MHZUTICKCLOCK_TAKEN = false };
332-
}
333-
334-
pub unsafe fn steal() -> Self {
335-
Fro1MhzUtickClock {
336-
_state: init_state::Disabled,
337-
}
338-
}
339-
340-
/// Enable the FRO1MHZ UTICK clock
341-
pub fn enable(self, syscon: &mut Syscon) -> Fro1MhzUtickClock<init_state::Enabled> {
342-
syscon
343-
.raw
344-
.clock_ctrl
345-
.modify(|_, w| w.fro1mhz_utick_ena().enable());
346-
347-
Fro1MhzUtickClock {
348-
_state: init_state::Enabled(()),
349-
}
350-
}
351-
}
352-
353-
impl Fro1MhzUtickClock<init_state::Enabled> {
354-
/// Disable the FRO1MHZ UTICK clock
355-
pub fn disable(self, syscon: &mut Syscon) -> Fro1MhzUtickClock<init_state::Disabled> {
356-
syscon
357-
.raw
358-
.clock_ctrl
359-
.modify(|_, w| w.fro1mhz_utick_ena().disable());
360-
361-
Fro1MhzUtickClock {
362-
_state: init_state::Disabled,
363-
}
364-
}
365-
}
366-
367-
impl<State> time::Frequency for Fro1MhzUtickClock<State> {
368-
fn hz(&self) -> u32 {
369-
1_000_000
370-
}
371-
}
372-
373-
impl clock::Enabled for Fro1MhzUtickClock<init_state::Enabled> {}

src/peripherals/utick.rs

+16-41
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,44 @@
77
//!
88
//! # Examples: led.rs, led_utick.rs
99
10+
// TODO: move this to drivers section,
11+
// possibly merge with ctimers when they're implemented
12+
1013
use embedded_hal::timer;
1114
use nb;
1215
use void::Void;
1316

1417
use crate::{
1518
raw,
1619
peripherals::{
17-
syscon::{
18-
self,
19-
Fro1MhzUtickClock,
20-
},
20+
syscon,
2121
},
2222
typestates::{
2323
init_state,
24+
ClocksSupportUtickToken,
2425
},
2526
};
2627

2728
crate::wrap_stateful_peripheral!(Utick, UTICK0);
2829

29-
pub type EnabledUtick<'fro1mhz> =
30-
Utick<init_state::Enabled<&'fro1mhz Fro1MhzUtickClock<init_state::Enabled>>>;
30+
pub type EnabledUtick = Utick<init_state::Enabled>;
3131

3232
impl<State> Utick<State> {
33-
/// Enable the UTICK
34-
///
35-
/// Consume a UTICK in `Disabled` state, return an instance in `Enabled` state.
36-
pub fn enabled<'fro1mhz>(
33+
pub fn enabled(
3734
mut self,
3835
syscon: &mut syscon::Syscon,
39-
fro1mhz: &'fro1mhz Fro1MhzUtickClock<init_state::Enabled>,
40-
) -> EnabledUtick<'fro1mhz> {
36+
_clocktree_token: &ClocksSupportUtickToken,
37+
) -> EnabledUtick {
4138
syscon.enable_clock(&mut self.raw);
4239
syscon.reset(&mut self.raw);
4340

44-
// Below is no longer needed, since we require passing in an enabled FRO1MHZ.
45-
// Maybe these references will have to go again though due to RTFM...
46-
//
47-
// NB: UM says bit 4 (FRO_HF_FREQM_ENA), which is incorrect
48-
// Empirically, it is enough to enable `fro1mhz_utick_ena`,
49-
// even if `fro1mhz_clk_ena` is explicitly disabled.
50-
// unsafe { &*crate::raw::SYSCON::ptr() }
51-
// .clock_ctrl
52-
// .modify(|_, w| w.fro1mhz_utick_ena().enable());
53-
5441
Utick {
5542
raw: self.raw,
56-
_state: init_state::Enabled(fro1mhz),
43+
_state: init_state::Enabled(()),
5744
}
5845
}
5946

60-
/// Disable the UTICK
61-
///
62-
/// Consume a UTICK in `Enabled` state, return an instance in `Disabled` state.
6347
pub fn disabled(mut self, syscon: &mut syscon::Syscon) -> Utick<init_state::Disabled> {
64-
// unsafe { &*crate::raw::SYSCON::ptr() }
65-
// .clock_ctrl
66-
// .modify(|_, w| w.fro1mhz_utick_ena().disable());
6748
syscon.disable_clock(&mut self.raw);
6849

6950
Utick {
@@ -73,7 +54,9 @@ impl<State> Utick<State> {
7354
}
7455
}
7556

76-
impl timer::Cancel for EnabledUtick<'_> {
57+
// TODO: This does not feel like it belongs here.
58+
59+
impl timer::Cancel for EnabledUtick {
7760
type Error = Void;
7861

7962
fn cancel(&mut self) -> Result<(), Self::Error> {
@@ -84,7 +67,7 @@ impl timer::Cancel for EnabledUtick<'_> {
8467
}
8568

8669
// TODO: also implement Periodic for UTICK
87-
impl timer::CountDown for EnabledUtick<'_> {
70+
impl timer::CountDown for EnabledUtick {
8871
type Time = u32;
8972

9073
fn start<T>(&mut self, timeout: T)
@@ -117,17 +100,9 @@ impl timer::CountDown for EnabledUtick<'_> {
117100
}
118101
}
119102

120-
impl EnabledUtick<'_> {
103+
// TODO: Either get rid of `nb` or get rid of this
104+
impl EnabledUtick {
121105
pub fn blocking_wait(&mut self) {
122106
while self.raw.stat.read().active().bit_is_set() {}
123107
}
124108
}
125-
126-
/// A clock that is usable by the micro-tick timer (UTICK)
127-
///
128-
/// This trait is implemented for all clocks that are supported by the UTICK,
129-
/// which is just FRO1MHZ.
130-
/// The user shouldn't need to implement this trait themselves.
131-
pub trait Clock {}
132-
133-
impl Clock for Fro1MhzUtickClock<init_state::Enabled> {}

src/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub use crate::traits::wg::prelude::*;
55
pub use crate::traits::wg::digital::v2::OutputPin as _embedded_hal_digital_v2_OutputPin;
66
pub use crate::traits::wg::digital::v2::StatefulOutputPin as _embedded_hal_digital_v2_StatefulOutputPin;
77

8-
pub use crate::time::Sleep as _lpc55s6x_hal_time_Sleep;
8+
// pub use crate::time::Sleep as _lpc55s6x_hal_time_Sleep;
99
pub use crate::time::U32Ext as _lpc55s6x_hal_time_U32Ext;
1010

1111
// Good idea? Bad idea?

0 commit comments

Comments
 (0)