Creation

Committer:
UsmanKhan
Date:
Wed Jun 06 16:12:05 2018 +0500
Revision:
2:df51c015ac67
added drivers si7034, on board humidity temperature sensor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
UsmanKhan 2:df51c015ac67 1 /*******************************************************************************
UsmanKhan 2:df51c015ac67 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
UsmanKhan 2:df51c015ac67 3 *
UsmanKhan 2:df51c015ac67 4 * Permission is hereby granted, free of charge, to any person obtaining a
UsmanKhan 2:df51c015ac67 5 * copy of this software and associated documentation files (the "Software"),
UsmanKhan 2:df51c015ac67 6 * to deal in the Software without restriction, including without limitation
UsmanKhan 2:df51c015ac67 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
UsmanKhan 2:df51c015ac67 8 * and/or sell copies of the Software, and to permit persons to whom the
UsmanKhan 2:df51c015ac67 9 * Software is furnished to do so, subject to the following conditions:
UsmanKhan 2:df51c015ac67 10 *
UsmanKhan 2:df51c015ac67 11 * The above copyright notice and this permission notice shall be included
UsmanKhan 2:df51c015ac67 12 * in all copies or substantial portions of the Software.
UsmanKhan 2:df51c015ac67 13 *
UsmanKhan 2:df51c015ac67 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
UsmanKhan 2:df51c015ac67 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
UsmanKhan 2:df51c015ac67 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
UsmanKhan 2:df51c015ac67 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
UsmanKhan 2:df51c015ac67 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
UsmanKhan 2:df51c015ac67 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
UsmanKhan 2:df51c015ac67 20 * OTHER DEALINGS IN THE SOFTWARE.
UsmanKhan 2:df51c015ac67 21 *
UsmanKhan 2:df51c015ac67 22 * Except as contained in this notice, the name of Maxim Integrated
UsmanKhan 2:df51c015ac67 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
UsmanKhan 2:df51c015ac67 24 * Products, Inc. Branding Policy.
UsmanKhan 2:df51c015ac67 25 *
UsmanKhan 2:df51c015ac67 26 * The mere transfer of this software does not imply any licenses
UsmanKhan 2:df51c015ac67 27 * of trade secrets, proprietary technology, copyrights, patents,
UsmanKhan 2:df51c015ac67 28 * trademarks, maskwork rights, or any other form of intellectual
UsmanKhan 2:df51c015ac67 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
UsmanKhan 2:df51c015ac67 30 * ownership rights.
UsmanKhan 2:df51c015ac67 31 *******************************************************************************
UsmanKhan 2:df51c015ac67 32 */
UsmanKhan 2:df51c015ac67 33
UsmanKhan 2:df51c015ac67 34 #ifndef _SI7030_H_
UsmanKhan 2:df51c015ac67 35 #define _SI7030_H_
UsmanKhan 2:df51c015ac67 36
UsmanKhan 2:df51c015ac67 37 #include "mbed.h"
UsmanKhan 2:df51c015ac67 38
UsmanKhan 2:df51c015ac67 39 /**
UsmanKhan 2:df51c015ac67 40 * Silicon Labs Si7020 Humidity and Temperature Sensor
UsmanKhan 2:df51c015ac67 41 *
UsmanKhan 2:df51c015ac67 42 * @code
UsmanKhan 2:df51c015ac67 43 * #include <stdio.h>
UsmanKhan 2:df51c015ac67 44 * #include "mbed.h"
UsmanKhan 2:df51c015ac67 45 * #include "Si7020.h"
UsmanKhan 2:df51c015ac67 46 *
UsmanKhan 2:df51c015ac67 47 * I2C i2c(I2C_SDA, I2C_SCL);
UsmanKhan 2:df51c015ac67 48 * Si7020 si(&i2c);
UsmanKhan 2:df51c015ac67 49 *
UsmanKhan 2:df51c015ac67 50 * int main()
UsmanKhan 2:df51c015ac67 51 * {
UsmanKhan 2:df51c015ac67 52 * while(1) {
UsmanKhan 2:df51c015ac67 53 *
UsmanKhan 2:df51c015ac67 54 * float humid;
UsmanKhan 2:df51c015ac67 55 * if(si.getHumidity(&humid) != 0) {
UsmanKhan 2:df51c015ac67 56 * printf("Error getting humidity\n");
UsmanKhan 2:df51c015ac67 57 * humid = -1;
UsmanKhan 2:df51c015ac67 58 * }
UsmanKhan 2:df51c015ac67 59 *
UsmanKhan 2:df51c015ac67 60 * float temp;
UsmanKhan 2:df51c015ac67 61 * if(si.getTemperature(&temp) != 0) {
UsmanKhan 2:df51c015ac67 62 * printf("Error getting temperature");
UsmanKhan 2:df51c015ac67 63 * temp = -1;
UsmanKhan 2:df51c015ac67 64 * }
UsmanKhan 2:df51c015ac67 65 * printf("Humidity = %f%% Temperature = %fC\n", humid, temp);
UsmanKhan 2:df51c015ac67 66 *
UsmanKhan 2:df51c015ac67 67 * wait(1);
UsmanKhan 2:df51c015ac67 68 * }
UsmanKhan 2:df51c015ac67 69 * }
UsmanKhan 2:df51c015ac67 70 * @endcode
UsmanKhan 2:df51c015ac67 71 */
UsmanKhan 2:df51c015ac67 72 class Si7034
UsmanKhan 2:df51c015ac67 73 {
UsmanKhan 2:df51c015ac67 74
UsmanKhan 2:df51c015ac67 75 public:
UsmanKhan 2:df51c015ac67 76
UsmanKhan 2:df51c015ac67 77 /**
UsmanKhan 2:df51c015ac67 78 * @brief Measurement resolution.
UsmanKhan 2:df51c015ac67 79 * @details Controls the resolution of the humidity and temperarure readings.
UsmanKhan 2:df51c015ac67 80 */
UsmanKhan 2:df51c015ac67 81 typedef enum {
UsmanKhan 2:df51c015ac67 82 RH_12b_TEMP_14b = 0x0, ///< 12 bits for RH, 14 bits for Temp
UsmanKhan 2:df51c015ac67 83 RH_8b_TEMP_12b = 0x1, ///< 8 bits for RH, 12 bits for Temp
UsmanKhan 2:df51c015ac67 84 RH_10b_TEMP_13b = 0x2, ///< 10 bits for RH, 13 bits for Temp
UsmanKhan 2:df51c015ac67 85 RH_11b_TEMP_11b = 0x3, ///< 11 bits for RH, 11 bits for Temp
UsmanKhan 2:df51c015ac67 86 } resolution_t;
UsmanKhan 2:df51c015ac67 87
UsmanKhan 2:df51c015ac67 88 /**
UsmanKhan 2:df51c015ac67 89 * Si7020 constructor.
UsmanKhan 2:df51c015ac67 90 *
UsmanKhan 2:df51c015ac67 91 * @param sda mbed pin to use for SDA line of I2C interface.
UsmanKhan 2:df51c015ac67 92 * @param scl mbed pin to use for SCL line of I2C interface.
UsmanKhan 2:df51c015ac67 93 */
UsmanKhan 2:df51c015ac67 94 Si7034(PinName sda, PinName scl);
UsmanKhan 2:df51c015ac67 95
UsmanKhan 2:df51c015ac67 96 /**
UsmanKhan 2:df51c015ac67 97 * Si7020 constructor.
UsmanKhan 2:df51c015ac67 98 *
UsmanKhan 2:df51c015ac67 99 * @param i2c I2C object to use
UsmanKhan 2:df51c015ac67 100 */
UsmanKhan 2:df51c015ac67 101 Si7034(I2C *i2c);
UsmanKhan 2:df51c015ac67 102
UsmanKhan 2:df51c015ac67 103 /**
UsmanKhan 2:df51c015ac67 104 * Si7020 destructor.
UsmanKhan 2:df51c015ac67 105 */
UsmanKhan 2:df51c015ac67 106 ~Si7034();
UsmanKhan 2:df51c015ac67 107
UsmanKhan 2:df51c015ac67 108 /**
UsmanKhan 2:df51c015ac67 109 * @brief Reset.
UsmanKhan 2:df51c015ac67 110 * @details Sends the rest command.
UsmanKhan 2:df51c015ac67 111 * @returns 0 if no errors, -1 if error.
UsmanKhan 2:df51c015ac67 112 */
UsmanKhan 2:df51c015ac67 113 int reset(void);
UsmanKhan 2:df51c015ac67 114
UsmanKhan 2:df51c015ac67 115 /**
UsmanKhan 2:df51c015ac67 116 * @brief Get Electronic ID.
UsmanKhan 2:df51c015ac67 117 * @details Gets the Electronic ID of the connected device. Verifies the
UsmanKhan 2:df51c015ac67 118 * ID with an 8-bit CRC.
UsmanKhan 2:df51c015ac67 119 *
UsmanKhan 2:df51c015ac67 120 * @param Character buffer to store the id. Needs to be at least 8 bytes.
UsmanKhan 2:df51c015ac67 121 * @returns 0 if no errors, -1 if error.
UsmanKhan 2:df51c015ac67 122 */
UsmanKhan 2:df51c015ac67 123 int getElectronicId(char *id);
UsmanKhan 2:df51c015ac67 124
UsmanKhan 2:df51c015ac67 125 /**
UsmanKhan 2:df51c015ac67 126 * @brief Configure sample resolution.
UsmanKhan 2:df51c015ac67 127 * @details Sets the number of bits used for humidity and temperature readings.
UsmanKhan 2:df51c015ac67 128 * @param resolution Enum for the resolution setting.
UsmanKhan 2:df51c015ac67 129 * @returns 0 if no errors, -1 if error.
UsmanKhan 2:df51c015ac67 130 */
UsmanKhan 2:df51c015ac67 131 int configResolution(Si7034::resolution_t resolution);
UsmanKhan 2:df51c015ac67 132
UsmanKhan 2:df51c015ac67 133 /**
UsmanKhan 2:df51c015ac67 134 * @brief Get temperature reading.
UsmanKhan 2:df51c015ac67 135 * @details Initiates a temperature reading and blocks until reading has
UsmanKhan 2:df51c015ac67 136 * been calculated.
UsmanKhan 2:df51c015ac67 137 *
UsmanKhan 2:df51c015ac67 138 * @note Will hold the I2C bus until reading is complete. Refer to datasheet
UsmanKhan 2:df51c015ac67 139 * for timing specifications.
UsmanKhan 2:df51c015ac67 140 *
UsmanKhan 2:df51c015ac67 141 * @param tempCx10 Pointer for temperature reading. Unit is 1/10th degree Celcius.
UsmanKhan 2:df51c015ac67 142 * @returns 0 if no errors, -1 if error.
UsmanKhan 2:df51c015ac67 143 */
UsmanKhan 2:df51c015ac67 144 int getTemperature(int16_t *tempCx10);
UsmanKhan 2:df51c015ac67 145
UsmanKhan 2:df51c015ac67 146 /**
UsmanKhan 2:df51c015ac67 147 * @brief Get temperature reading.
UsmanKhan 2:df51c015ac67 148 * @details Initiates a temperature reading and blocks until reading has
UsmanKhan 2:df51c015ac67 149 * been calculated.
UsmanKhan 2:df51c015ac67 150 *
UsmanKhan 2:df51c015ac67 151 * @note Will hold the I2C bus until reading is complete. Refer to datasheet
UsmanKhan 2:df51c015ac67 152 * for timing specifications.
UsmanKhan 2:df51c015ac67 153 *
UsmanKhan 2:df51c015ac67 154 * @param tempC Pointer for temperature reading. Unit is degree Celcius.
UsmanKhan 2:df51c015ac67 155 * @returns 0 if no errors, -1 if error.
UsmanKhan 2:df51c015ac67 156 */
UsmanKhan 2:df51c015ac67 157 int getTemperature(float *tempC);
UsmanKhan 2:df51c015ac67 158
UsmanKhan 2:df51c015ac67 159 /**
UsmanKhan 2:df51c015ac67 160 * @brief Start temperature reading.
UsmanKhan 2:df51c015ac67 161 * @details Initiates a temperature reading. Will not hold the bus or block.
UsmanKhan 2:df51c015ac67 162 * @returns 0 if no errors, -1 if error.
UsmanKhan 2:df51c015ac67 163 */
UsmanKhan 2:df51c015ac67 164 int startTemperature(void);
UsmanKhan 2:df51c015ac67 165
UsmanKhan 2:df51c015ac67 166 /**
UsmanKhan 2:df51c015ac67 167 * @brief Check temperature reading.
UsmanKhan 2:df51c015ac67 168 * @details Checks to see if the temperature reading has been completed.
UsmanKhan 2:df51c015ac67 169 Returns temperature if reading complete.
UsmanKhan 2:df51c015ac67 170 * @note Must call startTemperature() prior to calling this function.
UsmanKhan 2:df51c015ac67 171 * @param tempCx10 Pointer for temperature reading. Unit is 1/10th degree Celcius.
UsmanKhan 2:df51c015ac67 172 * @returns 0 if reading taken, -1 if reading pending.
UsmanKhan 2:df51c015ac67 173 */
UsmanKhan 2:df51c015ac67 174 int checkTemperature(int16_t *tempCx10);
UsmanKhan 2:df51c015ac67 175
UsmanKhan 2:df51c015ac67 176 /**
UsmanKhan 2:df51c015ac67 177 * @brief Check temperature reading.
UsmanKhan 2:df51c015ac67 178 * @details Checks to see if the temperature reading has been completed.
UsmanKhan 2:df51c015ac67 179 Returns temperature if reading complete.
UsmanKhan 2:df51c015ac67 180 * @note Must call startTemperature() prior to calling this function.
UsmanKhan 2:df51c015ac67 181 * @param tempC Pointer for temperature reading. Unit is degree Celcius.
UsmanKhan 2:df51c015ac67 182 * @returns 0 if reading taken, -1 if reading pending.
UsmanKhan 2:df51c015ac67 183 */
UsmanKhan 2:df51c015ac67 184 int checkTemperature(float *tempC);
UsmanKhan 2:df51c015ac67 185
UsmanKhan 2:df51c015ac67 186 /**
UsmanKhan 2:df51c015ac67 187 * @brief Get humidity reading.
UsmanKhan 2:df51c015ac67 188 * @details Initiates a humidity reading and blocks until reading has
UsmanKhan 2:df51c015ac67 189 * been calculated.
UsmanKhan 2:df51c015ac67 190 *
UsmanKhan 2:df51c015ac67 191 * @note Will hold the I2C bus until reading is complete. Refer to datasheet
UsmanKhan 2:df51c015ac67 192 * for timing specifications.
UsmanKhan 2:df51c015ac67 193 *
UsmanKhan 2:df51c015ac67 194 * @param humidx10 Pointer for humidity reading. Unit is 1/10th percent.
UsmanKhan 2:df51c015ac67 195 * @returns 0 if no errors, -1 if error.
UsmanKhan 2:df51c015ac67 196 */
UsmanKhan 2:df51c015ac67 197 int getHumidity(int16_t *humidx10);
UsmanKhan 2:df51c015ac67 198
UsmanKhan 2:df51c015ac67 199 /**
UsmanKhan 2:df51c015ac67 200 * @brief Get humidity reading.
UsmanKhan 2:df51c015ac67 201 * @details Initiates a humidity reading and blocks until reading has
UsmanKhan 2:df51c015ac67 202 * been calculated.
UsmanKhan 2:df51c015ac67 203 *
UsmanKhan 2:df51c015ac67 204 * @note Will hold the I2C bus until reading is complete. Refer to datasheet
UsmanKhan 2:df51c015ac67 205 * for timing specifications.
UsmanKhan 2:df51c015ac67 206 *
UsmanKhan 2:df51c015ac67 207 * @param humid Pointer for humidity reading. Unit is percent.
UsmanKhan 2:df51c015ac67 208 * @returns 0 if no errors, -1 if error.
UsmanKhan 2:df51c015ac67 209 */
UsmanKhan 2:df51c015ac67 210 int getHumidity(float *humid);
UsmanKhan 2:df51c015ac67 211
UsmanKhan 2:df51c015ac67 212 /**
UsmanKhan 2:df51c015ac67 213 * @brief Start humidity reading.
UsmanKhan 2:df51c015ac67 214 * @details Initiates a humidity reading. Will not hold the bus or block.
UsmanKhan 2:df51c015ac67 215 * @returns 0 if no errors, -1 if error.
UsmanKhan 2:df51c015ac67 216 */
UsmanKhan 2:df51c015ac67 217 int startHumidity(void);
UsmanKhan 2:df51c015ac67 218
UsmanKhan 2:df51c015ac67 219 /**
UsmanKhan 2:df51c015ac67 220 * @brief Check humidity reading.
UsmanKhan 2:df51c015ac67 221 * @details Checks to see if the humidity reading has been completed.
UsmanKhan 2:df51c015ac67 222 Returns humidity if reading complete.
UsmanKhan 2:df51c015ac67 223
UsmanKhan 2:df51c015ac67 224 * @note Must call startHumidity() prior to calling this function.
UsmanKhan 2:df51c015ac67 225 * @param humidCx10 Pointer for humidity reading. Unit is 1/10th percent.
UsmanKhan 2:df51c015ac67 226 * @returns 0 if reading taken, -1 if reading pending.
UsmanKhan 2:df51c015ac67 227 */
UsmanKhan 2:df51c015ac67 228 int checkHumidity(int16_t *humidx10);
UsmanKhan 2:df51c015ac67 229
UsmanKhan 2:df51c015ac67 230 /**
UsmanKhan 2:df51c015ac67 231 * @brief Check humidity reading.
UsmanKhan 2:df51c015ac67 232 * @details Checks to see if the humidity reading has been completed.
UsmanKhan 2:df51c015ac67 233 Returns humidity if reading complete.
UsmanKhan 2:df51c015ac67 234
UsmanKhan 2:df51c015ac67 235 * @note Must call startHumidity() prior to calling this function.
UsmanKhan 2:df51c015ac67 236 * @param humid Pointer for humidity reading. Unit is percent.
UsmanKhan 2:df51c015ac67 237 * @returns 0 if reading taken, -1 if reading pending.
UsmanKhan 2:df51c015ac67 238 */
UsmanKhan 2:df51c015ac67 239 int checkHumidity(float *humid);
UsmanKhan 2:df51c015ac67 240
UsmanKhan 2:df51c015ac67 241 /**
UsmanKhan 2:df51c015ac67 242 * @brief Get temperature from humidity reading.
UsmanKhan 2:df51c015ac67 243 * @details Gets temperature reading from previous humidity reading.
UsmanKhan 2:df51c015ac67 244 * @note Must call startHumidity() prior to calling this function.
UsmanKhan 2:df51c015ac67 245 * @param tempC Pointer for temperature reading. Unit is degree Celcius.
UsmanKhan 2:df51c015ac67 246 * @returns 0 if reading taken, -1 if reading pending.
UsmanKhan 2:df51c015ac67 247 */
UsmanKhan 2:df51c015ac67 248 int getPrevTemperature(float* tempC);
UsmanKhan 2:df51c015ac67 249
UsmanKhan 2:df51c015ac67 250 /**
UsmanKhan 2:df51c015ac67 251 * @brief Get temperature from humidity reading.
UsmanKhan 2:df51c015ac67 252 * @details Gets temperature reading from previous humidity reading.
UsmanKhan 2:df51c015ac67 253 * @note Must call startHumidity() prior to calling this function.
UsmanKhan 2:df51c015ac67 254 * @param tempCx10 Pointer for temperature reading. Unit is 1/10th degree Celcius.
UsmanKhan 2:df51c015ac67 255 * @returns 0 if reading taken, -1 if reading pending.
UsmanKhan 2:df51c015ac67 256 */
UsmanKhan 2:df51c015ac67 257 int getPrevTemperature(int16_t *tempCx10);
UsmanKhan 2:df51c015ac67 258
UsmanKhan 2:df51c015ac67 259 /**
UsmanKhan 2:df51c015ac67 260 * @brief Get firmware revision.
UsmanKhan 2:df51c015ac67 261 * @details Reads the firmware revision, refer to datasheet for codes.
UsmanKhan 2:df51c015ac67 262 * @param rev Pointer to store firmware revision.
UsmanKhan 2:df51c015ac67 263 * @returns 0 if no errors, -1 if error.
UsmanKhan 2:df51c015ac67 264 */
UsmanKhan 2:df51c015ac67 265 int getRev(char *rev);
UsmanKhan 2:df51c015ac67 266
UsmanKhan 2:df51c015ac67 267 /**
UsmanKhan 2:df51c015ac67 268 * @brief Control heater.
UsmanKhan 2:df51c015ac67 269 * @details Enable or disable the heater.
UsmanKhan 2:df51c015ac67 270 * @param enable True to enable heater, false to disable.
UsmanKhan 2:df51c015ac67 271 * @returns 0 if no errors, -1 if error.
UsmanKhan 2:df51c015ac67 272 */
UsmanKhan 2:df51c015ac67 273 int heater(bool enable);
UsmanKhan 2:df51c015ac67 274
UsmanKhan 2:df51c015ac67 275 private:
UsmanKhan 2:df51c015ac67 276
UsmanKhan 2:df51c015ac67 277 char crc8(char value, char seed);
UsmanKhan 2:df51c015ac67 278 I2C *i2c_;
UsmanKhan 2:df51c015ac67 279 bool i2c_owner;
UsmanKhan 2:df51c015ac67 280 };
UsmanKhan 2:df51c015ac67 281
UsmanKhan 2:df51c015ac67 282 #endif /* _SI7020_H_ */