cancel
Showing results for 
Search instead for 
Did you mean: 

lis2mdl driver computes the wrong hard iron offsets

Clark Sann
Senior

lis2mdl_reg.c has an error in the function int32_t lis2mdl_mag_user_offset_set(stmdev_ctx_t *ctx, int16_t *val).

The function incorrectly calculates the OFFSET_X_REG_H, OFFSET_Y_REG_H, and the OFFSET_Z_REG_H registers because it converts val[0] to an uint8_t before performing the calculation.

I believe the function should be modified as follows:

int32_t lis2mdl_mag_user_offset_set(stmdev_ctx_t *ctx, int16_t *val)

{

 uint8_t buff[6];

 int32_t ret;

 buff[1] = (uint8_t) (val[0] / 256U);

 buff[0] = (uint8_t) (val[0] - (buff[1] * 256U));

 buff[3] = (uint8_t) (val[1] / 256U);

 buff[2] = (uint8_t) (val[1] - (buff[1] * 256U));

 buff[5] = (uint8_t) (val[2] / 256U);

 buff[4] = (uint8_t) (val[2] - (buff[1] * 256U));

 ret = lis2mdl_write_reg(ctx, LIS2MDL_OFFSET_X_REG_L, buff, 6);

 return ret;

}

1 ACCEPTED SOLUTION

Accepted Solutions
Clark Sann
Senior

Yes, the change I made does work properly. I will open an issue on the GitHub page.

Thanks for your reply.

View solution in original post

2 REPLIES 2
Eleon BORLINI
ST Employee

Hi @Clark Sann​ ,

thank you for the notification. Does it work properly with your correction?

I suggest you to open an Issue in the Github page of the STMems_Standard_C_drivers.

This will facilitate our experts to fix the code. The typical due date for the correction is 1 week: please let me know if this issue is solved after this timeframe.

-Eleon

Clark Sann
Senior

Yes, the change I made does work properly. I will open an issue on the GitHub page.

Thanks for your reply.