This is a library to read out sensor values from Silicon Labs' si70xx-range of relative humidity and temperature sensors.

Dependents:   MemLCD-Temperature-Humidity-Demo lab123

Information

All examples in this repo are considered EXPERIMENTAL QUALITY, meaning this code has been created as one-off proof-of-concept and is suitable as a demonstration for experimental purposes only. This code will not be regularly maintained by Silicon Labs and there is no guarantee that these projects will work across all environments, SDK versions and hardware.

Caution

This library targets mbed's asynchronous transfer APIs, so it can only be used in conjunction with platforms supporting these APIs.

The library is currently compatible with Si7013, Si7020 and Si7021 parts.

Usage

Include mbed low-power to use this driver

#include "mbed.h"
#include "SILABS_RHT.h"
 
I2C sensorI2C(PD6, PD7); //PD6=SDA, PD7=SCL
silabs::SILABS_RHT rhtSensor(&sensorI2C);
 
volatile bool busChecked = false;
 
void respondedCallback( void ) {
    busChecked = true;
}

int main() {
    rhtSensor.check_availability(si7021, respondedCallback);
    while(busChecked == false) sleep();
    
    busChecked = false;
    rhtSensor.measure(si7021, respondedCallback);
    while(busChecked == false) sleep();

    if(rhtSensor.get_active()) {
        printf("Temperature: %d.%03d degC\n", rhtSensor.get_Temperature()/1000, rhtSensor.get_Temperature()%1000);
    } else {
        printf("No sensor found\n");
    }

    while(1) sleep();
}

Datasheets

http://www.silabs.com/products/sensors/humidity-sensors/Pages/si7013-20-21.aspx

