title: Cortex M0 HardFault_Handler and getting the fault address
body: <p>I'm having a HardFault when executing my program. I've found dozens of ways to get PC's value, but I'm using Keil uVision 5 and none of them has worked.</p>
  
  <p>As far as I know I'm not in a multitasking context, and PSP contains 0xFFFFFFF1, so adding 24 to it would cause overflow.</p>
  
  <p>Here's what I've managed to get working (as in, it compiles and execute):</p>
  
  <pre><code>enum { r0, r1, r2, r3, r12, lr, pc, psr};
  
  extern "C" void HardFault_Handler()
  {
    uint32_t *stack;
    __ASM volatile("MRS stack, MSP");
  
    stack += 0x20;
  
    pc = stack[pc];
    psr = stack[psr];
    __ASM volatile("BKPT #01");   
  }
  </code></pre>
  
  <p>Note the "+= 0x20", which is here to compensate for C function stack.</p>
  
  <p>Whenever I read the PC's value, it's 0.
  Would anyone have working code for that?</p>
  
  <p>Otherwise, here's how I do it manually:</p>
  
  <ul>
  <li>Put a breakpoint on HardFault_Handler (the original one)</li>
  <li>When it breaks, look as MSP</li>
  <li>Add 24 to its value.</li>
  <li>Dump memory at that address.
  And there it is, 0x00000000.</li>
  </ul>
  
  <p>What am I doing wrong?</p>