From 0920c0cb1d8b5b0a9698a4a113a89691ccc139fa Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Thu, 1 Jul 2021 11:26:56 -0400 Subject: [PATCH] Make UART pins Rx/Tx/etc in addition to USART. --- embassy-stm32/src/usart/mod.rs | 29 ++++++++- embassy-stm32/src/usart/v3.rs | 4 +- examples/stm32h7/src/bin/usart.rs | 99 +++++++++++++++++++++++++++++++ stm32-data | 2 +- 4 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 examples/stm32h7/src/bin/usart.rs diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index 41327d25..2c011719 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs @@ -1,7 +1,7 @@ #![macro_use] -#[cfg_attr(usart_v1, path = "v1.rs")] -#[cfg_attr(usart_v2, path = "v2.rs")] +//#[cfg_attr(usart_v1, path = "v1.rs")] +//#[cfg_attr(usart_v2, path = "v2.rs")] #[cfg_attr(usart_v3, path = "v3.rs")] mod _version; use crate::peripherals; @@ -140,6 +140,9 @@ macro_rules! impl_pin { } crate::pac::peripheral_pins!( + + // USART + ($inst:ident, usart, USART, $pin:ident, TX, $af:expr) => { impl_pin!($inst, $pin, TxPin, $af); }; @@ -159,4 +162,26 @@ crate::pac::peripheral_pins!( ($inst:ident, usart, USART, $pin:ident, CK, $af:expr) => { impl_pin!($inst, $pin, CkPin, $af); }; + + // UART + + ($inst:ident, uart, UART, $pin:ident, TX, $af:expr) => { + impl_pin!($inst, $pin, TxPin, $af); + }; + + ($inst:ident, uart, UART, $pin:ident, RX, $af:expr) => { + impl_pin!($inst, $pin, RxPin, $af); + }; + + ($inst:ident, uart, UART, $pin:ident, CTS, $af:expr) => { + impl_pin!($inst, $pin, CtsPin, $af); + }; + + ($inst:ident, uart, UART, $pin:ident, RTS, $af:expr) => { + impl_pin!($inst, $pin, RtsPin, $af); + }; + + ($inst:ident, uart, UART, $pin:ident, CK, $af:expr) => { + impl_pin!($inst, $pin, CkPin, $af); + }; ); diff --git a/embassy-stm32/src/usart/v3.rs b/embassy-stm32/src/usart/v3.rs index c45af004..1e905144 100644 --- a/embassy-stm32/src/usart/v3.rs +++ b/embassy-stm32/src/usart/v3.rs @@ -22,7 +22,7 @@ impl<'d, T: Instance> Uart<'d, T> { unborrow!(inner, rx, tx); // Uncomment once we find all of the H7's UART clocks. - //T::enable(); + T::enable(); let pclk_freq = T::frequency(); // TODO: better calculation, including error checking and OVER8 if possible. @@ -65,7 +65,7 @@ impl<'d, T: Instance> Uart<'d, T> { }); } let r = self.inner.regs(); - let dst = r.dr().ptr() as *mut u8; + let dst = r.tdr().ptr() as *mut u8; ch.transfer(buffer, dst).await; Ok(()) } diff --git a/examples/stm32h7/src/bin/usart.rs b/examples/stm32h7/src/bin/usart.rs new file mode 100644 index 00000000..b8524f2c --- /dev/null +++ b/examples/stm32h7/src/bin/usart.rs @@ -0,0 +1,99 @@ + +#![no_std] +#![no_main] +#![feature(trait_alias)] +#![feature(min_type_alias_impl_trait)] +#![feature(impl_trait_in_bindings)] +#![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] + +#[path = "../example_common.rs"] +mod example_common; +use cortex_m::prelude::_embedded_hal_blocking_serial_Write; +use embassy::executor::Executor; +use embassy::time::Clock; +use embassy::util::Forever; +use embassy_stm32::usart::{Config, Uart}; +use example_common::*; + +use stm32h7xx_hal as hal; +use hal::prelude::*; + +use cortex_m_rt::entry; +use stm32h7::stm32h743 as pac; + +#[embassy::task] +async fn main_task() { + let p = embassy_stm32::init(Default::default()); + + let config = Config::default(); + let mut usart = Uart::new(p.UART7, p.PF6, p.PF7, config); + + usart.bwrite_all(b"Hello Embassy World!\r\n").unwrap(); + info!("wrote Hello, starting echo"); + + let mut buf = [0u8; 1]; + loop { + usart.read(&mut buf).unwrap(); + usart.bwrite_all(&buf).unwrap(); + } +} + +struct ZeroClock; + +impl Clock for ZeroClock { + fn now(&self) -> u64 { + 0 + } +} + +static EXECUTOR: Forever = Forever::new(); + +#[entry] +fn main() -> ! { + info!("Hello World!"); + + let pp = pac::Peripherals::take().unwrap(); + + let pwrcfg = pp.PWR.constrain().freeze(); + + let rcc = pp.RCC.constrain(); + + let ccdr = rcc + .sys_ck(96.mhz()) + .pclk1(48.mhz()) + .pclk2(48.mhz()) + .pclk3(48.mhz()) + .pclk4(48.mhz()) + .pll1_q_ck(48.mhz()) + .freeze(pwrcfg, &pp.SYSCFG); + + let pp = unsafe { pac::Peripherals::steal() }; + + pp.DBGMCU.cr.modify(|_, w| { + w.dbgsleep_d1().set_bit(); + w.dbgstby_d1().set_bit(); + w.dbgstop_d1().set_bit(); + w.d1dbgcken().set_bit(); + w + }); + + pp.RCC.ahb4enr.modify(|_, w| { + w.gpioaen().set_bit(); + w.gpioben().set_bit(); + w.gpiocen().set_bit(); + w.gpioden().set_bit(); + w.gpioeen().set_bit(); + w.gpiofen().set_bit(); + w + }); + + + unsafe { embassy::time::set_clock(&ZeroClock) }; + + let executor = EXECUTOR.put(Executor::new()); + + executor.run(|spawner| { + unwrap!(spawner.spawn(main_task())); + }) +} \ No newline at end of file diff --git a/stm32-data b/stm32-data index 424f96ea..3cc324ef 160000 --- a/stm32-data +++ b/stm32-data @@ -1 +1 @@ -Subproject commit 424f96eae80015536c4df5ed62c1caad85bb531f +Subproject commit 3cc324ef25916ff4d70acfc4e7551109f7d02900