Skip to content

Commit 6aef7f4

Browse files
committed
lib: support uart peripheral, small fixes
Signed-off-by: zhujunxing <zjx1319@hust.edu.cn>
1 parent bc87185 commit 6aef7f4

File tree

10 files changed

+63
-15
lines changed

10 files changed

+63
-15
lines changed

Cargo.lock

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/hello-world/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hello-world"
3-
version = "0.1.0"
3+
version = "0.0.0"
44

55
edition.workspace = true
66
license.workspace = true
@@ -9,7 +9,6 @@ repository.workspace = true
99
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1010

1111
[dependencies]
12-
uart16550 = "0.0.1"
1312
sophgo-rom-rt = { version = "0.0.0", path = "../../sophgo-rom-rt" }
1413
panic-halt = "0.2.0"
1514

examples/hello-world/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
#![no_main]
66

77
use panic_halt as _;
8-
use sophgo_rom_rt::entry;
8+
use sophgo_rom_rt::{entry, Peripherals};
99

1010
#[entry]
11-
fn main() -> ! {
12-
let uart = unsafe { &*(0x04140000 as *const uart16550::Uart16550<u32>) };
11+
fn main(p: Peripherals) -> ! {
1312
loop {
14-
uart.write(b"Hello World from Rust!\n");
13+
p.uart0.write(b"Hello World from Rust!\n");
1514
// TODO fix Uart16550 crate bug; it doesn't block when UART FIFO is not empty
1615
}
1716
}

sophgo-hal/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ repository.workspace = true
99
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1010

1111
[dependencies]
12+
base-address = "0.0.0"
13+
uart16550 = "0.0.1"

sophgo-hal/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
11
#![no_std]
2+
3+
use core::ops;
4+
5+
use base_address::BaseAddress;
6+
7+
/// Universal Asynchronous Receiver/Transmitter.
8+
pub struct UART<A: BaseAddress> {
9+
base: A,
10+
}
11+
12+
impl<A: BaseAddress> ops::Deref for UART<A> {
13+
type Target = uart16550::Uart16550<u32>;
14+
15+
#[inline(always)]
16+
fn deref(&self) -> &Self::Target {
17+
unsafe { &*(self.base.ptr() as *const _) }
18+
}
19+
}

sophgo-rom-rt/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ repository.workspace = true
99
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1010

1111
[dependencies]
12+
base-address = "0.0.0"
13+
sophgo-hal = { version = "0.0.0", path = "../sophgo-hal" }
1214
sophgo-rom-rt-macros = { version = "0.0.0", path = "macros" }

sophgo-rom-rt/macros/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
name = "sophgo-rom-rt-macros"
33
version = "0.0.0"
4-
edition = "2021"
4+
5+
edition.workspace = true
6+
license.workspace = true
7+
repository.workspace = true
58

69
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
710

sophgo-rom-rt/macros/src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
1515

1616
let f = parse_macro_input!(input as ItemFn);
1717

18-
if f.sig.inputs.len() != 0 {
18+
if f.sig.inputs.len() != 1 {
1919
return parse::Error::new(
2020
f.sig.inputs.span(),
2121
"`#[entry]` function without rom peripherals should not include any parameter",
@@ -35,7 +35,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
3535
if !valid_signature {
3636
return parse::Error::new(
3737
f.sig.span(),
38-
"`#[entry]` function must have signature `[unsafe] fn() -> !`",
38+
"`#[entry]` function must have signature `[unsafe] fn(Peripherals) -> !`",
3939
)
4040
.to_compile_error()
4141
.into();
@@ -44,12 +44,18 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
4444
let attrs = f.attrs;
4545
let unsafety = f.sig.unsafety;
4646
let stmts = f.block.stmts;
47+
let inputs = f.sig.inputs;
4748

4849
quote!(
49-
#[allow(non_snake_case)]
5050
#[export_name = "main"]
51+
pub extern "C" fn main() -> ! {
52+
let p = unsafe { core::mem::transmute(()) };
53+
unsafe { __sophgo_rom_rt_macros__main(p) }
54+
}
55+
#[allow(non_snake_case)]
56+
#[inline(always)]
5157
#(#attrs)*
52-
pub #unsafety fn main() -> ! {
58+
#unsafety fn __sophgo_rom_rt_macros__main(#inputs) -> ! {
5359
#(#stmts)*
5460
}
5561
)

sophgo-rom-rt/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33

44
pub use sophgo_rom_rt_macros::entry;
55

6+
use base_address::Static;
7+
8+
/// Peripherals available on ROM start.
9+
pub struct Peripherals {
10+
/// Universal Asynchronous Receiver/Transmitter 0.
11+
pub uart0: sophgo_hal::UART<Static<0x04140000>>,
12+
}
13+
614
#[cfg(target_arch = "riscv64")]
715
use core::arch::asm;
816

sophgo-rom-tool/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sophgo-rom-tool"
3-
version = "0.1.0"
3+
version = "0.0.0"
44

55
edition.workspace = true
66
license.workspace = true

0 commit comments

Comments
 (0)