Skip to content

Commit

Permalink
Copy changes from rp2040-hal-examples to rp235x-hal-examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jannic committed Oct 17, 2024
1 parent 29aa62f commit 0d79844
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion rp235x-hal-examples/src/bin/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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 rp235x-hal-examples/src/bin/gpio_irq_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LedAndButton>`
Expand Down
1 change: 1 addition & 0 deletions rp235x-hal-examples/src/bin/multicore_fifo_blink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
Expand Down
1 change: 1 addition & 0 deletions rp235x-hal-examples/src/bin/multicore_polyblink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions rp235x-hal-examples/src/bin/pwm_irq_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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::bank0::Gpio25, gpio::FunctionSio<gpio::SioOutput>, gpio::PullNone>;

Expand Down Expand Up @@ -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<LedAndInput>`
Expand Down
49 changes: 22 additions & 27 deletions rp235x-hal-examples/src/bin/vector_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 @@ -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<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 @@ -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(
Expand Down Expand Up @@ -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 {
Expand All @@ -128,7 +127,7 @@ fn main() -> ! {
delay.delay_ms(5000);
unsafe {
critical_section::with(|_| {
RAM_VTABLE.activate(ppb);
ram_vtable.activate(ppb);
});
}

Expand All @@ -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();
Expand All @@ -153,31 +152,27 @@ 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)));
}
});
}

// 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 0d79844

Please sign in to comment.