<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Debugging a Hard Fault in LPCXpresso IDE FAQs</title>
    <link>https://community.nxp.com/t5/LPCXpresso-IDE-FAQs/Debugging-a-Hard-Fault/m-p/469845#M49</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;H2 id="toc-hId-1707656514" style="font-family: Lato, sans-serif; color: #333333;"&gt;&lt;A name="Displaying_hard_fault_information_in_the_debugger" style="color: #5e89c1; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent;" target="_blank"&gt;&lt;/A&gt;&lt;SPAN style="color: #eb7a3d;"&gt;Displaying hard fault information in the debugger&lt;/SPAN&gt;&lt;/H2&gt;&lt;P style="min- padding: 0px;"&gt;&lt;/P&gt;&lt;P&gt;If you are getting a hard fault when executing your code under the debugger, the VECTPC pseudo-register tells you the PC of your application when the Hard Fault occurred. By clicking on the VECTPC register in the Register View, additional information is displayed, including the PC that caused the fault, and its matching source file/location (if available) and various Fault registers indicating the exact cause of the Fault, as shown in the screenshot below.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="color: #646464; font-family: Arial, sans-serif; font-size: 12px;"&gt;&lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=https%3A%2F%2Fwww.lpcware.com%2Fsystem%2Ffiles%2Fimages%2FHardFaultDisplay.png" rel="nofollow noopener noreferrer" target="_blank"&gt;&lt;IMG alt="" class="bb-image jive-image" src="https://www.lpcware.com/system/files/images/HardFaultDisplay.png" /&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Possible reasons for a Hard Fault include:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Trying to read or write to an on-chip peripheral that is powered down or not being clocked&lt;/LI&gt;&lt;LI&gt;Stack corruption - for example, overwriting the stack with data&lt;/LI&gt;&lt;LI&gt;Calling a function pointer with an invalid address&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In some circumstances, a hard fault might be caused early on during the initialisation of the system before the breakpoint on main() is hit. This can mean that the fault is triggered before the debugger can take action to display VECTPC.If this happens, try setting a breakpoint in the startup code (ie inside cr_startup_lpcXX.c, say on the call to SystemInit() ) - this might then allow your code to load without the hard fault being triggered. You should then be able to single step / run until the cause of the hard fault is hit. You will then see VECTPC displayed.For further information on fault handling, review the following ARM Documentation:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Cortex-M4: &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Finfocenter.arm.com%2Fhelp%2Ftopic%2Fcom.arm.doc.dui0553a%2FBabcefea.html" rel="nofollow noopener noreferrer" target="_blank"&gt;http://infocenter.arm.com/help/topic/com.arm.doc.dui0553a/Babcefea.html&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;Cortex-M3: &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Finfocenter.arm.com%2Fhelp%2Findex.jsp%3Ftopic%3D%2Fcom.arm.doc.dui0552a%2FBabcefea.html" rel="nofollow noopener noreferrer" target="_blank"&gt;http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0552a/Babcefea.html&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;Cortex-M0+: &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Finfocenter.arm.com%2Fhelp%2Findex.jsp%3Ftopic%3D%2Fcom.arm.doc.dui0662b%2FBabcefea.html" rel="nofollow noopener noreferrer" target="_blank"&gt;http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/Babcefea.html&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;Cortex-M0: &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Finfocenter.arm.com%2Fhelp%2Findex.jsp%3Ftopic%3D%2Fcom.arm.doc.dui0497a%2FBabcefea.html" rel="nofollow noopener noreferrer" target="_blank"&gt;http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0497a/Babcefea.html&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P style="min- padding: 0px; color: #646464; font-family: Arial, sans-serif; font-size: 12px;"&gt;&lt;/P&gt;&lt;P style="min- padding: 0px; color: #646464; font-family: Arial, sans-serif; font-size: 12px;"&gt;&lt;/P&gt;&lt;H2 id="toc-hId--844500447" style="font-family: Lato, sans-serif; color: #333333;"&gt;&lt;A name="Extending_the_hard_fault_handler" style="color: #5e89c1; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent;" target="_blank"&gt;&lt;/A&gt;&lt;SPAN style="color: #eb7a3d;"&gt;Extending the hard fault handler&lt;/SPAN&gt;&lt;/H2&gt;&lt;P style="min- padding: 0px;"&gt;&lt;/P&gt;&lt;P&gt;It is possible to provide an extended hard fault handler in your application, for example to provide more detailed information as to the state of the MCU when the hard fault condition is encountered. The below code shows an example of how this can be done:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;/**&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * HardFault_HandlerAsm:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * Alternative Hard Fault handler to help debug the reason for a fault.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * To use, edit the vector table to reference this function in the HardFault vector&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * This code is suitable for Cortex-M3 and Cortex-M0 cores&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// Use the 'naked' attribute so that C stacking is not used.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;__attribute__((naked))&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;void HardFault_HandlerAsm(void){&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;/*&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;* Get the appropriate stack pointer, depending on our mode,&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;* and use it as the parameter to the C handler. This function&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;* will never return&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;*/&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;__asm( ".syntax unified\n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"MOVS R0, #4 \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"MOV R1, LR \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"TST R0, R1 \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"BEQ _MSP \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"MRS R0, PSP \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"B HardFault_HandlerC \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"_MSP: \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"MRS R0, MSP \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"B HardFault_HandlerC \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;".syntax divided\n") ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;/**&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * HardFaultHandler_C:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * This is called from the HardFault_HandlerAsm with a pointer the Fault stack&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * as the parameter. We can then read the values from the stack and place them&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * into local variables for ease of reading.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * We then read the various Fault Status and Address Registers to help decode&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * cause of the fault.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * The function ends with a BKPT instruction to force control back into the debugger&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;void HardFault_HandlerC(unsigned long *hardfault_args){&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_r0 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_r1 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_r2 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_r3 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_r12 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_lr ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_pc ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_psr ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long _CFSR ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long _HFSR ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long _DFSR ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long _AFSR ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long _BFAR ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long _MMAR ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_r0 = ((unsigned long)hardfault_args[0]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_r1 = ((unsigned long)hardfault_args[1]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_r2 = ((unsigned long)hardfault_args[2]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_r3 = ((unsigned long)hardfault_args[3]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_r12 = ((unsigned long)hardfault_args[4]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_lr = ((unsigned long)hardfault_args[5]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_pc = ((unsigned long)hardfault_args[6]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_psr = ((unsigned long)hardfault_args[7]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;&amp;nbsp; // Configurable Fault Status Register&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;&amp;nbsp; // Consists of MMSR, BFSR and UFSR&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;_CFSR = (*((volatile unsigned long *)(0xE000ED28))) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// Hard Fault Status Register&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;_HFSR = (*((volatile unsigned long *)(0xE000ED2C))) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// Debug Fault Status Register&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;_DFSR = (*((volatile unsigned long *)(0xE000ED30))) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// Auxiliary Fault Status Register&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;_AFSR = (*((volatile unsigned long *)(0xE000ED3C))) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// Read the Fault Address Registers. These may not contain valid values.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// Check BFARVALID/MMARVALID to see if they are valid values&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// MemManage Fault Address Register&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;_MMAR = (*((volatile unsigned long *)(0xE000ED34))) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// Bus Fault Address Register&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;_BFAR = (*((volatile unsigned long *)(0xE000ED38))) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;__asm("BKPT #0\n") ; // Break into the debugger&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;&lt;STRONG&gt;}&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note that depending upon tools version and target MCU, you may encounter an assembler error when building the above code along the lines of:&lt;/P&gt;&lt;PRE class="bb-code-block" style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;Error: instruction not supported in Thumb16 mode -- `adds r4,r4,#12' &lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you do, then deleting the `".syntax unified\n"` and `".syntax divided\n"` in the inline assembler of the function `HardFault_HandlerAsm()` should solve the problem.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 31 Mar 2016 18:17:18 GMT</pubDate>
    <dc:creator>lpcware-support</dc:creator>
    <dc:date>2016-03-31T18:17:18Z</dc:date>
    <item>
      <title>Debugging a Hard Fault</title>
      <link>https://community.nxp.com/t5/LPCXpresso-IDE-FAQs/Debugging-a-Hard-Fault/m-p/469845#M49</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;H2 id="toc-hId-1707656514" style="font-family: Lato, sans-serif; color: #333333;"&gt;&lt;A name="Displaying_hard_fault_information_in_the_debugger" style="color: #5e89c1; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent;" target="_blank"&gt;&lt;/A&gt;&lt;SPAN style="color: #eb7a3d;"&gt;Displaying hard fault information in the debugger&lt;/SPAN&gt;&lt;/H2&gt;&lt;P style="min- padding: 0px;"&gt;&lt;/P&gt;&lt;P&gt;If you are getting a hard fault when executing your code under the debugger, the VECTPC pseudo-register tells you the PC of your application when the Hard Fault occurred. By clicking on the VECTPC register in the Register View, additional information is displayed, including the PC that caused the fault, and its matching source file/location (if available) and various Fault registers indicating the exact cause of the Fault, as shown in the screenshot below.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="color: #646464; font-family: Arial, sans-serif; font-size: 12px;"&gt;&lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=https%3A%2F%2Fwww.lpcware.com%2Fsystem%2Ffiles%2Fimages%2FHardFaultDisplay.png" rel="nofollow noopener noreferrer" target="_blank"&gt;&lt;IMG alt="" class="bb-image jive-image" src="https://www.lpcware.com/system/files/images/HardFaultDisplay.png" /&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Possible reasons for a Hard Fault include:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Trying to read or write to an on-chip peripheral that is powered down or not being clocked&lt;/LI&gt;&lt;LI&gt;Stack corruption - for example, overwriting the stack with data&lt;/LI&gt;&lt;LI&gt;Calling a function pointer with an invalid address&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In some circumstances, a hard fault might be caused early on during the initialisation of the system before the breakpoint on main() is hit. This can mean that the fault is triggered before the debugger can take action to display VECTPC.If this happens, try setting a breakpoint in the startup code (ie inside cr_startup_lpcXX.c, say on the call to SystemInit() ) - this might then allow your code to load without the hard fault being triggered. You should then be able to single step / run until the cause of the hard fault is hit. You will then see VECTPC displayed.For further information on fault handling, review the following ARM Documentation:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Cortex-M4: &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Finfocenter.arm.com%2Fhelp%2Ftopic%2Fcom.arm.doc.dui0553a%2FBabcefea.html" rel="nofollow noopener noreferrer" target="_blank"&gt;http://infocenter.arm.com/help/topic/com.arm.doc.dui0553a/Babcefea.html&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;Cortex-M3: &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Finfocenter.arm.com%2Fhelp%2Findex.jsp%3Ftopic%3D%2Fcom.arm.doc.dui0552a%2FBabcefea.html" rel="nofollow noopener noreferrer" target="_blank"&gt;http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0552a/Babcefea.html&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;Cortex-M0+: &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Finfocenter.arm.com%2Fhelp%2Findex.jsp%3Ftopic%3D%2Fcom.arm.doc.dui0662b%2FBabcefea.html" rel="nofollow noopener noreferrer" target="_blank"&gt;http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0662b/Babcefea.html&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;Cortex-M0: &lt;A _jive_internal="true" href="https://community.nxp.com/external-link.jspa?url=http%3A%2F%2Finfocenter.arm.com%2Fhelp%2Findex.jsp%3Ftopic%3D%2Fcom.arm.doc.dui0497a%2FBabcefea.html" rel="nofollow noopener noreferrer" target="_blank"&gt;http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0497a/Babcefea.html&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P style="min- padding: 0px; color: #646464; font-family: Arial, sans-serif; font-size: 12px;"&gt;&lt;/P&gt;&lt;P style="min- padding: 0px; color: #646464; font-family: Arial, sans-serif; font-size: 12px;"&gt;&lt;/P&gt;&lt;H2 id="toc-hId--844500447" style="font-family: Lato, sans-serif; color: #333333;"&gt;&lt;A name="Extending_the_hard_fault_handler" style="color: #5e89c1; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: transparent;" target="_blank"&gt;&lt;/A&gt;&lt;SPAN style="color: #eb7a3d;"&gt;Extending the hard fault handler&lt;/SPAN&gt;&lt;/H2&gt;&lt;P style="min- padding: 0px;"&gt;&lt;/P&gt;&lt;P&gt;It is possible to provide an extended hard fault handler in your application, for example to provide more detailed information as to the state of the MCU when the hard fault condition is encountered. The below code shows an example of how this can be done:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;/**&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * HardFault_HandlerAsm:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * Alternative Hard Fault handler to help debug the reason for a fault.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * To use, edit the vector table to reference this function in the HardFault vector&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * This code is suitable for Cortex-M3 and Cortex-M0 cores&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// Use the 'naked' attribute so that C stacking is not used.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;__attribute__((naked))&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;void HardFault_HandlerAsm(void){&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;/*&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;* Get the appropriate stack pointer, depending on our mode,&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;* and use it as the parameter to the C handler. This function&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;* will never return&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;*/&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;__asm( ".syntax unified\n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"MOVS R0, #4 \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"MOV R1, LR \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"TST R0, R1 \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"BEQ _MSP \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"MRS R0, PSP \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"B HardFault_HandlerC \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"_MSP: \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"MRS R0, MSP \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;"B HardFault_HandlerC \n"&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;".syntax divided\n") ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 60px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;/**&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * HardFaultHandler_C:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * This is called from the HardFault_HandlerAsm with a pointer the Fault stack&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * as the parameter. We can then read the values from the stack and place them&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * into local variables for ease of reading.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * We then read the various Fault Status and Address Registers to help decode&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * cause of the fault.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; * The function ends with a BKPT instruction to force control back into the debugger&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt; */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;void HardFault_HandlerC(unsigned long *hardfault_args){&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_r0 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_r1 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_r2 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_r3 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_r12 ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_lr ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_pc ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long stacked_psr ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long _CFSR ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long _HFSR ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long _DFSR ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long _AFSR ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long _BFAR ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;volatile unsigned long _MMAR ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_r0 = ((unsigned long)hardfault_args[0]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_r1 = ((unsigned long)hardfault_args[1]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_r2 = ((unsigned long)hardfault_args[2]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_r3 = ((unsigned long)hardfault_args[3]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_r12 = ((unsigned long)hardfault_args[4]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_lr = ((unsigned long)hardfault_args[5]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_pc = ((unsigned long)hardfault_args[6]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;stacked_psr = ((unsigned long)hardfault_args[7]) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;&amp;nbsp; // Configurable Fault Status Register&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;&amp;nbsp; // Consists of MMSR, BFSR and UFSR&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;_CFSR = (*((volatile unsigned long *)(0xE000ED28))) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// Hard Fault Status Register&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;_HFSR = (*((volatile unsigned long *)(0xE000ED2C))) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// Debug Fault Status Register&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;_DFSR = (*((volatile unsigned long *)(0xE000ED30))) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// Auxiliary Fault Status Register&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;_AFSR = (*((volatile unsigned long *)(0xE000ED3C))) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// Read the Fault Address Registers. These may not contain valid values.&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// Check BFARVALID/MMARVALID to see if they are valid values&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// MemManage Fault Address Register&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;_MMAR = (*((volatile unsigned long *)(0xE000ED34))) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;// Bus Fault Address Register&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;_BFAR = (*((volatile unsigned long *)(0xE000ED38))) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="padding-left: 30px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;__asm("BKPT #0\n") ; // Break into the debugger&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: courier new,courier;"&gt;&lt;STRONG&gt;}&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note that depending upon tools version and target MCU, you may encounter an assembler error when building the above code along the lines of:&lt;/P&gt;&lt;PRE class="bb-code-block" style="padding-left: 30px;"&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;Error: instruction not supported in Thumb16 mode -- `adds r4,r4,#12' &lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you do, then deleting the `".syntax unified\n"` and `".syntax divided\n"` in the inline assembler of the function `HardFault_HandlerAsm()` should solve the problem.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Mar 2016 18:17:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPCXpresso-IDE-FAQs/Debugging-a-Hard-Fault/m-p/469845#M49</guid>
      <dc:creator>lpcware-support</dc:creator>
      <dc:date>2016-03-31T18:17:18Z</dc:date>
    </item>
  </channel>
</rss>

