diff options
| author | Bob McWhirter <[email protected]> | 2021-07-01 11:26:56 -0400 |
|---|---|---|
| committer | Bob McWhirter <[email protected]> | 2021-07-01 11:30:54 -0400 |
| commit | 0920c0cb1d8b5b0a9698a4a113a89691ccc139fa (patch) | |
| tree | 2fee4cb1c5d83ac755786b498278b479e5204ab9 /examples | |
| parent | 54ada5bae113dbff1f5b76fac2676734f6ddd8eb (diff) | |
Make UART pins Rx/Tx/etc in addition to USART.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32h7/src/bin/usart.rs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/examples/stm32h7/src/bin/usart.rs b/examples/stm32h7/src/bin/usart.rs new file mode 100644 index 000000000..b8524f2c0 --- /dev/null +++ b/examples/stm32h7/src/bin/usart.rs | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | |||
| 2 | #![no_std] | ||
| 3 | #![no_main] | ||
| 4 | #![feature(trait_alias)] | ||
| 5 | #![feature(min_type_alias_impl_trait)] | ||
| 6 | #![feature(impl_trait_in_bindings)] | ||
| 7 | #![feature(type_alias_impl_trait)] | ||
| 8 | #![allow(incomplete_features)] | ||
| 9 | |||
| 10 | #[path = "../example_common.rs"] | ||
| 11 | mod example_common; | ||
| 12 | use cortex_m::prelude::_embedded_hal_blocking_serial_Write; | ||
| 13 | use embassy::executor::Executor; | ||
| 14 | use embassy::time::Clock; | ||
| 15 | use embassy::util::Forever; | ||
| 16 | use embassy_stm32::usart::{Config, Uart}; | ||
| 17 | use example_common::*; | ||
| 18 | |||
| 19 | use stm32h7xx_hal as hal; | ||
| 20 | use hal::prelude::*; | ||
| 21 | |||
| 22 | use cortex_m_rt::entry; | ||
| 23 | use stm32h7::stm32h743 as pac; | ||
| 24 | |||
| 25 | #[embassy::task] | ||
| 26 | async fn main_task() { | ||
| 27 | let p = embassy_stm32::init(Default::default()); | ||
| 28 | |||
| 29 | let config = Config::default(); | ||
| 30 | let mut usart = Uart::new(p.UART7, p.PF6, p.PF7, config); | ||
| 31 | |||
| 32 | usart.bwrite_all(b"Hello Embassy World!\r\n").unwrap(); | ||
| 33 | info!("wrote Hello, starting echo"); | ||
| 34 | |||
| 35 | let mut buf = [0u8; 1]; | ||
| 36 | loop { | ||
| 37 | usart.read(&mut buf).unwrap(); | ||
| 38 | usart.bwrite_all(&buf).unwrap(); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | struct ZeroClock; | ||
| 43 | |||
| 44 | impl Clock for ZeroClock { | ||
| 45 | fn now(&self) -> u64 { | ||
| 46 | 0 | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 51 | |||
| 52 | #[entry] | ||
| 53 | fn main() -> ! { | ||
| 54 | info!("Hello World!"); | ||
| 55 | |||
| 56 | let pp = pac::Peripherals::take().unwrap(); | ||
| 57 | |||
| 58 | let pwrcfg = pp.PWR.constrain().freeze(); | ||
| 59 | |||
| 60 | let rcc = pp.RCC.constrain(); | ||
| 61 | |||
| 62 | let ccdr = rcc | ||
| 63 | .sys_ck(96.mhz()) | ||
| 64 | .pclk1(48.mhz()) | ||
| 65 | .pclk2(48.mhz()) | ||
| 66 | .pclk3(48.mhz()) | ||
| 67 | .pclk4(48.mhz()) | ||
| 68 | .pll1_q_ck(48.mhz()) | ||
| 69 | .freeze(pwrcfg, &pp.SYSCFG); | ||
| 70 | |||
| 71 | let pp = unsafe { pac::Peripherals::steal() }; | ||
| 72 | |||
| 73 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 74 | w.dbgsleep_d1().set_bit(); | ||
| 75 | w.dbgstby_d1().set_bit(); | ||
| 76 | w.dbgstop_d1().set_bit(); | ||
| 77 | w.d1dbgcken().set_bit(); | ||
| 78 | w | ||
| 79 | }); | ||
| 80 | |||
| 81 | pp.RCC.ahb4enr.modify(|_, w| { | ||
| 82 | w.gpioaen().set_bit(); | ||
| 83 | w.gpioben().set_bit(); | ||
| 84 | w.gpiocen().set_bit(); | ||
| 85 | w.gpioden().set_bit(); | ||
| 86 | w.gpioeen().set_bit(); | ||
| 87 | w.gpiofen().set_bit(); | ||
| 88 | w | ||
| 89 | }); | ||
| 90 | |||
| 91 | |||
| 92 | unsafe { embassy::time::set_clock(&ZeroClock) }; | ||
| 93 | |||
| 94 | let executor = EXECUTOR.put(Executor::new()); | ||
| 95 | |||
| 96 | executor.run(|spawner| { | ||
| 97 | unwrap!(spawner.spawn(main_task())); | ||
| 98 | }) | ||
| 99 | } \ No newline at end of file | ||
