-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsht3x.c
108 lines (87 loc) · 2.86 KB
/
sht3x.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "sht3x.h"
#include <assert.h>
/**
* Registers addresses.
*/
typedef enum
{
SHT3X_COMMAND_MEASURE_HIGHREP_STRETCH = 0x2c06,
SHT3X_COMMAND_CLEAR_STATUS = 0x3041,
SHT3X_COMMAND_SOFT_RESET = 0x30A2,
SHT3X_COMMAND_HEATER_ENABLE = 0x306d,
SHT3X_COMMAND_HEATER_DISABLE = 0x3066,
SHT3X_COMMAND_READ_STATUS = 0xf32d,
SHT3X_COMMAND_FETCH_DATA = 0xe000,
SHT3X_COMMAND_MEASURE_HIGHREP_10HZ = 0x2737,
SHT3X_COMMAND_MEASURE_LOWREP_10HZ = 0x272a
} sht3x_command_t;
static uint8_t calculate_crc(const uint8_t *data, size_t length)
{
uint8_t crc = 0xff;
for (size_t i = 0; i < length; i++) {
crc ^= data[i];
for (size_t j = 0; j < 8; j++) {
if ((crc & 0x80u) != 0) {
crc = (uint8_t)((uint8_t)(crc << 1u) ^ 0x31u);
} else {
crc <<= 1u;
}
}
}
return crc;
}
static bool sht3x_send_command(sht3x_handle_t *handle, sht3x_command_t command)
{
uint8_t command_buffer[2] = {(command & 0xff00u) >> 8u, command & 0xffu};
if (HAL_I2C_Master_Transmit(handle->i2c_handle, handle->device_address << 1u, command_buffer, sizeof(command_buffer),
SHT3X_I2C_TIMEOUT) != HAL_OK) {
return false;
}
return true;
}
static uint16_t uint8_to_uint16(uint8_t msb, uint8_t lsb)
{
return (uint16_t)((uint16_t)msb << 8u) | lsb;
}
bool sht3x_init(sht3x_handle_t *handle)
{
assert(handle->i2c_handle->Init.NoStretchMode == I2C_NOSTRETCH_DISABLE);
// TODO: Assert i2c frequency is not too high
uint8_t status_reg_and_checksum[3];
if (HAL_I2C_Mem_Read(handle->i2c_handle, handle->device_address << 1u, SHT3X_COMMAND_READ_STATUS, 2, (uint8_t*)&status_reg_and_checksum,
sizeof(status_reg_and_checksum), SHT3X_I2C_TIMEOUT) != HAL_OK) {
return false;
}
uint8_t calculated_crc = calculate_crc(status_reg_and_checksum, 2);
if (calculated_crc != status_reg_and_checksum[2]) {
return false;
}
return true;
}
bool sht3x_read_temperature_and_humidity(sht3x_handle_t *handle, float *temperature, float *humidity)
{
sht3x_send_command(handle, SHT3X_COMMAND_MEASURE_HIGHREP_STRETCH);
HAL_Delay(1);
uint8_t buffer[6];
if (HAL_I2C_Master_Receive(handle->i2c_handle, handle->device_address << 1u, buffer, sizeof(buffer), SHT3X_I2C_TIMEOUT) != HAL_OK) {
return false;
}
uint8_t temperature_crc = calculate_crc(buffer, 2);
uint8_t humidity_crc = calculate_crc(buffer + 3, 2);
if (temperature_crc != buffer[2] || humidity_crc != buffer[5]) {
return false;
}
uint16_t temperature_raw = uint8_to_uint16(buffer[0], buffer[1]);
uint16_t humidity_raw = uint8_to_uint16(buffer[3], buffer[4]);
*temperature = -45.0f + 175.0f * (float)temperature_raw / 65535.0f;
*humidity = 100.0f * (float)humidity_raw / 65535.0f;
return true;
}
bool sht3x_set_header_enable(sht3x_handle_t *handle, bool enable)
{
if (enable) {
return sht3x_send_command(handle, SHT3X_COMMAND_HEATER_ENABLE);
} else {
return sht3x_send_command(handle, SHT3X_COMMAND_HEATER_DISABLE);
}
}