Skip to content

Commit d25d385

Browse files
str4t0mcfriedt
authored andcommitted
drivers: sensor: sht3xd: use sys functions for crc and byteorder
replace custom crc8 with sys/crc8 use sys_put/sys_get helpers for byteorder specific operations Signed-off-by: Thomas Stranger <[email protected]>
1 parent 22c8d99 commit d25d385

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

drivers/sensor/sht3xd/sht3xd.c

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <kernel.h>
1212
#include <drivers/sensor.h>
1313
#include <sys/__assert.h>
14+
#include <sys/byteorder.h>
15+
#include <sys/crc.h>
1416
#include <logging/log.h>
1517

1618
#include "sht3xd.h"
@@ -42,29 +44,17 @@ static const int measure_wait[3] = {
4244
*/
4345
static uint8_t sht3xd_compute_crc(uint16_t value)
4446
{
45-
uint8_t buf[2] = { value >> 8, value & 0xFF };
46-
uint8_t crc = 0xFF;
47-
uint8_t polynom = 0x31;
48-
int i, j;
49-
50-
for (i = 0; i < 2; ++i) {
51-
crc = crc ^ buf[i];
52-
for (j = 0; j < 8; ++j) {
53-
if (crc & 0x80) {
54-
crc = (crc << 1) ^ polynom;
55-
} else {
56-
crc = crc << 1;
57-
}
58-
}
59-
}
47+
uint8_t buf[2];
6048

61-
return crc;
49+
sys_put_be16(value, buf);
50+
return crc8(buf, 2, 0x31, 0xFF, false);
6251
}
6352

6453
int sht3xd_write_command(const struct device *dev, uint16_t cmd)
6554
{
66-
uint8_t tx_buf[2] = { cmd >> 8, cmd & 0xFF };
55+
uint8_t tx_buf[2];
6756

57+
sys_put_be16(cmd, tx_buf);
6858
return i2c_write(sht3xd_i2c_device(dev), tx_buf, sizeof(tx_buf),
6959
sht3xd_i2c_address(dev));
7060
}
@@ -73,10 +63,8 @@ int sht3xd_write_reg(const struct device *dev, uint16_t cmd, uint16_t val)
7363
{
7464
uint8_t tx_buf[5];
7565

76-
tx_buf[0] = cmd >> 8;
77-
tx_buf[1] = cmd & 0xFF;
78-
tx_buf[2] = val >> 8;
79-
tx_buf[3] = val & 0xFF;
66+
sys_put_be16(cmd, &tx_buf[0]);
67+
sys_put_be16(val, &tx_buf[2]);
8068
tx_buf[4] = sht3xd_compute_crc(val);
8169

8270
return i2c_write(sht3xd_i2c_device(dev), tx_buf, sizeof(tx_buf),
@@ -110,10 +98,9 @@ static int sht3xd_sample_fetch(const struct device *dev,
11098
}
11199
#endif
112100
#ifdef CONFIG_SHT3XD_PERIODIC_MODE
113-
uint8_t tx_buf[2] = {
114-
SHT3XD_CMD_FETCH >> 8,
115-
SHT3XD_CMD_FETCH & 0xFF
116-
};
101+
uint8_t tx_buf[2];
102+
103+
sys_put_be16(SHT3XD_CMD_FETCH, tx_buf);
117104

118105
if (i2c_write_read(i2c, address, tx_buf, sizeof(tx_buf),
119106
rx_buf, sizeof(rx_buf)) < 0) {
@@ -122,13 +109,13 @@ static int sht3xd_sample_fetch(const struct device *dev,
122109
}
123110
#endif
124111

125-
t_sample = (rx_buf[0] << 8) | rx_buf[1];
112+
t_sample = sys_get_be16(&rx_buf[0]);
126113
if (sht3xd_compute_crc(t_sample) != rx_buf[2]) {
127114
LOG_DBG("Received invalid temperature CRC!");
128115
return -EIO;
129116
}
130117

131-
rh_sample = (rx_buf[3] << 8) | rx_buf[4];
118+
rh_sample = sys_get_be16(&rx_buf[3]);
132119
if (sht3xd_compute_crc(rh_sample) != rx_buf[5]) {
133120
LOG_DBG("Received invalid relative humidity CRC!");
134121
return -EIO;

0 commit comments

Comments
 (0)