Skip to content

Commit 4740de7

Browse files
committed
Clear pending bits
1 parent f7b1c18 commit 4740de7

File tree

1 file changed

+84
-42
lines changed

1 file changed

+84
-42
lines changed

src/gpiohs.rs

Lines changed: 84 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! High-speed GPIO peripheral (GPIOHS)
22
3-
use crate::pac;
3+
use crate::pac::GPIOHS;
44
use core::marker::PhantomData;
55
use crate::bit_utils::{u32_set_bit, u32_toggle_bit, u32_bit_is_set, u32_bit_is_clear};
66
use embedded_hal::digital::v2::{InputPin, OutputPin};
@@ -23,7 +23,7 @@ pub trait GpiohsExt {
2323
fn split(self) -> Parts;
2424
}
2525

26-
impl GpiohsExt for pac::GPIOHS {
26+
impl GpiohsExt for GPIOHS {
2727
fn split(self) -> Parts {
2828
Parts {
2929
gpiohs0: Gpiohs0 { _mode: PhantomData },
@@ -41,9 +41,9 @@ pub struct Gpiohs0<MODE> {
4141

4242
impl<MODE> Gpiohs0<MODE> {
4343
pub fn into_pull_up_input(self) -> Gpiohs0<Input<PullUp>> {
44-
pac::GPIOHS::set_output_en(0, false);
45-
pac::GPIOHS::set_input_en(0, true);
46-
pac::GPIOHS::set_pullup_en(0, true);
44+
GPIOHS::set_output_en(0, false);
45+
GPIOHS::set_input_en(0, true);
46+
GPIOHS::set_pullup_en(0, true);
4747
Gpiohs0 { _mode: PhantomData }
4848
}
4949

@@ -60,27 +60,40 @@ bitflags::bitflags! {
6060
}
6161

6262
impl<MODE> Gpiohs0<MODE> {
63-
pub fn trigger_on_edge(&self, edge: Edge) {
64-
pac::GPIOHS::set_rise_ie(0, false);
65-
pac::GPIOHS::set_rise_ip(0, true);
66-
pac::GPIOHS::set_fall_ie(0, false);
67-
pac::GPIOHS::set_fall_ip(0, true);
68-
pac::GPIOHS::set_high_ie(0, false);
69-
pac::GPIOHS::set_high_ip(0, true);
70-
pac::GPIOHS::set_low_ie(0, false);
71-
pac::GPIOHS::set_low_ip(0, true);
72-
if edge.contains(Edge::RISING) {
73-
pac::GPIOHS::set_rise_ie(0, true);
74-
}
75-
if edge.contains(Edge::FALLING) {
76-
pac::GPIOHS::set_fall_ie(0, true);
77-
}
78-
if edge.contains(Edge::HIGH) {
79-
pac::GPIOHS::set_high_ie(0, true);
80-
}
81-
if edge.contains(Edge::LOW) {
82-
pac::GPIOHS::set_low_ie(0, true);
83-
}
63+
pub fn trigger_on_edge(&mut self, edge: Edge) {
64+
// clear all pending bits
65+
GPIOHS::clear_rise_ip(0);
66+
GPIOHS::clear_fall_ip(0);
67+
GPIOHS::clear_high_ip(0);
68+
GPIOHS::clear_low_ip(0);
69+
// enable interrupts according to flags
70+
GPIOHS::set_rise_ie(0, edge.contains(Edge::RISING));
71+
GPIOHS::set_fall_ie(0, edge.contains(Edge::FALLING));
72+
GPIOHS::set_high_ie(0, edge.contains(Edge::HIGH));
73+
GPIOHS::set_low_ie(0, edge.contains(Edge::LOW));
74+
}
75+
76+
pub fn clear_interrupt_pending_bits(&mut self) {
77+
if GPIOHS::has_rise_ie(0) {
78+
GPIOHS::set_rise_ie(0, false);
79+
GPIOHS::clear_rise_ip(0);
80+
GPIOHS::set_rise_ie(0, true);
81+
}
82+
if GPIOHS::has_fall_ie(0) {
83+
GPIOHS::set_fall_ie(0, false);
84+
GPIOHS::clear_fall_ip(0);
85+
GPIOHS::set_fall_ie(0, true);
86+
}
87+
if GPIOHS::has_high_ie(0) {
88+
GPIOHS::set_high_ie(0, false);
89+
GPIOHS::clear_high_ip(0);
90+
GPIOHS::set_high_ie(0, true);
91+
}
92+
if GPIOHS::has_low_ie(0) {
93+
GPIOHS::set_low_ie(0, false);
94+
GPIOHS::clear_low_ip(0);
95+
GPIOHS::set_low_ie(0, true);
96+
}
8497
}
8598
}
8699

@@ -89,14 +102,14 @@ impl<MODE> InputPin for Gpiohs0<Input<MODE>> {
89102

90103
fn is_high(&self) -> Result<bool, Self::Error> {
91104
Ok(unsafe {
92-
let p = &(*pac::GPIOHS::ptr()).input_val as *const _ as *const _;
105+
let p = &(*GPIOHS::ptr()).input_val as *const _ as *const _;
93106
u32_bit_is_set(p, 0)
94107
})
95108
}
96109

97110
fn is_low(&self) -> Result<bool, Self::Error> {
98111
Ok(unsafe {
99-
let p = &(*pac::GPIOHS::ptr()).input_val as *const _ as *const _;
112+
let p = &(*GPIOHS::ptr()).input_val as *const _ as *const _;
100113
u32_bit_is_clear(p, 0)
101114
})
102115
}
@@ -107,23 +120,23 @@ impl<MODE> OutputPin for Gpiohs0<Output<MODE>> {
107120

108121
fn set_high(&mut self) -> Result<(), Self::Error> {
109122
unsafe {
110-
let p = &(*pac::GPIOHS::ptr()).output_val as *const _ as *mut _;
123+
let p = &(*GPIOHS::ptr()).output_val as *const _ as *mut _;
111124
u32_set_bit(p, true, 0);
112125
}
113126
Ok(())
114127
}
115128

116129
fn set_low(&mut self) -> Result<(), Self::Error> {
117130
unsafe {
118-
let p = &(*pac::GPIOHS::ptr()).output_val as *const _ as *mut _;
131+
let p = &(*GPIOHS::ptr()).output_val as *const _ as *mut _;
119132
u32_set_bit(p, false, 0);
120133
}
121134
Ok(())
122135
}
123136
}
124137

125138
trait GpiohsAccess {
126-
fn peripheral() -> &'static mut pac::gpiohs::RegisterBlock;
139+
fn peripheral() -> &'static mut crate::pac::gpiohs::RegisterBlock;
127140

128141
fn set_drive(index: usize, bit: bool) {
129142
unsafe {
@@ -202,10 +215,10 @@ trait GpiohsAccess {
202215
}
203216
}
204217

205-
fn set_rise_ip(index: usize, bit: bool) {
218+
fn clear_rise_ip(index: usize) {
206219
unsafe {
207220
let p = &mut Self::peripheral().rise_ip as *mut _ as *mut _;
208-
u32_set_bit(p, bit, index);
221+
u32_set_bit(p, true, index);
209222
}
210223
}
211224

@@ -216,10 +229,10 @@ trait GpiohsAccess {
216229
}
217230
}
218231

219-
fn set_fall_ip(index: usize, bit: bool) {
232+
fn clear_fall_ip(index: usize) {
220233
unsafe {
221234
let p = &mut Self::peripheral().fall_ip as *mut _ as *mut _;
222-
u32_set_bit(p, bit, index);
235+
u32_set_bit(p, true, index);
223236
}
224237
}
225238

@@ -230,10 +243,10 @@ trait GpiohsAccess {
230243
}
231244
}
232245

233-
fn set_high_ip(index: usize, bit: bool) {
246+
fn clear_high_ip(index: usize,) {
234247
unsafe {
235248
let p = &mut Self::peripheral().high_ip as *mut _ as *mut _;
236-
u32_set_bit(p, bit, index);
249+
u32_set_bit(p, true, index);
237250
}
238251
}
239252

@@ -244,16 +257,45 @@ trait GpiohsAccess {
244257
}
245258
}
246259

247-
fn set_low_ip(index: usize, bit: bool) {
260+
fn clear_low_ip(index: usize) {
248261
unsafe {
249262
let p = &mut Self::peripheral().low_ip as *mut _ as *mut _;
250-
u32_set_bit(p, bit, index);
263+
u32_set_bit(p, true, index);
251264
}
252265
}
266+
267+
fn has_rise_ie(index: usize) -> bool {
268+
unsafe {
269+
let p = &mut Self::peripheral().rise_ie as *mut _ as *mut _;
270+
u32_bit_is_set(p, index)
271+
}
272+
}
273+
274+
fn has_fall_ie(index: usize) -> bool {
275+
unsafe {
276+
let p = &mut Self::peripheral().fall_ie as *mut _ as *mut _;
277+
u32_bit_is_set(p, index)
278+
}
279+
}
280+
281+
fn has_high_ie(index: usize) -> bool {
282+
unsafe {
283+
let p = &mut Self::peripheral().high_ie as *mut _ as *mut _;
284+
u32_bit_is_set(p, index)
285+
}
286+
}
287+
288+
fn has_low_ie(index: usize) -> bool {
289+
unsafe {
290+
let p = &mut Self::peripheral().low_ie as *mut _ as *mut _;
291+
u32_bit_is_set(p, index)
292+
}
293+
}
294+
253295
}
254296

255-
impl GpiohsAccess for pac::GPIOHS {
256-
fn peripheral() -> &'static mut pac::gpiohs::RegisterBlock {
257-
unsafe { &mut *(pac::GPIOHS::ptr() as *mut _) }
297+
impl GpiohsAccess for GPIOHS {
298+
fn peripheral() -> &'static mut crate::pac::gpiohs::RegisterBlock {
299+
unsafe { &mut *(GPIOHS::ptr() as *mut _) }
258300
}
259301
}

0 commit comments

Comments
 (0)