Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ExceptionHandlers.cpp « Hardware « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 640c0a416563c2311fffc93024c76f9df13a36ad (plain)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * CrashHandlers.cpp
 *
 *  Created on: 24 Aug 2020
 *      Author: David
 */

#include "ExceptionHandlers.h"
#include <Platform/RepRap.h>
#include <Platform/Platform.h>
#include <Platform/Tasks.h>
#include <Hardware/NonVolatileMemory.h>
#include <Cache.h>
#if SAME70 || SAM4S || SAM4E
# include <Reset.h>
#endif

// Perform a software reset. 'stk' points to the exception stack (r0 r1 r2 r3 r12 lr pc xPSR) if the cause is an exception, otherwise it is nullptr.
[[noreturn]] void SoftwareReset(SoftwareResetReason initialReason, const uint32_t *stk) noexcept
{
	IrqDisable();							// disable interrupts before we call any flash functions. We don't enable them again.
	WatchdogReset();							// kick the watchdog

#if SAME70 || SAM4E
	WatchdogResetSecondary();					// kick the secondary watchdog
#endif

	Cache::Disable();

	uint16_t fullReason = (uint16_t)initialReason;
	if (initialReason == SoftwareResetReason::erase)
	{
#if SAME5x
		//TODO invalidate flash so the USB bootloader runs
#else
		EraseAndReset();
#endif
 	}
	else
	{
		if (initialReason != SoftwareResetReason::user)
		{
			if (SERIAL_MAIN_DEVICE.canWrite() == 0)
			{
				fullReason |= (uint16_t)SoftwareResetReason::inUsbOutput;	// if we are resetting because we are stuck in a Spin function, record whether we are trying to send to USB
			}

#if HAS_AUX_DEVICES
			if (SERIAL_AUX_DEVICE.canWrite() == 0
# ifdef SERIAL_AUX2_DEVICE
				|| SERIAL_AUX2_DEVICE.canWrite() == 0
# endif
			   )
			{
				fullReason |= (uint16_t)SoftwareResetReason::inAuxOutput;	// if we are resetting because we are stuck in a Spin function, record whether we are trying to send to aux
			}
#endif
		}
		fullReason |= (uint8_t)reprap.GetSpinningModule();
		if (reprap.GetPlatform().WasDeliberateError())
		{
			fullReason |= (uint16_t)SoftwareResetReason::deliberate;
		}

		// Record the reason for the software reset
		NonVolatileMemory * const mem = new(Tasks::GetNVMBuffer(stk)) NonVolatileMemory;
		SoftwareResetData * const srd = mem->AllocateResetDataSlot();
        srd->Populate(fullReason, stk);
        mem->EnsureWritten();
	}

#if defined(__LPC17xx__)
    LPC_SYSCTL->RSID = 0x3F;					// Clear bits in reset reasons stored in RSID
#elif !SAME5x
	RSTC->RSTC_MR = RSTC_MR_KEY_PASSWD;			// ignore any signal on the NRST pin for now so that the reset reason will show as Software
#endif
	Reset();
	for(;;) {}
}

[[noreturn]] void OutOfMemoryHandler() noexcept
{
	register const uint32_t * stack_ptr asm ("sp");
	SoftwareReset(SoftwareResetReason::outOfMemory, stack_ptr);
}

// Exception handlers
// By default the Usage Fault, Bus Fault and Memory Management fault handlers are not enabled,
// so they escalate to a Hard Fault and we don't need to provide separate exception handlers for them.
extern "C" [[noreturn]] void hardFaultDispatcher(const uint32_t *pulFaultStackAddress) noexcept
{
	SoftwareReset(SoftwareResetReason::hardFault, pulFaultStackAddress);
}

// The fault handler implementation calls a function called hardFaultDispatcher()
extern "C" void HardFault_Handler() noexcept __attribute__((naked));
void HardFault_Handler() noexcept
{
	__asm volatile
	(
		" tst lr, #4                                                \n"		/* test bit 2 of the EXC_RETURN in LR to determine which stack was in use */
		" ite eq                                                    \n"		/* load the appropriate stack pointer into R0 */
		" mrseq r0, msp                                             \n"
		" mrsne r0, psp                                             \n"
		" ldr r2, handler_hf_address_const                          \n"
		" bx r2                                                     \n"
		" .align 2                                                  \n"		/* make the 2 LSBs zero at the next instruction */
		" handler_hf_address_const: .word hardFaultDispatcher       \n"
	);
}

