2121#include <string.h>
2222
2323#include "device.h"
24+ #include "mbed_atomic.h"
2425#include "mbed_error.h"
2526#include "mbed_interface.h"
2627#include "mbed_crash_data_offsets.h"
@@ -39,41 +40,49 @@ mbed_fault_context_t fault_context;
3940mbed_fault_context_t * const mbed_fault_context = & fault_context ;
4041#endif
4142
43+ extern bool mbed_error_in_progress ;
44+
4245//This is a handler function called from Fault handler to print the error information out.
4346//This runs in fault context and uses special functions(defined in mbed_rtx_fault_handler.c) to print the information without using C-lib support.
4447void mbed_fault_handler (uint32_t fault_type , const mbed_fault_context_t * mbed_fault_context_in )
4548{
4649 mbed_error_status_t faultStatus = MBED_SUCCESS ;
4750
48- mbed_error_printf ("\n++ MbedOS Fault Handler ++\n\nFaultType: " );
49-
50- switch (fault_type ) {
51- case MEMMANAGE_FAULT_EXCEPTION :
52- mbed_error_printf ("MemManageFault" );
53- faultStatus = MBED_ERROR_MEMMANAGE_EXCEPTION ;
54- break ;
55-
56- case BUS_FAULT_EXCEPTION :
57- mbed_error_printf ("BusFault" );
58- faultStatus = MBED_ERROR_BUSFAULT_EXCEPTION ;
59- break ;
60-
61- case USAGE_FAULT_EXCEPTION :
62- mbed_error_printf ("UsageFault" );
63- faultStatus = MBED_ERROR_USAGEFAULT_EXCEPTION ;
64- break ;
65-
66- //There is no way we can hit this code without getting an exception, so we have the default treated like hardfault
67- case HARD_FAULT_EXCEPTION :
68- default :
69- mbed_error_printf ("HardFault" );
70- faultStatus = MBED_ERROR_HARDFAULT_EXCEPTION ;
71- break ;
72- }
73- mbed_error_printf ("\n\nContext:" );
74- print_context_info ();
51+ /* Need to set the flag to ensure prints do not trigger a "mutex in ISR" trap
52+ * if they're first prints since boot and we have to init the I/O system.
53+ */
54+ if (!core_util_atomic_exchange_bool (& mbed_error_in_progress , true)) {
55+ mbed_error_printf ("\n++ MbedOS Fault Handler ++\n\nFaultType: " );
56+
57+ switch (fault_type ) {
58+ case MEMMANAGE_FAULT_EXCEPTION :
59+ mbed_error_printf ("MemManageFault" );
60+ faultStatus = MBED_ERROR_MEMMANAGE_EXCEPTION ;
61+ break ;
62+
63+ case BUS_FAULT_EXCEPTION :
64+ mbed_error_printf ("BusFault" );
65+ faultStatus = MBED_ERROR_BUSFAULT_EXCEPTION ;
66+ break ;
67+
68+ case USAGE_FAULT_EXCEPTION :
69+ mbed_error_printf ("UsageFault" );
70+ faultStatus = MBED_ERROR_USAGEFAULT_EXCEPTION ;
71+ break ;
72+
73+ //There is no way we can hit this code without getting an exception, so we have the default treated like hardfault
74+ case HARD_FAULT_EXCEPTION :
75+ default :
76+ mbed_error_printf ("HardFault" );
77+ faultStatus = MBED_ERROR_HARDFAULT_EXCEPTION ;
78+ break ;
79+ }
80+ mbed_error_printf ("\n\nContext:" );
81+ print_context_info ();
7582
76- mbed_error_printf ("\n\n-- MbedOS Fault Handler --\n\n" );
83+ mbed_error_printf ("\n\n-- MbedOS Fault Handler --\n\n" );
84+ core_util_atomic_store_bool (& mbed_error_in_progress , false);
85+ }
7786
7887 //Now call mbed_error, to log the error and halt the system
7988 mbed_error (faultStatus , "Fault exception" , (unsigned int )mbed_fault_context_in , NULL , 0 );
0 commit comments