Skip to content

Commit

Permalink
Merge pull request tock#498 from tock/example-gpio
Browse files Browse the repository at this point in the history
Add GPIO example
  • Loading branch information
jrvanwhy authored Sep 13, 2023
2 parents 52b727f + 00d8c98 commit cda8737
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
10 changes: 5 additions & 5 deletions apis/gpio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use libtock_platform::{
share::Handle, subscribe::OneId, DefaultConfig, ErrorCode, Subscribe, Syscalls, Upcall,
};

/// The Gpio driver
/// The GPIO driver.
///
/// # Example
/// ```ignore
/// use libtock::Gpios;
///
/// // Turn on led 0
/// let pin = Gpios::get_pin(0)?;
/// use libtock::gpio;
///
/// // Set pin to high.
/// let pin = gpio::Gpio::get_pin(0).unwrap().make_output().unwrap();
/// let _ = pin.set();
/// ```

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
Expand Down
43 changes: 43 additions & 0 deletions examples/gpio.rs
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();
}
});
}

0 comments on commit cda8737

Please sign in to comment.