From 08e6c56846e11395d95151cea6a4d75e75c7a4bd Mon Sep 17 00:00:00 2001 From: bigbass1997 Date: Wed, 12 Apr 2023 15:54:41 -0500 Subject: [PATCH] Added example of relocating program code from Flash to RAM. --- memory.x | 15 ++++++++++++++- src/main.rs | 30 ++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/memory.x b/memory.x index 070eac7..0e89a21 100644 --- a/memory.x +++ b/memory.x @@ -12,4 +12,17 @@ SECTIONS { { KEEP(*(.boot2)); } > BOOT2 -} INSERT BEFORE .text; \ No newline at end of file +} INSERT BEFORE .text; + +SECTIONS { + .ram_code ORIGIN(RAM) : + { + . = ALIGN(4); + __ram_code_dest_start = .; + KEEP(*(.ram_code)); + . = ALIGN(4); + __ram_code_dest_end = .; + } > RAM AT > FLASH + + __ram_code_src_start = LOADADDR(.ram_code); +} INSERT AFTER .text \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 71b0223..05cdda0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,10 +20,30 @@ use bsp::hal::{ pac, sio::Sio, watchdog::Watchdog, + gpio::{Pin, PushPullOutput, bank0::Gpio25}, }; +use cortex_m::delay::Delay; + +unsafe fn relocate_ram_code() { + extern "C" { + static __ram_code_dest_start: u32; + static __ram_code_dest_end: u32; + static __ram_code_src_start: u32; + } + + let ptr_dest_start = &__ram_code_dest_start as *const u32; + let ptr_dest_end = &__ram_code_dest_end as *const u32; + let ptr_src_start = &__ram_code_src_start as *const u32; + + let length = (ptr_dest_end as u32) - (ptr_dest_start as u32); + + bsp::hal::rom_data::memcpy44(ptr_dest_start as *mut u32, ptr_src_start, length); +} #[entry] fn main() -> ! { + unsafe { relocate_ram_code(); } + info!("Program start"); let mut pac = pac::Peripherals::take().unwrap(); let core = pac::CorePeripherals::take().unwrap(); @@ -44,7 +64,7 @@ fn main() -> ! { .ok() .unwrap(); - let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz()); + let delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz()); let pins = bsp::Pins::new( pac.IO_BANK0, @@ -58,8 +78,14 @@ fn main() -> ! { // Notably, on the Pico W, the LED is not connected to any of the RP2040 GPIOs but to the cyw43 module instead. If you have // a Pico W and want to toggle a LED with a simple GPIO output pin, you can connect an external // LED to one of the GPIO pins, and reference that pin here. - let mut led_pin = pins.led.into_push_pull_output(); + let led_pin = pins.led.into_push_pull_output(); + + foobar(delay, led_pin); +} +#[link_section = ".ram_code"] +fn foobar(mut delay: Delay, mut led_pin: Pin) -> ! { + info!("Executing from {:#010X}", foobar as *const u32 as u32); loop { info!("on!"); led_pin.set_high().unwrap();