[En-Nut-Discussion] IntDefaultHandler

Ole Reinhardt ole.reinhardt at embedded-it.de
Thu Jul 4 00:21:44 CEST 2013


Hi Achim,

> I tried single step, but I get into IntDefaultHandler just after return
> from a function Š.

Very likely the stack gets corrupted in this function, which might lead
to a bad return address on the stack.

In general, the JTAG is very helpfull and it is quite easy to find the
address of the instruction that triggered the exception.

Tracing is nice, but I'm sure your JTAG debugger will do it's job.

A few weeks ago, I also had to debug a CM3 board, where I did not have
JTAG access. I wrote a little exception handler, that dumps the register
set in case of an exception. You only need a debug UART connected (debug
UART driver in polling mode, not interrupt driven) to stdout.

Here is the patch (to arch/cm3/cmsis/cortex_init.c):

Index: cmsis/cortex_init.c
===================================================================
--- cmsis/cortex_init.c	(Revision 5217)
+++ cmsis/cortex_init.c	(Arbeitskopie)
@@ -151,10 +151,54 @@
 extern void * _pspstack_end;    /* Process stack end address */
 extern void * _stack_start;     /* Main stack start address */
 extern void * _stack_end;       /* Main stack end address */
+#include <stdio.h>
+#include <sys/thread.h>

+static void RegDump(uint32_t *arg)
+{
+	unsigned int stacked_r0;
+	unsigned int stacked_r1;
+	unsigned int stacked_r2;
+	unsigned int stacked_r3;
+	unsigned int stacked_r12;
+	unsigned int stacked_lr;
+	unsigned int stacked_pc;
+	unsigned int stacked_psr;
+
+	printf("-----------------------------------------------\n");
+	stacked_r0 = ((unsigned long) arg[0]);
+	stacked_r1 = ((unsigned long) arg[1]);
+	stacked_r2 = ((unsigned long) arg[2]);
+	stacked_r3 = ((unsigned long) arg[3]);
+
+	stacked_r12 = ((unsigned long) arg[4]);
+	stacked_lr  = ((unsigned long) arg[5]);
+	stacked_pc  = ((unsigned long) arg[6]);
+	stacked_psr = ((unsigned long) arg[7]);
+
+	printf ("\n\n[Hard fault handler - all numbers in hex]\n");
+	printf ("R0 =   %08x\n", stacked_r0);
+	printf ("R1 =   %08x\n", stacked_r1);
+	printf ("R2 =   %08x\n", stacked_r2);
+	printf ("R3 =   %08x\n", stacked_r3);
+	printf ("R12 =  %08x\n", stacked_r12);
+	printf ("LR [R14] = %08x  subroutine call return address\n", stacked_lr);
+	printf ("PC [R15] = %08x  program counter\n", stacked_pc);
+	printf ("PSR = %x\n", stacked_psr);
+	printf ("BFAR = %08lx\n", (*((volatile unsigned long *)(0xE000ED38))));
+	printf ("CFSR = %08lx\n", (*((volatile unsigned long *)(0xE000ED28))));
+	printf ("HFSR = %08lx\n", (*((volatile unsigned long *)(0xE000ED2C))));
+	printf ("DFSR = %08lx\n", (*((volatile unsigned long *)(0xE000ED30))));
+	printf ("AFSR = %08lx\n", (*((volatile unsigned long *)(0xE000ED3C))));
+	printf ("SCB_SHCSR = %08lx\n", SCB->SHCSR);
+	while(1);
+}
+
 /* Default interrupt handler */
 static void IntDefaultHandler(void *arg)
 {
+	printf("DefaultIrq\n");
+	RegDump(arg);
     for (;;);
 }

@@ -165,6 +209,8 @@
  */
 static void IntNmiHandler(void *arg)
 {
+	printf("NmiFault\n");
+	RegDump(arg);
     for (;;);
 }

@@ -173,6 +219,8 @@
  */
 static void IntHardfaultHandler(void *arg)
 {
+    printf("HardFault\n");
+	RegDump(arg);
     for (;;);
 }

@@ -181,6 +229,8 @@
  */
 static void IntMemfaultHandler(void *arg)
 {
+	printf("MemFault\n");
+	RegDump(arg);
     for (;;);
 }

@@ -189,6 +239,8 @@
  */
 static void IntBusfaultHandler(void *arg)
 {
+	printf("BusFault\n");
+	RegDump(arg);
     for (;;);
 }

@@ -197,6 +249,8 @@
  */
 static void IntUsagefaultHandler(void *arg)
 {
+	printf("UsageFault\n");
+	RegDump(arg);
     for (;;);
 }

@@ -309,7 +363,7 @@
                   SCB_SHCSR_BUSFAULTENA_Msk |
                   SCB_SHCSR_MEMFAULTENA_Msk;

-    for (int_id = 0; int_id < NUM_INTERRUPTS - 16; int_id ++) {
+    for (int_id = 16; int_id < NUM_INTERRUPTS - 16; int_id ++) {
         /* Make sure the interrupt is disabled */
         NVIC_DisableIRQ(int_id);
         g_pfnRAMVectors[int_id] = &IntDefaultHandler;









Best regards,

Ole

-- 
kernel concepts GmbH            Tel: +49-271-771091-14
Sieghuetter Hauptweg 48         Mob: +49-177-7420433
D-57072 Siegen
http://www.embedded-it.de
http://www.kernelconcepts.de


More information about the En-Nut-Discussion mailing list