aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-stm32')
-rw-r--r--embassy-stm32/Cargo.toml4
-rw-r--r--embassy-stm32/src/usart/buffered.rs4
-rw-r--r--embassy-stm32/src/usart/mod.rs128
3 files changed, 102 insertions, 34 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml
index 9686b10ce..b9887f9b3 100644
--- a/embassy-stm32/Cargo.toml
+++ b/embassy-stm32/Cargo.toml
@@ -58,7 +58,7 @@ sdio-host = "0.5.0"
58embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true } 58embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true }
59critical-section = "1.1" 59critical-section = "1.1"
60atomic-polyfill = "1.0.1" 60atomic-polyfill = "1.0.1"
61stm32-metapac = "6" 61stm32-metapac = "7"
62vcell = "0.1.3" 62vcell = "0.1.3"
63bxcan = "0.7.0" 63bxcan = "0.7.0"
64nb = "1.0.0" 64nb = "1.0.0"
@@ -74,7 +74,7 @@ critical-section = { version = "1.1", features = ["std"] }
74[build-dependencies] 74[build-dependencies]
75proc-macro2 = "1.0.36" 75proc-macro2 = "1.0.36"
76quote = "1.0.15" 76quote = "1.0.15"
77stm32-metapac = { version = "6", default-features = false, features = ["metadata"]} 77stm32-metapac = { version = "7", default-features = false, features = ["metadata"]}
78 78
79[features] 79[features]
80default = ["stm32-metapac/rt"] 80default = ["stm32-metapac/rt"]
diff --git a/embassy-stm32/src/usart/buffered.rs b/embassy-stm32/src/usart/buffered.rs
index 3e23e7ca1..12cf8b0fc 100644
--- a/embassy-stm32/src/usart/buffered.rs
+++ b/embassy-stm32/src/usart/buffered.rs
@@ -84,7 +84,7 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> {
84 Self::new_inner(peri, irq, rx, tx, tx_buffer, rx_buffer, config) 84 Self::new_inner(peri, irq, rx, tx, tx_buffer, rx_buffer, config)
85 } 85 }
86 86
87 #[cfg(not(usart_v1))] 87 #[cfg(not(any(usart_v1, usart_v2)))]
88 pub fn new_with_de( 88 pub fn new_with_de(
89 peri: impl Peripheral<P = T> + 'd, 89 peri: impl Peripheral<P = T> + 'd,
90 irq: impl Peripheral<P = T::Interrupt> + 'd, 90 irq: impl Peripheral<P = T::Interrupt> + 'd,
@@ -133,7 +133,7 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> {
133 tx.set_as_af(tx.af_num(), AFType::OutputPushPull); 133 tx.set_as_af(tx.af_num(), AFType::OutputPushPull);
134 } 134 }
135 135
136 configure(r, &config, T::frequency(), T::MULTIPLIER, true, true); 136 configure(r, &config, T::frequency(), T::KIND, true, true);
137 137
138 unsafe { 138 unsafe {
139 r.cr1().modify(|w| { 139 r.cr1().modify(|w| {
diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs
index ad450f2b3..dbce668c2 100644
--- a/embassy-stm32/src/usart/mod.rs
+++ b/embassy-stm32/src/usart/mod.rs
@@ -12,10 +12,11 @@ use futures::future::{select, Either};
12 12
13use crate::dma::{NoDma, Transfer}; 13use crate::dma::{NoDma, Transfer};
14use crate::gpio::sealed::AFType; 14use crate::gpio::sealed::AFType;
15#[cfg(any(lpuart_v1, lpuart_v2))] 15#[cfg(not(any(usart_v1, usart_v2)))]
16use crate::pac::lpuart::{regs, vals, Lpuart as Regs}; 16use crate::pac::usart::Lpuart as Regs;
17#[cfg(not(any(lpuart_v1, lpuart_v2)))] 17#[cfg(any(usart_v1, usart_v2))]
18use crate::pac::usart::{regs, vals, Usart as Regs}; 18use crate::pac::usart::Usart as Regs;
19use crate::pac::usart::{regs, vals};
19use crate::time::Hertz; 20use crate::time::Hertz;
20use crate::{peripherals, Peripheral}; 21use crate::{peripherals, Peripheral};
21 22
@@ -159,7 +160,7 @@ impl<'d, T: BasicInstance, TxDma> UartTx<'d, T, TxDma> {
159 tx.set_as_af(tx.af_num(), AFType::OutputPushPull); 160 tx.set_as_af(tx.af_num(), AFType::OutputPushPull);
160 } 161 }
161 162
162 configure(r, &config, T::frequency(), T::MULTIPLIER, false, true); 163 configure(r, &config, T::frequency(), T::KIND, false, true);
163 164
164 // create state once! 165 // create state once!
165 let _s = T::state(); 166 let _s = T::state();
@@ -261,7 +262,7 @@ impl<'d, T: BasicInstance, RxDma> UartRx<'d, T, RxDma> {
261 rx.set_as_af(rx.af_num(), AFType::Input); 262 rx.set_as_af(rx.af_num(), AFType::Input);
262 } 263 }
263 264
264 configure(r, &config, T::frequency(), T::MULTIPLIER, true, false); 265 configure(r, &config, T::frequency(), T::KIND, true, false);
265 266
266 irq.set_handler(Self::on_interrupt); 267 irq.set_handler(Self::on_interrupt);
267 irq.unpend(); 268 irq.unpend();
@@ -653,7 +654,7 @@ impl<'d, T: BasicInstance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
653 Self::new_inner(peri, rx, tx, irq, tx_dma, rx_dma, config) 654 Self::new_inner(peri, rx, tx, irq, tx_dma, rx_dma, config)
654 } 655 }
655 656
656 #[cfg(not(usart_v1))] 657 #[cfg(not(any(usart_v1, usart_v2)))]
657 pub fn new_with_de( 658 pub fn new_with_de(
658 peri: impl Peripheral<P = T> + 'd, 659 peri: impl Peripheral<P = T> + 'd,
659 rx: impl Peripheral<P = impl RxPin<T>> + 'd, 660 rx: impl Peripheral<P = impl RxPin<T>> + 'd,
@@ -696,7 +697,7 @@ impl<'d, T: BasicInstance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
696 tx.set_as_af(tx.af_num(), AFType::OutputPushPull); 697 tx.set_as_af(tx.af_num(), AFType::OutputPushPull);
697 } 698 }
698 699
699 configure(r, &config, T::frequency(), T::MULTIPLIER, true, true); 700 configure(r, &config, T::frequency(), T::KIND, true, true);
700 701
701 irq.set_handler(UartRx::<T, RxDma>::on_interrupt); 702 irq.set_handler(UartRx::<T, RxDma>::on_interrupt);
702 irq.unpend(); 703 irq.unpend();
@@ -763,16 +764,74 @@ impl<'d, T: BasicInstance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
763 } 764 }
764} 765}
765 766
766fn configure(r: Regs, config: &Config, pclk_freq: Hertz, multiplier: u32, enable_rx: bool, enable_tx: bool) { 767fn configure(r: Regs, config: &Config, pclk_freq: Hertz, kind: Kind, enable_rx: bool, enable_tx: bool) {
767 if !enable_rx && !enable_tx { 768 if !enable_rx && !enable_tx {
768 panic!("USART: At least one of RX or TX should be enabled"); 769 panic!("USART: At least one of RX or TX should be enabled");
769 } 770 }
770 771
771 // TODO: better calculation, including error checking and OVER8 if possible. 772 #[cfg(not(usart_v4))]
772 let div = (pclk_freq.0 + (config.baudrate / 2)) / config.baudrate * multiplier; 773 static DIVS: [(u16, ()); 1] = [(1, ())];
774
775 #[cfg(usart_v4)]
776 static DIVS: [(u16, vals::Presc); 12] = [
777 (1, vals::Presc::DIV1),
778 (2, vals::Presc::DIV2),
779 (4, vals::Presc::DIV4),
780 (6, vals::Presc::DIV6),
781 (8, vals::Presc::DIV8),
782 (10, vals::Presc::DIV10),
783 (12, vals::Presc::DIV12),
784 (16, vals::Presc::DIV16),
785 (32, vals::Presc::DIV32),
786 (64, vals::Presc::DIV64),
787 (128, vals::Presc::DIV128),
788 (256, vals::Presc::DIV256),
789 ];
790
791 let (mul, brr_min, brr_max) = match kind {
792 #[cfg(any(usart_v3, usart_v4))]
793 Kind::Lpuart => (256, 0x300, 0x10_0000),
794 Kind::Uart => (1, 0x10, 0x1_0000),
795 };
796
797 #[cfg(not(usart_v1))]
798 let mut over8 = false;
799 let mut found = false;
800 for &(presc, _presc_val) in &DIVS {
801 let denom = (config.baudrate * presc as u32) as u64;
802 let div = (pclk_freq.0 as u64 * mul + (denom / 2)) / denom;
803 trace!("USART: presc={} div={:08x}", presc, div);
804
805 if div < brr_min {
806 #[cfg(not(usart_v1))]
807 if div * 2 >= brr_min && kind == Kind::Uart && !cfg!(usart_v1) {
808 over8 = true;
809 let div = div as u32;
810 unsafe {
811 r.brr().write_value(regs::Brr(((div << 1) & !0xF) | (div & 0x07)));
812 #[cfg(usart_v4)]
813 r.presc().write(|w| w.set_prescaler(_presc_val));
814 }
815 found = true;
816 break;
817 }
818 panic!("USART: baudrate too high");
819 }
820
821 if div < brr_max {
822 unsafe {
823 r.brr().write_value(regs::Brr(div as u32));
824 #[cfg(usart_v4)]
825 r.presc().write(|w| w.set_prescaler(_presc_val));
826 }
827 found = true;
828 break;
829 }
830 }
831
832 assert!(found, "USART: baudrate too low");
773 833
774 unsafe { 834 unsafe {
775 r.brr().write_value(regs::Brr(div));
776 r.cr2().write(|w| { 835 r.cr2().write(|w| {
777 w.set_stop(match config.stop_bits { 836 w.set_stop(match config.stop_bits {
778 StopBits::STOP0P5 => vals::Stop::STOP0P5, 837 StopBits::STOP0P5 => vals::Stop::STOP0P5,
@@ -801,6 +860,8 @@ fn configure(r: Regs, config: &Config, pclk_freq: Hertz, multiplier: u32, enable
801 Parity::ParityEven => vals::Ps::EVEN, 860 Parity::ParityEven => vals::Ps::EVEN,
802 _ => vals::Ps::EVEN, 861 _ => vals::Ps::EVEN,
803 }); 862 });
863 #[cfg(not(usart_v1))]
864 w.set_over8(vals::Over8(over8 as _));
804 }); 865 });
805 } 866 }
806} 867}
@@ -986,43 +1047,45 @@ mod rx_ringbuffered;
986#[cfg(not(gpdma))] 1047#[cfg(not(gpdma))]
987pub use rx_ringbuffered::RingBufferedUartRx; 1048pub use rx_ringbuffered::RingBufferedUartRx;
988 1049
989#[cfg(usart_v1)] 1050use self::sealed::Kind;
1051
1052#[cfg(any(usart_v1, usart_v2))]
990fn tdr(r: crate::pac::usart::Usart) -> *mut u8 { 1053fn tdr(r: crate::pac::usart::Usart) -> *mut u8 {
991 r.dr().ptr() as _ 1054 r.dr().ptr() as _
992} 1055}
993 1056
994#[cfg(usart_v1)] 1057#[cfg(any(usart_v1, usart_v2))]
995fn rdr(r: crate::pac::usart::Usart) -> *mut u8 { 1058fn rdr(r: crate::pac::usart::Usart) -> *mut u8 {
996 r.dr().ptr() as _ 1059 r.dr().ptr() as _
997} 1060}
998 1061
999#[cfg(usart_v1)] 1062#[cfg(any(usart_v1, usart_v2))]
1000fn sr(r: crate::pac::usart::Usart) -> crate::pac::common::Reg<regs::Sr, crate::pac::common::RW> { 1063fn sr(r: crate::pac::usart::Usart) -> crate::pac::common::Reg<regs::Sr, crate::pac::common::RW> {
1001 r.sr() 1064 r.sr()
1002} 1065}
1003 1066
1004#[cfg(usart_v1)] 1067#[cfg(any(usart_v1, usart_v2))]
1005#[allow(unused)] 1068#[allow(unused)]
1006unsafe fn clear_interrupt_flags(_r: Regs, _sr: regs::Sr) { 1069unsafe fn clear_interrupt_flags(_r: Regs, _sr: regs::Sr) {
1007 // On v1 the flags are cleared implicitly by reads and writes to DR. 1070 // On v1 the flags are cleared implicitly by reads and writes to DR.
1008} 1071}
1009 1072
1010#[cfg(usart_v2)] 1073#[cfg(any(usart_v3, usart_v4))]
1011fn tdr(r: Regs) -> *mut u8 { 1074fn tdr(r: Regs) -> *mut u8 {
1012 r.tdr().ptr() as _ 1075 r.tdr().ptr() as _
1013} 1076}
1014 1077
1015#[cfg(usart_v2)] 1078#[cfg(any(usart_v3, usart_v4))]
1016fn rdr(r: Regs) -> *mut u8 { 1079fn rdr(r: Regs) -> *mut u8 {
1017 r.rdr().ptr() as _ 1080 r.rdr().ptr() as _
1018} 1081}
1019 1082
1020#[cfg(usart_v2)] 1083#[cfg(any(usart_v3, usart_v4))]
1021fn sr(r: Regs) -> crate::pac::common::Reg<regs::Isr, crate::pac::common::R> { 1084fn sr(r: Regs) -> crate::pac::common::Reg<regs::Isr, crate::pac::common::R> {
1022 r.isr() 1085 r.isr()
1023} 1086}
1024 1087
1025#[cfg(usart_v2)] 1088#[cfg(any(usart_v3, usart_v4))]
1026#[allow(unused)] 1089#[allow(unused)]
1027unsafe fn clear_interrupt_flags(r: Regs, sr: regs::Isr) { 1090unsafe fn clear_interrupt_flags(r: Regs, sr: regs::Isr) {
1028 r.icr().write(|w| *w = regs::Icr(sr.0)); 1091 r.icr().write(|w| *w = regs::Icr(sr.0));
@@ -1033,6 +1096,13 @@ pub(crate) mod sealed {
1033 1096
1034 use super::*; 1097 use super::*;
1035 1098
1099 #[derive(Clone, Copy, PartialEq, Eq)]
1100 pub enum Kind {
1101 Uart,
1102 #[cfg(any(usart_v3, usart_v4))]
1103 Lpuart,
1104 }
1105
1036 pub struct State { 1106 pub struct State {
1037 pub rx_waker: AtomicWaker, 1107 pub rx_waker: AtomicWaker,
1038 pub tx_waker: AtomicWaker, 1108 pub tx_waker: AtomicWaker,
@@ -1048,7 +1118,7 @@ pub(crate) mod sealed {
1048 } 1118 }
1049 1119
1050 pub trait BasicInstance: crate::rcc::RccPeripheral { 1120 pub trait BasicInstance: crate::rcc::RccPeripheral {
1051 const MULTIPLIER: u32; 1121 const KIND: Kind;
1052 type Interrupt: crate::interrupt::Interrupt; 1122 type Interrupt: crate::interrupt::Interrupt;
1053 1123
1054 fn regs() -> Regs; 1124 fn regs() -> Regs;
@@ -1077,10 +1147,10 @@ pin_trait!(DePin, BasicInstance);
1077dma_trait!(TxDma, BasicInstance); 1147dma_trait!(TxDma, BasicInstance);
1078dma_trait!(RxDma, BasicInstance); 1148dma_trait!(RxDma, BasicInstance);
1079 1149
1080macro_rules! impl_lpuart { 1150macro_rules! impl_usart {
1081 ($inst:ident, $irq:ident, $mul:expr) => { 1151 ($inst:ident, $irq:ident, $kind:expr) => {
1082 impl sealed::BasicInstance for crate::peripherals::$inst { 1152 impl sealed::BasicInstance for crate::peripherals::$inst {
1083 const MULTIPLIER: u32 = $mul; 1153 const KIND: Kind = $kind;
1084 type Interrupt = crate::interrupt::$irq; 1154 type Interrupt = crate::interrupt::$irq;
1085 1155
1086 fn regs() -> Regs { 1156 fn regs() -> Regs {
@@ -1104,21 +1174,19 @@ macro_rules! impl_lpuart {
1104} 1174}
1105 1175
1106foreach_interrupt!( 1176foreach_interrupt!(
1107 ($inst:ident, lpuart, $block:ident, $signal_name:ident, $irq:ident) => { 1177 ($inst:ident, usart, LPUART, $signal_name:ident, $irq:ident) => {
1108 impl_lpuart!($inst, $irq, 256); 1178 impl_usart!($inst, $irq, Kind::Lpuart);
1109 }; 1179 };
1110 1180
1111 ($inst:ident, usart, $block:ident, $signal_name:ident, $irq:ident) => { 1181 ($inst:ident, usart, $block:ident, $signal_name:ident, $irq:ident) => {
1112 impl_lpuart!($inst, $irq, 1); 1182 impl_usart!($inst, $irq, Kind::Uart);
1113 1183
1114 impl sealed::FullInstance for peripherals::$inst { 1184 impl sealed::FullInstance for peripherals::$inst {
1115
1116 fn regs_uart() -> crate::pac::usart::Usart { 1185 fn regs_uart() -> crate::pac::usart::Usart {
1117 crate::pac::$inst 1186 crate::pac::$inst
1118 } 1187 }
1119 } 1188 }
1120 1189
1121 impl FullInstance for peripherals::$inst { 1190 impl FullInstance for peripherals::$inst {}
1122 }
1123 }; 1191 };
1124); 1192);