Skip to content

Commit

Permalink
make enable unsafe. Add note about preallocated interrupts in vectore…
Browse files Browse the repository at this point in the history
…d mode.
  • Loading branch information
MabezDev committed Jul 14, 2022
1 parent efe2fec commit 1c19c96
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 100 deletions.
40 changes: 25 additions & 15 deletions esp-hal-common/src/interrupt/xtensa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,31 @@ pub enum CpuInterrupt {
}

/// Enable and assign a peripheral interrupt to an CPU interrupt.
pub fn enable(core: Cpu, interrupt: Interrupt, which: CpuInterrupt) {
unsafe {
let interrupt_number = interrupt as isize;
let cpu_interrupt_number = which as isize;
let intr_map_base = match core {
Cpu::ProCpu => (*core0_interrupt_peripheral()).pro_mac_intr_map.as_ptr(),
#[cfg(feature = "multicore")]
Cpu::AppCpu => (*core1_interrupt_peripheral()).app_mac_intr_map.as_ptr(),
#[cfg(feature = "unicore")]
Cpu::AppCpu => (*core0_interrupt_peripheral()).pro_mac_intr_map.as_ptr(),
};
intr_map_base
.offset(interrupt_number)
.write_volatile(cpu_interrupt_number as u32);
}
///
/// Great care **must** be taken when using with function with interrupt
/// vectoring (enabled by default). Avoid the following CPU interrupts:
/// - Interrupt1LevelPriority1
/// - Interrupt19LevelPriority2
/// - Interrupt23LevelPriority3
/// - Interrupt10EdgePriority1
/// - Interrupt22EdgePriority3
/// As they are preallocated for interrupt vectoring.
///
/// Note: this only maps the interrupt to the CPU interrupt. The CPU interrupt
/// still needs to be enabled afterwards
pub unsafe fn enable(core: Cpu, interrupt: Interrupt, which: CpuInterrupt) {
let interrupt_number = interrupt as isize;
let cpu_interrupt_number = which as isize;
let intr_map_base = match core {
Cpu::ProCpu => (*core0_interrupt_peripheral()).pro_mac_intr_map.as_ptr(),
#[cfg(feature = "multicore")]
Cpu::AppCpu => (*core1_interrupt_peripheral()).app_mac_intr_map.as_ptr(),
#[cfg(feature = "unicore")]
Cpu::AppCpu => (*core0_interrupt_peripheral()).pro_mac_intr_map.as_ptr(),
};
intr_map_base
.offset(interrupt_number)
.write_volatile(cpu_interrupt_number as u32);
}

/// Disable the given peripheral interrupt.
Expand Down
12 changes: 7 additions & 5 deletions esp32-hal/examples/serial_interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ fn main() -> ! {
serial0.listen_at_cmd();
serial0.listen_rx_fifo_full();

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::UART0,
interrupt::CpuInterrupt::Interrupt20LevelPriority2,
);
unsafe {
interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::UART0,
interrupt::CpuInterrupt::Interrupt20LevelPriority2,
);
}

timer0.start(1u64.secs());

Expand Down
24 changes: 13 additions & 11 deletions esp32-hal/examples/timer_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,21 @@ fn main() -> ! {
timer1.disable();
rtc_cntl.set_wdt_global_enable(false);

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::TG0_T0_LEVEL,
interrupt::CpuInterrupt::Interrupt20LevelPriority2,
);
unsafe {
interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::TG0_T0_LEVEL,
interrupt::CpuInterrupt::Interrupt20LevelPriority2,
);

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::TG1_T0_LEVEL,
interrupt::CpuInterrupt::Interrupt23LevelPriority3,
);
}
timer0.start(500u64.millis());
timer0.listen();

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::TG1_T0_LEVEL,
interrupt::CpuInterrupt::Interrupt23LevelPriority3,
);
timer1.start(1u64.secs());
timer1.listen();

Expand Down
2 changes: 1 addition & 1 deletion esp32-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub use esp_hal_common::{
pac,
prelude,
pulse_control,
spi,
serial,
spi,
utils,
Cpu,
Delay,
Expand Down
12 changes: 7 additions & 5 deletions esp32s2-hal/examples/serial_interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ fn main() -> ! {
serial0.listen_at_cmd();
serial0.listen_rx_fifo_full();

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::UART0,
interrupt::CpuInterrupt::Interrupt20LevelPriority2,
);
unsafe {
interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::UART0,
interrupt::CpuInterrupt::Interrupt20LevelPriority2,
);
}

timer0.start(1u64.secs());

