Driver: drive an LED through GPIO registers
Make a pin an output without disturbing its neighbours, then set, clear and toggle it atomically. A medium embedded C problem (GPIO), run and graded in the…
medium · GPIO · driver · registers · gpio · bsrr
Write a small GPIO driver against the register map under Provided. The port is simulated like an STM32F4's, and every change of an output pin shows up in the output as gpioa: pin 5 high.
gpio_set_output(g, pin)makespin(0 to 15) a push-pull output. Each pin has two bits inMODER; change only that pin's two bits. GPIOA comes out of reset with PA13 and PA14 on their debug (SWD) function, and the simulator reports it if you take them off it.gpio_write(g, pin, level)drives the pin high (levelnon-zero) or low, with a single write toBSRR, so an interrupt changing another pin of the port in between cannot be undone.gpio_toggle(g, pin)inverts the pin's output, again without being able to undo an interrupt's change to another pin.
One hidden test plays such an interrupt.
Starter code
void gpio_set_output(gpio_t *g, int pin) {
}
void gpio_write(gpio_t *g, int pin, int level) {
}
void gpio_toggle(gpio_t *g, int pin) {
}Example tests
Blink PA5
gpio_set_output(GPIOA, 5);
gpio_write(GPIOA, 5, 1);
gpio_toggle(GPIOA, 5);
gpio_toggle(GPIOA, 5);
gpio_write(GPIOA, 5, 0);Expected output:
gpioa: pin 5 high
gpioa: pin 5 low
gpioa: pin 5 high
gpioa: pin 5 lowLearn it first
- GPIO · Pin modes, push-pull versus open-drain, pulls, edge interrupts and debouncing.
More GPIO problems
- Update several pins at once · Write new levels to the pins selected by a mask and leave every other pin alone.
- Toggle an LED · Flip one pin with a single XOR, whatever its current level.
- Read an active-low button · A button wired to ground reads 0 when pressed. Report it as pressed.
- Configure pin modes · Set the direction and pull-up bits for one pin as input, input with pull-up or output.
- Driver: a debounced button on PC13 · Configure an input with its pull-up, then report each press once, ignoring contact bounce.
- Fix: the debugger cannot connect any more · Configuring one pin overwrites the mode of every other pin of the port, the debug pins included.
- Smart doorbell: one ring per press · Debounce a bouncing button sampled every millisecond, so one press rings once.