diff options
| author | chemicstry <[email protected]> | 2022-05-26 14:02:55 +0300 |
|---|---|---|
| committer | chemicstry <[email protected]> | 2022-05-26 14:02:55 +0300 |
| commit | 1d951a54beeee4184315260b3f2ba0a021f4f9d0 (patch) | |
| tree | 2c3444eb1f8c15ce6be53ece689080b19d65020e /examples | |
| parent | 36a1f203648dcb402727ea3eb5d30cf1f6993795 (diff) | |
Reimplement BufRead for BufferedUart
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f4/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/usart_buffered.rs | 38 |
2 files changed, 39 insertions, 1 deletions
diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index e2065bed9..4cd8b7a28 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml | |||
| @@ -9,13 +9,13 @@ resolver = "2" | |||
| 9 | [dependencies] | 9 | [dependencies] |
| 10 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits"] } | 10 | embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits"] } |
| 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti"] } | 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti"] } |
| 12 | |||
| 13 | defmt = "0.3" | 12 | defmt = "0.3" |
| 14 | defmt-rtt = "0.3" | 13 | defmt-rtt = "0.3" |
| 15 | 14 | ||
| 16 | cortex-m = "0.7.3" | 15 | cortex-m = "0.7.3" |
| 17 | cortex-m-rt = "0.7.0" | 16 | cortex-m-rt = "0.7.0" |
| 18 | embedded-hal = "0.2.6" | 17 | embedded-hal = "0.2.6" |
| 18 | embedded-io = "0.3.0" | ||
| 19 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 19 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
| 20 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 20 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 21 | heapless = { version = "0.7.5", default-features = false } | 21 | heapless = { version = "0.7.5", default-features = false } |
diff --git a/examples/stm32f4/src/bin/usart_buffered.rs b/examples/stm32f4/src/bin/usart_buffered.rs new file mode 100644 index 000000000..c5fbbbe5d --- /dev/null +++ b/examples/stm32f4/src/bin/usart_buffered.rs | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use defmt_rtt as _; // global logger | ||
| 7 | use embassy::executor::Spawner; | ||
| 8 | use embassy_stm32::dma::NoDma; | ||
| 9 | use embassy_stm32::usart::{BufferedUart, Config, State, Uart}; | ||
| 10 | use embassy_stm32::{interrupt, Peripherals}; | ||
| 11 | use embedded_io::asynch::BufRead; | ||
| 12 | use panic_probe as _; | ||
| 13 | |||
| 14 | #[embassy::main] | ||
| 15 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 16 | info!("Hello World!"); | ||
| 17 | |||
| 18 | let config = Config::default(); | ||
| 19 | let usart = Uart::new(p.USART3, p.PD9, p.PD8, NoDma, NoDma, config); | ||
| 20 | |||
| 21 | let mut state = State::new(); | ||
| 22 | let irq = interrupt::take!(USART3); | ||
| 23 | let mut tx_buf = [0u8; 32]; | ||
| 24 | let mut rx_buf = [0u8; 32]; | ||
| 25 | let mut buf_usart = | ||
| 26 | unsafe { BufferedUart::new(&mut state, usart, irq, &mut tx_buf, &mut rx_buf) }; | ||
| 27 | |||
| 28 | loop { | ||
| 29 | let n = { | ||
| 30 | let buf = buf_usart.fill_buf().await.unwrap(); | ||
| 31 | info!("Received: {}", buf); | ||
| 32 | buf.len() | ||
| 33 | }; | ||
| 34 | |||
| 35 | // Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again | ||
| 36 | buf_usart.consume(n); | ||
| 37 | } | ||
| 38 | } | ||
