-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqdn_stm32_hard_fault_handler_c.c
102 lines (90 loc) · 4.5 KB
/
qdn_stm32_hard_fault_handler_c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**************************************************************************
*
* Copyright (c) 2013, Qromodyn Corporation
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the FreeBSD Project.
**************************************************************************/
// From Joseph Yiu, minor edits by FVH
// hard fault handler in C,
// with stack frame location as input parameter
// called from HardFault_Handler in file xxx.s
#include "stm32f10x.h"
#include "qdn_stm32_hard_fault_handler.h"
#include "qdn_cpu.h"
__no_init volatile QDN_StackDump_t qdn_StackDump;
void hard_fault_handler_c (unsigned int * hardfault_args);
void hard_fault_handler_c (unsigned int * hardfault_args) { // ISR
#ifdef PRINTF_ON_HARDFAULT
unsigned int stacked_r0;
unsigned int stacked_r1;
unsigned int stacked_r2;
unsigned int stacked_r3;
unsigned int stacked_r12;
unsigned int stacked_lr;
unsigned int stacked_pc;
unsigned int stacked_psr;
stacked_r0 = ((unsigned long) hardfault_args[0]);
stacked_r1 = ((unsigned long) hardfault_args[1]);
stacked_r2 = ((unsigned long) hardfault_args[2]);
stacked_r3 = ((unsigned long) hardfault_args[3]);
stacked_r12 = ((unsigned long) hardfault_args[4]);
stacked_lr = ((unsigned long) hardfault_args[5]);
stacked_pc = ((unsigned long) hardfault_args[6]);
stacked_psr = ((unsigned long) hardfault_args[7]);
printf ("\n\n[Hard fault handler - all numbers in hex]\n");
printf ("R0 = %x\n", stacked_r0);
printf ("R1 = %x\n", stacked_r1);
printf ("R2 = %x\n", stacked_r2);
printf ("R3 = %x\n", stacked_r3);
printf ("R12 = %x\n", stacked_r12);
printf ("LR [R14] = %x subroutine call return address\n", stacked_lr);
printf ("PC [R15] = %x program counter\n", stacked_pc);
printf ("PSR = %x\n", stacked_psr);
printf ("BFAR = %x\n", (*((volatile unsigned long *)(0xE000ED38))));
printf ("CFSR = %x\n", (*((volatile unsigned long *)(0xE000ED28))));
printf ("HFSR = %x\n", (*((volatile unsigned long *)(0xE000ED2C))));
printf ("DFSR = %x\n", (*((volatile unsigned long *)(0xE000ED30))));
printf ("AFSR = %x\n", (*((volatile unsigned long *)(0xE000ED3C))));
printf ("SCB_SHCSR = %x\n", SCB->SHCSR);
#else
qdn_StackDump.R0 = ((uint32_t) hardfault_args[0]);
qdn_StackDump.R1 = ((uint32_t) hardfault_args[1]);
qdn_StackDump.R2 = ((uint32_t) hardfault_args[2]);
qdn_StackDump.R3 = ((uint32_t) hardfault_args[3]);
qdn_StackDump.R12 = ((uint32_t) hardfault_args[4]);
qdn_StackDump.LR = ((uint32_t) hardfault_args[5]);
qdn_StackDump.PC = ((uint32_t) hardfault_args[6]);
qdn_StackDump.PSR = ((uint32_t) hardfault_args[7]);
qdn_StackDump.BFAR = (*((volatile unsigned long *)(0xE000ED38)));
qdn_StackDump.CFSR = (*((volatile unsigned long *)(0xE000ED28)));
qdn_StackDump.HFSR = (*((volatile unsigned long *)(0xE000ED2C)));
qdn_StackDump.DFSR = (*((volatile unsigned long *)(0xE000ED30)));
qdn_StackDump.AFSR = (*((volatile unsigned long *)(0xE000ED3C)));
qdn_StackDump.SCB_SHCSR = SCB->SHCSR;
qdn_StackDump.status = 0xDEADBEEF;
#endif
// this will let the watchdog do a reset, if it is enabled.
while (1);
}