From 22151c7d7236b852dd0a331702efde614a830bc2 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 8 Aug 2025 03:37:31 +0800 Subject: [PATCH] partial progress on baremetal bringup --- baremetal/src/platform/cramium/cramium.rs | 13 +- baremetal/src/platform/cramium/debug.rs | 78 +++-- baremetal/src/platform/cramium/link.x | 358 +++++++++++----------- baremetal/src/repl.rs | 57 ++-- 4 files changed, 277 insertions(+), 229 deletions(-) diff --git a/baremetal/src/platform/cramium/cramium.rs b/baremetal/src/platform/cramium/cramium.rs index f2cd86c20..e7b71c12a 100644 --- a/baremetal/src/platform/cramium/cramium.rs +++ b/baremetal/src/platform/cramium/cramium.rs @@ -29,6 +29,18 @@ pub const SYSTEM_CLOCK_FREQUENCY: u32 = 400_000_000; pub const SYSTEM_TICK_INTERVAL_MS: u32 = 1; pub fn early_init() { + let daric_cgu = sysctrl::HW_SYSCTRL_BASE as *mut u32; + /* + let ao_sysctrl = utra::ao_sysctrl::HW_AO_SYSCTRL_BASE as *mut u32; + unsafe { + // this turns off the VDD85D (doesn't work) + // ao_sysctrl.add(utra::ao_sysctrl::SFR_PMUCSR.offset()).write_volatile(0x6c); + + // this sets VDD85D to 0.90V + ao_sysctrl.add(utra::ao_sysctrl::SFR_PMUTRM0CSR.offset()).write_volatile(0x0842_10E0); // 0x0842_1080 default + daric_cgu.add(utra::sysctrl::SFR_IPCARIPFLOW.offset()).write_volatile(0x57); + } + */ let uart = crate::debug::Uart {}; for _ in 0..100 { @@ -37,7 +49,6 @@ pub fn early_init() { unsafe { // this block is mandatory in all cases to get clocks set into some consistent, expected mode { - let daric_cgu = sysctrl::HW_SYSCTRL_BASE as *mut u32; // conservative dividers daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_0.offset()).write_volatile(0x7f7f); daric_cgu.add(utra::sysctrl::SFR_CGUFD_CFGFDCR_0_4_1.offset()).write_volatile(0x7f7f); diff --git a/baremetal/src/platform/cramium/debug.rs b/baremetal/src/platform/cramium/debug.rs index 8cd7f3f4c..4eb2455c9 100644 --- a/baremetal/src/platform/cramium/debug.rs +++ b/baremetal/src/platform/cramium/debug.rs @@ -67,10 +67,16 @@ impl Write for Uart { impl SerialRead for Uart { fn getc(&mut self) -> Option { let uart_buf_addr = crate::platform::UART_IFRAM_ADDR; + #[cfg(feature = "nto-evb")] let mut udma_uart = unsafe { // safety: this is safe to call, because we set up clock and events prior to calling new. udma::Uart::get_handle(utra::udma_uart_1::HW_UDMA_UART_1_BASE, uart_buf_addr, uart_buf_addr) }; + #[cfg(not(feature = "nto-evb"))] + let mut udma_uart = unsafe { + // safety: this is safe to call, because we set up clock and events prior to calling new. + udma::Uart::get_handle(utra::udma_uart_2::HW_UDMA_UART_2_BASE, uart_buf_addr, uart_buf_addr) + }; let mut c = 0u8; udma_uart.read_async(&mut c); @@ -111,26 +117,51 @@ macro_rules! println pub fn setup_rx(perclk: u32) -> cramium_hal::udma::Uart { let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); - iox.set_alternate_function(IoxPort::PD, 13, IoxFunction::AF1); - iox.set_alternate_function(IoxPort::PD, 14, IoxFunction::AF1); - // rx as input, with pull-up - iox.set_gpio_dir(IoxPort::PD, 13, IoxDir::Input); - iox.set_gpio_pullup(IoxPort::PD, 13, IoxEnable::Enable); - // tx as output - iox.set_gpio_dir(IoxPort::PD, 14, IoxDir::Output); - let udma_global = GlobalConfig::new(); - udma_global.clock_on(PeriphId::Uart1); - udma_global.map_event( - PeriphId::Uart1, - PeriphEventType::Uart(EventUartOffset::Rx), - EventChannel::Channel0, - ); - udma_global.map_event( - PeriphId::Uart1, - PeriphEventType::Uart(EventUartOffset::Tx), - EventChannel::Channel1, - ); + #[cfg(feature = "nto-evb")] + { + iox.set_alternate_function(IoxPort::PD, 13, IoxFunction::AF1); + iox.set_alternate_function(IoxPort::PD, 14, IoxFunction::AF1); + // rx as input, with pull-up + iox.set_gpio_dir(IoxPort::PD, 13, IoxDir::Input); + iox.set_gpio_pullup(IoxPort::PD, 13, IoxEnable::Enable); + // tx as output + iox.set_gpio_dir(IoxPort::PD, 14, IoxDir::Output); + + udma_global.clock_on(PeriphId::Uart1); + udma_global.map_event( + PeriphId::Uart1, + PeriphEventType::Uart(EventUartOffset::Rx), + EventChannel::Channel0, + ); + udma_global.map_event( + PeriphId::Uart1, + PeriphEventType::Uart(EventUartOffset::Tx), + EventChannel::Channel1, + ); + } + #[cfg(not(feature = "nto-evb"))] + { + iox.set_alternate_function(IoxPort::PB, 13, IoxFunction::AF1); + iox.set_alternate_function(IoxPort::PB, 14, IoxFunction::AF1); + // rx as input, with pull-up + iox.set_gpio_dir(IoxPort::PB, 13, IoxDir::Input); + iox.set_gpio_pullup(IoxPort::PB, 13, IoxEnable::Enable); + // tx as output + iox.set_gpio_dir(IoxPort::PB, 14, IoxDir::Output); + + udma_global.clock_on(PeriphId::Uart2); + udma_global.map_event( + PeriphId::Uart2, + PeriphEventType::Uart(EventUartOffset::Rx), + EventChannel::Channel0, + ); + udma_global.map_event( + PeriphId::Uart2, + PeriphEventType::Uart(EventUartOffset::Tx), + EventChannel::Channel1, + ); + } let baudrate: u32 = 115200; let freq: u32 = perclk / 2; @@ -139,16 +170,25 @@ pub fn setup_rx(perclk: u32) -> cramium_hal::udma::Uart { // IFRAM0. This is a convention that must be respected by the UDMA UART library implementation // for things to work. let uart_buf_addr = crate::platform::UART_IFRAM_ADDR; + #[cfg(feature = "nto-evb")] let mut udma_uart = unsafe { // safety: this is safe to call, because we set up clock and events prior to calling new. udma::Uart::get_handle(utra::udma_uart_1::HW_UDMA_UART_1_BASE, uart_buf_addr, uart_buf_addr) }; + #[cfg(not(feature = "nto-evb"))] + let mut udma_uart = unsafe { + // safety: this is safe to call, because we set up clock and events prior to calling new. + udma::Uart::get_handle(utra::udma_uart_2::HW_UDMA_UART_2_BASE, uart_buf_addr, uart_buf_addr) + }; udma_uart.set_baud(baudrate, freq); udma_uart.setup_async_read(); // setup interrupt here let mut uart_irq = UartIrq::new(); + #[cfg(feature = "nto-evb")] uart_irq.rx_irq_ena(udma::UartChannel::Uart1, true); + #[cfg(not(feature = "nto-evb"))] + uart_irq.rx_irq_ena(udma::UartChannel::Uart2, true); udma_uart } diff --git a/baremetal/src/platform/cramium/link.x b/baremetal/src/platform/cramium/link.x index 16cee1952..86d3cbf02 100644 --- a/baremetal/src/platform/cramium/link.x +++ b/baremetal/src/platform/cramium/link.x @@ -1,179 +1,179 @@ -MEMORY -{ - FLASH : ORIGIN = 0x61000000, LENGTH = 256k - RAM : ORIGIN = 0x61080000, LENGTH = 1536k -} - -REGION_ALIAS("REGION_TEXT", FLASH); -REGION_ALIAS("REGION_RODATA", FLASH); -REGION_ALIAS("REGION_DATA", RAM); -REGION_ALIAS("REGION_BSS", RAM); -REGION_ALIAS("REGION_STACK", RAM); -REGION_ALIAS("REGION_HEAP", RAM); - -/* Size of the main kernel stack */ -_stack_size = 16K; -_eheap = ORIGIN(RAM) + LENGTH(RAM); - - -PROVIDE(_stext = ORIGIN(REGION_TEXT)); -PROVIDE(_stack_start = ORIGIN(REGION_STACK) + LENGTH(REGION_STACK)); -PROVIDE(_max_hart_id = 0); -PROVIDE(_hart_stack_size = 2K); -PROVIDE(_heap_size = 0); - -PROVIDE(trap_handler = default_trap_handler); - -/* # Pre-initialization function */ -/* If the user overrides this using the `#[pre_init]` attribute or by creating a `__pre_init` function, - then the function this points to will be called before the RAM is initialized. */ -PROVIDE(__pre_init = default_pre_init); - -/* # Multi-processing hook function - fn _mp_hook() -> bool; - - This function is called from all the harts and must return true only for one hart, - which will perform memory initialization. For other harts it must return false - and implement wake-up in platform-dependent way (e.g. after waiting for a user interrupt). -*/ -PROVIDE(_mp_hook = default_mp_hook); - -ENTRY(_start) - -SECTIONS -{ - .text.dummy (NOLOAD) : - { - /* This section is intended to make _stext address work */ - . = ABSOLUTE(_stext); - } > REGION_TEXT - - .text _stext : - { - /* Put reset handler first in .text section so it ends up as the entry */ - /* point of the program. */ - KEEP(*(.text.init)); - KEEP(*(.init)); - KEEP(*(.init.rust)); - . = ALIGN(4); - KEEP(*(.trap)); - KEEP(*(.trap.rust)); - - *(.text .text.*); - } > REGION_TEXT - - .rodata : ALIGN(4) - { - *(.rodata .rodata.*); - - /* 4-byte align the end (VMA) of this section. - This is required by LLD to ensure the LMA of the following .data - section will have the correct alignment. */ - . = ALIGN(4); - _etext = .; - } > REGION_RODATA - - .data : ALIGN(4) - { - _sidata = LOADADDR(.data); - _sdata = .; - /* Must be called __global_pointer$ for linker relaxations to work. */ - PROVIDE(__global_pointer$ = . + 0x800); - *(.sdata .sdata.* .sdata2 .sdata2.*); - *(.data .data.*); - . = ALIGN(4); - _edata = .; - } > REGION_DATA AT > REGION_RODATA - - .bss (NOLOAD) : - { - _sbss = .; - *(.sbss .sbss.* .bss .bss.*); - . = ALIGN(4); - _ebss = .; - } > REGION_BSS - - /* fictitious region that represents the memory available for the stack */ - .stack (NOLOAD) : - { - _sstack = .; - . += _stack_size; - . = ALIGN(4096); - _estack = .; - } > REGION_STACK - - /* fictitious region that represents the memory available for the heap */ - .heap (NOLOAD) : - { - . = ALIGN(4); - _sheap = .; - /* _eheap is defined elsewhere and is the remainder of RAM */ - } > REGION_HEAP - - /* fake output .got section */ - /* Dynamic relocations are unsupported. This section is only used to detect - relocatable code in the input files and raise an error if relocatable code - is found */ - .got (INFO) : - { - KEEP(*(.got .got.*)); - } - - /* Discard .eh_frame, we are not doing unwind on panic so it is not needed */ - /DISCARD/ : - { - *(.eh_frame); - *(.eh_frame_hdr); - } -} - -/* Do not exceed this mark in the error messages above | */ -ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, " -ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned"); - -ASSERT(ORIGIN(REGION_RODATA) % 4 == 0, " -ERROR(riscv-rt): the start of the REGION_RODATA must be 4-byte aligned"); - -ASSERT(ORIGIN(REGION_DATA) % 4 == 0, " -ERROR(riscv-rt): the start of the REGION_DATA must be 4-byte aligned"); - -ASSERT(ORIGIN(REGION_HEAP) % 4 == 0, " -ERROR(riscv-rt): the start of the REGION_HEAP must be 4-byte aligned"); - -ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, " -ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned"); - -ASSERT(ORIGIN(REGION_STACK) % 4 == 0, " -ERROR(riscv-rt): the start of the REGION_STACK must be 4-byte aligned"); - -ASSERT(_stext % 4 == 0, " -ERROR(riscv-rt): `_stext` must be 4-byte aligned"); - -ASSERT(_sdata % 4 == 0 && _edata % 4 == 0, " -BUG(riscv-rt): .data is not 4-byte aligned"); - -ASSERT(_sidata % 4 == 0, " -BUG(riscv-rt): the LMA of .data is not 4-byte aligned"); - -ASSERT(_sbss % 4 == 0 && _ebss % 4 == 0, " -BUG(riscv-rt): .bss is not 4-byte aligned"); - -ASSERT(_sheap % 4 == 0, " -BUG(riscv-rt): start of .heap is not 4-byte aligned"); - -ASSERT(_stext + SIZEOF(.text) < ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT), " -ERROR(riscv-rt): The .text section must be placed inside the REGION_TEXT region. -Set _stext to an address smaller than 'ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT)'"); - -ASSERT(SIZEOF(.stack) > (_max_hart_id + 1) * _hart_stack_size, " -ERROR(riscv-rt): .stack section is too small for allocating stacks for all the harts. -Consider changing `_max_hart_id` or `_hart_stack_size`."); - -ASSERT(SIZEOF(.got) == 0, " -.got section detected in the input files. Dynamic relocations are not -supported. If you are linking to C code compiled using the `gcc` crate -then modify your build script to compile the C code _without_ the --fPIC flag. See the documentation of the `gcc::Config.fpic` method for -details."); - -/* Do not exceed this mark in the error messages above | */ +MEMORY +{ + FLASH : ORIGIN = 0x60000000, LENGTH = 256k + RAM : ORIGIN = 0x61080000, LENGTH = 1536k +} + +REGION_ALIAS("REGION_TEXT", FLASH); +REGION_ALIAS("REGION_RODATA", FLASH); +REGION_ALIAS("REGION_DATA", RAM); +REGION_ALIAS("REGION_BSS", RAM); +REGION_ALIAS("REGION_STACK", RAM); +REGION_ALIAS("REGION_HEAP", RAM); + +/* Size of the main kernel stack */ +_stack_size = 16K; +_eheap = ORIGIN(RAM) + LENGTH(RAM); + + +PROVIDE(_stext = ORIGIN(REGION_TEXT)); +PROVIDE(_stack_start = ORIGIN(REGION_STACK) + LENGTH(REGION_STACK)); +PROVIDE(_max_hart_id = 0); +PROVIDE(_hart_stack_size = 2K); +PROVIDE(_heap_size = 0); + +PROVIDE(trap_handler = default_trap_handler); + +/* # Pre-initialization function */ +/* If the user overrides this using the `#[pre_init]` attribute or by creating a `__pre_init` function, + then the function this points to will be called before the RAM is initialized. */ +PROVIDE(__pre_init = default_pre_init); + +/* # Multi-processing hook function + fn _mp_hook() -> bool; + + This function is called from all the harts and must return true only for one hart, + which will perform memory initialization. For other harts it must return false + and implement wake-up in platform-dependent way (e.g. after waiting for a user interrupt). +*/ +PROVIDE(_mp_hook = default_mp_hook); + +ENTRY(_start) + +SECTIONS +{ + .text.dummy (NOLOAD) : + { + /* This section is intended to make _stext address work */ + . = ABSOLUTE(_stext); + } > REGION_TEXT + + .text _stext : + { + /* Put reset handler first in .text section so it ends up as the entry */ + /* point of the program. */ + KEEP(*(.text.init)); + KEEP(*(.init)); + KEEP(*(.init.rust)); + . = ALIGN(4); + KEEP(*(.trap)); + KEEP(*(.trap.rust)); + + *(.text .text.*); + } > REGION_TEXT + + .rodata : ALIGN(4) + { + *(.rodata .rodata.*); + + /* 4-byte align the end (VMA) of this section. + This is required by LLD to ensure the LMA of the following .data + section will have the correct alignment. */ + . = ALIGN(4); + _etext = .; + } > REGION_RODATA + + .data : ALIGN(4) + { + _sidata = LOADADDR(.data); + _sdata = .; + /* Must be called __global_pointer$ for linker relaxations to work. */ + PROVIDE(__global_pointer$ = . + 0x800); + *(.sdata .sdata.* .sdata2 .sdata2.*); + *(.data .data.*); + . = ALIGN(4); + _edata = .; + } > REGION_DATA AT > REGION_RODATA + + .bss (NOLOAD) : + { + _sbss = .; + *(.sbss .sbss.* .bss .bss.*); + . = ALIGN(4); + _ebss = .; + } > REGION_BSS + + /* fictitious region that represents the memory available for the stack */ + .stack (NOLOAD) : + { + _sstack = .; + . += _stack_size; + . = ALIGN(4096); + _estack = .; + } > REGION_STACK + + /* fictitious region that represents the memory available for the heap */ + .heap (NOLOAD) : + { + . = ALIGN(4); + _sheap = .; + /* _eheap is defined elsewhere and is the remainder of RAM */ + } > REGION_HEAP + + /* fake output .got section */ + /* Dynamic relocations are unsupported. This section is only used to detect + relocatable code in the input files and raise an error if relocatable code + is found */ + .got (INFO) : + { + KEEP(*(.got .got.*)); + } + + /* Discard .eh_frame, we are not doing unwind on panic so it is not needed */ + /DISCARD/ : + { + *(.eh_frame); + *(.eh_frame_hdr); + } +} + +/* Do not exceed this mark in the error messages above | */ +ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_RODATA) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_RODATA must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_DATA) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_DATA must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_HEAP) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_HEAP must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_STACK) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_STACK must be 4-byte aligned"); + +ASSERT(_stext % 4 == 0, " +ERROR(riscv-rt): `_stext` must be 4-byte aligned"); + +ASSERT(_sdata % 4 == 0 && _edata % 4 == 0, " +BUG(riscv-rt): .data is not 4-byte aligned"); + +ASSERT(_sidata % 4 == 0, " +BUG(riscv-rt): the LMA of .data is not 4-byte aligned"); + +ASSERT(_sbss % 4 == 0 && _ebss % 4 == 0, " +BUG(riscv-rt): .bss is not 4-byte aligned"); + +ASSERT(_sheap % 4 == 0, " +BUG(riscv-rt): start of .heap is not 4-byte aligned"); + +ASSERT(_stext + SIZEOF(.text) < ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT), " +ERROR(riscv-rt): The .text section must be placed inside the REGION_TEXT region. +Set _stext to an address smaller than 'ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT)'"); + +ASSERT(SIZEOF(.stack) > (_max_hart_id + 1) * _hart_stack_size, " +ERROR(riscv-rt): .stack section is too small for allocating stacks for all the harts. +Consider changing `_max_hart_id` or `_hart_stack_size`."); + +ASSERT(SIZEOF(.got) == 0, " +.got section detected in the input files. Dynamic relocations are not +supported. If you are linking to C code compiled using the `gcc` crate +then modify your build script to compile the C code _without_ the +-fPIC flag. See the documentation of the `gcc::Config.fpic` method for +details."); + +/* Do not exceed this mark in the error messages above | */ \ No newline at end of file diff --git a/baremetal/src/repl.rs b/baremetal/src/repl.rs index c4519e280..21d68ca06 100644 --- a/baremetal/src/repl.rs +++ b/baremetal/src/repl.rs @@ -241,11 +241,11 @@ impl Repl { // host_fifo_tests + 1 // spi_test - + 1 + // + 1 // i2c_test - + 1 + // + 1 // complex_i2c_test - + 1 + // + 1 // fifo_level_tests + 1 // fifo_alias_tests @@ -283,8 +283,27 @@ impl Repl { passing_tests += bio_tests::units::hello_multiverse(); - bio_tests::dma::dma_filter_off(); + passing_tests += bio_tests::units::hello_world(); + passing_tests += bio_tests::arith::stack_test(); + // safety: this is safe only if the target supports multiplication + passing_tests += unsafe { bio_tests::arith::mac_test() }; // 1 + + passing_tests += bio_tests::units::aclk_tests(); + + passing_tests += bio_tests::units::event_aliases(); + passing_tests += bio_tests::units::fifo_alias_tests(); + + passing_tests += bio_tests::units::fifo_basic(); + passing_tests += bio_tests::units::host_fifo_tests(); + + passing_tests += bio_tests::units::fifo_level_tests(); + + passing_tests += bio_tests::dma::filter_test(); + bio_tests::dma::dma_filter_off(); + passing_tests += bio_tests::dma::dmareq_test(); + + bio_tests::dma::dma_filter_off(); crate::println!("*** CLKMODE 3 ***"); passing_tests += bio_tests::dma::dma_basic(false, 3); // 4 passing_tests += bio_tests::dma::dma_basic(true, 3); // 4 @@ -293,31 +312,9 @@ impl Repl { passing_tests += bio_tests::dma::dma_coincident(3); // 4 passing_tests += bio_tests::dma::dma_multicore(3); // 1 - passing_tests += bio_tests::units::hello_world(); - passing_tests += bio_tests::arith::stack_test(); - - { - // safety: this is safe only if the target supports multiplication - passing_tests += unsafe { bio_tests::arith::mac_test() }; // 1 - } - - passing_tests += bio_tests::units::aclk_tests(); - - passing_tests += bio_tests::dma::filter_test(); - - bio_tests::dma::dma_filter_off(); - passing_tests += bio_tests::dma::dmareq_test(); - passing_tests += bio_tests::units::event_aliases(); - passing_tests += bio_tests::units::fifo_alias_tests(); - - passing_tests += bio_tests::units::fifo_basic(); - passing_tests += bio_tests::units::host_fifo_tests(); - - passing_tests += bio_tests::spi::spi_test(); - passing_tests += bio_tests::i2c::i2c_test(); - passing_tests += bio_tests::i2c::complex_i2c_test(); - - passing_tests += bio_tests::units::fifo_level_tests(); + // passing_tests += bio_tests::spi::spi_test(); + // passing_tests += bio_tests::i2c::i2c_test(); + // passing_tests += bio_tests::i2c::complex_i2c_test(); // Final report crate::println!("\n--- BIO Tests Complete: {}/{} passed. ---\n", passing_tests, BIO_TESTS); @@ -335,7 +332,7 @@ impl Repl { crate::print!(", rram"); #[cfg(not(feature = "cramium-soc"))] crate::print!(", mon"); - #[cfg(not(feature = "nto-bio"))] + #[cfg(feature = "nto-bio")] crate::print!(", bio"); crate::println!(""); }