46#include "lib/sensors.h"
50#define PRINTF(...) printf(__VA_ARGS__)
59#define SHT21_ADDRESS (0x40)
66#define SHT21_USER_REG_READ (0xE7)
67#define SHT21_USER_REG_WRITE (0xE6)
68#define SHT21_USER_REG_RESERVED_BITS (0x38)
70#define SHT21_TEMPERATURE_HM_CMD (0xE3)
71#define SHT21_HUMIDITY_HM_CMD (0xE5)
72#define SHT21_TEMPERATURE_NHM_CMD (0xF3)
73#define SHT21_HUMIDITY_NHM_CMD (0xF5)
74#define SHT21_RESET_CMD (0xFE)
76#define SHT21_STATUS_MASK (0xFC)
78#define SHT21_RESOLUTION_12b_14b ((0 << 7) | (0 << 0))
79#define SHT21_RESOLUTION_8b_12b ((0 << 7) | (1 << 0))
80#define SHT21_RESOLUTION_10b_13b ((1 << 7) | (0 << 0))
81#define SHT21_RESOLUTION_11b_11b ((1 << 7) | (1 << 0))
82#define SHT21_BATTERY_ABOVE_2V25 (0 << 6)
83#define SHT21_BATTERY_BELOW_2V25 (1 << 6)
84#define SHT21_ONCHIP_HEATER_ENABLE (1 << 2)
85#define SHT21_ONCHIP_HEATER_DISABLE (0 << 2)
86#define SHT21_OTP_RELOAD_ENABLE (0 << 1)
87#define SHT21_OTP_RELOAD_DISABLE (1 << 1)
94#define SHT21_DEFAULT_CONFIG (SHT21_RESOLUTION_12b_14b | \
95 SHT21_ONCHIP_HEATER_DISABLE | \
96 SHT21_BATTERY_ABOVE_2V25 | \
97 SHT21_OTP_RELOAD_DISABLE)
99#define SHT21_USER_CONFIG (SHT21_RESOLUTION_12b_14b | \
100 SHT21_ONCHIP_HEATER_DISABLE | \
101 SHT21_BATTERY_ABOVE_2V25 | \
102 SHT21_OTP_RELOAD_DISABLE)
105static uint8_t enabled;
114 config[0] = SHT21_USER_REG_WRITE;
122 config[1] &= SHT21_USER_REG_RESERVED_BITS;
125 config[1] |= SHT21_USER_CONFIG;
138sht21_is_present(
void)
146 if(status != I2C_MASTER_ERR_NONE) {
151 is_present &= ~SHT21_USER_REG_RESERVED_BITS;
153 return (is_present == SHT21_USER_CONFIG) || (is_present == SHT21_DEFAULT_CONFIG);
157sht21_read_temperature(
void)
159 uint8_t sht21_temperature[2];
160 uint16_t temperature;
166 temperature = (sht21_temperature[0] << 8) | ((sht21_temperature[1] & SHT21_STATUS_MASK));
172sht21_convert_temperature(uint32_t temperature)
176 temperature *= 17572;
177 temperature = temperature >> 16;
178 result = (int16_t)temperature - 4685;
184sht21_read_humidity(
void)
186 uint8_t sht21_humidity[2];
193 humidity = (sht21_humidity[0] << 8) | ((sht21_humidity[1] & SHT21_STATUS_MASK));
199sht21_convert_humidity(uint32_t humidity)
204 humidity = humidity >> 16;
205 result = (int16_t)humidity - 600;
206 result = (result > 10000) ? 10000 : result;
228 PRINTF(
"SHT21: sensor not started\n");
232 if(type == SHT21_READ_RAW_TEMP) {
233 return sht21_read_temperature();
234 }
else if(type == SHT21_READ_RAW_RHUM) {
235 return sht21_read_humidity();
236 }
else if(type == SHT21_READ_TEMP) {
237 value = sht21_read_temperature();
238 return sht21_convert_temperature(value);
239 }
else if(type == SHT21_READ_RHUM) {
240 value = sht21_read_humidity();
241 return sht21_convert_humidity(value);
243 PRINTF(
"SHT21: invalid value requested\n");
249configure(
int type,
int value)
251 if(type == SHT21_ACTIVATE) {
252 if(!sht21_is_present()) {
253 PRINTF(
"SHT21: is not present\n");
258 return SHT21_SUCCESS;
262 if(type == SHT21_RESET && enabled) {
264 return SHT21_SUCCESS;
266 PRINTF(
"SHT21: is not enabled\n");
273SENSORS_SENSOR(sht21, SHT21_SENSOR, value, configure, status);
uint8_t i2c_burst_send(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to send multiple bytes to a slave.
uint8_t i2c_single_receive(uint8_t slave_addr, uint8_t *data)
Perform all operations to receive a byte from a slave.
uint8_t i2c_burst_receive(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to receive multiple bytes from a slave.
uint8_t i2c_single_send(uint8_t slave_addr, uint8_t data)
Perform all operations to send a byte to a slave.
Header file for the SHT21 temperature and humidity sensor driver.