Device driver interface for the SHT2X sensor. More...

Detailed Description

Device driver interface for the SHT2X sensor.

Files

file  sht2x.h
 Device driver implementation for the SHT2x temperature and humidity sensor.
 
file  sht2x_params.h
 Default configuration for SHT2X.
 

Data Structures

struct  sht2x_params_t
 Device initialization parameters. More...
 
struct  sht2x_t
 Device descriptor for the SHT2X sensor. More...
 

Enumerations

enum  {
  SHT2X_OK = 0, SHT2X_ERR_I2C = -1, SHT2X_ERR_NODEV = -2, SHT2X_ERR_NOCAL = -3,
  SHT2X_ERR_I2C_READ = -4, SHT2X_ERR_TIMEDOUT = -5, SHT2X_ERR_CRC = -6, SHT2X_ERR_USERREG = -7,
  SHT2X_ERR_RES = -8, SHT2X_ERR_OTHER = -999
}
 Status and error return codes. More...
 
enum  sht2x_res_t { SHT2X_RES_12_14BIT = 0x00, SHT2X_RES_8_12BIT = 0x01, SHT2X_RES_10_13BIT = 0x80, SHT2X_RES_11_11BIT = 0x81 }
 Available resolutions. More...
 
enum  sht2x_measure_mode_t { SHT2X_MEASURE_MODE_HOLD = 0, SHT2X_MEASURE_MODE_NO_HOLD = 1 }
 Available Measuring modes. More...
 

Functions

int sht2x_init (sht2x_t *dev, const sht2x_params_t *params)
 Initialize the given SHT2X device. More...
 
int sht2x_reset (sht2x_t *dev)
 Reset the SHT2X device. More...
 
int16_t sht2x_read_temperature (const sht2x_t *dev)
 Read temperature value from the given SHT2X device, returned in centi °C. More...
 
uint16_t sht2x_read_humidity (const sht2x_t *dev)
 Read humidity value from the given SHT2X device, returned in centi RH. More...
 
int sht2x_read_ident (const sht2x_t *dev, uint8_t *buffer, size_t buflen)
 Read identification code from the given SHT2X device. More...
 
int sht2x_read_userreg (const sht2x_t *dev, uint8_t *userreg)
 Read User Register from the given SHT2X device. More...
 
int sht2x_write_userreg (const sht2x_t *dev, uint8_t userreg)
 Write User Register to the given SHT2X device. More...
 
#define SHT2X_USER_RESOLUTION_MASK   0x81
 User register masks. More...
 
#define SHT2X_USER_EOB_MASK   0x40
 
#define SHT2X_USER_HEATER_MASK   0x04
 
#define SHT2X_USER_RESERVED_MASK   0x38
 
#define SHT2X_USER_OTP_MASK   0x02
 

Macro Definition Documentation

◆ SHT2X_USER_RESOLUTION_MASK

#define SHT2X_USER_RESOLUTION_MASK   0x81

User register masks.

Notice that the values of sht2x_res_t must only have the bits in SHT2X_USER_RESOLUTION_MASK.

Definition at line 79 of file sht2x.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

Status and error return codes.

Enumerator
SHT2X_OK 

everything was fine

SHT2X_ERR_I2C 

error initializing the I2C bus

SHT2X_ERR_NODEV 

did not detect SHT2x

SHT2X_ERR_NOCAL 

could not read calibration data

SHT2X_ERR_I2C_READ 

I2C read error.

SHT2X_ERR_TIMEDOUT 

timed out

SHT2X_ERR_CRC 

CRC error.

SHT2X_ERR_USERREG 

cannot write User Reg

SHT2X_ERR_RES 

invalid resolution

SHT2X_ERR_OTHER 

fatal error

Definition at line 40 of file sht2x.h.

◆ sht2x_measure_mode_t

Available Measuring modes.

Enumerator
SHT2X_MEASURE_MODE_HOLD 

trigger measurement, hold master

SHT2X_MEASURE_MODE_NO_HOLD 

trigger measurement, no hold master (i.e.

poll)

Definition at line 68 of file sht2x.h.

◆ sht2x_res_t

Available resolutions.

The values represent bits 7 and 0 in the User Register

Enumerator
SHT2X_RES_12_14BIT 

RH=12bit, T=14bit.

SHT2X_RES_8_12BIT 

RH= 8bit, T=12bit.

SHT2X_RES_10_13BIT 

RH=10bit, T=13bit.

SHT2X_RES_11_11BIT 

RH=11bit, T=11bit.

Definition at line 58 of file sht2x.h.

Function Documentation

◆ sht2x_init()

int sht2x_init ( sht2x_t dev,
const sht2x_params_t params 
)

Initialize the given SHT2X device.

Parameters
[out]devInitialized device descriptor of SHT2X device
[in]paramsThe parameters for the SHT2x device
Returns
SHT2X_OK on success
SHT2X_ERR_I2C if given I2C is not enabled in board config
SHT2X_ERR_RES invalid resolution
SHT2X_ERR_USERREG error writing User Register
errors returned by sht2x_write_userreg or sht2x_read_userreg

◆ sht2x_read_humidity()

uint16_t sht2x_read_humidity ( const sht2x_t dev)

Read humidity value from the given SHT2X device, returned in centi RH.

Notice that this function will sleep (max 22 ms) when the measure_mode is set to SHT2X_MEASURE_MODE_NO_HOLD.

Parameters
[in]devDevice descriptor of SHT2X device to read from
Returns
Humidity in centi RH (i.e. the percentage times 100). In case of an error it returns 0 (i.e. an unrealistic value).

◆ sht2x_read_ident()

int sht2x_read_ident ( const sht2x_t dev,
uint8_t *  buffer,
size_t  buflen 
)

Read identification code from the given SHT2X device.

Parameters
[in]devDevice descriptor of SHT2X device to read from
[out]bufferBuffer to write the identification code to
[in]buflenThe size of buffer
Returns
>= 0 on success, number of bytes read
SHT2X_ERR_CRC in case of a CRC mismatch
<0 errors returned by i2c_read_regs

◆ sht2x_read_temperature()

int16_t sht2x_read_temperature ( const sht2x_t dev)

Read temperature value from the given SHT2X device, returned in centi °C.

Notice that this function will sleep (max 66 ms) when the measure_mode is set to SHT2X_MEASURE_MODE_NO_HOLD.

Parameters
[in]devDevice descriptor of SHT2X device to read from
Returns
The temperature in centi Celsius. In case of an error it returns INT16_MIN.

◆ sht2x_read_userreg()

int sht2x_read_userreg ( const sht2x_t dev,
uint8_t *  userreg 
)

Read User Register from the given SHT2X device.

Parameters
[in]devDevice descriptor of SHT2X device to read from
[out]userregThe value of User Register
Returns
SHT2X_OK on success
SHT2X_ERR_I2C if given I2C is not enabled in board config
SHT2X_ERR_OTHER if NULL pointer was given for buffer

◆ sht2x_reset()

int sht2x_reset ( sht2x_t dev)

Reset the SHT2X device.

Parameters
[in]devDevice descriptor of SHT2X device to read from
Returns
SHT2X_OK on success
SHT2X_ERR_I2C if given I2C is not enabled in board config

◆ sht2x_write_userreg()

int sht2x_write_userreg ( const sht2x_t dev,
uint8_t  userreg 
)

Write User Register to the given SHT2X device.

Parameters
[in]devDevice descriptor of SHT2X device to read from
[in]userregThe value of User Register
Returns
SHT2X_OK on success
SHT2X_ERR_I2C if given I2C is not enabled in board config