]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_M33F_NXP_LPC55S69_MCUXpresso/NXP_Code/source/semihost_hardfault.c
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS / Demo / CORTEX_MPU_M33F_NXP_LPC55S69_MCUXpresso / NXP_Code / source / semihost_hardfault.c
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 // Software that is described herein is for illustrative purposes only
11 // which provides customers with programming information regarding the
12 // NXP Cortex-M based MCUs. This software is supplied "AS IS" without any
13 // warranties of any kind, and NXP Semiconductors and its licensor disclaim any
14 // and all warranties, express or implied, including all implied warranties of
15 // merchantability, fitness for a particular purpose and non-infringement of
16 // intellectual property rights.  NXP Semiconductors assumes no responsibility
17 // or liability for the use of the software, conveys no license or rights under
18 // any patent, copyright, mask work right, or any other intellectual property
19 // rights in or to any products. NXP Semiconductors reserves the right to make
20 // changes in the software without notification. NXP Semiconductors also makes
21 // no representation or warranty that such application will be suitable for the
22 // specified use without further testing or modification.
23 //
24 // Permission to use, copy, modify, and distribute this software and its
25 // documentation is hereby granted, under NXP Semiconductors' and its
26 // licensor's relevant copyrights in the software, without fee, provided that it
27 // is used in conjunction with NXP Semiconductors microcontrollers.  This
28 // copyright, permission, and disclaimer notice must appear in all copies of
29 // this code.
30 // ****************************************************************************
31 //
32 //                       ===== DESCRIPTION =====
33 //
34 // One of the issues with applications that make use of semihosting operations
35 // (such as printf calls) is that the code will not execute correctly when the
36 // debugger is not connected. Generally this will show up with the application
37 // appearing to just hang. This may include the application running from reset
38 // or powering up the board (with the application already in FLASH), and also
39 // as the application failing to continue to execute after a debug session is
40 // terminated.
41 //
42 // The problem here is that the "bottom layer" of the semihosted variants of
43 // the C library, semihosting is implemented by a "BKPT 0xAB" instruction.
44 // When the debug tools are not connected, this instruction triggers a hard
45 // fault - and the default hard fault handler within an application will
46 // typically just contains an infinite loop - causing the application to
47 // appear to have hang when no debugger is connected.
48 //
49 // The below code provides an example hard fault handler which instead looks
50 // to see what the instruction that caused the hard fault was - and if it
51 // was a "BKPT 0xAB", then it instead returns back to the user application.
52 //
53 // In most cases this will allow applications containing semihosting
54 // operations to execute (to some degree) when the debugger is not connected.
55 //
56 // == NOTE ==
57 //
58 // Correct execution of the application containing semihosted operations
59 // which are vectored onto this hard fault handler cannot be guaranteed. This
60 // is because the handler may not return data or return codes that the higher
61 // level C library code or application code expects. This hard fault handler
62 // is meant as a development aid, and it is not recommended to leave
63 // semihosted code in a production build of your application!
64 //
65 // ****************************************************************************
66
67 // Allow handler to be removed by setting a define (via command line)
68 #if !defined (__SEMIHOST_HARDFAULT_DISABLE)
69
70 __attribute__((naked))
71 void HardFault_Handler(void){
72     __asm(  ".syntax unified\n"
73         // Check which stack is in use
74             "MOVS   R0, #4  \n"
75             "MOV    R1, LR  \n"
76             "TST    R0, R1  \n"
77             "BEQ    _MSP    \n"
78             "MRS    R0, PSP \n"
79             "B  _process      \n"
80             "_MSP:  \n"
81             "MRS    R0, MSP \n"
82         // Load the instruction that triggered hard fault
83         "_process:     \n"
84             "LDR    R1,[R0,#24] \n"
85             "LDRH    R2,[r1] \n"
86         // Semihosting instruction is "BKPT 0xAB" (0xBEAB)
87             "LDR    R3,=0xBEAB \n"
88             "CMP     R2,R3 \n"
89             "BEQ    _semihost_return \n"
90         // Wasn't semihosting instruction so enter infinite loop
91             "B . \n"
92         // Was semihosting instruction, so adjust location to
93         // return to by 1 instruction (2 bytes), then exit function
94         "_semihost_return: \n"
95             "ADDS    R1,#2 \n"
96             "STR    R1,[R0,#24] \n"
97         // Set a return value from semihosting operation.
98         // 32 is slightly arbitrary, but appears to allow most
99         // C Library IO functions sitting on top of semihosting to
100         // continue to operate to some degree
101                     "MOVS   R1,#32 \n"
102                     "STR R1,[ R0,#0 ] \n" // R0 is at location 0 on stack
103         // Return from hard fault handler to application
104             "BX LR \n"
105         ".syntax divided\n") ;
106 }
107
108 #endif
109