Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix `psram` availability lookup in `esp-hal-common` build script (#718)
- Fix wrong `dram_seg` length in `esp32s2-hal` linker script (#732)
- Fix setting alarm when a timer group is used as the alarm source. (#730)

### Removed

Expand Down
77 changes: 53 additions & 24 deletions esp-hal-common/src/embassy/time_driver_timg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ use critical_section::{CriticalSection, Mutex};
use peripherals::TIMG0;

use super::AlarmState;
#[cfg(any(esp32, esp32s2, esp32s3))]
use crate::timer::Timer1;
use crate::{
clock::Clocks,
peripherals,
prelude::*,
timer::{Timer, Timer0},
timer::{Instance, Timer, Timer0},
};

#[cfg(not(any(esp32, esp32s2, esp32s3)))]
pub const ALARM_COUNT: usize = 1;
#[cfg(any(esp32, esp32s2, esp32s3))]
pub const ALARM_COUNT: usize = 2;

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

pub struct EmbassyTimer {
pub(crate) alarms: Mutex<[AlarmState; ALARM_COUNT]>,
Expand All @@ -29,7 +30,7 @@ embassy_time::time_driver_impl!(static DRIVER: EmbassyTimer = EmbassyTimer {

impl EmbassyTimer {
pub(crate) fn now() -> u64 {
unsafe { TimerInner::steal() }.now()
unsafe { Timer0::<TIMG0>::steal() }.now()
}

pub(crate) fn trigger_alarm(&self, n: usize, cs: CriticalSection) {
Expand All @@ -41,9 +42,9 @@ impl EmbassyTimer {
}
}

fn on_interrupt(&self, id: u8) {
critical_section::with(|cs| unsafe {
TimerInner::steal().clear_interrupt();
fn on_interrupt<Timer: Instance>(&self, id: u8, mut timer: Timer) {
critical_section::with(|cs| {
timer.clear_interrupt();
self.trigger_alarm(id as usize, cs);
});
}
Expand All @@ -62,13 +63,15 @@ impl EmbassyTimer {

#[interrupt]
fn TG0_T0_LEVEL() {
DRIVER.on_interrupt(0);
let timer = unsafe { Timer0::<TIMG0>::steal() };
DRIVER.on_interrupt(0, timer);
}

#[cfg(any(esp32, esp32s2, esp32s3))]
#[interrupt]
fn TG0_T1_LEVEL() {
DRIVER.on_interrupt(1);
let timer = unsafe { Timer1::<TIMG0>::steal() };
DRIVER.on_interrupt(1, timer);
}
}

Expand All @@ -79,23 +82,49 @@ impl EmbassyTimer {
) -> 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 = unsafe { TimerInner::steal() };
if timestamp < now {
tg.unlisten();
alarm_state.timestamp.set(u64::MAX);
return false;
}
alarm_state.timestamp.set(timestamp);

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);
let n = alarm.id() as usize;
let alarm_state = unsafe { self.alarms.borrow(cs).get_unchecked(n) };

#[cfg(any(esp32, esp32s2, esp32s3))]
if n == 1 {
let tg = unsafe { Timer1::<TIMG0>::steal() };
return Self::set_alarm_impl(tg, alarm_state, now, timestamp);
}

true
let tg = unsafe { Timer0::<TIMG0>::steal() };
Self::set_alarm_impl(tg, alarm_state, now, timestamp)
})
}

fn set_alarm_impl<Timer: Instance>(
tg: Timer,
alarm_state: &AlarmState,
now: u64,
timestamp: u64,
) -> bool {
if timestamp < now {
Self::disarm(tg);
alarm_state.timestamp.set(u64::MAX);
return false;
}
alarm_state.timestamp.set(timestamp);

Self::arm(tg, timestamp);

true
}

fn disarm<Timer: Instance>(mut tg: Timer) {
tg.unlisten();
}

fn arm<Timer: Instance>(mut tg: Timer, timestamp: u64) {
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);
}
}
4 changes: 2 additions & 2 deletions esp-hal-common/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl<TG> Timer0<TG>
where
TG: TimerGroupInstance,
{
#[cfg(feature = "embassy-time-timg0")]
#[allow(unused)]
pub(crate) unsafe fn steal() -> Self {
Self {
phantom: PhantomData,
Expand Down Expand Up @@ -483,7 +483,7 @@ impl<TG> Timer1<TG>
where
TG: TimerGroupInstance,
{
#[cfg(feature = "embassy-time-timg1")]
#[allow(unused)]
pub(crate) unsafe fn steal() -> Self {
Self {
phantom: PhantomData,
Expand Down