diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf/src/bin/buffered_uart.rs | 20 | ||||
| -rw-r--r-- | examples/stm32f4/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/usart_buffered.rs | 36 |
3 files changed, 42 insertions, 15 deletions
diff --git a/examples/nrf/src/bin/buffered_uart.rs b/examples/nrf/src/bin/buffered_uart.rs index a64c5821b..782c39499 100644 --- a/examples/nrf/src/bin/buffered_uart.rs +++ b/examples/nrf/src/bin/buffered_uart.rs | |||
| @@ -6,7 +6,7 @@ use defmt::*; | |||
| 6 | use embassy::executor::Spawner; | 6 | use embassy::executor::Spawner; |
| 7 | use embassy_nrf::buffered_uarte::State; | 7 | use embassy_nrf::buffered_uarte::State; |
| 8 | use embassy_nrf::{buffered_uarte::BufferedUarte, interrupt, uarte, Peripherals}; | 8 | use embassy_nrf::{buffered_uarte::BufferedUarte, interrupt, uarte, Peripherals}; |
| 9 | use embedded_io::asynch::{Read, Write}; | 9 | use embedded_io::asynch::{BufRead, Write}; |
| 10 | use futures::pin_mut; | 10 | use futures::pin_mut; |
| 11 | 11 | ||
| 12 | use defmt_rtt as _; // global logger | 12 | use defmt_rtt as _; // global logger |
| @@ -46,23 +46,13 @@ async fn main(_spawner: Spawner, p: Peripherals) { | |||
| 46 | unwrap!(u.write_all(b"Hello!\r\n").await); | 46 | unwrap!(u.write_all(b"Hello!\r\n").await); |
| 47 | info!("wrote hello in uart!"); | 47 | info!("wrote hello in uart!"); |
| 48 | 48 | ||
| 49 | // Simple demo, reading 8-char chunks and echoing them back reversed. | ||
| 50 | loop { | 49 | loop { |
| 51 | info!("reading..."); | 50 | info!("reading..."); |
| 52 | let mut buf = [0u8; 8]; | 51 | let buf = unwrap!(u.fill_buf().await); |
| 53 | unwrap!(u.read_exact(&mut buf).await); | ||
| 54 | info!("read done, got {}", buf); | 52 | info!("read done, got {}", buf); |
| 55 | 53 | ||
| 56 | // Reverse buf | 54 | // Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again |
| 57 | for i in 0..4 { | 55 | let n = buf.len(); |
| 58 | buf.swap(i, 7 - i); | 56 | u.consume(n); |
| 59 | } | ||
| 60 | |||
| 61 | info!("writing..."); | ||
| 62 | unwrap!(u.write_all(&buf).await); | ||
| 63 | info!("write done"); | ||
| 64 | |||
| 65 | // Wait until the bytes are actually finished being transmitted | ||
| 66 | unwrap!(u.flush().await); | ||
| 67 | } | 57 | } |
| 68 | } | 58 | } |
diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index e2065bed9..651d01388 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml | |||
| @@ -16,6 +16,7 @@ defmt-rtt = "0.3" | |||
| 16 | cortex-m = "0.7.3" | 16 | cortex-m = "0.7.3" |
| 17 | cortex-m-rt = "0.7.0" | 17 | cortex-m-rt = "0.7.0" |
| 18 | embedded-hal = "0.2.6" | 18 | embedded-hal = "0.2.6" |
| 19 | embedded-io = "0.3.0" | ||
| 19 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 20 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
| 20 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 21 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 21 | heapless = { version = "0.7.5", default-features = false } | 22 | 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..80b65f0d4 --- /dev/null +++ b/examples/stm32f4/src/bin/usart_buffered.rs | |||
| @@ -0,0 +1,36 @@ | |||
| 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 buf = buf_usart.fill_buf().await.unwrap(); | ||
| 30 | info!("Received: {}", buf); | ||
| 31 | |||
| 32 | // Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again | ||
| 33 | let n = buf.len(); | ||
| 34 | buf_usart.consume(n); | ||
| 35 | } | ||
| 36 | } | ||
