cancel
Showing results for 
Search instead for 
Did you mean: 

When MotionMC calls back to MotionMC_SaveCalInNVM, reading from the supplied pointer causes a Hard Fault.

Rstua.1
Senior

My call back code looks like this:

char MotionMC_SaveCalInNVM(unsigned short int datasize, unsigned int *data)
{
    // This function is called when a call to MothionMC_Initialize(sampletime,0) is made with
    // data set to zero.
    uint8_t i;
    unsigned int *data_local;
    data_local = data;
    for(i = 0; i < datasize; i++)
    {
        otter_mag_cal_nvm[i] = *data_local;
        data_local++;
    }
    return (char)0; 
}

...in debug, this code hard faults when the loop reaches 106. Far short of the st.com's MotionMC passed "datasize" of 184. The code has been tested to make sure the array otter_mag_cal_nvm[] is not at fault. For this testing the line "otter_mag_cal_nvm[0] = *data_local;" was used instead and the hard faults still occurred on the 106th iteration.

4 REPLIES 4
Miroslav BATEK
ST Employee

The datazise is number of bytes.

It is better to use memcpy command to copy data from the source to otter_mag_cal_nvm array. Please make sure the otter_mag_cal_nvm is big enough.

Rstua.1
Senior

...

Hi @Miroslav BATEK​ ,

I have changed to using memcpy.

I am reading the calibration data provided when MotionMC calls back to the MotionMC_SaveCalInNVM() and used the IDE workspace Memory / Export feature to grab the compass calibration data and add it to a header file (our Nonvolatile Memory feature is not ready). Then, upon power up, I write the header file back into the structure provided by the call to the call back function MotionMC_LoadCalFromNVM().

But even after doing this my compass still requires the normal calibration maneuvers over the normal amount of time. So, I assume I am doing something wrong. I have read the compass calibration data several times and most values do are the same. Some values are different. But I assume this is because of subtle differences in how the compass was calibrated each time.

I was expecting the compass to report a 3 or "calibration good" immediately after the calibration data was written back. What might I be doing wrong?

-thanks

Martin B
ST Employee

In fact unsigned int *data is bit misleading. The data is pointing to library internal data structure and should be saved/loaded as raw 184 bytes. Try to use:

#include <string.h>
 
...
 
uint8_t CalParams[184];
 
...
 
char MotionMC_LoadCalFromNVM(unsigned short int datasize, unsigned int *data)
{
  memcpy(data, CalParams, datasize);
  return 0;
}
 
char MotionMC_SaveCalInNVM(unsigned short int datasize, unsigned int *data)
{
  memcpy(CalParams, data, datasize);
  return 0;
}