Skip to content

Commit

Permalink
Add flexspi support
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaibal-Microsoft committed Mar 4, 2025
1 parent 66949e8 commit e56746c
Show file tree
Hide file tree
Showing 8 changed files with 1,863 additions and 12 deletions.
18 changes: 12 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ default = ["mimxrt685s", "rt"]
## [`rp-pac`](https://docs.rs/crates/rp-pac). This brings in the
## [`cortex-m-rt`](https://docs.rs/cortex-m-rt) crate, which adds
## startup code and minimal runtime initialization.
rt = [
"mimxrt685s-pac?/rt",
"mimxrt633s-pac?/rt",
]
rt = ["mimxrt685s-pac?/rt", "mimxrt633s-pac?/rt"]

## Enable [defmt support](https://docs.rs/defmt) and enables `defmt`
## debug-log messages and formatting in embassy drivers.
Expand Down Expand Up @@ -87,13 +84,22 @@ embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = [
embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
embedded-hal-async = { version = "1.0" }
embedded-hal-nb = { version = "1.0" }
mimxrt600-fcb = "0.1.0"

document-features = "0.2.7"
paste = "1.0"

# PACs
mimxrt685s-pac = { version = "0.2.2", optional = true, features = ["rt", "critical-section", "defmt"] }
mimxrt633s-pac = { version = "0.2.0", optional = true, features = ["rt", "critical-section", "defmt"] }
mimxrt685s-pac = { version = "0.2.2", optional = true, features = [
"rt",
"critical-section",
"defmt",
] }
mimxrt633s-pac = { version = "0.2.0", optional = true, features = [
"rt",
"critical-section",
"defmt",
] }

[dev-dependencies]
embassy-executor = { git = "https://github.com/embassy-rs/embassy" }
Expand Down
2 changes: 2 additions & 0 deletions examples/rt685s-evk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ embedded-hal-async = "1.0.0"
futures = { version = "0.3.30", default-features = false, features = [
"async-await",
] }
embedded-storage = { version = "0.3" }
embedded-storage-async = { version = "0.4.1" }
mimxrt600-fcb = "0.1.0"
rand = { version = "0.8.5", default-features = false }

Expand Down
23 changes: 17 additions & 6 deletions examples/rt685s-evk/memory.x
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,43 @@ MEMORY {
FCB : ORIGIN = 0x08000400, LENGTH = 512
BIV : ORIGIN = 0x08000600, LENGTH = 4
KEYSTORE : ORIGIN = 0x08000800, LENGTH = 2K
FLASH : ORIGIN = 0x08001000, LENGTH = 1M
FLASH : ORIGIN = 0x08001000, LENGTH = 1004K
FLEXSPIFLASH: ORIGIN = 0x080FC000, LENGTH = 10K
RAM : ORIGIN = 0x20080000, LENGTH = 1536K
FLEXSPIRAM: ORIGIN = 0x0, LENGTH = 10K
}


__flexspi_ram_start_addr__ = ORIGIN(FLEXSPIRAM);
__flexspi_flash_start_addr__ = ORIGIN(FLEXSPIFLASH);
__flexspi_flash_end_addr__ = ORIGIN(FLEXSPIFLASH) + LENGTH(FLEXSPIFLASH);


SECTIONS {
.otfad : {
. = ALIGN(4);
KEEP(* (.otfad))
. = ALIGN(4);
} > OTFAD

.fcb : {
. = ALIGN(4);
KEEP(* (.fcb))
. = ALIGN(4);
} > FCB

.biv : {
. = ALIGN(4);
KEEP(* (.biv))
. = ALIGN(4);
} > BIV

.keystore : {
. = ALIGN(4);
KEEP(* (.keystore))
. = ALIGN(4);
} > KEYSTORE
}

.flexspi_code : {
* (.flexspi_code)
} >FLEXSPIRAM AT>FLEXSPIFLASH
}
108 changes: 108 additions & 0 deletions examples/rt685s-evk/src/bin/flexspi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#![no_std]
#![no_main]

use core::arch::asm;

use defmt::*;
use embassy_executor::Spawner;
use embassy_imxrt::flexspi::{FlexSpiBusWidth, FlexSpiFlashPort, FlexSpiFlashPortDeviceInstance, FlexspiStorage};
use embassy_time::Timer;
use embedded_storage::nor_flash::{NorFlash as BlockingNorFlash, ReadNorFlash as BlockingReadNorFlash};
use {defmt_rtt as _, panic_probe as _};

static ADDR: u32 = 0x2F000;

fn copy_flexspi_ramcode_to_ram() {
unsafe {
let mut flash_code_start = 0;
let mut flash_code_end = 0;
let mut ram_code_start = 0;
asm!("ldr {}, =__flexspi_flash_start_addr__", out(reg) flash_code_start);
asm!("ldr {}, =__flexspi_flash_end_addr__", out(reg) flash_code_end);
asm!("ldr {}, =__flexspi_ram_start_addr__", out(reg) ram_code_start);
info!(
"code flash addr start = {:08X} end = {:08X}",
flash_code_start, flash_code_end
);
info!("ram addr start = {:08X}", ram_code_start);
let mut flash_code_start_ptr = flash_code_start as *const u8;
let flash_code_end_ptr = flash_code_end as *const u8;
let mut ram_code_start_ptr = ram_code_start as *mut u8;
loop {
if flash_code_start_ptr >= flash_code_end_ptr {
break;
}
*ram_code_start_ptr = *flash_code_start_ptr;
ram_code_start_ptr = ram_code_start_ptr.add(1);
flash_code_start_ptr = flash_code_start_ptr.add(1);
}
}
}

#[embassy_executor::main]
#[no_mangle]
async fn main(_spawner: Spawner) {
let p = embassy_imxrt::init(Default::default());

let mut read_data = [0; 32];
let mut write_data = [0; 32];

let mut flexspi = FlexspiStorage::new_blocking_xip(
p.FLEXSPI, // FlexSPI peripheral
FlexSpiFlashPort::PortB, // FlexSPI port
FlexSpiFlashPortDeviceInstance::DeviceInstance0, // FlexSPI device instance
);

// Copy the Ram code to .flexspi_code partition
copy_flexspi_ramcode_to_ram();

unsafe { asm!("cpsid i") }

cortex_m::asm::dsb();

flexspi.write_enable();
let status = flexspi.read_status_register();
info!("Status = {:02X} after write enable", status[0]);

flexspi.erase_sector(ADDR);

let status = flexspi.read_status_register();
info!("Status = {:02X} after erase", status[0]);

for i in 0..5 {
flexspi.read(ADDR + i as u32, &mut read_data);
info!("Read data = {:02X} after erase", read_data[0]);
}

flexspi.write_enable();

for i in 0..32 {
let mut temp_array = [0_u8; 1];
temp_array[0] += i;
write_data[i as usize] = temp_array[0] as u8;
flexspi.write(ADDR + i as u32, &temp_array);
flexspi.wait_for_operation_completion();
flexspi.write_enable();
}

for i in 0..32 {
let mut temp_array: [u8; 1] = [0; 1];
flexspi.read(ADDR + i as u32, &mut temp_array);
read_data[i] = temp_array[0];
}

for i in 0..32 {
if read_data[i] != write_data[i] {
crate::panic!("Data mismatch idx = {} addr = {}", i, ADDR + i as u32);
}
}
info!("Data matched");

cortex_m::asm::dsb();

unsafe { asm!("cpsie i") }

loop {
Timer::after_millis(2000).await;
}
}
Loading

0 comments on commit e56746c

Please sign in to comment.