10 Pain-Free Steps to Configuring an Interrupt Controller
Setting up interrupts properly on a microcontroller isn’t easy. Thankfully, there are steps that can be followed to alleviate these painful setup sessions and allow a developer to configure interrupts on the first try.
June 17, 2015
Setting up interrupts properly on a microcontroller isn't easy. The number of times an engineer has set up interrupts in his or her career doesn't seem to make a difference. Configuring interrupts always has some hidden "gotcha!" that leaves developers scratching their heads and seeking answers in an obscure data sheet or online code snippet. Thankfully, there are steps that can be followed to alleviate these painful setup sessions and allow a developer to configure interrupts on the first try.
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.
Step #1 - Configure the GPIO Pin
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.
Figure 1 – Configuring the GPIO Pin
Step #2 - Disable Interrupts
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.
MORE FROM DESIGN NEWS: Capturing the Essentials of Low-Power Design
Step #3 - Clear Interrupt Flags
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.
Step #4 - Connect Pin to Interrupt Line
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.
Figure 2 – Connecting GPIO to the Interrupt Controller
Step #5 - Set Trigger Polarity
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.
Figure 3 – Setting the falling edge trigger
Steps 6-10: Next page
About the Author
You May Also Like