Async examples

This commit is contained in:
ivmarkov 2023-10-14 07:36:30 +00:00
parent e9d1744065
commit bb92e2a47e
21 changed files with 223 additions and 32 deletions

View File

@ -11,7 +11,7 @@ use esp_idf_hal::adc::*;
use esp_idf_hal::peripherals::Peripherals; use esp_idf_hal::peripherals::Peripherals;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
#[cfg(not(esp32))] #[cfg(not(esp32))]
let mut adc = AdcDriver::new(peripherals.adc1, &Config::new().calibration(true))?; let mut adc = AdcDriver::new(peripherals.adc1, &Config::new().calibration(true))?;
@ -32,6 +32,6 @@ fn main() -> anyhow::Result<()> {
loop { loop {
// you can change the sleep duration depending on how often you want to sample // you can change the sleep duration depending on how often you want to sample
thread::sleep(Duration::from_millis(10)); thread::sleep(Duration::from_millis(10));
println!("ADC value: {}", adc.read(&mut adc_pin).unwrap()); println!("ADC value: {}", adc.read(&mut adc_pin)?);
} }
} }

View File

@ -11,7 +11,7 @@ fn main() -> anyhow::Result<()> {
use esp_idf_hal::adc::oneshot::*; use esp_idf_hal::adc::oneshot::*;
use esp_idf_hal::peripherals::Peripherals; use esp_idf_hal::peripherals::Peripherals;
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
#[cfg(not(esp32))] #[cfg(not(esp32))]
let adc = AdcDriver::new(peripherals.adc1)?; let adc = AdcDriver::new(peripherals.adc1)?;
@ -36,7 +36,7 @@ fn main() -> anyhow::Result<()> {
loop { loop {
// you can change the sleep duration depending on how often you want to sample // you can change the sleep duration depending on how often you want to sample
thread::sleep(Duration::from_millis(100)); thread::sleep(Duration::from_millis(100));
println!("ADC value: {}", adc.read(&mut adc_pin).unwrap()); println!("ADC value: {}", adc.read(&mut adc_pin)?);
} }
} }

View File

@ -12,7 +12,7 @@ use esp_idf_hal::peripherals::Peripherals;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches(); esp_idf_hal::sys::link_patches();
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
let mut led = PinDriver::output(peripherals.pins.gpio4)?; let mut led = PinDriver::output(peripherals.pins.gpio4)?;
loop { loop {

32
examples/blinky_async.rs Normal file
View File

@ -0,0 +1,32 @@
//! Blinks an LED
//!
//! This assumes that a LED is connected to GPIO4.
//! Depending on your target and the board you are using you should change the pin.
//! If your board doesn't have on-board LEDs don't forget to add an appropriate resistor.
//!
use esp_idf_hal::gpio::*;
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::task::*;
use esp_idf_hal::timer::*;
fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches();
let peripherals = Peripherals::take()?;
let mut led = PinDriver::output(peripherals.pins.gpio4)?;
let mut timer = TimerDriver::new(peripherals.timer00, &TimerConfig::new())?;
block_on(async {
loop {
led.set_high()?;
timer.delay(timer.tick_hz()).await?;
led.set_low()?;
timer.delay(timer.tick_hz()).await?;
}
})
}

View File

@ -14,7 +14,7 @@ use esp_idf_hal::peripherals::Peripherals;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches(); esp_idf_hal::sys::link_patches();
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
let mut led = PinDriver::output(peripherals.pins.gpio4)?; let mut led = PinDriver::output(peripherals.pins.gpio4)?;
let mut button = PinDriver::input(peripherals.pins.gpio9)?; let mut button = PinDriver::input(peripherals.pins.gpio9)?;

35
examples/button_async.rs Normal file
View File

@ -0,0 +1,35 @@
//! Turn an LED on/off depending on the state of a button
//!
//! This assumes that a LED is connected to GPIO4.
//! Additionally this assumes a button connected to GPIO9.
//! On an ESP32C3 development board this is the BOOT button.
//!
//! Depending on your target and the board you are using you should change the pins.
//! If your board doesn't have on-board LEDs don't forget to add an appropriate resistor.
use esp_idf_hal::gpio::*;
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::task::*;
fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches();
let peripherals = Peripherals::take()?;
let mut led = PinDriver::output(peripherals.pins.gpio4)?;
let mut button = PinDriver::input(peripherals.pins.gpio9)?;
button.set_pull(Pull::Down)?;
block_on(async {
loop {
button.wait_for_high().await?;
led.set_high()?;
button.wait_for_low().await?;
led.set_low()?;
}
})
}

