Simvik

Clear a write-1-to-clear flag

Acknowledge one interrupt flag without wiping the others. A medium embedded C problem (Registers and memory-mapped I/O), run and graded in the browser.

medium · Registers and memory-mapped I/O · status · flags

Many status registers are write-1-to-clear: writing 1 to a bit clears that flag, and writing 0 leaves a flag alone.

Clear flag bit in the register *sr, and only that flag. The test inspects the value your function wrote to the register.

Starter code

void clear_flag(volatile uint32_t *sr, uint8_t bit) {
  // Writing 1 clears a flag, writing 0 does nothing.
  *sr &= ~(1u << bit);
}

Example tests

Acknowledge flag 1 of four pending

uint32_t sr = 0x0000000F;
clear_flag(&sr, 1);
printf("wrote 0x%08X\n", sr);

Expected output:

wrote 0x00000002

Acknowledge the top flag

uint32_t sr = 0x80000001;
clear_flag(&sr, 31);
printf("wrote 0x%08X\n", sr);

Expected output:

wrote 0x80000000

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