Skip to content

Commit

Permalink
Extract common setup fn
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 12, 2023
1 parent 1f67e5e commit 7d0733d
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 46 deletions.
8 changes: 8 additions & 0 deletions esp-wifi/src/timer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ mod chip_specific;
mod arch_specific;

pub use chip_specific::*;

pub fn setup_timer_isr(timebase: TimeBase) {
setup_radio_isr();

setup_timer(timebase);

setup_multitasking();
}
4 changes: 3 additions & 1 deletion esp-wifi/src/timer/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ use peripherals::INTPRI as SystemPeripheral;
#[cfg(not(feature = "esp32c6"))]
use peripherals::SYSTEM as SystemPeripheral;

pub type TimeBase = Alarm<Target, 0>;

pub const TICKS_PER_SECOND: u64 = 16_000_000;

const TIMER_DELAY: fugit::HertzU32 = fugit::HertzU32::from_raw(crate::CONFIG.tick_rate_hz);

static ALARM0: Mutex<RefCell<Option<Alarm<Periodic, 0>>>> = Mutex::new(RefCell::new(None));

pub fn setup_timer(systimer: Alarm<Target, 0>) {
pub fn setup_timer(systimer: TimeBase) {
let alarm0 = systimer.into_periodic();
alarm0.set_period(TIMER_DELAY.into());
alarm0.clear_interrupt();
Expand Down
8 changes: 2 additions & 6 deletions esp-wifi/src/timer/timer_esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::hal::{
timer::{Timer, Timer0},
};

pub use super::arch_specific::{get_systimer_count, yield_task, TICKS_PER_SECOND};
pub use super::arch_specific::{get_systimer_count, yield_task, TimeBase, TICKS_PER_SECOND};
use super::arch_specific::{setup_multitasking, setup_timer};

pub fn setup_timer_isr(timg1_timer0: Timer<Timer0<TIMG1>>) {
pub fn setup_radio_isr() {
#[cfg(feature = "wifi")]
unwrap!(interrupt::enable(
peripherals::Interrupt::WIFI_MAC,
Expand All @@ -31,10 +31,6 @@ pub fn setup_timer_isr(timg1_timer0: Timer<Timer0<TIMG1>>) {
interrupt::disable(crate::hal::Cpu::ProCpu, peripherals::Interrupt::ETH_MAC);
interrupt::disable(crate::hal::Cpu::ProCpu, peripherals::Interrupt::UART0);
}

setup_timer(timg1_timer0);

setup_multitasking();
}

#[cfg(feature = "ble")]
Expand Down
8 changes: 2 additions & 6 deletions esp-wifi/src/timer/timer_esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ use crate::{
},
};

pub use super::arch_specific::{get_systimer_count, yield_task, TICKS_PER_SECOND};
pub use super::arch_specific::{get_systimer_count, yield_task, TimeBase, TICKS_PER_SECOND};
use super::arch_specific::{setup_multitasking, setup_timer};

pub fn setup_timer_isr(systimer: Alarm<Target, 0>) {
setup_timer(systimer);

pub fn setup_radio_isr() {
#[cfg(feature = "wifi")]
{
unwrap!(interrupt::enable(
Expand All @@ -38,8 +36,6 @@ pub fn setup_timer_isr(systimer: Alarm<Target, 0>) {
interrupt::Priority::Priority1
));
}

setup_multitasking();
}

#[cfg(feature = "wifi")]
Expand Down
8 changes: 2 additions & 6 deletions esp-wifi/src/timer/timer_esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ use crate::{
},
};

pub use super::arch_specific::{get_systimer_count, yield_task, TICKS_PER_SECOND};
pub use super::arch_specific::{get_systimer_count, yield_task, TimeBase, TICKS_PER_SECOND};
use super::arch_specific::{setup_multitasking, setup_timer};

pub fn setup_timer_isr(systimer: Alarm<Target, 0>) {
setup_timer(systimer);

pub fn setup_radio_isr() {
#[cfg(feature = "wifi")]
{
unwrap!(interrupt::enable(
Expand Down Expand Up @@ -42,8 +40,6 @@ pub fn setup_timer_isr(systimer: Alarm<Target, 0>) {
interrupt::Priority::Priority1
));
}

setup_multitasking();
}

#[cfg(feature = "wifi")]
Expand Down
8 changes: 2 additions & 6 deletions esp-wifi/src/timer/timer_esp32c6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ use crate::{
},
};

pub use super::arch_specific::{get_systimer_count, yield_task, TICKS_PER_SECOND};
pub use super::arch_specific::{get_systimer_count, yield_task, TimeBase, TICKS_PER_SECOND};
use super::arch_specific::{setup_multitasking, setup_timer};

pub fn setup_timer_isr(systimer: Alarm<Target, 0>) {
setup_timer(systimer);

pub fn setup_radio_isr() {
#[cfg(feature = "wifi")]
{
unwrap!(interrupt::enable(
Expand Down Expand Up @@ -48,8 +46,6 @@ pub fn setup_timer_isr(systimer: Alarm<Target, 0>) {
interrupt::Priority::Priority1
));
}

setup_multitasking();
}

#[cfg(feature = "wifi")]
Expand Down
13 changes: 3 additions & 10 deletions esp-wifi/src/timer/timer_esp32s2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@ use crate::hal::{
timer::{Timer, Timer0},
};

pub use super::arch_specific::{get_systimer_count, yield_task, TICKS_PER_SECOND};
pub use super::arch_specific::{get_systimer_count, yield_task, TimeBase, TICKS_PER_SECOND};
use super::arch_specific::{setup_multitasking, setup_timer};

pub fn setup_timer_isr(timg1_timer0: Timer<Timer0<TIMG1>>) {
unwrap!(interrupt::enable(
peripherals::Interrupt::TG1_T0_LEVEL,
interrupt::Priority::Priority2,
));

pub fn setup_radio_isr() {
#[cfg(feature = "wifi")]
{
unwrap!(interrupt::enable(
Expand All @@ -26,9 +21,7 @@ pub fn setup_timer_isr(timg1_timer0: Timer<Timer0<TIMG1>>) {
));
}

setup_timer(timg1_timer0);

setup_multitasking();
// ble not supported
}

#[cfg(feature = "wifi")]
Expand Down
8 changes: 2 additions & 6 deletions esp-wifi/src/timer/timer_esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::hal::{
timer::{Timer, Timer0},
};

pub use super::arch_specific::{get_systimer_count, yield_task, TICKS_PER_SECOND};
pub use super::arch_specific::{get_systimer_count, yield_task, TimeBase, TICKS_PER_SECOND};
use super::arch_specific::{setup_multitasking, setup_timer};

pub fn setup_timer_isr(timg1_timer0: Timer<Timer0<TIMG1>>) {
pub fn setup_radio_isr() {
#[cfg(feature = "wifi")]
{
unwrap!(interrupt::enable(
Expand All @@ -32,10 +32,6 @@ pub fn setup_timer_isr(timg1_timer0: Timer<Timer0<TIMG1>>) {
interrupt::Priority::Priority1,
));
}

setup_timer(timg1_timer0);

setup_multitasking();
}

#[cfg(feature = "wifi")]
Expand Down
11 changes: 6 additions & 5 deletions esp-wifi/src/timer/xtensa.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::cell::RefCell;

use critical_section::Mutex;

use crate::{
hal::{
interrupt,
Expand All @@ -13,9 +15,10 @@ use crate::{
preempt::preempt::task_switch,
};
use atomic_polyfill::{AtomicU32, Ordering};
use critical_section::Mutex;

static TIMER1: Mutex<RefCell<Option<Timer<Timer0<TIMG1>>>>> = Mutex::new(RefCell::new(None));
pub type TimeBase = Timer<Timer0<TIMG1>>;

static TIMER1: Mutex<RefCell<Option<TimeBase>>> = Mutex::new(RefCell::new(None));

static TIMER_OVERFLOWS: AtomicU32 = AtomicU32::new(0);

Expand All @@ -41,9 +44,7 @@ pub fn get_systimer_count() -> u64 {
(((overflow as u64) << 32) + counter_after as u64) * 40_000_000 / 240_000_000
}

pub fn setup_timer(timg1_timer0: Timer<Timer0<TIMG1>>) {
let mut timer1 = timg1_timer0;

pub fn setup_timer(mut timer1: TimeBase) {
unwrap!(interrupt::enable(
peripherals::Interrupt::TG1_T0_LEVEL,
interrupt::Priority::Priority2,
Expand Down

0 comments on commit 7d0733d

Please sign in to comment.