1+ //! HAL interface for the GPIOTE peripheral
2+ //!
3+ //! The GPIO tasks and events (GPIOTE) module provides functionality for accessing GPIO pins using tasks and events.
4+
15#[ cfg( feature = "51" ) ]
26use crate :: target:: { gpio, GPIO as P0 } ;
37
@@ -23,11 +27,13 @@ const NUM_CHANNELS: usize = 8;
2327#[ cfg( feature = "51" ) ]
2428const NUM_CHANNELS : usize = 4 ;
2529
30+ /// A safe wrapper around the GPIOTE peripheral.
2631pub struct Gpiote {
2732 gpiote : GPIOTE ,
2833}
2934
3035impl Gpiote {
36+ /// Takes ownership of the `GPIOTE` peripheral, returning a safe wrapper.
3137 pub fn new ( gpiote : GPIOTE ) -> Self {
3238 Self { gpiote }
3339 }
@@ -73,12 +79,13 @@ impl Gpiote {
7379 }
7480 }
7581
82+ /// Marks all GPIOTE events as handled
7683 pub fn reset_events ( & self ) {
77- // Mark all events as handled
7884 ( 0 ..NUM_CHANNELS ) . for_each ( |ch| self . gpiote . events_in [ ch] . write ( |w| w) ) ;
7985 self . gpiote . events_port . write ( |w| w) ;
8086 }
8187
88+ /// Consumes `self` and return back the raw `GPIOTE` peripheral.
8289 pub fn free ( self ) -> GPIOTE {
8390 self . gpiote
8491 }
@@ -90,14 +97,15 @@ pub struct GpioteChannel<'a> {
9097}
9198
9299impl < ' a > GpioteChannel < ' _ > {
100+ /// Configures the channel as an event input with associated pin
93101 pub fn input_pin < P : GpioteInputPin > ( & ' a self , pin : & ' a P ) -> GpioteChannelEvent < ' a , P > {
94102 GpioteChannelEvent {
95103 gpiote : & self . gpiote ,
96104 pin : pin,
97105 channel : self . channel ,
98106 }
99107 }
100-
108+ /// Configures the channel as a task output with associated pin
101109 pub fn output_pin < P : GpioteOutputPin > ( & ' a self , pin : P ) -> GpioteTask < ' a , P > {
102110 GpioteTask {
103111 gpiote : & self . gpiote ,
@@ -107,47 +115,49 @@ impl<'a> GpioteChannel<'_> {
107115 }
108116 }
109117
118+ /// Checks if the channel event has been triggered
110119 pub fn is_event_triggered ( & self ) -> bool {
111120 self . gpiote . events_in [ self . channel ] . read ( ) . bits ( ) != 0
112121 }
113-
122+ /// Resets channel events
114123 pub fn reset_events ( & self ) {
115124 self . gpiote . events_in [ self . channel ] . write ( |w| w) ;
116125 }
117126
127+ /// Triggers `task out` (as configured with task_out_polarity, defaults to Toggle)
118128 pub fn out ( & self ) {
119129 self . gpiote . tasks_out [ self . channel ] . write ( |w| unsafe { w. bits ( 1 ) } ) ;
120130 }
121-
131+ /// Triggers `task set` (set associated pin high)
122132 #[ cfg( not( feature = "51" ) ) ]
123133 pub fn set ( & self ) {
124134 self . gpiote . tasks_set [ self . channel ] . write ( |w| unsafe { w. bits ( 1 ) } ) ;
125135 }
126-
136+ /// Triggers `task clear` (set associated pin low)
127137 #[ cfg( not( feature = "51" ) ) ]
128138 pub fn clear ( & self ) {
129139 self . gpiote . tasks_clr [ self . channel ] . write ( |w| unsafe { w. bits ( 1 ) } ) ;
130140 }
131141
142+ /// Returns reference to channel event endpoint for PPI
132143 pub fn event ( & self ) -> & Reg < u32 , _EVENTS_IN > {
133- // Return reference to event for PPI
134144 & self . gpiote . events_in [ self . channel ]
135145 }
136146
147+ /// Returns reference to task_out endpoint for PPI
137148 pub fn task_out ( & self ) -> & Reg < u32 , _TASKS_OUT > {
138- // Return reference to task_out for PPI
139149 & self . gpiote . tasks_out [ self . channel ]
140150 }
141151
152+ /// Returns reference to task_clr endpoint for PPI
142153 #[ cfg( not( feature = "51" ) ) ]
143154 pub fn task_clr ( & self ) -> & Reg < u32 , _TASKS_CLR > {
144- // Return reference to task_clr for PPI
145155 & self . gpiote . tasks_clr [ self . channel ]
146156 }
147157
158+ /// Returns reference to task_set endpoint for PPI
148159 #[ cfg( not( feature = "51" ) ) ]
149160 pub fn task_set ( & self ) -> & Reg < u32 , _TASKS_SET > {
150- // Return reference to task_set for PPI
151161 & self . gpiote . tasks_set [ self . channel ]
152162 }
153163}
@@ -157,26 +167,28 @@ pub struct GpiotePort<'a> {
157167}
158168
159169impl < ' a > GpiotePort < ' _ > {
170+ /// Configures associated pin as port event trigger
160171 pub fn input_pin < P : GpioteInputPin > ( & ' a self , pin : & ' a P ) -> GpiotePortEvent < ' a , P > {
161172 GpiotePortEvent { pin }
162173 }
174+ /// Enables GPIOTE interrupt for port events
163175 pub fn enable_interrupt ( & self ) {
164- // Enable port interrupt
165176 self . gpiote . intenset . write ( |w| w. port ( ) . set ( ) ) ;
166177 }
178+ /// Disables GPIOTE interrupt for port events
167179 pub fn disable_interrupt ( & self ) {
168- // Disable port interrupt
169180 self . gpiote . intenclr . write ( |w| w. port ( ) . set_bit ( ) ) ;
170181 }
182+ /// Checks if port event has been triggered
171183 pub fn is_event_triggered ( & self ) -> bool {
172184 self . gpiote . events_port . read ( ) . bits ( ) != 0
173185 }
186+ /// Marks port events as handled
174187 pub fn reset_events ( & self ) {
175- // Mark port events as handled
176188 self . gpiote . events_port . write ( |w| w) ;
177189 }
190+ /// Returns reference to port event endpoint for PPI
178191 pub fn event ( & self ) -> & Reg < u32 , _EVENTS_PORT > {
179- // Return reference to event for PPI
180192 & self . gpiote . events_port
181193 }
182194}
@@ -188,28 +200,29 @@ pub struct GpioteChannelEvent<'a, P: GpioteInputPin> {
188200}
189201
190202impl < ' a , P : GpioteInputPin > GpioteChannelEvent < ' _ , P > {
203+ /// Generates event on falling edge
191204 pub fn hi_to_lo ( & self ) -> & Self {
192205 config_channel_event_pin ( self . gpiote , self . channel , self . pin , EventPolarity :: HiToLo ) ;
193206 self
194207 }
195-
208+ /// Generates event on rising edge
196209 pub fn lo_to_hi ( & self ) -> & Self {
197210 config_channel_event_pin ( self . gpiote , self . channel , self . pin , EventPolarity :: LoToHi ) ;
198211 self
199212 }
200-
213+ /// Generates event on any pin activity
201214 pub fn toggle ( & self ) -> & Self {
202215 config_channel_event_pin ( self . gpiote , self . channel , self . pin , EventPolarity :: Toggle ) ;
203216 self
204217 }
205-
218+ /// No event is generated on pin activity
206219 pub fn none ( & self ) -> & Self {
207220 config_channel_event_pin ( self . gpiote , self . channel , self . pin , EventPolarity :: None ) ;
208221 self
209222 }
210223
224+ /// Enables GPIOTE interrupt for pin
211225 pub fn enable_interrupt ( & self ) -> & Self {
212- // Enable interrupt for pin
213226 unsafe {
214227 self . gpiote
215228 . intenset
@@ -218,8 +231,8 @@ impl<'a, P: GpioteInputPin> GpioteChannelEvent<'_, P> {
218231 self
219232 }
220233
234+ /// Disables GPIOTE interrupt for pin
221235 pub fn disable_interrupt ( & self ) -> & Self {
222- // Disable interrupt for pin
223236 unsafe {
224237 self . gpiote
225238 . intenclr
@@ -252,12 +265,15 @@ pub struct GpiotePortEvent<'a, P: GpioteInputPin> {
252265}
253266
254267impl < ' a , P : GpioteInputPin > GpiotePortEvent < ' _ , P > {
268+ /// Generates event on pin low
255269 pub fn low ( & self ) {
256270 config_port_event_pin ( self . pin , PortEventSense :: Low ) ;
257271 }
272+ /// Generates event on pin high
258273 pub fn high ( & self ) {
259274 config_port_event_pin ( self . pin , PortEventSense :: High ) ;
260275 }
276+ /// No event is generated on pin activity
261277 pub fn disabled ( & self ) {
262278 config_port_event_pin ( self . pin , PortEventSense :: Disabled ) ;
263279 }
@@ -290,6 +306,7 @@ pub struct GpioteTask<'a, P: GpioteOutputPin> {
290306}
291307
292308impl < ' a , P : GpioteOutputPin > GpioteTask < ' _ , P > {
309+ /// Sets initial task output pin state to high
293310 pub fn init_high ( & self ) {
294311 config_channel_task_pin (
295312 self . gpiote ,
@@ -299,7 +316,7 @@ impl<'a, P: GpioteOutputPin> GpioteTask<'_, P> {
299316 Level :: High ,
300317 ) ;
301318 }
302-
319+ /// Sets initial task output pin state to low
303320 pub fn init_low ( & self ) {
304321 config_channel_task_pin (
305322 self . gpiote ,
@@ -309,7 +326,7 @@ impl<'a, P: GpioteOutputPin> GpioteTask<'_, P> {
309326 Level :: Low ,
310327 ) ;
311328 }
312-
329+ /// Configures polarity of the `task out` operation
313330 pub fn task_out_polarity ( & mut self , polarity : TaskOutPolarity ) -> & mut Self {
314331 self . task_out_polarity = polarity;
315332 self
@@ -338,6 +355,7 @@ fn config_channel_task_pin<P: GpioteOutputPin>(
338355 } ) ;
339356}
340357
358+ /// Polarity of the `task out` operation
341359pub enum TaskOutPolarity {
342360 Set ,
343361 Clear ,
@@ -357,6 +375,7 @@ pub enum PortEventSense {
357375 Low ,
358376}
359377
378+ /// Trait to represent event input pin
360379pub trait GpioteInputPin {
361380 fn pin ( & self ) -> u8 ;
362381 fn port ( & self ) -> Port ;
@@ -389,6 +408,7 @@ impl GpioteInputPin for Pin<Input<Floating>> {
389408 }
390409}
391410
411+ /// Trait to represent task output pin
392412pub trait GpioteOutputPin {
393413 fn pin ( & self ) -> u8 ;
394414}
0 commit comments