Tips and Tricks – An API for Interrupts

Interrupts play a critical role in a real-time embedded system but rarely is interrupt control ever associated with an application programming interface (API).  Assembly language code or compiler intrinsics are usually sprinkled throughout the code making the use of interrupt functions hap hazard and difficult to understand.this post will explore what an interrupt API should look like.

There are six different functions that can be defined to handle the majority of interrupt behavior on any microcontroller.  The summary of those functions can be seen in below.

Isr_Enable(IsrType)

Isr_Disable(IsrType)

Isr_GlobalEnable()

Isr_GlobalDisable()

Isr_CriticalSectionStart()

Isr_CriticalSectionEnd()

The first two functions are used to enable and disable an individual interrupt.  For example, on an ARM Coretex-M0+ the IsrType would allow for control of up to 32 different interrupts within the nested vector interrupt controller (NVIC).  These functions are separated out into two different functions in order to improve performance by preventing any branching code that would require the instruction pipeline to be flushed.

The global functions are used to disable and enable interrupts at the global or system level.  These functions would normally be used during initialization.  This is where assembly level instructions or compiler instrinsics might be used to control the CPU at its lowest levels.

The final two functions for controlling interrupts are designed to start and end a critical section of code.  When an atomic operation is needed, developers will usually disable interrupts prior to the critical section and then enable them afterwards.  The problem with this is that it is assumed that interrupts were enabled in the first place!  These last two functions keep track of whether interrupts were enabled and disabled and restore the state of the interrupts after the critical section.

The usage of these six simple API functions can greatly improve the readability and clarity of embedded software.  A downloadable template of the API can be downloaded HERE or by clicking the download image at the end of this article.  These are simple examples of a small subset of APIs that should be used when developing an embedded system.  For additional APIs consider the API Standard for MCU’s located HERE. Templates associated with the API Standard will be available shortly in the shop.

Download the ISR API:

Download Link

Share >

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.