Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for CLIC #1112

Merged
merged 3 commits into from
Jan 25, 2024
Merged

Conversation

bjoernQ
Copy link
Contributor

@bjoernQ bjoernQ commented Jan 25, 2024

This adds support for interrupts on ESP32-P4

You can test it with this (you need to add the critical-section dependency)

#![no_std]
#![no_main]

use core::cell::RefCell;

use critical_section::Mutex;
use esp32p4_hal::{
    gpio::{Gpio35, Input, PullDown, IO},
    interrupt,
    peripherals,
    peripherals::Peripherals,
    prelude::*,
};
use esp_backtrace as _;
use esp_println::println;

static BUTTON: Mutex<RefCell<Option<Gpio35<Input<PullDown>>>>> = Mutex::new(RefCell::new(None));

#[entry]
fn main() -> ! {
    esp_println::logger::init_logger_from_env();
    let peripherals = Peripherals::take();

    let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
    let mut led = io.pins.gpio36.into_push_pull_output();
    let mut button = io.pins.gpio35.into_pull_down_input();

    button.listen(esp32p4_hal::gpio::Event::FallingEdge);

    critical_section::with(|cs| BUTTON.borrow_ref_mut(cs).replace(button));
    interrupt::enable(
        peripherals::Interrupt::GPIO_INT0,
        interrupt::Priority::Priority3,
    )
    .unwrap();

    led.set_high().unwrap();
    loop {
        led.toggle().unwrap();

        #[cfg(debug_assertions)]
        let count = 150000;

        #[cfg(not(debug_assertions))]
        let count = 3500000;

        for _ in 0..count {
            unsafe {
                core::arch::asm!("nop");
            }
        }
        println!("blink!");
    }
}

#[interrupt]
fn GPIO_INT0() {
    critical_section::with(|cs| {
        println!("GPIO interrupt");
        BUTTON
            .borrow_ref_mut(cs)
            .as_mut()
            .unwrap()
            .clear_interrupt();
    });
}

This blinks an LED on GPIO36 and handles an interrupt when using the boot button.

Only external external peripheral interrupts are supported.

Doesn't support "interrupt-preemption" and "direct-vectoring" features - there is apparently support for pre-emption by CLIC and a different way to do direct vectored interrupts. We can explore that later.

Probably the naming of "very_large_intr_status" and "large_intr_status" could be improved. I don't expect to have "extremely_large_intr_status" in future but probably we can come up with better naming in future. (Out of scope of this PR)

Not tested with the second core and anything but the GPIO interrupt for obvious reasons.

Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Mostly looks good, I'd just like to see a comment or two explaining some of the operations.

esp-hal-common/src/interrupt/riscv.rs Outdated Show resolved Hide resolved
Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@bjoernQ bjoernQ added this pull request to the merge queue Jan 25, 2024
Merged via the queue into esp-rs:main with commit de54a17 Jan 25, 2024
18 checks passed
@bjoernQ bjoernQ deleted the feature/esp32p4-interrupts branch January 25, 2024 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants