Skip to content

Commit

Permalink
Embassy init updates:
Browse files Browse the repository at this point in the history
- Rename timg feature to timg0 to better refect which TG is being used
- Use the time_driver::TimerType in the signature of init to fix #268
- Update examples
- Fix CI features
- Add timg0 cfg to build.rs
  • Loading branch information
MabezDev committed Dec 8, 2022
1 parent f726eb0 commit 23f1ed0
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 76 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/ci-async.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ jobs:
chip_features:
[
{ chip: esp32c2, features: "embassy,embassy-time-systick" },
{ chip: esp32c2, features: "embassy,embassy-time-timg0" },
{ chip: esp32c3, features: "embassy,embassy-time-systick" },
{ chip: esp32c3, features: "embassy,embassy-time-timg0" },
]
toolchain: [nightly]
example:
Expand Down Expand Up @@ -52,11 +54,11 @@ jobs:
matrix:
chip_features:
[
{ chip: esp32, features: "embassy,embassy-time-timg" },
{ chip: esp32, features: "embassy,embassy-time-timg0" },
# { chip: esp32s2, features: "embassy,embassy-time-systick" }, # Removed for now, see esp32s2-hal/Cargo.toml
{ chip: esp32s2, features: "embassy,embassy-time-timg" },
{ chip: esp32s2, features: "embassy,embassy-time-timg0" },
{ chip: esp32s3, features: "embassy,embassy-time-systick" },
{ chip: esp32s3, features: "embassy,embassy-time-timg" },
{ chip: esp32s3, features: "embassy,embassy-time-timg0" },
]
example:
[
Expand Down
2 changes: 1 addition & 1 deletion esp-hal-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async = ["embedded-hal-async", "eh1", "embassy-sync"]
embassy = ["embassy-time"]

embassy-time-systick = []
embassy-time-timg = []
embassy-time-timg0 = []

# Architecture-specific features (intended for internal use)
riscv = ["dep:riscv", "critical-section/restore-state-u8", "procmacros/riscv", "riscv-atomic-emulation-trap"]
Expand Down
7 changes: 6 additions & 1 deletion esp-hal-common/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn main() {
// - 'rmt'
// - 'spi3'
// - 'systimer'
// - 'timg0'
// - 'timg1'
// - 'uart2'
// - 'usb_otg'
Expand All @@ -49,11 +50,12 @@ fn main() {
"pdma",
"rmt",
"spi3",
"timg0",
"timg1",
"uart2",
]
} else if esp32c2 {
vec!["esp32c2", "riscv", "single_core", "gdma", "systimer"]
vec!["esp32c2", "riscv", "single_core", "gdma", "systimer", "timg0"]
} else if esp32c3 {
vec![
"esp32c3",
Expand All @@ -64,6 +66,7 @@ fn main() {
"rmt",
"spi3",
"systimer",
"timg0",
"timg1",
"usb_serial_jtag",
]
Expand All @@ -79,6 +82,7 @@ fn main() {
"rmt",
"spi3",
"systimer",
"timg0",
"timg1",
"usb_otg",
]
Expand All @@ -94,6 +98,7 @@ fn main() {
"rmt",
"spi3",
"systimer",
"timg0",
"timg1",
"uart2",
"usb_otg",
Expand Down
12 changes: 4 additions & 8 deletions esp-hal-common/src/embassy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@ use core::{cell::Cell, ptr};

use embassy_time::driver::{AlarmHandle, Driver};

use crate::clock::Clocks;

#[cfg_attr(
all(systimer, feature = "embassy-time-systick",),
path = "embassy/time_driver_systimer.rs"
)]
#[cfg_attr(
all(feature = "embassy-time-timg", any(esp32, esp32s2, esp32s3)),
all(timg0, feature = "embassy-time-timg0"),
path = "embassy/time_driver_timg.rs"
)]
mod time_driver;

use time_driver::EmbassyTimer;

