forked from u-blox/ubxlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
u_exception_handler.c
256 lines (221 loc) · 7.07 KB
/
u_exception_handler.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright 2019-2024 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @file
* @brief Cortex M4 exception handlers.
*/
#include "stdbool.h"
#include "stm32f4xx.h"
#include "cmsis_os.h"
#include "core_cm4.h"
#include "task_snapshot.h"
#include "u_assert.h"
#include "u_port_debug.h"
#include "u_debug_utils.h"
#include "u_debug_utils_internal.h"
#include "FreeRTOS.h" // For xPortGetFreeHeapSize()
#include "task.h" // For xTaskGetSchedulerState()
/* FreeRTOS tick timer interrupt handler prototype */
extern void xPortSysTickHandler(void);
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
#define CALL_EXCEPTION_HANDLER(handler) \
__asm volatile( \
"tst lr, #4 \n" \
"ite eq \n" \
"mrseq r0, msp \n" \
"mrsne r0, psp \n" \
"b %0 \n" \
: : "i"(handler) )
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
typedef struct __attribute__((packed))
{
uint32_t r0;
uint32_t r1;
uint32_t r2;
uint32_t r3;
uint32_t r12;
uint32_t lr;
uint32_t pc;
uint32_t xpsr;
}
uExceptionFrame_t;
extern uint32_t uDebugUtilsGetThreadStackTop(TaskHandle_t handle);
/* ----------------------------------------------------------------
* VARIABLES
* -------------------------------------------------------------- */
/* Need to update this value in cellular_port_private.c. */
extern int32_t gTickTimerRtosCount;
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */
#ifdef U_DEBUG_UTILS_DUMP_THREADS
static void dummyAssert(const char *pFileStr, int32_t line)
{
(void)pFileStr;
(void)line;
}
#endif
__attribute__((optimize("O0")))
static void dumpData(uExceptionFrame_t *frame)
{
if (frame != NULL) {
uPortLogF(" PC: 0x%08x LR: 0x%08x\n", frame->pc, frame->lr);
uPortLogF(" R0: 0x%08x R1: 0x%08x R2: 0x%08x R3: 0x%08x\n",
frame->r0, frame->r1, frame->r2, frame->r3);
uPortLogF(" R12: 0x%08x XPSR: 0x%08x\n", frame->r12, frame->xpsr);
#ifndef U_DEBUG_UTILS_DUMP_THREADS
// Our monitor will automatically call addr2line for target strings
// that starts with "Backtrace: ", so we print PC and LR again
// as a backtrace:
uPortLogF(" Backtrace: 0x%08x 0x%08x\n", frame->pc, frame->lr);
#else
uStackFrame_t sFrame;
TaskSnapshot_t snapShot;
char *pName;
uint32_t psp = ((uint32_t)frame) + sizeof(uExceptionFrame_t);
uint32_t stackTop;
vTaskGetSnapshot(xTaskGetCurrentTaskHandle(), &snapShot);
pName = pcTaskGetName(xTaskGetCurrentTaskHandle());
stackTop = (uint32_t)snapShot.pxTopOfStack;
uPortLogF("### Dumping current thread (%s) ###\n", pName);
uPortLogF(" Backtrace: 0x%08x 0x%08x ", frame->pc, frame->lr);
if (uDebugUtilsInitStackFrame(psp, stackTop, &sFrame)) {
for (int depth = 0; depth < 16; depth++) {
if (uDebugUtilsGetNextStackFrame(stackTop, &sFrame)) {
if ((depth > 0) || (sFrame.pc != frame->lr)) {
uPortLogF("0x%08x ", (unsigned int)sFrame.pc);
}
} else {
break;
}
}
}
uPortLogF("\n\n");
#endif
}
#ifdef U_DEBUG_UTILS_DUMP_THREADS
// When calling uDebugUtilsDumpThreads() vPortEnterCritical
// will be called. vPortEnterCritical is not interrupt safe
// which we clearly don't care about during an exception.
// However, this will make an assert fail which would result
// in an endless loop halfway through uDebugUtilsDumpThreads().
// For this reason we replace the current assert handler
// with a dummy handler that does nothing.
uAssertHookSet(dummyAssert);
uDebugUtilsDumpThreads();
#endif
}
static void uHardfaultHandler(uExceptionFrame_t *frame)
{
uPortLogF("\n### Caught HardFault exception ###\n");
uPortLogF(" HFSR: 0x%08x\n", SCB->HFSR);
uPortLogF(" CFSR: 0x%08x\n", SCB->CFSR);
dumpData(frame);
// TODO: Should probably reboot here instead
while (1) {}
}
static void uMemManageHandler(uExceptionFrame_t *frame)
{
uPortLogF("\n### Caught MemManage exception ###\n");
uPortLogF(" MMFAR: 0x%08x\n", SCB->MMFAR);
uPortLogF(" CFSR: 0x%08x\n", SCB->CFSR);
dumpData(frame);
// TODO: Should probably reboot here instead
while (1) {}
}
static void uUsageFaultHandler(uExceptionFrame_t *frame)
{
uPortLogF("\n### Caught UsageFault exception ###\n");
uPortLogF(" CFSR: 0x%08x\n", SCB->CFSR);
dumpData(frame);
// TODO: Should probably reboot here instead
while (1) {}
}
static void uBusFaultHandler(uExceptionFrame_t *frame)
{
uPortLogF("\n### Caught BusFault exception ###\n");
uPortLogF(" BFAR: 0x%08x\n", SCB->BFAR);
uPortLogF(" CFSR: 0x%08x\n", SCB->CFSR);
dumpData(frame);
// TODO: Should probably reboot here instead
while (1) {}
}
/* ----------------------------------------------------------------
* PUBLIC FUNCTIONS
* -------------------------------------------------------------- */
/**
* @brief NMI handler.
*/
void NMI_Handler(void)
{
}
/**
* @brief Hard Fault hook. Actual handler is uHardfaultHandler.
*/
__attribute__((naked))
void HardFault_Handler(void)
{
CALL_EXCEPTION_HANDLER(uHardfaultHandler);
}
/**
* @brief Memory Manage hook. Actual handler is uMemManageHandler.
*/
__attribute__((naked))
void MemManage_Handler(void)
{
CALL_EXCEPTION_HANDLER(uMemManageHandler);
}
/**
* @brief Bus Fault hook. Actual handler is uBusFaultHandler.
*/
__attribute__((naked))
void BusFault_Handler(void)
{
CALL_EXCEPTION_HANDLER(uBusFaultHandler);
}
/**
* @brief Usage Fault hook. Actual handler is uUsageFaultHandler.
*/
__attribute__((naked))
void UsageFault_Handler(void)
{
CALL_EXCEPTION_HANDLER(uUsageFaultHandler);
}
/**
* @brief This function handles Debug Monitor exception.
*/
void DebugMon_Handler(void)
{
}
/**
* @brief SysTick handler.
*/
void SysTick_Handler(void)
{
gTickTimerRtosCount++;
#ifdef CMSIS_V2
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
/* Call tick handler */
xPortSysTickHandler();
}
#else
osSystickHandler();
#endif
}
// End of file