Merge pull request #1454 from alignan/pull/sht25-sensor

Fixes and enhancements for the SHT25 sensor
This commit is contained in:
Antonio Lignan 2016-01-11 20:29:14 +01:00
commit 57f4134c9e
3 changed files with 178 additions and 47 deletions

View file

@ -60,13 +60,29 @@ PROCESS_THREAD(remote_sht25_process, ev, data)
PROCESS_BEGIN(); PROCESS_BEGIN();
SENSORS_ACTIVATE(sht25); SENSORS_ACTIVATE(sht25);
/* Check if the sensor voltage operation is over 2.25V */
if(sht25.value(SHT25_VOLTAGE_ALARM)) {
printf("Voltage is lower than recommended for the sensor operation\n");
PROCESS_EXIT();
}
/* Configure the sensor for maximum resolution (14-bit temperature, 12-bit
* relative humidity), this will require up to 85ms for the temperature
* integration, and 29ms for the relative humidity (this is the default
* setting at power on). To achieve a faster integration time at the cost
* of a lower resolution, change the value below accordingly, see sht25.h.
*/
sht25.configure(SHT25_RESOLUTION, SHT2X_RES_14T_12RH);
/* Let it spin and read sensor data */
while(1) { while(1) {
etimer_set(&et, CLOCK_SECOND); etimer_set(&et, CLOCK_SECOND);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
temperature = sht25.value(SHT25_VAL_TEMP); temperature = sht25.value(SHT25_VAL_TEMP);
printf("Temperature %d.%d ºC\n", temperature / 100, temperature % 100); printf("Temperature %02d.%02d ºC, ", temperature / 100, temperature % 100);
humidity = sht25.value(SHT25_VAL_HUM); humidity = sht25.value(SHT25_VAL_HUM);
printf("Humidity %d.%d RH\n", humidity / 100, humidity % 100); printf("Humidity %02d.%02d RH\n", humidity / 100, humidity % 100);
} }
PROCESS_END(); PROCESS_END();
} }

View file

