Scott Mabin 1789780d06
Xtensa vectored interrupts (#103)
* Xtensa interrupt vectoring: peripheral source

- Initial Xtensa vectoring, updated esp32 gpio example to use new interrupt macro.
- Only peripheral sources supported.
- Only level one priority supported.
- CPU & Edge interrupts still need to be handled.

* Xtensa interrupt vectoring: CPU & EDGE

- Add support for handling CPU interrupts and edge interrupts
- PR required to xtensa-lx-rt for CPU handlers

* Xtensa interrupt vectoring: Priority

- Finally implement priortization
- Only three priorities available at the moment. Xtensa programmer guide
  discourages using highpri interrupts in Rust/C. Guide also mentions
  using software priortization to increase the number of Priorities
  available

* support CPU interrupts, using patch xtensa-lx-rt

* Update example

* Add support & examples for the s2 & s3 too

* Fix formatting and missing imports

* Run interrupt handling in ram, optionally run the vector handler in ram in the examples

* Use xtensa_lx::Mutex CS when enabling interrupts

* Run clippy on each target

* Remove redundant features

* Fix C3 builds

* make enable unsafe. Add note about preallocated interrupts in vectored mode.

* Remove `INTERRUPT_LEVELS` static

The interrupt levels static introduces a few issues
  - A lock is needed when configuring interrupts to keep
    INTERRUPT_LEVELS in a consistent state
  - Interrupts enabled from outside the Rust domain wouldn't be
    serviced, this is the case with the wifi blobs

To remove it, the prioty configuration is now calculated dynamically in
the interrupt handler. Essentially INTERRUPT_LEVELS is now created once
the interrupt triggers. It has some benefits, such as only having to
look at interrupts configured on the current core, not both, but there
is of course an overhead with doing this in the interrupt.

* Allow raw interrupts on levels 4-7, whilst also supporting vectoring on levels 1-3

* rename core number features

* Fix examples and formatting

* use xtensa-lx-rt release, update pacs

* Support passing the trap frame into interrupt handlers

* cfg away the #[interrupt] macro when not using vectoring

* rename enable to map

move vectored feature to chip specific hals

* export vectored functions

- rename `enable_with_priority` to `enable`
- add docs for interrupt macro

* Update all examples to use vectored interrupts
2022-07-25 07:12:34 -07:00

106 lines
2.9 KiB
Rust

//! This shows how to use RTC memory.
//! RTC memory is retained during resets and during most sleep modes.
//! Initialized memory is always re-initialized on startup.
//! Uninitialzed memory isn't initialized on startup and can be used to keep
//! data during resets. Zeroed memory is initialized to zero on startup.
//! We can also run code from RTC memory.
#![no_std]
#![no_main]
use core::fmt::Write;
use esp32_hal::{
clock::ClockControl,
macros::ram,
pac::{Peripherals, UART0},
prelude::*,
timer::TimerGroup,
Serial,
};
use nb::block;
use panic_halt as _;
use xtensa_lx_rt::entry;
#[ram(rtc_fast)]
static mut SOME_INITED_DATA: [u8; 2] = [0xaa, 0xbb];
#[ram(rtc_fast, uninitialized)]
static mut SOME_UNINITED_DATA: [u8; 2] = [0; 2];
#[ram(rtc_fast, zeroed)]
static mut SOME_ZEROED_DATA: [u8; 8] = [0; 8];
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take().unwrap();
let system = peripherals.DPORT.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let mut timer0 = timer_group0.timer0;
let mut wdt = timer_group0.wdt;
let mut serial0 = Serial::new(peripherals.UART0);
// Disable MWDT flash boot protection
wdt.disable();
// The RWDT flash boot protection remains enabled and it being triggered is part
// of the example
timer0.start(1u64.secs());
writeln!(
serial0,
"IRAM function located at {:p}",
function_in_ram as *const ()
)
.unwrap();
unsafe {
writeln!(serial0, "SOME_INITED_DATA {:x?}", SOME_INITED_DATA).unwrap();
writeln!(serial0, "SOME_UNINITED_DATA {:x?}", SOME_UNINITED_DATA).unwrap();
writeln!(serial0, "SOME_ZEROED_DATA {:x?}", SOME_ZEROED_DATA).unwrap();
SOME_INITED_DATA[0] = 0xff;
SOME_ZEROED_DATA[0] = 0xff;
writeln!(serial0, "SOME_INITED_DATA {:x?}", SOME_INITED_DATA).unwrap();
writeln!(serial0, "SOME_UNINITED_DATA {:x?}", SOME_UNINITED_DATA).unwrap();
writeln!(serial0, "SOME_ZEROED_DATA {:x?}", SOME_ZEROED_DATA).unwrap();
if SOME_UNINITED_DATA[0] != 0 {
SOME_UNINITED_DATA[0] = 0;
SOME_UNINITED_DATA[1] = 0;
}
if SOME_UNINITED_DATA[1] == 0xff {
SOME_UNINITED_DATA[1] = 0;
}
writeln!(serial0, "Counter {}", SOME_UNINITED_DATA[1]).unwrap();
SOME_UNINITED_DATA[1] += 1;
}
writeln!(
serial0,
"RTC_FAST function located at {:p}",
function_in_rtc_ram as *const ()
)
.unwrap();
writeln!(serial0, "Result {}", function_in_rtc_ram()).unwrap();
loop {
function_in_ram(&mut serial0);
block!(timer0.wait()).unwrap();
}
}
#[ram]
fn function_in_ram(serial0: &mut Serial<UART0>) {
writeln!(serial0, "Hello world!").unwrap();
}
#[ram(rtc_fast)]
fn function_in_rtc_ram() -> u32 {
42
}