Commit 9ea4cb96 authored by Armando Visconti's avatar Armando Visconti Committed by Anas Nashif
Browse files

drivers/sensor/st: Fix wrong data byte swap for be



A 16-bit value built using byte shifts and ORs from a given
couple of lsb and msb bytes will result to be the same on both
little-endian and big-endian architectures, e.g.

    uint8_t lsb, msb;
    int16_t val;

    /* val is the same number on both le and be archs, but has
       different layout in memory */
    val = (msb << 8) | lsb;

All the xyz_raw_get() APIs of stmemsc sensor module build the sensor
data using the above method and DO NOT hence require (it actually leads
to wrong values on big-endian machines) to use any le/be swap routines,
such as sys_le16_to_cpu().

Fix #75758

Signed-off-by: default avatarArmando Visconti <armando.visconti@st.com>
parent cdfa11ee
Loading
Loading
Loading
Loading
+8 −9
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@
#include <zephyr/drivers/i2c.h>
#include <zephyr/init.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/byteorder.h>
#include <string.h>
#include <zephyr/logging/log.h>

@@ -82,8 +81,8 @@ static int hts221_sample_fetch(const struct device *dev,
		return status;
	}

	data->rh_sample = sys_le16_to_cpu(buf[0] | (buf[1] << 8));
	data->t_sample = sys_le16_to_cpu(buf[2] | (buf[3] << 8));
	data->rh_sample = buf[0] | (buf[1] << 8);
	data->t_sample = buf[2] | (buf[3] << 8);

	return 0;
}
@@ -105,12 +104,12 @@ static int hts221_read_conversion_data(const struct device *dev)

	data->h0_rh_x2 = buf[0];
	data->h1_rh_x2 = buf[1];
	data->t0_degc_x8 = sys_le16_to_cpu(buf[2] | ((buf[5] & 0x3) << 8));
	data->t1_degc_x8 = sys_le16_to_cpu(buf[3] | ((buf[5] & 0xC) << 6));
	data->h0_t0_out = sys_le16_to_cpu(buf[6] | (buf[7] << 8));
	data->h1_t0_out = sys_le16_to_cpu(buf[10] | (buf[11] << 8));
	data->t0_out = sys_le16_to_cpu(buf[12] | (buf[13] << 8));
	data->t1_out = sys_le16_to_cpu(buf[14] | (buf[15] << 8));
	data->t0_degc_x8 = buf[2] | ((buf[5] & 0x3) << 8);
	data->t1_degc_x8 = buf[3] | ((buf[5] & 0xC) << 6);
	data->h0_t0_out = buf[6] | (buf[7] << 8);
	data->h1_t0_out = buf[10] | (buf[11] << 8);
	data->t0_out = buf[12] | (buf[13] << 8);
	data->t1_out = buf[14] | (buf[15] << 8);

	return 0;
}
+3 −4
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@

#include <zephyr/init.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util_macro.h>
#include <zephyr/logging/log.h>
#include <zephyr/drivers/sensor.h>
@@ -213,9 +212,9 @@ static int iis2dh_sample_fetch(const struct device *dev,
		return -EIO;
	}

	iis2dh->acc[0] = sys_le16_to_cpu(buf[0]);
	iis2dh->acc[1] = sys_le16_to_cpu(buf[1]);
	iis2dh->acc[2] = sys_le16_to_cpu(buf[2]);
	iis2dh->acc[0] = buf[0];
	iis2dh->acc[1] = buf[1];
	iis2dh->acc[2] = buf[2];

	return 0;
}
+3 −4
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@

#include <zephyr/init.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/logging/log.h>
#include <zephyr/drivers/sensor.h>

@@ -244,9 +243,9 @@ static int iis2dlpc_sample_fetch(const struct device *dev,
		shift = IIS2DLPC_SHIFT_PMOTHER;
	}

	iis2dlpc->acc[0] = sys_le16_to_cpu(buf[0]) >> shift;
	iis2dlpc->acc[1] = sys_le16_to_cpu(buf[1]) >> shift;
	iis2dlpc->acc[2] = sys_le16_to_cpu(buf[2]) >> shift;
	iis2dlpc->acc[0] = buf[0] >> shift;
	iis2dlpc->acc[1] = buf[1] >> shift;
	iis2dlpc->acc[2] = buf[2] >> shift;

	return 0;
}
+3 −4
Original line number Diff line number Diff line
@@ -15,7 +15,6 @@
#include <zephyr/device.h>
#include <zephyr/init.h>
#include <string.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/logging/log.h>

@@ -204,8 +203,8 @@ static int iis2iclx_sample_fetch_accel(const struct device *dev)
		return -EIO;
	}

	data->acc[0] = sys_le16_to_cpu(buf[0]);
	data->acc[1] = sys_le16_to_cpu(buf[1]);
	data->acc[0] = buf[0];
	data->acc[1] = buf[1];

	return 0;
}
@@ -222,7 +221,7 @@ static int iis2iclx_sample_fetch_temp(const struct device *dev)
		return -EIO;
	}

	data->temp_sample = sys_le16_to_cpu(buf);
	data->temp_sample = buf;

	return 0;
}
+2 −3
Original line number Diff line number Diff line
@@ -12,7 +12,6 @@

#include <zephyr/device.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/util.h>
#include <zephyr/kernel.h>
@@ -171,8 +170,8 @@ static int hts221_read_conv_data(const struct device *dev,

	ht->y0 = buf[0] / 2;
	ht->y1 = buf[1] / 2;
	ht->x0 = sys_le16_to_cpu(buf[6] | (buf[7] << 8));
	ht->x1 = sys_le16_to_cpu(buf[10] | (buf[11] << 8));
	ht->x0 = buf[6] | (buf[7] << 8);
	ht->x1 = buf[10] | (buf[11] << 8);

	return 0;
}
Loading