Skip to content

Commit

Permalink
Make Alarms real singletons
Browse files Browse the repository at this point in the history
- Remove runtime Option, turn into compile error
- Make Systimer::now() not take self
  • Loading branch information
MabezDev committed Jun 9, 2022
1 parent 3b7f790 commit 8f445d8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 37 deletions.
48 changes: 19 additions & 29 deletions esp-hal-common/src/systimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,49 @@ use crate::pac::SYSTIMER;

#[derive(Debug)]
pub struct SystemTimer {
inner: SYSTIMER,
alrm0: Option<Alarm<Target, 0>>,
alrm1: Option<Alarm<Target, 1>>,
alrm2: Option<Alarm<Target, 2>>,
_inner: SYSTIMER,
pub alarm0: Alarm<Target, 0>,
pub alarm1: Alarm<Target, 1>,
pub alarm2: Alarm<Target, 2>,
}

impl SystemTimer {
pub fn new(p: SYSTIMER) -> Self {
// TODO enable
Self {
inner: p,
alrm0: Some(Alarm::new()),
alrm1: Some(Alarm::new()),
alrm2: Some(Alarm::new()),
_inner: p,
alarm0: Alarm::new(),
alarm1: Alarm::new(),
alarm2: Alarm::new(),
}
}

// TODO use fugit types
pub fn now(&self) -> u64 {
self.inner
pub fn now() -> u64 {
// This should be safe to access from multiple contexts
// worst case scenario the second accesor ends up reading
// an older time stamp
let systimer = unsafe { &*SYSTIMER::ptr() };
systimer
.unit0_op
.write(|w| w.timer_unit0_update().set_bit());
.modify(|_, w| w.timer_unit0_update().set_bit());

while !self
.inner
while !systimer
.unit0_op
.read()
.timer_unit0_value_valid()
.bit_is_set()
{}

let value_lo = self.inner.unit0_value_lo.read().bits();
let value_hi = self.inner.unit0_value_hi.read().bits();
let value_lo = systimer.unit0_value_lo.read().bits();
let value_hi = systimer.unit0_value_hi.read().bits();

((value_hi as u64) << 32) | value_lo as u64
}

pub fn alarm0(&mut self) -> Option<Alarm<Target, 0>> {
self.alrm0.take()
}

pub fn alarm1(&mut self) -> Option<Alarm<Target, 1>> {
self.alrm1.take()
}

pub fn alarm2(&mut self) -> Option<Alarm<Target, 2>> {
self.alrm2.take()
}
}

#[derive(Debug)]
pub struct Target;
// pub struct Periodic; // TODO
// pub struct Periodic; // TODO, also impl e-h timer traits

#[derive(Debug)]
pub struct Alarm<MODE, const CHANNEL: u8> {
Expand Down
16 changes: 8 additions & 8 deletions esp32c3-hal/examples/systimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ fn main() -> ! {

writeln!(serial0, "SYSTIMER Demo start!").ok();

let mut syst = SystemTimer::new(peripherals.SYSTIMER);
let syst = SystemTimer::new(peripherals.SYSTIMER);

writeln!(serial0, "SYSTIMER Current value = {}", syst.now()).ok();
writeln!(serial0, "SYSTIMER Current value = {}", SystemTimer::now()).ok();

let alarm0 = syst.alarm0().unwrap();
let alarm0 = syst.alarm0;
alarm0.set_target(40_000_000);
alarm0.enable_interrupt();

let alarm1 = syst.alarm1().unwrap();
let alarm1 = syst.alarm1;
alarm1.set_target(41_111_111);
alarm1.enable_interrupt();

let alarm2 = syst.alarm2().unwrap();
let alarm2 = syst.alarm2;
alarm2.set_target(42_222_222 * 2);
alarm2.enable_interrupt();

Expand Down Expand Up @@ -123,7 +123,7 @@ pub fn interrupt1() {
riscv::interrupt::free(|cs| unsafe {
let mut serial = SERIAL.borrow(*cs).borrow_mut();
let serial = serial.as_mut().unwrap();
writeln!(serial, "Interrupt 1").ok();
writeln!(serial, "Interrupt 1 = {}", SystemTimer::now()).ok();

let mut alarm = ALARM0.borrow(*cs).borrow_mut();
let alarm = alarm.as_mut().unwrap();
Expand All @@ -138,7 +138,7 @@ pub fn interrupt2() {
riscv::interrupt::free(|cs| unsafe {
let mut serial = SERIAL.borrow(*cs).borrow_mut();
let serial = serial.as_mut().unwrap();
writeln!(serial, "Interrupt 2").ok();
writeln!(serial, "Interrupt 2 = {}", SystemTimer::now()).ok();

let mut alarm = ALARM1.borrow(*cs).borrow_mut();
let alarm = alarm.as_mut().unwrap();
Expand All @@ -153,7 +153,7 @@ pub fn interrupt3() {
riscv::interrupt::free(|cs| unsafe {
let mut serial = SERIAL.borrow(*cs).borrow_mut();
let serial = serial.as_mut().unwrap();
writeln!(serial, "Interrupt 3").ok();
writeln!(serial, "Interrupt 3 = {}", SystemTimer::now()).ok();

let mut alarm = ALARM2.borrow(*cs).borrow_mut();
let alarm = alarm.as_mut().unwrap();
Expand Down

0 comments on commit 8f445d8

Please sign in to comment.