Crate esp_hal

source ·
Expand description

§Bare-metal (no_std) HAL for all Espressif ESP32 devices.

§Overview

The HAL implements both blocking and async APIs for many peripherals. Where applicable, driver implement the embedded-hal and embedded-hal-async traits.

This documentation is built for the ESP32 . Please ensure you are reading the correct documentation for your target device.

§Choosing a Device

Depending on your target device, you need to enable the chip feature for that device. You may also need to do this on ancillary esp-hal crates.

§Examples

We have a plethora of examples in the esp-hal repository. We use an xtask to automate the building, running, and testing of code and examples within esp-hal.

Invoke the following command in the root of the esp-hal repository to get started:

cargo xtask help

§Creating a Project

We have a book that explains the full esp-rs ecosystem and how to get started, it’s advisable to give that a read before proceeding. We also have a training that covers some common scenarios with examples.

We have developed a project generation tool, esp-generate, which we recommend when starting new projects. It can be installed and run, e.g. for the ESP32-C6, as follows:

cargo install esp-generate
esp-generate --chip=esp32c6 your-project

§Blinky

Some minimal code to blink an LED looks like this:

#![no_std]
#![no_main]

// You'll need a panic handler e.g. `use esp_backtrace as _;`
use esp_hal::{
    delay::Delay,
    gpio::{Io, Level, Output},
    prelude::*,
};

#[entry]
fn main() -> ! {
    let peripherals = esp_hal::init({
        let mut config = esp_hal::Config::default();
        // Configure the CPU to run at the maximum frequency.
        config.cpu_clock = CpuClock::max();
        config
    });

    // Set GPIO0 as an output, and set its state high initially.
    let mut led = Output::new(peripherals.GPIO0, Level::High);

    let delay = Delay::new();

    loop {
        led.toggle();
        delay.delay_millis(1000);
    }
}

§Additional configuration

We’ve exposed some configuration options that don’t fit into cargo features. These can be set via environment variables, or via cargo’s [env] section inside .cargo/config.toml. Below is a table of tunable parameters for this crate:

NameDescriptionDefault value
ESP_HAL_PLACE_SPI_DRIVER_IN_RAMPlaces the SPI driver in RAM for better performancefalse
ESP_HAL_SPI_ADDRESS_WORKAROUND(ESP32 only) Enables a workaround for the issue where SPI in half-duplex mode incorrectly transmits the address on a single line if the data buffer is empty.true
ESP_HAL_PLACE_SWITCH_TABLES_IN_RAMPlaces switch-tables, some lookup tables and constants related to interrupt handling into RAM - resulting in better performance but slightly more RAM consumption.true
ESP_HAL_PLACE_ANON_IN_RAMPlaces anonymous symbols into RAM - resulting in better performance at the cost of significant more RAM consumption. Best to be combined with place-switch-tables-in-ram.false

It’s important to note that due to a bug in cargo, any modifications to the environment, local or otherwise will only get picked up on a full clean build of the project.

§Peripheral Pattern

Drivers take pins and peripherals as peripheral::Peripheral in most circumstances. This means you can pass the pin/peripheral or a mutable reference to the pin/peripheral.

The latter can be used to regain access to the pin when the driver gets dropped. Then it’s possible to reuse the pin/peripheral for a different purpose.

§Don’t use core::mem::forget

You should never use core::mem::forget on any type defined in the HAL. Some types heavily rely on their Drop implementation to not leave the hardware in undefined state and causing UB.

You might want to consider using #[deny(clippy::mem_forget) in your project.

§Feature Flags

  • debug — Enable debug features in the HAL (used for development).
  • log — Enable logging output using the log crate.

§RISC-V Exclusive Feature Flags

  • flip-link — Move the stack to start of RAM to get zero-cost stack overflow protection (ESP32-C6 and ESPS32-H2 only!).

§Trait Implementation Feature Flags

  • defmt — Implement defmt::Format on certain types.

§PSRAM Feature Flags

  • quad-psram — Use externally connected Quad PSRAM
  • octal-psram — Use externally connected Octal RAM

Re-exports§

Modules§

  • Advanced Encryption Standard (AES).
  • Analog Peripherals
  • CPU Clock Control
  • Configuration
  • Control CPU Cores (ESP32)
  • Debugger utilities
  • Delay
  • Direct Memory Access (DMA)
  • Reading of eFuses (ESP32)
  • General Purpose Input/Output (GPIO)
  • Inter-Integrated Circuit (I2C)
  • Inter-IC Sound (I2S)
  • Interrupt support
  • LED Controller (LEDC)
  • Macros used by the HAL.
  • Motor Control Pulse Width Modulator (MCPWM)
  • Pulse Counter (PCNT)
  • Exclusive peripheral access
  • Peripheral Instances
  • The esp-hal Prelude
  • PSRAM “virtual peripheral” driver (ESP32)
  • Hardware and Software Reset
  • Remote Control Peripheral (RMT)
  • Random Number Generator (RNG)
  • ESP ROM libraries
  • RSA (Rivest–Shamir–Adleman) accelerator.
  • Real-Time Control and Low-power Management (RTC_CNTL)
  • Secure Hash Algorithm (SHA) Accelerator
  • Serial Peripheral Interface (SPI)
  • System Control
  • Time
  • General-purpose Timers
  • Capacitive Touch Sensor
  • State of the CPU saved when entering exception or interrupt
  • Two-wire Automotive Interface (TWAI)
  • Universal Asynchronous Receiver/Transmitter (UART)

Macros§

Structs§

  • Driver initialized in async mode.
  • Driver initialized in blocking mode.
  • System configuration.

Enums§

  • Available CPU cores

Constants§

Traits§

Functions§

  • Initialize the system.

Attribute Macros§

  • Marks a function as the main function to be called on program start