Skip to content

Commit 236a82f

Browse files
committed
Add button example
1 parent a4a7c8c commit 236a82f

File tree

7 files changed

+65
-8
lines changed

7 files changed

+65
-8
lines changed

hifive1-examples/.cargo/config.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[target.'cfg(all(target_arch = "riscv32", target_os = "none"))']
22
runner = "qemu-system-riscv32 -machine sifive_e,revb=true -nographic -semihosting-config enable=on,target=native -kernel" # Uncomment for QEMU
3-
# runner = "riscv64-unknown-elf-gdb -q -x hifive1-examples/gdb_init" # Uncomment for hardware (no semihosting)
4-
# runner = "riscv64-unknown-elf-gdb -q -x hifive1-examples/gdb_init_sh" # Uncomment for hardware (semihosting)
3+
# runner = "riscv64-unknown-elf-gdb -q -x gdb_init" # Uncomment for hardware (no semihosting)
4+
# runner = "riscv64-unknown-elf-gdb -q -x gdb_init_sh" # Uncomment for hardware (semihosting)
55
rustflags = [
66
"-C", "link-arg=-Thifive1-link.x",
77
"--cfg", "portable_atomic_target_feature=\"zaamo\"",

hifive1-examples/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ rust-version = "1.72"
1515
[dependencies]
1616
critical-section = { version = "1.1.3" }
1717
hifive1 = { path = "../hifive1", version = "0.13.0", features = ["board-hifive1-revb"] } # Change to your board
18+
riscv = { version = "0.11.0" }
1819
riscv-rt = { version = "0.12.2", features = ["single-hart"] }
1920
panic-halt = "0.2.0"
2021
semihosting = { version = "0.1", features = ["stdio", "panic-handler"], optional = true }
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! Example of polling a button and turning on a LED when the button is pressed.
2+
3+
#![no_std]
4+
#![no_main]
5+
6+
use hifive1::{
7+
clock,
8+
hal::{delay::Sleep, prelude::*, DeviceResources},
9+
pin, sprintln, Led,
10+
};
11+
extern crate panic_halt;
12+
13+
#[riscv_rt::entry]
14+
fn main() -> ! {
15+
let dr = DeviceResources::take().unwrap();
16+
let p = dr.peripherals;
17+
let pins = dr.pins;
18+
19+
// Configure clocks
20+
let clocks = clock::configure(p.PRCI, p.AONCLK, 320.mhz().into());
21+
22+
// Configure UART for stdout
23+
hifive1::stdout::configure(
24+
p.UART0,
25+
pin!(pins, uart0_tx),
26+
pin!(pins, uart0_rx),
27+
115_200.bps(),
28+
clocks,
29+
);
30+
31+
// Configure button pin as pull-up input
32+
let mut button = pins.pin9.into_pull_up_input();
33+
34+
// get blue LED pin
35+
let pin = pin!(pins, led_blue);
36+
let mut led = pin.into_inverted_output();
37+
38+
// Get the sleep struct from CLINT
39+
let clint = dr.core_peripherals.clint;
40+
let mut sleep = Sleep::new(clint.mtimecmp, clocks);
41+
42+
const STEP: u32 = 1000; // 1s
43+
loop {
44+
if button.is_low().unwrap() {
45+
sprintln!("Button pressed");
46+
led.on();
47+
} else {
48+
sprintln!("Button released");
49+
led.off();
50+
}
51+
sprintln!("LED is on: {}", led.is_on());
52+
sleep.delay_ms(STEP);
53+
}
54+
}

hifive1-examples/examples/hello_world.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ fn main() -> ! {
3131
);
3232

3333
println!("Hello, world!");
34-
loop {}
34+
loop {
35+
riscv::asm::wfi();
36+
}
3537
}

hifive1-examples/examples/led_blink.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn main() -> ! {
2929
clocks,
3030
);
3131

32-
// get all 3 led pins in a tuple (each pin is it's own type here)
32+
// get blue LED pin
3333
let pin = pin!(pins, led_blue);
3434
let mut led = pin.into_inverted_output();
3535

hifive1/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
9-
- Add examples based on semihosting
9+
- Fix Led implementation, as pins are configured as inverted outputs
1010
- Adapt to embedded-hal 1.0
1111
- Replace static muts with Mutexes
1212
- Apply clippy changes

hifive1/src/led.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ macro_rules! led_impl {
6666
$(
6767
impl Led for $LEDTYPE {
6868
fn is_on(&mut self) -> bool {
69-
self.is_set_high().unwrap()
69+
self.is_set_low().unwrap()
7070
}
7171

7272
fn off(&mut self) {
73-
self.set_low().unwrap();
73+
self.set_high().unwrap();
7474
}
7575

7676
fn on(&mut self) {
77-
self.set_high().unwrap();
77+
self.set_low().unwrap();
7878
}
7979
}
8080
)+

0 commit comments

Comments
 (0)