HardFault in interrupt handler

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

cross mob
Stephan_Hochmül
Employee
Employee
I want to implement a bus system. For this bus system I need a very well timing performance. For the timing interval I use the CCU40_0 from the XMC4500. To provide a jitter free signal I need to program the Interrupt handler in Assembler. At this point I have big problems. If I debug the interrupt handler step by step every think works fine. But ifI run the program I get an hard fault (Invalid state UsageFault). As I recognized this error I tried out to implement the interrupt handler in C. This works fine. So I copied the generated assembler Code to my own assembler written interrupt handler and tried it again. The same error occurs.

Enclosed the two Disassembly Cases and the associated program codes. That shows that the Disassembly Codes are the same.
Does anyone have an idea what could be wrong?

yours sincerely
Stephan

Assembler Case:

Code


#include 

.thumb
.text
//.global swhp_updatetimer
.global swhp_pulse
.func swhp_pulse

.align 4
swhp_pulse:

push {r7}
add r7, sp, #0x0
nop
mov r13, r7
pop {r7}
bx r14


Disassembly:

080005da:   df f8 24 04  ldr     r0, [pc, #1060] 
080005de: 00 b5 push {lr}
080005e0: 81 b0 sub sp, #0x4
080005e2: 80 47 blx r0

08001540: 80 b4 push {r7}
08001542: 00 af add r7, sp, #0x0
08001544: c0 46 mov r8, r8
08001546: bd 46 mov r13, r7
08001548: 80 bc pop {r7}
0800154a: Unable to retrieve disassembly data from backend. (bx r14)


080005e4: 01 b0 add sp, #0x4
080005e6: 00 bd pop {pc}


C Case

Code:

void CCU40_0_IRQHandler() {
return;
}


Disassembly

080005da:   df f8 24 04  ldr     r0, [pc, #1060] 
080005de: 00 b5 push {lr}
080005e0: 81 b0 sub sp, #0x4
080005e2: 80 47 blx r0


08001530: 80 b4 push {r7}
08001532: 00 af add r7, sp, #0x0
08001534: 00 bf nop
08001536: bd 46 mov r13, r7
08001538: 80 bc pop {r7}
0800153a: 70 47 bx r14


080005e4: 01 b0 add sp, #0x4
080005e6: 00 bd pop {pc}
0 Likes
10 Replies
Travis
Employee
Employee
First solution authored Welcome! 500 replies posted
At this point I have big problems. When I debug the interrupt handler step by step every think works fine. But when I run the program I get an hard fault (Invalid state UsageFault).
Travis: Can you provide a screen shot when the hard fault happens and hope we can get some hints from there.
0 Likes
Stephan_Hochmül
Employee
Employee
Thanks for your answer. It is difficult to find the point when the hard fault happens, because when I debug it step by step every think works fine. And as soon as I press the resume button a hard fault happens. Interesting on this point is that if I place a breakpoint on the point where the Insert_exceptionHandler branches to my interrupt handler and I press resume, also every think works.
The picture below shows the Debug Window on that point.
1093.attach
0 Likes
Not applicable
Hi,

sometimes it is helpful to overwrite the usage fault handler with your own C function. Add a breakpoint and try to rebuild the stack trace (e.g. memory watch window or debugger stack trace) to see where you coming from. Maybe it's not your assembler code throwing this fault.
Attention must be payed if the stack is corrupted. In this cases you have no chance to find out where you are coming from.
0 Likes
lock attach
Attachments are accessible only for community members.
Travis
Employee
Employee
First solution authored Welcome! 500 replies posted
Hi Stephan,

Sorry for the delay. I had tried to replicate your issue with your provided assembly code and seems like everything is working good as I don't encounter any hard fault. Please reference to the attached project.

Best regards
Travis
0 Likes
Stephan_Hochmül
Employee
Employee
Hi,
Thank you for your efforts!
Travis: I was testing your project, and saw that you also used a C function to call the assembler function. That was the case which worked. I want to implement the interrupt handler direct in assembler. In this case the HardFauld occurs. I attached my Project. Maybe you can take a look on it.

Tomwel: The stack has always the same value when the program enters the CCU40_0 Insert_ExceptionHandler. And by debugging it words fine. So I think I can’t find the error on this way.

Best regards
Stephan
0 Likes
Travis
Employee
Employee
First solution authored Welcome! 500 replies posted
Hi Stephan,

This is a little bit too hard core which we don't usually do. Why can't you just code your assembly within the interrupt function and let the compiler take care of the push pull of the stack.

Best Regards
Travis
0 Likes
lock attach
Attachments are accessible only for community members.
Stephan_Hochmül
Employee
Employee
Stephan Hochmüller wrote:
Hi,
Thank you for your efforts!
Travis: I was testing your project, and saw that you also used a C function to call the assembler function. That was the case which worked. I want to implement the interrupt handler direct in assembler. In this case the HardFauld occurs. I attached my Project. Maybe you can take a look on it.

Tomwel: The stack has always the same value when the program enters the CCU40_0 Insert_ExceptionHandler. And by debugging it words fine. So I think I can’t find the error on this way.

Best regards
Stephan



Sorry forget to attach the Project:
0 Likes
Travis
Employee
Employee
First solution authored Welcome! 500 replies posted
Hi Stephan,

I will have a look at your code when I am free. But at the moment I recommend that you embed your assembly code inside the interrupt C function. May I know if this way actually generates more codes?

Best Regards
Travis
0 Likes
Stephan_Hochmül
Employee
Employee
Hallo Travis,
Yes it generates more code. If an interrupt appears the program jumps into the C interrupt handler. And in this handler the program jumps to the assembler function. Thus there is one jump more than it is needed.
Best Regards
Stephan
0 Likes
Not applicable
You could try using inline assembler in the interrupt handler function.
0 Likes