Skip to content

Commit

Permalink
drivers/sensor: lps22hh: Fix the raw to kPa sample conversion
Browse files Browse the repository at this point in the history
The lps22hh 24 bit raw sample is left aligned, which means that
it needs to be right-shifted by 8 before applying conversion.
Moreover the conversion has been simplified for clarity.

Fix #35871

Signed-off-by: Armando Visconti <armando.visconti@st.com>
  • Loading branch information
avisconti authored and galak committed Jun 3, 2021
1 parent 92afba6 commit 4e8c809
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions drivers/sensor/lps22hh/lps22hh.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ static int lps22hh_sample_fetch(const struct device *dev,
static inline void lps22hh_press_convert(struct sensor_value *val,
int32_t raw_val)
{
int32_t press_tmp = raw_val >> 8; /* raw value is left aligned (24 msb) */

/* Pressure sensitivity is 4096 LSB/hPa */
/* Convert raw_val to val in kPa */
val->val1 = (raw_val >> 12) / 10;
val->val2 = (raw_val >> 12) % 10 * 100000 +
(((int32_t)((raw_val) & 0x0FFF) * 100000L) >> 12);
/* Also convert hPa into kPa */

val->val1 = press_tmp / 40960;
val->val2 = (press_tmp % 40960) * 1000000 / 40960;
}

static inline void lps22hh_temp_convert(struct sensor_value *val,
Expand Down

1 comment on commit 4e8c809

@sevirip
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
I think it's better to have :

val->val1 = press_tmp / 4096;
val->val2 = (press_tmp % 4096) * 3125 / 128;

It's necessary to have the value in hPa instead of kPa to avoid side effet for the digits after the comma. You won more precision in having the value in hPa. It's really important for example to calculate a height (precision of 10 centimeters instead of 3 meters).

I say it here but i don't know were, maybe you can add this in the next realease.

Best regards,

Pierre-Yves

Please sign in to comment.