forked from tock/libtock-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request tock#498 from tock/example-gpio
Add GPIO example
- Loading branch information
Showing
2 changed files
with
48 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//! A simple GPIO example for getting GPIO interrupts. | ||
//! | ||
//! This will configure GPIO 0 to be a rising-edge triggered interrupt and print | ||
//! a message when the interrupt is triggered. | ||
|
||
#![no_main] | ||
#![no_std] | ||
|
||
use core::fmt::Write; | ||
use libtock::console::Console; | ||
use libtock::gpio; | ||
use libtock::gpio::Gpio; | ||
use libtock::runtime::{set_main, stack_size}; | ||
use libtock_platform::{share, Syscalls}; | ||
use libtock_runtime::TockSyscalls; | ||
|
||
set_main! {main} | ||
stack_size! {0x1000} | ||
|
||
fn main() { | ||
let listener = gpio::GpioInterruptListener(|gpio_index, state| { | ||
writeln!(Console::writer(), "GPIO[{}]: {:?}", gpio_index, state).unwrap(); | ||
}); | ||
|
||
if !Gpio::count().is_ok_and(|c| c > 0) { | ||
writeln!(Console::writer(), "No GPIO pins on this board.").unwrap(); | ||
return; | ||
} | ||
|
||
// Configure pin 0 as an input and enable rising interrupts | ||
let pin = Gpio::get_pin(0).unwrap(); | ||
let input_pin = pin.make_input::<gpio::PullNone>().unwrap(); | ||
let _ = input_pin.enable_interrupts(gpio::PinInterruptEdge::Rising); | ||
|
||
// Wait for callbacks. | ||
share::scope(|subscribe| { | ||
Gpio::register_listener(&listener, subscribe).unwrap(); | ||
|
||
loop { | ||
TockSyscalls::yield_wait(); | ||
} | ||
}); | ||
} |