extern
porcupine
demo
mcu
imxrt1050
imxrt1050-evkb
source
semihost_hardfault.c
Go to the documentation of this file.
1
// ****************************************************************************
2
// semihost_hardfault.c
3
// - Provides hard fault handler to allow semihosting code not
4
// to hang application when debugger not connected.
5
//
6
// ****************************************************************************
7
// Copyright 2017-2020 NXP
8
// All rights reserved.
9
//
10
// NXP Confidential. This software is owned or controlled by NXP and may only be
11
// used strictly in accordance with the applicable license terms.
12
//
13
// By expressly accepting such terms or by downloading, installing, activating
14
// and/or otherwise using the software, you are agreeing that you have read, and
15
// that you agree to comply with and are bound by, such license terms.
16
//
17
// If you do not agree to be bound by the applicable license terms, then you may not
18
// retain, install, activate or otherwise use the software.
19
20
// ****************************************************************************
21
//
22
// ===== DESCRIPTION =====
23
//
24
// One of the issues with applications that make use of semihosting operations
25
// (such as printf calls) is that the code will not execute correctly when the
26
// debugger is not connected. Generally this will show up with the application
27
// appearing to just hang. This may include the application running from reset
28
// or powering up the board (with the application already in FLASH), and also
29
// as the application failing to continue to execute after a debug session is
30
// terminated.
31
//
32
// The problem here is that the "bottom layer" of the semihosted variants of
33
// the C library, semihosting is implemented by a "BKPT 0xAB" instruction.
34
// When the debug tools are not connected, this instruction triggers a hard
35
// fault - and the default hard fault handler within an application will
36
// typically just contains an infinite loop - causing the application to
37
// appear to have hang when no debugger is connected.
38
//
39
// The below code provides an example hard fault handler which instead looks
40
// to see what the instruction that caused the hard fault was - and if it
41
// was a "BKPT 0xAB", then it instead returns back to the user application.
42
//
43
// In most cases this will allow applications containing semihosting
44
// operations to execute (to some degree) when the debugger is not connected.
45
//
46
// == NOTE ==
47
//
48
// Correct execution of the application containing semihosted operations
49
// which are vectored onto this hard fault handler cannot be guaranteed. This
50
// is because the handler may not return data or return codes that the higher
51
// level C library code or application code expects. This hard fault handler
52
// is meant as a development aid, and it is not recommended to leave
53
// semihosted code in a production build of your application!
54
//
55
// ****************************************************************************
56
57
// Allow handler to be removed by setting a define (via command line)
58
#if !defined (__SEMIHOST_HARDFAULT_DISABLE)
59
60
__attribute__
((naked))
61
void
HardFault_Handler
(
void
){
62
__asm(
".syntax unified\n"
63
// Check which stack is in use
64
"MOVS R0, #4 \n"
65
"MOV R1, LR \n"
66
"TST R0, R1 \n"
67
"BEQ _MSP \n"
68
"MRS R0, PSP \n"
69
"B _process \n"
70
"_MSP: \n"
71
"MRS R0, MSP \n"
72
// Load the instruction that triggered hard fault
73
"_process: \n"
74
"LDR R1,[R0,#24] \n"
75
"LDRH R2,[r1] \n"
76
// Semihosting instruction is "BKPT 0xAB" (0xBEAB)
77
"LDR R3,=0xBEAB \n"
78
"CMP R2,R3 \n"
79
"BEQ _semihost_return \n"
80
// Wasn't semihosting instruction so enter infinite loop
81
"B . \n"
82
// Was semihosting instruction, so adjust location to
83
// return to by 1 instruction (2 bytes), then exit function
84
"_semihost_return: \n"
85
"ADDS R1,#2 \n"
86
"STR R1,[R0,#24] \n"
87
// Set a return value from semihosting operation.
88
// 32 is slightly arbitrary, but appears to allow most
89
// C Library IO functions sitting on top of semihosting to
90
// continue to operate to some degree
91
"MOVS R1,#32 \n"
92
"STR R1,[ R0,#0 ] \n"
// R0 is at location 0 on stack
93
// Return from hard fault handler to application
94
"BX LR \n"
95
".syntax divided\n"
) ;
96
}
97
98
#endif
99
__attribute__
__attribute__((naked))
Definition:
semihost_hardfault.c:60
HardFault_Handler
WEAK void HardFault_Handler(void)
Definition:
startup_mimxrt1052.c:735
picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50