Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
7.9 KiB

  1. // -----------------------------------------------------------------------------
  2. // BMP085/BMP180 Sensor over I2C
  3. // Copyright (C) 2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && BMP180_SUPPORT
  6. #pragma once
  7. #include "I2CSensor.h"
  8. #include "../utils.h"
  9. #define BMP180_CHIP_ID 0x55
  10. #define BMP180_REGISTER_CHIPID 0xD0
  11. #define BMP180_REGISTER_CAL_AC1 0xAA
  12. #define BMP180_REGISTER_CAL_AC2 0xAC
  13. #define BMP180_REGISTER_CAL_AC3 0xAE
  14. #define BMP180_REGISTER_CAL_AC4 0xB0
  15. #define BMP180_REGISTER_CAL_AC5 0xB2
  16. #define BMP180_REGISTER_CAL_AC6 0xB4
  17. #define BMP180_REGISTER_CAL_B1 0xB6
  18. #define BMP180_REGISTER_CAL_B2 0xB8
  19. #define BMP180_REGISTER_CAL_MB 0xBA
  20. #define BMP180_REGISTER_CAL_MC 0xBC
  21. #define BMP180_REGISTER_CAL_MD 0xBE
  22. #define BMP180_REGISTER_VERSION 0xD1
  23. #define BMP180_REGISTER_SOFTRESET 0xE0
  24. #define BMP180_REGISTER_CONTROL 0xF4
  25. #define BMP180_REGISTER_TEMPDATA 0xF6
  26. #define BMP180_REGISTER_PRESSUREDATA 0xF6
  27. #define BMP180_REGISTER_READTEMPCMD 0x2E
  28. #define BMP180_REGISTER_READPRESSURECMD 0x34
  29. class BMP180Sensor : public I2CSensor<> {
  30. public:
  31. static unsigned char addresses[1];
  32. // ---------------------------------------------------------------------
  33. // Public
  34. // ---------------------------------------------------------------------
  35. BMP180Sensor() {
  36. _sensor_id = SENSOR_BMP180_ID;
  37. _count = 2;
  38. }
  39. // ---------------------------------------------------------------------
  40. // Sensor API
  41. // ---------------------------------------------------------------------
  42. // Initialization method, must be idempotent
  43. void begin() {
  44. if (!_dirty) return;
  45. _init();
  46. _dirty = !_ready;
  47. }
  48. // Descriptive name of the sensor
  49. String description() {
  50. char buffer[20];
  51. snprintf(buffer, sizeof(buffer), "BMP180 @ I2C (0x%02X)", _address);
  52. return String(buffer);
  53. }
  54. // Type for slot # index
  55. unsigned char type(unsigned char index) {
  56. if (index == 0) return MAGNITUDE_TEMPERATURE;
  57. if (index == 1) return MAGNITUDE_PRESSURE;
  58. return MAGNITUDE_NONE;
  59. }
  60. // Pre-read hook (usually to populate registers with up-to-date data)
  61. virtual void pre() {
  62. if (_run_init) {
  63. i2cClearBus();
  64. _init();
  65. }
  66. if (_chip == 0) {
  67. _error = SENSOR_ERROR_UNKNOWN_ID;
  68. return;
  69. }
  70. _error = SENSOR_ERROR_OK;
  71. _error = _read();
  72. if (_error != SENSOR_ERROR_OK) {
  73. _run_init = true;
  74. }
  75. }
  76. // Current value for slot # index
  77. double value(unsigned char index) {
  78. if (index == 0) return _temperature;
  79. if (index == 1) return _pressure / 100;
  80. return 0;
  81. }
  82. protected:
  83. void _init() {
  84. // Make sure sensor had enough time to turn on. BMP180 requires 2ms to start up
  85. nice_delay(10);
  86. // I2C auto-discover
  87. _address = _begin_i2c(_address, sizeof(BMP180Sensor::addresses), BMP180Sensor::addresses);
  88. if (_address == 0) return;
  89. // Check sensor correctly initialized
  90. _chip = i2c_read_uint8(_address, BMP180_REGISTER_CHIPID);
  91. if (_chip != BMP180_CHIP_ID) {
  92. _chip = 0;
  93. i2cReleaseLock(_address);
  94. _previous_address = 0;
  95. _error = SENSOR_ERROR_UNKNOWN_ID;
  96. // Setting _address to 0 forces auto-discover
  97. // This might be necessary at this stage if there is a
  98. // different sensor in the hardcoded address
  99. _address = 0;
  100. return;
  101. }
  102. _readCoefficients();
  103. _run_init = false;
  104. _ready = true;
  105. }
  106. void _readCoefficients() {
  107. _bmp180_calib.ac1 = i2c_read_int16(_address, BMP180_REGISTER_CAL_AC1);
  108. _bmp180_calib.ac2 = i2c_read_int16(_address, BMP180_REGISTER_CAL_AC2);
  109. _bmp180_calib.ac3 = i2c_read_int16(_address, BMP180_REGISTER_CAL_AC3);
  110. _bmp180_calib.ac4 = i2c_read_uint16(_address, BMP180_REGISTER_CAL_AC4);
  111. _bmp180_calib.ac5 = i2c_read_uint16(_address, BMP180_REGISTER_CAL_AC5);
  112. _bmp180_calib.ac6 = i2c_read_uint16(_address, BMP180_REGISTER_CAL_AC6);
  113. _bmp180_calib.b1 = i2c_read_int16(_address, BMP180_REGISTER_CAL_B1);
  114. _bmp180_calib.b2 = i2c_read_int16(_address, BMP180_REGISTER_CAL_B2);
  115. _bmp180_calib.mb = i2c_read_int16(_address, BMP180_REGISTER_CAL_MB);
  116. _bmp180_calib.mc = i2c_read_int16(_address, BMP180_REGISTER_CAL_MC);
  117. _bmp180_calib.md = i2c_read_int16(_address, BMP180_REGISTER_CAL_MD);
  118. }
  119. // Compute B5 coefficient used in temperature & pressure calcs.
  120. // Based on Adafruit_BMP085_Unified library
  121. long _computeB5(unsigned long t) {
  122. long X1 = (t - (long)_bmp180_calib.ac6) * ((long)_bmp180_calib.ac5) >> 15;
  123. long X2 = ((long)_bmp180_calib.mc << 11) / (X1+(long)_bmp180_calib.md);
  124. return X1 + X2;
  125. }
  126. unsigned char _read() {
  127. // Read raw temperature
  128. i2c_write_uint8(_address, BMP180_REGISTER_CONTROL, BMP180_REGISTER_READTEMPCMD);
  129. nice_delay(5);
  130. unsigned long t = i2c_read_uint16(_address, BMP180_REGISTER_TEMPDATA);
  131. // Compute B5 coeficient
  132. long b5 = _computeB5(t);
  133. // Final temperature
  134. _temperature = ((double) ((b5 + 8) >> 4)) / 10.0;
  135. // Read raw pressure
  136. i2c_write_uint8(_address, BMP180_REGISTER_CONTROL, BMP180_REGISTER_READPRESSURECMD + (_mode << 6));
  137. nice_delay(26);
  138. unsigned long p1 = i2c_read_uint16(_address, BMP180_REGISTER_PRESSUREDATA);
  139. unsigned long p2 = i2c_read_uint8(_address, BMP180_REGISTER_PRESSUREDATA+2);
  140. long p = ((p1 << 8) + p2) >> (8 - _mode);
  141. // Pressure compensation
  142. long b6 = b5 - 4000;
  143. long x1 = (_bmp180_calib.b2 * ((b6 * b6) >> 12)) >> 11;
  144. long x2 = (_bmp180_calib.ac2 * b6) >> 11;
  145. long x3 = x1 + x2;
  146. long b3 = (((((int32_t) _bmp180_calib.ac1) * 4 + x3) << _mode) + 2) >> 2;
  147. x1 = (_bmp180_calib.ac3 * b6) >> 13;
  148. x2 = (_bmp180_calib.b1 * ((b6 * b6) >> 12)) >> 16;
  149. x3 = ((x1 + x2) + 2) >> 2;
  150. unsigned long b4 = (_bmp180_calib.ac4 * (uint32_t) (x3 + 32768)) >> 15;
  151. unsigned long b7 = ((uint32_t) (p - b3) * (50000 >> _mode));
  152. if (b7 < 0x80000000) {
  153. p = (b7 << 1) / b4;
  154. } else {
  155. p = (b7 / b4) << 1;
  156. }
  157. x1 = (p >> 8) * (p >> 8);
  158. x1 = (x1 * 3038) >> 16;
  159. x2 = (-7357 * p) >> 16;
  160. _pressure = p + ((x1 + x2 + 3791) >> 4);
  161. return SENSOR_ERROR_OK;
  162. }
  163. // ---------------------------------------------------------------------
  164. unsigned char _chip;
  165. bool _run_init = false;
  166. double _temperature = 0;
  167. double _pressure = 0;
  168. unsigned int _mode = BMP180_MODE;
  169. typedef struct {
  170. int16_t ac1;
  171. int16_t ac2;
  172. int16_t ac3;
  173. uint16_t ac4;
  174. uint16_t ac5;
  175. uint16_t ac6;
  176. int16_t b1;
  177. int16_t b2;
  178. int16_t mb;
  179. int16_t mc;
  180. int16_t md;
  181. } bmp180_calib_t;
  182. bmp180_calib_t _bmp180_calib;
  183. };
  184. // Static inizializations
  185. unsigned char BMP180Sensor::addresses[1] = {0x77};
  186. #endif // SENSOR_SUPPORT && BMP180_SUPPORT