Skip to content

Commit

Permalink
Update doc and example
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Fischer committed Jul 2, 2024
1 parent 9eb6fb5 commit 6afda54
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
8 changes: 8 additions & 0 deletions esp-hal/src/timer/systimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ impl<'d> SystemTimer<'d> {

impl SystemTimer<'static> {
/// Split the System Timer into three alarms.
///
/// This is a convenience method to create `'static` alarms of the same
/// type. You are encouraged to use [Alarm::new] over this very specific
/// helper.
pub fn split<MODE>(self) -> SysTimerAlarms<MODE, Blocking> {
static CONFIG: Mutex<Cell<Option<Config>>> = Mutex::new(Cell::new(None));
static mut UNIT0: Option<Unit<'static, 0>> = None;
Expand All @@ -193,6 +197,10 @@ impl SystemTimer<'static> {
}

/// Split the System Timer into three alarms.
///
/// This is a convenience method to create `'static` alarms of the same
/// type. You are encouraged to use [Alarm::new_async] over this very
/// specific helper.
pub fn split_async<MODE>(self) -> SysTimerAlarms<MODE, Async> {
static CONFIG: Mutex<Cell<Option<Config>>> = Mutex::new(Cell::new(None));
static mut UNIT0: Option<Unit<'static, 0>> = None;
Expand Down
21 changes: 15 additions & 6 deletions examples/src/bin/systimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#![no_std]
#![no_main]

use core::cell::RefCell;
use core::cell::{Cell, RefCell};

use critical_section::Mutex;
use esp_backtrace as _;
Expand All @@ -18,11 +18,12 @@ use esp_hal::{
peripherals::{Interrupt, Peripherals},
prelude::*,
system::SystemControl,
timer::systimer::{Alarm, Periodic, SystemTimer, Target},
timer::systimer::{Alarm, Config, FrozenUnit, Periodic, SystemTimer, Target, Unit},
Blocking,
};
use esp_println::println;
use fugit::ExtU32;
use static_cell::StaticCell;

static ALARM0: Mutex<RefCell<Option<Alarm<Periodic, Blocking, 0, 0>>>> =
Mutex::new(RefCell::new(None));
Expand All @@ -40,20 +41,28 @@ fn main() -> ! {
let systimer = SystemTimer::new(peripherals.SYSTIMER);
println!("SYSTIMER Current value = {}", SystemTimer::now());

let alarms = systimer.split();
static CONFIG: Mutex<Cell<Option<Config>>> = Mutex::new(Cell::new(None));
static UNIT0: StaticCell<Unit<'static, 0>> = StaticCell::new();

let unit0 = UNIT0.init(systimer.unit0);
critical_section::with(|cs| CONFIG.borrow(cs).set(Some(systimer.config)));

let frozen_unit = FrozenUnit::new(unit0);

let mut alarm0 = Alarm::new(systimer.comparator0, &frozen_unit, &CONFIG);
let mut alarm1 = Alarm::new(systimer.comparator1, &frozen_unit, &CONFIG);
let mut alarm2 = Alarm::new(systimer.comparator2, &frozen_unit, &CONFIG);

critical_section::with(|cs| {
let mut alarm0 = alarms.alarm0.into_periodic();
let mut alarm0 = alarm0.into_periodic();
alarm0.set_interrupt_handler(systimer_target0);
alarm0.set_period(1u32.secs());
alarm0.enable_interrupt(true);

let mut alarm1 = alarms.alarm1;
alarm1.set_interrupt_handler(systimer_target1);
alarm1.set_target(SystemTimer::now() + (SystemTimer::TICKS_PER_SECOND * 2));
alarm1.enable_interrupt(true);

let mut alarm2 = alarms.alarm2;
alarm2.set_interrupt_handler(systimer_target2);
alarm2.set_target(SystemTimer::now() + (SystemTimer::TICKS_PER_SECOND * 3));
alarm2.enable_interrupt(true);
Expand Down

0 comments on commit 6afda54

Please sign in to comment.