Simvik

Dispatch through a vector table

Call the handler registered for an interrupt number, with a safe fallback. An easy embedded C problem (Function pointers and state machines), run and graded…

easy · Function pointers and state machines · callbacks · interrupts

An interrupt vector table is an array of function pointers indexed by interrupt number.

Call the handler for irq from the table vectors, which has n entries. If irq is outside the table, or its entry is NULL, call default_handler() instead.

Starter code

void dispatch_irq(const isr_t *vectors, size_t n, uint8_t irq) {
  // Call vectors[irq], or default_handler() when there is no valid entry.
}

Example tests

Registered handlers

const isr_t v[3] = { isr_timer, NULL, isr_uart };
dispatch_irq(v, 3, 0);
dispatch_irq(v, 3, 2);
printf("\n");

Expected output:

timer uart

Empty slot

const isr_t v[3] = { isr_timer, NULL, isr_uart };
dispatch_irq(v, 3, 1);
printf("\n");

Expected output:

default

Learn it first

More Function pointers and state machines problems