Configuring Interrupt Controller
Configuring Interrupt Controller
To follow along these steps, feel free to download the code example for STMicroelectronics'
STM32 Nucleo-L053R8 for Keil ARM-MDK located here. Scroll to the bottom of the page under
Course Resources and download the Course Source Code. The exercise 4 button.c module has
the associated C code.
External interrupts that trigger on a GPIO pin are always the worst interrupts to set up. The only
difference between an external interrupt and an internal interrupt is the need to set up that pesky
GPIO. Configuring the GPIO has a number of steps. First, enable the GPIO clock. Second,
configure the GPIO as an input. Depending on the hardware, this may also require configuring the
internal pull-up resistors on the GPIO peripheral. An example of how this can be done on a
STM32 Nucleo board can be seen in Figure 1.
Once the GPIO pin is configured it is time to start focusing on the actual interrupt configuration.
Before doing anything, a developer should first disable all interrupts. This ensures that during the
setup process a partially configured interrupt doesn't accidentally fire and throw the system into a
chaotic and unknown state.
http://www.designnews.com/author.asp?section_id=1386&doc_id=277869&print=yes 19-06-2015
Design News - Blog - 10 Pain-Free Steps to Configuring an Interrupt Controller Page 2 of 4
With interrupts now disabled, the developer no longer has to worry about the setup process being
interrupted. However, there could have been interrupts pending prior to the setup process due to
start-up state of the system. Clearing out the interrupt flags makes certain that once the interrupt
controller is configured and enabled, the system won't immediately jump to an old and expired
interrupt request.
The GPIO pin is configured as an input and ready to go but at the moment isn't internally
connected to anything. In order to trigger an interrupt, a developer will need to connect that GPIO
pin to the interrupt controller. Each microcontroller does this in a slightly different manner. For an
ARM microcontroller, this is done using the system configuration peripherals EXTICFG registers.
This requires an additional step of turning on the clock for the system configuration peripheral.
Figure 4 shows an example of how this can be done for the push-button located on GPIO C13 of
the STM32 Nucleo board.
The interrupt controller is now connected to the GPIO pin, but the controller doesn't know what
should actually trigger the interrupt. Modern microcontrollers have many different options.
Interrupts can be level triggered and edge triggered such as rising or falling. The trigger setting will
be highly dependent upon the application. For the STM32 Nucleo board, the GPIO has a pull-up
that keeps the input at logic 1 unless the button is pressed. The interrupt controller can be set up
to trigger on both rising and falling edges. Figure 3 shows how the rising edge trigger is disabled
and the falling edge trigger is enabled.
Modern interrupt controllers are not simple, straightforward peripherals. Interrupt controllers
provide a wide range of features and capabilities that developers can take advantage of and tune
for their own specific applications. An interrupt controller can have as many as 256 different
interrupts! In the event that two or more interrupts fire at the same time, the controller needs to
know which interrupt should be handled first. Setting the interrupt priority can be a simple exercise
of just setting the priority bits in the interrupt controller. An example that uses the ARM CMSIS
specification can be seen in Figure 4.
http://www.designnews.com/author.asp?section_id=1386&doc_id=277869&print=yes 19-06-2015
Design News - Blog - 10 Pain-Free Steps to Configuring an Interrupt Controller Page 3 of 4
Enabling interrupts is usually a two-step process. The first step is to examine the interrupt
registers and unmask the interrupts that are going to be used by the system. Unmasking the
interrupt allows the interrupt controller to respond when that particular interrupt is fired. The
second step is then to enable the actual interrupt. Once again, enabling interrupts can vary from
microcontroller to microcontroller, so it is important to have the datasheet open and examine it
closely. Figure 5 shows an example of how GPIO C13 on the STM32 Nucleo board is enabled by
first unmasking the interrupt and then using CMSIS to enable the interrupt line that is associated
with GPIO C13.
MORE FROM DESIGN NEWS: MCUs Embrace New Demand for Ultra-Low Power Innovation
The interrupt controller is now configured! There is just one problem: When an interrupt occurs
there is no interrupt handler to respond to the interrupt. The next logical step is to create an
interrupt handler. There are various ways that this can be done, depending not only on the
architecture but also on the compiler and IDE. Specifying that a function is an interrupt often uses
#pragma or a similar type of compiler intrinsic. When developing on an ARM platform, a developer
simply needs to look at the list of interrupts and create a function with the matching pre-specified
handler. Figure 6 shows an example of how an interrupt handler would look for GPIO C13.
Most microcontrollers require that an interrupt flag be cleared manually by the developer in the
interrupt handler. In special cases, the interrupt flag is automatically cleared but the datasheet of
the microcontroller should be referenced to determine which interrupts behave this way. GPIO
interrupts usually fire as a block, and upon entering the interrupt a simple check needs to be
performed to determine which GPIO line caused the interrupt. The corresponding flag can then be
cleared. Figure 7 shows how this can be done.
http://www.designnews.com/author.asp?section_id=1386&doc_id=277869&print=yes 19-06-2015
Design News - Blog - 10 Pain-Free Steps to Configuring an Interrupt Controller Page 4 of 4
Finally, after completing all of these steps a developer can now test the code. It is highly
improbable that the firmware will run correctly on the first try, but having followed each of these
steps closely, there should only be a minor adjustment required before interrupts are up and
running correctly.
Design & Manufacturing Canada, the largest advanced design and manufacturing trade show
serving Canada, will take place in Toronto, June 16-18, 2015. Learn more here.
Copyright © 2015 UBM Canon, A UBM company, All rights reserved. Privacy Policy | Terms of Service
http://www.designnews.com/author.asp?section_id=1386&doc_id=277869&print=yes 19-06-2015