```rust
loop {
if button.is_low() {
// react to button press
}
// CPU is busy-waiting here
}
```
The CPU constantly checks the state of a peripheral in a loop.
- Simple to implement
- Wastes CPU cycles
- Processor is busy-waiting even when nothing happens
---
## Interrupts
```rust
// 1. Global Shared Data
static SHARED: Mutex>> =
Mutex::new(RefCell::new(None));
fn main() {
// 2. Interrupt Setup
button.listen(Event::FallingEdge);
critical_section::with(|cs| {
SHARED.borrow_ref_mut(cs)
.replace(button);
});
}
// 3. Interrupt Service Routine
#[handler]
fn gpio_handler() {
critical_section::with(|cs| {
// handle event, clear interrupt
});
}
```
Three components:
1. **Global Shared Data** — data shared between main thread and ISR
2. **Interrupt Setup** — configure what event to listen for, enable it, move data to shared state
3. **ISR** — code that reacts to the interrupt event
---
## Setup Happens in the Configure Stage
Setup entails three steps:
1. **Configuring the interrupt** — What do we want to listen to?
- Edge events (rising, falling, any)
- Level events (high, low)
2. **Enabling the interrupt** — Allowing the interrupt events to go through
- Peripheral-level enable
- Interrupt controller enable
3. **Configuring global shared data** — Setting up shared state
- `Mutex