18static constexpr uint32_t DATA_TIMEOUT_ID = 0;
21static const uint8_t ENS210_BOOTING_MS = 2;
22static const uint8_t ENS210_SINGLE_MEASURMENT_CONVERSION_TIME_MS =
24static const uint16_t ENS210_PART_ID = 0x0210;
27static const uint8_t ENS210_REGISTER_PART_ID = 0x00;
28static const uint8_t ENS210_REGISTER_UID = 0x04;
29static const uint8_t ENS210_REGISTER_SYS_CTRL = 0x10;
30static const uint8_t ENS210_REGISTER_SYS_STAT = 0x11;
31static const uint8_t ENS210_REGISTER_SENS_RUN = 0x21;
32static const uint8_t ENS210_REGISTER_SENS_START = 0x22;
33static const uint8_t ENS210_REGISTER_SENS_STOP = 0x23;
34static const uint8_t ENS210_REGISTER_SENS_STAT = 0x24;
35static const uint8_t ENS210_REGISTER_T_VAL = 0x30;
36static const uint8_t ENS210_REGISTER_H_VAL = 0x33;
39static const uint8_t CRC7_WIDTH = 7;
40static const uint8_t CRC7_POLY = 0x89;
41static const uint8_t CRC7_IVEC = 0x7F;
44static const uint8_t DATA7_WIDTH = 17;
45static const uint32_t DATA7_MASK = ((1UL << DATA7_WIDTH) - 1);
46static const uint32_t DATA7_MSB = (1UL << (DATA7_WIDTH - 1));
49static const LogString *ens210_status_to_human(
int status) {
52 return LOG_STR(
"I2C error - communication with ENS210 failed!");
54 return LOG_STR(
"CRC error");
56 return LOG_STR(
"Invalid data");
58 return LOG_STR(
"Status OK");
60 return LOG_STR(
"Unknown");
70 polynomial = polynomial << (DATA7_WIDTH - CRC7_WIDTH - 1);
74 value = value << CRC7_WIDTH;
75 bit = bit << CRC7_WIDTH;
76 polynomial = polynomial << CRC7_WIDTH;
80 while (bit & (DATA7_MASK << CRC7_WIDTH)) {
93 if (!this->
write_byte(ENS210_REGISTER_SYS_CTRL, 0x80)) {
94 this->
write_byte(ENS210_REGISTER_SYS_CTRL, 0x80);
99 delay(ENS210_BOOTING_MS);
108 if (!this->
read_bytes(ENS210_REGISTER_PART_ID, data, 2)) {
115 part_id = data[1] * 256U + data[0] * 1U;
117 if (part_id != ENS210_PART_ID) {
128 ESP_LOGCONFIG(TAG,
"ENS210:");
129 LOG_I2C_DEVICE(
this);
130 LOG_UPDATE_INTERVAL(
this);
137 if (!this->
write_byte(ENS210_REGISTER_SENS_RUN, 0x00)) {
138 ESP_LOGE(TAG,
"Starting single measurement failed!");
143 if (!this->
write_byte(ENS210_REGISTER_SENS_START, 0x03)) {
144 ESP_LOGE(TAG,
"Trigger of measurement failed!");
149 this->
set_timeout(DATA_TIMEOUT_ID,
uint32_t(ENS210_SINGLE_MEASURMENT_CONVERSION_TIME_MS), [
this]() {
150 int temperature_data, temperature_status, humidity_data, humidity_status;
158 if (!this->
read_bytes(ENS210_REGISTER_T_VAL, data, 6)) {
159 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
170 float humidity = (humidity_data & 0xFFFF) / 512.0;
174 ESP_LOGW(TAG,
"Humidity status failure: %s", LOG_STR_ARG(ens210_status_to_human(humidity_status)));
186 float temperature = (temperature_data & 0xFFFF) / 64.0 - 27315L / 100.0;
190 ESP_LOGW(TAG,
"Temperature status failure: %s", LOG_STR_ARG(ens210_status_to_human(temperature_status)));
197 *data = (
val >> 0) & 0xffff;
202 uint8_t crc_ok = crc7(payload) == crc;
215 uint8_t low_power_cmd = enable ? 0x01 : 0x00;
216 ESP_LOGD(TAG,
"Enable low power: %s", enable ? LOG_STR_LITERAL(
"true") : LOG_STR_LITERAL(
"false"));
217 bool result = this->
write_byte(ENS210_REGISTER_SYS_CTRL, low_power_cmd);
218 delay(ENS210_BOOTING_MS);
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.