title: stm32f4 HardFault_Handler - need debugging advice
body: <p>I'm working on a project based on the stm32f4discovery board using IAR Embedded Workbench (though I'm very close to the 32kb limit on the free version so I'll have to find something else soon). This is a learning project for me and so far I've been able to solve most of my issues with a few google searches and a lot of trial and error. But this is the first time I've encountered a run-time error that doesn't appear to be caused by a problem with my logic and I'm pretty stuck. Any general debugging strategy advice is welcome.</p>
  
  <p>So here's what happens. I have an interrupt on a button; each time the button is pressed, the callback function runs my <code>void cal_acc(uint16_t* data)</code> function defined in <code>stm32f4xx_it.c</code>. This function gathers some data, and on the 6th press, it calls my <code>void gn(float32_t* data, float32_t* beta)</code> function. Eventually, two functions are called, <code>gn_resids</code> and <code>gn_jacobian</code>. The functions are very similar in structure. Both take in 3 pointers to 3 arrays of floats and then modify the values of the first array based on the second two. Unfortunately, when the second function <code>gn_jacobian</code> exits, I get the HardFault.</p>
  
  <p>Please look at the link (<a href="https://i.stack.imgur.com/YbjvV.png" rel="nofollow noreferrer">code structure</a>) for a picture showing how the program runs up to the fault.</p>
  
  <p>Thank you very much! I appreciate any advice or guidance you can give me,</p>
  
  <p>-Ben</p>
  
  <p>Extra info that might be helpful below:</p>
  
  <p>Running in debug mode, I can step into the function and run through all the lines click by click and it's OK. But as soon as I run the last line and it should exit and move on to the next line in the function where it was called, it crashes. I have also tried rearranging the order of the calls around this function and it is always this one that crashes.</p>
  
  <p>I had been getting a similar crash on the first function <code>gn_resids</code> when one of the input pointers pointed to an array that was not defined as "static". But now all the arrays are static and I'm quite confused - especially since I can't tell what is different between the <code>gn_resids</code> function that works and the <code>gn_jacobian</code> function that does not work.</p>
  
  <p><code>acc1beta</code> is declared as a float array at the beginning of <code>main.c</code> and then also as extern <code>float32_t acc1beta[6]</code> at the top of <code>stm32f4xx_it.c</code>. I want it as a global variable; there is probably a better way to do this, but it's been working so far with many other variables defined in the same way.</p>
  
  <p>Here's a screenshot of what I see when it crashes during debug (after I pause the session) <a href="https://i.stack.imgur.com/ADVaM.png" rel="nofollow noreferrer">IAR view at crash</a></p>
  
  <p>EDIT: I changed the code of gn_step to look like this for a test so that it just runs gn_resids twice and it crashes as soon as it gets to the second call - I can't even step into it. gn_jacobian is not the problem. </p>
  
  <pre><code>void gn_step(float32_t* data, float32_t* beta) {
    static float32_t resids[120];
    gn_resids(resids, data, beta);
    arm_matrix_instance_f32 R;
    arm_mat_init_f32(&amp;R, 120, 1, resids);
  
  //  static float32_t J_f32[720];
  //  gn_jacobian(J_f32, data, beta);
    static float32_t J_f32[120];
    gn_resids(J_f32, data, beta);
    arm_matrix_instance_f32 J;
    arm_mat_init_f32(&amp;J, 120, 1, J_f32);
  </code></pre>