Skip to content

Commit

Permalink
Fix several clippy warnings in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jannic committed Oct 17, 2024
1 parent 5ad3adc commit 29aa62f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion rp2040-hal-examples/src/bin/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn main() -> ! {
use core::mem::MaybeUninit;
const HEAP_SIZE: usize = 1024;
static mut HEAP: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { ALLOCATOR.init(HEAP.as_ptr() as usize, HEAP_SIZE) }
unsafe { ALLOCATOR.init(core::ptr::addr_of_mut!(HEAP) as usize, HEAP_SIZE) }
}

// Grab our singleton objects
Expand Down
1 change: 1 addition & 0 deletions rp2040-hal-examples/src/bin/gpio_irq_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ fn main() -> ! {
}
}

#[allow(static_mut_refs)] // See https://github.com/rust-embedded/cortex-m/pull/561
#[interrupt]
fn IO_IRQ_BANK0() {
// The `#[interrupt]` attribute covertly converts this to `&'static mut Option<LedAndButton>`
Expand Down
1 change: 1 addition & 0 deletions rp2040-hal-examples/src/bin/multicore_fifo_blink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ fn main() -> ! {
let mut mc = Multicore::new(&mut pac.PSM, &mut pac.PPB, &mut sio.fifo);
let cores = mc.cores();
let core1 = &mut cores[1];
#[allow(static_mut_refs)]
let _test = core1.spawn(unsafe { &mut CORE1_STACK.mem }, move || {
core1_task(sys_freq)
});
Expand Down
1 change: 1 addition & 0 deletions rp2040-hal-examples/src/bin/multicore_polyblink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ fn main() -> ! {
let mut mc = Multicore::new(&mut pac.PSM, &mut pac.PPB, &mut sio.fifo);
let cores = mc.cores();
let core1 = &mut cores[1];
#[allow(static_mut_refs)]
core1
.spawn(unsafe { &mut CORE1_STACK.mem }, move || {
// Get the second core's copy of the `CorePeripherals`, which are per-core.
Expand Down
1 change: 1 addition & 0 deletions rp2040-hal-examples/src/bin/pwm_irq_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ fn main() -> ! {
}
}

#[allow(static_mut_refs)] // See https://github.com/rust-embedded/cortex-m/pull/561
#[interrupt]
fn IO_IRQ_BANK0() {
// The `#[interrupt]` attribute covertly converts this to `&'static mut Option<LedAndInput>`
Expand Down
1 change: 1 addition & 0 deletions rp2040-hal-examples/src/bin/rtc_irq_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ fn main() -> ! {
}

#[allow(non_snake_case)]
#[allow(static_mut_refs)] // See https://github.com/rust-embedded/cortex-m/pull/561
#[interrupt]
fn RTC_IRQ() {
// The `#[interrupt]` attribute covertly converts this to `&'static mut Option<LedAndRtc>`
Expand Down
43 changes: 19 additions & 24 deletions rp2040-hal-examples/src/bin/vector_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use rp2040_hal as hal;

// A shorter alias for the Peripheral Access Crate
use hal::pac;
use static_cell::ConstStaticCell;

// Some traits we need
use core::cell::RefCell;
Expand All @@ -29,7 +30,7 @@ use rp2040_hal::timer::Alarm;
use rp2040_hal::vector_table::VectorTable;

// Memory that will hold our vector table in RAM
static mut RAM_VTABLE: VectorTable = VectorTable::new();
static RAM_VTABLE: ConstStaticCell<VectorTable> = ConstStaticCell::new(VectorTable::new());

// Give our LED and Alarm a type alias to make it easier to refer to them
type LedAndAlarm = (
Expand All @@ -38,7 +39,7 @@ type LedAndAlarm = (
);

// Place our LED and Alarm type in a static variable, so we can access it from interrupts
static mut LED_AND_ALARM: Mutex<RefCell<Option<LedAndAlarm>>> = Mutex::new(RefCell::new(None));
static LED_AND_ALARM: Mutex<RefCell<Option<LedAndAlarm>>> = Mutex::new(RefCell::new(None));

// Period that each of the alarms will be set for - 1 second and 300ms respectively
const SLOW_BLINK_INTERVAL_US: MicrosDurationU32 = MicrosDurationU32::secs(1);
Expand Down Expand Up @@ -76,12 +77,12 @@ fn main() -> ! {

// Need to make a reference to the Peripheral Base at this scope to avoid confusing the borrow checker
let ppb = &mut pac.PPB;
unsafe {
// Copy the vector table that cortex_m_rt produced into the RAM vector table
RAM_VTABLE.init(ppb);
// Replace the function that is called on Alarm0 interrupts with a new one
RAM_VTABLE.register_handler(pac::Interrupt::TIMER_IRQ_0 as usize, timer_irq0_replacement);
}

let ram_vtable = RAM_VTABLE.take();
// Copy the vector table that cortex_m_rt produced into the RAM vector table
ram_vtable.init(ppb);
// Replace the function that is called on Alarm0 interrupts with a new one
ram_vtable.register_handler(pac::Interrupt::TIMER_IRQ_0 as usize, timer_irq0_replacement);

// Configure the clocks
let clocks = hal::clocks::init_clocks_and_plls(
Expand Down Expand Up @@ -117,9 +118,7 @@ fn main() -> ! {
// Enable generating an interrupt on alarm
alarm.enable_interrupt();
// Move alarm into ALARM, so that it can be accessed from interrupts
unsafe {
LED_AND_ALARM.borrow(cs).replace(Some((led_pin, alarm)));
}
LED_AND_ALARM.borrow(cs).replace(Some((led_pin, alarm)));
});
// Unmask the timer0 IRQ so that it will generate an interrupt
unsafe {
Expand All @@ -130,7 +129,7 @@ fn main() -> ! {
delay.delay_ms(5000);
unsafe {
critical_section::with(|_| {
RAM_VTABLE.activate(ppb);
ram_vtable.activate(ppb);
});
}

Expand All @@ -146,7 +145,7 @@ fn main() -> ! {
fn TIMER_IRQ_0() {
critical_section::with(|cs| {
// Temporarily take our LED_AND_ALARM
let ledalarm = unsafe { LED_AND_ALARM.borrow(cs).take() };
let ledalarm = LED_AND_ALARM.borrow(cs).take();
if let Some((mut led, mut alarm)) = ledalarm {
// Clear the alarm interrupt or this interrupt service routine will keep firing
alarm.clear_interrupt();
Expand All @@ -155,31 +154,27 @@ fn TIMER_IRQ_0() {
// Blink the LED so we know we hit this interrupt
led.toggle().unwrap();
// Return LED_AND_ALARM into our static variable
unsafe {
LED_AND_ALARM
.borrow(cs)
.replace_with(|_| Some((led, alarm)));
}
LED_AND_ALARM
.borrow(cs)
.replace_with(|_| Some((led, alarm)));
}
});
}

// This is the function we will use to replace TIMER_IRQ_0 in our RAM Vector Table
extern "C" fn timer_irq0_replacement() {
critical_section::with(|cs| {
let ledalarm = unsafe { LED_AND_ALARM.borrow(cs).take() };
let ledalarm = LED_AND_ALARM.borrow(cs).take();
if let Some((mut led, mut alarm)) = ledalarm {
// Clear the alarm interrupt or this interrupt service routine will keep firing
alarm.clear_interrupt();
// Schedule a new alarm after FAST_BLINK_INTERVAL_US have passed (300 milliseconds)
let _ = alarm.schedule(FAST_BLINK_INTERVAL_US);
led.toggle().unwrap();
// Return LED_AND_ALARM into our static variable
unsafe {
LED_AND_ALARM
.borrow(cs)
.replace_with(|_| Some((led, alarm)));
}
LED_AND_ALARM
.borrow(cs)
.replace_with(|_| Some((led, alarm)));
}
});
}
Expand Down

0 comments on commit 29aa62f

Please sign in to comment.