From 0d7984478cb75604e4fabc9df099bd62953cc7b0 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Thu, 17 Oct 2024 22:28:41 +0000 Subject: [PATCH] Copy changes from rp2040-hal-examples to rp235x-hal-examples --- rp235x-hal-examples/src/bin/alloc.rs | 2 +- .../src/bin/gpio_irq_example.rs | 1 + .../src/bin/multicore_fifo_blink.rs | 1 + .../src/bin/multicore_polyblink.rs | 1 + rp235x-hal-examples/src/bin/pwm_irq_input.rs | 5 +- rp235x-hal-examples/src/bin/vector_table.rs | 49 +++++++++---------- 6 files changed, 29 insertions(+), 30 deletions(-) diff --git a/rp235x-hal-examples/src/bin/alloc.rs b/rp235x-hal-examples/src/bin/alloc.rs index 9c5b3d233..3dd600086 100644 --- a/rp235x-hal-examples/src/bin/alloc.rs +++ b/rp235x-hal-examples/src/bin/alloc.rs @@ -55,7 +55,7 @@ fn main() -> ! { use core::mem::MaybeUninit; const HEAP_SIZE: usize = 1024; static mut HEAP: [MaybeUninit; 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 diff --git a/rp235x-hal-examples/src/bin/gpio_irq_example.rs b/rp235x-hal-examples/src/bin/gpio_irq_example.rs index 03baf9886..04a457405 100644 --- a/rp235x-hal-examples/src/bin/gpio_irq_example.rs +++ b/rp235x-hal-examples/src/bin/gpio_irq_example.rs @@ -142,6 +142,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` diff --git a/rp235x-hal-examples/src/bin/multicore_fifo_blink.rs b/rp235x-hal-examples/src/bin/multicore_fifo_blink.rs index 50a901dc3..de65a83e6 100644 --- a/rp235x-hal-examples/src/bin/multicore_fifo_blink.rs +++ b/rp235x-hal-examples/src/bin/multicore_fifo_blink.rs @@ -107,6 +107,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) }); diff --git a/rp235x-hal-examples/src/bin/multicore_polyblink.rs b/rp235x-hal-examples/src/bin/multicore_polyblink.rs index a8ccfe9a1..96518602d 100644 --- a/rp235x-hal-examples/src/bin/multicore_polyblink.rs +++ b/rp235x-hal-examples/src/bin/multicore_polyblink.rs @@ -99,6 +99,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. diff --git a/rp235x-hal-examples/src/bin/pwm_irq_input.rs b/rp235x-hal-examples/src/bin/pwm_irq_input.rs index 75c428df0..6a411b3f2 100644 --- a/rp235x-hal-examples/src/bin/pwm_irq_input.rs +++ b/rp235x-hal-examples/src/bin/pwm_irq_input.rs @@ -40,7 +40,7 @@ pub static IMAGE_DEF: hal::block::ImageDef = hal::block::ImageDef::secure_exe(); /// 50 Hz PWM servo signals have a pulse width between 1000 us and 2000 us with /// 1500 us as the centre point. us is the abbreviation for micro seconds. - +/// /// The PWM threshold value for turning off the LED in us const LOW_US: u16 = 1475; @@ -53,7 +53,7 @@ const XTAL_FREQ_HZ: u32 = 12_000_000u32; /// Pin types quickly become very long! /// We'll create some type aliases using `type` to help with that - +/// /// This pin will be our output - it will drive an LED if you run this on a Pico type LedPin = gpio::Pin, gpio::PullNone>; @@ -157,6 +157,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` diff --git a/rp235x-hal-examples/src/bin/vector_table.rs b/rp235x-hal-examples/src/bin/vector_table.rs index f975c1966..4149c5677 100644 --- a/rp235x-hal-examples/src/bin/vector_table.rs +++ b/rp235x-hal-examples/src/bin/vector_table.rs @@ -25,9 +25,10 @@ use hal::timer::Alarm; use hal::pac::interrupt; use hal::vector_table::VectorTable; +use static_cell::ConstStaticCell; // Memory that will hold our vector table in RAM -static mut RAM_VTABLE: VectorTable = VectorTable::new(); +static RAM_VTABLE: ConstStaticCell = ConstStaticCell::new(VectorTable::new()); // Give our LED and Alarm a type alias to make it easier to refer to them type LedAndAlarm = ( @@ -36,7 +37,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>> = Mutex::new(RefCell::new(None)); +static LED_AND_ALARM: Mutex>> = 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); @@ -70,15 +71,15 @@ 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( - hal::pac::Interrupt::TIMER0_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( + hal::pac::Interrupt::TIMER0_IRQ_0 as usize, + timer_irq0_replacement, + ); // Configure the clocks let clocks = hal::clocks::init_clocks_and_plls( @@ -115,9 +116,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 { @@ -128,7 +127,7 @@ fn main() -> ! { delay.delay_ms(5000); unsafe { critical_section::with(|_| { - RAM_VTABLE.activate(ppb); + ram_vtable.activate(ppb); }); } @@ -144,7 +143,7 @@ fn main() -> ! { fn TIMER0_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(); @@ -153,11 +152,9 @@ fn TIMER0_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))); } }); } @@ -165,7 +162,7 @@ fn TIMER0_IRQ_0() { // 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(); @@ -173,11 +170,9 @@ extern "C" fn timer_irq0_replacement() { 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))); } }); }