74
74
#include "pendsv.h"
75
75
#include "irq.h"
76
76
#include "pybthread.h"
77
+ #include "gccollect.h"
77
78
#include "extint.h"
78
79
#include "timer.h"
79
80
#include "uart.h"
80
81
#include "storage.h"
81
82
#include "can.h"
82
83
#include "dma.h"
83
84
#include "i2c.h"
85
+ #include "usb.h"
84
86
85
87
extern void __fatal_error (const char * );
86
88
extern PCD_HandleTypeDef pcd_fs_handle ;
@@ -123,6 +125,15 @@ STATIC void print_reg(const char *label, uint32_t val) {
123
125
mp_hal_stdout_tx_str ("\r\n" );
124
126
}
125
127
128
+ STATIC void print_hex_hex (const char * label , uint32_t val1 , uint32_t val2 ) {
129
+ char hex_str [9 ];
130
+ mp_hal_stdout_tx_str (label );
131
+ mp_hal_stdout_tx_str (fmt_hex (val1 , hex_str ));
132
+ mp_hal_stdout_tx_str (" " );
133
+ mp_hal_stdout_tx_str (fmt_hex (val2 , hex_str ));
134
+ mp_hal_stdout_tx_str ("\r\n" );
135
+ }
136
+
126
137
// The ARMv7M Architecture manual (section B.1.5.6) says that upon entry
127
138
// to an exception, that the registers will be in the following order on the
128
139
// // stack: R0, R1, R2, R3, R12, LR, PC, XPSR
@@ -132,11 +143,18 @@ typedef struct {
132
143
} ExceptionRegisters_t ;
133
144
134
145
void HardFault_C_Handler (ExceptionRegisters_t * regs ) {
146
+ // We need to disable the USB so it doesn't try to write data out on
147
+ // the VCP and then block indefinitely waiting for the buffer to drain.
148
+ pyb_usb_flags = 0 ;
149
+
150
+ mp_hal_stdout_tx_str ("HardFault\r\n" );
151
+
135
152
print_reg ("R0 " , regs -> r0 );
136
153
print_reg ("R1 " , regs -> r1 );
137
154
print_reg ("R2 " , regs -> r2 );
138
155
print_reg ("R3 " , regs -> r3 );
139
156
print_reg ("R12 " , regs -> r12 );
157
+ print_reg ("SP " , (uint32_t )regs );
140
158
print_reg ("LR " , regs -> lr );
141
159
print_reg ("PC " , regs -> pc );
142
160
print_reg ("XPSR " , regs -> xpsr );
@@ -151,6 +169,19 @@ void HardFault_C_Handler(ExceptionRegisters_t *regs) {
151
169
if (cfsr & 0x8000 ) {
152
170
print_reg ("BFAR " , SCB -> BFAR );
153
171
}
172
+
173
+ if ((void * )& _ram_start <= (void * )regs && (void * )regs < (void * )& _ram_end ) {
174
+ mp_hal_stdout_tx_str ("Stack:\r\n" );
175
+ uint32_t * stack_top = & _estack ;
176
+ if ((void * )regs < (void * )& _heap_end ) {
177
+ // stack not in static stack area so limit the amount we print
178
+ stack_top = (uint32_t * )regs + 32 ;
179
+ }
180
+ for (uint32_t * sp = (uint32_t * )regs ; sp < stack_top ; ++ sp ) {
181
+ print_hex_hex (" " , (uint32_t )sp , * sp );
182
+ }
183
+ }
184
+
154
185
/* Go to infinite loop when Hard Fault exception occurs */
155
186
while (1 ) {
156
187
__fatal_error ("HardFault" );
0 commit comments