Add adapter for implementing async traits for blocking types
This allows writing drivers relying on async traits, while still functioning with implementations that already implement the embedded-hal traits. Add examples to stm32l4 for using this feature.
This commit is contained in:
29
examples/stm32l4/src/bin/i2c_blocking_async.rs
Normal file
29
examples/stm32l4/src/bin/i2c_blocking_async.rs
Normal file
@ -0,0 +1,29 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
|
||||
use embassy::executor::Spawner;
|
||||
use embassy_stm32::dma::NoDma;
|
||||
use embassy_stm32::i2c::I2c;
|
||||
use embassy_stm32::interrupt;
|
||||
use embassy_stm32::time::Hertz;
|
||||
use embassy_stm32::Peripherals;
|
||||
use embassy_traits::{adapter::BlockingAsync, i2c::I2c as _};
|
||||
use example_common::{info, unwrap};
|
||||
|
||||
const ADDRESS: u8 = 0x5F;
|
||||
const WHOAMI: u8 = 0x0F;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) -> ! {
|
||||
let irq = interrupt::take!(I2C2_EV);
|
||||
let i2c = I2c::new(p.I2C2, p.PB10, p.PB11, irq, NoDma, NoDma, Hertz(100_000));
|
||||
let mut i2c = BlockingAsync::new(i2c);
|
||||
|
||||
let mut data = [0u8; 1];
|
||||
unwrap!(i2c.write_read(ADDRESS, &[WHOAMI], &mut data).await);
|
||||
info!("Whoami: {}", data[0]);
|
||||
}
|
57
examples/stm32l4/src/bin/spi_blocking_async.rs
Normal file
57
examples/stm32l4/src/bin/spi_blocking_async.rs
Normal file
@ -0,0 +1,57 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
|
||||
use embassy::executor::Spawner;
|
||||
use embassy_stm32::dma::NoDma;
|
||||
use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
|
||||
use embassy_stm32::spi::{Config, Spi};
|
||||
use embassy_stm32::time::Hertz;
|
||||
use embassy_stm32::Peripherals;
|
||||
use embassy_traits::{adapter::BlockingAsync, spi::FullDuplex};
|
||||
use embedded_hal::digital::v2::{InputPin, OutputPin};
|
||||
use example_common::*;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
info!("Hello World!");
|
||||
|
||||
let spi = Spi::new(
|
||||
p.SPI3,
|
||||
p.PC10,
|
||||
p.PC12,
|
||||
p.PC11,
|
||||
NoDma,
|
||||
NoDma,
|
||||
Hertz(1_000_000),
|
||||
Config::default(),
|
||||
);
|
||||
|
||||
let mut spi = BlockingAsync::new(spi);
|
||||
|
||||
// These are the pins for the Inventek eS-Wifi SPI Wifi Adapter.
|
||||
|
||||
let _boot = Output::new(p.PB12, Level::Low, Speed::VeryHigh);
|
||||
let _wake = Output::new(p.PB13, Level::Low, Speed::VeryHigh);
|
||||
let mut reset = Output::new(p.PE8, Level::Low, Speed::VeryHigh);
|
||||
let mut cs = Output::new(p.PE0, Level::High, Speed::VeryHigh);
|
||||
let ready = Input::new(p.PE1, Pull::Up);
|
||||
|
||||
cortex_m::asm::delay(100_000);
|
||||
unwrap!(reset.set_high());
|
||||
cortex_m::asm::delay(100_000);
|
||||
|
||||
while unwrap!(ready.is_low()) {
|
||||
info!("waiting for ready");
|
||||
}
|
||||
|
||||
let write = [0x0A; 10];
|
||||
let mut read = [0; 10];
|
||||
unwrap!(cs.set_low());
|
||||
spi.read_write(&mut read, &write).await.ok();
|
||||
unwrap!(cs.set_high());
|
||||
info!("xfer {=[u8]:x}", read);
|
||||
}
|
32
examples/stm32l4/src/bin/usart_blocking_async.rs
Normal file
32
examples/stm32l4/src/bin/usart_blocking_async.rs
Normal file
@ -0,0 +1,32 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
|
||||
use embassy::executor::Spawner;
|
||||
use embassy::traits::{
|
||||
adapter::BlockingAsync,
|
||||
uart::{Read, Write},
|
||||
};
|
||||
use embassy_stm32::dma::NoDma;
|
||||
use embassy_stm32::usart::{Config, Uart};
|
||||
use embassy_stm32::Peripherals;
|
||||
use example_common::*;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
let config = Config::default();
|
||||
let usart = Uart::new(p.UART4, p.PA1, p.PA0, NoDma, NoDma, config);
|
||||
let mut usart = BlockingAsync::new(usart);
|
||||
|
||||
unwrap!(usart.write(b"Hello Embassy World!\r\n").await);
|
||||
info!("wrote Hello, starting echo");
|
||||
|
||||
let mut buf = [0u8; 1];
|
||||
loop {
|
||||
unwrap!(usart.read(&mut buf).await);
|
||||
unwrap!(usart.write(&buf).await);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user