ESP32 Standard Library Embedded Rust: GPIO Interrupts

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • esp-idf-template

    A "Hello, world!" template of a Rust binary crate for the ESP-IDF framework. (by esp-rs)

  • use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering; use esp_idf_hal::gpio::*; use esp_idf_hal::peripherals::Peripherals; use esp_idf_sys::{self as _}; static FLAG: AtomicBool = AtomicBool::new(false); fn gpio_int_callback() { // Assert FLAG indicating a press button happened FLAG.store(true, Ordering::Relaxed); } fn main() -> ! { // 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_sys::link_patches(); // Take Peripherals let dp = Peripherals::take().unwrap(); // Configure button pin as input let mut button = PinDriver::input(dp.pins.gpio0).unwrap(); // Configure button pin with internal pull up button.set_pull(Pull::Up).unwrap(); // Configure button pin to detect interrupts on a positive edge button.set_interrupt_type(InterruptType::PosEdge).unwrap(); // Attach the ISR to the button interrupt unsafe { button.subscribe(gpio_int_callback).unwrap() } // Enable interrupts button.enable_interrupt().unwrap(); // Set up a variable that keeps track of press button count let mut count = 0_u32; loop { // Check if global flag is asserted if FLAG.load(Ordering::Relaxed) { // Reset global flag FLAG.store(false, Ordering::Relaxed); // Update Press count and print count = count.wrapping_add(1); println!("Press Count {}", count); } } }

  • std-training

    Embedded Rust on Espressif training material.

  • It's well established that interrupts are a tough concept to grasp for the embedded beginner. Add to that when doing it in Rust the complexity of dealing with mutable static variables. This is because working with shared variables and interrupts is inherently unsafe if proper measures are not taken. When looking at how to do interrupts using the esp-idf-hal I first resorted to the Embedded Rust on Espressif book. Interrupts are covered under the Advanced Workshop in section 4.3, and to be honest, I was taken aback a little at what could be an additional level of complexity for a beginner. Without too much detail, this is because the book resorts to using lower-level implementations. For those interested, by that, I mean FFI interfaces to FreeRTOS which I will be creating a separate post about later.

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts