STMicroelectronics / lps22hb-pid

lps22hb platform independent driver based on Standard C language and compliant with MISRA standard
BSD 3-Clause "New" or "Revised" License
3 stars 2 forks source link

Missing burst read for stored fifo data. #2

Open qutefox opened 8 months ago

qutefox commented 8 months ago

I am unable to find any methods to burst read fifo stored data.

The datasheet mentions it here:

5 FIFO
The LPS22HB embeds 32 slots of 40-bit data FIFO to store the pressure and temperature
output values. This allows consistent power saving for the system, since the host
processor does not need to continuously poll data from the sensor, but it can wake up only
when needed and burst the significant data out from the FIFO. 
...
5.8 Retrieving data from FIFO
FIFO data is read from PRESS_OUT (Addr. reg 28h,29h,2Ah) and TEMP_OUT (Addr. reg
2Bh,2Ch).
Each time data is read from the FIFO, the oldest data are placed in the PRESS_OUT_XL
(28h), PRESS_OUT_L (29h), PRESS_OUT_H (2Ah), TEMP_OUT_L (2Bh) and
TEMP_OUT_H (2Ch) registers and both single-read and read-burst operations can be
used.
The device automatically updates the reading address and it rolls back to 28h when register
2Ch is reached. In order to read all FIFO levels in multiple byte reading, 160 bytes (5 output
registers by 32 levels) must be read.

So I would add the following methods: lps22hb_reg.h

typedef struct
{
  uint8_t bytes[5];
} lps22hb_fifo_output_data_t;

int32_t lps22hb_fifo_output_data_to_raw_pressure(lps22hb_fifo_output_data_t* val);

int16_t lps22hb_fifo_output_data_to_raw_temperature(lps22hb_fifo_output_data_t* val);

int32_t lps22hb_fifo_output_data_burst_get(stmdev_ctx_t *ctx, lps22hb_fifo_output_data_t *buff, uint8_t len);

lps22hb_reg.c

int32_t lps22hb_fifo_output_data_to_raw_pressure(lps22hb_fifo_output_data_t* val)
{
  int32_t pressure = val->bytes[2];
  pressure = (pressure * 256) + val->bytes[1];
  return (pressure * 256) + val->bytes[0];
}

int16_t lps22hb_fifo_output_data_to_raw_temperature(lps22hb_fifo_output_data_t* val)
{
  int16_t temperature = val->bytes[4];
  return (temperature * 256) + val->bytes[3];
}

/**
  * @brief  Burst read fifo output data.
  *
  * @param  ctx    Read / write interface definitions
  * @param  buff   Buffer that stores data read.
  * @param  len    How many data item to read from the fifo.
  * @retval        Interface status (MANDATORY: return 0 -> no Error).
  *
  */
int32_t lps22hb_fifo_output_data_burst_get(stmdev_ctx_t *ctx, lps22hb_fifo_output_data_t *buff, uint8_t len)
{
  return lps22hb_read_reg(ctx, LPS22HB_PRESS_OUT_XL, (uint8_t*)&buff[0], len * sizeof(lps22hb_fifo_output_data_t));
}

This way the end user can:

    uint8_t data_level = 0;
    lps22hb_fifo_data_level_get(&dev_ctx, &data_level);

    debug_print("Fifo data level: %d.\n", data_level);

    lps22hb_fifo_output_data_t fifo_buffer[32];
    lps22hb_fifo_output_data_burst_get(&dev_ctx, fifo_buffer, data_level);

    for (uint8_t i = 0 ; i < data_level ; ++i)
    {
        int32_t pressure = lps22hb_fifo_output_data_to_raw_pressure(&fifo_buffer[i]);
        int16_t temperature = lps22hb_fifo_output_data_to_raw_temperature(&fifo_buffer[i]);

        float pressure_hpa = lps22hb_from_lsb_to_hpa(pressure);
        float temperature_c = lps22hb_from_lsb_to_degc(temperature);

        debug_print("%d.) pressure: %f hPa, temperature: %f C\n", i, pressure_hpa, temperature_c);
    }
qutefox commented 8 months ago

The sizeof(lps22hb_fifo_output_data_t) might cause issues on other micros when padding is added to the lps22hb_fifo_output_data_t struct.

For example sizeof(lps22hb_fifo_output_data_t) returns 8, out of which 5 bytes are the data, 3 bytes are padding.

On my board (MAX32660) this is not an issue.

qutefox commented 8 months ago

PR: https://github.com/STMicroelectronics/lps22hb-pid/pull/3 Added some other stuff too.