From 782953aceb834f5d0235dcd34c9954d948978351 Mon Sep 17 00:00:00 2001 From: Andelf Date: Fri, 11 Oct 2024 13:36:38 +0800 Subject: [PATCH] feat(hpm62): add demo project --- examples/hpm6200evk/.cargo/config.toml | 30 +++ examples/hpm6200evk/Cargo.toml | 31 +++ examples/hpm6200evk/build.rs | 4 + examples/hpm6200evk/link-fixed.x | 229 ++++++++++++++++++ examples/hpm6200evk/memory-ram.x | 15 ++ examples/hpm6200evk/memory-xip.x | 19 ++ examples/hpm6200evk/memory.x | 19 ++ examples/hpm6200evk/src/bin/embassy_blinky.rs | 54 +++++ examples/hpm6200evk/src/lib.rs | 1 + 9 files changed, 402 insertions(+) create mode 100644 examples/hpm6200evk/.cargo/config.toml create mode 100644 examples/hpm6200evk/Cargo.toml create mode 100644 examples/hpm6200evk/build.rs create mode 100644 examples/hpm6200evk/link-fixed.x create mode 100644 examples/hpm6200evk/memory-ram.x create mode 100644 examples/hpm6200evk/memory-xip.x create mode 100644 examples/hpm6200evk/memory.x create mode 100644 examples/hpm6200evk/src/bin/embassy_blinky.rs create mode 100644 examples/hpm6200evk/src/lib.rs diff --git a/examples/hpm6200evk/.cargo/config.toml b/examples/hpm6200evk/.cargo/config.toml new file mode 100644 index 0000000..2ffb9f1 --- /dev/null +++ b/examples/hpm6200evk/.cargo/config.toml @@ -0,0 +1,30 @@ +[build] +target = "riscv32imafc-unknown-none-elf" + +[target.riscv32imafc-unknown-none-elf] +# runner = 'riscv64-unknown-elf-gdb -x ../../openocd.gdb' +runner = "probe-rs run --chip HPM6280 --protocol jtag --chip-description-path ../../HPMicro.yaml" + +rustflags = [ + # +zba,+zbb,+zbc,+zbs are not available + # Linker scripts: + "-C", + "link-arg=-Tmemory.x", + "-C", + "link-arg=-Tdevice.x", # __VECTORED_INTERRUPTS + "-C", + "link-arg=-Tlink-fixed.x", + "-C", + "link-arg=-Tdefmt.x", + "-C", + "link-arg=-nmagic", + + # "--emit", "obj", + # "--emit", "asm", +] + +[unstable] +build-std = ["core"] + +[env] +DEFMT_LOG = "info" diff --git a/examples/hpm6200evk/Cargo.toml b/examples/hpm6200evk/Cargo.toml new file mode 100644 index 0000000..12a071d --- /dev/null +++ b/examples/hpm6200evk/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "hpm6200evk" +version = "0.1.0" +edition = "2021" + +[dependencies] +hpm-hal = { path = "../..", features = ["rt", "embassy", "hpm6280"] } + +# embedded helper libraries +defmt = "0.3.8" +defmt-rtt = "0.4.1" +panic-halt = "0.2.0" +assign-resources = "0.4.1" +heapless = "0.8.0" + +# embassy dependencies +embassy-time = { version = "0.3.0", features = ["tick-hz-1_000_000"] } +embassy-executor = { version = "0.6.0", features = [ + "nightly", + "integrated-timers", + "arch-riscv32", + "executor-thread", +] } +embassy-embedded-hal = "0.2.0" +embassy-sync = "0.6.0" + +# embedded-hal ecosystem +embedded-hal = "1.0.0" +embedded-hal-async = "1.0.0" +embedded-io = "0.6.1" +embedded-hal-bus = "0.2.0" diff --git a/examples/hpm6200evk/build.rs b/examples/hpm6200evk/build.rs new file mode 100644 index 0000000..ba87614 --- /dev/null +++ b/examples/hpm6200evk/build.rs @@ -0,0 +1,4 @@ +fn main() { + println!("rerun-if-changed=memory.x"); + println!("cargo:rerun-if-changed=link-fixed.x"); +} diff --git a/examples/hpm6200evk/link-fixed.x b/examples/hpm6200evk/link-fixed.x new file mode 100644 index 0000000..d8b3813 --- /dev/null +++ b/examples/hpm6200evk/link-fixed.x @@ -0,0 +1,229 @@ +/* Temp fix for https://github.com/rust-embedded/riscv/issues/196 */ + +/* # Developer notes + +- Symbols that start with a double underscore (__) are considered "private" + +- Symbols that start with a single underscore (_) are considered "semi-public"; they can be + overridden in a user linker script, but should not be referred from user code (e.g. `extern "C" { + static mut _heap_size }`). + +- `EXTERN` forces the linker to keep a symbol in the final binary. We use this to make sure a + symbol if not dropped if it appears in or near the front of the linker arguments and "it's not + needed" by any of the preceding objects (linker arguments) + +- `PROVIDE` is used to provide default values that can be overridden by a user linker script + +- In this linker script, you may find symbols that look like `${...}` (e.g., `4`). + These are wildcards used by the `build.rs` script to adapt to different target particularities. + Check `build.rs` for more details about these symbols. + +- On alignment: it's important for correctness that the VMA boundaries of both .bss and .data *and* + the LMA of .data are all `4`-byte aligned. These alignments are assumed by the RAM + initialization routine. There's also a second benefit: `4`-byte aligned boundaries + means that you won't see "Address (..) is out of bounds" in the disassembly produced by `objdump`. +*/ + +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(InstructionMisaligned = ExceptionHandler); +PROVIDE(InstructionFault = ExceptionHandler); +PROVIDE(IllegalInstruction = ExceptionHandler); +PROVIDE(Breakpoint = ExceptionHandler); +PROVIDE(LoadMisaligned = ExceptionHandler); +PROVIDE(LoadFault = ExceptionHandler); +PROVIDE(StoreMisaligned = ExceptionHandler); +PROVIDE(StoreFault = ExceptionHandler);; +PROVIDE(UserEnvCall = ExceptionHandler); +PROVIDE(SupervisorEnvCall = ExceptionHandler); +PROVIDE(MachineEnvCall = ExceptionHandler); +PROVIDE(InstructionPageFault = ExceptionHandler); +PROVIDE(LoadPageFault = ExceptionHandler); +PROVIDE(StorePageFault = ExceptionHandler); + +PROVIDE(SupervisorSoft = DefaultHandler); +PROVIDE(MachineSoft = DefaultHandler); +PROVIDE(SupervisorTimer = DefaultHandler); +PROVIDE(MachineTimer = DefaultHandler); +PROVIDE(SupervisorExternal = DefaultHandler); +PROVIDE(MachineExternal = DefaultHandler); + +PROVIDE(DefaultHandler = DefaultInterruptHandler); +PROVIDE(ExceptionHandler = DefaultExceptionHandler); + +/* # 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); + +/* A PAC/HAL defined routine that should initialize custom interrupt controller if needed. */ +PROVIDE(_setup_interrupts = default_setup_interrupts); + +/* # 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); + +/* # Start trap function override + By default uses the riscv crates default trap handler + but by providing the `_start_trap` symbol external crates can override. +*/ +PROVIDE(_start_trap = default_start_trap); + +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(*(.init)); + KEEP(*(.init.rust)); + . = ALIGN(4); + *(.trap); + *(.trap.rust); + *(.text.abort); + *(.text .text.*); + } > REGION_TEXT + + /* In-ILM code section */ + .fast : ALIGN(4) + { + _sifast = LOADADDR(.fast); + _sfast = .; + /** CAUTION: + ILM0 starts at 0x00000000. Using this as an IRQ handler address results in `None` when cast to `Option`. + The IRQ handler will not be called. + */ + KEEP(*(.vector_table.interrupts)); + *(.fast .fast.*); + . = ALIGN(4); + PROVIDE(_efast= .); /* No idea why `PROVIDE` is needed here */ + } > REGION_FASTTEXT AT > REGION_RODATA + + .rodata : ALIGN(4) + { + *(.srodata .srodata.*); + *(.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); + } > 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) : ALIGN(4) + { + _sbss = .; + *(.sbss .sbss.* .bss .bss.*); + . = ALIGN(4); + _ebss = .; + } > REGION_BSS + + /* fictitious region that represents the memory available for the heap */ + .heap (NOLOAD) : + { + _sheap = .; + . += _heap_size; + . = ALIGN(4); + _eheap = .; + } > REGION_HEAP + + /* fictitious region that represents the memory available for the stack */ + .stack (NOLOAD) : + { + _estack = .; + . = ABSOLUTE(_stack_start); + _sstack = .; + } > REGION_STACK + + /* 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.*)); + } + + /*.eh_frame (INFO) : { KEEP(*(.eh_frame)) } + .eh_frame_hdr (INFO) : { *(.eh_frame_hdr) }*/ + .eh_frame : { KEEP(*(.eh_frame)) } > REGION_TEXT + .eh_frame_hdr : { *(.eh_frame_hdr) } > REGION_TEXT +} + +/* 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 | */ diff --git a/examples/hpm6200evk/memory-ram.x b/examples/hpm6200evk/memory-ram.x new file mode 100644 index 0000000..648ae17 --- /dev/null +++ b/examples/hpm6200evk/memory-ram.x @@ -0,0 +1,15 @@ +MEMORY +{ + ILM0 : ORIGIN = 0x00000000, LENGTH = 128K /* instruction local memory */ + DLM0 : ORIGIN = 0x00080000, LENGTH = 128K /* data local memory */ + + AXI_SRAM : ORIGIN = 0x01080000, LENGTH = 256K + AHB_SRAM : ORIGIN = 0xF0300000, LENGTH = 32K +} +REGION_ALIAS("REGION_TEXT", ILM0); +REGION_ALIAS("REGION_RODATA", ILM0); +REGION_ALIAS("REGION_DATA", DLM0); +REGION_ALIAS("REGION_BSS", DLM0) +REGION_ALIAS("REGION_HEAP", DLM0); +REGION_ALIAS("REGION_STACK", DLM0); +REGION_ALIAS("REGION_FASTTEXT", ILM0); diff --git a/examples/hpm6200evk/memory-xip.x b/examples/hpm6200evk/memory-xip.x new file mode 100644 index 0000000..f8c85c1 --- /dev/null +++ b/examples/hpm6200evk/memory-xip.x @@ -0,0 +1,19 @@ +MEMORY +{ + XPI0_HEADER : ORIGIN = 0x80000000, LENGTH = 0x3000 /* bootheader */ + XPI0_APP : ORIGIN = 0x80003000, LENGTH = 16M - 0x3000 /* app firmware */ + + ILM0 : ORIGIN = 0x00000000, LENGTH = 128K /* instruction local memory */ + DLM0 : ORIGIN = 0x00080000, LENGTH = 128K /* data local memory */ + + AXI_SRAM : ORIGIN = 0x01080000, LENGTH = 256K + AHB_SRAM : ORIGIN = 0xF0300000, LENGTH = 32K +} + +REGION_ALIAS("REGION_TEXT", XPI0_APP); +REGION_ALIAS("REGION_RODATA", XPI0_APP); +REGION_ALIAS("REGION_DATA", DLM0); +REGION_ALIAS("REGION_BSS", DLM0) +REGION_ALIAS("REGION_HEAP", DLM0); +REGION_ALIAS("REGION_STACK", DLM0); +REGION_ALIAS("REGION_FASTTEXT", ILM0); diff --git a/examples/hpm6200evk/memory.x b/examples/hpm6200evk/memory.x new file mode 100644 index 0000000..f8c85c1 --- /dev/null +++ b/examples/hpm6200evk/memory.x @@ -0,0 +1,19 @@ +MEMORY +{ + XPI0_HEADER : ORIGIN = 0x80000000, LENGTH = 0x3000 /* bootheader */ + XPI0_APP : ORIGIN = 0x80003000, LENGTH = 16M - 0x3000 /* app firmware */ + + ILM0 : ORIGIN = 0x00000000, LENGTH = 128K /* instruction local memory */ + DLM0 : ORIGIN = 0x00080000, LENGTH = 128K /* data local memory */ + + AXI_SRAM : ORIGIN = 0x01080000, LENGTH = 256K + AHB_SRAM : ORIGIN = 0xF0300000, LENGTH = 32K +} + +REGION_ALIAS("REGION_TEXT", XPI0_APP); +REGION_ALIAS("REGION_RODATA", XPI0_APP); +REGION_ALIAS("REGION_DATA", DLM0); +REGION_ALIAS("REGION_BSS", DLM0) +REGION_ALIAS("REGION_HEAP", DLM0); +REGION_ALIAS("REGION_STACK", DLM0); +REGION_ALIAS("REGION_FASTTEXT", ILM0); diff --git a/examples/hpm6200evk/src/bin/embassy_blinky.rs b/examples/hpm6200evk/src/bin/embassy_blinky.rs new file mode 100644 index 0000000..5b08b01 --- /dev/null +++ b/examples/hpm6200evk/src/bin/embassy_blinky.rs @@ -0,0 +1,54 @@ +#![no_main] +#![no_std] +#![feature(type_alias_impl_trait)] +#![feature(impl_trait_in_assoc_type)] + +use defmt::println; +use embassy_executor::Spawner; +use embassy_time::Timer; +use hal::gpio::{AnyPin, Flex, Pin}; +use hal::pac::MCHTMR; +use {defmt_rtt as _, hpm_hal as hal}; + +#[embassy_executor::task(pool_size = 2)] +async fn blink(pin: AnyPin, interval: u64) { + let mut led = Flex::new(pin); + led.set_as_output(Default::default()); + led.set_high(); + + loop { + led.toggle(); + + Timer::after_millis(interval).await; + } +} + +#[embassy_executor::main(entry = "hpm_hal::entry")] +async fn main(spawner: Spawner) -> ! { + let p = hal::init(Default::default()); + + println!("Rust SDK: hpm-hal v0.0.1"); + println!("Embassy driver: hpm-hal v0.0.1"); + + println!("cpu0:\t\t {}Hz", hal::sysctl::clocks().cpu0.0); + println!("ahb:\t\t {}Hz", hal::sysctl::clocks().ahb.0); + println!("=============================="); + + println!("Hello, world!"); + + spawner.spawn(blink(p.PA27.degrade(), 500)).unwrap(); + spawner.spawn(blink(p.PB01.degrade(), 300)).unwrap(); + + loop { + Timer::after_millis(1000).await; + + defmt::info!("tick {}", MCHTMR.mtime().read()); + } +} + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + //let _ = println!("\n\n\n{}", info); + + loop {} +} diff --git a/examples/hpm6200evk/src/lib.rs b/examples/hpm6200evk/src/lib.rs new file mode 100644 index 0000000..0c9ac1a --- /dev/null +++ b/examples/hpm6200evk/src/lib.rs @@ -0,0 +1 @@ +#![no_std]