1use crate::{impl_peripheral, peripherals};
2
3trait SealedChannel {}
4
5#[allow(private_bounds)]
6pub trait Channel: SealedChannel + embassy_hal_internal::PeripheralType + Sized {
7 fn number(&self) -> u8;
8 fn degrade(self) -> AnyChannel {
9 AnyChannel {
10 number: self.number() as u8,
11 }
12 }
13}
14
15pub struct AnyChannel {
16 number: u8,
17}
18
19impl_peripheral!(AnyChannel);
20impl SealedChannel for AnyChannel {}
21impl Channel for AnyChannel {
22 fn number(&self) -> u8 {
23 self.number
24 }
25}
26
27macro_rules! impl_exti {
28 ($type:ident, $number:expr) => {
29 impl SealedChannel for peripherals::$type {}
30 impl Channel for peripherals::$type {
31 fn number(&self) -> u8 {
32 $number
33 }
34 }
35 };
36}
37
38mod _exti_8lines {
39 use super::*;
40
41 impl_exti!(EXTI0, 0);
42 impl_exti!(EXTI1, 1);
43 impl_exti!(EXTI2, 2);
44 impl_exti!(EXTI3, 3);
45 impl_exti!(EXTI4, 4);
46 impl_exti!(EXTI5, 5);
47 impl_exti!(EXTI6, 6);
48 impl_exti!(EXTI7, 7);
49}
50
51#[cfg(not(any(ch32v0, ch32m0)))]
52mod _exti_16lines {
53 use super::*;
54
55 impl_exti!(EXTI8, 8);
56 impl_exti!(EXTI9, 9);
57 impl_exti!(EXTI10, 10);
58 impl_exti!(EXTI11, 11);
59 impl_exti!(EXTI12, 12);
60 impl_exti!(EXTI13, 13);
61 impl_exti!(EXTI14, 14);
62 impl_exti!(EXTI15, 15);
63}
64
65#[cfg(any(ch32x0, ch643))]
66mod _exti_24lines {
67 use super::*;
68
69 impl_exti!(EXTI16, 16);
70 impl_exti!(EXTI17, 17);
71 impl_exti!(EXTI18, 18);
72 impl_exti!(EXTI19, 19);
73 impl_exti!(EXTI20, 20);
74 impl_exti!(EXTI21, 21);
75 impl_exti!(EXTI22, 22);
76 impl_exti!(EXTI23, 23);
77}
78
79#[cfg(feature = "exti")]
80pub(crate) use exti_inner::init;
81#[cfg(feature = "exti")]
82pub use exti_inner::*;
83
84#[cfg(feature = "exti")]
85mod exti_inner {
86 use core::future::Future;
87 use core::marker::PhantomData;
88 use core::pin::Pin;
89 use core::task::{Context, Poll};
90
91 use embassy_sync::waitqueue::AtomicWaker;
92 use qingke_rt::interrupt;
93
94 use super::Channel;
95 use crate::gpio::{AnyPin, Input, Level, Pin as GpioPin, Pull};
96 use crate::Peri;
97
98 #[cfg(exti_v4x7)]
99 const EXTI_COUNT: usize = 16;
100 #[cfg(not(exti_v4x7))]
101 const EXTI_COUNT: usize = 24;
102 const NEW_AW: AtomicWaker = AtomicWaker::new();
103 static EXTI_WAKERS: [AtomicWaker; EXTI_COUNT] = [NEW_AW; EXTI_COUNT];
104
105 pub unsafe fn on_irq() {
106 let exti = &crate::pac::EXTI;
107
108 let bits = exti.intfr().read();
109
110 #[cfg(not(exti_v4x7))]
112 let bits = bits.0 & 0x00FFFFFF;
113 #[cfg(exti_v4x7)]
114 let bits = bits.0 & exti.intenr().read().0 & 0x0000FFFF;
115
116 exti.intfr().write(|w| w.0 = bits);
118
119 exti.intenr().modify(|w| w.0 = w.0 & !bits);
120
121 for pin in BitIter(bits) {
123 EXTI_WAKERS[pin as usize].wake();
124 }
125 }
126
127 struct BitIter(u32);
128
129 impl Iterator for BitIter {
130 type Item = u32;
131
132 fn next(&mut self) -> Option<Self::Item> {
133 match self.0.trailing_zeros() {
134 32 => None,
135 b => {
136 self.0 &= !(1 << b);
137 Some(b)
138 }
139 }
140 }
141 }
142
143 pub struct ExtiInput<'d> {
145 pin: Input<'d>,
146 }
147
148 impl<'d> Unpin for ExtiInput<'d> {}
149
150 impl<'d> ExtiInput<'d> {
151 pub fn new<T: GpioPin>(pin: Peri<'d, T>, ch: Peri<'d, T::ExtiChannel>, pull: Pull) -> Self {
152 assert_eq!(pin.pin(), ch.number());
154
155 Self {
156 pin: Input::new(pin, pull),
157 }
158 }
159
160 pub fn is_high(&self) -> bool {
161 self.pin.is_high()
162 }
163
164 pub fn is_low(&self) -> bool {
165 self.pin.is_low()
166 }
167
168 pub fn get_level(&self) -> Level {
169 self.pin.get_level()
170 }
171
172 pub async fn wait_for_high<'a>(&'a mut self) {
173 let fut = ExtiInputFuture::new(self.pin.pin.pin.pin(), self.pin.pin.pin.port(), true, false);
174 if self.is_high() {
175 return;
176 }
177 fut.await
178 }
179
180 pub async fn wait_for_low<'a>(&'a mut self) {
181 let fut = ExtiInputFuture::new(self.pin.pin.pin.pin(), self.pin.pin.pin.port(), false, true);
182 if self.is_low() {
183 return;
184 }
185 fut.await
186 }
187
188 pub async fn wait_for_rising_edge<'a>(&'a mut self) {
189 ExtiInputFuture::new(self.pin.pin.pin.pin(), self.pin.pin.pin.port(), true, false).await
190 }
191
192 pub async fn wait_for_falling_edge<'a>(&'a mut self) {
193 ExtiInputFuture::new(self.pin.pin.pin.pin(), self.pin.pin.pin.port(), false, true).await
194 }
195
196 pub async fn wait_for_any_edge<'a>(&'a mut self) {
197 ExtiInputFuture::new(self.pin.pin.pin.pin(), self.pin.pin.pin.port(), true, true).await
198 }
199 }
200
201 impl<'d> embedded_hal::digital::ErrorType for ExtiInput<'d> {
202 type Error = core::convert::Infallible;
203 }
204
205 impl<'d> embedded_hal_async::digital::Wait for ExtiInput<'d> {
206 async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
207 self.wait_for_high().await;
208 Ok(())
209 }
210
211 async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
212 self.wait_for_low().await;
213 Ok(())
214 }
215
216 async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
217 self.wait_for_rising_edge().await;
218 Ok(())
219 }
220
221 async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
222 self.wait_for_falling_edge().await;
223 Ok(())
224 }
225
226 async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
227 self.wait_for_any_edge().await;
228 Ok(())
229 }
230 }
231
232 #[must_use = "futures do nothing unless you `.await` or poll them"]
233 struct ExtiInputFuture<'a> {
234 pin: u8,
235 phantom: PhantomData<&'a mut AnyPin>,
236 }
237
238 impl<'a> ExtiInputFuture<'a> {
240 fn new(pin: u8, port: u8, rising: bool, falling: bool) -> Self {
241 critical_section::with(|_| {
242 let exti = &crate::pac::EXTI;
243 let afio = &crate::pac::AFIO;
244
245 let port = port as u8;
246 let pin = pin as usize;
247
248 #[cfg(exti_v4x7)]
249 exti.intenr().modify(|w| w.set_mr(pin, false));
250
251 #[cfg(any(afio_v003, afio_v00x))]
252 {
253 afio.exticr().modify(|w| w.set_exti(pin, port));
256 }
257 #[cfg(any(afio_v3, afio_l1, afio_v4x7))]
259 {
260 afio.exticr(pin / 4).modify(|w| w.set_exti(pin % 4, port));
263 }
264 #[cfg(afio_x0)]
265 {
266 afio.exticr(pin / 16).modify(|w| w.set_exti(pin % 16, port));
268 }
269 #[cfg(afio_ch641)]
270 {
271 afio.exticr().modify(|w| w.set_exti(pin, port != 0));
273 }
274
275 #[cfg(not(exti_v4x7))]
277 exti.intenr().modify(|w| w.set_mr(pin, true)); exti.rtenr().modify(|w| w.set_tr(pin, rising));
280 exti.ftenr().modify(|w| w.set_tr(pin, falling));
281
282 #[cfg(exti_v4x7)]
283 {
284 exti.intfr().write(|w| w.set_if_(pin, true));
285 exti.intenr().modify(|w| w.set_mr(pin, true));
286 }
287 });
288
289 Self {
290 pin,
291 phantom: PhantomData,
292 }
293 }
294 }
295
296 impl<'a> Drop for ExtiInputFuture<'a> {
297 fn drop(&mut self) {
298 critical_section::with(|_| {
299 let exti = &crate::pac::EXTI;
300 let pin = self.pin;
301 exti.intenr().modify(|w| w.0 = w.0 & !(1 << pin));
302 });
303 }
304 }
305
306 impl<'a> Future for ExtiInputFuture<'a> {
307 type Output = ();
308
309 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
310 let exti = &crate::pac::EXTI;
311
312 EXTI_WAKERS[self.pin as usize].register(cx.waker());
313
314 if exti.intenr().read().mr(self.pin as _) == false {
315 Poll::Ready(())
317 } else {
318 Poll::Pending
319 }
320 }
321 }
322
323 #[cfg(gpio_x0)]
338 mod irq_impl {
339 use super::*;
340
341 #[interrupt]
342 unsafe fn EXTI7_0() {
343 on_irq();
344 }
345 #[interrupt]
346 unsafe fn EXTI15_8() {
347 on_irq();
348 }
349 #[interrupt]
350 unsafe fn EXTI25_16() {
351 on_irq();
352 }
353
354 pub(crate) unsafe fn init(_cs: critical_section::CriticalSection) {
355 use crate::pac::Interrupt;
356
357 qingke::pfic::enable_interrupt(Interrupt::EXTI7_0 as u8);
358 qingke::pfic::enable_interrupt(Interrupt::EXTI15_8 as u8);
359 qingke::pfic::enable_interrupt(Interrupt::EXTI25_16 as u8);
360 }
361 }
362
363 #[cfg(all(gpio_v3, not(ch641), not(ch32h4)))]
364 mod irq_impl {
365 use super::*;
366
367 #[interrupt]
368 unsafe fn EXTI0() {
369 on_irq();
370 }
371 #[interrupt]
372 unsafe fn EXTI1() {
373 on_irq();
374 }
375 #[interrupt]
376 unsafe fn EXTI2() {
377 on_irq();
378 }
379 #[interrupt]
380 unsafe fn EXTI3() {
381 on_irq();
382 }
383 #[interrupt]
384 unsafe fn EXTI4() {
385 on_irq();
386 }
387 #[interrupt]
388 unsafe fn EXTI9_5() {
389 on_irq();
390 }
391 #[interrupt]
392 unsafe fn EXTI15_10() {
393 on_irq();
394 }
395
396 pub(crate) unsafe fn init(_cs: critical_section::CriticalSection) {
397 use crate::pac::Interrupt;
398
399 qingke::pfic::enable_interrupt(Interrupt::EXTI0 as u8);
400 qingke::pfic::enable_interrupt(Interrupt::EXTI1 as u8);
401 qingke::pfic::enable_interrupt(Interrupt::EXTI2 as u8);
402 qingke::pfic::enable_interrupt(Interrupt::EXTI3 as u8);
403 qingke::pfic::enable_interrupt(Interrupt::EXTI4 as u8);
404 qingke::pfic::enable_interrupt(Interrupt::EXTI9_5 as u8);
405 qingke::pfic::enable_interrupt(Interrupt::EXTI15_10 as u8);
406 }
407 }
408
409 #[cfg(gpio_v0)]
410 mod irq_impl {
411 use super::*;
412
413 #[interrupt]
414 unsafe fn EXTI7_0() {
415 on_irq();
416 }
417
418 pub(crate) unsafe fn init(_cs: critical_section::CriticalSection) {
419 use crate::pac::Interrupt;
420
421 qingke::pfic::enable_interrupt(Interrupt::EXTI7_0 as u8);
422 }
423 }
424
425 #[cfg(all(any(gpio_v3, gpio_h4), any(ch641, ch32h4)))]
428 mod irq_impl {
429 use super::*;
430
431 #[interrupt]
432 unsafe fn EXTI7_0() {
433 on_irq();
434 }
435
436 #[interrupt]
437 unsafe fn EXTI15_8() {
438 on_irq();
439 }
440
441 pub(crate) unsafe fn init(_cs: critical_section::CriticalSection) {
442 use crate::pac::Interrupt;
443
444 qingke::pfic::enable_interrupt(Interrupt::EXTI7_0 as u8);
445 qingke::pfic::enable_interrupt(Interrupt::EXTI15_8 as u8);
446 }
447 }
448
449 pub(crate) use irq_impl::init;
450}