1 // ****************************************************************************
2 // semihost_hardfault.c
3 // - Provides hard fault handler to allow semihosting code not
4 // to hang application when debugger not connected.
6 // ****************************************************************************
7 // Copyright 2017-2020 NXP
8 // All rights reserved.
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.
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
30 // ****************************************************************************
32 // ===== DESCRIPTION =====
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
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.
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.
53 // In most cases this will allow applications containing semihosting
54 // operations to execute (to some degree) when the debugger is not connected.
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!
65 // ****************************************************************************
67 // Allow handler to be removed by setting a define (via command line)
68 #if !defined (__SEMIHOST_HARDFAULT_DISABLE)
70 __attribute__((naked))
71 void HardFault_Handler(void){
72 __asm( ".syntax unified\n"
73 // Check which stack is in use
82 // Load the instruction that triggered hard fault
86 // Semihosting instruction is "BKPT 0xAB" (0xBEAB)
89 "BEQ _semihost_return \n"
90 // Wasn't semihosting instruction so enter infinite loop
92 // Was semihosting instruction, so adjust location to
93 // return to by 1 instruction (2 bytes), then exit function
94 "_semihost_return: \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
102 "STR R1,[ R0,#0 ] \n" // R0 is at location 0 on stack
103 // Return from hard fault handler to application
105 ".syntax divided\n") ;