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.

438 lines
16 KiB

6 years ago
  1. // -----------------------------------------------------------------------------
  2. // BME280/BMP280 Sensor over I2C
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && BMX280_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "I2CSensor.h"
  9. #define BMX280_CHIP_BMP280 0x58
  10. #define BMX280_CHIP_BME280 0x60
  11. #define BMX280_REGISTER_DIG_T1 0x88
  12. #define BMX280_REGISTER_DIG_T2 0x8A
  13. #define BMX280_REGISTER_DIG_T3 0x8C
  14. #define BMX280_REGISTER_DIG_P1 0x8E
  15. #define BMX280_REGISTER_DIG_P2 0x90
  16. #define BMX280_REGISTER_DIG_P3 0x92
  17. #define BMX280_REGISTER_DIG_P4 0x94
  18. #define BMX280_REGISTER_DIG_P5 0x96
  19. #define BMX280_REGISTER_DIG_P6 0x98
  20. #define BMX280_REGISTER_DIG_P7 0x9A
  21. #define BMX280_REGISTER_DIG_P8 0x9C
  22. #define BMX280_REGISTER_DIG_P9 0x9E
  23. #define BMX280_REGISTER_DIG_H1 0xA1
  24. #define BMX280_REGISTER_DIG_H2 0xE1
  25. #define BMX280_REGISTER_DIG_H3 0xE3
  26. #define BMX280_REGISTER_DIG_H4 0xE4
  27. #define BMX280_REGISTER_DIG_H5 0xE5
  28. #define BMX280_REGISTER_DIG_H6 0xE7
  29. #define BMX280_REGISTER_CHIPID 0xD0
  30. #define BMX280_REGISTER_VERSION 0xD1
  31. #define BMX280_REGISTER_SOFTRESET 0xE0
  32. #define BMX280_REGISTER_CAL26 0xE1
  33. #define BMX280_REGISTER_CONTROLHUMID 0xF2
  34. #define BMX280_REGISTER_CONTROL 0xF4
  35. #define BMX280_REGISTER_CONFIG 0xF5
  36. #define BMX280_REGISTER_PRESSUREDATA 0xF7
  37. #define BMX280_REGISTER_TEMPDATA 0xFA
  38. #define BMX280_REGISTER_HUMIDDATA 0xFD
  39. class BMX280Sensor : public I2CSensor {
  40. public:
  41. static unsigned char addresses[2];
  42. // ---------------------------------------------------------------------
  43. // Public
  44. // ---------------------------------------------------------------------
  45. BMX280Sensor(): I2CSensor() {
  46. _sensor_id = SENSOR_BMX280_ID;
  47. }
  48. // ---------------------------------------------------------------------
  49. // Sensor API
  50. // ---------------------------------------------------------------------
  51. // Initialization method, must be idempotent
  52. void begin() {
  53. if (!_dirty) return;
  54. _init();
  55. _dirty = !_ready;
  56. }
  57. // Descriptive name of the sensor
  58. String description() {
  59. char buffer[20];
  60. snprintf(buffer, sizeof(buffer), "%s @ I2C (0x%02X)", _chip == BMX280_CHIP_BME280 ? "BME280" : "BMP280", _address);
  61. return String(buffer);
  62. }
  63. // Type for slot # index
  64. unsigned char type(unsigned char index) {
  65. unsigned char i = 0;
  66. #if BMX280_TEMPERATURE > 0
  67. if (index == i++) return MAGNITUDE_TEMPERATURE;
  68. #endif
  69. #if BMX280_PRESSURE > 0
  70. if (index == i++) return MAGNITUDE_PRESSURE;
  71. #endif
  72. #if BMX280_HUMIDITY > 0
  73. if (_chip == BMX280_CHIP_BME280) {
  74. if (index == i) return MAGNITUDE_HUMIDITY;
  75. }
  76. #endif
  77. return MAGNITUDE_NONE;
  78. }
  79. // Pre-read hook (usually to populate registers with up-to-date data)
  80. virtual void pre() {
  81. if (_run_init) {
  82. i2cClearBus();
  83. _init();
  84. }
  85. if (_chip == 0) {
  86. _error = SENSOR_ERROR_UNKNOWN_ID;
  87. return;
  88. }
  89. _error = SENSOR_ERROR_OK;
  90. #if BMX280_MODE == 1
  91. _forceRead();
  92. #endif
  93. _error = _read();
  94. if (_error != SENSOR_ERROR_OK) {
  95. _run_init = true;
  96. }
  97. }
  98. // Current value for slot # index
  99. double value(unsigned char index) {
  100. unsigned char i = 0;
  101. #if BMX280_TEMPERATURE > 0
  102. if (index == i++) return _temperature;
  103. #endif
  104. #if BMX280_PRESSURE > 0
  105. if (index == i++) return _pressure / 100;
  106. #endif
  107. #if BMX280_HUMIDITY > 0
  108. if (_chip == BMX280_CHIP_BME280) {
  109. if (index == i) return _humidity;
  110. }
  111. #endif
  112. return 0;
  113. }
  114. // Load the configuration manifest
  115. static void manifest(JsonArray& sensors) {
  116. char buffer[10];
  117. JsonObject& sensor = sensors.createNestedObject();
  118. sensor["sensor_id"] = SENSOR_BMX280_ID;
  119. JsonArray& fields = sensor.createNestedArray("fields");
  120. {
  121. JsonObject& field = fields.createNestedObject();
  122. field["tag"] = UI_TAG_SELECT;
  123. field["name"] = "address";
  124. field["label"] = "Address";
  125. JsonArray& options = field.createNestedArray("options");
  126. {
  127. JsonObject& option = options.createNestedObject();
  128. option["name"] = "auto";
  129. option["value"] = 0;
  130. }
  131. for (unsigned char i=0; i< sizeof(BMX280Sensor::addresses); i++) {
  132. JsonObject& option = options.createNestedObject();
  133. snprintf(buffer, sizeof(buffer), "0x%02X", BMX280Sensor::addresses[i]);
  134. option["name"] = String(buffer);
  135. option["value"] = BMX280Sensor::addresses[i];
  136. }
  137. }
  138. };
  139. void getConfig(JsonObject& root) {
  140. root["sensor_id"] = _sensor_id;
  141. root["address"] = _address;
  142. };
  143. void setConfig(JsonObject& root) {
  144. if (root.containsKey("address")) setAddress(root["address"]);
  145. };
  146. protected:
  147. void _init() {
  148. // Make sure sensor had enough time to turn on. BMX280 requires 2ms to start up
  149. nice_delay(10);
  150. // No chip ID by default
  151. _chip = 0;
  152. // I2C auto-discover
  153. _address = _begin_i2c(_address, sizeof(BMX280Sensor::addresses), BMX280Sensor::addresses);
  154. if (_address == 0) return;
  155. // Check sensor correctly initialized
  156. _chip = i2c_read_uint8(_address, BMX280_REGISTER_CHIPID);
  157. if ((_chip != BMX280_CHIP_BME280) && (_chip != BMX280_CHIP_BMP280)) {
  158. _chip = 0;
  159. i2cReleaseLock(_address);
  160. _previous_address = 0;
  161. _error = SENSOR_ERROR_UNKNOWN_ID;
  162. // Setting _address to 0 forces auto-discover
  163. // This might be necessary at this stage if there is a
  164. // different sensor in the hardcoded address
  165. _address = 0;
  166. return;
  167. }
  168. _count = 0;
  169. #if BMX280_TEMPERATURE > 0
  170. ++_count;
  171. #endif
  172. #if BMX280_PRESSURE > 0
  173. ++_count;
  174. #endif
  175. #if BMX280_HUMIDITY > 0
  176. if (_chip == BMX280_CHIP_BME280) ++_count;
  177. #endif
  178. _readCoefficients();
  179. unsigned char data = 0;
  180. i2c_write_uint8(_address, BMX280_REGISTER_CONTROL, data);
  181. data = (BMX280_STANDBY << 0x5) & 0xE0;
  182. data |= (BMX280_FILTER << 0x02) & 0x1C;
  183. i2c_write_uint8(_address, BMX280_REGISTER_CONFIG, data);
  184. data = (BMX280_HUMIDITY) & 0x07;
  185. i2c_write_uint8(_address, BMX280_REGISTER_CONTROLHUMID, data);
  186. data = (BMX280_TEMPERATURE << 5) & 0xE0;
  187. data |= (BMX280_PRESSURE << 2) & 0x1C;
  188. data |= (BMX280_MODE) & 0x03;
  189. i2c_write_uint8(_address, BMX280_REGISTER_CONTROL, data);
  190. _measurement_delay = _measurementTime();
  191. _run_init = false;
  192. _ready = true;
  193. }
  194. void _readCoefficients() {
  195. _bmx280_calib.dig_T1 = i2c_read_uint16_le(_address, BMX280_REGISTER_DIG_T1);
  196. _bmx280_calib.dig_T2 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_T2);
  197. _bmx280_calib.dig_T3 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_T3);
  198. _bmx280_calib.dig_P1 = i2c_read_uint16_le(_address, BMX280_REGISTER_DIG_P1);
  199. _bmx280_calib.dig_P2 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P2);
  200. _bmx280_calib.dig_P3 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P3);
  201. _bmx280_calib.dig_P4 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P4);
  202. _bmx280_calib.dig_P5 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P5);
  203. _bmx280_calib.dig_P6 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P6);
  204. _bmx280_calib.dig_P7 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P7);
  205. _bmx280_calib.dig_P8 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P8);
  206. _bmx280_calib.dig_P9 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P9);
  207. _bmx280_calib.dig_H1 = i2c_read_uint8(_address, BMX280_REGISTER_DIG_H1);
  208. _bmx280_calib.dig_H2 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_H2);
  209. _bmx280_calib.dig_H3 = i2c_read_uint8(_address, BMX280_REGISTER_DIG_H3);
  210. _bmx280_calib.dig_H4 = (i2c_read_uint8(_address, BMX280_REGISTER_DIG_H4) << 4) | (i2c_read_uint8(_address, BMX280_REGISTER_DIG_H4+1) & 0xF);
  211. _bmx280_calib.dig_H5 = (i2c_read_uint8(_address, BMX280_REGISTER_DIG_H5+1) << 4) | (i2c_read_uint8(_address, BMX280_REGISTER_DIG_H5) >> 4);
  212. _bmx280_calib.dig_H6 = (int8_t) i2c_read_uint8(_address, BMX280_REGISTER_DIG_H6);
  213. }
  214. unsigned long _measurementTime() {
  215. // Measurement Time (as per BMX280 datasheet section 9.1)
  216. // T_max(ms) = 1.25
  217. // + (2.3 * T_oversampling)
  218. // + (2.3 * P_oversampling + 0.575)
  219. // + (2.4 * H_oversampling + 0.575)
  220. // ~ 9.3ms for current settings
  221. double t = 1.25;
  222. #if BMX280_TEMPERATURE > 0
  223. t += (2.3 * BMX280_TEMPERATURE);
  224. #endif
  225. #if BMX280_PRESSURE > 0
  226. t += (2.3 * BMX280_PRESSURE + 0.575);
  227. #endif
  228. #if BMX280_HUMIDITY > 0
  229. if (_chip == BMX280_CHIP_BME280) {
  230. t += (2.4 * BMX280_HUMIDITY + 0.575);
  231. }
  232. #endif
  233. return round(t + 1); // round up
  234. }
  235. void _forceRead() {
  236. // We set the sensor in "forced mode" to force a reading.
  237. // After the reading the sensor will go back to sleep mode.
  238. uint8_t value = i2c_read_uint8(_address, BMX280_REGISTER_CONTROL);
  239. value = (value & 0xFC) + 0x01;
  240. i2c_write_uint8(_address, BMX280_REGISTER_CONTROL, value);
  241. nice_delay(_measurement_delay);
  242. }
  243. unsigned char _read() {
  244. #if BMX280_TEMPERATURE > 0
  245. int32_t adc_T = i2c_read_uint16(_address, BMX280_REGISTER_TEMPDATA);
  246. if (0xFFFF == adc_T) return SENSOR_ERROR_I2C;
  247. adc_T <<= 8;
  248. adc_T |= i2c_read_uint8(_address, BMX280_REGISTER_TEMPDATA+2);
  249. adc_T >>= 4;
  250. int32_t var1t = ((((adc_T>>3) -
  251. ((int32_t)_bmx280_calib.dig_T1 <<1))) *
  252. ((int32_t)_bmx280_calib.dig_T2)) >> 11;
  253. int32_t var2t = (((((adc_T>>4) -
  254. ((int32_t)_bmx280_calib.dig_T1)) *
  255. ((adc_T>>4) - ((int32_t)_bmx280_calib.dig_T1))) >> 12) *
  256. ((int32_t)_bmx280_calib.dig_T3)) >> 14;
  257. int32_t t_fine = var1t + var2t;
  258. double T = (t_fine * 5 + 128) >> 8;
  259. _temperature = T / 100;
  260. #else
  261. int32_t t_fine = 102374; // ~20ºC
  262. #endif
  263. // -----------------------------------------------------------------
  264. #if BMX280_PRESSURE > 0
  265. int64_t var1, var2, p;
  266. int32_t adc_P = i2c_read_uint16(_address, BMX280_REGISTER_PRESSUREDATA);
  267. if (0xFFFF == adc_P) return SENSOR_ERROR_I2C;
  268. adc_P <<= 8;
  269. adc_P |= i2c_read_uint8(_address, BMX280_REGISTER_PRESSUREDATA+2);
  270. adc_P >>= 4;
  271. var1 = ((int64_t)t_fine) - 128000;
  272. var2 = var1 * var1 * (int64_t)_bmx280_calib.dig_P6;
  273. var2 = var2 + ((var1*(int64_t)_bmx280_calib.dig_P5)<<17);
  274. var2 = var2 + (((int64_t)_bmx280_calib.dig_P4)<<35);
  275. var1 = ((var1 * var1 * (int64_t)_bmx280_calib.dig_P3)>>8) +
  276. ((var1 * (int64_t)_bmx280_calib.dig_P2)<<12);
  277. var1 = (((((int64_t)1)<<47)+var1))*((int64_t)_bmx280_calib.dig_P1)>>33;
  278. if (var1 == 0) return SENSOR_ERROR_I2C; // avoid exception caused by division by zero
  279. p = 1048576 - adc_P;
  280. p = (((p<<31) - var2)*3125) / var1;
  281. var1 = (((int64_t)_bmx280_calib.dig_P9) * (p>>13) * (p>>13)) >> 25;
  282. var2 = (((int64_t)_bmx280_calib.dig_P8) * p) >> 19;
  283. p = ((p + var1 + var2) >> 8) + (((int64_t)_bmx280_calib.dig_P7)<<4);
  284. _pressure = (double) p / 256;
  285. #endif
  286. // -----------------------------------------------------------------
  287. #if BMX280_HUMIDITY > 0
  288. if (_chip == BMX280_CHIP_BME280) {
  289. int32_t adc_H = i2c_read_uint16(_address, BMX280_REGISTER_HUMIDDATA);
  290. if (0xFFFF == adc_H) return SENSOR_ERROR_I2C;
  291. int32_t v_x1_u32r;
  292. v_x1_u32r = (t_fine - ((int32_t)76800));
  293. v_x1_u32r = (((((adc_H << 14) - (((int32_t)_bmx280_calib.dig_H4) << 20) -
  294. (((int32_t)_bmx280_calib.dig_H5) * v_x1_u32r)) + ((int32_t)16384)) >> 15) *
  295. (((((((v_x1_u32r * ((int32_t)_bmx280_calib.dig_H6)) >> 10) *
  296. (((v_x1_u32r * ((int32_t)_bmx280_calib.dig_H3)) >> 11) + ((int32_t)32768))) >> 10) +
  297. ((int32_t)2097152)) * ((int32_t)_bmx280_calib.dig_H2) + 8192) >> 14));
  298. v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) *
  299. ((int32_t)_bmx280_calib.dig_H1)) >> 4));
  300. v_x1_u32r = (v_x1_u32r < 0) ? 0 : v_x1_u32r;
  301. v_x1_u32r = (v_x1_u32r > 419430400) ? 419430400 : v_x1_u32r;
  302. double h = (v_x1_u32r >> 12);
  303. _humidity = h / 1024.0;
  304. }
  305. #endif
  306. return SENSOR_ERROR_OK;
  307. }
  308. // ---------------------------------------------------------------------
  309. unsigned char _chip;
  310. unsigned long _measurement_delay;
  311. bool _run_init = false;
  312. double _temperature = 0;
  313. double _pressure = 0;
  314. double _humidity = 0;
  315. typedef struct {
  316. uint16_t dig_T1;
  317. int16_t dig_T2;
  318. int16_t dig_T3;
  319. uint16_t dig_P1;
  320. int16_t dig_P2;
  321. int16_t dig_P3;
  322. int16_t dig_P4;
  323. int16_t dig_P5;
  324. int16_t dig_P6;
  325. int16_t dig_P7;
  326. int16_t dig_P8;
  327. int16_t dig_P9;
  328. uint8_t dig_H1;
  329. int16_t dig_H2;
  330. uint8_t dig_H3;
  331. int16_t dig_H4;
  332. int16_t dig_H5;
  333. int8_t dig_H6;
  334. } bmx280_calib_t;
  335. bmx280_calib_t _bmx280_calib;
  336. };
  337. // Static inizializations
  338. unsigned char BMX280Sensor::addresses[2] = {0x76, 0x77};
  339. #endif // SENSOR_SUPPORT && BMX280_SUPPORT