Committer:
Steven Cooreman
Date:
Tue Mar 17 13:46:44 2015 -0500
Revision:
0:9fd18754e0c0
Child:
1:f3c25dea392e
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Steven Cooreman 0:9fd18754e0c0 1 /***************************************************************************//**
Steven Cooreman 0:9fd18754e0c0 2 * @file SILABS_RHT.h
Steven Cooreman 0:9fd18754e0c0 3 * @brief Driver class for the Silicon Labs si70xx series I2C RHT sensors.
Steven Cooreman 0:9fd18754e0c0 4 *******************************************************************************
Steven Cooreman 0:9fd18754e0c0 5 * @section License
Steven Cooreman 0:9fd18754e0c0 6 * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
Steven Cooreman 0:9fd18754e0c0 7 *******************************************************************************
Steven Cooreman 0:9fd18754e0c0 8 *
Steven Cooreman 0:9fd18754e0c0 9 * Permission is granted to anyone to use this software for any purpose,
Steven Cooreman 0:9fd18754e0c0 10 * including commercial applications, and to alter it and redistribute it
Steven Cooreman 0:9fd18754e0c0 11 * freely, subject to the following restrictions:
Steven Cooreman 0:9fd18754e0c0 12 *
Steven Cooreman 0:9fd18754e0c0 13 * 1. The origin of this software must not be misrepresented; you must not
Steven Cooreman 0:9fd18754e0c0 14 * claim that you wrote the original software.
Steven Cooreman 0:9fd18754e0c0 15 * 2. Altered source versions must be plainly marked as such, and must not be
Steven Cooreman 0:9fd18754e0c0 16 * misrepresented as being the original software.
Steven Cooreman 0:9fd18754e0c0 17 * 3. This notice may not be removed or altered from any source distribution.
Steven Cooreman 0:9fd18754e0c0 18 *
Steven Cooreman 0:9fd18754e0c0 19 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
Steven Cooreman 0:9fd18754e0c0 20 * obligation to support this Software. Silicon Labs is providing the
Steven Cooreman 0:9fd18754e0c0 21 * Software "AS IS", with no express or implied warranties of any kind,
Steven Cooreman 0:9fd18754e0c0 22 * including, but not limited to, any implied warranties of merchantability
Steven Cooreman 0:9fd18754e0c0 23 * or fitness for any particular purpose or warranties against infringement
Steven Cooreman 0:9fd18754e0c0 24 * of any proprietary rights of a third party.
Steven Cooreman 0:9fd18754e0c0 25 *
Steven Cooreman 0:9fd18754e0c0 26 * Silicon Labs will not be liable for any consequential, incidental, or
Steven Cooreman 0:9fd18754e0c0 27 * special damages, or any other relief, or for any claim by any third party,
Steven Cooreman 0:9fd18754e0c0 28 * arising from your use of this Software.
Steven Cooreman 0:9fd18754e0c0 29 *
Steven Cooreman 0:9fd18754e0c0 30 ******************************************************************************/
Steven Cooreman 0:9fd18754e0c0 31
Steven Cooreman 0:9fd18754e0c0 32 #ifndef SILABS_RHT_H
Steven Cooreman 0:9fd18754e0c0 33 #define SILABS_RHT_H
Steven Cooreman 0:9fd18754e0c0 34
Steven Cooreman 0:9fd18754e0c0 35 #include "platform.h"
Steven Cooreman 0:9fd18754e0c0 36 #include <mbed.h>
Steven Cooreman 0:9fd18754e0c0 37 #include "CallbackPointer.h"
Steven Cooreman 0:9fd18754e0c0 38
Steven Cooreman 0:9fd18754e0c0 39 typedef void (*cbptr_t)(void);
Steven Cooreman 0:9fd18754e0c0 40
Steven Cooreman 0:9fd18754e0c0 41 #define SILABS_RHT_ERROR_BUSY -1
Steven Cooreman 0:9fd18754e0c0 42 #define SILABS_RHT_ERROR_I2C_BUSY -2
Steven Cooreman 0:9fd18754e0c0 43 #define SILABS_RHT_NO_ACTION -3
Steven Cooreman 0:9fd18754e0c0 44 #define SILABS_RHT_ERROR_ARGUMENT -4
Steven Cooreman 0:9fd18754e0c0 45 #define SILABS_RHT_OK 0
Steven Cooreman 0:9fd18754e0c0 46
Steven Cooreman 0:9fd18754e0c0 47 typedef enum {
Steven Cooreman 0:9fd18754e0c0 48 RHT_IDLE, // Object initialized
Steven Cooreman 0:9fd18754e0c0 49 RHT_ACTIVE, // Object initialized and sensor available
Steven Cooreman 0:9fd18754e0c0 50 RHT_MEASURING, // In the process of getting an RH measurement
Steven Cooreman 0:9fd18754e0c0 51 RHT_TEMPERATURE, // In the process of reading temperature measurement
Steven Cooreman 0:9fd18754e0c0 52 RHT_PINGING, // In the process of polling the device
Steven Cooreman 0:9fd18754e0c0 53 RHT_FWREV // In the process of reading the device's firmware revision
Steven Cooreman 0:9fd18754e0c0 54 } SILABS_RHT_state_t;
Steven Cooreman 0:9fd18754e0c0 55
Steven Cooreman 0:9fd18754e0c0 56 /** Supported devices */
Steven Cooreman 0:9fd18754e0c0 57 typedef enum {
Steven Cooreman 0:9fd18754e0c0 58 si7013,
Steven Cooreman 0:9fd18754e0c0 59 si7020,
Steven Cooreman 0:9fd18754e0c0 60 si7021
Steven Cooreman 0:9fd18754e0c0 61 } SILABS_RHT_device_t;
Steven Cooreman 0:9fd18754e0c0 62
Steven Cooreman 0:9fd18754e0c0 63 namespace silabs {
Steven Cooreman 0:9fd18754e0c0 64 /** A driver for the Silicon Labs si70xx series of I2C RHT sensors.
Steven Cooreman 0:9fd18754e0c0 65 *
Steven Cooreman 0:9fd18754e0c0 66 * Currently supports si7013, si7020 and si7021.
Steven Cooreman 0:9fd18754e0c0 67 *
Steven Cooreman 0:9fd18754e0c0 68 * @code
Steven Cooreman 0:9fd18754e0c0 69 * #include "mbed.h"
Steven Cooreman 0:9fd18754e0c0 70 * #include "SILABS_RHT.h"
Steven Cooreman 0:9fd18754e0c0 71 *
Steven Cooreman 0:9fd18754e0c0 72 * I2C sensorI2C(PD6, PD7); //PD6=SDA, PD7=SCL
Steven Cooreman 0:9fd18754e0c0 73 * silabs::SILABS_RHT rhtSensor(&sensorI2C);
Steven Cooreman 0:9fd18754e0c0 74 *
Steven Cooreman 0:9fd18754e0c0 75 * volatile bool busChecked = false;
Steven Cooreman 0:9fd18754e0c0 76 *
Steven Cooreman 0:9fd18754e0c0 77 * void measureCallback( void ) {
Steven Cooreman 0:9fd18754e0c0 78 * if(rhtSensor.get_active()) {
Steven Cooreman 0:9fd18754e0c0 79 * printf("Temperature: %d.%03d degC\n", rhtSensor.get_Temperature()/1000, rhtSensor.get_Temperature()%1000);
Steven Cooreman 0:9fd18754e0c0 80 * } else {
Steven Cooreman 0:9fd18754e0c0 81 * printf("No sensor found\n");
Steven Cooreman 0:9fd18754e0c0 82 * }
Steven Cooreman 0:9fd18754e0c0 83 * }
Steven Cooreman 0:9fd18754e0c0 84 *
Steven Cooreman 0:9fd18754e0c0 85 * void respondedCallback( void ) {
Steven Cooreman 0:9fd18754e0c0 86 * busChecked = true;
Steven Cooreman 0:9fd18754e0c0 87 * }
Steven Cooreman 0:9fd18754e0c0 88 *
Steven Cooreman 0:9fd18754e0c0 89 * int main() {
Steven Cooreman 0:9fd18754e0c0 90 * rhtSensor.check_availability(si7021, respondedCallback);
Steven Cooreman 0:9fd18754e0c0 91 * while(busChecked == false) sleep();
Steven Cooreman 0:9fd18754e0c0 92 *
Steven Cooreman 0:9fd18754e0c0 93 * rhtSensor.measure(si7021, measureCallback);
Steven Cooreman 0:9fd18754e0c0 94 * while(1) sleep();
Steven Cooreman 0:9fd18754e0c0 95 * }
Steven Cooreman 0:9fd18754e0c0 96 * @endcode
Steven Cooreman 0:9fd18754e0c0 97 */
Steven Cooreman 0:9fd18754e0c0 98 class SILABS_RHT {
Steven Cooreman 0:9fd18754e0c0 99
Steven Cooreman 0:9fd18754e0c0 100 public:
Steven Cooreman 0:9fd18754e0c0 101
Steven Cooreman 0:9fd18754e0c0 102 /**
Steven Cooreman 0:9fd18754e0c0 103 * Constructor.
Steven Cooreman 0:9fd18754e0c0 104 *
Steven Cooreman 0:9fd18754e0c0 105 * @param i2c pointer to active mbed I2C interface object.
Steven Cooreman 0:9fd18754e0c0 106 */
Steven Cooreman 0:9fd18754e0c0 107 SILABS_RHT(I2C * i2c);
Steven Cooreman 0:9fd18754e0c0 108
Steven Cooreman 0:9fd18754e0c0 109 /**
Steven Cooreman 0:9fd18754e0c0 110 * Get last measured temperature data
Steven Cooreman 0:9fd18754e0c0 111 *
Steven Cooreman 0:9fd18754e0c0 112 * @return temperature in millidegrees centigrade
Steven Cooreman 0:9fd18754e0c0 113 */
Steven Cooreman 0:9fd18754e0c0 114 int32_t get_temperature();
Steven Cooreman 0:9fd18754e0c0 115
Steven Cooreman 0:9fd18754e0c0 116 /**
Steven Cooreman 0:9fd18754e0c0 117 * Get last measured relative humidity data
Steven Cooreman 0:9fd18754e0c0 118 *
Steven Cooreman 0:9fd18754e0c0 119 * @return relative humidity value in milli-percent
Steven Cooreman 0:9fd18754e0c0 120 */
Steven Cooreman 0:9fd18754e0c0 121 uint32_t get_humidity();
Steven Cooreman 0:9fd18754e0c0 122
Steven Cooreman 0:9fd18754e0c0 123 /**
Steven Cooreman 0:9fd18754e0c0 124 * Get current state of the sensor, active or inactive. Need to call check_availability first after initialization.
Steven Cooreman 0:9fd18754e0c0 125 *
Steven Cooreman 0:9fd18754e0c0 126 * @return true if the last operation (check_active or measure) was a success, false if not (device did not respond).
Steven Cooreman 0:9fd18754e0c0 127 */
Steven Cooreman 0:9fd18754e0c0 128 bool get_active();
Steven Cooreman 0:9fd18754e0c0 129
Steven Cooreman 0:9fd18754e0c0 130 /**
Steven Cooreman 0:9fd18754e0c0 131 * Perform and read back measurement.
Steven Cooreman 0:9fd18754e0c0 132 *
Steven Cooreman 0:9fd18754e0c0 133 * @param deviceType Device signature to check for.
Steven Cooreman 0:9fd18754e0c0 134 * @param callback Asynchronous callback can be provided (type void (*)(void)). If null, no callback will get called.
Steven Cooreman 0:9fd18754e0c0 135 * @return 0 if successful, else one of the defined error codes.
Steven Cooreman 0:9fd18754e0c0 136 */
Steven Cooreman 0:9fd18754e0c0 137 int measure(SILABS_RHT_device_t deviceType, cbptr_t callback = NULL);
Steven Cooreman 0:9fd18754e0c0 138
Steven Cooreman 0:9fd18754e0c0 139 /**
Steven Cooreman 0:9fd18754e0c0 140 * Check if the sensor is active and responding. This will update the get_active value.
Steven Cooreman 0:9fd18754e0c0 141 *
Steven Cooreman 0:9fd18754e0c0 142 * @param deviceType Device signature to check for.
Steven Cooreman 0:9fd18754e0c0 143 * @param callback Asynchronous callback can be provided (type void (*)(void)). If null, no callback will get called.
Steven Cooreman 0:9fd18754e0c0 144 * @return 0 if successful, else one of the defined error codes.
Steven Cooreman 0:9fd18754e0c0 145 */
Steven Cooreman 0:9fd18754e0c0 146 int check_availability(SILABS_RHT_device_t deviceType, cbptr_t callback = NULL);
Steven Cooreman 0:9fd18754e0c0 147
Steven Cooreman 0:9fd18754e0c0 148 protected:
Steven Cooreman 0:9fd18754e0c0 149 mbed::I2C *_i2c;
Steven Cooreman 0:9fd18754e0c0 150
Steven Cooreman 0:9fd18754e0c0 151 mbed::CallbackPointer _internalCallback;
Steven Cooreman 0:9fd18754e0c0 152 SILABS_RHT_state_t _state;
Steven Cooreman 0:9fd18754e0c0 153 cbptr_t _completionCallbackPtr;
Steven Cooreman 0:9fd18754e0c0 154
Steven Cooreman 0:9fd18754e0c0 155 uint8_t _address;
Steven Cooreman 0:9fd18754e0c0 156 uint8_t _rx_buf[8];
Steven Cooreman 0:9fd18754e0c0 157 uint8_t _tx_buf[2];
Steven Cooreman 0:9fd18754e0c0 158
Steven Cooreman 0:9fd18754e0c0 159 uint32_t _rhData;
Steven Cooreman 0:9fd18754e0c0 160 int32_t _tData;
Steven Cooreman 0:9fd18754e0c0 161
Steven Cooreman 0:9fd18754e0c0 162
Steven Cooreman 0:9fd18754e0c0 163
Steven Cooreman 0:9fd18754e0c0 164 /*
Steven Cooreman 0:9fd18754e0c0 165 * Callback handler for internal I2C transfers.
Steven Cooreman 0:9fd18754e0c0 166 */
Steven Cooreman 0:9fd18754e0c0 167 void _cbHandler( int event );
Steven Cooreman 0:9fd18754e0c0 168
Steven Cooreman 0:9fd18754e0c0 169 /*
Steven Cooreman 0:9fd18754e0c0 170 * Callback handler for internal I2C transfers triggered by timeout.
Steven Cooreman 0:9fd18754e0c0 171 */
Steven Cooreman 0:9fd18754e0c0 172 void _cbHandlerTimeout( void );
Steven Cooreman 0:9fd18754e0c0 173
Steven Cooreman 0:9fd18754e0c0 174 /*
Steven Cooreman 0:9fd18754e0c0 175 * Internal lookup table for I2C address by device type.
Steven Cooreman 0:9fd18754e0c0 176 */
Steven Cooreman 0:9fd18754e0c0 177 uint8_t get_address(SILABS_RHT_device_t device);
Steven Cooreman 0:9fd18754e0c0 178 };
Steven Cooreman 0:9fd18754e0c0 179
Steven Cooreman 0:9fd18754e0c0 180 } // namespace silabs
Steven Cooreman 0:9fd18754e0c0 181
Steven Cooreman 0:9fd18754e0c0 182 #endif //SILABS_RHT_H