This blog is the second in a series on the new debug features in SOMNIUM DRT. The first one was about the live expression view.
Debugging is difficult, especially when unexpected errors cause the program to behave unpredictably, such as writing to an invalid area of memory or executing an illegal instruction. The ARM architecture includes an exception mechanism to trap such faults. This is a powerful feature but it can be difficult to understand the information available.
SOMNIUM DRT includes features to support debugging embedded systems. One of these is a Fault Diagnosis tool that presents the fault information in a human-readable format. I have written briefly about the Fault Diagnosis view in DRT before. This article goes into a bit more detail about how to make the best use of it.
SOMNIUM DRT is is a set of development tools for ARM Cortex-M based devices such as SMART devices from Atmel, Kinetis and LPC devices from NXP, and STM32 devices from STMicroelectronics. It is fully compatible with industry-standard tools such as the GNU toolchain and Eclipse IDE. DRT uses our patented techniques to produce highly optimized code by exploiting information about the embedded processor, the memory system and other peripherals to deliver improved performance, lower code size and reduced energy use.
ARM processors raise an exception when a fault occurs. If the exception handler triggers a breakpoint, then you can use this when debugging to find errors in the program. In a live system, the exception handler could attempt to correct for the cause of the error and then restart execution.
There are four types of faults that are detected by Cortex-M3 and Cortex-M4 processors. These trap errors such as illegal memory accesses or attempting to execute non-existent instructions.
In the case of the Cortex-M0 processor, only hard faults are available.
To make best use of the Fault Diagnosis view, you should make sure that your program is set up to catch any faults when they occur. There are two steps to this:
There are various ways to ensure a breakpoint occurs when a fault handler is triggered. Perhaps the simplest, for occasional use, is to add a breakpoint to the default exception handler code. This is defined in the file startup_ device.S
A more flexible approach is to define your own exception handler. For example:
void __attribute__((naked)) HardFault_Handler(void)
{
__asm__("bkpt 0");
while (1);
}
The names of the other fault handlers are: MemManage_Handler, BusFault_Handler, and UsageFault_Handler. Using this approach allows you to quickly see what type of error occurred. You can also use this as the basis for a more complete exception handler that attempts to continue execution after the fault.
The next step is to enable all the necessary fault handlers.
Note that only the hard-fault handler is enabled by default. Also, fault handling is not enabled by default for for unaligned memory accesses and divide by zero errors, Therefore you should make sure that the exception handlers and fault types you need are enabled. To enable exception handlers for all the above, you can add the following code where you do most of your initialization (for example, at the start of the main() function):
// Enable all exception handlers
SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk
| SCB_SHCSR_BUSFAULTENA_Msk
| SCB_SHCSR_USGFAULTENA_Msk;
// Enable divide by zero and unaligned access faults
SCB->CCR |= SCB_CCR_DIV_0_TRP_Msk
| SCB_CCR_UNALIGN_TRP_Msk;
The hardware records information about the cause of the fault in various system registers. Different levels of detail are available depending on the architecture and the type of the fault.
The DRT Fault Diagnosis tool will automatically extract this information, decode it and display it in a readable form, relating it to the program source code (when possible). This is much easier than looking at register values, decoding bit-fields and referring to the ARM architecture manual.
The Fault Diagnosis view will display the source of the error (including the file name and line number, when available). You can display the source or the disassembly view of the code that caused the error, and see the values of the main registers when the error occurred.
For more information on the information displayed, see the DRT Reference Manual.
A free trial of DRT is available. DRT is able to automatically import projects from other development tools, making it simple to migrate your projects to DRT so you can quickly see the benefits for yourself. Download your free trial today and try out these powerful debug features for yourself.