Skip to content

Commit e4be878

Browse files
committed
handle_error: Now supports HW exception from handler mode.
1 parent 04cb668 commit e4be878

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

platform/source/mbed_error.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626
#include "platform/mbed_interface.h"
2727
#include "platform/mbed_power_mgmt.h"
2828
#include "platform/mbed_stats.h"
29-
#if defined(__CORTEX_M)
3029
#include "platform/source/TARGET_CORTEX_M/mbed_fault_handler.h"
31-
#endif
30+
#include "mbed_rtx.h"
3231
#ifdef MBED_CONF_RTOS_PRESENT
3332
#include "rtx_os.h"
3433
#endif
@@ -153,29 +152,40 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
153152
//Capture error information
154153
current_error_ctx.error_status = error_status;
155154
current_error_ctx.error_value = error_value;
156-
if (error_status == MBED_ERROR_MEMMANAGE_EXCEPTION ||
155+
bool bHWException = (error_status == MBED_ERROR_MEMMANAGE_EXCEPTION ||
157156
error_status == MBED_ERROR_BUSFAULT_EXCEPTION ||
158157
error_status == MBED_ERROR_USAGEFAULT_EXCEPTION ||
159-
error_status == MBED_ERROR_HARDFAULT_EXCEPTION) {
160-
#if defined(__CORTEX_M)
161-
mbed_fault_context_t *mfc = (mbed_fault_context_t *)error_value;
158+
error_status == MBED_ERROR_HARDFAULT_EXCEPTION);
159+
mbed_fault_context_t *mfc = NULL;
160+
if (bHWException) {
161+
mfc = (mbed_fault_context_t *)error_value;
162162
current_error_ctx.error_address = (uint32_t)mfc->PC_reg;
163163
// Note that this SP_reg is the correct SP value of the fault. PSP and MSP are slightly different due to HardFault_Handler.
164164
current_error_ctx.thread_current_sp = (uint32_t)mfc->SP_reg;
165165
// Note that the RTX thread itself is the same even under this fault exception handler.
166-
#endif
167166
} else {
168167
current_error_ctx.error_address = (uint32_t)caller;
169168
current_error_ctx.thread_current_sp = (uint32_t)&current_error_ctx; // Address local variable to get a stack pointer
170169
}
171170

172171
#ifdef MBED_CONF_RTOS_PRESENT
173-
//Capture thread info
174-
osRtxThread_t *current_thread = osRtxInfo.thread.run.curr;
175-
current_error_ctx.thread_id = (uint32_t)current_thread;
176-
current_error_ctx.thread_entry_address = (uint32_t)current_thread->thread_addr;
177-
current_error_ctx.thread_stack_size = current_thread->stack_size;
178-
current_error_ctx.thread_stack_mem = (uint32_t)current_thread->stack_mem;
172+
if (mfc && !(mfc->EXC_RETURN & 0x4))
173+
{
174+
// handler mode
175+
current_error_ctx.thread_id = 0;
176+
current_error_ctx.thread_entry_address = 0;
177+
current_error_ctx.thread_stack_size = MAX(0, INITIAL_SP - current_error_ctx.thread_current_sp - sizeof(int));
178+
current_error_ctx.thread_stack_mem = current_error_ctx.thread_current_sp;
179+
}
180+
else
181+
{
182+
// Capture thread info in thread mode
183+
osRtxThread_t *current_thread = osRtxInfo.thread.run.curr;
184+
current_error_ctx.thread_id = (uint32_t)current_thread;
185+
current_error_ctx.thread_entry_address = (uint32_t)current_thread->thread_addr;
186+
current_error_ctx.thread_stack_size = current_thread->stack_size;
187+
current_error_ctx.thread_stack_mem = (uint32_t)current_thread->stack_mem;
188+
}
179189
#endif //MBED_CONF_RTOS_PRESENT
180190

181191
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED
@@ -458,16 +468,20 @@ mbed_error_status_t mbed_clear_all_errors(void)
458468
return status;
459469
}
460470

461-
static inline const char *name_or_unnamed(const char *name)
471+
static inline const char *name_or_unnamed(const osRtxThread_t *thread)
462472
{
473+
if (!thread)
474+
return "<handler>";
475+
476+
const char *name = thread->name;
463477
return name ? name : "<unnamed>";
464478
}
465479

466480
#if MBED_CONF_PLATFORM_ERROR_ALL_THREADS_INFO && defined(MBED_CONF_RTOS_PRESENT)
467481
/* Prints info of a thread(using osRtxThread_t struct)*/
468482
static void print_thread(const osRtxThread_t *thread)
469483
{
470-
mbed_error_printf("\n%s State: 0x%" PRIX8 " Entry: 0x%08" PRIX32 " Stack Size: 0x%08" PRIX32 " Mem: 0x%08" PRIX32 " SP: 0x%08" PRIX32, name_or_unnamed(thread->name), thread->state, thread->thread_addr, thread->stack_size, (uint32_t)thread->stack_mem, thread->sp);
484+
mbed_error_printf("\n%s State: 0x%" PRIX8 " Entry: 0x%08" PRIX32 " Stack Size: 0x%08" PRIX32 " Mem: 0x%08" PRIX32 " SP: 0x%08" PRIX32, name_or_unnamed(thread), thread->state, thread->thread_addr, thread->stack_size, (uint32_t)thread->stack_mem, thread->sp);
471485
}
472486

473487
/* Prints thread info from a list */
@@ -551,7 +565,7 @@ static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg,
551565
mbed_error_printf("\nError Value: 0x%" PRIX32, ctx->error_value);
552566
#ifdef MBED_CONF_RTOS_PRESENT
553567
mbed_error_printf("\nCurrent Thread: %s Id: 0x%" PRIX32 " Entry: 0x%" PRIX32 " StackSize: 0x%" PRIX32 " StackMem: 0x%" PRIX32 " SP: 0x%" PRIX32 " ",
554-
name_or_unnamed(((osRtxThread_t *)ctx->thread_id)->name),
568+
name_or_unnamed((osRtxThread_t *)ctx->thread_id),
555569
ctx->thread_id, ctx->thread_entry_address, ctx->thread_stack_size, ctx->thread_stack_mem, ctx->thread_current_sp);
556570
#endif
557571

0 commit comments

Comments
 (0)