diff --git a/Cargo.toml b/Cargo.toml index c711a6d..156f03f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,4 +33,8 @@ default = ["d1"] d1 = [] [workspace] -members = ["allwinner-rt", "allwinner-rt/macros"] +members = [ + "allwinner-rt", + "allwinner-rt/macros", + "examples/nezha-d1", +] diff --git a/allwinner-rt/src/lib.rs b/allwinner-rt/src/lib.rs index a886dda..5b266c3 100644 --- a/allwinner-rt/src/lib.rs +++ b/allwinner-rt/src/lib.rs @@ -76,27 +76,27 @@ unsafe extern "C" fn start() -> ! { // Clear `.bss` section "la t1, sbss la t2, ebss - 1: bgeu t1, t2, 1f + 3: bgeu t1, t2, 3f sd zero, 0(t1) addi t1, t1, 8 - j 1b - 1: ", + j 3b + 3: ", // Prepare data segment " la t3, sidata la t4, sdata la t5, edata - 1: bgeu t4, t5, 2f + 3: bgeu t4, t5, 2f ld t6, 0(t3) sd t6, 0(t4) addi t3, t3, 8 addi t4, t4, 8 - j 1b", + j 3b", "2: ", // Start Rust main function "call {main}", // Platform halt if main function returns - "1: wfi - j 1b", + "3: wfi + j 3b", stack = sym STACK, stack_size = const STACK_SIZE, main = sym main, diff --git a/examples/nezha-d1/Cargo.toml b/examples/nezha-d1/Cargo.toml new file mode 100644 index 0000000..c4581a5 --- /dev/null +++ b/examples/nezha-d1/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "nezha-d1-example" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +allwinner-hal = { path = "../.." } +allwinner-rt = { path = "../../allwinner-rt" } +panic-halt = "0.2.0" +embedded-io = "0.6.1" diff --git a/examples/nezha-d1/build.rs b/examples/nezha-d1/build.rs new file mode 100644 index 0000000..48a6818 --- /dev/null +++ b/examples/nezha-d1/build.rs @@ -0,0 +1,3 @@ +fn main() { + println!("cargo:rustc-link-arg=-Tallwinner-rt.ld"); +} diff --git a/examples/nezha-d1/src/bin/uart.rs b/examples/nezha-d1/src/bin/uart.rs new file mode 100644 index 0000000..e0fd820 --- /dev/null +++ b/examples/nezha-d1/src/bin/uart.rs @@ -0,0 +1,37 @@ +#![no_std] +#![no_main] + +use allwinner_hal::uart::{Config, Serial}; +use allwinner_rt::{entry, Clocks, Peripherals}; +use embedded_io::{Read, Write}; +use panic_halt as _; + +#[entry] +fn main(p: Peripherals, c: Clocks) { + let tx = p.gpio.pb8.into_function::<7>(); + let rx = p.gpio.pb9.into_function::<7>(); + let mut serial = Serial::new(p.uart0, (tx, rx), Config::default(), &c, &p.ccu); + + writeln!(serial, "Hello World!").ok(); + + let mut buf = [0u8; 64]; + let mut cur = 0; + + writeln!(serial, "Please input your name (maximum 64 bytes):").unwrap(); + write!(serial, "> ").unwrap(); + loop { + let mut ch = 0u8; + let _len = serial.read(core::slice::from_mut(&mut ch)).unwrap(); + if ch == b'\r' || ch == b'\n' { + break; + } + buf[cur] = ch; + cur += 1; + if cur > buf.len() { + break; + } + } + + let name = core::str::from_utf8(&buf).unwrap(); + writeln!(serial, "Hello, {}!", name).unwrap(); +} diff --git a/examples/nezha-d1/src/lib.rs b/examples/nezha-d1/src/lib.rs new file mode 100644 index 0000000..0c9ac1a --- /dev/null +++ b/examples/nezha-d1/src/lib.rs @@ -0,0 +1 @@ +#![no_std]