Bug in lis2ds12_fifo_data_level_get implementation

Open Beginner friendly
#1 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
c
Domain
embedded-iot

Research direction

Start at the lis2ds12_fifo_data_level_get implementation and consult the LIS2DS12 datasheet sections 8.24 and 8.25. Verify the FIFO sample count comes from the samples register rather than the threshold register, then check the driver’s available build or validation process to confirm the corrected behavior.

Written by the indexing model from the issue text.

Description

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.

Dominant language
C
Stars
6
Forks
1
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.