Simvik

Atomic pin write

Change a pin with a single write to the bit set/reset register. A medium embedded C problem (Registers and memory-mapped I/O), run and graded in the browser.

medium · Registers and memory-mapped I/O · gpio · atomic

BSRR is a write-only register. Writing 1 to bit n (0 to 15) sets pin n; writing 1 to bit n + 16 resets it. Bits written as 0 do nothing.

Set or reset pin pin using exactly one write to BSRR. Do not read BSRR and do not touch ODR.

Starter code

void gpio_write_atomic(gpio_t *port, uint8_t pin, bool level) {
  // One write to port->BSRR: bit `pin` sets, bit `pin + 16` resets.
}

Example tests

Set PA3

GPIOA->BSRR = 0;
gpio_write_atomic(GPIOA, 3, true);
printf("0x%08X\n", GPIOA->BSRR);

Expected output:

0x00000008

Reset PA3

GPIOA->BSRR = 0;
gpio_write_atomic(GPIOA, 3, false);
printf("0x%08X\n", GPIOA->BSRR);

Expected output:

0x00080000

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