Simvik

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) makes pin (0 to 15) a push-pull output. Each pin has two bits in MODER; 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 (level non-zero) or low, with a single write to BSRR, 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 low

Learn it first

  • GPIO · Pin modes, push-pull versus open-drain, pulls, edge interrupts and debouncing.

More GPIO problems