I probably should have said more about what I am doing.
I usually add some backdoor to my projects which allows a user to, among other things, view MCU memory from a PC as the real program is executing in the MCU. The PC user might specify some non-existent memory address and I do not want this to cause the MCU program to crash.
So the MCU that facilitates this doesn’t just load the memory directly in to its transmit buffer but rather calls a routine that is supposed to return the contents of the desired memory location, or if that location doesn’t exist, return 0xff (or whatever). The ‘C’ code looks like this:
TxBuf[n1] = LookupByte(DumpAddressn);
Knowing this could generate a fault, I wrote LookupByte in assembly language:
.global LookupByte
LookupByte:
ldrb r0,[R0]
bx lr
.global HardFault_Handler
HardFault_Handler:
push {r7, lr}
add r7,sp,#0
ldr r0,LookupByteAdr
ldr r1,[sp,#0x020] // Get address of faulting intruction
cmp r0,r1 // If it's not LookupByte
HFH10: // then loop forever
bne HFH10
adds r1, r1, #2 // If it is our LookupByte intruction
str r1,[sp,#0x020] // add two and store back so we
// continue on
movs r0,#0xff // We rturn 0xff for non-existent locations
str r0,[sp,#8] // Store at locn from where r0 will be restored
mov sp, r7
pop {r7, pc}
LookupByteAdr: .word LookupByte
This works just fine if I single step through the code in KDS. It also works fine if I set a breakpoint in the Disassembly window at the first instruction of HardFaultHandler (HFH) and hit CONTINUE each time the breakpoint is reached. (For some reason setting the same breakpoint in the source window doesn’t work.) But if I set the breakpoint at the second instruction of HFH or if I set no breakpoint at all the processor regularly resets; probably when the processor tries to execute that second instruction of HFH.
I would also point out that if I set the breakpoint at the first instruction of LookupByte (LB) and press CONTINUE, the program breaks again at the second instruction of LB. CONTINUing there gets back to the breakpoint at the first instruction of LB, and this sequence can be CONTINUEd indefinitely.
Apart from getting my code to work, I would like to understand what is happening. I have experience with debuggers probably since there have been debuggers and I am not used to having a debugger change the behavior of a program not related to timing when a breakpoint is hit or when single-stepping, except that interrupts are usually blocked when single-stepping.
I look forward to receiving your response.