Skip to content

Commit

Permalink
Add button example
Browse files Browse the repository at this point in the history
  • Loading branch information
romancardenas committed Oct 19, 2024
1 parent 088dc69 commit 5216882
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 8 deletions.
4 changes: 2 additions & 2 deletions hifive1-examples/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[target.'cfg(all(target_arch = "riscv32", target_os = "none"))']
runner = "qemu-system-riscv32 -machine sifive_e,revb=true -nographic -semihosting-config enable=on,target=native -kernel" # Uncomment for QEMU
# runner = "riscv64-unknown-elf-gdb -q -x hifive1-examples/gdb_init" # Uncomment for hardware (no semihosting)
# runner = "riscv64-unknown-elf-gdb -q -x hifive1-examples/gdb_init_sh" # Uncomment for hardware (semihosting)
# runner = "riscv64-unknown-elf-gdb -q -x gdb_init" # Uncomment for hardware (no semihosting)
# runner = "riscv64-unknown-elf-gdb -q -x gdb_init_sh" # Uncomment for hardware (semihosting)
rustflags = [
"-C", "link-arg=-Thifive1-link.x",
"--cfg", "portable_atomic_target_feature=\"zaamo\"",
Expand Down
1 change: 1 addition & 0 deletions hifive1-examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rust-version = "1.72"
[dependencies]
critical-section = { version = "1.1.3" }
hifive1 = { path = "../hifive1", version = "0.13.0", features = ["board-hifive1-revb"] } # Change to your board
riscv = { version = "0.11.0" }
riscv-rt = { version = "0.12.2", features = ["single-hart"] }
panic-halt = "0.2.0"
semihosting = { version = "0.1", features = ["stdio", "panic-handler"], optional = true }
Expand Down
54 changes: 54 additions & 0 deletions hifive1-examples/examples/button_poll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! Example of polling a button and turning on a LED when the button is pressed.
#![no_std]
#![no_main]

use hifive1::{
clock,
hal::{delay::Sleep, prelude::*, DeviceResources},
pin, sprintln, Led,
};
extern crate panic_halt;

#[riscv_rt::entry]
fn main() -> ! {
let dr = DeviceResources::take().unwrap();
let p = dr.peripherals;
let pins = dr.pins;

// Configure clocks
let clocks = clock::configure(p.PRCI, p.AONCLK, 320.mhz().into());

// Configure UART for stdout
hifive1::stdout::configure(
p.UART0,
pin!(pins, uart0_tx),
pin!(pins, uart0_rx),
115_200.bps(),
clocks,
);

// Configure button pin as pull-up input
let mut button = pins.pin9.into_pull_up_input();

// get blue LED pin
let pin = pin!(pins, led_blue);
let mut led = pin.into_inverted_output();

// Get the sleep struct from CLINT
let clint = dr.core_peripherals.clint;
let mut sleep = Sleep::new(clint.mtimecmp, clocks);

const STEP: u32 = 1000; // 1s
loop {
if button.is_low().unwrap() {
sprintln!("Button pressed");
led.on();
} else {
sprintln!("Button released");
led.off();
}
sprintln!("LED is on: {}", led.is_on());
sleep.delay_ms(STEP);
}
}
4 changes: 3 additions & 1 deletion hifive1-examples/examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ fn main() -> ! {
);

println!("Hello, world!");
loop {}
loop {
riscv::asm::wfi();
}
}
2 changes: 1 addition & 1 deletion hifive1-examples/examples/led_blink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() -> ! {
clocks,
);

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

Expand Down
2 changes: 1 addition & 1 deletion hifive1/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
- Add examples based on semihosting
- Fix Led implementation, as pins are configured as inverted outputs
- Adapt to embedded-hal 1.0
- Replace static muts with Mutexes
- Apply clippy changes
Expand Down
6 changes: 3 additions & 3 deletions hifive1/src/led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ macro_rules! led_impl {
$(
impl Led for $LEDTYPE {
fn is_on(&mut self) -> bool {
self.is_set_high().unwrap()
self.is_set_low().unwrap()
}

fn off(&mut self) {
self.set_low().unwrap();
self.set_high().unwrap();
}

fn on(&mut self) {
self.set_high().unwrap();
self.set_low().unwrap();
}
}
)+
Expand Down

0 comments on commit 5216882

Please sign in to comment.