From 36517fd1c580bdd0dffdcbb052bdf9961a4b7346 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sun, 8 Nov 2020 19:00:08 +0100 Subject: [PATCH] Add gpiote port example. --- examples/src/bin/gpiote_port.rs | 62 +++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 examples/src/bin/gpiote_port.rs diff --git a/examples/src/bin/gpiote_port.rs b/examples/src/bin/gpiote_port.rs new file mode 100644 index 00000000..bc0cb436 --- /dev/null +++ b/examples/src/bin/gpiote_port.rs @@ -0,0 +1,62 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; +use example_common::*; + +use core::mem; +use cortex_m_rt::entry; +use nrf52840_hal::gpio; + +use embassy::executor::{task, Executor}; +use embassy::util::Forever; +use embassy_nrf::gpiote; + +async fn button(g: &gpiote::Gpiote, n: usize, pin: gpio::Pin>) { + let ch = g.new_port_input(pin); + + loop { + ch.wait(gpiote::PortInputPolarity::Low).await; + info!("Button {:?} pressed!", n); + ch.wait(gpiote::PortInputPolarity::High).await; + info!("Button {:?} released!", n); + } +} + +#[task] +async fn run() { + let p = unwrap!(embassy_nrf::pac::Peripherals::take()); + let port0 = gpio::p0::Parts::new(p.P0); + + let g = gpiote::Gpiote::new(p.GPIOTE); + info!( + "sizeof Signal<()> = {:usize}", + mem::size_of::>() + ); + info!("sizeof gpiote = {:usize}", mem::size_of::()); + + info!("Starting!"); + + let button1 = button(&g, 1, port0.p0_11.into_pullup_input().degrade()); + let button2 = button(&g, 2, port0.p0_12.into_pullup_input().degrade()); + let button3 = button(&g, 3, port0.p0_24.into_pullup_input().degrade()); + let button4 = button(&g, 4, port0.p0_25.into_pullup_input().degrade()); + futures::join!(button1, button2, button3, button4); +} + +static EXECUTOR: Forever = Forever::new(); + +#[entry] +fn main() -> ! { + info!("Hello World!"); + + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev)); + unwrap!(executor.spawn(run())); + + loop { + executor.run(); + cortex_m::asm::wfe(); + } +}