title: How can I debug "undefined instruction" faults for an LPC1788 in IAR Embedded Workbench?
body: <p>I'm developing an application for an LPC1788 (Cortex-M3) microcontroller. This application involves sending and receiving CAN messages, and I find that when I subject it to heavy load and twiddle my thumbs for 30-60 minutes, a hardfault will occur.</p>
  
  <p>I'm debugging the firmware through IAR Embedded Workbench 6.60.1.5104, and when this hardfault occurs it breaks at a weakly-linked default hardfault handler defined in <code>startup_LPC177x_8x.s</code>:</p>
  
  <pre><code>        PUBWEAK HardFault_Handler
          SECTION .text:CODE:REORDER(1)
  HardFault_Handler
          B HardFault_Handler
  </code></pre>
  
  <p>Unfortunately, the call stack only contains the address of this handler and not whichever part of code called into it (where the error occurred).</p>
  
  <p>The only useful information I've been able to gather is from the <code>UNDEFINSTR</code> bit being set in the <code>NVIC:CFSR</code> register. From the documentation:</p>
  
  <blockquote>
    <p>When this bit is set, the PC value stacked for the exception return
    points to the undefined instruction. An undefined instruction is an
    instruction that the processor cannot decode. Potential reasons:</p>
    
    <p>a) Use of instructions not supported in the Cortex-M device.<br>
    b) Bad or corrupted memory contents.</p>
  </blockquote>
  
  <p>I've read that the value of the program counter at the point the instruction was executed is stored in one of the exception stack registers, but I'm not sure how to access these from IAR.</p>
  
  <p>In case it's any help, I've included a screenshot containing some of the debugging details (<code>right click -&gt; view image</code> for a larger version):</p>
  
  <p><img src="https://i.stack.imgur.com/wtYhc.png" alt=""></p>