00001
00016 #include <stddef.h>
00017 #include "si7013.h"
00018 #include "i2cspm.h"
00019
00020 #include "stddef.h"
00021
00022
00023
00024
00025
00029 #define SI7013_READ_TEMP 0xE0
00030
00031
00032 #define SI7013_READ_RH 0xE5
00033
00034 #define SI7013_READ_RH_NH 0xF5
00035
00036 #define SI7013_READ_VIN 0xEE
00037
00038 #define SI7013_READ_ID1_1 0xFA
00039 #define SI7013_READ_ID1_2 0x0F
00040 #define SI7013_READ_ID2_1 0xFc
00041 #define SI7013_READ_ID2_2 0xc9
00042
00043 #define SI7013_READ_FWREV_1 0x84
00044 #define SI7013_READ_FWREV_2 0xB8
00045
00048
00049
00050
00051
00052
00053
00068 static int32_t Si7013_Measure(I2C_TypeDef *i2c, uint8_t addr, uint32_t *data,
00069 uint8_t command)
00070 {
00071 I2C_TransferSeq_TypeDef seq;
00072 I2C_TransferReturn_TypeDef ret;
00073 uint8_t i2c_read_data[2];
00074 uint8_t i2c_write_data[1];
00075
00076 seq.addr = addr;
00077 seq.flags = I2C_FLAG_WRITE_READ;
00078
00079 i2c_write_data[0] = command;
00080 seq.buf[0].data = i2c_write_data;
00081 seq.buf[0].len = 1;
00082
00083 seq.buf[1].data = i2c_read_data;
00084 seq.buf[1].len = 2;
00085
00086 ret = I2CSPM_Transfer(i2c, &seq);
00087
00088 if (ret != i2cTransferDone)
00089 {
00090 *data = 0;
00091 return((int) ret);
00092 }
00093
00094 *data = ((uint32_t) i2c_read_data[0] << 8) + (i2c_read_data[1] & 0xfc);
00095
00096 return((int) 2);
00097 }
00098
00099
00112 static int32_t Si7013_StartNoHoldMeasure(I2C_TypeDef *i2c, uint8_t addr, uint8_t command)
00113
00114 {
00115 I2C_TransferSeq_TypeDef seq;
00116 I2C_TransferReturn_TypeDef ret;
00117 uint8_t i2c_read_data[2];
00118 uint8_t i2c_write_data[1];
00119
00120 seq.addr = addr;
00121 seq.flags = I2C_FLAG_WRITE;
00122
00123 i2c_write_data[0] = command;
00124 seq.buf[0].data = i2c_write_data;
00125 seq.buf[0].len = 1;
00126
00127 seq.buf[1].data = i2c_read_data;
00128 seq.buf[1].len = 0;
00129
00130 ret = I2CSPM_Transfer(i2c, &seq);
00131
00132 if (ret != i2cTransferDone)
00133 {
00134 return((int) ret);
00135 }
00136
00137 return((int) 0);
00138 }
00139
00140
00141
00153 int32_t Si7013_GetFirmwareRevision(I2C_TypeDef *i2c, uint8_t addr, uint8_t *fwRev)
00154 {
00155 I2C_TransferSeq_TypeDef seq;
00156 I2C_TransferReturn_TypeDef ret;
00157 uint8_t i2c_write_data[2];
00158 uint8_t i2c_read_data[1];
00159
00160 seq.addr = addr;
00161 seq.flags = I2C_FLAG_WRITE_READ;
00162
00163 i2c_write_data[0] = SI7013_READ_FWREV_1;
00164 i2c_write_data[1] = SI7013_READ_FWREV_2;
00165 seq.buf[0].data = i2c_write_data;
00166 seq.buf[0].len = 2;
00167
00168 seq.buf[1].data = i2c_read_data;
00169 seq.buf[1].len = 1;
00170
00171 ret = I2CSPM_Transfer(i2c, &seq);
00172
00173 if (ret != i2cTransferDone)
00174 {
00175 *fwRev = 0;
00176 return (uint32_t) ret;
00177 }
00178 *fwRev = i2c_read_data[0];
00179
00180 return (uint32_t) i2cTransferDone;
00181 }
00182
00183
00193 int32_t Si7013_StartNoHoldMeasureRHAndTemp(I2C_TypeDef *i2c, uint8_t addr)
00194 {
00195 int ret = Si7013_StartNoHoldMeasure(i2c, addr, SI7013_READ_RH_NH);
00196
00197 return ret;
00198 }
00199
00200
00213 static int32_t Si7013_ReadNoHoldData(I2C_TypeDef *i2c, uint8_t addr, uint32_t *data)
00214 {
00215 I2C_TransferSeq_TypeDef seq;
00216 I2C_TransferReturn_TypeDef ret;
00217 uint8_t i2c_read_data[2];
00218
00219 seq.addr = addr;
00220 seq.flags = I2C_FLAG_READ;
00221
00222 seq.buf[0].data = i2c_read_data;
00223 seq.buf[0].len = 2;
00224
00225 seq.buf[1].data = i2c_read_data;
00226 seq.buf[1].len = 2;
00227
00228 ret = I2CSPM_Transfer(i2c, &seq);
00229
00230 if (ret != i2cTransferDone)
00231 {
00232 *data = 0;
00233 return((int) ret);
00234 }
00235
00236 *data = ((uint32_t) i2c_read_data[0] << 8) + (i2c_read_data[1] & 0xfc);
00237
00238 return((int) 2);
00239 }
00240
00241
00255 int32_t Si7013_ReadNoHoldRHAndTemp(I2C_TypeDef *i2c, uint8_t addr, uint32_t *rhData,
00256 int32_t *tData)
00257 {
00258 int ret = Si7013_ReadNoHoldData(i2c, addr, rhData);
00259
00260 if (ret == 2)
00261 {
00262
00263 *rhData = (((*rhData) * 15625L) >> 13) - 6000;
00264 }
00265 else
00266 {
00267 return -1;
00268 }
00269
00270 ret = Si7013_Measure(i2c, addr, (uint32_t *) tData, SI7013_READ_TEMP);
00271
00272 if (ret == 2)
00273 {
00274 *tData = (((*tData) * 21965L) >> 13) - 46850;
00275 }
00276 else
00277 {
00278 return -1;
00279 }
00280
00281 return 0;
00282 }
00283
00284
00298 int32_t Si7013_MeasureRHAndTemp(I2C_TypeDef *i2c, uint8_t addr, uint32_t *rhData,
00299 int32_t *tData)
00300 {
00301 int ret = Si7013_Measure(i2c, addr, rhData, SI7013_READ_RH);
00302
00303 if (ret == 2)
00304 {
00305
00306 *rhData = (((*rhData) * 15625L) >> 13) - 6000;
00307 }
00308 else
00309 {
00310 return -1;
00311 }
00312
00313 ret = Si7013_Measure(i2c, addr, (uint32_t *) tData, SI7013_READ_TEMP);
00314
00315 if (ret == 2)
00316 {
00317 *tData = (((*tData) * 21965L) >> 13) - 46850;
00318 }
00319 else
00320 {
00321 return -1;
00322 }
00323
00324 return 0;
00325 }
00326
00327 static int32_t Si7013_WriteUserReg2(I2C_TypeDef *i2c, uint8_t addr, int8_t data)
00328 {
00329 I2C_TransferSeq_TypeDef seq;
00330 I2C_TransferReturn_TypeDef ret;
00331 uint8_t i2c_read_data[2];
00332 uint8_t i2c_write_data[2];
00333
00334 seq.addr = addr;
00335 seq.flags = I2C_FLAG_WRITE;
00336
00337 i2c_write_data[0] = 0x50;
00338 i2c_write_data[1] = data;
00339 seq.buf[0].data = i2c_write_data;
00340 seq.buf[0].len = 2;
00341
00342 seq.buf[1].data = i2c_read_data;
00343 seq.buf[1].len = 0;
00344
00345 ret = I2CSPM_Transfer(i2c, &seq);
00346
00347 if (ret != i2cTransferDone)
00348 {
00349 return((int) ret);
00350 }
00351
00352 return((int) 0);
00353 }
00354
00355
00356
00370 int32_t Si7013_MeasureV(I2C_TypeDef *i2c, uint8_t addr, int32_t *vData)
00371 {
00372 int ret;
00373 Si7013_WriteUserReg2(i2c, addr, 0x0e);
00374 ret = Si7013_Measure(i2c, addr, (uint32_t *) vData, SI7013_READ_VIN);
00375 Si7013_WriteUserReg2(i2c, addr, 0x09);
00376 if (ret == 2)
00377 {
00378
00379 }
00380 else
00381 {
00382 return -1;
00383 }
00384 return 0;
00385 }
00386
00387
00388
00401 bool Si7013_Detect(I2C_TypeDef *i2c, uint8_t addr, uint8_t *deviceId)
00402 {
00403 I2C_TransferSeq_TypeDef seq;
00404 I2C_TransferReturn_TypeDef ret;
00405 uint8_t i2c_read_data[8];
00406 uint8_t i2c_write_data[2];
00407
00408 seq.addr = addr;
00409 seq.flags = I2C_FLAG_WRITE_READ;
00410
00411 i2c_write_data[0] = SI7013_READ_ID2_1;
00412 i2c_write_data[1] = SI7013_READ_ID2_2;
00413 seq.buf[0].data = i2c_write_data;
00414 seq.buf[0].len = 2;
00415
00416 seq.buf[1].data = i2c_read_data;
00417 seq.buf[1].len = 8;
00418
00419 ret = I2CSPM_Transfer(i2c, &seq);
00420 if (ret != i2cTransferDone)
00421 {
00422 return false;
00423 }
00424 if (NULL != deviceId)
00425 {
00426 *deviceId = i2c_read_data[0];
00427 }
00428 return true;
00429 }