esp-hal/esp32-hal/examples/timer_interrupt.rs
Scott Mabin db409ffe7b
Unify the system peripheral (#832)
* Unify the system peripheral

Whilst the PCR, SYSTEM and DPORT peripherals are different, we currently
use them all in the same way. This PR unifies the peripheral name in the
hal to `SYSTEM`. The idea is that they all do the same sort of thing, so
we can collect them under the same name, and later down the line we can
being to expose differences under an extended API.

The benifits to this are imo quite big, the examples now are all identical,
which makes things easier for esp-wifi, and paves a path towards the
multichip hal.

Why not do this in the PAC? Imo the pac should be as close to the
hardware as possible, and the HAL is where we should abstractions such
as this.

* changelog
2023-09-29 08:14:50 -07:00

123 lines
3.6 KiB
Rust

//! This shows how to use the TIMG peripheral interrupts.
//! There is TIMG0 and TIMG1 each of them containing two general purpose timers
//! and a watchdog timer.
#![no_std]
#![no_main]
use core::cell::RefCell;
use critical_section::Mutex;
use esp32_hal::{
clock::ClockControl,
interrupt,
interrupt::Priority,
peripherals::{self, Peripherals, TIMG0, TIMG1},
prelude::*,
timer::{Timer, Timer0, Timer1, TimerGroup},
};
use esp_backtrace as _;
static TIMER00: Mutex<RefCell<Option<Timer<Timer0<TIMG0>>>>> = Mutex::new(RefCell::new(None));
static TIMER01: Mutex<RefCell<Option<Timer<Timer1<TIMG0>>>>> = Mutex::new(RefCell::new(None));
static TIMER10: Mutex<RefCell<Option<Timer<Timer0<TIMG1>>>>> = Mutex::new(RefCell::new(None));
static TIMER11: Mutex<RefCell<Option<Timer<Timer1<TIMG1>>>>> = Mutex::new(RefCell::new(None));
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let mut timer00 = timer_group0.timer0;
let mut timer01 = timer_group0.timer1;
let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks);
let mut timer10 = timer_group1.timer0;
let mut timer11 = timer_group1.timer1;
interrupt::enable(peripherals::Interrupt::TG0_T0_LEVEL, Priority::Priority2).unwrap();
interrupt::enable(peripherals::Interrupt::TG0_T1_LEVEL, Priority::Priority2).unwrap();
interrupt::enable(peripherals::Interrupt::TG1_T0_LEVEL, Priority::Priority3).unwrap();
interrupt::enable(peripherals::Interrupt::TG1_T1_LEVEL, Priority::Priority3).unwrap();
timer00.start(500u64.millis());
timer00.listen();
timer01.start(2500u64.millis());
timer01.listen();
timer10.start(1u64.secs());
timer10.listen();
timer11.start(3u64.secs());
timer11.listen();
critical_section::with(|cs| {
TIMER00.borrow_ref_mut(cs).replace(timer00);
TIMER01.borrow_ref_mut(cs).replace(timer01);
TIMER10.borrow_ref_mut(cs).replace(timer10);
TIMER11.borrow_ref_mut(cs).replace(timer11);
});
loop {}
}
#[interrupt]
fn TG0_T0_LEVEL() {
critical_section::with(|cs| {
let mut timer = TIMER00.borrow_ref_mut(cs);
let timer = timer.as_mut().unwrap();
if timer.is_interrupt_set() {
timer.clear_interrupt();
timer.start(500u64.millis());
esp_println::println!("Interrupt Level 2 - Timer0");
}
});
}
#[interrupt]
fn TG0_T1_LEVEL() {
critical_section::with(|cs| {
let mut timer = TIMER01.borrow_ref_mut(cs);
let timer = timer.as_mut().unwrap();
if timer.is_interrupt_set() {
timer.clear_interrupt();
timer.start(2500u64.millis());
esp_println::println!("Interrupt Level 2 - Timer1");
}
});
}
#[interrupt]
fn TG1_T0_LEVEL() {
critical_section::with(|cs| {
let mut timer = TIMER10.borrow_ref_mut(cs);
let timer = timer.as_mut().unwrap();
if timer.is_interrupt_set() {
timer.clear_interrupt();
timer.start(1u64.secs());
esp_println::println!("Interrupt Level 3 - Timer0");
}
});
}
#[interrupt]
fn TG1_T1_LEVEL() {
critical_section::with(|cs| {
let mut timer = TIMER10.borrow_ref_mut(cs);
let timer = timer.as_mut().unwrap();
if timer.is_interrupt_set() {
timer.clear_interrupt();
timer.start(3u64.secs());
esp_println::println!("Interrupt Level 3 - Timer1");
}
});
}