Exercise A: Interrupt-Driven Button

~20 min

Goal

Configure the existing GPIO input button example from Part 2 to use interrupts instead of polling. Check the esp-hal abstractions and code examples to see if you can find any examples that you can adapt.

Steps

1. Create a New Project

esp-generate --chip esp32c3 -o unstable-hal -o vscode -o esp-backtrace -o log --headless gpio_interrupt
cd gpio_interrupt

2. Find Interrupt Examples

Search the GPIO Input documentation or the esp-hal examples directory for interrupt examples.

Look for how interrupts are set up — recall the three components from the slides:

  1. Interrupt Setup — configuring what event to listen for and enabling the interrupt
  2. Interrupt Service Routine — the code that reacts to the interrupt event
  3. Global Shared Data — any data shared between the main thread and the handler

3. Apply the Pattern

Convert your polling code to use interrupts:

  • Configure the interrupt (what event do you want to listen to?)
  • Enable the interrupt (allow events to go through)
  • Set up global shared data (how will the handler communicate with the main loop?)

4. Build and Flash

cargo build --release
espflash flash target/riscv32imc-unknown-none-elf/release/interrupt-button --monitor

Press the button. The LED should toggle.

5. Explore Trigger Configurations

Look up the Event enum in esp-hal's GPIO module. What variants are available?

What to Notice

  • The interrupt setup happens in the Configure stage
  • You must always call clear_interrupt() in the handler
  • Compare: what can the main loop do now that it couldn't when polling?