GPIO Essentials
~10 min slides
GPIO — General Purpose Input/Output. The most basic peripheral: making pins go high or low, and reading whether they're high or low.
Configurations
Before you can use a GPIO pin, you need to decide how it will behave.
Direction
- Output — you drive the pin (e.g., turn on an LED)
- Input — you read the pin (e.g., detect a button press)
Output Modes
- Push-pull (default) — actively drives the pin high and low
- Open-drain — actively drives low, but floats when "high" (needs external pull-up)
Pull Resistors (for inputs)
- Pull-up — pin reads high when nothing is connected; button pulls it low
- Pull-down — pin reads low when nothing is connected; button pulls it high
- Floating — no pull resistor; pin state is undefined when nothing is connected
Drive Strength (for outputs)
How much current the pin can source or sink. Higher drive strength = brighter LED, but more power consumption.
Controls
Output Controls
| Method | What It Does |
|---|---|
set_high() | Drive the pin high |
set_low() | Drive the pin low |
toggle() | Flip the pin state |
set_level(Level) | Set to a specific level |
is_set_high() | Check what you last set |
Input Controls
| Method | What It Does |
|---|---|
is_high() → bool | Is the pin high? |
is_low() → bool | Is the pin low? |
get_level() → Level | Get the level as an enum |
`is_high()` returns a `bool` — simple, but you lose information. `get_level()` returns a `Level` enum that you can `match` on. Both work; the enum version is more expressive and more Rusty.
uFerris GPIO Map
Check your pinout card for the exact pin assignments on the uFerris Megalops board:
| Function | Pin | Notes |
|---|---|---|
| Onboard LED | GPIO? | Active high/low? |
| User Button | GPIO? | Needs pull-up? |
| I2C SDA | GPIO? | Used in Part 3 |
| I2C SCL | GPIO? | Used in Part 3 |
The exact pin numbers depend on your uFerris board revision. Always check the pinout card provided in your workshop kit.
Mapping to docs.rs
Here's how GPIO maps to the Create → Configure → Control pattern:
| Step | What to Look For | Where on docs.rs |
|---|---|---|
| Create | Output::new(pin, level, config) | Struct page → impl Output |
| Configure | OutputConfig, InputConfig, Pull, DriveStrength | Associated types / linked structs |
| Control | set_high(), toggle(), get_level() | Trait impls + inherent methods |
Now let's put this into practice.