pub fn init(clocks: &Clocks) {
// TODO:
// In the future allow taking of &mut Peripheral when we move to the
// PeripheralRef way of driver initialization, see: https://github.com/esp-rs/esp-idf-hal/blob/5d1aea58cdda195e20d1489fcba8a8ecb6562d9a/src/peripheral.rs#L94
use crate::clock::Clocks;

EmbassyTimer::init(clocks);
pub fn init(clocks: &Clocks, td: time_driver::TimerType) {
EmbassyTimer::init(clocks, td)
}

pub struct AlarmState {
Expand Down
12 changes: 7 additions & 5 deletions esp-hal-common/src/embassy/time_driver_systimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use crate::{

pub const ALARM_COUNT: usize = 3;

pub type TimerType = SystemTimer;

pub struct EmbassyTimer {
pub alarms: Mutex<[AlarmState; ALARM_COUNT]>,
pub alarm0: Alarm<Target, 0>,
pub alarm1: Alarm<Target, 1>,
pub alarm2: Alarm<Target, 2>,
pub(crate) alarms: Mutex<[AlarmState; ALARM_COUNT]>,
pub(crate) alarm0: Alarm<Target, 0>,
pub(crate) alarm1: Alarm<Target, 1>,
pub(crate) alarm2: Alarm<Target, 2>,
}

const ALARM_STATE_NONE: AlarmState = AlarmState::new();
Expand Down Expand Up @@ -52,7 +54,7 @@ impl EmbassyTimer {
})
}

pub(crate) fn init(_clocks: &Clocks) {
pub fn init(_clocks: &Clocks, _systimer: TimerType) {
use crate::{interrupt, interrupt::Priority, macros::interrupt};

interrupt::enable(pac::Interrupt::SYSTIMER_TARGET0, Priority::max()).unwrap();
Expand Down
72 changes: 22 additions & 50 deletions esp-hal-common/src/embassy/time_driver_timg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@ use crate::{
clock::Clocks,
pac,
prelude::*,
timer::{Instance, TimerGroup},
timer::{Timer, Timer0},
};

pub const ALARM_COUNT: usize = 2;
pub const ALARM_COUNT: usize = 1;

pub type TimerType = Timer<Timer0<TIMG0>>;

pub struct EmbassyTimer {
pub alarms: Mutex<[AlarmState; ALARM_COUNT]>,
pub(crate) alarms: Mutex<[AlarmState; ALARM_COUNT]>,
pub(crate) timer: Mutex<RefCell<Option<TimerType>>>,
}

const ALARM_STATE_NONE: AlarmState = AlarmState::new();

static TG: Mutex<RefCell<Option<TimerGroup<TIMG0>>>> = Mutex::new(RefCell::new(None));

embassy_time::time_driver_impl!(static DRIVER: EmbassyTimer = EmbassyTimer {
alarms: Mutex::new([ALARM_STATE_NONE; ALARM_COUNT]),
timer: Mutex::new(RefCell::new(None)),
});

impl EmbassyTimer {
pub(crate) fn now() -> u64 {
critical_section::with(|cs| TG.borrow_ref(cs).as_ref().unwrap().timer0.now())
critical_section::with(|cs| DRIVER.timer.borrow_ref(cs).as_ref().unwrap().now())
}

pub(crate) fn trigger_alarm(&self, n: usize, cs: CriticalSection) {
Expand All @@ -42,80 +44,50 @@ impl EmbassyTimer {

fn on_interrupt(&self, id: u8) {
critical_section::with(|cs| {
let mut tg = TG.borrow_ref_mut(cs);
let mut tg = self.timer.borrow_ref_mut(cs);
let tg = tg.as_mut().unwrap();
match id {
0 => tg.timer0.clear_interrupt(),
1 => tg.timer1.clear_interrupt(),
_ => unreachable!(),
};
tg.clear_interrupt();
self.trigger_alarm(id as usize, cs);
});
}

pub(crate) fn init(clocks: &Clocks) {
pub fn init(clocks: &Clocks, mut timer: TimerType) {
use crate::{interrupt, interrupt::Priority};

// TODO can we avoid this steal in the future...
let mut tg = TimerGroup::new(unsafe { pac::Peripherals::steal().TIMG0 }, clocks);
// set divider to get a 1mhz clock. abp (80mhz) / 80 = 1mhz... // TODO assert
// abp clock is the source and its at the correct speed for the divider
tg.timer0.set_divider(clocks.apb_clock.to_MHz() as u16);
tg.timer1.set_divider(clocks.apb_clock.to_MHz() as u16);
timer.set_divider(clocks.apb_clock.to_MHz() as u16);

critical_section::with(|cs| TG.borrow_ref_mut(cs).replace(tg));
critical_section::with(|cs| DRIVER.timer.borrow_ref_mut(cs).replace(timer));

interrupt::enable(pac::Interrupt::TG0_T0_LEVEL, Priority::max()).unwrap();
interrupt::enable(pac::Interrupt::TG0_T1_LEVEL, Priority::max()).unwrap();

#[interrupt]
fn TG0_T0_LEVEL() {
DRIVER.on_interrupt(0);
}

#[interrupt]
fn TG0_T1_LEVEL() {
DRIVER.on_interrupt(1);
}
}

pub(crate) fn set_alarm(&self, alarm: embassy_time::driver::AlarmHandle, timestamp: u64) -> bool {
critical_section::with(|cs| {
let now = Self::now();
let alarm_state = unsafe { self.alarms.borrow(cs).get_unchecked(alarm.id() as usize) };
let mut tg = TG.borrow_ref_mut(cs);
let mut tg = self.timer.borrow_ref_mut(cs);
let tg = tg.as_mut().unwrap();
if timestamp < now {
match alarm.id() {
0 => tg.timer0.unlisten(),
1 => tg.timer1.unlisten(),
_ => unreachable!()
}
tg.unlisten();
alarm_state.timestamp.set(u64::MAX);
return false;
}
alarm_state.timestamp.set(timestamp);

match alarm.id() {
0 => {
tg.timer0.load_alarm_value(timestamp);
tg.timer0.listen();
tg.timer0.set_counter_decrementing(false);
tg.timer0.set_auto_reload(false);
tg.timer0.set_counter_active(true);
tg.timer0.set_alarm_active(true);
}
1 => {
tg.timer1.load_alarm_value(timestamp);
tg.timer1.listen();
tg.timer1.set_counter_decrementing(false);
tg.timer1.set_auto_reload(false);
tg.timer1.set_counter_active(true);
tg.timer1.set_alarm_active(true);
}
_ => unreachable!(),
}

tg.load_alarm_value(timestamp);
tg.listen();
tg.set_counter_decrementing(false);
tg.set_auto_reload(false);
tg.set_counter_active(true);
tg.set_alarm_active(true);

true
})
}
Expand Down
2 changes: 1 addition & 1 deletion esp32-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ ufmt = ["esp-hal-common/ufmt"]
vectored = ["esp-hal-common/vectored"]
async = ["esp-hal-common/async", "embedded-hal-async"]
embassy = ["esp-hal-common/embassy"]
embassy-time-timg = ["esp-hal-common/embassy-time-timg", "embassy-time/tick-hz-1_000_000"]
embassy-time-timg0 = ["esp-hal-common/embassy-time-timg0", "embassy-time/tick-hz-1_000_000"]

[[example]]
name = "hello_rgb"
Expand Down
4 changes: 3 additions & 1 deletion esp32-hal/examples/embassy_hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ fn main() -> ! {
let peripherals = Peripherals::take().unwrap();
let system = peripherals.DPORT.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
embassy::init(&clocks);

let mut rtc = Rtc::new(peripherals.RTC_CNTL);
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
Expand All @@ -51,6 +50,9 @@ fn main() -> ! {
wdt0.disable();
wdt1.disable();

#[cfg(feature = "embassy-time-timg0")]
embassy::init(&clocks, timer_group0.timer0);


let executor = EXECUTOR.init(Executor::new());
executor.run(|spawner| {
Expand Down
1 change: 1 addition & 0 deletions esp32c2-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ vectored = ["esp-hal-common/vectored"]
async = ["esp-hal-common/async", "embedded-hal-async"]
embassy = ["esp-hal-common/embassy"]
embassy-time-systick = ["esp-hal-common/embassy-time-systick", "embassy-time/tick-hz-16_000_000"]
embassy-time-timg0 = ["esp-hal-common/embassy-time-timg0", "embassy-time/tick-hz-1_000_000"]

[[example]]
name = "spi_eh1_loopback"
Expand Down
7 changes: 6 additions & 1 deletion esp32c2-hal/examples/embassy_hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ fn main() -> ! {
let peripherals = Peripherals::take().unwrap();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
embassy::init(&clocks);

let mut rtc = Rtc::new(peripherals.RTC_CNTL);
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
Expand All @@ -50,6 +49,12 @@ fn main() -> ! {
rtc.rwdt.disable();
wdt0.disable();

#[cfg(feature = "embassy-time-systick")]
embassy::init(&clocks, esp32c2_hal::systimer::SystemTimer::new(peripherals.SYSTIMER));

#[cfg(feature = "embassy-time-timg0")]
embassy::init(&clocks, timer_group0.timer0);

let executor = EXECUTOR.init(Executor::new());
executor.run(|spawner| {
spawner.spawn(run1()).ok();
Expand Down
1 change: 1 addition & 0 deletions esp32c3-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ allow-opt-level-z = []
async = ["esp-hal-common/async", "embedded-hal-async"]
embassy = ["esp-hal-common/embassy"]
embassy-time-systick = ["esp-hal-common/embassy-time-systick", "embassy-time/tick-hz-16_000_000"]
embassy-time-timg0 = ["esp-hal-common/embassy-time-timg0", "embassy-time/tick-hz-1_000_000"]

[[example]]
name = "hello_rgb"
Expand Down
6 changes: 5 additions & 1 deletion esp32c3-hal/examples/embassy_hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ fn main() -> ! {
let peripherals = Peripherals::take().unwrap();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
embassy::init(&clocks);

let mut rtc = Rtc::new(peripherals.RTC_CNTL);
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
Expand All @@ -52,6 +51,11 @@ fn main() -> ! {
wdt0.disable();
wdt1.disable();

#[cfg(feature = "embassy-time-systick")]
embassy::init(&clocks, esp32c3_hal::systimer::SystemTimer::new(peripherals.SYSTIMER));

#[cfg(feature = "embassy-time-timg0")]
embassy::init(&clocks, timer_group0.timer0);

let executor = EXECUTOR.init(Executor::new());
executor.run(|spawner| {
Expand Down
2 changes: 1 addition & 1 deletion esp32s2-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ embassy = ["esp-hal-common/embassy"]
# - add 80_000_000 support to embassy time
# - Fix https://github.com/esp-rs/esp-hal/issues/253
# embassy-time-systick = ["esp-hal-common/embassy-time-systick", "embassy-time/tick-hz-1_000_000"]
embassy-time-timg = ["esp-hal-common/embassy-time-timg", "embassy-time/tick-hz-1_000_000"]
embassy-time-timg0 = ["esp-hal-common/embassy-time-timg0", "embassy-time/tick-hz-1_000_000"]

[[example]]
name = "hello_rgb"
Expand Down
3 changes: 2 additions & 1 deletion esp32s2-hal/examples/embassy_hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ fn main() -> ! {
let peripherals = Peripherals::take().unwrap();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
embassy::init(&clocks);

let mut rtc = Rtc::new(peripherals.RTC_CNTL);
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
Expand All @@ -52,6 +51,8 @@ fn main() -> ! {
wdt0.disable();
wdt1.disable();

#[cfg(feature = "embassy-time-timg0")]
embassy::init(&clocks, timer_group0.timer0);

let executor = EXECUTOR.init(Executor::new());
executor.run(|spawner| {
Expand Down
2 changes: 1 addition & 1 deletion esp32s3-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ vectored = ["esp-hal-common/vectored"]
async = ["esp-hal-common/async", "embedded-hal-async"]
embassy = ["esp-hal-common/embassy"]
embassy-time-systick = ["esp-hal-common/embassy-time-systick", "embassy-time/tick-hz-16_000_000"]
embassy-time-timg = ["esp-hal-common/embassy-time-timg", "embassy-time/tick-hz-1_000_000"]
embassy-time-timg0 = ["esp-hal-common/embassy-time-timg0", "embassy-time/tick-hz-1_000_000"]

[[example]]
name = "hello_rgb"
Expand Down
Loading

0 comments on commit 23f1ed0

Please sign in to comment.