aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2020-11-08 19:00:08 +0100
committerDario Nieuwenhuis <[email protected]>2020-11-08 19:00:08 +0100
commit36517fd1c580bdd0dffdcbb052bdf9961a4b7346 (patch)
tree2ad6f4d8bd14715f95346652f30d12bbc2f18b8d /examples
parentfc0fe842ee0c9a39b23a0b82847580bd596b6e82 (diff)
Add gpiote port example.
Diffstat (limited to 'examples')
-rw-r--r--examples/src/bin/gpiote_port.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/examples/src/bin/gpiote_port.rs b/examples/src/bin/gpiote_port.rs
new file mode 100644
index 000000000..bc0cb4367
--- /dev/null
+++ b/examples/src/bin/gpiote_port.rs
@@ -0,0 +1,62 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5#[path = "../example_common.rs"]
6mod example_common;
7use example_common::*;
8
9use core::mem;
10use cortex_m_rt::entry;
11use nrf52840_hal::gpio;
12
13use embassy::executor::{task, Executor};
14use embassy::util::Forever;
15use embassy_nrf::gpiote;
16
17async fn button(g: &gpiote::Gpiote, n: usize, pin: gpio::Pin<gpio::Input<gpio::PullUp>>) {
18 let ch = g.new_port_input(pin);
19
20 loop {
21 ch.wait(gpiote::PortInputPolarity::Low).await;
22 info!("Button {:?} pressed!", n);
23 ch.wait(gpiote::PortInputPolarity::High).await;
24 info!("Button {:?} released!", n);
25 }
26}
27
28#[task]
29async fn run() {
30 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
31 let port0 = gpio::p0::Parts::new(p.P0);
32
33 let g = gpiote::Gpiote::new(p.GPIOTE);
34 info!(
35 "sizeof Signal<()> = {:usize}",
36 mem::size_of::<embassy::util::Signal<()>>()
37 );
38 info!("sizeof gpiote = {:usize}", mem::size_of::<gpiote::Gpiote>());
39
40 info!("Starting!");
41
42 let button1 = button(&g, 1, port0.p0_11.into_pullup_input().degrade());
43 let button2 = button(&g, 2, port0.p0_12.into_pullup_input().degrade());
44 let button3 = button(&g, 3, port0.p0_24.into_pullup_input().degrade());
45 let button4 = button(&g, 4, port0.p0_25.into_pullup_input().degrade());
46 futures::join!(button1, button2, button3, button4);
47}
48
49static EXECUTOR: Forever<Executor> = Forever::new();
50
51#[entry]
52fn main() -> ! {
53 info!("Hello World!");
54
55 let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
56 unwrap!(executor.spawn(run()));
57
58 loop {
59 executor.run();
60 cortex_m::asm::wfe();
61 }
62}