42#define CHIP_NAME "Sensirion SHT2X"
43#define MANUFACTURER_NAME "Sensirion"
44#define SUPPLY_VOLTAGE_MIN 2.1f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 0.33f
47#define TEMPERATURE_MIN -50.0f
48#define TEMPERATURE_MAX 125.0f
49#define DRIVER_VERSION 1000
54#define SHT2X_COMMAND_TRIGGER_T_MEASUREMENT_HOLD_MASTER 0xE3
55#define SHT2X_COMMAND_TRIGGER_RH_MEASUREMENT_HOLD_MASTER 0xE5
56#define SHT2X_COMMAND_TRIGGER_T_MEASUREMENT_NO_HOLD_MASTER 0xF3
57#define SHT2X_COMMAND_TRIGGER_RH_MEASUREMENT_NO_HOLD_MASTER 0xF5
58#define SHT2X_COMMAND_WRITE_REG 0xE6
59#define SHT2X_COMMAND_READ_REG 0xE7
60#define SHT2X_COMMAND_SOFT_RESET 0xFE
61#define SHT2X_COMMAND_GET_SN1 0xFA0FU
62#define SHT2X_COMMAND_GET_SN2 0xFCC9U
67#define SHT2X_ADDRESS 0x80
79static uint8_t a_sht2x_write_command(
sht2x_handle_t *handle, uint8_t *data, uint16_t len)
99static uint8_t a_sht2x_read_command(
sht2x_handle_t *handle, uint8_t *data, uint16_t len)
120static uint8_t a_sht2x_write(
sht2x_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
141static uint8_t a_sht2x_read(
sht2x_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
162static uint8_t a_sht2x_read_hold(
sht2x_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
183static uint8_t a_sht2x_read16(
sht2x_handle_t *handle, uint16_t reg, uint8_t *data, uint16_t len)
200static uint8_t a_sht2x_crc(uint8_t *data, uint16_t len)
202 const uint16_t POLYNOMIAL = 0x131;
207 for (i = 0; i < len; i++)
210 for (j= 8; j > 0; j--)
212 if ((crc & 0x80) != 0)
214 crc = (crc << 1) ^ POLYNOMIAL;
257 handle->
debug_print(
"sht2x: iic_deinit is null.\n");
263 handle->
debug_print(
"sht2x: iic_read_cmd is null.\n");
275 handle->
debug_print(
"sht2x: iic_read_address16 is null.\n");
281 handle->
debug_print(
"sht2x: iic_read_with_wait is null.\n");
287 handle->
debug_print(
"sht2x: iic_write_cmd is null.\n");
293 handle->
debug_print(
"sht2x: iic_write is null.\n");
312 res = a_sht2x_write_command(handle, &command, 1);
315 handle->
debug_print(
"sht2x: write command failed.\n");
351 res = a_sht2x_write_command(handle, &command, 1);
354 handle->
debug_print(
"sht2x: write command failed.\n");
361 handle->
debug_print(
"sht2x: iic deinit failed.\n");
395 res = a_sht2x_write_command(handle, &command, 1);
398 handle->
debug_print(
"sht2x: write command failed.\n");
434 res = a_sht2x_read(handle, command, &prev, 1);
443 prev |= (((uint8_t)(resolution) >> 1) & 0x01) << 7;
444 prev |= (((uint8_t)(resolution) >> 0) & 0x01) << 0;
446 res = a_sht2x_write(handle, command, &prev, 1);
486 res = a_sht2x_read(handle, command, &prev, 1);
493 r = (((prev >> 7) & 0x01) << 1) | (((prev >> 0) & 0x01) << 0);
526 res = a_sht2x_read(handle, command, &prev, 1);
536 res = a_sht2x_write(handle, command, &prev, 1);
574 res = a_sht2x_read(handle, command, &prev, 1);
613 res = a_sht2x_read(handle, command, &prev, 1);
623 res = a_sht2x_write(handle, command, &prev, 1);
661 res = a_sht2x_read(handle, command, &prev, 1);
700 res = a_sht2x_read(handle, command, &prev, 1);
733 handle->
mode = (uint8_t)mode;
780 uint16_t *humidity_raw,
float *humidity_s)
798 res = a_sht2x_read_hold(handle, command, data, 3);
805 if (a_sht2x_crc(data, 2) != data[2])
811 *temperature_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]);
812 (*temperature_raw) &= ~0x0003;
813 *temperature_s = (float)(*temperature_raw) / 65536.0f * 175.72f - 46.85f;
816 res = a_sht2x_read_hold(handle, command, data, 3);
823 if (a_sht2x_crc(data, 2) != data[2])
829 *humidity_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]);
830 (*humidity_raw) &= ~0x0003;
831 *humidity_s = (float)(*humidity_raw) / 65536.0f * 125.0f - 6.0f;
836 res = a_sht2x_write_command(handle, &command, 1);
859 res = a_sht2x_read_command(handle, data, 3);
866 if (a_sht2x_crc(data, 2) != data[2])
872 *temperature_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]);
873 (*temperature_raw) &= ~0x0003;
874 *temperature_s = (float)(*temperature_raw) / 65536.0f * 175.72f - 46.85f;
877 res = a_sht2x_write_command(handle, &command, 1);
900 res = a_sht2x_read_command(handle, data, 3);
907 if (a_sht2x_crc(data, 2) != data[2])
913 *humidity_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]);
914 (*humidity_raw) &= ~0x0003;
915 *humidity_s = (float)(*humidity_raw) / 65536.0f * 125.0f - 6.0f;
949 res = a_sht2x_read16(handle, command, (uint8_t *)data, 8);
956 if (a_sht2x_crc((uint8_t *)&data[0], 1) != data[1])
962 if (a_sht2x_crc((uint8_t *)&data[2], 1) != data[3])
968 if (a_sht2x_crc((uint8_t *)&data[4], 1) != data[5])
974 if (a_sht2x_crc((uint8_t *)&data[6], 1) != data[7])
986 res = a_sht2x_read16(handle, command, (uint8_t *)data, 6);
993 if (a_sht2x_crc((uint8_t *)data, 2) != data[2])
999 if (a_sht2x_crc((uint8_t *)&data[3], 2) != data[5])
1001 handle->
debug_print(
"sht2x: crc check failed.\n");
1037 return a_sht2x_write(handle, reg, data, len);
1064 return a_sht2x_read(handle, reg, data, len);
1091 return a_sht2x_read16(handle, reg, data, len);
1117 return a_sht2x_write_command(handle, data, len);
1143 return a_sht2x_read_command(handle, data, len);
#define SHT2X_COMMAND_GET_SN2
#define SHT2X_COMMAND_SOFT_RESET
#define SHT2X_COMMAND_TRIGGER_T_MEASUREMENT_HOLD_MASTER
chip command definition
#define SUPPLY_VOLTAGE_MAX
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define SHT2X_ADDRESS
chip address definition
#define SHT2X_COMMAND_TRIGGER_T_MEASUREMENT_NO_HOLD_MASTER
#define SHT2X_COMMAND_GET_SN1
#define SHT2X_COMMAND_TRIGGER_RH_MEASUREMENT_NO_HOLD_MASTER
#define SHT2X_COMMAND_WRITE_REG
#define CHIP_NAME
chip information definition
#define SHT2X_COMMAND_READ_REG
#define SHT2X_COMMAND_TRIGGER_RH_MEASUREMENT_HOLD_MASTER
struct sht2x_info_s sht2x_info_t
sht2x information structure definition
uint8_t sht2x_set_heater(sht2x_handle_t *handle, sht2x_bool_t enable)
enable or disable heater
uint8_t sht2x_read(sht2x_handle_t *handle, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, float *humidity_s)
read data
uint8_t sht2x_get_status(sht2x_handle_t *handle, sht2x_status_t *status)
get status
sht2x_mode_t
sht2x mode enumeration definition
uint8_t sht2x_init(sht2x_handle_t *handle)
initialize the chip
uint8_t sht2x_soft_reset(sht2x_handle_t *handle)
soft reset the chip
uint8_t sht2x_get_mode(sht2x_handle_t *handle, sht2x_mode_t *mode)
get chip mode
uint8_t sht2x_get_disable_otp_reload(sht2x_handle_t *handle, sht2x_bool_t *enable)
get disable otp reload status
uint8_t sht2x_info(sht2x_info_t *info)
get chip's information
uint8_t sht2x_get_resolution(sht2x_handle_t *handle, sht2x_resolution_t *resolution)
get resolution
uint8_t sht2x_set_resolution(sht2x_handle_t *handle, sht2x_resolution_t resolution)
set resolution
struct sht2x_handle_s sht2x_handle_t
sht2x handle structure definition
uint8_t sht2x_set_disable_otp_reload(sht2x_handle_t *handle, sht2x_bool_t enable)
enable or disable disable otp reload
uint8_t sht2x_get_serial_number(sht2x_handle_t *handle, uint8_t sn[8])
get serial number
sht2x_status_t
sht2x status enumeration definition
uint8_t sht2x_deinit(sht2x_handle_t *handle)
close the chip
uint8_t sht2x_set_mode(sht2x_handle_t *handle, sht2x_mode_t mode)
set chip mode
sht2x_resolution_t
sht2x resolution enumeration definition
uint8_t sht2x_get_heater(sht2x_handle_t *handle, sht2x_bool_t *enable)
get heater status
sht2x_bool_t
sht2x bool enumeration definition
@ SHT2X_RESOLUTION_RH_12BIT_T_14BIT
@ SHT2X_RESOLUTION_RH_10BIT_T_13BIT
@ SHT2X_RESOLUTION_RH_8BIT_T_12BIT
uint8_t sht2x_get_cmd(sht2x_handle_t *handle, uint8_t *data, uint16_t len)
get command
uint8_t sht2x_get_reg(sht2x_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
get the chip register
uint8_t sht2x_get_reg16(sht2x_handle_t *handle, uint16_t reg, uint8_t *data, uint16_t len)
get the chip register16
uint8_t sht2x_set_cmd(sht2x_handle_t *handle, uint8_t *data, uint16_t len)
set command
uint8_t sht2x_set_reg(sht2x_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
set the chip register
void(* delay_ms)(uint32_t ms)
uint8_t(* iic_read_address16)(uint8_t addr, uint16_t reg, uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_read_with_wait)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
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