Skip to content

Commit

Permalink
Async examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Oct 14, 2023
1 parent e9d1744 commit bb92e2a
Show file tree
Hide file tree
Showing 21 changed files with 223 additions and 32 deletions.
4 changes: 2 additions & 2 deletions examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use esp_idf_hal::adc::*;
use esp_idf_hal::peripherals::Peripherals;

fn main() -> anyhow::Result<()> {
let peripherals = Peripherals::take().unwrap();
let peripherals = Peripherals::take()?;

#[cfg(not(esp32))]
let mut adc = AdcDriver::new(peripherals.adc1, &Config::new().calibration(true))?;
Expand All @@ -32,6 +32,6 @@ fn main() -> anyhow::Result<()> {
loop {
// you can change the sleep duration depending on how often you want to sample
thread::sleep(Duration::from_millis(10));
println!("ADC value: {}", adc.read(&mut adc_pin).unwrap());
println!("ADC value: {}", adc.read(&mut adc_pin)?);
}
}
4 changes: 2 additions & 2 deletions examples/adc_oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn main() -> anyhow::Result<()> {
use esp_idf_hal::adc::oneshot::*;
use esp_idf_hal::peripherals::Peripherals;

let peripherals = Peripherals::take().unwrap();
let peripherals = Peripherals::take()?;

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

Expand Down
2 changes: 1 addition & 1 deletion examples/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use esp_idf_hal::peripherals::Peripherals;
fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches();

let peripherals = Peripherals::take().unwrap();
let peripherals = Peripherals::take()?;
let mut led = PinDriver::output(peripherals.pins.gpio4)?;

loop {
Expand Down
32 changes: 32 additions & 0 deletions examples/blinky_async.rs
Original file line number Diff line number Diff line change
@@ -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?;
}
})
}
2 changes: 1 addition & 1 deletion examples/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use esp_idf_hal::peripherals::Peripherals;
fn main() -> anyhow::Result<()> {
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 button = PinDriver::input(peripherals.pins.gpio9)?;

Expand Down
35 changes: 35 additions & 0 deletions examples/button_async.rs
Original file line number Diff line number Diff line change
@@ -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()?;
}
})
}
2 changes: 1 addition & 1 deletion examples/i2c_master_slave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn main() -> anyhow::Result<()> {

println!("Starting I2C self test");

let peripherals = Peripherals::take().unwrap();
let peripherals = Peripherals::take()?;

let mut i2c_master = i2c_master_init(
peripherals.i2c0,
Expand Down
2 changes: 1 addition & 1 deletion examples/i2c_ssd1306.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const SSD1306_ADDRESS: u8 = 0x3c;
fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches();

let peripherals = Peripherals::take().unwrap();
let peripherals = Peripherals::take()?;
let i2c = peripherals.i2c0;
let sda = peripherals.pins.gpio5;
let scl = peripherals.pins.gpio6;
Expand Down
2 changes: 1 addition & 1 deletion examples/ledc_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() -> anyhow::Result<()> {

println!("Configuring output channel");

let peripherals = Peripherals::take().unwrap();
let peripherals = Peripherals::take()?;
let mut channel = LedcDriver::new(
peripherals.ledc.channel0,
LedcTimerDriver::new(
Expand Down
2 changes: 1 addition & 1 deletion examples/ledc_threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() -> anyhow::Result<()> {

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 timer = Arc::new(LedcTimerDriver::new(peripherals.ledc.timer0, &config)?);
let channel0 = LedcDriver::new(
Expand Down
2 changes: 1 addition & 1 deletion examples/rmt_morse_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use esp_idf_hal::units::FromValueType;
fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches();

let peripherals = Peripherals::take().unwrap();
let peripherals = Peripherals::take()?;
let mut channel = peripherals.rmt.channel0;
let mut led = peripherals.pins.gpio17;
let stop = peripherals.pins.gpio16;
Expand Down
2 changes: 1 addition & 1 deletion examples/rmt_musical_buzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use notes::*;
fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches();

let peripherals = Peripherals::take().unwrap();
let peripherals = Peripherals::take()?;
let led = peripherals.pins.gpio17;
let channel = peripherals.rmt.channel0;
let config = TransmitConfig::new().looping(Loop::Endless);
Expand Down
2 changes: 1 addition & 1 deletion examples/rmt_neopixel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use esp_idf_hal::rmt::*;
fn main() -> Result<()> {
esp_idf_hal::sys::link_patches();

let peripherals = Peripherals::take().unwrap();
let peripherals = Peripherals::take()?;
// Onboard RGB LED pin
// ESP32-C3-DevKitC-02 gpio8, ESP32-C3-DevKit-RUST-1 gpio2
let led = peripherals.pins.gpio2;
Expand Down
4 changes: 2 additions & 2 deletions examples/rmt_transceiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use esp_idf_hal::rmt::{
fn main() -> anyhow::Result<()> {
println!("Starting APP!");

let peripherals = Peripherals::take().unwrap();
let peripherals = Peripherals::take()?;

/*
*********************** SET UP RMT RECEIVER ******************************
Expand All @@ -40,7 +40,7 @@ fn main() -> anyhow::Result<()> {
250,
)?;

rx.start().unwrap();
rx.start()?;

let _ = std::thread::Builder::new()
.stack_size(10000)
Expand Down
2 changes: 1 addition & 1 deletion examples/spi_loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use esp_idf_hal::spi::*;
fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches();

let peripherals = Peripherals::take().unwrap();
let peripherals = Peripherals::take()?;
let spi = peripherals.spi2;

let sclk = peripherals.pins.gpio6;
Expand Down
73 changes: 73 additions & 0 deletions examples/spi_loopback_async.rs
Original file line number Diff line number Diff line change
@@ -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?}");
}
})
}
2 changes: 1 addition & 1 deletion examples/spi_st7789.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use embedded_graphics::prelude::*;
use mipidsi::{Builder, Orientation};

fn main() -> anyhow::Result<()> {
let peripherals = Peripherals::take().unwrap();
let peripherals = Peripherals::take()?;
let spi = peripherals.spi2;

let rst = PinDriver::output(peripherals.pins.gpio3)?;
Expand Down
11 changes: 7 additions & 4 deletions examples/timer_async.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
use esp_idf_hal::peripherals::*;
use esp_idf_hal::sys::EspError;
use esp_idf_hal::task::*;
use esp_idf_hal::timer::*;

fn main() -> Result<(), EspError> {
// 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
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 = esp_idf_hal::timer::TimerDriver::new(per.timer00, &timer_conf)?;
let mut timer = TimerDriver::new(per.timer00, &TimerConfig::new())?;

esp_idf_hal::task::block_on(async move {
block_on(async {
loop {
timer.delay(timer.tick_hz()).await?; // Every second

println!("Tick");
}
})
Expand Down
11 changes: 7 additions & 4 deletions examples/timer_notify.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
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> {
// 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
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.
let notification = Notification::new();

// BaseClock for the Timer is the APB_CLK that is running on 80MHz at default
// The default clock-divider is -> 80
// 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 mut timer = esp_idf_hal::timer::TimerDriver::new(per.timer00, &timer_conf)?;
let timer_conf = config::Config::new().auto_reload(true);
let mut timer = TimerDriver::new(per.timer00, &timer_conf)?;

// Every half a second
timer.set_alarm(timer.tick_hz() / 2)?;
Expand Down
13 changes: 6 additions & 7 deletions examples/uart_loopback.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! UART loopback test
//!
//! Folowing pins are used:
//! TX GPIO5
//! RX GPIO6
//! TX GPIO12
//! RX GPIO13
//!
//! Depending on your target and the board you are using you have to change the pins.
//!
Expand All @@ -18,9 +18,9 @@ use esp_idf_hal::uart::*;
fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches();

let peripherals = Peripherals::take().unwrap();
let tx = peripherals.pins.gpio5;
let rx = peripherals.pins.gpio6;
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));
Expand All @@ -31,8 +31,7 @@ fn main() -> anyhow::Result<()> {
Option::<gpio::Gpio0>::None,
Option::<gpio::Gpio1>::None,
&config,
)
.unwrap();
)?;

loop {
uart.write(&[0xaa])?;
Expand Down
Loading

0 comments on commit bb92e2a

Please sign in to comment.