Simvik

Drive a GPIO pin

Set or clear one pin in a port's output data register. An easy embedded C problem (Registers and memory-mapped I/O), run and graded in the browser.

easy · Registers and memory-mapped I/O · gpio · read-modify-write

The port's registers are mapped into memory as the gpio_t struct shown under Provided. Bit n of ODR drives pin n.

Make pin pin (0 to 15) high when level is true and low when it is false. No other pin and no other register may change.

Starter code

void gpio_write(gpio_t *port, uint8_t pin, bool level) {
  // Update only bit `pin` of port->ODR.
}

Example tests

Drive PA5 high

GPIOA->ODR = 0x00000000;
gpio_write(GPIOA, 5, true);
printf("0x%08X\n", GPIOA->ODR);

Expected output:

0x00000020

Drive PA0 low

GPIOA->ODR = 0x0000FFFF;
gpio_write(GPIOA, 0, false);
printf("0x%08X\n", GPIOA->ODR);

Expected output:

0x0000FFFE

Learn it first

  • Registers, volatile and const · How C talks to hardware: memory-mapped registers, what volatile and const promise, register structs and read-modify-write.

More Registers and memory-mapped I/O problems