Expand Down
36 changes: 18 additions & 18 deletions esp32s2-hal/examples/systimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,25 @@ fn main() -> ! {
(&ALARM0).lock(|data| (*data).replace(Some(alarm0)));
(&ALARM1).lock(|data| (*data).replace(Some(alarm1)));
(&ALARM2).lock(|data| (*data).replace(Some(alarm2)));
}

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::SYSTIMER_TARGET0,
interrupt::CpuInterrupt::Interrupt0LevelPriority1,
);

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::SYSTIMER_TARGET1,
interrupt::CpuInterrupt::Interrupt19LevelPriority2,
);

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::SYSTIMER_TARGET2,
interrupt::CpuInterrupt::Interrupt23LevelPriority3,
);
interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::SYSTIMER_TARGET0,
interrupt::CpuInterrupt::Interrupt0LevelPriority1,
);

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::SYSTIMER_TARGET1,
interrupt::CpuInterrupt::Interrupt19LevelPriority2,
);

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::SYSTIMER_TARGET2,
interrupt::CpuInterrupt::Interrupt23LevelPriority3,
);
}

// Initialize the Delay peripheral, and use it to toggle the LED state in a
// loop.
Expand Down
23 changes: 13 additions & 10 deletions esp32s2-hal/examples/timer_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,22 @@ fn main() -> ! {
timer1.disable();
rtc_cntl.set_wdt_global_enable(false);

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::TG0_T0_LEVEL,
interrupt::CpuInterrupt::Interrupt20LevelPriority2,
);
unsafe {
interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::TG0_T0_LEVEL,
interrupt::CpuInterrupt::Interrupt20LevelPriority2,
);

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::TG1_T0_LEVEL,
interrupt::CpuInterrupt::Interrupt23LevelPriority3,
);
}
timer0.start(500u64.millis());
timer0.listen();

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::TG1_T0_LEVEL,
interrupt::CpuInterrupt::Interrupt23LevelPriority3,
);
timer1.start(1u64.secs());
timer1.listen();

Expand Down
2 changes: 1 addition & 1 deletion esp32s2-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub use esp_hal_common::{
pac,
prelude,
pulse_control,
spi,
serial,
spi,
systimer,
utils,
Cpu,
Expand Down
12 changes: 7 additions & 5 deletions esp32s3-hal/examples/serial_interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ fn main() -> ! {
serial0.listen_at_cmd();
serial0.listen_rx_fifo_full();

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::UART0,
interrupt::CpuInterrupt::Interrupt20LevelPriority2,
);
unsafe {
interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::UART0,
interrupt::CpuInterrupt::Interrupt20LevelPriority2,
);
}

timer0.start(1u64.secs());

Expand Down
36 changes: 18 additions & 18 deletions esp32s3-hal/examples/systimer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,25 @@ fn main() -> ! {
(&ALARM0).lock(|data| (*data).replace(Some(alarm0)));
(&ALARM1).lock(|data| (*data).replace(Some(alarm1)));
(&ALARM2).lock(|data| (*data).replace(Some(alarm2)));
}

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::SYSTIMER_TARGET0,
interrupt::CpuInterrupt::Interrupt0LevelPriority1,
);

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::SYSTIMER_TARGET1,
interrupt::CpuInterrupt::Interrupt19LevelPriority2,
);

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::SYSTIMER_TARGET2,
interrupt::CpuInterrupt::Interrupt23LevelPriority3,
);
interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::SYSTIMER_TARGET0,
interrupt::CpuInterrupt::Interrupt0LevelPriority1,
);

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::SYSTIMER_TARGET1,
interrupt::CpuInterrupt::Interrupt19LevelPriority2,
);

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::SYSTIMER_TARGET2,
interrupt::CpuInterrupt::Interrupt23LevelPriority3,
);
}

// Initialize the Delay peripheral, and use it to toggle the LED state in a
// loop.
Expand Down
23 changes: 13 additions & 10 deletions esp32s3-hal/examples/timer_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,22 @@ fn main() -> ! {
timer1.disable();
rtc_cntl.set_wdt_global_enable(false);

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::TG0_T0_LEVEL,
interrupt::CpuInterrupt::Interrupt20LevelPriority2,
);
unsafe {
interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::TG0_T0_LEVEL,
interrupt::CpuInterrupt::Interrupt20LevelPriority2,
);

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::TG1_T0_LEVEL,
interrupt::CpuInterrupt::Interrupt23LevelPriority3,
);
}
timer0.start(500u64.millis());
timer0.listen();

interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::TG1_T0_LEVEL,
interrupt::CpuInterrupt::Interrupt23LevelPriority3,
);
timer1.start(1u64.secs());
timer1.listen();

Expand Down
2 changes: 1 addition & 1 deletion esp32s3-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub use esp_hal_common::{
pac,
prelude,
pulse_control,
spi,
serial,
spi,
systimer,
usb_serial_jtag,
utils,
Expand Down

0 comments on commit 1c19c96

Please sign in to comment.