From 5d3324d3a9c4cc4aa3676740960439322aab0ccc Mon Sep 17 00:00:00 2001 From: Dylan Laduranty Date: Tue, 23 Apr 2024 20:23:28 +0200 Subject: [PATCH 1/2] cpu/cortexm: rework bkpt instruction call on panic Only call this instruction if a debug session is active otherwise it will trigger a hardfault Signed-off-by: Dylan Laduranty --- cpu/cortexm_common/panic.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cpu/cortexm_common/panic.c b/cpu/cortexm_common/panic.c index f7aa9c6b193f..a256352db3d0 100644 --- a/cpu/cortexm_common/panic.c +++ b/cpu/cortexm_common/panic.c @@ -39,7 +39,15 @@ void panic_arch(void) { #ifdef DEVELHELP print_ipsr(); - /* The bkpt instruction will signal to the debugger to break here. */ - __asm__("bkpt #0"); + /* CM0+ has a C_DEBUGEN bit but it is NOT accessible by CPU (only by debugger) */ +#ifdef CoreDebug_DHCSR_C_DEBUGEN_Msk + if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) { + /* if Debug session is running, tell the debugger to break here. + Skip it otherwise as this instruction will cause either a fault + escalation to hardfault or a CPU lockup */ + __asm__("bkpt #0"); + } +#endif /* CoreDebug_DHCSR_C_DEBUGEN_Msk */ + #endif } From e784794b370534f1d2b22bdb26f4beb33bf81714 Mon Sep 17 00:00:00 2001 From: Dylan Laduranty Date: Wed, 24 Apr 2024 13:20:59 +0200 Subject: [PATCH 2/2] cpu/cortexm: uncrustify panic.c Signed-off-by: Dylan Laduranty --- cpu/cortexm_common/panic.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpu/cortexm_common/panic.c b/cpu/cortexm_common/panic.c index a256352db3d0..5cef36cd50f2 100644 --- a/cpu/cortexm_common/panic.c +++ b/cpu/cortexm_common/panic.c @@ -27,7 +27,8 @@ static void print_ipsr(void) { uint32_t ipsr = __get_IPSR() & IPSR_ISR_Msk; - if(ipsr) { + + if (ipsr) { /* if you get here, you might have forgotten to implement the isr * for the printed interrupt number */ LOG_ERROR("Inside isr %d\n", ((int)ipsr) - 16);