Skip to content

Commit

Permalink
examples: add nezha-d1 example package
Browse files Browse the repository at this point in the history
Signed-off-by: Zhouqi Jiang <[email protected]>
  • Loading branch information
luojia65 committed Oct 9, 2024
1 parent 89f1e6c commit 747e9b5
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ default = ["d1"]
d1 = []

[workspace]
members = ["allwinner-rt", "allwinner-rt/macros"]
members = [
"allwinner-rt",
"allwinner-rt/macros",
"examples/nezha-d1",
]
14 changes: 7 additions & 7 deletions allwinner-rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions examples/nezha-d1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions examples/nezha-d1/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("cargo:rustc-link-arg=-Tallwinner-rt.ld");
}
37 changes: 37 additions & 0 deletions examples/nezha-d1/src/bin/uart.rs
Original file line number Diff line number Diff line change
@@ -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();
}
1 change: 1 addition & 0 deletions examples/nezha-d1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#![no_std]

0 comments on commit 747e9b5

Please sign in to comment.