si7013.c

Go to the documentation of this file.
00001 /***************************************************************************/
00017 #include "si7013.h"
00018 #include "i2cdrv.h"
00019 
00020 /*******************************************************************************
00021  *******************************   DEFINES   ***********************************
00022  ******************************************************************************/
00023 
00027 #define SI7013_READ_TEMP     0xE0 /* Read previous T data from RH measurement
00028                                      command*/
00029 
00030 #define SI7013_READ_RH       0xE5 /* Perform RH (and T) measurement. */
00031 
00032 #define SI7013_READ_ID1_1    0xFA
00033 #define SI7013_READ_ID1_2    0x0F
00034 #define SI7013_READ_ID2_1    0xFc
00035 #define SI7013_READ_ID2_2    0xc9
00036 
00039 /*******************************************************************************
00040  **************************   GLOBAL FUNCTIONS   *******************************
00041  ******************************************************************************/
00042 
00043 
00044 /**************************************************************************/
00059 static int Si7013_Measure(I2C_TypeDef *i2c, uint8_t addr, uint32_t *data,
00060                           uint8_t command)
00061 {
00062   I2C_TransferSeq_TypeDef    seq;
00063   I2C_TransferReturn_TypeDef ret;
00064   uint8_t                    i2c_read_data[2];
00065   uint8_t                    i2c_write_data[1];
00066 
00067   /* Unused parameter */
00068   (void) i2c;
00069 
00070   seq.addr  = addr;
00071   seq.flags = I2C_FLAG_WRITE_READ;
00072   /* Select command to issue */
00073   i2c_write_data[0] = command;
00074   seq.buf[0].data   = i2c_write_data;
00075   seq.buf[0].len    = 1;
00076   /* Select location/length of data to be read */
00077   seq.buf[1].data = i2c_read_data;
00078   seq.buf[1].len  = 2;
00079 
00080   ret = I2CDRV_Transfer(&seq);
00081 
00082   if (ret != i2cTransferDone)
00083   {
00084     *data = 0;
00085     return((int) ret);
00086   }
00087 
00088   *data = ((uint32_t) i2c_read_data[0] << 8) + (i2c_read_data[1] & 0xfc);
00089 
00090   return((int) 2);
00091 }
00092 
00093 
00094 /**************************************************************************/
00108 int Si7013_MeasureRHAndTemp(I2C_TypeDef *i2c, uint8_t addr, uint32_t *rhData,
00109                         int32_t *tData)
00110 {
00111   int ret = Si7013_Measure(i2c, addr, rhData, SI7013_READ_RH);
00112 
00113   if (ret == 2)
00114   {
00115     /* convert to milli-percent */
00116     *rhData = (((*rhData) * 15625L) >> 13) - 6000;
00117   }
00118   else
00119   {
00120     return -1;
00121   }
00122 
00123   ret = Si7013_Measure(i2c, addr, (uint32_t *) tData, SI7013_READ_TEMP);
00124 
00125   if (ret == 2)
00126   {
00127     *tData = (((*tData) * 21965L) >> 13) - 46850; /* convert to milli-degC */
00128   }
00129   else
00130   {
00131     return -1;
00132   }
00133 
00134   return 0;
00135 }
00136 
00137 /**************************************************************************/
00147 bool Si7013_Detect(I2C_TypeDef *i2c, uint8_t addr)
00148 {
00149   I2C_TransferSeq_TypeDef    seq;
00150   I2C_TransferReturn_TypeDef ret;
00151   uint8_t                    i2c_read_data[8];
00152   uint8_t                    i2c_write_data[2];
00153 
00154   /* Unused parameter */
00155   (void) i2c;
00156 
00157   seq.addr  = addr;
00158   seq.flags = I2C_FLAG_WRITE_READ;
00159   /* Select command to issue */
00160   i2c_write_data[0] = SI7013_READ_ID1_1;
00161   i2c_write_data[1] = SI7013_READ_ID1_2;
00162   seq.buf[0].data   = i2c_write_data;
00163   seq.buf[0].len    = 2;
00164   /* Select location/length of data to be read */
00165   seq.buf[1].data = i2c_read_data;
00166   seq.buf[1].len  = 8;
00167 
00168   ret = I2CDRV_Transfer(&seq);
00169   if (ret != i2cTransferDone)
00170   {
00171     return(false);
00172   }
00173 
00174   return(true);
00175 }