mirror of
https://github.com/esp-rs/esp-idf-hal.git
synced 2025-09-27 12:21:02 +00:00
Add some basic examples (#46)
This commit is contained in:
parent
87e9c4878e
commit
e57736e4c2
@ -33,3 +33,5 @@ The examples could be built and flashed conveniently with [`cargo-espflash`](htt
|
||||
```
|
||||
$ cargo espflash --release --target riscv32imc-esp-espidf --example ledc-simple --monitor /dev/ttyUSB0
|
||||
```
|
||||
|
||||
In order to run the examples on other chips you will most likely need to adapt at least the used pins.
|
||||
|
29
examples/blinky.rs
Normal file
29
examples/blinky.rs
Normal file
@ -0,0 +1,29 @@
|
||||
//! 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 std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use embedded_hal::digital::blocking::OutputPin;
|
||||
use esp_idf_hal::peripherals::Peripherals;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
esp_idf_sys::link_patches();
|
||||
esp_idf_svc::log::EspLogger::initialize_default();
|
||||
|
||||
let peripherals = Peripherals::take().unwrap();
|
||||
let mut led = peripherals.pins.gpio4.into_output()?;
|
||||
|
||||
loop {
|
||||
led.set_high()?;
|
||||
// we are using thread::sleep here to make sure the watchdog isn't triggered
|
||||
thread::sleep(Duration::from_millis(1000));
|
||||
|
||||
led.set_low()?;
|
||||
thread::sleep(Duration::from_millis(1000));
|
||||
}
|
||||
}
|
37
examples/button.rs
Normal file
37
examples/button.rs
Normal file
@ -0,0 +1,37 @@
|
||||
//! 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 std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use embedded_hal::digital::blocking::InputPin;
|
||||
use embedded_hal::digital::blocking::OutputPin;
|
||||
use esp_idf_hal::gpio::Pull;
|
||||
use esp_idf_hal::peripherals::Peripherals;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
esp_idf_sys::link_patches();
|
||||
esp_idf_svc::log::EspLogger::initialize_default();
|
||||
|
||||
let peripherals = Peripherals::take().unwrap();
|
||||
let mut led = peripherals.pins.gpio4.into_output()?;
|
||||
let mut button = peripherals.pins.gpio9.into_input()?;
|
||||
button.set_pull_down()?;
|
||||
|
||||
loop {
|
||||
// we are using thread::sleep here to make sure the watchdog isn't triggered
|
||||
thread::sleep(Duration::from_millis(10));
|
||||
|
||||
if button.is_high()? {
|
||||
led.set_low()?;
|
||||
} else {
|
||||
led.set_high()?;
|
||||
}
|
||||
}
|
||||
}
|
78
examples/i2c_ssd1306.rs
Normal file
78
examples/i2c_ssd1306.rs
Normal file
@ -0,0 +1,78 @@
|
||||
//! I2C test with SSD1306
|
||||
//!
|
||||
//! Folowing pins are used:
|
||||
//! SDA GPIO5
|
||||
//! SCL GPIO6
|
||||
//!
|
||||
//! Depending on your target and the board you are using you have to change the pins.
|
||||
//!
|
||||
//! For this example you need to hook up an SSD1306 I2C display.
|
||||
//! The display will flash black and white.
|
||||
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use embedded_hal::i2c::blocking::Write;
|
||||
use esp_idf_hal::i2c;
|
||||
use esp_idf_hal::peripherals::Peripherals;
|
||||
use esp_idf_hal::prelude::*;
|
||||
use log::*;
|
||||
|
||||
const SSD1306_ADDRESS: u8 = 0x3c;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
esp_idf_sys::link_patches();
|
||||
esp_idf_svc::log::EspLogger::initialize_default();
|
||||
|
||||
let peripherals = Peripherals::take().unwrap();
|
||||
let i2c = peripherals.i2c0;
|
||||
let sda = peripherals.pins.gpio5;
|
||||
let scl = peripherals.pins.gpio6;
|
||||
|
||||
info!("Starting I2C SSD1306 test");
|
||||
|
||||
let config = <i2c::config::MasterConfig as Default>::default().baudrate(100.kHz().into());
|
||||
let mut i2c = i2c::Master::<i2c::I2C0, _, _>::new(i2c, i2c::MasterPins { sda, scl }, config)?;
|
||||
|
||||
// initialze the display - don't worry about the meaning of these bytes - it's specific to SSD1306
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xae])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xd4])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0x80])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xa8])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0x3f])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xd3])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0x00])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0x40])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0x8d])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0x14])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xa1])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xc8])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xda])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0x12])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0x81])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xcf])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xf1])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xdb])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0x40])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xa4])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xa6])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xaf])?;
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0x20, 0x00])?;
|
||||
|
||||
// fill the display
|
||||
for _ in 0..64 {
|
||||
let data: [u8; 17] = [
|
||||
0x40, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff,
|
||||
];
|
||||
i2c.write(SSD1306_ADDRESS, &data)?;
|
||||
}
|
||||
|
||||
loop {
|
||||
// we are using thread::sleep here to make sure the watchdog isn't triggered
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xa6])?;
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
i2c.write(SSD1306_ADDRESS, &[0, 0xa7])?;
|
||||
}
|
||||
}
|
57
examples/spi_loopback.rs
Normal file
57
examples/spi_loopback.rs
Normal file
@ -0,0 +1,57 @@
|
||||
//! SPI loopback test
|
||||
//!
|
||||
//! Folowing pins are used:
|
||||
//! SCLK GPIO6
|
||||
//! MISO GPIO2
|
||||
//! MOSI GPIO7
|
||||
//! CS GPIO10
|
||||
//!
|
||||
//! Depending on your target and the board you are using you have to change the pins.
|
||||
//!
|
||||
//! This example transfers data via SPI.
|
||||
//! Connect MISO and MOSI pins to see the outgoing data is read as incoming data.
|
||||
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use embedded_hal::spi::blocking::Transfer;
|
||||
use esp_idf_hal::peripherals::Peripherals;
|
||||
use esp_idf_hal::prelude::*;
|
||||
use esp_idf_hal::spi;
|
||||
use log::*;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
esp_idf_sys::link_patches();
|
||||
esp_idf_svc::log::EspLogger::initialize_default();
|
||||
|
||||
let peripherals = Peripherals::take().unwrap();
|
||||
let spi = peripherals.spi2;
|
||||
|
||||
let sclk = peripherals.pins.gpio6;
|
||||
let miso = peripherals.pins.gpio2;
|
||||
let mosi = peripherals.pins.gpio7;
|
||||
let cs = peripherals.pins.gpio10;
|
||||
|
||||
info!("Starting SPI loopback test");
|
||||
let config = <spi::config::Config as Default>::default().baudrate(26.MHz().into());
|
||||
let mut spi = spi::Master::<spi::SPI2, _, _, _, _>::new(
|
||||
spi,
|
||||
spi::Pins {
|
||||
sclk,
|
||||
sdo: miso,
|
||||
sdi: Some(mosi),
|
||||
cs: Some(cs),
|
||||
},
|
||||
config,
|
||||
)?;
|
||||
|
||||
let mut read = [0u8; 4];
|
||||
let write = [0xde, 0xad, 0xbe, 0xef];
|
||||
|
||||
loop {
|
||||
// we are using thread::sleep here to make sure the watchdog isn't triggered
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
spi.transfer(&mut read, &write)?;
|
||||
info!("Wrote {:x?}, read {:x?}", write, read);
|
||||
}
|
||||
}
|
52
examples/uart_loopback.rs
Normal file
52
examples/uart_loopback.rs
Normal file
@ -0,0 +1,52 @@
|
||||
//! UART loopback test
|
||||
//!
|
||||
//! Folowing pins are used:
|
||||
//! TX GPIO5
|
||||
//! RX GPIO6
|
||||
//!
|
||||
//! 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 std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use embedded_hal::serial::nb::{Read, Write};
|
||||
use esp_idf_hal::peripherals::Peripherals;
|
||||
use esp_idf_hal::prelude::*;
|
||||
use esp_idf_hal::serial;
|
||||
use log::*;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
esp_idf_sys::link_patches();
|
||||
esp_idf_svc::log::EspLogger::initialize_default();
|
||||
|
||||
let peripherals = Peripherals::take().unwrap();
|
||||
let tx = peripherals.pins.gpio5;
|
||||
let rx = peripherals.pins.gpio6;
|
||||
|
||||
info!("Starting UART loopback test");
|
||||
let config = serial::config::Config::default().baudrate(Hertz(115_200));
|
||||
let mut serial: serial::Serial<serial::UART1, _, _> = serial::Serial::new(
|
||||
peripherals.uart1,
|
||||
serial::Pins {
|
||||
tx,
|
||||
rx,
|
||||
cts: None,
|
||||
rts: None,
|
||||
},
|
||||
config,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
loop {
|
||||
// we are using thread::sleep here to make sure the watchdog isn't triggered
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
nb::block!(serial.write(0xaa))?;
|
||||
|
||||
// note: this will block - if you don't connect RX and TX you will see the watchdog kick in
|
||||
let byte = nb::block!(serial.read())?;
|
||||
info!("Written 0xaa, read 0x{:02x}", byte);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user