aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThales Fragoso <[email protected]>2021-07-04 18:34:37 -0300
committerBob McWhirter <[email protected]>2021-07-13 10:08:43 -0400
commit91521a86a0b273a8f375eff205963df14fa93f4f (patch)
treeb2e8631652938b68cf5f744c31cd1411ad47f451
parenta56ddfdc04a02d9de2dc474f5cc9dbae8cff1ab1 (diff)
F0: usart + DMA working
-rw-r--r--embassy-stm32/src/bdma/v1.rs24
-rw-r--r--embassy-stm32/src/usart/mod.rs15
-rw-r--r--embassy-stm32/src/usart/v2.rs12
3 files changed, 36 insertions, 15 deletions
diff --git a/embassy-stm32/src/bdma/v1.rs b/embassy-stm32/src/bdma/v1.rs
index 13caf329b..f81a08768 100644
--- a/embassy-stm32/src/bdma/v1.rs
+++ b/embassy-stm32/src/bdma/v1.rs
@@ -78,7 +78,6 @@ pub(crate) async unsafe fn transfer_p2m(
78 }) 78 })
79 .await; 79 .await;
80 80
81 on_drop.defuse();
82 // TODO handle error 81 // TODO handle error
83 assert!(res == CH_STATUS_COMPLETED); 82 assert!(res == CH_STATUS_COMPLETED);
84} 83}
@@ -128,7 +127,6 @@ pub(crate) async unsafe fn transfer_m2p(
128 }) 127 })
129 .await; 128 .await;
130 129
131 on_drop.defuse();
132 // TODO handle error 130 // TODO handle error
133 assert!(res == CH_STATUS_COMPLETED); 131 assert!(res == CH_STATUS_COMPLETED);
134} 132}
@@ -150,7 +148,6 @@ unsafe fn on_irq() {
150 STATE.ch_wakers[n].wake(); 148 STATE.ch_wakers[n].wake();
151 } 149 }
152 } 150 }
153
154 }; 151 };
155 } 152 }
156} 153}
@@ -162,6 +159,13 @@ pub(crate) unsafe fn init() {
162 crate::interrupt::$irq::steal().enable(); 159 crate::interrupt::$irq::steal().enable();
163 }; 160 };
164 } 161 }
162 pac::peripherals! {
163 (bdma, DMA1) => {
164 critical_section::with(|_| {
165 pac::RCC.ahbenr().modify(|w| w.set_dmaen(true));
166 });
167 };
168 }
165} 169}
166 170
167pub(crate) mod sealed { 171pub(crate) mod sealed {
@@ -285,3 +289,17 @@ pac::interrupts! {
285 } 289 }
286 }; 290 };
287} 291}
292
293#[cfg(usart)]
294use crate::usart;
295pac::peripheral_dma_channels! {
296 ($peri:ident, usart, $kind:ident, RX, $channel_peri:ident, $dma_peri:ident, $channel_num:expr) => {
297 impl usart::RxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
298 impl usart::sealed::RxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
299 };
300
301 ($peri:ident, usart, $kind:ident, TX, $channel_peri:ident, $dma_peri:ident, $channel_num:expr) => {
302 impl usart::TxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
303 impl usart::sealed::TxDma<crate::peripherals::$peri> for crate::peripherals::$channel_peri { }
304 };
305}
diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs
index 2fa758ecc..2bab4016f 100644
--- a/embassy-stm32/src/usart/mod.rs
+++ b/embassy-stm32/src/usart/mod.rs
@@ -8,7 +8,6 @@ use crate::peripherals;
8pub use _version::*; 8pub use _version::*;
9 9
10use crate::gpio::Pin; 10use crate::gpio::Pin;
11use crate::pac::usart::Usart;
12use crate::rcc::RccPeripheral; 11use crate::rcc::RccPeripheral;
13 12
14#[derive(Clone, Copy, PartialEq, Eq, Debug)] 13#[derive(Clone, Copy, PartialEq, Eq, Debug)]
@@ -58,6 +57,7 @@ impl Default for Config {
58 57
59/// Serial error 58/// Serial error
60#[derive(Debug, Eq, PartialEq, Copy, Clone)] 59#[derive(Debug, Eq, PartialEq, Copy, Clone)]
60#[cfg_attr(feature = "defmt", derive(defmt::Format))]
61#[non_exhaustive] 61#[non_exhaustive]
62pub enum Error { 62pub enum Error {
63 /// Framing error 63 /// Framing error
@@ -76,8 +76,11 @@ pub(crate) mod sealed {
76 #[cfg(any(dma, dmamux))] 76 #[cfg(any(dma, dmamux))]
77 use crate::dma::WriteDma; 77 use crate::dma::WriteDma;
78 78
79 #[cfg(bdma)]
80 use crate::bdma::WriteDma;
81
79 pub trait Instance { 82 pub trait Instance {
80 fn regs(&self) -> Usart; 83 fn regs(&self) -> crate::pac::usart::Usart;
81 } 84 }
82 pub trait RxPin<T: Instance>: Pin { 85 pub trait RxPin<T: Instance>: Pin {
83 fn af_num(&self) -> u8; 86 fn af_num(&self) -> u8;
@@ -95,10 +98,10 @@ pub(crate) mod sealed {
95 fn af_num(&self) -> u8; 98 fn af_num(&self) -> u8;
96 } 99 }
97 100
98 #[cfg(any(dma, dmamux))] 101 #[cfg(any(bdma, dma, dmamux))]
99 pub trait RxDma<T: Instance> {} 102 pub trait RxDma<T: Instance> {}
100 103
101 #[cfg(any(dma, dmamux))] 104 #[cfg(any(bdma, dma, dmamux))]
102 pub trait TxDma<T: Instance>: WriteDma<T> {} 105 pub trait TxDma<T: Instance>: WriteDma<T> {}
103} 106}
104 107
@@ -109,10 +112,10 @@ pub trait CtsPin<T: Instance>: sealed::CtsPin<T> {}
109pub trait RtsPin<T: Instance>: sealed::RtsPin<T> {} 112pub trait RtsPin<T: Instance>: sealed::RtsPin<T> {}
110pub trait CkPin<T: Instance>: sealed::CkPin<T> {} 113pub trait CkPin<T: Instance>: sealed::CkPin<T> {}
111 114
112#[cfg(any(dma, dmamux))] 115#[cfg(any(bdma, dma, dmamux))]
113pub trait RxDma<T: Instance>: sealed::RxDma<T> {} 116pub trait RxDma<T: Instance>: sealed::RxDma<T> {}
114 117
115#[cfg(any(dma, dmamux))] 118#[cfg(any(bdma, dma, dmamux))]
116pub trait TxDma<T: Instance>: sealed::TxDma<T> {} 119pub trait TxDma<T: Instance>: sealed::TxDma<T> {}
117 120
118crate::pac::peripherals!( 121crate::pac::peripherals!(
diff --git a/embassy-stm32/src/usart/v2.rs b/embassy-stm32/src/usart/v2.rs
index 271eff850..22041b4aa 100644
--- a/embassy-stm32/src/usart/v2.rs
+++ b/embassy-stm32/src/usart/v2.rs
@@ -3,7 +3,7 @@ use core::marker::PhantomData;
3use embassy::util::Unborrow; 3use embassy::util::Unborrow;
4use embassy_extras::unborrow; 4use embassy_extras::unborrow;
5 5
6use crate::pac::usart::{regs, vals}; 6use crate::pac::usart::vals;
7 7
8use super::*; 8use super::*;
9 9
@@ -21,7 +21,6 @@ impl<'d, T: Instance> Uart<'d, T> {
21 ) -> Self { 21 ) -> Self {
22 unborrow!(inner, rx, tx); 22 unborrow!(inner, rx, tx);
23 23
24 // Uncomment once we find all of the H7's UART clocks.
25 T::enable(); 24 T::enable();
26 let pclk_freq = T::frequency(); 25 let pclk_freq = T::frequency();
27 26
@@ -34,7 +33,10 @@ impl<'d, T: Instance> Uart<'d, T> {
34 rx.set_as_af(rx.af_num()); 33 rx.set_as_af(rx.af_num());
35 tx.set_as_af(tx.af_num()); 34 tx.set_as_af(tx.af_num());
36 35
37 r.brr().write_value(regs::Brr(div)); 36 r.cr2().write(|_w| {});
37 r.cr3().write(|_w| {});
38
39 r.brr().write(|w| w.set_brr(div as u16));
38 r.cr1().write(|w| { 40 r.cr1().write(|w| {
39 w.set_ue(true); 41 w.set_ue(true);
40 w.set_te(true); 42 w.set_te(true);
@@ -48,8 +50,6 @@ impl<'d, T: Instance> Uart<'d, T> {
48 _ => vals::Ps::EVEN, 50 _ => vals::Ps::EVEN,
49 }); 51 });
50 }); 52 });
51 r.cr2().write(|_w| {});
52 r.cr3().write(|_w| {});
53 } 53 }
54 54
55 Self { 55 Self {
@@ -58,7 +58,7 @@ impl<'d, T: Instance> Uart<'d, T> {
58 } 58 }
59 } 59 }
60 60
61 #[cfg(dma)] 61 #[cfg(bdma)]
62 pub async fn write_dma(&mut self, ch: &mut impl TxDma<T>, buffer: &[u8]) -> Result<(), Error> { 62 pub async fn write_dma(&mut self, ch: &mut impl TxDma<T>, buffer: &[u8]) -> Result<(), Error> {
63 unsafe { 63 unsafe {
64 self.inner.regs().cr3().modify(|reg| { 64 self.inner.regs().cr3().modify(|reg| {