Skip to content

Commit

Permalink
Fix two more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
thejpster committed Sep 1, 2024
1 parent 03e56ec commit 9e18c29
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
2 changes: 2 additions & 0 deletions rp235x-hal-examples/riscv_examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ block_loop
dht11
gpio_dyn_pin_array
gpio_in_out
gpio_irq_example
i2c
lcd_display
mem_to_mem_dma
Expand All @@ -20,6 +21,7 @@ pio_synchronized
powman_test
pwm_blink
pwm_blink_embedded_hal_1
pwm_irq_input
rom_funcs
rosc_as_system_clock
spi
Expand Down
16 changes: 8 additions & 8 deletions rp235x-hal-examples/src/bin/gpio_irq_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ use rp235x_hal as hal;
// Some things we need
use embedded_hal::digital::StatefulOutputPin;

// Our interrupt macro
use hal::pac::interrupt;

// Some short-cuts to useful types
use core::cell::RefCell;
use critical_section::Mutex;
Expand Down Expand Up @@ -133,7 +130,8 @@ fn main() -> ! {
// We do this last so that the interrupt can't go off while
// it is in the middle of being configured
unsafe {
cortex_m::peripheral::NVIC::unmask(hal::pac::Interrupt::IO_IRQ_BANK0);
hal::arch::interrupt_unmask(hal::pac::Interrupt::IO_IRQ_BANK0);
hal::arch::interrupt_enable();
}

loop {
Expand All @@ -142,21 +140,23 @@ fn main() -> ! {
}
}

#[interrupt]
#[no_mangle]
#[allow(non_snake_case)]
fn IO_IRQ_BANK0() {
// The `#[interrupt]` attribute covertly converts this to `&'static mut Option<LedAndButton>`
static mut LED_AND_BUTTON: Option<LedAndButton> = None;
let led_and_button = unsafe { &mut *core::ptr::addr_of_mut!(LED_AND_BUTTON) };

// This is one-time lazy initialisation. We steal the variables given to us
// via `GLOBAL_PINS`.
if LED_AND_BUTTON.is_none() {
if led_and_button.is_none() {
critical_section::with(|cs| {
*LED_AND_BUTTON = GLOBAL_PINS.borrow(cs).take();
*led_and_button = GLOBAL_PINS.borrow(cs).take();
});
}

// Need to check if our Option<LedAndButtonPins> contains our pins
if let Some(gpios) = LED_AND_BUTTON {
if let Some(gpios) = led_and_button {
// borrow led and button by *destructuring* the tuple
// these will be of type `&mut LedPin` and `&mut ButtonPin`, so we don't have
// to move them back into the static after we use them
Expand Down
23 changes: 11 additions & 12 deletions rp235x-hal-examples/src/bin/pwm_irq_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ use rp235x_hal as hal;
// Some things we need
use embedded_hal::digital::OutputPin;

// Our interrupt macro
use hal::pac::interrupt;

// Shorter alias for gpio and pwm modules
use hal::gpio;
use hal::pwm;
Expand Down Expand Up @@ -143,12 +140,12 @@ fn main() -> ! {
GLOBAL_PINS.borrow(cs).replace(Some((led, input_pin, pwm)));
});

// Unmask the IO_BANK0 IRQ so that the NVIC interrupt controller
// will jump to the interrupt function when the interrupt occurs.
// We do this last so that the interrupt can't go off while
// it is in the middle of being configured
// Unmask the IO_BANK0 IRQ so that the interrupt controller will jump to the
// interrupt function when the interrupt occurs. We do this last so that the
// interrupt can't go off while it is in the middle of being configured
unsafe {
cortex_m::peripheral::NVIC::unmask(hal::pac::Interrupt::IO_IRQ_BANK0);
hal::arch::interrupt_unmask(hal::pac::Interrupt::IO_IRQ_BANK0);
hal::arch::interrupt_enable();
}

loop {
Expand All @@ -157,24 +154,26 @@ fn main() -> ! {
}
}

#[interrupt]
#[no_mangle]
#[allow(non_snake_case)]
fn IO_IRQ_BANK0() {
// The `#[interrupt]` attribute covertly converts this to `&'static mut Option<LedAndInput>`
static mut LED_INPUT_AND_PWM: Option<LedInputAndPwm> = None;
let led_input_and_pwm = unsafe { &mut *core::ptr::addr_of_mut!(LED_INPUT_AND_PWM) };

// This is one-time lazy initialisation. We steal the variables given to us
// via `GLOBAL_PINS`.
if LED_INPUT_AND_PWM.is_none() {
if led_input_and_pwm.is_none() {
critical_section::with(|cs| {
*LED_INPUT_AND_PWM = GLOBAL_PINS.borrow(cs).take();
*led_input_and_pwm = GLOBAL_PINS.borrow(cs).take();
});
}

// Need to check if our Option<LedInputAndPwm> contains our pins and pwm slice
// borrow led, input and pwm by *destructuring* the tuple
// these will be of type `&mut LedPin`, `&mut InputPwmPin` and `&mut PwmSlice`, so we
// don't have to move them back into the static after we use them
if let Some((led, input, pwm)) = LED_INPUT_AND_PWM {
if let Some((led, input, pwm)) = led_input_and_pwm {
// Check if the interrupt source is from the input pin going from high-to-low.
// Note: this will always be true in this example, as that is the only enabled GPIO interrupt source
if input.interrupt_status(gpio::Interrupt::EdgeLow) {
Expand Down

0 comments on commit 9e18c29

Please sign in to comment.