aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml1
-rw-r--r--embassy-stm32l0/Cargo.toml26
-rw-r--r--embassy-stm32l0/src/exti.rs220
-rw-r--r--embassy-stm32l0/src/fmt.rs119
-rw-r--r--embassy-stm32l0/src/interrupt.rs162
-rw-r--r--embassy-stm32l0/src/lib.rs32
6 files changed, 560 insertions, 0 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 069437317..31fe26476 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,6 +5,7 @@ members = [
5 "embassy-traits", 5 "embassy-traits",
6 "embassy-nrf", 6 "embassy-nrf",
7 "embassy-stm32f4", 7 "embassy-stm32f4",
8 "embassy-stm32l0",
8 "embassy-nrf-examples", 9 "embassy-nrf-examples",
9 "embassy-stm32f4-examples", 10 "embassy-stm32f4-examples",
10 "embassy-macros", 11 "embassy-macros",
diff --git a/embassy-stm32l0/Cargo.toml b/embassy-stm32l0/Cargo.toml
new file mode 100644
index 000000000..ad668b8d3
--- /dev/null
+++ b/embassy-stm32l0/Cargo.toml
@@ -0,0 +1,26 @@
1[package]
2name = "embassy-stm32l0"
3version = "0.1.0"
4authors = ["Michael Beaumont <[email protected]>"]
5edition = "2018"
6
7[features]
8defmt-trace = [ ]
9defmt-debug = [ ]
10defmt-info = [ ]
11defmt-warn = [ ]
12defmt-error = [ ]
13
14stm32l0x1 = ["stm32l0xx-hal/stm32l0x1"]
15stm32l0x2 = ["stm32l0xx-hal/stm32l0x2"]
16stm32l0x3 = ["stm32l0xx-hal/stm32l0x3"]
17
18[dependencies]
19embassy = { version = "0.1.0", path = "../embassy" }
20defmt = { version = "0.2.0", optional = true }
21log = { version = "0.4.11", optional = true }
22cortex-m-rt = "0.6.13"
23cortex-m = "0.7.1"
24embedded-hal = { version = "0.2.4" }
25embedded-dma = { version = "0.1.2" }
26stm32l0xx-hal = { version = "0.6.2", features = ["rt"], git = "https://github.com/stm32-rs/stm32l0xx-hal.git"}
diff --git a/embassy-stm32l0/src/exti.rs b/embassy-stm32l0/src/exti.rs
new file mode 100644
index 000000000..84c38ccc7
--- /dev/null
+++ b/embassy-stm32l0/src/exti.rs
@@ -0,0 +1,220 @@
1use core::future::Future;
2use core::mem;
3use core::pin::Pin;
4
5use embassy::interrupt::Interrupt;
6use embassy::traits::gpio::{WaitForFallingEdge, WaitForRisingEdge};
7use embassy::util::InterruptFuture;
8
9use crate::hal::{
10 exti::{Exti, ExtiLine, GpioLine, TriggerEdge},
11 gpio,
12 syscfg::SYSCFG,
13};
14use crate::interrupt;
15use crate::pac::EXTI;
16
17pub struct ExtiManager {
18 syscfg: SYSCFG,
19}
20
21impl<'a> ExtiManager {
22 pub fn new(_exti: Exti, syscfg: SYSCFG) -> Self {
23 Self { syscfg }
24 }
25
26 pub fn new_pin<T, I>(&'static mut self, pin: T, interrupt: I) -> ExtiPin<T, I>
27 where
28 T: PinWithInterrupt<Interrupt = I>,
29 I: Interrupt,
30 {
31 ExtiPin {
32 pin,
33 interrupt,
34 mgr: self,
35 }
36 }
37}
38
39pub struct ExtiPin<T, I> {
40 pin: T,
41 interrupt: I,
42 mgr: &'static mut ExtiManager,
43}
44
45impl<T: PinWithInterrupt<Interrupt = I> + 'static, I: Interrupt + 'static> WaitForRisingEdge
46 for ExtiPin<T, I>
47{
48 type Future<'a> = impl Future<Output = ()> + 'a;
49
50 fn wait_for_rising_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> {
51 let s = unsafe { self.get_unchecked_mut() };
52
53 let line = s.pin.line();
54 Exti::unpend(line);
55
56 async move {
57 let exti: EXTI = unsafe { mem::transmute(()) };
58 let mut exti = Exti::new(exti);
59 let fut = InterruptFuture::new(&mut s.interrupt);
60
61 exti.listen_gpio(&mut s.mgr.syscfg, s.pin.port(), line, TriggerEdge::Rising);
62 fut.await;
63
64 Exti::unpend(line);
65 }
66 }
67}
68
69impl<T: PinWithInterrupt<Interrupt = I> + 'static, I: Interrupt + 'static> WaitForFallingEdge
70 for ExtiPin<T, I>
71{
72 type Future<'a> = impl Future<Output = ()> + 'a;
73
74 fn wait_for_falling_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> {
75 let s = unsafe { self.get_unchecked_mut() };
76
77 let line = s.pin.line();
78 Exti::unpend(line);
79
80 async move {
81 let exti: EXTI = unsafe { mem::transmute(()) };
82 let mut exti = Exti::new(exti);
83 let fut = InterruptFuture::new(&mut s.interrupt);
84
85 exti.listen_gpio(&mut s.mgr.syscfg, s.pin.port(), line, TriggerEdge::Falling);
86 fut.await;
87
88 Exti::unpend(line);
89 }
90 }
91}
92
93mod private {
94 pub trait Sealed {}
95}
96
97pub trait PinWithInterrupt: private::Sealed {
98 type Interrupt;
99 fn port(&self) -> gpio::Port;
100 fn line(&self) -> GpioLine;
101}
102
103macro_rules! exti {
104 ($($PER:ident => ($set:ident, $pin:ident),)+) => {
105 $(
106 impl<T> private::Sealed for gpio::$set::$pin<T> {}
107 impl<T> PinWithInterrupt for gpio::$set::$pin<T> {
108 type Interrupt = interrupt::$PER;
109 fn port(&self) -> gpio::Port {
110 self.port()
111 }
112 fn line(&self) -> GpioLine {
113 GpioLine::from_raw_line(self.pin_number()).unwrap()
114 }
115 }
116 )+
117 }
118}
119
120exti! {
121 EXTI0_1 => (gpioa, PA0),
122 EXTI0_1 => (gpioa, PA1),
123 EXTI2_3 => (gpioa, PA2),
124 EXTI2_3 => (gpioa, PA3),
125 EXTI4_15 => (gpioa, PA4),
126 EXTI4_15 => (gpioa, PA5),
127 EXTI4_15 => (gpioa, PA6),
128 EXTI4_15 => (gpioa, PA7),
129 EXTI4_15 => (gpioa, PA8),
130 EXTI4_15 => (gpioa, PA9),
131 EXTI4_15 => (gpioa, PA10),
132 EXTI4_15 => (gpioa, PA11),
133 EXTI4_15 => (gpioa, PA12),
134 EXTI4_15 => (gpioa, PA13),
135 EXTI4_15 => (gpioa, PA14),
136 EXTI4_15 => (gpioa, PA15),
137}
138
139exti! {
140 EXTI0_1 => (gpiob, PB0),
141 EXTI0_1 => (gpiob, PB1),
142 EXTI2_3 => (gpiob, PB2),
143 EXTI2_3 => (gpiob, PB3),
144 EXTI4_15 => (gpiob, PB4),
145 EXTI4_15 => (gpiob, PB5),
146 EXTI4_15 => (gpiob, PB6),
147 EXTI4_15 => (gpiob, PB7),
148 EXTI4_15 => (gpiob, PB8),
149 EXTI4_15 => (gpiob, PB9),
150 EXTI4_15 => (gpiob, PB10),
151 EXTI4_15 => (gpiob, PB11),
152 EXTI4_15 => (gpiob, PB12),
153 EXTI4_15 => (gpiob, PB13),
154 EXTI4_15 => (gpiob, PB14),
155 EXTI4_15 => (gpiob, PB15),
156}
157
158exti! {
159 EXTI0_1 => (gpioc, PC0),
160 EXTI0_1 => (gpioc, PC1),
161 EXTI2_3 => (gpioc, PC2),
162 EXTI2_3 => (gpioc, PC3),
163 EXTI4_15 => (gpioc, PC4),
164 EXTI4_15 => (gpioc, PC5),
165 EXTI4_15 => (gpioc, PC6),
166 EXTI4_15 => (gpioc, PC7),
167 EXTI4_15 => (gpioc, PC8),
168 EXTI4_15 => (gpioc, PC9),
169 EXTI4_15 => (gpioc, PC10),
170 EXTI4_15 => (gpioc, PC11),
171 EXTI4_15 => (gpioc, PC12),
172 EXTI4_15 => (gpioc, PC13),
173 EXTI4_15 => (gpioc, PC14),
174 EXTI4_15 => (gpioc, PC15),
175}
176
177exti! {
178 EXTI0_1 => (gpiod, PD0),
179 EXTI0_1 => (gpiod, PD1),
180 EXTI2_3 => (gpiod, PD2),
181 EXTI2_3 => (gpiod, PD3),
182 EXTI4_15 => (gpiod, PD4),
183 EXTI4_15 => (gpiod, PD5),
184 EXTI4_15 => (gpiod, PD6),
185 EXTI4_15 => (gpiod, PD7),
186 EXTI4_15 => (gpiod, PD8),
187 EXTI4_15 => (gpiod, PD9),
188 EXTI4_15 => (gpiod, PD10),
189 EXTI4_15 => (gpiod, PD11),
190 EXTI4_15 => (gpiod, PD12),
191 EXTI4_15 => (gpiod, PD13),
192 EXTI4_15 => (gpiod, PD14),
193 EXTI4_15 => (gpiod, PD15),
194}
195
196exti! {
197 EXTI0_1 => (gpioe, PE0),
198 EXTI0_1 => (gpioe, PE1),
199 EXTI2_3 => (gpioe, PE2),
200 EXTI2_3 => (gpioe, PE3),
201 EXTI4_15 => (gpioe, PE4),
202 EXTI4_15 => (gpioe, PE5),
203 EXTI4_15 => (gpioe, PE6),
204 EXTI4_15 => (gpioe, PE7),
205 EXTI4_15 => (gpioe, PE8),
206 EXTI4_15 => (gpioe, PE9),
207 EXTI4_15 => (gpioe, PE10),
208 EXTI4_15 => (gpioe, PE11),
209 EXTI4_15 => (gpioe, PE12),
210 EXTI4_15 => (gpioe, PE13),
211 EXTI4_15 => (gpioe, PE14),
212 EXTI4_15 => (gpioe, PE15),
213}
214
215exti! {
216 EXTI0_1 => (gpioh, PH0),
217 EXTI0_1 => (gpioh, PH1),
218 EXTI4_15 => (gpioh, PH9),
219 EXTI4_15 => (gpioh, PH10),
220}
diff --git a/embassy-stm32l0/src/fmt.rs b/embassy-stm32l0/src/fmt.rs
new file mode 100644
index 000000000..1be1057a7
--- /dev/null
+++ b/embassy-stm32l0/src/fmt.rs
@@ -0,0 +1,119 @@
1#![macro_use]
2#![allow(clippy::module_inception)]
3
4#[cfg(all(feature = "defmt", feature = "log"))]
5compile_error!("You may not enable both `defmt` and `log` features.");
6
7pub use fmt::*;
8
9#[cfg(feature = "defmt")]
10mod fmt {
11 pub use defmt::{
12 assert, assert_eq, assert_ne, debug, debug_assert, debug_assert_eq, debug_assert_ne, error,
13 info, panic, todo, trace, unreachable, unwrap, warn,
14 };
15}
16
17#[cfg(feature = "log")]
18mod fmt {
19 pub use core::{
20 assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo,
21 unreachable,
22 };
23 pub use log::{debug, error, info, trace, warn};
24}
25
26#[cfg(not(any(feature = "defmt", feature = "log")))]
27mod fmt {
28 #![macro_use]
29
30 pub use core::{
31 assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo,
32 unreachable,
33 };
34
35 #[macro_export]
36 macro_rules! trace {
37 ($($msg:expr),+ $(,)?) => {
38 ()
39 };
40 }
41
42 #[macro_export]
43 macro_rules! debug {
44 ($($msg:expr),+ $(,)?) => {
45 ()
46 };
47 }
48
49 #[macro_export]
50 macro_rules! info {
51 ($($msg:expr),+ $(,)?) => {
52 ()
53 };
54 }
55
56 #[macro_export]
57 macro_rules! warn {
58 ($($msg:expr),+ $(,)?) => {
59 ()
60 };
61 }
62
63 #[macro_export]
64 macro_rules! error {
65 ($($msg:expr),+ $(,)?) => {
66 ()
67 };
68 }
69}
70
71#[cfg(not(feature = "defmt"))]
72#[macro_export]
73macro_rules! unwrap {
74 ($arg:expr) => {
75 match $crate::fmt::Try::into_result($arg) {
76 ::core::result::Result::Ok(t) => t,
77 ::core::result::Result::Err(e) => {
78 ::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e);
79 }
80 }
81 };
82 ($arg:expr, $($msg:expr),+ $(,)? ) => {
83 match $crate::fmt::Try::into_result($arg) {
84 ::core::result::Result::Ok(t) => t,
85 ::core::result::Result::Err(e) => {
86 ::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e);
87 }
88 }
89 }
90}
91
92#[derive(Debug, Copy, Clone, Eq, PartialEq)]
93pub struct NoneError;
94
95pub trait Try {
96 type Ok;
97 type Error;
98 fn into_result(self) -> Result<Self::Ok, Self::Error>;
99}
100
101impl<T> Try for Option<T> {
102 type Ok = T;
103 type Error = NoneError;
104
105 #[inline]
106 fn into_result(self) -> Result<T, NoneError> {
107 self.ok_or(NoneError)
108 }
109}
110
111impl<T, E> Try for Result<T, E> {
112 type Ok = T;
113 type Error = E;
114
115 #[inline]
116 fn into_result(self) -> Self {
117 self
118 }
119}
diff --git a/embassy-stm32l0/src/interrupt.rs b/embassy-stm32l0/src/interrupt.rs
new file mode 100644
index 000000000..499f3f275
--- /dev/null
+++ b/embassy-stm32l0/src/interrupt.rs
@@ -0,0 +1,162 @@
1//! Interrupt management
2use crate::pac::NVIC_PRIO_BITS;
3
4// Re-exports
5pub use cortex_m::interrupt::{CriticalSection, Mutex};
6pub use embassy::interrupt::{declare, take, Interrupt};
7
8#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
9#[cfg_attr(feature = "defmt", derive(defmt::Format))]
10#[repr(u8)]
11pub enum Priority {
12 Level0 = 0,
13 Level1 = 1,
14 Level2 = 2,
15 Level3 = 3,
16 Level4 = 4,
17 Level5 = 5,
18 Level6 = 6,
19 Level7 = 7,
20 Level8 = 8,
21 Level9 = 9,
22 Level10 = 10,
23 Level11 = 11,
24 Level12 = 12,
25 Level13 = 13,
26 Level14 = 14,
27 Level15 = 15,
28}
29
30impl From<u8> for Priority {
31 fn from(priority: u8) -> Self {
32 match priority >> (8 - NVIC_PRIO_BITS) {
33 0 => Self::Level0,
34 1 => Self::Level1,
35 2 => Self::Level2,
36 3 => Self::Level3,
37 4 => Self::Level4,
38 5 => Self::Level5,
39 6 => Self::Level6,
40 7 => Self::Level7,
41 8 => Self::Level8,
42 9 => Self::Level9,
43 10 => Self::Level10,
44 11 => Self::Level11,
45 12 => Self::Level12,
46 13 => Self::Level13,
47 14 => Self::Level14,
48 15 => Self::Level15,
49 _ => unreachable!(),
50 }
51 }
52}
53
54impl From<Priority> for u8 {
55 fn from(p: Priority) -> Self {
56 (p as u8) << (8 - NVIC_PRIO_BITS)
57 }
58}
59
60#[cfg(feature = "stm32l0x1")]
61mod irqs {
62 use super::*;
63 declare!(WWDG);
64 declare!(PVD);
65 declare!(RTC);
66 declare!(FLASH);
67 declare!(RCC);
68 declare!(EXTI0_1);
69 declare!(EXTI2_3);
70 declare!(EXTI4_15);
71 declare!(DMA1_CHANNEL1);
72 declare!(DMA1_CHANNEL2_3);
73 declare!(DMA1_CHANNEL4_7);
74 declare!(ADC_COMP);
75 declare!(LPTIM1);
76 declare!(USART4_USART5);
77 declare!(TIM2);
78 declare!(TIM3);
79 declare!(TIM6);
80 declare!(TIM7);
81 declare!(TIM21);
82 declare!(I2C3);
83 declare!(TIM22);
84 declare!(I2C1);
85 declare!(I2C2);
86 declare!(SPI1);
87 declare!(SPI2);
88 declare!(USART1);
89 declare!(USART2);
90 declare!(AES_RNG_LPUART1);
91}
92
93#[cfg(feature = "stm32l0x2")]
94mod irqs {
95 use super::*;
96 declare!(WWDG);
97 declare!(PVD);
98 declare!(RTC);
99 declare!(RCC);
100 declare!(EXTI0_1);
101 declare!(EXTI2_3);
102 declare!(EXTI4_15);
103 declare!(TSC);
104 declare!(DMA1_CHANNEL1);
105 declare!(DMA1_CHANNEL2_3);
106 declare!(DMA1_CHANNEL4_7);
107 declare!(ADC_COMP);
108 declare!(LPTIM1);
109 declare!(USART4_USART5);
110 declare!(TIM2);
111 declare!(TIM3);
112 declare!(TIM6_DAC);
113 declare!(TIM7);
114 declare!(TIM21);
115 declare!(I2C3);
116 declare!(TIM22);
117 declare!(I2C1);
118 declare!(I2C2);
119 declare!(SPI1);
120 declare!(SPI2);
121 declare!(USART1);
122 declare!(USART2);
123 declare!(AES_RNG_LPUART1);
124 declare!(USB);
125}
126
127#[cfg(feature = "stm32l0x3")]
128mod irqs {
129 use super::*;
130 declare!(WWDG);
131 declare!(PVD);
132 declare!(RTC);
133 declare!(RCC);
134 declare!(EXTI0_1);
135 declare!(EXTI2_3);
136 declare!(EXTI4_15);
137 declare!(TSC);
138 declare!(DMA1_CHANNEL1);
139 declare!(DMA1_CHANNEL2_3);
140 declare!(DMA1_CHANNEL4_7);
141 declare!(ADC_COMP);
142 declare!(LPTIM1);
143 declare!(USART4_USART5);
144 declare!(TIM2);
145 declare!(TIM3);
146 declare!(TIM6_DAC);
147 declare!(TIM7);
148 declare!(TIM21);
149 declare!(I2C3);
150 declare!(TIM22);
151 declare!(I2C1);
152 declare!(I2C2);
153 declare!(SPI1);
154 declare!(SPI2);
155 declare!(USART1);
156 declare!(USART2);
157 declare!(AES_RNG_LPUART1);
158 declare!(LCD);
159 declare!(USB);
160}
161
162pub use irqs::*;
diff --git a/embassy-stm32l0/src/lib.rs b/embassy-stm32l0/src/lib.rs
new file mode 100644
index 000000000..c4fa15c54
--- /dev/null
+++ b/embassy-stm32l0/src/lib.rs
@@ -0,0 +1,32 @@
1#![no_std]
2#![feature(generic_associated_types)]
3#![feature(asm)]
4#![feature(type_alias_impl_trait)]
5#![allow(incomplete_features)]
6
7#[cfg(not(any(
8 feature = "stm32l0x1",
9 feature = "stm32l0x2",
10 feature = "stm32l0x3",
11)))]
12compile_error!(
13 "No chip feature activated. You must activate exactly one of the following features: "
14);
15
16#[cfg(any(
17 all(feature = "stm32l0x1", feature = "stm32l0x2"),
18 all(feature = "stm32l0x1", feature = "stm32l0x3"),
19 all(feature = "stm32l0x2", feature = "stm32l0x3"),
20))]
21compile_error!(
22 "Multile chip features activated. You must activate exactly one of the following features: "
23);
24
25pub use stm32l0xx_hal as hal;
26pub use stm32l0xx_hal::pac;
27
28// This mod MUST go first, so that the others see its macros.
29pub(crate) mod fmt;
30
31pub mod exti;
32pub mod interrupt;