@ -46,21 +46,15 @@
#include "dev/sht25.h" #include "dev/sht25.h"
#include "lib/sensors.h" #include "lib/sensors.h"
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static uint8_t enabled; #define DEBUG 1
#if DEBUG
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static int static uint8_t enabled;
configure(int type, int value) static uint8_t user_reg;
{
if(type != SENSORS_ACTIVE) {
return SHT25_ERROR;
}
if(value) {
i2c_init(I2C_SDA_PORT, I2C_SDA_PIN, I2C_SCL_PORT, I2C_SCL_PIN,
I2C_SCL_NORMAL_BUS_SPEED);
}
enabled = value;
return 0;
}
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static int static int
status(int type) status(int type)
@ -74,10 +68,15 @@ status(int type)
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static uint16_t static uint16_t
sht25_read_reg(uint8_t reg, uint8_t *buf, uint8_t regNum) sht25_read_reg(uint8_t reg, uint8_t *buf, uint8_t num)
{ {
if((buf == NULL) || (num <= 0)) {
return SHT25_ERROR;
}
i2c_master_enable();
if(i2c_single_send(SHT25_ADDR, reg) == I2C_MASTER_ERR_NONE) { if(i2c_single_send(SHT25_ADDR, reg) == I2C_MASTER_ERR_NONE) {
if(i2c_burst_receive(SHT25_ADDR, buf, regNum) == I2C_MASTER_ERR_NONE) { if(i2c_burst_receive(SHT25_ADDR, buf, num) == I2C_MASTER_ERR_NONE) {
return SHT25_SUCCESS; return SHT25_SUCCESS;
} }
} }
@ -89,7 +88,10 @@ sht25_convert(uint8_t variable, uint16_t value)
{ {
int16_t rd; int16_t rd;
uint32_t buff; uint32_t buff;
buff = (uint32_t)value;
/* Clear the status bits */
buff = (uint32_t)(value & ~SHT25_STATUS_BITS_MASK);
if(variable == SHT25_VAL_TEMP) { if(variable == SHT25_VAL_TEMP) {
buff *= 17572; buff *= 17572;
buff = buff >> 16; buff = buff >> 16;
@ -103,13 +105,14 @@ sht25_convert(uint8_t variable, uint16_t value)
return rd; return rd;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static int16_t static int
sht25_read(uint8_t variable, uint16_t *rd) sht25_read(uint8_t variable, uint16_t *rd)
{ {
uint8_t buf[2]; uint8_t buf[2];
uint16_t raw; uint16_t raw;
if((variable != SHT25_VAL_TEMP) && (variable != SHT25_VAL_HUM)) { if((variable != SHT25_VAL_TEMP) && (variable != SHT25_VAL_HUM)) {
PRINTF("SHT25: invalid sensor requested\n");
return SHT25_ERROR; return SHT25_ERROR;
} }
@ -118,6 +121,34 @@ sht25_read(uint8_t variable, uint16_t *rd)
*rd = sht25_convert(variable, raw); *rd = sht25_convert(variable, raw);
return SHT25_SUCCESS; return SHT25_SUCCESS;
} }
PRINTF("SHT25: failed to read sensor\n");
return SHT25_ERROR;
}
/*---------------------------------------------------------------------------*/
static int
sht25_write_reg(uint8_t *buf, uint8_t num)
{
if((buf == NULL) || (num <= 0)) {
PRINTF("SHT25: invalid write values\n");
return SHT25_ERROR;
}
i2c_master_enable();
if(i2c_burst_send(SHT25_ADDR, buf, num) == I2C_MASTER_ERR_NONE) {
return SHT25_SUCCESS;
}
return SHT25_ERROR;
}
/*---------------------------------------------------------------------------*/
static int
sht25_read_user_register(void)
{
if(sht25_read_reg(SHT2X_UREG_READ, &user_reg, 1) == SHT25_SUCCESS) {
PRINTF("SHT25: user register 0x%02X\n", user_reg);
return SHT25_SUCCESS;
}
PRINTF("SHT25: failed to read user register\n");
return SHT25_ERROR; return SHT25_ERROR;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
@ -125,9 +156,84 @@ static int
value(int type) value(int type)
{ {
uint16_t val; uint16_t val;
if(sht25_read(type, &val) == SHT25_SUCCESS) {
return val; if(!enabled) {
PRINTF("SHT25: sensor not started\n");
return SHT25_ERROR;
} }
if((type != SHT25_VAL_TEMP) && (type != SHT25_VAL_HUM) &&
(type != SHT25_VOLTAGE_ALARM)) {
PRINTF("SHT25: invalid value requested\n");
return SHT25_ERROR;
}
if(type == SHT25_VOLTAGE_ALARM) {
if(sht25_read_user_register() == SHT25_SUCCESS) {
return (user_reg & SHT2x_LOW_VOLTAGE_MASK) >> SHT2x_LOW_VOLTAGE_SHIFT;
}
} else {
if(sht25_read(type, &val) == SHT25_SUCCESS) {
return val;
}
}
return SHT25_ERROR;
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, int value)
{
uint8_t buf[2];
if((type != SHT25_ACTIVE) && (type != SHT25_SOFT_RESET) &&
(type != SHT25_RESOLUTION)) {
PRINTF("SHT25: option not supported\n");
return SHT25_ERROR;
}
switch(type) {
case SHT25_ACTIVE:
if(value) {
i2c_init(I2C_SDA_PORT, I2C_SDA_PIN, I2C_SCL_PORT, I2C_SCL_PIN,
I2C_SCL_NORMAL_BUS_SPEED);
/* Read the user config register */
if(sht25_read_user_register() == SHT25_SUCCESS) {
enabled = value;
return SHT25_SUCCESS;
}
}
case SHT25_SOFT_RESET:
buf[0] = SHT2X_SOFT_RESET;
if(sht25_write_reg(&buf[0], 1) != SHT25_SUCCESS) {
PRINTF("SHT25: failed to reset the sensor\n");
return SHT25_ERROR;
}
clock_delay_usec(SHT25_RESET_DELAY);
return SHT25_SUCCESS;
case SHT25_RESOLUTION:
if((value != SHT2X_RES_14T_12RH) && (value != SHT2X_RES_12T_08RH) &&
(value != SHT2X_RES_13T_10RH) && (value != SHT2X_RES_11T_11RH)) {
PRINTF("SHT25: invalid resolution value\n");
return SHT25_ERROR;
}
user_reg &= ~SHT2X_RES_11T_11RH;
user_reg |= value;
buf[0] = SHT2X_UREG_WRITE;
buf[1] = user_reg;
if(sht25_write_reg(buf, 2) == SHT25_SUCCESS) {
PRINTF("SHT25: new user register value 0x%02X\n", user_reg);
return SHT25_SUCCESS;
}
default:
return SHT25_ERROR;
}
return SHT25_ERROR; return SHT25_ERROR;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/

View file

@ -44,34 +44,43 @@
*/ */
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#include "lib/sensors.h" #include "lib/sensors.h"
/* -------------------------------------------------------------------------- */
#ifndef SHT25_H_ #ifndef SHT25_H_
#define SHT25_H_ #define SHT25_H_
/* -------------------------------------------------------------------------- */
#define SHT25_ADDR 0x40
#define SHT25_TEMP_HOLD 0xE3
#define SHT25_HUM_HOLD 0xE5
#define SHT25_TEMP_NO_HOLD 0xF3
#define SHT25_HUM_NO_HOLD 0xF5
#define SHT2X_UREG_WRITE 0xE6
#define SHT2X_UREG_READ 0xE7
#define SHT2X_SOFT_RESET 0XFE
#define SHT2X_NULL 0x00
/* -------------------------------------------------------------------------- */
#define SHT2X_RES_14T_12RH 0x00
#define SHT2X_RES_12T_08RH 0x01
#define SHT2X_RES_13T_10RH 0x80
#define SHT2X_RES_11T_11RH 0x81
#define SHT2X_HEATER_ON 0x04
#define SHT2X_HEATER_OFF 0x00
#define SHT2X_OTP_RELOAD_EN 0x00
#define SHT2X_OTP_RELOAD_DIS 0x02
#define SHT2x_LOW_VOLTAGE_MASK 0x40
#define SHT2x_LOW_VOLTAGE_SHIFT 0x06
/* -------------------------------------------------------------------------- */
#define SHT25_ACTIVE SENSORS_ACTIVE
#define SHT25_SOFT_RESET 0x01
#define SHT25_RESOLUTION 0x02
/* -------------------------------------------------------------------------- */ #define SHT25_VAL_TEMP SHT25_TEMP_HOLD
#define SHT25_ADDR 0x40 #define SHT25_VAL_HUM SHT25_HUM_HOLD
#define SHT25_TEMP_HOLD 0xE3 #define SHT25_VOLTAGE_ALARM 0x01
#define SHT25_HUM_HOLD 0xE5
#define SHT25_TEMP_NO_HOLD 0xF3 #define SHT25_ERROR (-1)
#define SHT25_HUM_NO_HOLD 0xF5 #define SHT25_SUCCESS 0x00
#define SHT2X_UREG_WRITE 0xE6 #define SHT25_RESET_DELAY 15000
#define SHT2X_UREG_READ 0xE7 #define SHT25_STATUS_BITS_MASK 0x0003
#define SHT2X_SOFT_RESET 0XFE
#define SHT2X_NULL 0x00
/* -------------------------------------------------------------------------- */
#define SHT2X_RES_14T_12RH 0x00
#define SHT2X_RES_12T_08RH 0x01
#define SHT2X_RES_13T_10RH 0x80
#define SHT2X_RES_11T_11RH 0x81
#define SHT2X_HEATER_ON 0x04
#define SHT2X_HEATER_OFF 0x00
#define SHT2X_OTP_RELOAD_EN 0x00
#define SHT2X_OTP_RELOAD_DIS 0x02
/* -------------------------------------------------------------------------- */
#define SHT25_VAL_TEMP SHT25_TEMP_HOLD
#define SHT25_VAL_HUM SHT25_HUM_HOLD
#define SHT25_ERROR -1
#define SHT25_SUCCESS 0x00
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
#define SHT25_SENSOR "SHT25 Sensor" #define SHT25_SENSOR "SHT25 Sensor"
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */