trying hard fault

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Not applicable
This code trying to write to an reserved address on XMC1300.
Write to the ADDR generates hard fault but it is called continiusly, why?

At this point called hard fault but the the interrupt returned exactly to the same address, and after hard fault called again and again.
Why returns the interrupt to 0x10001196 instead of 0x10001198 (which is the next address)
10001196: str r2, [r3, #0]


11 int* p = ADDR;
1000118e: ldr r3, [pc, #12] ; (0x1000119c
)
10001190: str r3, [r7, #4]
13 *p = 1;
10001192: ldr r3, [r7, #4]
10001194: movs r2, #1
10001196: str r2, [r3, #0]
15 while(1);
10001198: b.n 0x10001198




#include

#include "XMC1300.h"
#include "GPIO.h"

#define ADDR 0x40000800

int main(void)
{

int* p = ADDR;

*p = 1;

while(1);
}

void HardFault_Handler()
{
}
0 Likes
1 Reply
User10215
Level 4
Level 4
First like received
Hi jsmith,

the Hard Fault Interrupt works just as any other interrupt. When an interrupt occurs the current program counter is memorized and when the interrupt returns the CPU jumps back to that program counter.
In your case the program counter gets to this address here "10001196: str r2, [r3, #0]"...the instruction here tries to store a value at an invalid address which leads to the Hard Fault Interrupt. So that program counter is memorized and when your hard fault handler returns the CPU jumps back to that address where that invalid instruction leads to an hard fault again.

It's actually a good thing that the faulty instruction gets memorized because there are ways to extract that address for debugging purposes when inside the hard fault handler so you can see at which point the error occurred.

Regards,
Niclas
0 Likes