aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf-examples/src
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-03-27 16:13:32 +0100
committerDario Nieuwenhuis <[email protected]>2021-03-29 00:58:58 +0200
commitb6496a85d89beb0f9bc4d3db2f39d82e8768e5da (patch)
treeebe3f634cd636dce23888397408eed5910e69db6 /embassy-nrf-examples/src
parent26705ec32862a00e79cf334019aa3fbd4596bf17 (diff)
nrf/ppi: implement and add example
Diffstat (limited to 'embassy-nrf-examples/src')
-rw-r--r--embassy-nrf-examples/src/bin/ppi.rs112
1 files changed, 112 insertions, 0 deletions
diff --git a/embassy-nrf-examples/src/bin/ppi.rs b/embassy-nrf-examples/src/bin/ppi.rs
new file mode 100644
index 000000000..87854fa5c
--- /dev/null
+++ b/embassy-nrf-examples/src/bin/ppi.rs
@@ -0,0 +1,112 @@
1#![no_std]
2#![no_main]
3#![feature(min_type_alias_impl_trait)]
4#![feature(impl_trait_in_bindings)]
5#![feature(type_alias_impl_trait)]
6#![allow(incomplete_features)]
7
8#[path = "../example_common.rs"]
9mod example_common;
10use core::future::pending;
11
12use example_common::*;
13
14use cortex_m_rt::entry;
15use defmt::panic;
16
17use embassy::executor::{task, Executor};
18use embassy::util::Forever;
19use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pull};
20use embassy_nrf::gpiote::{self, InputChannel, InputChannelPolarity};
21use embassy_nrf::ppi::Ppi;
22use embassy_nrf::{interrupt, Peripherals};
23use futures::future;
24use gpiote::{OutputChannel, OutputChannelPolarity};
25
26#[task]
27async fn run() {
28 let p = Peripherals::take().unwrap();
29 let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE));
30
31 info!("Starting!");
32
33 let button1 = InputChannel::new(
34 g,
35 p.GPIOTE_CH0,
36 Input::new(p.P0_11, Pull::Up),
37 InputChannelPolarity::HiToLo,
38 );
39 let button2 = InputChannel::new(
40 g,
41 p.GPIOTE_CH1,
42 Input::new(p.P0_12, Pull::Up),
43 InputChannelPolarity::HiToLo,
44 );
45 let button3 = InputChannel::new(
46 g,
47 p.GPIOTE_CH2,
48 Input::new(p.P0_24, Pull::Up),
49 InputChannelPolarity::HiToLo,
50 );
51 let button4 = InputChannel::new(
52 g,
53 p.GPIOTE_CH3,
54 Input::new(p.P0_25, Pull::Up),
55 InputChannelPolarity::HiToLo,
56 );
57
58 let led1 = OutputChannel::new(
59 g,
60 p.GPIOTE_CH4,
61 Output::new(p.P0_13, Level::Low, OutputDrive::Standard),
62 OutputChannelPolarity::Toggle,
63 );
64
65 let led2 = OutputChannel::new(
66 g,
67 p.GPIOTE_CH5,
68 Output::new(p.P0_14, Level::Low, OutputDrive::Standard),
69 OutputChannelPolarity::Toggle,
70 );
71
72 let mut ppi = Ppi::new(p.PPI_CH0);
73 ppi.set_event(button1.event_in());
74 ppi.set_task(led1.task_out());
75 ppi.enable();
76
77 let mut ppi = Ppi::new(p.PPI_CH1);
78 ppi.set_event(button2.event_in());
79 ppi.set_task(led1.task_clr());
80 ppi.enable();
81
82 let mut ppi = Ppi::new(p.PPI_CH2);
83 ppi.set_event(button3.event_in());
84 ppi.set_task(led1.task_set());
85 ppi.enable();
86
87 let mut ppi = Ppi::new(p.PPI_CH3);
88 ppi.set_event(button4.event_in());
89 ppi.set_task(led1.task_out());
90 ppi.set_fork_task(led2.task_out());
91 ppi.enable();
92
93 info!("PPI setup!");
94 info!("Press button 1 to toggle LED 1");
95 info!("Press button 2 to turn on LED 1");
96 info!("Press button 3 to turn off LED 1");
97 info!("Press button 4 to toggle LEDs 1 and 2");
98 // Block forever so the above drivers don't get dropped
99 pending::<()>().await;
100}
101
102static EXECUTOR: Forever<Executor> = Forever::new();
103
104#[entry]
105fn main() -> ! {
106 info!("Hello World!");
107
108 let executor = EXECUTOR.put(Executor::new());
109 executor.run(|spawner| {
110 unwrap!(spawner.spawn(run()));
111 });
112}