diff options
| author | Dario Nieuwenhuis <[email protected]> | 2020-09-23 00:32:49 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2020-09-23 00:32:49 +0200 |
| commit | f9e2cef7f23cb4471f1b7a14c5bf771a09a56cbc (patch) | |
| tree | 456677d24209734a75cf1a9459e14c1c9d4e0cd9 /examples | |
| parent | 9a57deef9b531b8dae9d98a5accf5aeb128ab86d (diff) | |
Add GPIOTE async hal.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/src/bin/gpiote.rs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/examples/src/bin/gpiote.rs b/examples/src/bin/gpiote.rs new file mode 100644 index 000000000..f55b0386d --- /dev/null +++ b/examples/src/bin/gpiote.rs | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[path = "../example_common.rs"] | ||
| 6 | mod example_common; | ||
| 7 | use example_common::*; | ||
| 8 | |||
| 9 | use core::pin::Pin; | ||
| 10 | use cortex_m_rt::entry; | ||
| 11 | use embassy::io::{AsyncBufRead, AsyncBufReadExt, AsyncWrite, AsyncWriteExt}; | ||
| 12 | use embassy_nrf::gpiote; | ||
| 13 | use futures::pin_mut; | ||
| 14 | use nrf52840_hal::gpio; | ||
| 15 | |||
| 16 | #[static_executor::task] | ||
| 17 | async fn run() { | ||
| 18 | let p = embassy_nrf::pac::Peripherals::take().dewrap(); | ||
| 19 | let port0 = gpio::p0::Parts::new(p.P0); | ||
| 20 | |||
| 21 | let g = gpiote::Gpiote::new(p.GPIOTE); | ||
| 22 | |||
| 23 | info!("Starting!"); | ||
| 24 | |||
| 25 | let pin1 = port0.p0_11.into_pullup_input().degrade(); | ||
| 26 | let button1 = async { | ||
| 27 | let ch = g | ||
| 28 | .new_input_channel(pin1, gpiote::EventPolarity::HiToLo) | ||
| 29 | .dewrap(); | ||
| 30 | |||
| 31 | loop { | ||
| 32 | ch.wait().await; | ||
| 33 | info!("Button 1 pressed") | ||
| 34 | } | ||
| 35 | }; | ||
| 36 | |||
| 37 | let pin2 = port0.p0_12.into_pullup_input().degrade(); | ||
| 38 | let button2 = async { | ||
| 39 | let ch = g | ||
| 40 | .new_input_channel(pin2, gpiote::EventPolarity::LoToHi) | ||
| 41 | .dewrap(); | ||
| 42 | |||
| 43 | loop { | ||
| 44 | ch.wait().await; | ||
| 45 | info!("Button 2 released") | ||
| 46 | } | ||
| 47 | }; | ||
| 48 | |||
| 49 | let pin3 = port0.p0_24.into_pullup_input().degrade(); | ||
| 50 | let button3 = async { | ||
| 51 | let ch = g | ||
| 52 | .new_input_channel(pin3, gpiote::EventPolarity::Toggle) | ||
| 53 | .dewrap(); | ||
| 54 | |||
| 55 | loop { | ||
| 56 | ch.wait().await; | ||
| 57 | info!("Button 3 toggled") | ||
| 58 | } | ||
| 59 | }; | ||
| 60 | |||
| 61 | let pin4 = port0.p0_25.into_pullup_input().degrade(); | ||
| 62 | let button4 = async { | ||
| 63 | let ch = g | ||
| 64 | .new_input_channel(pin4, gpiote::EventPolarity::Toggle) | ||
| 65 | .dewrap(); | ||
| 66 | |||
| 67 | loop { | ||
| 68 | ch.wait().await; | ||
| 69 | info!("Button 4 toggled") | ||
| 70 | } | ||
| 71 | }; | ||
| 72 | |||
| 73 | futures::join!(button1, button2, button3, button4); | ||
| 74 | } | ||
| 75 | |||
| 76 | #[entry] | ||
| 77 | fn main() -> ! { | ||
| 78 | info!("Hello World!"); | ||
| 79 | |||
| 80 | unsafe { | ||
| 81 | run.spawn().dewrap(); | ||
| 82 | static_executor::run(); | ||
| 83 | } | ||
| 84 | } | ||
