Skip to content

Commit 9f22b12

Browse files
author
Henrik Alsér
committed
Hard-code channel numbers
1 parent 24a2b59 commit 9f22b12

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

examples/gpiote-demo/src/main.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const APP: () = {
2222
struct Resources {
2323
gpiote: Gpiote,
2424
btn1: Pin<Input<PullUp>>,
25-
btn2: Pin<Input<PullUp>>,
2625
btn3: Pin<Input<PullUp>>,
2726
btn4: Pin<Input<PullUp>>,
2827
}
@@ -41,7 +40,7 @@ const APP: () = {
4140
let gpiote = Gpiote::new(ctx.device.GPIOTE);
4241

4342
// Set btn1 to generate event on channel 0 and enable interrupt
44-
gpiote.channel(0).input_pin(&btn1).hi_to_lo(true);
43+
gpiote.channel0().input_pin(&btn1).hi_to_lo(true);
4544

4645
// Set both btn3 & btn4 to generate port event
4746
gpiote.port().input_pin(&btn3).low();
@@ -51,15 +50,15 @@ const APP: () = {
5150

5251
// PPI usage, channel 2 event triggers "task out" (toggle) on channel 1 (toggles led1)
5352
gpiote
54-
.channel(1)
53+
.channel1()
5554
.output_pin(&led1)
5655
.task_out_polarity(TaskOutPolarity::Toggle)
5756
.init_high();
58-
gpiote.channel(2).input_pin(&btn2).hi_to_lo(false);
57+
gpiote.channel2().input_pin(&btn2).hi_to_lo(false);
5958
let ppi_channels = ppi::Parts::new(ctx.device.PPI);
6059
let mut channel0 = ppi_channels.ppi0;
61-
channel0.set_task_endpoint(gpiote.channel(1).task_out());
62-
channel0.set_event_endpoint(gpiote.channel(2).event());
60+
channel0.set_task_endpoint(gpiote.channel1().task_out());
61+
channel0.set_event_endpoint(gpiote.channel2().event());
6362
channel0.enable();
6463

6564
// Enable the monotonic timer (CYCCNT)
@@ -71,7 +70,6 @@ const APP: () = {
7170
init::LateResources {
7271
gpiote,
7372
btn1,
74-
btn2,
7573
btn3,
7674
btn4,
7775
}
@@ -92,17 +90,16 @@ const APP: () = {
9290
ctx.schedule.debounce(ctx.start + 3_000_000.cycles()).ok();
9391
}
9492

95-
#[task(resources = [gpiote, btn1, btn2, btn3, btn4])]
93+
#[task(resources = [gpiote, btn1, btn3, btn4])]
9694
fn debounce(ctx: debounce::Context) {
9795
let btn1_pressed = ctx.resources.btn1.is_low().unwrap();
98-
let btn2_pressed = ctx.resources.btn2.is_low().unwrap();
9996
let btn3_pressed = ctx.resources.btn3.is_low().unwrap();
10097
let btn4_pressed = ctx.resources.btn4.is_low().unwrap();
10198

10299
if btn1_pressed {
103100
rprintln!("Button 1 was pressed!");
104101
// Manually run "task out" (toggle) on channel 1 (toggles led1)
105-
ctx.resources.gpiote.channel(1).out();
102+
ctx.resources.gpiote.channel1().out();
106103
}
107104
if btn3_pressed {
108105
rprintln!("Button 3 was pressed!");

nrf-hal-common/src/gpiote.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,40 @@ impl Gpiote {
3232
Self { gpiote }
3333
}
3434

35-
pub fn channel(&self, channel: usize) -> GpioteChannel {
35+
fn channel(&self, channel: usize) -> GpioteChannel {
3636
GpioteChannel {
3737
gpiote: &self.gpiote,
3838
channel,
3939
}
4040
}
41+
pub fn channel0(&self) -> GpioteChannel {
42+
self.channel(0)
43+
}
44+
pub fn channel1(&self) -> GpioteChannel {
45+
self.channel(1)
46+
}
47+
pub fn channel2(&self) -> GpioteChannel {
48+
self.channel(2)
49+
}
50+
pub fn channel3(&self) -> GpioteChannel {
51+
self.channel(3)
52+
}
53+
#[cfg(not(feature = "51"))]
54+
pub fn channel4(&self) -> GpioteChannel {
55+
self.channel(4)
56+
}
57+
#[cfg(not(feature = "51"))]
58+
pub fn channel5(&self) -> GpioteChannel {
59+
self.channel(5)
60+
}
61+
#[cfg(not(feature = "51"))]
62+
pub fn channel6(&self) -> GpioteChannel {
63+
self.channel(6)
64+
}
65+
#[cfg(not(feature = "51"))]
66+
pub fn channel7(&self) -> GpioteChannel {
67+
self.channel(7)
68+
}
4169

4270
pub fn port(&self) -> GpiotePort {
4371
GpiotePort {

0 commit comments

Comments
 (0)