This guide shows how to write event-driven embedded system code.
This guide is the third in a collection of related guides:
- Building your first embedded image
- Retargeting output to UART
- Creating an event-driven embedded image (this guide)
- Changing Exception level and Security state in an embedded image
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:
- Configures the physical timer within the CPU to generate an exception in a few seconds.
- Once running, the code waits until the timer interrupt occurs.
- When the exception occurs, it is dealt with by the exception handler, and a suitable message is sent to the UART interface.