Skip to content

Commit b5900fb

Browse files
committed
examples: add a example of using GPIO to light up LED
Signed-off-by: zhujunxing <zjx1319@hust.edu.cn>
1 parent bc67c12 commit b5900fb

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ members = [
66
"sophgo-rom-rt/macros",
77
"sophgo-rom-tool",
88
"examples/hello-world",
9+
"examples/blinky",
910
]
1011

1112
[workspace.package]

examples/blinky/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "blinky"
3+
version = "0.0.0"
4+
5+
edition.workspace = true
6+
license.workspace = true
7+
repository.workspace = true
8+
9+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
10+
11+
[dependencies]
12+
sophgo-rom-rt = { version = "0.0.0", path = "../../sophgo-rom-rt" }
13+
panic-halt = "0.2.0"
14+
15+
[[bin]]
16+
name = "blinky"
17+
test = false
18+
bench = false

examples/blinky/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("cargo:rustc-link-arg=-Tsophgo-rom-rt.ld");
3+
}

examples/blinky/src/main.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// rustup target install riscv64imac-unknown-none-elf
2+
// cargo build -p blinky --target riscv64imac-unknown-none-elf --release
3+
4+
#![no_std]
5+
#![no_main]
6+
7+
use core::arch::asm;
8+
9+
use panic_halt as _;
10+
use sophgo_rom_rt::{entry, Peripherals};
11+
12+
#[entry]
13+
fn main(p: Peripherals) -> ! {
14+
unsafe {
15+
p.rtc_gpio
16+
.direction
17+
.write(p.rtc_gpio.direction.read().set_output(2));
18+
}
19+
loop {
20+
unsafe {
21+
p.rtc_gpio.data.write(p.rtc_gpio.data.read() | (1 << 2));
22+
}
23+
for _ in 0..=1_000_000 {
24+
unsafe { asm!("nop") };
25+
}
26+
unsafe {
27+
p.rtc_gpio.data.write(p.rtc_gpio.data.read() & !(1 << 2));
28+
}
29+
for _ in 0..=1_000_000 {
30+
unsafe { asm!("nop") };
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)