View File

@ -62,7 +62,7 @@ fn main() -> anyhow::Result<()> {
println!("Starting I2C self test"); println!("Starting I2C self test");
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
let mut i2c_master = i2c_master_init( let mut i2c_master = i2c_master_init(
peripherals.i2c0, peripherals.i2c0,

View File

@ -19,7 +19,7 @@ const SSD1306_ADDRESS: u8 = 0x3c;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches(); esp_idf_hal::sys::link_patches();
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
let i2c = peripherals.i2c0; let i2c = peripherals.i2c0;
let sda = peripherals.pins.gpio5; let sda = peripherals.pins.gpio5;
let scl = peripherals.pins.gpio6; let scl = peripherals.pins.gpio6;

View File

@ -8,7 +8,7 @@ fn main() -> anyhow::Result<()> {
println!("Configuring output channel"); println!("Configuring output channel");
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
let mut channel = LedcDriver::new( let mut channel = LedcDriver::new(
peripherals.ledc.channel0, peripherals.ledc.channel0,
LedcTimerDriver::new( LedcTimerDriver::new(

View File

@ -13,7 +13,7 @@ fn main() -> anyhow::Result<()> {
println!("Setting up PWM output channels"); println!("Setting up PWM output channels");
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
let config = config::TimerConfig::new().frequency(25.kHz().into()); let config = config::TimerConfig::new().frequency(25.kHz().into());
let timer = Arc::new(LedcTimerDriver::new(peripherals.ledc.timer0, &config)?); let timer = Arc::new(LedcTimerDriver::new(peripherals.ledc.timer0, &config)?);
let channel0 = LedcDriver::new( let channel0 = LedcDriver::new(

View File

@ -25,7 +25,7 @@ use esp_idf_hal::units::FromValueType;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches(); esp_idf_hal::sys::link_patches();
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
let mut channel = peripherals.rmt.channel0; let mut channel = peripherals.rmt.channel0;
let mut led = peripherals.pins.gpio17; let mut led = peripherals.pins.gpio17;
let stop = peripherals.pins.gpio16; let stop = peripherals.pins.gpio16;

View File

@ -16,7 +16,7 @@ use notes::*;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches(); esp_idf_hal::sys::link_patches();
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
let led = peripherals.pins.gpio17; let led = peripherals.pins.gpio17;
let channel = peripherals.rmt.channel0; let channel = peripherals.rmt.channel0;
let config = TransmitConfig::new().looping(Loop::Endless); let config = TransmitConfig::new().looping(Loop::Endless);

View File

@ -19,7 +19,7 @@ use esp_idf_hal::rmt::*;
fn main() -> Result<()> { fn main() -> Result<()> {
esp_idf_hal::sys::link_patches(); esp_idf_hal::sys::link_patches();
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
// Onboard RGB LED pin // Onboard RGB LED pin
// ESP32-C3-DevKitC-02 gpio8, ESP32-C3-DevKit-RUST-1 gpio2 // ESP32-C3-DevKitC-02 gpio8, ESP32-C3-DevKit-RUST-1 gpio2
let led = peripherals.pins.gpio2; let led = peripherals.pins.gpio2;

View File

@ -28,7 +28,7 @@ use esp_idf_hal::rmt::{
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
println!("Starting APP!"); println!("Starting APP!");
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
/* /*
*********************** SET UP RMT RECEIVER ****************************** *********************** SET UP RMT RECEIVER ******************************
@ -40,7 +40,7 @@ fn main() -> anyhow::Result<()> {
250, 250,
)?; )?;
rx.start().unwrap(); rx.start()?;
let _ = std::thread::Builder::new() let _ = std::thread::Builder::new()
.stack_size(10000) .stack_size(10000)

View File

@ -21,7 +21,7 @@ use esp_idf_hal::spi::*;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches(); esp_idf_hal::sys::link_patches();
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
let spi = peripherals.spi2; let spi = peripherals.spi2;
let sclk = peripherals.pins.gpio6; let sclk = peripherals.pins.gpio6;

View File

@ -0,0 +1,73 @@
//! SPI loopback test
//!
//! Folowing pins are used:
//! SCLK GPIO6
//! SDI GPIO2
//! SDO GPIO7
//! CS_1 GPIO10
//! CS_2 GPIO3
//!
//! Depending on your target and the board you are using you have to change the pins.
//!
//! This example transfers data via SPI.
//! Connect SDI and SDO pins to see the outgoing data is read as incoming data.
use embedded_hal::spi::Operation;
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::prelude::*;
use esp_idf_hal::spi::*;
use esp_idf_hal::task::*;
fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches();
let peripherals = Peripherals::take()?;
let spi = peripherals.spi2;
let sclk = peripherals.pins.gpio6;
let serial_in = peripherals.pins.gpio2; // SDI
let serial_out = peripherals.pins.gpio7; // SDO
let cs_1 = peripherals.pins.gpio10;
let cs_2 = peripherals.pins.gpio3;
println!("Starting SPI loopback test");
let driver = SpiDriver::new::<SPI2>(
spi,
sclk,
serial_out,
Some(serial_in),
&SpiDriverConfig::new(),
)?;
let config_1 = config::Config::new().baudrate(26.MHz().into());
let mut device_1 = SpiDeviceDriver::new(&driver, Some(cs_1), &config_1)?;
let config_2 = config::Config::new().baudrate(13.MHz().into());
let mut device_2 = SpiDeviceDriver::new(&driver, Some(cs_2), &config_2)?;
let mut read = [0u8; 4];
let write = [0xde, 0xad, 0xbe, 0xef];
block_on(async {
loop {
device_1.transfer_async(&mut read, &write).await?;
println!("Device 1: Wrote {write:x?}, read {read:x?}");
let write_buf = [0xde, 0xad, 0xbe, 0xef];
let mut write_in_place_buf = [0xde, 0xad, 0xbe, 0xef];
let mut read_buf = [0; 8];
println!("Device 2: To write {write_in_place_buf:x?} ... ");
// cascade multiple operations with different buffer length into one transaction
device_2
.transaction_async(&mut [
Operation::Write(&write_buf),
Operation::TransferInPlace(&mut write_in_place_buf),
Operation::Read(&mut read_buf),
])
.await?;
println!("... read {write_in_place_buf:x?}");
}
})
}

View File

@ -32,7 +32,7 @@ use embedded_graphics::prelude::*;
use mipidsi::{Builder, Orientation}; use mipidsi::{Builder, Orientation};
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
let spi = peripherals.spi2; let spi = peripherals.spi2;
let rst = PinDriver::output(peripherals.pins.gpio3)?; let rst = PinDriver::output(peripherals.pins.gpio3)?;

View File

@ -1,18 +1,21 @@
use esp_idf_hal::peripherals::*;
use esp_idf_hal::sys::EspError; use esp_idf_hal::sys::EspError;
use esp_idf_hal::task::*;
use esp_idf_hal::timer::*;
fn main() -> Result<(), EspError> { fn main() -> Result<(), EspError> {
// It is necessary to call this function once. Otherwise some patches to the runtime // It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_hal::sys::link_patches(); esp_idf_hal::sys::link_patches();
let per = esp_idf_hal::peripherals::Peripherals::take().unwrap(); let per = Peripherals::take()?;
let timer_conf = esp_idf_hal::timer::config::Config::new().auto_reload(true); let mut timer = TimerDriver::new(per.timer00, &TimerConfig::new())?;
let mut timer = esp_idf_hal::timer::TimerDriver::new(per.timer00, &timer_conf)?;
esp_idf_hal::task::block_on(async move { block_on(async {
loop { loop {
timer.delay(timer.tick_hz()).await?; // Every second timer.delay(timer.tick_hz()).await?; // Every second
println!("Tick"); println!("Tick");
} }
}) })

View File

@ -1,13 +1,16 @@
use std::num::NonZeroU32; use std::num::NonZeroU32;
use esp_idf_hal::{sys::EspError, task::notification::Notification}; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported use esp_idf_hal::peripherals::*;
use esp_idf_hal::sys::EspError;
use esp_idf_hal::task::notification::Notification;
use esp_idf_hal::timer::*;
fn main() -> Result<(), EspError> { fn main() -> Result<(), EspError> {
// It is necessary to call this function once. Otherwise some patches to the runtime // It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_hal::sys::link_patches(); esp_idf_hal::sys::link_patches();
let per = esp_idf_hal::peripherals::Peripherals::take().unwrap(); let per = Peripherals::take()?;
// A safer abstraction over FreeRTOS/ESP-IDF task notifications. // A safer abstraction over FreeRTOS/ESP-IDF task notifications.
let notification = Notification::new(); let notification = Notification::new();
@ -15,8 +18,8 @@ fn main() -> Result<(), EspError> {
// BaseClock for the Timer is the APB_CLK that is running on 80MHz at default // BaseClock for the Timer is the APB_CLK that is running on 80MHz at default
// The default clock-divider is -> 80 // The default clock-divider is -> 80
// default APB clk is available with the APB_CLK_FREQ constant // default APB clk is available with the APB_CLK_FREQ constant
let timer_conf = esp_idf_hal::timer::config::Config::new().auto_reload(true); let timer_conf = config::Config::new().auto_reload(true);
let mut timer = esp_idf_hal::timer::TimerDriver::new(per.timer00, &timer_conf)?; let mut timer = TimerDriver::new(per.timer00, &timer_conf)?;
// Every half a second // Every half a second
timer.set_alarm(timer.tick_hz() / 2)?; timer.set_alarm(timer.tick_hz() / 2)?;

View File

@ -1,8 +1,8 @@
//! UART loopback test //! UART loopback test
//! //!
//! Folowing pins are used: //! Folowing pins are used:
//! TX GPIO5 //! TX GPIO12
//! RX GPIO6 //! RX GPIO13
//! //!
//! Depending on your target and the board you are using you have to change the pins. //! Depending on your target and the board you are using you have to change the pins.
//! //!
@ -18,9 +18,9 @@ use esp_idf_hal::uart::*;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches(); esp_idf_hal::sys::link_patches();
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take()?;
let tx = peripherals.pins.gpio5; let tx = peripherals.pins.gpio12;
let rx = peripherals.pins.gpio6; let rx = peripherals.pins.gpio13;
println!("Starting UART loopback test"); println!("Starting UART loopback test");
let config = config::Config::new().baudrate(Hertz(115_200)); let config = config::Config::new().baudrate(Hertz(115_200));
@ -31,8 +31,7 @@ fn main() -> anyhow::Result<()> {
Option::<gpio::Gpio0>::None, Option::<gpio::Gpio0>::None,
Option::<gpio::Gpio1>::None, Option::<gpio::Gpio1>::None,
&config, &config,
) )?;
.unwrap();
loop { loop {
uart.write(&[0xaa])?; uart.write(&[0xaa])?;

View File

@ -0,0 +1,46 @@
//! UART loopback test
//!
//! Folowing pins are used:
//! TX GPIO12
//! RX GPIO13
//!
//! Depending on your target and the board you are using you have to change the pins.
//!
//! This example transfers data via UART.
//! Connect TX and RX pins to see the outgoing data is read as incoming data.
use esp_idf_hal::gpio;
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::prelude::*;
use esp_idf_hal::task::*;
use esp_idf_hal::uart::*;
fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches();
let peripherals = Peripherals::take()?;
let tx = peripherals.pins.gpio12;
let rx = peripherals.pins.gpio13;
println!("Starting UART loopback test");
let config = config::Config::new().baudrate(Hertz(115_200));
let uart = AsyncUartDriver::new(
peripherals.uart1,
tx,
rx,
Option::<gpio::Gpio0>::None,
Option::<gpio::Gpio1>::None,
&config,
)?;
block_on(async {
loop {
uart.write(&[0xaa]).await?;
let mut buf = [0_u8; 1];
uart.read(&mut buf).await?;
println!("Written 0xaa, read 0x{:02x}", buf[0]);
}
})
}