11
11
#include <kernel.h>
12
12
#include <drivers/sensor.h>
13
13
#include <sys/__assert.h>
14
+ #include <sys/byteorder.h>
15
+ #include <sys/crc.h>
14
16
#include <logging/log.h>
15
17
16
18
#include "sht3xd.h"
@@ -42,29 +44,17 @@ static const int measure_wait[3] = {
42
44
*/
43
45
static uint8_t sht3xd_compute_crc (uint16_t value )
44
46
{
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 ];
60
48
61
- return crc ;
49
+ sys_put_be16 (value , buf );
50
+ return crc8 (buf , 2 , 0x31 , 0xFF , false);
62
51
}
63
52
64
53
int sht3xd_write_command (const struct device * dev , uint16_t cmd )
65
54
{
66
- uint8_t tx_buf [2 ] = { cmd >> 8 , cmd & 0xFF } ;
55
+ uint8_t tx_buf [2 ];
67
56
57
+ sys_put_be16 (cmd , tx_buf );
68
58
return i2c_write (sht3xd_i2c_device (dev ), tx_buf , sizeof (tx_buf ),
69
59
sht3xd_i2c_address (dev ));
70
60
}
@@ -73,10 +63,8 @@ int sht3xd_write_reg(const struct device *dev, uint16_t cmd, uint16_t val)
73
63
{
74
64
uint8_t tx_buf [5 ];
75
65
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 ]);
80
68
tx_buf [4 ] = sht3xd_compute_crc (val );
81
69
82
70
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,
110
98
}
111
99
#endif
112
100
#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 );
117
104
118
105
if (i2c_write_read (i2c , address , tx_buf , sizeof (tx_buf ),
119
106
rx_buf , sizeof (rx_buf )) < 0 ) {
@@ -122,13 +109,13 @@ static int sht3xd_sample_fetch(const struct device *dev,
122
109
}
123
110
#endif
124
111
125
- t_sample = ( rx_buf [0 ] << 8 ) | rx_buf [ 1 ] ;
112
+ t_sample = sys_get_be16 ( & rx_buf [0 ]) ;
126
113
if (sht3xd_compute_crc (t_sample ) != rx_buf [2 ]) {
127
114
LOG_DBG ("Received invalid temperature CRC!" );
128
115
return - EIO ;
129
116
}
130
117
131
- rh_sample = ( rx_buf [3 ] << 8 ) | rx_buf [ 4 ] ;
118
+ rh_sample = sys_get_be16 ( & rx_buf [3 ]) ;
132
119
if (sht3xd_compute_crc (rh_sample ) != rx_buf [5 ]) {
133
120
LOG_DBG ("Received invalid relative humidity CRC!" );
134
121
return - EIO ;
0 commit comments