Embedded development is a lot more complicated than PC development; the main reason being the low abstraction level and the tight relation to the hardware implementation. One particular pain point for new or inexperienced embedded developers is interrupt handling.
Interrupts are events generated by the hardware. Common causes for interrupts being triggered may be timers that expire, communications interfaces that receive data bytes, DMA transfers being completed, and so on. The question then becomes how to develop interrupt handlers using the GNU/GCC compiler for popular Cortex-M devices like STM32, Kinetis, LPC or EFM32? Read our free eBook to learn more!
First of all, how do interrupt handlers start to execute? This is all handled by the hardware. Once a hardware interrupt fires, the CPU core looks into an interrupt vector table (at a well-defined location in memory) to find out at what address the corresponding interrupt handler function is located for this particular interrupt. The CPU then jumps to that address and start to execute the interrupt handler.
The developer thus needs to write an interrupt handler function in C that resolves the raised hardware situation (for example, by receiving and storing a byte that arrived on a UART channel). The next step is to develop an interrupt vector table, and make sure it is linked to the address the hardware requires. Finally, add the address of the interrupt handler function into the correct entry in the interrupt vector table to hook it all up.
Easy, right? But wait, there is more. Interrupt handlers will not start to execute unless you enable the interrupt source, and there are a number of common problems you need to be aware of as well. For example data integrity, if both your application code and the interrupt handler try to update the same global variable at the same time. This can lead to corrupted data values unless the reads and writes to the variable are made atomic. Aggressive compiler optimization can also introduce unexpected problems.
Special compiler directives and mutexes or critical regions can be used to prevent these problems, but there are many possibilities to introduce software errors that are very hard to debug here.
Read our free eBook on interrupt development to learn more on interrupts using the GNU/GCC compiler and Cortex-M devices!