From 97a7e98ac57af272793a0472d0d3e2c5db16806e Mon Sep 17 00:00:00 2001 From: Antonio Lignan Date: Sun, 10 Jan 2016 19:37:48 +0100 Subject: [PATCH 1/3] Fixed SHT25 sensor --- platform/zoul/dev/sht25.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/platform/zoul/dev/sht25.c b/platform/zoul/dev/sht25.c index 0ad7c31f6..df12e7f55 100644 --- a/platform/zoul/dev/sht25.c +++ b/platform/zoul/dev/sht25.c @@ -46,20 +46,30 @@ #include "dev/sht25.h" #include "lib/sensors.h" /*---------------------------------------------------------------------------*/ +#define DEBUG 0 +#if DEBUG +#define PRINTF(...) printf(__VA_ARGS__) +#else +#define PRINTF(...) +#endif +/*---------------------------------------------------------------------------*/ static uint8_t enabled; /*---------------------------------------------------------------------------*/ static int configure(int type, int value) { if(type != SENSORS_ACTIVE) { + PRINTF("SHT25: option not supported\n"); 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; + return SHT25_SUCCESS; } /*---------------------------------------------------------------------------*/ static int @@ -76,6 +86,7 @@ status(int type) static uint16_t sht25_read_reg(uint8_t reg, uint8_t *buf, uint8_t regNum) { + i2c_master_enable(); if(i2c_single_send(SHT25_ADDR, reg) == I2C_MASTER_ERR_NONE) { if(i2c_burst_receive(SHT25_ADDR, buf, regNum) == I2C_MASTER_ERR_NONE) { return SHT25_SUCCESS; @@ -110,6 +121,7 @@ sht25_read(uint8_t variable, uint16_t *rd) uint16_t raw; if((variable != SHT25_VAL_TEMP) && (variable != SHT25_VAL_HUM)) { + PRINTF("SHT25: invalid sensor requested\n"); return SHT25_ERROR; } @@ -118,6 +130,8 @@ sht25_read(uint8_t variable, uint16_t *rd) *rd = sht25_convert(variable, raw); return SHT25_SUCCESS; } + + PRINTF("SHT25: failed to read sensor\n"); return SHT25_ERROR; } /*---------------------------------------------------------------------------*/ @@ -125,6 +139,12 @@ static int value(int type) { uint16_t val; + + if(!enabled) { + PRINTF("SHT25: sensor not started\n"); + return SHT25_ERROR; + } + if(sht25_read(type, &val) == SHT25_SUCCESS) { return val; } From d53a62187d1498e837475ec1c4e77db2fa825da8 Mon Sep 17 00:00:00 2001 From: Antonio Lignan Date: Sun, 10 Jan 2016 21:10:40 +0100 Subject: [PATCH 2/3] Added extra features of the SHT25 sensor --- examples/zolertia/zoul/test-sht25.c | 20 ++++- platform/zoul/dev/sht25.c | 134 +++++++++++++++++++++++----- platform/zoul/dev/sht25.h | 59 ++++++------ 3 files changed, 162 insertions(+), 51 deletions(-) diff --git a/examples/zolertia/zoul/test-sht25.c b/examples/zolertia/zoul/test-sht25.c index bb403f61c..a2e264220 100644 --- a/examples/zolertia/zoul/test-sht25.c +++ b/examples/zolertia/zoul/test-sht25.c @@ -60,13 +60,29 @@ PROCESS_THREAD(remote_sht25_process, ev, data) PROCESS_BEGIN(); 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) { etimer_set(&et, CLOCK_SECOND); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); 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); - printf("Humidity %d.%d RH\n", humidity / 100, humidity % 100); + printf("Humidity %02d.%02d RH\n", humidity / 100, humidity % 100); } PROCESS_END(); } diff --git a/platform/zoul/dev/sht25.c b/platform/zoul/dev/sht25.c index df12e7f55..f46c42c89 100644 --- a/platform/zoul/dev/sht25.c +++ b/platform/zoul/dev/sht25.c @@ -46,7 +46,7 @@ #include "dev/sht25.h" #include "lib/sensors.h" /*---------------------------------------------------------------------------*/ -#define DEBUG 0 +#define DEBUG 1 #if DEBUG #define PRINTF(...) printf(__VA_ARGS__) #else @@ -54,23 +54,7 @@ #endif /*---------------------------------------------------------------------------*/ static uint8_t enabled; -/*---------------------------------------------------------------------------*/ -static int -configure(int type, int value) -{ - if(type != SENSORS_ACTIVE) { - PRINTF("SHT25: option not supported\n"); - 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 SHT25_SUCCESS; -} +static uint8_t user_reg; /*---------------------------------------------------------------------------*/ static int status(int type) @@ -84,11 +68,15 @@ status(int type) } /*---------------------------------------------------------------------------*/ 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_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; } } @@ -100,7 +88,10 @@ sht25_convert(uint8_t variable, uint16_t value) { int16_t rd; uint32_t buff; - buff = (uint32_t)value; + + /* Clear the status bits */ + buff = (uint32_t)(value & ~SHT25_STATUS_BITS_MASK); + if(variable == SHT25_VAL_TEMP) { buff *= 17572; buff = buff >> 16; @@ -114,7 +105,7 @@ sht25_convert(uint8_t variable, uint16_t value) return rd; } /*---------------------------------------------------------------------------*/ -static int16_t +static int sht25_read(uint8_t variable, uint16_t *rd) { uint8_t buf[2]; @@ -136,6 +127,32 @@ sht25_read(uint8_t variable, uint16_t *rd) } /*---------------------------------------------------------------------------*/ 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; +} +/*---------------------------------------------------------------------------*/ +static int value(int type) { uint16_t val; @@ -145,9 +162,78 @@ value(int type) return SHT25_ERROR; } - if(sht25_read(type, &val) == SHT25_SUCCESS) { - return val; + 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; } /*---------------------------------------------------------------------------*/ diff --git a/platform/zoul/dev/sht25.h b/platform/zoul/dev/sht25.h index df7dbcc77..602dd1331 100644 --- a/platform/zoul/dev/sht25.h +++ b/platform/zoul/dev/sht25.h @@ -44,34 +44,43 @@ */ /*---------------------------------------------------------------------------*/ #include "lib/sensors.h" - +/* -------------------------------------------------------------------------- */ #ifndef 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_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 SHT25_VAL_TEMP SHT25_TEMP_HOLD -#define SHT25_VAL_HUM SHT25_HUM_HOLD -#define SHT25_ERROR -1 -#define SHT25_SUCCESS 0x00 +#define SHT25_VAL_TEMP SHT25_TEMP_HOLD +#define SHT25_VAL_HUM SHT25_HUM_HOLD +#define SHT25_VOLTAGE_ALARM 0x01 + +#define SHT25_ERROR (-1) +#define SHT25_SUCCESS 0x00 +#define SHT25_RESET_DELAY 15000 +#define SHT25_STATUS_BITS_MASK 0x0003 /* -------------------------------------------------------------------------- */ #define SHT25_SENSOR "SHT25 Sensor" /* -------------------------------------------------------------------------- */ From 48eb2a4fe2dca93a6f27bc909dc3bb50a8c0b9c6 Mon Sep 17 00:00:00 2001 From: Antonio Lignan Date: Sun, 10 Jan 2016 23:36:00 +0100 Subject: [PATCH 3/3] Uncrustify changes --- examples/zolertia/zoul/test-sht25.c | 2 +- platform/zoul/dev/sht25.c | 76 ++++++++++++++--------------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/examples/zolertia/zoul/test-sht25.c b/examples/zolertia/zoul/test-sht25.c index a2e264220..ea853c30b 100644 --- a/examples/zolertia/zoul/test-sht25.c +++ b/examples/zolertia/zoul/test-sht25.c @@ -62,7 +62,7 @@ PROCESS_THREAD(remote_sht25_process, ev, data) /* 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"); + printf("Voltage is lower than recommended for the sensor operation\n"); PROCESS_EXIT(); } diff --git a/platform/zoul/dev/sht25.c b/platform/zoul/dev/sht25.c index f46c42c89..38a8be12e 100644 --- a/platform/zoul/dev/sht25.c +++ b/platform/zoul/dev/sht25.c @@ -163,14 +163,14 @@ value(int type) } if((type != SHT25_VAL_TEMP) && (type != SHT25_VAL_HUM) && - (type != SHT25_VOLTAGE_ALARM)) { + (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); + return (user_reg & SHT2x_LOW_VOLTAGE_MASK) >> SHT2x_LOW_VOLTAGE_SHIFT; } } else { if(sht25_read(type, &val) == SHT25_SUCCESS) { @@ -186,52 +186,52 @@ configure(int type, int value) uint8_t buf[2]; if((type != SHT25_ACTIVE) && (type != SHT25_SOFT_RESET) && - (type != SHT25_RESOLUTION)) { + (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); + 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); + /* Read the user config register */ + if(sht25_read_user_register() == SHT25_SUCCESS) { + enabled = value; return SHT25_SUCCESS; } + } - default: + 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;