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.

441 lines
16 KiB

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