Overview

This guide shows how to write event-driven embedded system code.

This guide is the third in a collection of related guides:

Embedded systems typically monitor inputs waiting for an event, which then triggers a response by the system. You need to write code that listens for these events and acts on them. For example, an embedded system in a thermostat might monitor room temperature until it drops below a specified threshold. When the threshold is reached, the embedded system turns on the heating system.

To add meaningful functionality to an embedded system, you must enable asynchronous exceptions: IRQs, FIQs, and SErrors. This guide does not explore all the relevant architectural features, but a guide and online course are available for readers who are not familiar with them. Asynchronous exceptions are taken when the CPU needs to handle something that is external to the current flow of execution. For example, if a user flips a power switch, the processor must stop what it is doing, and branch to a handler that ensures that the shutdown is done correctly.

By the end of this guide, you will have written an event-driven program that:

  1. Configures the physical timer within the CPU to generate an exception in a few seconds.
  2. Once running, the code waits until the timer interrupt occurs.
  3. When the exception occurs, it is dealt with by the exception handler, and a suitable message is sent to the UART interface.
Next