mirror of
https://github.com/esp-rs/esp-idf-hal.git
synced 2025-09-29 21:31:17 +00:00
Async examples
This commit is contained in:
parent
e9d1744065
commit
bb92e2a47e
@ -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))?;
|
||||
@ -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)?);
|
||||
}
|
||||
}
|
||||
|
@ -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)?;
|
||||
@ -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)?);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
32
examples/blinky_async.rs
Normal file
32
examples/blinky_async.rs
Normal 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?;
|
||||
}
|
||||
})
|
||||
}
|
@ -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)?;
|
||||
|
||||
|
35
examples/button_async.rs
Normal file
35
examples/button_async.rs
Normal 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()?;
|
||||
}
|
||||
})
|
||||
}
|
@ -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,
|
||||
|
@ -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;
|
||||
|
@ -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(
|
||||
|
@ -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(
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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 ******************************
|
||||
@ -40,7 +40,7 @@ fn main() -> anyhow::Result<()> {
|
||||
250,
|
||||
)?;
|
||||
|
||||
rx.start().unwrap();
|
||||
rx.start()?;
|
||||
|
||||
let _ = std::thread::Builder::new()
|
||||
.stack_size(10000)
|
||||
|
@ -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;
|
||||
|
73
examples/spi_loopback_async.rs
Normal file
73
examples/spi_loopback_async.rs
Normal 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?}");
|
||||
}
|
||||
})
|
||||
}
|
@ -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)?;
|
||||
|
@ -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");
|
||||
}
|
||||
})
|
||||
|
@ -1,13 +1,16 @@
|
||||
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();
|
||||
@ -15,8 +18,8 @@ fn main() -> Result<(), EspError> {
|
||||
// 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)?;
|
||||
|
@ -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.
|
||||
//!
|
||||
@ -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));
|
||||
@ -31,8 +31,7 @@ fn main() -> anyhow::Result<()> {
|
||||
Option::<gpio::Gpio0>::None,
|
||||
Option::<gpio::Gpio1>::None,
|
||||
&config,
|
||||
)
|
||||
.unwrap();
|
||||
)?;
|
||||
|
||||
loop {
|
||||
uart.write(&[0xaa])?;
|
||||
|
46
examples/uart_loopback_async.rs
Normal file
46
examples/uart_loopback_async.rs
Normal 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]);
|
||||
}
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user