#if USE_MPU

extern "C" [[noreturn]] void memManageDispatcher(const uint32_t *pulFaultStackAddress) noexcept
{
	SoftwareReset(SoftwareResetReason::memFault, pulFaultStackAddress);
}

// The fault handler implementation calls a function called memManageDispatcher()
extern "C" void MemManage_Handler() noexcept __attribute__((naked));
void MemManage_Handler() noexcept
{
	__asm volatile
	(
		" tst lr, #4                                                \n"		/* test bit 2 of the EXC_RETURN in LR to determine which stack was in use */
		" ite eq                                                    \n"		/* load the appropriate stack pointer into R0 */
		" mrseq r0, msp                                             \n"
		" mrsne r0, psp                                             \n"
		" ldr r2, handler_mf_address_const                          \n"
		" bx r2                                                     \n"
		" .align 2                                                  \n"		/* make the 2 LSBs zero at the next instruction */
		" handler_mf_address_const: .word memManageDispatcher       \n"
	);
}

#endif

extern "C" [[noreturn]] void wdtFaultDispatcher(const uint32_t *pulFaultStackAddress) noexcept
{
	SoftwareReset(SoftwareResetReason::wdtFault, pulFaultStackAddress);
}

#ifdef __LPC17xx__
extern "C" void WDT_IRQHandler() noexcept __attribute__((naked));
void WDT_IRQHandler() noexcept
{
	LPC_WDT->MOD &=~((uint32_t)(1<<2)); //SD::clear timout flag before resetting to prevent the Smoothie bootloader going into DFU mode
#else
# if SAME70		// SAME70 has a separate interrupt line for the RSWDT
extern "C" void RSWDT_Handler() noexcept __attribute__((naked));
void RSWDT_Handler() noexcept
# else
extern "C" void WDT_Handler() noexcept __attribute__((naked));
void WDT_Handler() noexcept
# endif
{
#endif
	__asm volatile
	(
		" tst lr, #4                                                \n"		/* test bit 2 of the EXC_RETURN in LR to determine which stack was in use */
		" ite eq                                                    \n"		/* load the appropriate stack pointer into R0 */
		" mrseq r0, msp                                             \n"
		" mrsne r0, psp                                             \n"
		" ldr r2, handler_wdt_address_const                         \n"
		" bx r2                                                     \n"
		" .align 2                                                  \n"		/* make the 2 LSBs zero at the next instruction */
		" handler_wdt_address_const: .word wdtFaultDispatcher       \n"
	);
}

extern "C" [[noreturn]] void otherFaultDispatcher(const uint32_t *pulFaultStackAddress) noexcept
{
	SoftwareReset(SoftwareResetReason::otherFault, pulFaultStackAddress);
}

// 2017-05-25: A user is getting 'otherFault' reports, so now we do a stack dump for those too.
// The fault handler implementation calls a function called otherFaultDispatcher()
extern "C" void OtherFault_Handler() noexcept __attribute__((naked));
void OtherFault_Handler() noexcept
{
	__asm volatile
	(
		" tst lr, #4                                                \n"		/* test bit 2 of the EXC_RETURN in LR to determine which stack was in use */
		" ite eq                                                    \n"		/* load the appropriate stack pointer into R0 */
		" mrseq r0, msp                                             \n"
		" mrsne r0, psp                                             \n"
		" ldr r2, handler_oflt_address_const                        \n"
		" bx r2                                                     \n"
		" .align 2                                                  \n"		/* make the 2 LSBs zero at the next instruction */
		" handler_oflt_address_const: .word otherFaultDispatcher    \n"
	);
}

// We could set up the following fault handlers to retrieve the program counter in the same way as for a Hard Fault,
// however these exceptions are unlikely to occur, so for now we just report the exception type.
extern "C" void NMI_Handler		   () noexcept __attribute__((naked));
extern "C" void UsageFault_Handler () noexcept __attribute__((naked));
extern "C" void DebugMon_Handler   () noexcept __attribute__((naked));

extern "C" void NMI_Handler        () noexcept { SoftwareReset(SoftwareResetReason::NMI); }
extern "C" void UsageFault_Handler () noexcept { SoftwareReset(SoftwareResetReason::usageFault); }
extern "C" void DebugMon_Handler   () noexcept __attribute__ ((alias("OtherFault_Handler")));

// FreeRTOS hooks that we need to provide
extern "C" [[noreturn]] void stackOverflowDispatcher(const uint32_t *pulFaultStackAddress, char* pcTaskName) noexcept
{
	SoftwareReset(SoftwareResetReason::stackOverflow, pulFaultStackAddress);
}

extern "C" [[noreturn]] void vApplicationStackOverflowHook(TaskHandle_t pxTask, char *pcTaskName) noexcept __attribute((naked));
void vApplicationStackOverflowHook(TaskHandle_t pxTask, char *pcTaskName) noexcept
{
	// r0 = pxTask, r1 = pxTaskName
	__asm volatile
	(
		" push {r0, r1, lr}											\n"		/* save parameters and call address on the stack */
		" mov r0, sp												\n"
		" ldr r2, handler_sovf_address_const                        \n"
		" bx r2                                                     \n"
		" .align 2                                                  \n"		/* make the 2 LSBs zero at the next instruction */
		" handler_sovf_address_const: .word stackOverflowDispatcher \n"
	);
}

extern "C" [[noreturn]] void assertCalledDispatcher(const uint32_t *pulFaultStackAddress) noexcept
{
	SoftwareReset(SoftwareResetReason::assertCalled, pulFaultStackAddress);
}

extern "C" [[noreturn]] void vAssertCalled(uint32_t line, const char *file) noexcept __attribute((naked));
void vAssertCalled(uint32_t line, const char *file) noexcept
{
#if false
	debugPrintf("ASSERTION FAILED IN %s on LINE %d\n", file, line);
	SERIAL_MAIN_DEVICE.flush();
#endif
	__asm volatile
	(
		" push {r0, r1, lr}											\n"		/* save parameters and call address */
		" mov r0, sp												\n"
		" ldr r2, handler_asrt_address_const                        \n"
		" bx r2                                                     \n"
		" .align 2                                                  \n"		/* make the 2 LSBs zero at the next instruction */
		" handler_asrt_address_const: .word assertCalledDispatcher  \n"
	);
}

#ifdef __LPC17xx__
[[noreturn]] void applicationMallocFailedCalledDispatcher(const uint32_t *pulFaultStackAddress) noexcept
{
	reprap.SoftwareReset(SoftwareResetReason::assertCalled, pulFaultStackAddress);
}

[[noreturn]] extern "C" void vApplicationMallocFailedHook() noexcept __attribute((naked));
void vApplicationMallocFailedHook() noexcept
{
	 __asm volatile
	(
		" push {r0, r1, lr}											\n"        /* save parameters and call address */
		" mov r0, sp												\n"
		" ldr r2, handler_amf_address_const							\n"
		" bx r2														\n"
		" .align 2                                                  \n"		/* make the 2 LSBs zero at the next instruction */
		" handler_amf_address_const: .word applicationMallocFailedCalledDispatcher  \n"
	 );
}
#endif

namespace std
{
	// We need to define this function in order to use lambda functions with captures
	[[noreturn]] void __throw_bad_function_call() noexcept { vAssertCalled(__LINE__, __FILE__); }
}

// The default terminate handler pulls in sprintf and lots of other functions, which makes the binary too large. So we replace it.
[[noreturn]] void Terminate() noexcept
{
	register const uint32_t * stack_ptr asm ("sp");
	SoftwareReset(SoftwareResetReason::terminateCalled, stack_ptr);
}

namespace __cxxabiv1
{
	std::terminate_handler __terminate_handler = Terminate;
}

extern "C" [[noreturn]] void __cxa_pure_virtual() noexcept
{
	register const uint32_t * stack_ptr asm ("sp");
	SoftwareReset(SoftwareResetReason::pureOrDeletedVirtual, stack_ptr);
}

extern "C" [[noreturn]] void __cxa_deleted_virtual() noexcept
{
	register const uint32_t * stack_ptr asm ("sp");
	SoftwareReset(SoftwareResetReason::pureOrDeletedVirtual, stack_ptr);
}

// End