Silicon Labs Si7210 device driver: I2C Hall Effect Magnetic Position and Temperature Sensor
Revision 3:beca2e65b1b0, committed 2017-10-14
- Comitter:
- sm168j
- Date:
- Sat Oct 14 20:10:05 2017 -0500
- Parent:
- 2:d85076c18c80
- Commit message:
- Corrected temperature reading calculation
Changed in this revision
SI7210.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/SI7210.h Thu Sep 14 00:09:07 2017 +0000 +++ b/SI7210.h Sat Oct 14 20:10:05 2017 -0500 @@ -34,7 +34,7 @@ * ok = si7210.enable() && * si7210.read(&data) && * si7210.disable(); - * + * * if (ok) { printf("Mag T: %f\r\n", data.mag_T); printf("temp C/F: %f/%f\r\n", data.temp_C, data.temp_C * 9 / 5 + 32); @@ -140,7 +140,7 @@ bool read(si7210_measurements_t * data) { uint16_t magRaw; uint16_t tempRaw; - + bool ok = _write_reg(SI72XX_ARAUTOINC, ARAUTOINC__ARAUTOINC_MASK) && _write_reg(SI72XX_DSPSIGSEL, DSPSIGSEL__MAG_VAL_SEL) //capture mag field measurement && _write_reg(SI72XX_POWER_CTRL, POWER_CTRL__ONEBURST_MASK) @@ -150,9 +150,9 @@ && _read_regs(SI72XX_DSPSIGM, 2, &tempRaw); if (ok && !_isTempOffsetAndGainValid) { - char otpTempOffset; - char otpTempGain; - + signed char otpTempOffset; + signed char otpTempGain; + ok = _read_otp(SI72XX_OTP_TEMP_OFFSET, &otpTempOffset) && _read_otp(SI72XX_OTP_TEMP_GAIN, &otpTempGain); if (ok) { @@ -161,7 +161,7 @@ _isTempOffsetAndGainValid = true; } } - + if (ok) { magRaw = ((magRaw >> 8) & 0xff) + ((magRaw & 0xff) << 8); tempRaw = ((tempRaw >> 8) & 0xff) + ((tempRaw & 0xff) << 8); @@ -170,7 +170,7 @@ data->temp_C = (float)((tempRaw & ~0x8000) >> 3); data->temp_C = _tempGain * (-3.83e-6F * data->temp_C * data->temp_C + 0.16094F * data->temp_C - 279.80F - 0.222F * 3.0F) + _tempOffset; } - + return ok; } @@ -189,11 +189,11 @@ bool _i2c_transfer(int address, void * buff, size_t writeSize, size_t readSize) { bool ok; bool expect_response = (readSize != 0); - + ok = !_i2c->write(address, (char*)buff, writeSize, expect_response); if (ok && expect_response) ok = !_i2c->read(address, (char*)buff, readSize); - + return ok; } @@ -236,13 +236,13 @@ */ bool _read_otp(uint8_t otpAddr, void *data) { uint8_t optCtrl; - + bool ok = _read_regs(SI72XX_OTP_CTRL, 1, &optCtrl) && !(optCtrl & OTP_CTRL__OPT_BUSY_MASK) && _write_reg(SI72XX_OTP_ADDR, otpAddr) && _write_reg(SI72XX_OTP_CTRL, OTP_CTRL__OPT_READ_EN_MASK) && _read_regs(SI72XX_OTP_DATA, 1, data); - + return ok; }