aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-rp/src/gpio.rs56
-rw-r--r--examples/rp/src/bin/blinky.rs5
-rw-r--r--examples/rp/src/bin/button.rs6
3 files changed, 47 insertions, 20 deletions
diff --git a/embassy-rp/src/gpio.rs b/embassy-rp/src/gpio.rs
index ee263fdb2..1eddce184 100644
--- a/embassy-rp/src/gpio.rs
+++ b/embassy-rp/src/gpio.rs
@@ -7,7 +7,7 @@ use crate::peripherals;
7 7
8use embassy::util::Unborrow; 8use embassy::util::Unborrow;
9use embassy_extras::{unborrow, unsafe_impl_unborrow}; 9use embassy_extras::{unborrow, unsafe_impl_unborrow};
10use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin}; 10use embedded_hal::digital::v2 as digital;
11 11
12/// Represents a digital input or output level. 12/// Represents a digital input or output level.
13#[derive(Debug, Eq, PartialEq)] 13#[derive(Debug, Eq, PartialEq)]
@@ -63,6 +63,15 @@ impl<'d, T: Pin> Input<'d, T> {
63 phantom: PhantomData, 63 phantom: PhantomData,
64 } 64 }
65 } 65 }
66
67 pub fn is_high(&self) -> bool {
68 !self.is_low()
69 }
70
71 pub fn is_low(&self) -> bool {
72 let val = 1 << self.pin.pin();
73 unsafe { self.pin.sio_in().read() & val == 0 }
74 }
66} 75}
67 76
68impl<'d, T: Pin> Drop for Input<'d, T> { 77impl<'d, T: Pin> Drop for Input<'d, T> {
@@ -71,16 +80,15 @@ impl<'d, T: Pin> Drop for Input<'d, T> {
71 } 80 }
72} 81}
73 82
74impl<'d, T: Pin> InputPin for Input<'d, T> { 83impl<'d, T: Pin> digital::InputPin for Input<'d, T> {
75 type Error = !; 84 type Error = !;
76 85
77 fn is_high(&self) -> Result<bool, Self::Error> { 86 fn is_high(&self) -> Result<bool, Self::Error> {
78 self.is_low().map(|v| !v) 87 Ok(self.is_high())
79 } 88 }
80 89
81 fn is_low(&self) -> Result<bool, Self::Error> { 90 fn is_low(&self) -> Result<bool, Self::Error> {
82 let val = 1 << self.pin.pin(); 91 Ok(self.is_low())
83 Ok(unsafe { self.pin.sio_in().read() } & val == 0)
84 } 92 }
85} 93}
86 94
@@ -111,6 +119,29 @@ impl<'d, T: Pin> Output<'d, T> {
111 phantom: PhantomData, 119 phantom: PhantomData,
112 } 120 }
113 } 121 }
122
123 /// Set the output as high.
124 pub fn set_high(&mut self) {
125 let val = 1 << self.pin.pin();
126 unsafe { self.pin.sio_out().value_set().write_value(val) };
127 }
128
129 /// Set the output as low.
130 pub fn set_low(&mut self) {
131 let val = 1 << self.pin.pin();
132 unsafe { self.pin.sio_out().value_clr().write_value(val) };
133 }
134
135 /// Is the output pin set as high?
136 pub fn is_set_high(&self) -> bool {
137 !self.is_set_low()
138 }
139
140 /// Is the output pin set as low?
141 pub fn is_set_low(&self) -> bool {
142 // todo
143 true
144 }
114} 145}
115 146
116impl<'d, T: Pin> Drop for Output<'d, T> { 147impl<'d, T: Pin> Drop for Output<'d, T> {
@@ -119,34 +150,31 @@ impl<'d, T: Pin> Drop for Output<'d, T> {
119 } 150 }
120} 151}
121 152
122impl<'d, T: Pin> OutputPin for Output<'d, T> { 153impl<'d, T: Pin> digital::OutputPin for Output<'d, T> {
123 type Error = !; 154 type Error = !;
124 155
125 /// Set the output as high. 156 /// Set the output as high.
126 fn set_high(&mut self) -> Result<(), Self::Error> { 157 fn set_high(&mut self) -> Result<(), Self::Error> {
127 let val = 1 << self.pin.pin(); 158 self.set_high();
128 unsafe { self.pin.sio_out().value_set().write_value(val) };
129 Ok(()) 159 Ok(())
130 } 160 }
131 161
132 /// Set the output as low. 162 /// Set the output as low.
133 fn set_low(&mut self) -> Result<(), Self::Error> { 163 fn set_low(&mut self) -> Result<(), Self::Error> {
134 let val = 1 << self.pin.pin(); 164 self.set_low();
135 unsafe { self.pin.sio_out().value_clr().write_value(val) };
136 Ok(()) 165 Ok(())
137 } 166 }
138} 167}
139 168
140impl<'d, T: Pin> StatefulOutputPin for Output<'d, T> { 169impl<'d, T: Pin> digital::StatefulOutputPin for Output<'d, T> {
141 /// Is the output pin set as high? 170 /// Is the output pin set as high?
142 fn is_set_high(&self) -> Result<bool, Self::Error> { 171 fn is_set_high(&self) -> Result<bool, Self::Error> {
143 self.is_set_low().map(|v| !v) 172 Ok(self.is_set_high())
144 } 173 }
145 174
146 /// Is the output pin set as low? 175 /// Is the output pin set as low?
147 fn is_set_low(&self) -> Result<bool, Self::Error> { 176 fn is_set_low(&self) -> Result<bool, Self::Error> {
148 // todo 177 Ok(self.is_set_low())
149 Ok(true)
150 } 178 }
151} 179}
152 180
diff --git a/examples/rp/src/bin/blinky.rs b/examples/rp/src/bin/blinky.rs
index e42999912..a162ed2f7 100644
--- a/examples/rp/src/bin/blinky.rs
+++ b/examples/rp/src/bin/blinky.rs
@@ -12,7 +12,6 @@ mod example_common;
12use defmt::*; 12use defmt::*;
13use embassy::executor::Spawner; 13use embassy::executor::Spawner;
14use embassy_rp::{gpio, Peripherals}; 14use embassy_rp::{gpio, Peripherals};
15use embedded_hal::digital::v2::OutputPin;
16use gpio::{Level, Output}; 15use gpio::{Level, Output};
17 16
18#[embassy::main] 17#[embassy::main]
@@ -21,11 +20,11 @@ async fn main(_spawner: Spawner, p: Peripherals) {
21 20
22 loop { 21 loop {
23 info!("led on!"); 22 info!("led on!");
24 led.set_high().unwrap(); 23 led.set_high();
25 cortex_m::asm::delay(1_000_000); 24 cortex_m::asm::delay(1_000_000);
26 25
27 info!("led off!"); 26 info!("led off!");
28 led.set_low().unwrap(); 27 led.set_low();
29 cortex_m::asm::delay(1_000_000); 28 cortex_m::asm::delay(1_000_000);
30 } 29 }
31} 30}
diff --git a/examples/rp/src/bin/button.rs b/examples/rp/src/bin/button.rs
index c4d942ff5..12bc0e0d9 100644
--- a/examples/rp/src/bin/button.rs
+++ b/examples/rp/src/bin/button.rs
@@ -20,10 +20,10 @@ async fn main(_spawner: Spawner, p: Peripherals) {
20 let mut led = Output::new(p.PIN_25, Level::Low); 20 let mut led = Output::new(p.PIN_25, Level::Low);
21 21
22 loop { 22 loop {
23 if button.is_high().unwrap() { 23 if button.is_high() {
24 led.set_high().unwrap(); 24 led.set_high();
25 } else { 25 } else {
26 led.set_low().unwrap(); 26 led.set_low();
27 } 27 }
28 } 28 }
29} 29}