TruePERSPECTIVES_logo.png

What you need to know about debugging interrupts and exceptions on Cortex-M devices

Posted by Magnus Unemyr on Apr 28, 2015 11:20:34 AM

Any Cortex-M developer have likely struggled with getting interrupts to work, or tried to debug them. Getting it all hooked up can be rather tricky, from getting a correctly defined interrupt vector table linked on the right memory addresses, to enabling interrupts properly, and then processing them in an interrupt handler. It all involves advanced topics like linker configuration files, SFR register bit field configuration and manipulation, interrupt #pragmas, possibly inline assembly and more.

So how do you go about analyzing and debugging the behavior of your interrupts and exceptions on a Cortex-M device like STM32, Kinetis, LPC or EFM32? Interrupts and exceptions are by definition asynchronous to the execution flow of your application software and it can be very difficult to visualize interrupt behavior, and debug the same. So what is a Cortex-M developer to do? As it so happens, the Cortex-M core has brilliant hardware support for visualizing and debugging interrupt behavior. This is how to use it.

swv_intr1

As part of the ARM Coresight debug architecture for Cortex-M, ARM Ltd. added support for non-intrusive real-time event- and data tracing through a debug technology called Serial Wire Viewer (SWV). Related technologies here are Serial Wire Debug (SWD), the Serial Wire Output pin (SWO) and to some extent the Instrumentation Trace Macrocell (ITM) as well. In short, debugger probes like SEGGER J-Link and ST-LINK can stream additional debug data to the PC debugger using the JTAG cable. Please note this has nothing to do with ETM/ETB instruction tracing that is a completely different technology.

By using SWV, Cortex-M devices like STM32, Kinetis, LPC or EFM32 gets hardware support for real-time event- and data tracing, including the possibility to “listen” to the interrupt and exception behavior as the application runs at full execution speed. Other highly useful capabilities of SWV include real-time data watch and graphical chart plotting, printf() redirection and software tracing, performance profiling, execution time measurement and more.

As this blog post focus on analyzing and debugging interrupt and exception behavior on Cortex-M devices, let’s look into this in some more detail. To analyze exception and interrupt behavior, the debugger must use the SWD debug mode, and have SWV enabled. Furthermore, the debugger must be configured to enable event tracing for interrupts and exceptions. If you want to use real-time graphical chart plots as well for system analysis visualization, SWV timestamps must be enabled too.

With that basic configuration out of the way, the debugger can be used for real-time system analysis, for example listing all interrupt and exception events that occur as the application is executing at full speed, including exception entry, exception return and exception exit.  This enables developers to work out if an interrupt actually fires, how often, and which interrupts intermix over time. It also enables developers to work out any interrupt nesting situations, as well as timing matters. With more capable debuggers like Atollic TrueSTUDIO you can also see what peripheral module generated the interrupt, and what interrupt handler is connected to the interrupt. A double click on the interrupt handler automatically brings you to the right code line in the source code editor as well.

swv_intr2

A graphical chart plot of the interrupt behavior provides an easy-to-understand view of how many interrupts fire over time. This is particularly useful if interrupts are expected to fire in certain patterns.

Good debuggers also provide you with interrupt statistics information for each interrupt type; listing interesting information such as:

  • What interrupt handler function is connected to the interrupt?
  • What % of all fired interrupts is this one?
  • Number of times this interrupt has fired?
  • What % of interrupt handling time was spent handling this interrupt?
  • Total time of all occurrences of this interrupt?
  • Average time of all occurrences of this interrupt?
  • Fastest and slowest time of handling this interrupt?
  • When was the first time this interrupt fired?
  • Etc

A good debugger, like Atollic TrueSTUDIO, also allows you to double click on any interrupt and jump you directly to the source code lines in the debugger that implements the corresponding interrupt handler.

swv_intr3

Interrupt and exception analysis and debugging using SWV works on Cortex-M devices like STM32, Kinetis, EFM32 or LPC. The Atollic TrueSTUDIO C/C++ IDE for ARM supports SWV using the SEGGER J-Link, SEGGER J-Trace and ST-LINK debug probes.

Read the superset blog post that covers the entire topic of Cortex-M debugging:

How to use the most powerful debug techniques on ARM Cortex-M devices

You can also get a good overview of Serial Wire Viewer real-time tracing on Cortex-M cores by reading this white paper on the subject:

Read our SWV event and data tracing whitepaper!

More information on ARM Cortex development and debugging is available in this whitepaper:

Read our ARM development whitepaper!

 

Topics: ARM Cortex, Debugging, Atollic TrueSTUDIO, SEGGER J-Link, ST-LINK