Skip to content

Commit bffda45

Browse files
committed
stmhal: On HardFault, print stack pointer and do a stack dump.
1 parent b7d27e3 commit bffda45

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

stmhal/stm32_it.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,15 @@
7474
#include "pendsv.h"
7575
#include "irq.h"
7676
#include "pybthread.h"
77+
#include "gccollect.h"
7778
#include "extint.h"
7879
#include "timer.h"
7980
#include "uart.h"
8081
#include "storage.h"
8182
#include "can.h"
8283
#include "dma.h"
8384
#include "i2c.h"
85+
#include "usb.h"
8486

8587
extern void __fatal_error(const char*);
8688
extern PCD_HandleTypeDef pcd_fs_handle;
@@ -123,6 +125,15 @@ STATIC void print_reg(const char *label, uint32_t val) {
123125
mp_hal_stdout_tx_str("\r\n");
124126
}
125127

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+
126137
// The ARMv7M Architecture manual (section B.1.5.6) says that upon entry
127138
// to an exception, that the registers will be in the following order on the
128139
// // stack: R0, R1, R2, R3, R12, LR, PC, XPSR
@@ -132,11 +143,18 @@ typedef struct {
132143
} ExceptionRegisters_t;
133144

134145
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+
135152
print_reg("R0 ", regs->r0);
136153
print_reg("R1 ", regs->r1);
137154
print_reg("R2 ", regs->r2);
138155
print_reg("R3 ", regs->r3);
139156
print_reg("R12 ", regs->r12);
157+
print_reg("SP ", (uint32_t)regs);
140158
print_reg("LR ", regs->lr);
141159
print_reg("PC ", regs->pc);
142160
print_reg("XPSR ", regs->xpsr);
@@ -151,6 +169,19 @@ void HardFault_C_Handler(ExceptionRegisters_t *regs) {
151169
if (cfsr & 0x8000) {
152170
print_reg("BFAR ", SCB->BFAR);
153171
}
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+
154185
/* Go to infinite loop when Hard Fault exception occurs */
155186
while (1) {
156187
__fatal_error("HardFault");

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy