[En-Nut-Discussion] Cortex M exception handler implementation

Ole Reinhardt ole.reinhardt at embedded-it.de
Sat Jul 27 00:49:04 CEST 2013

Hi all,

Exception handler debug output:

I implemented a meaningfull debug output for the Cortex M exception
handler, which allows you to find out the location of bugs like a buffer
overflow, overwritten stack, div by zero, and any other crashes.

When enabled, the Exception handler will call a registry dump function,
that prints out the stacked register values, including program counter,
link register etc. Further it prints out the fault handler status
register, which show several informations about the exception reason.

This register dump function uses special "Debug Macros", which
implements a very simple polling UART output without any driver
overhead. To work correctly the configured UART has to be configured
correctly before the exeption is triggered, as the debug macro just
sends out characters over the UART but does not configure the
pin-muxing, baudrate etc.

Currently this is implemented for STM32 and NXP LPC

@Uwe: I just tested on LPC platform. Could you please test the code on

To use these features, you have to select the UART, which shall be used
for this debug output and enable the Debug macro flag.

These options are located in the configurator at:

Architecture->CM3->CPU-Family->Common devices->Low-Level Debug macros
for use in exception handlers

The register dump will look like this:

[Bus Fault handler - all numbers in hex]

R0        = 0x0000000b
R1        = 0x00008119
R2        = 0x10001090
R3        = 0x00001d9c
R12       = 0x1000023c
LR [R14]  = 0x00000b4d
PC [R15]  = 0x00000b4c
PSR       = 0x61000200
BFAR      = 0xabcdefff
CFSR      = 0x00008200
HFSR      = 0x00000000
DFSR      = 0x00000008
AFSR      = 0x00000000
SCB_SHCSR = 0x00070002

In this example I tried to access a memory address, which is located
outside any valid address range (0xabcdefff). This memory access
resulted in a bus fault. You can see now, that the instruction, that
caused the exception is located at 0x00000b4c (program counter value)
and the invalid address is 0xabcdefff (BFAR register).

You can then take a look into your application .map file, to see at
which function the error occured. You can even disassemble the code
(arm-none-eabi-objdump -d test.elf) to find out the exact place.

I hope this may help you to debug hard to find errors in your code.

Please test it and report any problems or ideas for enhancement.

Best regards,


kernel concepts GmbH            Tel: +49-271-771091-14
Sieghuetter Hauptweg 48         Mob: +49-177-7420433
D-57072 Siegen

More information about the En-Nut-Discussion mailing list