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.

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