2020-06-30 04:03 PM
If I understand correctly, the BKPT instruction is "promoted" to hard fault when debugger is not connected.
Is it possible to tweak STM32H7 so that BKPT will be silently ignored?
-- pa
Solved! Go to Solution.
2020-06-30 11:53 PM
Enable the DebugMon exception
CoreDebug->DEMCR |= CoreDebug_DEMCR_MON_EN_Msk;
then it will be called instead of the hardfault handler. Unfortunately, it would return to the same BKPT instruction which would trigger the exception again and again, so you have to bump the exception return address on the stack past the BKPT. Assembly only, because we are messing with the stack frame.
__attribute__((naked))
void DebugMon_Handler(void) {
asm volatile(
"ldr r0, [sp, #24]\n\t" // PC is at offset 24 on the exception stack frame
"add r0, #2\n\t" // BKPT is encoded in 2 bytes
"str r0, [sp, #24]\n\t"
"bx lr\n\t"
);
}
This works only when the exception handler is actually allowed to run, i.e. interrupts are not disabled, no other handler is running with priority 0 or whatever priority DebugMon_Handler actually has, which I could not find in the PM. But it definitely has one.
2020-06-30 11:53 PM
Enable the DebugMon exception
CoreDebug->DEMCR |= CoreDebug_DEMCR_MON_EN_Msk;
then it will be called instead of the hardfault handler. Unfortunately, it would return to the same BKPT instruction which would trigger the exception again and again, so you have to bump the exception return address on the stack past the BKPT. Assembly only, because we are messing with the stack frame.
__attribute__((naked))
void DebugMon_Handler(void) {
asm volatile(
"ldr r0, [sp, #24]\n\t" // PC is at offset 24 on the exception stack frame
"add r0, #2\n\t" // BKPT is encoded in 2 bytes
"str r0, [sp, #24]\n\t"
"bx lr\n\t"
);
}
This works only when the exception handler is actually allowed to run, i.e. interrupts are not disabled, no other handler is running with priority 0 or whatever priority DebugMon_Handler actually has, which I could not find in the PM. But it definitely has one.
2020-07-01 01:55 AM
Thank you a lot, Peter
-- pa
2024-09-06 07:45 AM
I know this is an old thread but for anyone still looking for how to detect if the debugger is attached, you can check the C_DEBUGEN bit of the DHCSR register. Below is a link to the ARM documentation.
if (CoreDebug->DHCSR & 1) // check if debugger is connected
{
__BKPT(0); // halt program execution here with a breakpoint
}