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 Apr 14 15:27:17 2015 -0400
Revision:
1:f3c25dea392e
Parent:
0:9fd18754e0c0
Child:
2:83a8b7df827f
Move to event_callback_t for xfer callbacks instead of void pointers.

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 1:f3c25dea392e 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 typedef enum {
Steven Cooreman 0:9fd18754e0c0 57 si7013,
Steven Cooreman 0:9fd18754e0c0 58 si7020,
Steven Cooreman 0:9fd18754e0c0 59 si7021
Steven Cooreman 0:9fd18754e0c0 60 } SILABS_RHT_device_t;
Steven Cooreman 0:9fd18754e0c0 61
Steven Cooreman 0:9fd18754e0c0 62 namespace silabs {
Steven Cooreman 0:9fd18754e0c0 63 class SILABS_RHT {
Steven Cooreman 0:9fd18754e0c0 64
Steven Cooreman 0:9fd18754e0c0 65 public:
Steven Cooreman 0:9fd18754e0c0 66
Steven Cooreman 0:9fd18754e0c0 67 SILABS_RHT(I2C * i2c);
Steven Cooreman 0:9fd18754e0c0 68
Steven Cooreman 1:f3c25dea392e 69 /*
Steven Cooreman 0:9fd18754e0c0 70 * Get last measured temperature data
Steven Cooreman 1:f3c25dea392e 71 * return: int32_t = temperature in millidegrees centigrade
Steven Cooreman 0:9fd18754e0c0 72 */
Steven Cooreman 0:9fd18754e0c0 73 int32_t get_temperature();
Steven Cooreman 0:9fd18754e0c0 74
Steven Cooreman 1:f3c25dea392e 75 /*
Steven Cooreman 0:9fd18754e0c0 76 * Get last measured relative humidity data
Steven Cooreman 1:f3c25dea392e 77 * return: uint32_t = relative humidity value in milli-percent
Steven Cooreman 0:9fd18754e0c0 78 */
Steven Cooreman 0:9fd18754e0c0 79 uint32_t get_humidity();
Steven Cooreman 0:9fd18754e0c0 80
Steven Cooreman 1:f3c25dea392e 81 /*
Steven Cooreman 0:9fd18754e0c0 82 * Get current state of the sensor, active or inactive. Need to call check_availability first after initialization.
Steven Cooreman 1:f3c25dea392e 83 * return: true if the last operation (check_active or measure) was a success, false if not (device did not respond).
Steven Cooreman 0:9fd18754e0c0 84 */
Steven Cooreman 0:9fd18754e0c0 85 bool get_active();
Steven Cooreman 0:9fd18754e0c0 86
Steven Cooreman 1:f3c25dea392e 87 /*
Steven Cooreman 1:f3c25dea392e 88 * Perform measurement.
Steven Cooreman 1:f3c25dea392e 89 * Asynchronous callback can be provided (type void (*)(void)).
Steven Cooreman 1:f3c25dea392e 90 * return: 0 if successful, else one of the defined error codes.
Steven Cooreman 0:9fd18754e0c0 91 */
Steven Cooreman 0:9fd18754e0c0 92 int measure(SILABS_RHT_device_t deviceType, cbptr_t callback = NULL);
Steven Cooreman 0:9fd18754e0c0 93
Steven Cooreman 1:f3c25dea392e 94 /*
Steven Cooreman 0:9fd18754e0c0 95 * Check if the sensor is active and responding. This will update the get_active value.
Steven Cooreman 1:f3c25dea392e 96 * Asynchronous callback can be provided (type void (*)(void)).
Steven Cooreman 1:f3c25dea392e 97 * return: 0 if successful, else one of the defined error codes.
Steven Cooreman 0:9fd18754e0c0 98 */
Steven Cooreman 0:9fd18754e0c0 99 int check_availability(SILABS_RHT_device_t deviceType, cbptr_t callback = NULL);
Steven Cooreman 0:9fd18754e0c0 100
Steven Cooreman 0:9fd18754e0c0 101 protected:
Steven Cooreman 0:9fd18754e0c0 102 mbed::I2C *_i2c;
Steven Cooreman 0:9fd18754e0c0 103
Steven Cooreman 1:f3c25dea392e 104 event_callback_t _internalCallback;
Steven Cooreman 0:9fd18754e0c0 105 SILABS_RHT_state_t _state;
Steven Cooreman 0:9fd18754e0c0 106 cbptr_t _completionCallbackPtr;
Steven Cooreman 0:9fd18754e0c0 107
Steven Cooreman 0:9fd18754e0c0 108 uint8_t _address;
Steven Cooreman 0:9fd18754e0c0 109 uint8_t _rx_buf[8];
Steven Cooreman 0:9fd18754e0c0 110 uint8_t _tx_buf[2];
Steven Cooreman 0:9fd18754e0c0 111
Steven Cooreman 0:9fd18754e0c0 112 uint32_t _rhData;
Steven Cooreman 0:9fd18754e0c0 113 int32_t _tData;
Steven Cooreman 0:9fd18754e0c0 114
Steven Cooreman 0:9fd18754e0c0 115
Steven Cooreman 0:9fd18754e0c0 116
Steven Cooreman 1:f3c25dea392e 117 /**
Steven Cooreman 0:9fd18754e0c0 118 * Callback handler for internal I2C transfers.
Steven Cooreman 0:9fd18754e0c0 119 */
Steven Cooreman 0:9fd18754e0c0 120 void _cbHandler( int event );
Steven Cooreman 0:9fd18754e0c0 121
Steven Cooreman 1:f3c25dea392e 122 /**
Steven Cooreman 0:9fd18754e0c0 123 * Callback handler for internal I2C transfers triggered by timeout.
Steven Cooreman 0:9fd18754e0c0 124 */
Steven Cooreman 0:9fd18754e0c0 125 void _cbHandlerTimeout( void );
Steven Cooreman 0:9fd18754e0c0 126
Steven Cooreman 1:f3c25dea392e 127 /**
Steven Cooreman 0:9fd18754e0c0 128 * Internal lookup table for I2C address by device type.
Steven Cooreman 0:9fd18754e0c0 129 */
Steven Cooreman 0:9fd18754e0c0 130 uint8_t get_address(SILABS_RHT_device_t device);
Steven Cooreman 0:9fd18754e0c0 131 };
Steven Cooreman 0:9fd18754e0c0 132
Steven Cooreman 0:9fd18754e0c0 133 } // namespace silabs
Steven Cooreman 0:9fd18754e0c0 134
Steven Cooreman 0:9fd18754e0c0 135 #endif //SILABS_RHT_H