STMicroelectronics/lis2ds12-pid

Bug in lis2ds12_fifo_data_level_get implementation

Opened this issue · 0 comments

Current:

int32_t lis2ds12_fifo_data_level_get(stmdev_ctx_t *ctx, uint16_t *val)
{
  lis2ds12_fifo_ths_t fifo_ths;
  lis2ds12_fifo_src_t fifo_src;
  int32_t ret;

  ret = lis2ds12_read_reg(ctx, LIS2DS12_FIFO_THS, (uint8_t *)&fifo_ths, 1);

  if (ret == 0)
  {
    ret = lis2ds12_read_reg(ctx, LIS2DS12_FIFO_SRC, (uint8_t *)&fifo_src, 1);
    *val = fifo_src.diff;
    *val = (*val * 256U) +  fifo_ths.fth;
  }

  return ret;
}

Suggested:

int32_t lis2ds12_fifo_data_level_get(stmdev_ctx_t *ctx, uint16_t *val)
{
  uint8_t samples;
  lis2ds12_fifo_src_t fifo_src;
  int32_t ret;

  ret = lis2ds12_read_reg(ctx, LIS2DS12_FIFO_SAMPLES, &samples, 1);

  if (ret == 0)
  {
    ret = lis2ds12_read_reg(ctx, LIS2DS12_FIFO_SRC, (uint8_t *)&fifo_src, 1);
    *val = fifo_src.diff;
    *val = (*val * 256U) + samples;
  }

  return ret;
}

The problem with the current implementation is that it uses the LIS2DS12_FIFO_THS register to get the count of the samples in the FIFO. This is wrong because that register is the FIFO threshold. The function should be using LIS2DS12_FIFO_SAMPLES instead. This is all based on this datasheet, sections 8.24 and 8.25.