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
Diff: SILABS_RHT.h
- Revision:
- 0:9fd18754e0c0
- Child:
- 1:f3c25dea392e
diff -r 000000000000 -r 9fd18754e0c0 SILABS_RHT.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SILABS_RHT.h Tue Mar 17 13:46:44 2015 -0500 @@ -0,0 +1,182 @@ +/***************************************************************************//** + * @file SILABS_RHT.h + * @brief Driver class for the Silicon Labs si70xx series I2C RHT sensors. + ******************************************************************************* + * @section License + * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b> + ******************************************************************************* + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no + * obligation to support this Software. Silicon Labs is providing the + * Software "AS IS", with no express or implied warranties of any kind, + * including, but not limited to, any implied warranties of merchantability + * or fitness for any particular purpose or warranties against infringement + * of any proprietary rights of a third party. + * + * Silicon Labs will not be liable for any consequential, incidental, or + * special damages, or any other relief, or for any claim by any third party, + * arising from your use of this Software. + * + ******************************************************************************/ + +#ifndef SILABS_RHT_H +#define SILABS_RHT_H + +#include "platform.h" +#include <mbed.h> +#include "CallbackPointer.h" + +typedef void (*cbptr_t)(void); + +#define SILABS_RHT_ERROR_BUSY -1 +#define SILABS_RHT_ERROR_I2C_BUSY -2 +#define SILABS_RHT_NO_ACTION -3 +#define SILABS_RHT_ERROR_ARGUMENT -4 +#define SILABS_RHT_OK 0 + +typedef enum { + RHT_IDLE, // Object initialized + RHT_ACTIVE, // Object initialized and sensor available + RHT_MEASURING, // In the process of getting an RH measurement + RHT_TEMPERATURE, // In the process of reading temperature measurement + RHT_PINGING, // In the process of polling the device + RHT_FWREV // In the process of reading the device's firmware revision +} SILABS_RHT_state_t; + +/** Supported devices */ +typedef enum { + si7013, + si7020, + si7021 +} SILABS_RHT_device_t; + +namespace silabs { +/** A driver for the Silicon Labs si70xx series of I2C RHT sensors. + * + * Currently supports si7013, si7020 and si7021. + * + * @code + * #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 measureCallback( void ) { + * if(rhtSensor.get_active()) { + * printf("Temperature: %d.%03d degC\n", rhtSensor.get_Temperature()/1000, rhtSensor.get_Temperature()%1000); + * } else { + * printf("No sensor found\n"); + * } + * } + * + * void respondedCallback( void ) { + * busChecked = true; + * } + * + * int main() { + * rhtSensor.check_availability(si7021, respondedCallback); + * while(busChecked == false) sleep(); + * + * rhtSensor.measure(si7021, measureCallback); + * while(1) sleep(); + * } + * @endcode + */ +class SILABS_RHT { + +public: + + /** + * Constructor. + * + * @param i2c pointer to active mbed I2C interface object. + */ + SILABS_RHT(I2C * i2c); + + /** + * Get last measured temperature data + * + * @return temperature in millidegrees centigrade + */ + int32_t get_temperature(); + + /** + * Get last measured relative humidity data + * + * @return relative humidity value in milli-percent + */ + uint32_t get_humidity(); + + /** + * Get current state of the sensor, active or inactive. Need to call check_availability first after initialization. + * + * @return true if the last operation (check_active or measure) was a success, false if not (device did not respond). + */ + bool get_active(); + + /** + * Perform and read back measurement. + * + * @param deviceType Device signature to check for. + * @param callback Asynchronous callback can be provided (type void (*)(void)). If null, no callback will get called. + * @return 0 if successful, else one of the defined error codes. + */ + int measure(SILABS_RHT_device_t deviceType, cbptr_t callback = NULL); + + /** + * Check if the sensor is active and responding. This will update the get_active value. + * + * @param deviceType Device signature to check for. + * @param callback Asynchronous callback can be provided (type void (*)(void)). If null, no callback will get called. + * @return 0 if successful, else one of the defined error codes. + */ + int check_availability(SILABS_RHT_device_t deviceType, cbptr_t callback = NULL); + +protected: + mbed::I2C *_i2c; + + mbed::CallbackPointer _internalCallback; + SILABS_RHT_state_t _state; + cbptr_t _completionCallbackPtr; + + uint8_t _address; + uint8_t _rx_buf[8]; + uint8_t _tx_buf[2]; + + uint32_t _rhData; + int32_t _tData; + + + + /* + * Callback handler for internal I2C transfers. + */ + void _cbHandler( int event ); + + /* + * Callback handler for internal I2C transfers triggered by timeout. + */ + void _cbHandlerTimeout( void ); + + /* + * Internal lookup table for I2C address by device type. + */ + uint8_t get_address(SILABS_RHT_device_t device); +}; + +} // namespace silabs + +#endif //SILABS_RHT_H