update credits
[librepilot.git] / flight / libraries / cm3_fault_handlers.c
blob512ff274b0fecd8f9e14396d4b75ef138bd098b9
1 /*
2 * cm3_fault_handlers.c
4 * Created on: Apr 24, 2011
5 * Author: msmith
6 */
8 #include <stdint.h>
9 #include "inc/dcc_stdio.h"
10 #ifdef STM32F4
11 # include <stm32f4xx.h>
12 #endif
13 #ifdef STM32F3
14 # include <stm32f30x.h>
15 #endif
16 #ifdef STM32F2XX
17 # include <stm32f2xx.h>
18 #endif
19 #ifdef STM32F1
20 # include <stm32f10x.h>
21 #endif
23 #define FAULT_TRAMPOLINE(_vec) \
24 __attribute__((naked, no_instrument_function)) \
25 void \
26 _vec##_Handler(void) \
27 { \
28 __asm(".syntax unified\n" \
29 "MOVS R0, #4 \n" \
30 "MOV R1, LR \n" \
31 "TST R0, R1 \n" \
32 "BEQ 1f \n" \
33 "MRS R0, PSP \n" \
34 "B " #_vec "_Handler2 \n" \
35 "1: \n" \
36 "MRS R0, MSP \n" \
37 "B " #_vec "_Handler2 \n" \
38 ".syntax divided\n"); \
39 } \
40 struct hack
42 struct cm3_frame {
43 uint32_t r0;
44 uint32_t r1;
45 uint32_t r2;
46 uint32_t r3;
47 uint32_t r12;
48 uint32_t lr;
49 uint32_t pc;
50 uint32_t psr;
53 FAULT_TRAMPOLINE(HardFault);
54 FAULT_TRAMPOLINE(BusFault);
55 FAULT_TRAMPOLINE(UsageFault);
57 /* this is a hackaround to avoid an issue where dereferencing SCB seems to result in bad codegen and a link error */
58 #define SCB_REG(_reg) (*(uint32_t *)&(SCB->_reg))
60 void HardFault_Handler2(struct cm3_frame *frame)
62 dbg_write_str("\nHARD FAULT");
63 dbg_write_hex32(frame->pc);
64 dbg_write_char('\n');
65 dbg_write_hex32(SCB_REG(HFSR));
66 dbg_write_char('\n');
67 for (;;) {
72 void BusFault_Handler2(struct cm3_frame *frame)
74 dbg_write_str("\nBUS FAULT");
75 dbg_write_hex32(frame->pc);
76 dbg_write_char('\n');
77 dbg_write_hex32(SCB_REG(CFSR));
78 dbg_write_char('\n');
79 dbg_write_hex32(SCB_REG(BFAR));
80 dbg_write_char('\n');
81 for (;;) {
86 void UsageFault_Handler2(struct cm3_frame *frame)
88 dbg_write_str("\nUSAGE FAULT");
89 dbg_write_hex32(frame->pc);
90 dbg_write_char('\n');
91 dbg_write_hex32(SCB_REG(CFSR));
92 dbg_write_char('\n');
93 for (;;) {