42#define CHIP_NAME "TE HTU21D"
43#define MANUFACTURER_NAME "TE"
44#define SUPPLY_VOLTAGE_MIN 1.5f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 0.5f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 125.0f
49#define DRIVER_VERSION 1000
54#define HTU21D_ADDRESS 0x80
59#define HTU21D_COMMAND_TRIG_TEMP_HOLD_MASTER 0xE3
60#define HTU21D_COMMAND_TRIG_HUMI_HOLD_MASTER 0xE5
61#define HTU21D_COMMAND_TRIG_TEMP_NO_HOLD_MASTER 0xF3
62#define HTU21D_COMMAND_TRIG_HUMI_NO_HOLD_MASTER 0xF5
63#define HTU21D_COMMAND_WRITE_USER_REGISTER 0xE6
64#define HTU21D_COMMAND_READ_USER_REGISTER 0xE7
65#define HTU21D_COMMAND_SOFT_RESET 0xFE
77static uint8_t a_htu21d_write_cmd(
htu21d_handle_t *handle, uint8_t *buf, uint16_t len)
99static uint8_t a_htu21d_read_cmd(
htu21d_handle_t *handle, uint8_t *buf, uint16_t len)
122static uint8_t a_htu21d_write(
htu21d_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
146static uint8_t a_htu21d_read(
htu21d_handle_t *handle, uint8_t mode, uint8_t reg, uint8_t *buf, uint16_t len)
181static uint8_t a_htu21d_crc(uint16_t value, uint32_t crc)
183 uint32_t polynom = 0x988000U;
184 uint32_t msb = 0x800000U;
185 uint32_t mask = 0xFF8000U;
186 uint32_t result = (uint32_t)value << 8;
190 if ((result & msb) != 0)
192 result = ((result ^ polynom) & mask) | (result & (~mask));
314 uint16_t *temperature_raw,
float *temperature_s,
315 uint16_t *humidity_raw,
float *humidity_s
342 data = ((uint16_t)buf[0] << 8) | buf[1];
343 if (a_htu21d_crc(data, buf[2]) != 0)
356 *temperature_raw = data;
366 data = ((uint16_t)buf[0] << 8) | buf[1];
367 if (a_htu21d_crc(data, buf[2]) != 0)
380 *humidity_raw = data;
387 res = a_htu21d_write_cmd(handle, &cmd, 1);
390 handle->
debug_print(
"htu21d: write cmd failed.\n");
410 res = a_htu21d_read_cmd(handle, buf, 3);
417 data = ((uint16_t)buf[0] << 8) | buf[1];
418 if (a_htu21d_crc(data, buf[2]) != 0)
431 *temperature_raw = data;
434 res = a_htu21d_write_cmd(handle, &cmd, 1);
437 handle->
debug_print(
"htu21d: write cmd failed.\n");
457 res = a_htu21d_read_cmd(handle, buf, 3);
464 data = ((uint16_t)buf[0] << 8) | buf[1];
465 if (a_htu21d_crc(data, buf[2]) != 0)
478 *humidity_raw = data;
481 *temperature_s = (float)(*temperature_raw) / 65536.0f * 175.72f - 46.85f;
482 *humidity_s = (float)(*humidity_raw) / 65536.0f * 125.0f - 6.0f;
527 data = ((uint16_t)buf[0] << 8) | buf[1];
528 if (a_htu21d_crc(data, buf[2]) != 0)
541 *temperature_raw = data;
548 res = a_htu21d_write_cmd(handle, &cmd, 1);
551 handle->
debug_print(
"htu21d: write cmd failed.\n");
571 res = a_htu21d_read_cmd(handle, buf, 3);
578 data = ((uint16_t)buf[0] << 8) | buf[1];
579 if (a_htu21d_crc(data, buf[2]) != 0)
592 *temperature_raw = data;
595 *temperature_s = (float)(*temperature_raw) / 65536.0f * 175.72f - 46.85f;
640 data = ((uint16_t)buf[0] << 8) | buf[1];
641 if (a_htu21d_crc(data, buf[2]) != 0)
654 *humidity_raw = data;
661 res = a_htu21d_write_cmd(handle, &cmd, 1);
664 handle->
debug_print(
"htu21d: write cmd failed.\n");
684 res = a_htu21d_read_cmd(handle, buf, 3);
691 data = ((uint16_t)buf[0] << 8) | buf[1];
692 if (a_htu21d_crc(data, buf[2]) != 0)
705 *humidity_raw = data;
708 *humidity_s = (float)(*humidity_raw) / 65536.0f * 125.0f - 6.0f;
748 prev |= ((resolution >> 1) & 0x01) << 7;
749 prev |= ((resolution >> 0) & 0x01) << 0;
795 | (((prev >> 0) & 0x01) << 0));
1037 res = a_htu21d_write_cmd(handle, cmd, 2);
1040 handle->
debug_print(
"htu21d: write command failed.\n");
1044 res = a_htu21d_read_cmd(handle, data, 8);
1047 handle->
debug_print(
"htu21d: read command failed.\n");
1053 res = a_htu21d_write_cmd(handle, cmd, 2);
1056 handle->
debug_print(
"htu21d: write command failed.\n");
1060 res = a_htu21d_read_cmd(handle, data + 8, 6);
1063 handle->
debug_print(
"htu21d: read command failed.\n");
1068 for (i = 0; i < 8; i += 2)
1070 if (a_htu21d_crc(data[i], data[i + 1]) != 0)
1077 for (i = 8; i < 14; i += 3)
1079 if (a_htu21d_crc(((uint16_t)data[i] << 8) | data[i + 1], data[i + 2]) != 0)
1087 *number = ((uint64_t)data[0] << 56) | ((uint64_t)data[2] << 48)
1088 | ((uint64_t)data[4] << 40) | ((uint64_t)data[6] << 32)
1089 | ((uint64_t)data[8] << 24) | ((uint64_t)data[9] << 16)
1090 | ((uint64_t)data[11] << 8) | ((uint64_t)data[12] << 0);
1122 handle->
debug_print(
"htu21d: iic_init is null.\n");
1128 handle->
debug_print(
"htu21d: iic_deinit is null.\n");
1134 handle->
debug_print(
"htu21d: iic_read is null.\n");
1140 handle->
debug_print(
"htu21d: iic_read_cmd is null.\n");
1146 handle->
debug_print(
"htu21d: iic_read_with_scl is null.\n");
1152 handle->
debug_print(
"htu21d: iic_write is null.\n");
1158 handle->
debug_print(
"htu21d: iic_write_cmd is null.\n");
1164 handle->
debug_print(
"htu21d: delay_ms is null.\n");
1171 handle->
debug_print(
"htu21d: iic init failed.\n");
1178 handle->
debug_print(
"htu21d: soft reset failed.\n");
1188 handle->
debug_print(
"htu21d: read config failed.\n");
1194 | (((prev >> 0) & 0x01) << 0));
1229 handle->
debug_print(
"htu21d: soft reset failed.\n");
1236 handle->
debug_print(
"htu21d: iic deinit failed.\n");
1268 return a_htu21d_write_cmd(handle, buf, len);
1294 return a_htu21d_read_cmd(handle, buf, len);
#define HTU21D_COMMAND_READ_USER_REGISTER
#define HTU21D_COMMAND_TRIG_TEMP_NO_HOLD_MASTER
#define SUPPLY_VOLTAGE_MAX
#define HTU21D_COMMAND_TRIG_HUMI_HOLD_MASTER
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define HTU21D_COMMAND_WRITE_USER_REGISTER
#define HTU21D_COMMAND_SOFT_RESET
#define HTU21D_COMMAND_TRIG_TEMP_HOLD_MASTER
chip command definition
#define CHIP_NAME
chip information definition
#define HTU21D_COMMAND_TRIG_HUMI_NO_HOLD_MASTER
#define HTU21D_ADDRESS
chip address definition
driver htu21d header file
uint8_t htu21d_get_disable_otp_reload(htu21d_handle_t *handle, htu21d_bool_t *enable)
get the disable otp reload status
uint8_t htu21d_set_disable_otp_reload(htu21d_handle_t *handle, htu21d_bool_t enable)
enable or disable otp reload
uint8_t htu21d_read_temperature_humidity(htu21d_handle_t *handle, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, float *humidity_s)
read the temperature and humidity data
uint8_t htu21d_get_heater(htu21d_handle_t *handle, htu21d_bool_t *enable)
get the heater status
uint8_t htu21d_soft_reset(htu21d_handle_t *handle)
soft reset
uint8_t htu21d_get_resolution(htu21d_handle_t *handle, htu21d_resolution_t *resolution)
get the resolution
uint8_t htu21d_info(htu21d_info_t *info)
get chip's information
uint8_t htu21d_set_mode(htu21d_handle_t *handle, htu21d_mode_t mode)
set the chip mode
uint8_t htu21d_init(htu21d_handle_t *handle)
initialize the chip
htu21d_resolution_t
htu21d resolution enumeration definition
htu21d_mode_t
htu21d mode enumeration definition
uint8_t htu21d_set_resolution(htu21d_handle_t *handle, htu21d_resolution_t resolution)
set the resolution
uint8_t htu21d_set_heater(htu21d_handle_t *handle, htu21d_bool_t enable)
enable or disable the heater
struct htu21d_handle_s htu21d_handle_t
htu21d handle structure definition
uint8_t htu21d_read_temperature(htu21d_handle_t *handle, uint16_t *temperature_raw, float *temperature_s)
read the temperature data
struct htu21d_info_s htu21d_info_t
htu21d information structure definition
uint8_t htu21d_get_mode(htu21d_handle_t *handle, htu21d_mode_t *mode)
get the chip mode
uint8_t htu21d_get_serial_number(htu21d_handle_t *handle, uint64_t *number)
get the serial number
uint8_t htu21d_get_battery_status(htu21d_handle_t *handle, htu21d_status_t *status)
get the battery status
uint8_t htu21d_read_humidity(htu21d_handle_t *handle, uint16_t *humidity_raw, float *humidity_s)
read the humidity data
htu21d_status_t
htu21d status enumeration definition
htu21d_bool_t
htu21d bool enumeration definition
uint8_t htu21d_deinit(htu21d_handle_t *handle)
close the chip
@ HTU21D_RESOLUTION_TEMP_13_BITS_RH_10_BITS
@ HTU21D_RESOLUTION_TEMP_12_BITS_RH_8_BITS
@ HTU21D_RESOLUTION_TEMP_11_BITS_RH_11_BITS
@ HTU21D_MODE_HOLD_MASTER
@ HTU21D_MODE_NO_HOLD_MASTER
uint8_t htu21d_get_reg(htu21d_handle_t *handle, uint8_t *buf, uint16_t len)
get the chip register
uint8_t htu21d_set_reg(htu21d_handle_t *handle, uint8_t *buf, uint16_t len)
set the chip register
void(* delay_ms)(uint32_t ms)
uint8_t(* iic_read_with_scl)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_read_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
uint8_t(* iic_write_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
float supply_voltage_max_v
char manufacturer_name[32]
float supply_voltage_min_v