From ea60b60f3ff579cd175abecde49d2d9397420dc0 Mon Sep 17 00:00:00 2001 From: Ian McIntyre Date: Sat, 21 Nov 2026 13:39:55 -0500 Subject: Prototype secondary core support Haven't tested the included examples on hardware. It's just here so I can define an inspect_elf test. Out of tree, there's a basic example on an 1170EVK. --- CHANGELOG.md | 3 + board/Cargo.toml | 5 ++ board/build.rs | 17 +++++ board/src/imxrt1170evk_cm4.rs | 36 ++++++++++ board/src/lib.rs | 6 ++ src/host.rs | 156 ++++++++++++++++++++++++++++++++++++----- src/target.rs | 6 ++ tests/inspect_elf.rs | 158 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 371 insertions(+), 16 deletions(-) create mode 100644 board/src/imxrt1170evk_cm4.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bdd956..f090c46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,9 @@ Add a `.bootrom_reservation` section in OCRAM. If you need to reserve this OCRAM allocation, you may allocate your own `MaybeUninit` buffer sized for your boot ROM's requirements. +Add basic support for linking programs for a secondary core. See +`RuntimeBuilder` documentation for more information and limitations. + All sections now have a 8 byte alignment minimum. ### FlexRAM bank layouts diff --git a/board/Cargo.toml b/board/Cargo.toml index 5ac029c..03ec25f 100644 --- a/board/Cargo.toml +++ b/board/Cargo.toml @@ -55,6 +55,11 @@ imxrt1170evk-cm7 = [ "dep:rtt-target", "dep:panic-rtt-target", ] +imxrt1170evk-cm4 = [ + "imxrt-ral/imxrt1176_cm4", + "dep:rtt-target", + "dep:panic-rtt-target", +] imxrt1170evk-cm7-ram = [ "ram", "imxrt1170evk-cm7", diff --git a/board/build.rs b/board/build.rs index 1f4e207..8709852 100644 --- a/board/build.rs +++ b/board/build.rs @@ -79,6 +79,23 @@ fn main() { .build() .unwrap() } + "imxrt1170evk_cm4" => imxrt_rt::RuntimeBuilder::in_flash( + imxrt_rt::Family::Imxrt1170, + 256 * 1024, + 87 * 1024, + ) + .core(imxrt_rt::Core::Secondary) + .text(imxrt_rt::Memory::Itcm) + .rodata(imxrt_rt::Memory::Dtcm) + .data(imxrt_rt::Memory::Dtcm) + .vectors(imxrt_rt::Memory::Dtcm) + .bss(imxrt_rt::Memory::Dtcm) + .uninit(imxrt_rt::Memory::Dtcm) + .stack(imxrt_rt::Memory::Dtcm) + .heap(imxrt_rt::Memory::Ocram) + .heap_size(2 * 1024) + .build() + .unwrap(), _ => continue, } break; diff --git a/board/src/imxrt1170evk_cm4.rs b/board/src/imxrt1170evk_cm4.rs new file mode 100644 index 0000000..0f6057c --- /dev/null +++ b/board/src/imxrt1170evk_cm4.rs @@ -0,0 +1,36 @@ +//! Support for booting the Cortex M4 on the i.MX RT 1170 EVK. + +use crate::ral; + +#[cfg(target_arch = "arm")] +use panic_rtt_target as _; + +const LED_OFFSET: u32 = 3; + +pub mod rtic_support { + pub use crate::ral::NVIC_PRIO_BITS; + #[allow(non_snake_case)] // For RTIC trickery... + pub mod Interrupt { + pub const PIT: crate::ral::Interrupt = crate::ral::Interrupt::PIT1; + } + pub use Interrupt as interrupt; +} + +pub fn prepare(timer_delay_microseconds: u32) -> Option { + #[cfg(target_arch = "arm")] + rtt_target::rtt_init_print!(); + + let iomuxc = unsafe { ral::iomuxc::IOMUXC::instance() }; + ral::modify_reg!(ral::iomuxc, iomuxc, SW_MUX_CTL_PAD_GPIO_AD_04, MUX_MODE: 5); + + let ccm = unsafe { ral::ccm::CCM::instance() }; + // Enable LPCG for GPIOs. + ral::write_reg!(ral::ccm, ccm, LPCG51_DIRECT, 1); + + let gpio = unsafe { ral::gpio::GPIO3::instance() }; + let pit = crate::prepare_pit(timer_delay_microseconds)?; + Some(crate::Resources { + pit, + led: crate::Led::new(LED_OFFSET, &gpio), + }) +} diff --git a/board/src/lib.rs b/board/src/lib.rs index fa2afcd..0f60c66 100644 --- a/board/src/lib.rs +++ b/board/src/lib.rs @@ -22,6 +22,12 @@ cfg_if::cfg_if! { mod imxrt1170evk_cm7; pub use imxrt1170evk_cm7::*; + } else if #[cfg(feature = "imxrt1170evk-cm4")] { + mod shared { pub mod imxrt11xx; } + use shared::imxrt11xx::prepare_pit; + + mod imxrt1170evk_cm4; + pub use imxrt1170evk_cm4::*; } else { compile_error!("No board feature selected!"); } diff --git a/src/host.rs b/src/host.rs index b9809dd..ab5b50a 100644 --- a/src/host.rs +++ b/src/host.rs @@ -57,6 +57,32 @@ pub enum FlexSpi { FlexSpi2, } +/// The core that runs the program. +/// +/// When supplied, the program's memory map varies. It +/// may also change the inclusion of boot headers. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +#[repr(u32)] +pub enum Core { + /// The primary core for the part. + /// + /// The primary core is the one that usually executes + /// out of power-on reset. Note that this "primary" + /// core assumes that you haven't changed the boot + /// fuses to vary the boot core. + /// + /// This is the only option on 10xx parts. For 11xx + /// parts, this is the Cortex-M7. For 1180 parts, + /// this is the Cortex-M33. + #[default] + Primary, + /// The secondary core, if available. + /// + /// For 11xx parts, this is the Cortex-M4. For 1180 + /// parts, this is the Cortex-M7. + Secondary, +} + impl FlexSpi { fn family_default(family: Family) -> Self { match family { @@ -276,7 +302,7 @@ impl FlexRamEcc { /// stack sizes, heap sizes, and additional configurations. /// /// ``` -/// use imxrt_rt::{Family, RuntimeBuilder, Memory, Mecc64, FlexRamEcc}; +/// use imxrt_rt::{Core, Family, RuntimeBuilder, Memory, Mecc64, FlexRamEcc}; /// /// const FLASH_SIZE: usize = 16 * 1024; /// let family = Family::Imxrt1060; @@ -297,6 +323,7 @@ impl FlexRamEcc { /// b.linker_script_name("imxrt-link.x"); /// b.device_script_name("device.x"); /// b.flexram_ecc(FlexRamEcc::Disable); +/// b.core(Core::Primary); /// b.mecc64(Mecc64::Disable); /// /// assert_eq!(b, RuntimeBuilder::from_flexspi(family, FLASH_SIZE)); @@ -401,6 +428,28 @@ impl FlexRamEcc { /// in between those two allocations, then the runtime allocates the OCRAM region into the /// *dedicated* OCRAM banks. To minimize the impact of this partition, minimize your FlexRAM /// OCRAM banks. +/// +/// # Secondary core +/// +/// Change [`RuntimeBuilder::core`] to link the program for a secondary core, +/// if supported by your family. You're expected to use the primary core to +/// boot the secondary core. You should skip the boot headers when linking the +/// program, using [`RuntimeBuilder::in_flash`]. Generally, the design assumes +/// that the primary core boots first out of power-on reset. +/// +/// The runtime guarantees that the vector table is the first object linked +/// into secondary core's image. Similar to the primary core, the secondary +/// core's reset handler is XiP, and it can relocate itself from flash into its +/// accessible RAM regions. +/// +/// ITCM and DTCM are distinct from the primary core. However, flash and OCRAM +/// are shared. You're responsible for partitioning these regions. Note that +/// the linker script will assume that each core owns the OCRAM memory region, +/// which may result in different objects placed at the same address. +/// +/// FlexRAM, or any flexible TCM configurations, do not apply to a secondary +/// core. You're free to set it, but it's ignored. Additionally, ECC RAM in the +/// secondary core is not yet supported. #[derive(Debug, Clone, PartialEq, Eq)] pub struct RuntimeBuilder { family: Family, @@ -420,6 +469,7 @@ pub struct RuntimeBuilder { device_script_name: String, flexram_ecc: FlexRamEcc, mecc64: Mecc64, + core: Core, } const DEFAULT_LINKER_SCRIPT_NAME: &str = "imxrt-link.x"; @@ -454,6 +504,7 @@ impl RuntimeBuilder { device_script_name: DEFAULT_DEVICE_SCRIPT_NAME.into(), flexram_ecc: FlexRamEcc::Disable, mecc64: Mecc64::Disable, + core: Core::default(), } } @@ -494,6 +545,7 @@ impl RuntimeBuilder { device_script_name: DEFAULT_DEVICE_SCRIPT_NAME.into(), flexram_ecc: FlexRamEcc::Disable, mecc64: Mecc64::Disable, + core: Core::default(), } } @@ -517,6 +569,7 @@ impl RuntimeBuilder { device_script_name: DEFAULT_DEVICE_SCRIPT_NAME.into(), flexram_ecc: FlexRamEcc::Disable, mecc64: Mecc64::Disable, + core: Core::default(), } } @@ -681,6 +734,12 @@ impl RuntimeBuilder { self } + /// Change the target core for the program. + pub fn core(&mut self, core: Core) -> &mut Self { + self.core = core; + self + } + /// Commit the runtime configuration. /// /// `build()` ensures that the generated linker script is available to the @@ -747,6 +806,7 @@ impl RuntimeBuilder { &self.flexram_layout, self.flexram_ecc, self.mecc64, + self.core, )?; if flash_opts.boot_header { @@ -771,6 +831,7 @@ impl RuntimeBuilder { &self.flexram_layout, self.flexram_ecc, self.mecc64, + self.core, )?; } @@ -820,6 +881,7 @@ impl RuntimeBuilder { // The target runtime looks at this value to predicate some pre-init instructions. // Could be helpful for binary identification, but it's an undocumented feature. writeln!(writer, "__imxrt_rt_v0.2 = {:#010X};", self.family.id(),)?; + writeln!(writer, "__imxrt_rt_core = {:#010X};", self.core as u32)?; let link_x = include_bytes!("host/imxrt-link.x"); writer.write_all(link_x)?; @@ -900,6 +962,21 @@ impl RuntimeBuilder { return Err(format!("{:?} doesn't support MECC64", self.family)); } + match self.core { + Core::Primary => { /* Nothing to do */ } + Core::Secondary => { + if !self.family.has_secondary_core() { + return Err(format!("{:?} doesn't have a secondary core", self.family)); + } + if self.mecc64.is_enable() || self.flexram_ecc.is_enable() { + return Err(format!( + "Secondary core of {:?} doesn't yet support ECC RAM", + self.family + )); + } + } + } + Ok(()) } } @@ -914,21 +991,19 @@ fn write_flexram_memories( flexram_layout: &[FlexRamKind], flexram_ecc: FlexRamEcc, mecc64: Mecc64, + core: Core, ) -> io::Result<()> { let itcm_count = layout_count_of(FlexRamKind::Itcm, flexram_layout); let dtcm_count = layout_count_of(FlexRamKind::Dtcm, flexram_layout); let ocram_count = layout_count_of(FlexRamKind::Ocram, flexram_layout); if itcm_count > 0 { - let itcm = family.itcm_start_size(itcm_count); + let itcm = family.itcm_start_size(itcm_count, core); writeln!(output, "ITCM {itcm}")?; } if dtcm_count > 0 { - writeln!( - output, - "DTCM (RWX) : ORIGIN = 0x20000000, LENGTH = {:#X}", - dtcm_count * family.flexram_bank_size(), - )?; + let dtcm = family.dtcm_start_size(dtcm_count, core); + writeln!(output, "DTCM {dtcm}")?; } let ocram = family.ocram_start_size(ocram_count, flexram_ecc, mecc64); @@ -1012,6 +1087,7 @@ fn write_flash_memory_map( flexram_layout: &[FlexRamKind], flexram_ecc: FlexRamEcc, mecc64: Mecc64, + core: Core, ) -> io::Result<()> { writeln!( output, @@ -1025,7 +1101,7 @@ fn write_flash_memory_map( flash_opts.flash_origin(family).expect("Already checked"), flash_opts.size )?; - write_flexram_memories(output, family, flexram_layout, flexram_ecc, mecc64)?; + write_flexram_memories(output, family, flexram_layout, flexram_ecc, mecc64, core)?; writeln!(output, "}}")?; writeln!(output, "__fcb_offset = {:#X};", family.fcb_offset())?; Ok(()) @@ -1041,13 +1117,14 @@ fn write_ram_memory_map( flexram_layout: &[FlexRamKind], flexram_ecc: FlexRamEcc, mecc64: Mecc64, + core: Core, ) -> io::Result<()> { writeln!( output, "/* Memory map for '{family:?}' that executes from RAM. */", )?; writeln!(output, "MEMORY {{")?; - write_flexram_memories(output, family, flexram_layout, flexram_ecc, mecc64)?; + write_flexram_memories(output, family, flexram_layout, flexram_ecc, mecc64, core)?; writeln!(output, "}}")?; Ok(()) } @@ -1093,6 +1170,18 @@ impl Family { Family::Imxrt1180 => 0x1180, } } + const fn has_secondary_core(self) -> bool { + match self { + Family::Imxrt1010 + | Family::Imxrt1015 + | Family::Imxrt1020 + | Family::Imxrt1040 + | Family::Imxrt1050 + | Family::Imxrt1060 + | Family::Imxrt1064 => false, + Family::Imxrt1160 | Family::Imxrt1170 | Family::Imxrt1180 => true, + } + } /// How many FlexRAM banks are available? pub const fn flexram_bank_count(self) -> usize { match self { @@ -1409,9 +1498,37 @@ impl Family { } } + const fn tcm_size(self, banks: usize, core: Core) -> usize { + match core { + Core::Primary => banks * self.flexram_bank_size(), + Core::Secondary => match self { + Family::Imxrt1160 | Family::Imxrt1170 => 128 * 1024, + Family::Imxrt1180 => 256 * 1024, + Family::Imxrt1010 + | Family::Imxrt1015 + | Family::Imxrt1020 + | Family::Imxrt1040 + | Family::Imxrt1050 + | Family::Imxrt1060 + | Family::Imxrt1064 => unreachable!(), + }, + } + } + + /// Returns the start and size of the DTCM memory region. + const fn dtcm_start_size(self, dtcm_banks: usize, core: Core) -> MemoryRegion { + MemoryRegion { + start: 0x2000_0000, + size: self.tcm_size(dtcm_banks, core), + read: true, + write: true, + exec: true, + } + } + /// Returns the start and size of the ITCM memory region. - const fn itcm_start_size(self, itcm_banks: usize) -> MemoryRegion { - let mut itcm_size = itcm_banks * self.flexram_bank_size(); + const fn itcm_start_size(self, itcm_banks: usize, core: Core) -> MemoryRegion { + let mut itcm_size = self.tcm_size(itcm_banks, core); let itcm_start = match self { Family::Imxrt1010 | Family::Imxrt1015 @@ -1422,13 +1539,20 @@ impl Family { | Family::Imxrt1064 | Family::Imxrt1160 | Family::Imxrt1170 => { + let itcm_base = match core { + Core::Primary => 0x0000_0000, + Core::Secondary => 0x1FFE_0000, + }; // Establish a reservation for null pointers. // Note that this reservation is the minimum // size of an MPU region. itcm_size = itcm_size.saturating_sub(32); - 32 + itcm_base + 32 } - Family::Imxrt1180 => 0x10000000 - itcm_size, + Family::Imxrt1180 => match core { + Core::Primary => 0x10000000 - itcm_size, + Core::Secondary => 0x0000_0000, + }, }; MemoryRegion { @@ -1608,7 +1732,7 @@ impl std::fmt::Display for MemoryRegion { #[cfg(test)] mod tests { - use crate::{FlexRamEcc, Mecc64, Memory}; + use crate::{Core, FlexRamEcc, Mecc64, Memory}; use super::{Family, FlexRamBanks, RuntimeBuilder}; use std::{error, io}; @@ -1787,7 +1911,7 @@ mod tests { // at address 0. for family in MOST_FAMILIES { for itcm_banks in 0..=family.flexram_bank_count() { - let itcm = family.itcm_start_size(itcm_banks); + let itcm = family.itcm_start_size(itcm_banks, Core::Primary); assert_eq!(itcm.start, 32); assert_eq!( itcm.size, @@ -1800,7 +1924,7 @@ mod tests { // are properly configured. let family = Family::Imxrt1180; for itcm_banks in 0..=family.flexram_bank_count() { - let itcm = family.itcm_start_size(itcm_banks); + let itcm = family.itcm_start_size(itcm_banks, Core::Primary); assert_ne!(itcm.start, 0); assert_eq!(itcm.size, family.flexram_bank_size() * itcm_banks); } diff --git a/src/target.rs b/src/target.rs index 5e298e6..5cf6215 100644 --- a/src/target.rs +++ b/src/target.rs @@ -92,6 +92,12 @@ global_asm! {r#" .endm __pre_init: + # Are we the secondary core? If so, only + # perform the section copies. + ldr r0, =__imxrt_rt_core + cmp r0, #0 + bne 1000f + ldr r0, =__imxrt_rt_v0.2 @ Need to know which chip family we're initializing. ldr r1, =0x1180 cmp r0, r1 @ Is this an 1180? diff --git a/tests/inspect_elf.rs b/tests/inspect_elf.rs index 7352c8b..e2303fc 100644 --- a/tests/inspect_elf.rs +++ b/tests/inspect_elf.rs @@ -1264,3 +1264,161 @@ fn imxrt1170evk_cm7_secondary() { ); assert_eq!(binary.symbol_value("__eocram_unused").unwrap(), 0x2034_0000); } + +#[test] +#[ignore = "building an example can take time"] +fn imxrt1170evk_cm4() { + const IMAGE_OFFSET: u64 = 87 * 1024; + const CM4_ITCM: u64 = 0x1FFE_0020; + let path = cargo_build("imxrt1170evk-cm4").expect("Unable to build example"); + let contents = fs::read(path).expect("Could not read ELF file"); + let elf = Elf::parse(&contents).expect("Could not parse ELF"); + + let binary = ImxrtBinary::new(&elf, &contents); + assert_eq!(binary.symbol_value("__imxrt_rt_core"), Some(1)); + assert_eq!(binary.symbol_value("__imxrt_rt_v0.2"), Some(0x1170)); + + assert_eq!(binary.symbol_value("__dcd_start"), None); + assert_eq!(binary.symbol_value("__dcd_end"), None); + assert_eq!(binary.symbol_value("__dcd"), None); + assert!(binary.fcb().is_err()); + assert_eq!(binary.flexram_config().unwrap(), 0xFFAAFFAA); + + assert_eq!(binary.symbol_value("__ecc_ram_config").unwrap(), 0); + assert_eq!(binary.symbol_value("__sitcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__eitcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__sdtcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__edtcm_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__socram_unused").unwrap(), 0); + assert_eq!(binary.symbol_value("__eocram_unused").unwrap(), 0); + + assert!( + binary.ivt().is_err(), + "Secondary core image contains boot IVT" + ); + assert!( + binary.section(".boot").is_err(), + "Boot section is included in a secondary core image" + ); + + let stack = binary.section(".stack").unwrap(); + assert_eq!( + Section { + address: DTCM, + size: 8 * 1024 + }, + stack, + "stack not at ORIGIN(DTCM), or not 8 KiB large" + ); + assert_eq!(binary.section_lma(".stack"), stack.address); + + let vector_table = binary.section(".vector_table").unwrap(); + assert_eq!( + Section { + address: stack.address + stack.size, + size: 16 * 4 + IMXRT1170_INTERRUPTS * 4 + }, + vector_table, + "vector table not at expected VMA behind the stack" + ); + assert!( + vector_table.address.is_multiple_of(1024), + "vector table is not 1024-byte aligned" + ); + assert_eq!( + binary.section_lma(".vector_table"), + 0x3000_0000 + IMAGE_OFFSET, + "vector table is the first object in the secondary core's image" + ); + + let xip = binary.section(".xip").unwrap(); + // xip's lma==vma + assert_eq!( + xip.address, + 0x3000_0000 + IMAGE_OFFSET + vector_table.size, + "xip" + ); + assert_eq!( + binary.section_lma(".xip"), + 0x3000_0000 + IMAGE_OFFSET + vector_table.size, + "xip expected behind vector table" + ); + + let text = binary.section(".text").unwrap(); + assert_eq!(text.address, CM4_ITCM, "text in the CM4's ITCM"); + assert_eq!( + binary.section_lma(".text"), + aligned(0x3000_0000 + IMAGE_OFFSET + vector_table.size + xip.size, 8), + "text LMA expected behind xip" + ); + + let rodata = binary.section(".rodata").unwrap(); + assert_eq!( + rodata.address, + vector_table.address + vector_table.size, + "rodata moved to DTCM behind vector table" + ); + assert_eq!( + binary.section_lma(".rodata"), + binary.section_lma(".text") + aligned(text.size, 8), + "rodata LMA starts behind text" + ); + + let data = binary.section(".data").unwrap(); + assert_eq!( + data.address, + rodata.address + aligned(rodata.size, 8), + "data VMA in DTCM behind rodata" + ); + assert_eq!( + data.size, 8, + "blink-rtic expected to have a single static mut u32, padded to 8 bytes" + ); + assert_eq!( + binary.section_lma(".data"), + binary.section_lma(".rodata") + aligned(rodata.size, 8), + "data LMA starts behind rodata" + ); + + let bss = binary.section(".bss").unwrap(); + assert_eq!( + bss.address, + data.address + aligned(data.size, 8), + "bss in DTCM behind data" + ); + assert_eq!(binary.section_lma(".bss"), bss.address, "bss is NOLOAD"); + + let uninit = binary.section(".uninit").unwrap(); + assert_eq!( + uninit.address, + bss.address + aligned(bss.size, 8), + "uninit in DTCM behind bss" + ); + assert_eq!( + binary.section_lma(".uninit"), + uninit.address, + "uninit is NOLOAD" + ); + assert!( + uninit.address + uninit.size <= DTCM + 128 * 1024, + "DTCM sections exceed the CM4's 128 KiB DTCM" + ); + + let heap = binary.section(".heap").unwrap(); + assert_eq!( + Section { + address: 0x2024_0000, + size: 2048, + }, + heap, + "2048 byte heap at the start of OCRAM" + ); + assert_eq!(binary.section_lma(".heap"), heap.address, "Heap is NOLOAD"); + + let increment_data_xip = binary.symbol_value("increment_data").unwrap(); + assert!( + 0x3000_0000 < increment_data_xip && increment_data_xip < 0x4000_0000, + "increment_data is not XiP" + ); + assert!(binary.symbol("Reset").is_some()); +} -- cgit v1.2.3