42#define CHIP_NAME "Sensirion STS21"
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 -40.0f
48#define TEMPERATURE_MAX 125.0f
49#define DRIVER_VERSION 1000
54#define STS21_COMMAND_TRIGGER_T_MEASUREMENT_HOLD_MASTER 0xE3
55#define STS21_COMMAND_TRIGGER_T_MEASUREMENT_NO_HOLD_MASTER 0xF3
56#define STS21_COMMAND_WRITE_REG 0xE6
57#define STS21_COMMAND_READ_REG 0xE7
58#define STS21_COMMAND_SOFT_RESET 0xFE
59#define STS21_COMMAND_GET_SN1 0xFA0FU
60#define STS21_COMMAND_GET_SN2 0xFCC9U
65#define STS21_ADDRESS 0x94
77static uint8_t a_sts21_write_command(
sts21_handle_t *handle, uint8_t *data, uint16_t len)
97static uint8_t a_sts21_read_command(
sts21_handle_t *handle, uint8_t *data, uint16_t len)
118static uint8_t a_sts21_write(
sts21_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
139static uint8_t a_sts21_read(
sts21_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
160static uint8_t a_sts21_read_hold(
sts21_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
181static uint8_t a_sts21_read16(
sts21_handle_t *handle, uint16_t reg, uint8_t *data, uint16_t len)
198static uint8_t a_sts21_crc(uint8_t *data, uint16_t len)
200 const uint16_t POLYNOMIAL = 0x131;
205 for (i = 0; i < len; i++)
208 for (j= 8; j > 0; j--)
210 if ((crc & 0x80) != 0)
212 crc = (crc << 1) ^ POLYNOMIAL;
255 handle->
debug_print(
"sts21: iic_deinit is null.\n");
261 handle->
debug_print(
"sts21: iic_read_cmd is null.\n");
273 handle->
debug_print(
"sts21: iic_read_address16 is null.\n");
279 handle->
debug_print(
"sts21: iic_read_with_wait is null.\n");
285 handle->
debug_print(
"sts21: iic_write_cmd is null.\n");
291 handle->
debug_print(
"sts21: iic_write is null.\n");
310 res = a_sts21_write_command(handle, &command, 1);
313 handle->
debug_print(
"sts21: write command failed.\n");
349 res = a_sts21_write_command(handle, &command, 1);
352 handle->
debug_print(
"sts21: write command failed.\n");
359 handle->
debug_print(
"sts21: iic deinit failed.\n");
393 res = a_sts21_write_command(handle, &command, 1);
396 handle->
debug_print(
"sts21: write command failed.\n");
432 res = a_sts21_read(handle, command, &prev, 1);
441 prev |= (((uint8_t)(resolution) >> 1) & 0x01) << 7;
442 prev |= (((uint8_t)(resolution) >> 0) & 0x01) << 0;
444 res = a_sts21_write(handle, command, &prev, 1);
484 res = a_sts21_read(handle, command, &prev, 1);
491 r = (((prev >> 7) & 0x01) << 1) | (((prev >> 0) & 0x01) << 0);
524 res = a_sts21_read(handle, command, &prev, 1);
534 res = a_sts21_write(handle, command, &prev, 1);
572 res = a_sts21_read(handle, command, &prev, 1);
611 res = a_sts21_read(handle, command, &prev, 1);
621 res = a_sts21_write(handle, command, &prev, 1);
659 res = a_sts21_read(handle, command, &prev, 1);
698 res = a_sts21_read(handle, command, &prev, 1);
731 handle->
mode = (uint8_t)mode;
793 res = a_sts21_read_hold(handle, command, data, 3);
800 if (a_sts21_crc(data, 2) != data[2])
806 *temperature_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]);
807 (*temperature_raw) &= ~0x0003;
808 *temperature_s = (float)(*temperature_raw) / 65536.0f * 175.72f - 46.85f;
813 res = a_sts21_write_command(handle, &command, 1);
836 res = a_sts21_read_command(handle, data, 3);
843 if (a_sts21_crc(data, 2) != data[2])
849 *temperature_raw = (uint16_t)((((uint16_t)data[0]) << 8) | data[1]);
850 (*temperature_raw) &= ~0x0003;
851 *temperature_s = (float)(*temperature_raw) / 65536.0f * 175.72f - 46.85f;
885 res = a_sts21_read16(handle, command, (uint8_t *)data, 8);
892 if (a_sts21_crc((uint8_t *)&data[0], 1) != data[1])
898 if (a_sts21_crc((uint8_t *)&data[2], 1) != data[3])
904 if (a_sts21_crc((uint8_t *)&data[4], 1) != data[5])
910 if (a_sts21_crc((uint8_t *)&data[6], 1) != data[7])
922 res = a_sts21_read16(handle, command, (uint8_t *)data, 6);
929 if (a_sts21_crc((uint8_t *)data, 2) != data[2])
935 if (a_sts21_crc((uint8_t *)&data[3], 2) != data[5])
973 return a_sts21_write(handle, reg, data, len);
1000 return a_sts21_read(handle, reg, data, len);
1027 return a_sts21_read16(handle, reg, data, len);
1053 return a_sts21_write_command(handle, data, len);
1079 return a_sts21_read_command(handle, data, len);
#define STS21_COMMAND_SOFT_RESET
#define STS21_COMMAND_TRIGGER_T_MEASUREMENT_HOLD_MASTER
chip command definition
#define STS21_COMMAND_WRITE_REG
#define STS21_COMMAND_GET_SN2
#define STS21_ADDRESS
chip address definition
#define SUPPLY_VOLTAGE_MAX
#define STS21_COMMAND_GET_SN1
#define STS21_COMMAND_READ_REG
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define STS21_COMMAND_TRIGGER_T_MEASUREMENT_NO_HOLD_MASTER
sts21_resolution_t
sts21 resolution enumeration definition
uint8_t sts21_soft_reset(sts21_handle_t *handle)
soft reset the chip
uint8_t sts21_info(sts21_info_t *info)
get chip's information
uint8_t sts21_set_heater(sts21_handle_t *handle, sts21_bool_t enable)
enable or disable heater
uint8_t sts21_get_status(sts21_handle_t *handle, sts21_status_t *status)
get status
uint8_t sts21_get_disable_otp_reload(sts21_handle_t *handle, sts21_bool_t *enable)
get disable otp reload status
sts21_mode_t
sts21 mode enumeration definition
uint8_t sts21_get_mode(sts21_handle_t *handle, sts21_mode_t *mode)
get chip mode
uint8_t sts21_set_resolution(sts21_handle_t *handle, sts21_resolution_t resolution)
set resolution
sts21_status_t
sts21 status enumeration definition
uint8_t sts21_get_serial_number(sts21_handle_t *handle, uint8_t sn[8])
get serial number
uint8_t sts21_get_heater(sts21_handle_t *handle, sts21_bool_t *enable)
get heater status
struct sts21_info_s sts21_info_t
sts21 information structure definition
uint8_t sts21_set_disable_otp_reload(sts21_handle_t *handle, sts21_bool_t enable)
enable or disable disable otp reload
struct sts21_handle_s sts21_handle_t
sts21 handle structure definition
sts21_bool_t
sts21 bool enumeration definition
uint8_t sts21_deinit(sts21_handle_t *handle)
close the chip
uint8_t sts21_set_mode(sts21_handle_t *handle, sts21_mode_t mode)
set chip mode
uint8_t sts21_get_resolution(sts21_handle_t *handle, sts21_resolution_t *resolution)
get resolution
uint8_t sts21_init(sts21_handle_t *handle)
initialize the chip
uint8_t sts21_read(sts21_handle_t *handle, uint16_t *temperature_raw, float *temperature_s)
read data
@ STS21_RESOLUTION_T_14BIT
@ STS21_RESOLUTION_T_13BIT
@ STS21_RESOLUTION_T_12BIT
uint8_t sts21_get_cmd(sts21_handle_t *handle, uint8_t *data, uint16_t len)
get command
uint8_t sts21_set_cmd(sts21_handle_t *handle, uint8_t *data, uint16_t len)
set command
uint8_t sts21_set_reg(sts21_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
set the chip register
uint8_t sts21_get_reg16(sts21_handle_t *handle, uint16_t reg, uint8_t *data, uint16_t len)
get the chip register16
uint8_t sts21_get_reg(sts21_handle_t *handle, uint8_t reg, uint8_t *data, uint16_t len)
get 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