diff options
| author | Bob McWhirter <[email protected]> | 2021-06-30 14:46:53 -0400 |
|---|---|---|
| committer | Bob McWhirter <[email protected]> | 2021-07-01 11:30:54 -0400 |
| commit | f83559c6abc3ebc0fd7d2fb521dfc069eac93323 (patch) | |
| tree | 1f29d8374faace96ca08df0d18fd36be28bcc2e3 /examples | |
| parent | 497d3aa15383c627adf3873ac151bad8826ca7c8 (diff) | |
Add L4+ example for USART.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32l4/src/bin/usart.rs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/examples/stm32l4/src/bin/usart.rs b/examples/stm32l4/src/bin/usart.rs new file mode 100644 index 000000000..6b2b13810 --- /dev/null +++ b/examples/stm32l4/src/bin/usart.rs | |||
| @@ -0,0 +1,89 @@ | |||
| 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 cortex_m_rt::entry; | ||
| 20 | use stm32l4::stm32l4x5 as pac; | ||
| 21 | |||
| 22 | #[embassy::task] | ||
| 23 | async fn main_task() { | ||
| 24 | let p = embassy_stm32::init(Default::default()); | ||
| 25 | |||
| 26 | let config = Config::default(); | ||
| 27 | let mut usart = Uart::new(p.UART4, p.PA1, p.PA0, config); | ||
| 28 | |||
| 29 | usart.bwrite_all(b"Hello Embassy World!\r\n").unwrap(); | ||
| 30 | info!("wrote Hello, starting echo"); | ||
| 31 | |||
| 32 | let mut buf = [0u8; 1]; | ||
| 33 | loop { | ||
| 34 | usart.read(&mut buf).unwrap(); | ||
| 35 | usart.bwrite_all(&buf).unwrap(); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | struct ZeroClock; | ||
| 40 | |||
| 41 | impl Clock for ZeroClock { | ||
| 42 | fn now(&self) -> u64 { | ||
| 43 | 0 | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 48 | |||
| 49 | #[entry] | ||
| 50 | fn main() -> ! { | ||
| 51 | info!("Hello World!"); | ||
| 52 | |||
| 53 | let pp = pac::Peripherals::take().unwrap(); | ||
| 54 | |||
| 55 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 56 | w.dbg_sleep().set_bit(); | ||
| 57 | w.dbg_standby().set_bit(); | ||
| 58 | w.dbg_stop().set_bit() | ||
| 59 | }); | ||
| 60 | |||
| 61 | pp.RCC.ahb1enr.modify(|_, w| w.dma1en().set_bit()); | ||
| 62 | |||
| 63 | pp.RCC.ahb2enr.modify(|_, w| { | ||
| 64 | w.gpioaen().set_bit(); | ||
| 65 | w.gpioben().set_bit(); | ||
| 66 | w.gpiocen().set_bit(); | ||
| 67 | w.gpioden().set_bit(); | ||
| 68 | w.gpioeen().set_bit(); | ||
| 69 | w.gpiofen().set_bit(); | ||
| 70 | w | ||
| 71 | }); | ||
| 72 | |||
| 73 | pp.RCC.apb2enr.modify(|_, w| { | ||
| 74 | w.syscfgen().set_bit(); | ||
| 75 | w | ||
| 76 | }); | ||
| 77 | //pp.RCC.apb1enr.modify(|_, w| { | ||
| 78 | //w.usart3en().enabled(); | ||
| 79 | //w | ||
| 80 | //}); | ||
| 81 | |||
| 82 | unsafe { embassy::time::set_clock(&ZeroClock) }; | ||
| 83 | |||
| 84 | let executor = EXECUTOR.put(Executor::new()); | ||
| 85 | |||
| 86 | executor.run(|spawner| { | ||
| 87 | unwrap!(spawner.spawn(main_task())); | ||
| 88 | }) | ||
| 89 | } \ No newline at end of file | ||
