From 840efacd51a7c2894eec0f18e05360503159ef72 Mon Sep 17 00:00:00 2001 From: wangmihu Date: Mon, 19 Jul 2021 17:44:29 +0800 Subject: [PATCH 1/3] systick work ok --- link.x | 6 +-- src/main.rs | 109 ++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 85 insertions(+), 30 deletions(-) diff --git a/link.x b/link.x index e1ae7ba..e58c01d 100755 --- a/link.x +++ b/link.x @@ -70,11 +70,7 @@ SECTIONS */ .isr_vector : ALIGN(4) { - //FILL(0xFF) - LONG(__stack); - //__vectors_start = ABSOLUTE(.) ; - // __vectors_start__ = ABSOLUTE(.) ; /* STM specific definition */ - KEEP(*(.isr_vector.reset_vector)) /* Interrupt vectors */ + . = ALIGN(4); KEEP(*(.isr_vector)) /* Interrupt vectors */ KEEP(*(.cfmconfig)) /* Freescale configuration words */ diff --git a/src/main.rs b/src/main.rs index 2a2b213..baf85d8 100755 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ #![no_main] #![no_std] +#![allow(dead_code)] +#![allow(non_upper_case_globals)] use panic_halt as _; @@ -12,11 +14,48 @@ use core::{ ptr::{read, write_volatile}, }; -#[link_section = ".isr_vector.reset_vector"] -#[no_mangle] -pub static __RESET_VECTOR: fn() -> ! = reset_handler; +extern "C" { + fn _estack(); +} + +pub unsafe extern "C" fn svc_handler() { + hprintln!("svc_handler! ").unwrap(); +} + +pub unsafe extern "C" fn pendsv_handler() { + hprintln!("pendsv_handler! ").unwrap(); +} + +pub unsafe extern "C" fn systick_handler() { + hprintln!("systick_handler! ").unwrap(); +} + +pub unsafe extern "C" fn default_handler() { + hprintln!("default_handler! ").unwrap(); +} -pub fn reset_handler() -> ! { +#[link_section = ".isr_vector"] +#[used] +pub static BASE_VECTORS: [unsafe extern "C" fn(); 16] = [ + _estack, + reset_handler, + default_handler, // NMI + default_handler, // Hard Fault + default_handler, // MemManage + default_handler, // BusFault + default_handler, // UsageFault + default_handler, + default_handler, + default_handler, + default_handler, + svc_handler, // SVC + default_handler, // DebugMon + default_handler, + pendsv_handler, // PendSV + systick_handler, // SysTick +]; + +pub unsafe extern "C" fn reset_handler() { extern "C" { // These symbols come from `linker.ld` static mut _sbss: u32; // Start of .bss section @@ -24,41 +63,61 @@ pub fn reset_handler() -> ! { static mut _sdata: u32; // Start of .data section static mut _edata: u32; // End of .data section static __rodata_regions_array_start: u32; // Start of .rodata section + static mut __vectors_start:u32; } + + //unsafe{*(SCB_VTOR as *mut u32) = &mut __vectors_start}; + // Initialize (Zero) BSS - unsafe { - let mut sbss: *mut u32 = &mut _sbss; - let ebss: *mut u32 = &mut _ebss; - - while sbss < ebss { - write_volatile(sbss, zeroed()); - sbss = sbss.offset(1); - } - } + + let mut sbss: *mut u32 = &mut _sbss; + let ebss: *mut u32 = &mut _ebss; + + while sbss < ebss { + write_volatile(sbss, zeroed()); + sbss = sbss.offset(1); + } + // Initialize Data - unsafe { - let mut sdata: *mut u32 = &mut _sdata; - let edata: *mut u32 = &mut _edata; - let mut sidata: *const u32 = &__rodata_regions_array_start; - - while sdata < edata { - write_volatile(sdata, read(sidata)); - sdata = sdata.offset(1); - sidata = sidata.offset(1); - } - } + + let mut sdata: *mut u32 = &mut _sdata; + let edata: *mut u32 = &mut _edata; + let mut sidata: *const u32 = &__rodata_regions_array_start; + + while sdata < edata { + write_volatile(sdata, read(sidata)); + sdata = sdata.offset(1); + sidata = sidata.offset(1); + } + // Call user's main function main() } +//systick config +pub const SystemCoreClock:u32 = 168000000; +pub const SysTick:u32 = 0xE000E010; +pub const SysTickCTL:u32 = SysTick; +pub const SysTickLoad:u32 = SysTick + 0x4; +pub const SysTickVal:u32 = SysTick + 0x8; + +fn systick_config() +{ + unsafe{*(SysTickLoad as *mut u32) = SystemCoreClock / 1000 - 1}; + unsafe{*(SysTickVal as *mut u32) = 0}; + unsafe{*(SysTickCTL as *mut u32) = 1 | 1 << 1 | 1 << 2}; +} fn main() -> ! { hprintln!("Hello,liteos! ").unwrap(); - // exit QEMU + //systick can work; + //systick_config(); + + // exit QEMU // NOTE do not run this on hardware; it can corrupt OpenOCD state debug::exit(debug::EXIT_SUCCESS); -- Gitee From 16ed61c1ec3feadfb96ed6d9595419ec65fe6e85 Mon Sep 17 00:00:00 2001 From: evilbinary Date: Sun, 8 Aug 2021 11:04:36 +0800 Subject: [PATCH 2/3] add raspberrypi --- .cargo/config.toml | 6 ++++-- Cargo.lock | 13 +++++++++++++ Cargo.toml | 4 ++++ README.md | 2 +- boards/raspberrypi/Cargo.toml | 12 ++++++++++++ boards/raspberrypi/src/main.rs | 9 +++++++++ boards/stm32f4xx/Cargo.toml | 1 + kernel/arch/arm/cortex-a7/Cargo.toml | 8 ++++++++ kernel/arch/arm/cortex-a7/src/lib.rs | 8 ++++++++ 9 files changed, 60 insertions(+), 3 deletions(-) create mode 100755 boards/raspberrypi/Cargo.toml create mode 100644 boards/raspberrypi/src/main.rs create mode 100644 kernel/arch/arm/cortex-a7/Cargo.toml create mode 100644 kernel/arch/arm/cortex-a7/src/lib.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index a249dee..67051db 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -8,7 +8,8 @@ # runner = "arm-none-eabi-gdb -q -x openocd.gdb" # runner = "gdb-multiarch -q -x openocd.gdb" # runner = "gdb -q -x openocd.gdb" - runner = "qemu-system-gnuarmeclipse --verbose --verbose --board STM32F4-Discovery --mcu STM32F407VG -d unimp,guest_errors --nographic --semihosting-config enable=on,target=native --image " + # runner = "qemu-system-gnuarmeclipse --verbose --verbose --board STM32F4-Discovery --mcu STM32F407VG -d unimp,guest_errors --nographic --semihosting-config enable=on,target=native --image " +runner = "qemu-system-arm -M raspi2 -kernel " rustflags = [ # This is needed if your flash or ram addresses are not aligned to 0x10000 in memory.x @@ -34,8 +35,9 @@ rustflags = [ # Pick ONE of these compilation targets # target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+ #target = "thumbv7m-none-eabi" # Cortex-M3 -target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU) +# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU) # target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU) # target = "thumbv8m.base-none-eabi" # Cortex-M23 # target = "thumbv8m.main-none-eabi" # Cortex-M33 (no FPU) # target = "thumbv8m.main-none-eabihf" # Cortex-M33 (with FPU) +target = "armv7a-none-eabi" diff --git a/Cargo.lock b/Cargo.lock index 0e915a7..483ee6d 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,6 +52,10 @@ checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" name = "cmsis" version = "0.1.0" +[[package]] +name = "cortex-a7" +version = "0.1.0" + [[package]] name = "cortex-m" version = "0.6.7" @@ -220,6 +224,15 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2a38df5b15c8d5c7e8654189744d8e396bddc18ad48041a500ce52d6948941f" +[[package]] +name = "raspberrypi" +version = "0.1.0" +dependencies = [ + "kernel", + "panic-halt", + "utils", +] + [[package]] name = "riscv" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 2e49d11..55a52e1 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,11 +4,14 @@ members = [ "kal/posix", "kernel/source", "kernel/arch/arm/cortex-m4", + "kernel/arch/arm/cortex-a7", "kernel/arch/riscv", "boards/stm32f4xx", + "boards/raspberrypi", "components/exchook", "utils", ] + exclude = [ ] @@ -19,6 +22,7 @@ lto = false opt-level = "z" debug = true + [profile.release] panic = "abort" lto = true diff --git a/README.md b/README.md index f36b86f..d092095 100755 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ rust stable安装 rust nightly安装(使用嵌入式汇编时,需切换到nightly版本) 1. rustup install nightly 2. rustup default nightly -3. rustup target add thumbv7em-none-eabi +3. rustup target add thumbv7em-none-eabi armv7a-none-eabi #### 使用说明 diff --git a/boards/raspberrypi/Cargo.toml b/boards/raspberrypi/Cargo.toml new file mode 100755 index 0000000..dae3388 --- /dev/null +++ b/boards/raspberrypi/Cargo.toml @@ -0,0 +1,12 @@ +[package] +edition = "2018" +readme = "README.md" +name = "raspberrypi" +version = "0.1.0" +authors = ["evilbinary "] + +[dependencies] +panic-halt = "0.2.0" +utils = {path = "../../utils/"} +kernel = {path = "../../kernel/source"} + diff --git a/boards/raspberrypi/src/main.rs b/boards/raspberrypi/src/main.rs new file mode 100644 index 0000000..cfccc14 --- /dev/null +++ b/boards/raspberrypi/src/main.rs @@ -0,0 +1,9 @@ +#![no_main] +#![no_std] +use panic_halt as _; + +fn main() { + + + loop {} +} diff --git a/boards/stm32f4xx/Cargo.toml b/boards/stm32f4xx/Cargo.toml index 68c6374..76b689a 100755 --- a/boards/stm32f4xx/Cargo.toml +++ b/boards/stm32f4xx/Cargo.toml @@ -4,6 +4,7 @@ edition = "2018" readme = "README.md" name = "stm32f4xx" version = "0.1.0" +default-run ="stm32f4xx" [dependencies] cortex-m = "0.6.0" diff --git a/kernel/arch/arm/cortex-a7/Cargo.toml b/kernel/arch/arm/cortex-a7/Cargo.toml new file mode 100644 index 0000000..45f22ce --- /dev/null +++ b/kernel/arch/arm/cortex-a7/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "cortex-a7" +version = "0.1.0" +edition = "2018" +authors = ["evilbinary "] +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/kernel/arch/arm/cortex-a7/src/lib.rs b/kernel/arch/arm/cortex-a7/src/lib.rs new file mode 100644 index 0000000..2e67b25 --- /dev/null +++ b/kernel/arch/arm/cortex-a7/src/lib.rs @@ -0,0 +1,8 @@ +#![no_std] +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +} -- Gitee From d86447c60f5a582e4fabcdb351d90e3ce1debd79 Mon Sep 17 00:00:00 2001 From: evilbinary Date: Sun, 8 Aug 2021 13:03:21 +0800 Subject: [PATCH 3/3] add raspberrypi --- .cargo/config.toml | 5 +- boards/raspberrypi/Cargo.toml | 4 +- boards/raspberrypi/link.x | 37 ++++++++++++ boards/raspberrypi/src/main.rs | 107 ++++++++++++++++++++++++++++++++- 4 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 boards/raspberrypi/link.x diff --git a/.cargo/config.toml b/.cargo/config.toml index 67051db..1f583ac 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -9,7 +9,7 @@ # runner = "gdb-multiarch -q -x openocd.gdb" # runner = "gdb -q -x openocd.gdb" # runner = "qemu-system-gnuarmeclipse --verbose --verbose --board STM32F4-Discovery --mcu STM32F407VG -d unimp,guest_errors --nographic --semihosting-config enable=on,target=native --image " -runner = "qemu-system-arm -M raspi2 -kernel " +runner = "qemu-system-arm -M raspi2 -serial stdio -chardev socket,id=monitor,path=monitor.sock,server,nowait -monitor chardev:monitor -D ./qemu.log -d in_asm -d cpu_reset -d in_asm,int,mmu -kernel " rustflags = [ # This is needed if your flash or ram addresses are not aligned to 0x10000 in memory.x @@ -17,7 +17,8 @@ rustflags = [ #"-C", "link-arg=--nmagic", # LLD (shipped with the Rust toolchain) is used as the default linker - "-C", "link-arg=-Tboards/stm32f4xx/link.x", + # "-C", "link-arg=-Tboards/stm32f4xx/link.x", + "-C", "link-arg=-Tboards/raspberrypi/link.x", # if you run into problems with LLD switch to the GNU linker by commenting out # this line diff --git a/boards/raspberrypi/Cargo.toml b/boards/raspberrypi/Cargo.toml index dae3388..43440b0 100755 --- a/boards/raspberrypi/Cargo.toml +++ b/boards/raspberrypi/Cargo.toml @@ -7,6 +7,6 @@ authors = ["evilbinary "] [dependencies] panic-halt = "0.2.0" -utils = {path = "../../utils/"} -kernel = {path = "../../kernel/source"} +# utils = {path = "../../utils/"} +# kernel = {path = "../../kernel/source"} diff --git a/boards/raspberrypi/link.x b/boards/raspberrypi/link.x new file mode 100644 index 0000000..abc9ed2 --- /dev/null +++ b/boards/raspberrypi/link.x @@ -0,0 +1,37 @@ +ENTRY(_start) +K_VIR_ADDR = 0x00000; +K_PHY_ADDR = 0x00000; +PAGE_SIZE = 0x100; +SECTIONS +{ + /* Code. */ + .text K_VIR_ADDR : AT(K_PHY_ADDR) + { + . = ALIGN(PAGE_SIZE); + *(.text._start) + } + + /* Read-only data. */ + .rodata : ALIGN(4K) + { + *(.rodata) + } + + /* Read-write data (initialized) */ + .data : ALIGN(4K) + { + *(.data ) + _stack_top = . ; + } + + /* Read-write data (uninitialized) and stack */ + .bss : ALIGN(4K) + { + *(COMMON) + *(.bss) + } + .ARM.exidx :{ + *(.ARM) + } + _end = .; +} diff --git a/boards/raspberrypi/src/main.rs b/boards/raspberrypi/src/main.rs index cfccc14..17c2251 100644 --- a/boards/raspberrypi/src/main.rs +++ b/boards/raspberrypi/src/main.rs @@ -1,9 +1,112 @@ #![no_main] #![no_std] +#![feature(asm)] use panic_halt as _; -fn main() { + +extern "C" { + static mut _stack_top: u32; +} + + +fn io_write(reg: u32, val: u32) { + unsafe { *(reg as *mut u32)= val; } +} + +fn io_read(reg: u32) -> u32 { + unsafe { *(reg as *const u32) } +} + +pub fn uart_init(){ + unsafe { + const BASE:u32=0x3F000000; + // turn off + *((BASE+0x00201030) as *mut u32) |= 0 as u32; + + //clear int + *((BASE+0x00201044) as *mut u32) = 0x7FF as u32; + //115200 + *((BASE+0x00201024) as *mut u32) = 0x7FF as u32; + + //fbrd + *((BASE+0x00201028) as *mut u32) = 0xB as u32; + //8n1 + *((BASE+0x0020102C) as *mut u32) = 0b11 << 5 as u32; + + //enable tx rx + *((BASE+0x00201030) as *mut u32) = 0x301 as u32; + } +} + + +pub fn uart_send(byte: u8) { + unsafe { + const BASE:u32 = 0x3F000000; + while (*((BASE+0x00201018) as *mut u32) & 0x20!=0) { + } + *((BASE +0x00201000) as *mut u32)= byte as u32; + } +} + +fn write_string(msg: &str) { + for c in msg.chars() { + uart_send(c as u8) + } +} + + +#[no_mangle] +pub unsafe fn _start() { + asm!(" + mrc p15, #0, r1, c0, c0, #5 + and r1, r1, #3 + cmp r1, #0 + bne halt + + mrs r0, cpsr + ldr sp, = _stack_top + ldr fp, = _stack_top + // set sp in abt mode. + bic r1, r0, #0x1F + orr r1, r1, #0x17 + msr cpsr_c,r1 + mov sp, #0x1000 + + // set sp in undf mode. + bic r1, r0, #0x1F + orr r1, r1, #0x1B + msr cpsr_c,r1 + mov sp, #0x1000 + + // set sp in irq mode. + bic r1, r0, #0x1F + orr r1, r1, #0x12 + msr cpsr_c,r1 + mov sp, #0x2000 + //ldr sp,= stack_irq + + // set sp in svc mode. + bic r1, r0, #0x1F + orr r1, r1, #0x13 + msr cpsr_c, r0 + mov sp, #0x3000 + //ldr sp,= stack_svc + bl main + + halt: + wfe + b halt + " +); + main(); +} + +#[no_mangle] +fn main() { + uart_init(); + write_string("Hello,liteos cortex-a7!\n"); - loop {} + loop{ + } } -- Gitee