Simvik

Read a GPIO pin

Report the level on one input pin. 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

Bit n of the input data register IDR reflects the voltage on pin n.

Return true if pin pin (0 to 15) is high.

Starter code

bool gpio_read(const gpio_t *port, uint8_t pin) {
  // Return the state of bit `pin` of the input data register.
  return false;
}

Example tests

Pin is high

GPIOA->IDR = 0x00002000;
printf("%d\n", gpio_read(GPIOA, 13));

Expected output:

1

Pin is low

GPIOA->IDR = 0x00002000;
printf("%d\n", gpio_read(GPIOA, 12));

Expected output:

0

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