Skip to main content
Explorer II
March 20, 2021
Solved

lis2mdl driver computes the wrong hard iron offsets

  • March 20, 2021
  • 2 replies
  • 769 views

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;

}

    This topic has been closed for replies.
    Best answer by Clark Sann

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

    Thanks for your reply.

    2 replies

    ST Employee
    March 22, 2021

    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 SannAuthorAnswer
    Explorer II
    March 22, 2021

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

    Thanks for your reply.