BMx280.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. Unitemp - Universal temperature reader
  3. Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "BMx280.h"
  16. const SensorType BMP280 = {
  17. .typename = "BMP280",
  18. .interface = &I2C,
  19. .datatype = UT_TEMPERATURE | UT_PRESSURE,
  20. .pollingInterval = 500,
  21. .allocator = unitemp_BMx280_alloc,
  22. .mem_releaser = unitemp_BMx280_free,
  23. .initializer = unitemp_BMx280_init,
  24. .deinitializer = unitemp_BMx280_deinit,
  25. .updater = unitemp_BMx280_update};
  26. const SensorType BME280 = {
  27. .typename = "BME280",
  28. .interface = &I2C,
  29. .datatype = UT_TEMPERATURE | UT_HUMIDITY | UT_PRESSURE,
  30. .pollingInterval = 500,
  31. .allocator = unitemp_BMx280_alloc,
  32. .mem_releaser = unitemp_BMx280_free,
  33. .initializer = unitemp_BMx280_init,
  34. .deinitializer = unitemp_BMx280_deinit,
  35. .updater = unitemp_BMx280_update};
  36. //Интервал обновления калибровочных значений
  37. #define BOSCH_CAL_UPDATE_INTERVAL 60000
  38. #define TEMP_CAL_START_ADDR 0x88
  39. #define PRESS_CAL_START_ADDR 0x8E
  40. #define HUM_CAL_H1_ADDR 0xA1
  41. #define HUM_CAL_H2_ADDR 0xE1
  42. #define BMP280_ID 0x58
  43. #define BME280_ID 0x60
  44. #define BMx280_I2C_ADDR_MIN (0x76 << 1)
  45. #define BMx280_I2C_ADDR_MAX (0x77 << 1)
  46. #define BMx280_REG_STATUS 0xF3
  47. #define BMx280_REG_CTRL_MEAS 0xF4
  48. #define BMx280_REG_CONFIG 0xF5
  49. #define BME280_REG_CTRL_HUM 0xF2
  50. //Преддескретизация температуры
  51. #define BMx280_TEMP_OVERSAMPLING_SKIP 0b00000000
  52. #define BMx280_TEMP_OVERSAMPLING_1 0b00100000
  53. #define BMx280_TEMP_OVERSAMPLING_2 0b01000000
  54. #define BMx280_TEMP_OVERSAMPLING_4 0b01100000
  55. #define BMx280_TEMP_OVERSAMPLING_8 0b10000000
  56. #define BMx280_TEMP_OVERSAMPLING_16 0b10100000
  57. //Преддескретизация давления
  58. #define BMx280_PRESS_OVERSAMPLING_SKIP 0b00000000
  59. #define BMx280_PRESS_OVERSAMPLING_1 0b00000100
  60. #define BMx280_PRESS_OVERSAMPLING_2 0b00001000
  61. #define BMx280_PRESS_OVERSAMPLING_4 0b00001100
  62. #define BMx280_PRESS_OVERSAMPLING_8 0b00010000
  63. #define BMx280_PRESS_OVERSAMPLING_16 0b00010100
  64. //Преддескретизация влажности
  65. #define BME280_HUM_OVERSAMPLING_SKIP 0b00000000
  66. #define BME280_HUM_OVERSAMPLING_1 0b00000001
  67. #define BME280_HUM_OVERSAMPLING_2 0b00000010
  68. #define BME280_HUM_OVERSAMPLING_4 0b00000011
  69. #define BME280_HUM_OVERSAMPLING_8 0b00000100
  70. #define BME280_HUM_OVERSAMPLING_16 0b00000101u
  71. //Режимы работы датчика
  72. #define BMx280_MODE_SLEEP 0b00000000 //Наелся и спит
  73. #define BMx280_MODE_FORCED 0b00000001 //Обновляет значения 1 раз, после чего уходит в сон
  74. #define BMx280_MODE_NORMAL 0b00000011 //Регулярно обновляет значения
  75. //Период обновления в нормальном режиме
  76. #define BMx280_STANDBY_TIME_0_5 0b00000000
  77. #define BMx280_STANDBY_TIME_62_5 0b00100000
  78. #define BMx280_STANDBY_TIME_125 0b01000000
  79. #define BMx280_STANDBY_TIME_250 0b01100000
  80. #define BMx280_STANDBY_TIME_500 0b10000000
  81. #define BMx280_STANDBY_TIME_1000 0b10100000
  82. #define BMx280_STANDBY_TIME_2000 0b11000000
  83. #define BMx280_STANDBY_TIME_4000 0b11100000
  84. //Коэффициент фильтрации значений
  85. #define BMx280_FILTER_COEFF_1 0b00000000
  86. #define BMx280_FILTER_COEFF_2 0b00000100
  87. #define BMx280_FILTER_COEFF_4 0b00001000
  88. #define BMx280_FILTER_COEFF_8 0b00001100
  89. #define BMx280_FILTER_COEFF_16 0b00010000
  90. //Разрешить работу по SPI
  91. #define BMx280_SPI_3W_ENABLE 0b00000001
  92. #define BMx280_SPI_3W_DISABLE 0b00000000
  93. static float BMx280_compensate_temperature(I2CSensor* i2c_sensor, int32_t adc_T) {
  94. BMx280_instance* bmx280_instance = (BMx280_instance*)i2c_sensor->sensorInstance;
  95. int32_t var1, var2;
  96. var1 = ((((adc_T >> 3) - ((int32_t)bmx280_instance->temp_cal.dig_T1 << 1))) *
  97. ((int32_t)bmx280_instance->temp_cal.dig_T2)) >>
  98. 11;
  99. var2 = (((((adc_T >> 4) - ((int32_t)bmx280_instance->temp_cal.dig_T1)) *
  100. ((adc_T >> 4) - ((int32_t)bmx280_instance->temp_cal.dig_T1))) >>
  101. 12) *
  102. ((int32_t)bmx280_instance->temp_cal.dig_T3)) >>
  103. 14;
  104. bmx280_instance->t_fine = var1 + var2;
  105. return ((bmx280_instance->t_fine * 5 + 128) >> 8) / 100.0f;
  106. }
  107. static float BMx280_compensate_pressure(I2CSensor* i2c_sensor, int32_t adc_P) {
  108. BMx280_instance* bmx280_instance = (BMx280_instance*)i2c_sensor->sensorInstance;
  109. int32_t var1, var2;
  110. uint32_t p;
  111. var1 = (((int32_t)bmx280_instance->t_fine) >> 1) - (int32_t)64000;
  112. var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * ((int32_t)bmx280_instance->press_cal.dig_P6);
  113. var2 = var2 + ((var1 * ((int32_t)bmx280_instance->press_cal.dig_P5)) << 1);
  114. var2 = (var2 >> 2) + (((int32_t)bmx280_instance->press_cal.dig_P4) << 16);
  115. var1 = (((bmx280_instance->press_cal.dig_P3 * (((var1 >> 2) * (var1 >> 2)) >> 13)) >> 3) +
  116. ((((int32_t)bmx280_instance->press_cal.dig_P2) * var1) >> 1)) >>
  117. 18;
  118. var1 = ((((32768 + var1)) * ((int32_t)bmx280_instance->press_cal.dig_P1)) >> 15);
  119. if(var1 == 0) {
  120. return 0; // avoid exception caused by division by zero
  121. }
  122. p = (((uint32_t)(((int32_t)1048576) - adc_P) - (var2 >> 12))) * 3125;
  123. if(p < 0x80000000) {
  124. p = (p << 1) / ((uint32_t)var1);
  125. } else {
  126. p = (p / (uint32_t)var1) * 2;
  127. }
  128. var1 = (((int32_t)bmx280_instance->press_cal.dig_P9) *
  129. ((int32_t)(((p >> 3) * (p >> 3)) >> 13))) >>
  130. 12;
  131. var2 = (((int32_t)(p >> 2)) * ((int32_t)bmx280_instance->press_cal.dig_P8)) >> 13;
  132. p = (uint32_t)((int32_t)p + ((var1 + var2 + bmx280_instance->press_cal.dig_P7) >> 4));
  133. return p;
  134. }
  135. static float BMx280_compensate_humidity(I2CSensor* i2c_sensor, int32_t adc_H) {
  136. BMx280_instance* bmx280_instance = (BMx280_instance*)i2c_sensor->sensorInstance;
  137. int32_t v_x1_u32r;
  138. v_x1_u32r = (bmx280_instance->t_fine - ((int32_t)76800));
  139. v_x1_u32r =
  140. (((((adc_H << 14) - (((int32_t)bmx280_instance->hum_cal.dig_H4) << 20) -
  141. (((int32_t)bmx280_instance->hum_cal.dig_H5) * v_x1_u32r)) +
  142. ((int32_t)16384)) >>
  143. 15) *
  144. (((((((v_x1_u32r * ((int32_t)bmx280_instance->hum_cal.dig_H6)) >> 10) *
  145. (((v_x1_u32r * ((int32_t)bmx280_instance->hum_cal.dig_H3)) >> 11) +
  146. ((int32_t)32768))) >>
  147. 10) +
  148. ((int32_t)2097152)) *
  149. ((int32_t)bmx280_instance->hum_cal.dig_H2) +
  150. 8192) >>
  151. 14));
  152. v_x1_u32r =
  153. (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) *
  154. ((int32_t)bmx280_instance->hum_cal.dig_H1)) >>
  155. 4));
  156. v_x1_u32r = (v_x1_u32r < 0 ? 0 : v_x1_u32r);
  157. v_x1_u32r = (v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r);
  158. return ((uint32_t)(v_x1_u32r >> 12)) / 1024.0f;
  159. }
  160. static bool bmx280_readCalValues(I2CSensor* i2c_sensor) {
  161. BMx280_instance* bmx280_instance = (BMx280_instance*)i2c_sensor->sensorInstance;
  162. if(!unitemp_i2c_readRegArray(
  163. i2c_sensor, TEMP_CAL_START_ADDR, 6, (uint8_t*)&bmx280_instance->temp_cal))
  164. return false;
  165. #ifdef UNITEMP_DEBUG
  166. FURI_LOG_D(
  167. APP_NAME,
  168. "Sensor BMx280 (0x%02X) T1-T3: %d, %d, %d",
  169. i2c_sensor->currentI2CAdr,
  170. bmx280_instance->temp_cal.dig_T1,
  171. bmx280_instance->temp_cal.dig_T2,
  172. bmx280_instance->temp_cal.dig_T3);
  173. #endif
  174. if(!unitemp_i2c_readRegArray(
  175. i2c_sensor, PRESS_CAL_START_ADDR, 18, (uint8_t*)&bmx280_instance->press_cal))
  176. return false;
  177. #ifdef UNITEMP_DEBUG
  178. FURI_LOG_D(
  179. APP_NAME,
  180. "Sensor BMx280 (0x%02X): P1-P9: %d, %d, %d, %d, %d, %d, %d, %d, %d",
  181. i2c_sensor->currentI2CAdr,
  182. bmx280_instance->press_cal.dig_P1,
  183. bmx280_instance->press_cal.dig_P2,
  184. bmx280_instance->press_cal.dig_P3,
  185. bmx280_instance->press_cal.dig_P4,
  186. bmx280_instance->press_cal.dig_P5,
  187. bmx280_instance->press_cal.dig_P6,
  188. bmx280_instance->press_cal.dig_P7,
  189. bmx280_instance->press_cal.dig_P8,
  190. bmx280_instance->press_cal.dig_P9);
  191. #endif
  192. if(bmx280_instance->chip_id == BME280_ID) {
  193. uint8_t buff[7] = {0};
  194. if(!unitemp_i2c_readRegArray(i2c_sensor, HUM_CAL_H1_ADDR, 1, buff)) return false;
  195. bmx280_instance->hum_cal.dig_H1 = buff[0];
  196. if(!unitemp_i2c_readRegArray(i2c_sensor, HUM_CAL_H2_ADDR, 7, buff)) return false;
  197. bmx280_instance->hum_cal.dig_H2 = (uint16_t)(buff[0] | ((uint16_t)buff[1] << 8));
  198. bmx280_instance->hum_cal.dig_H3 = buff[2];
  199. bmx280_instance->hum_cal.dig_H4 = ((int16_t)buff[3] << 4) | (buff[4] & 0x0F);
  200. bmx280_instance->hum_cal.dig_H5 = (buff[4] & 0x0F) | ((int16_t)buff[5] << 4);
  201. bmx280_instance->hum_cal.dig_H6 = buff[6];
  202. #ifdef UNITEMP_DEBUG
  203. FURI_LOG_D(
  204. APP_NAME,
  205. "Sensor BMx280 (0x%02X): H1-H6: %d, %d, %d, %d, %d, %d",
  206. i2c_sensor->currentI2CAdr,
  207. bmx280_instance->hum_cal.dig_H1,
  208. bmx280_instance->hum_cal.dig_H2,
  209. bmx280_instance->hum_cal.dig_H3,
  210. bmx280_instance->hum_cal.dig_H4,
  211. bmx280_instance->hum_cal.dig_H5,
  212. bmx280_instance->hum_cal.dig_H6);
  213. #endif
  214. }
  215. bmx280_instance->last_cal_update_time = furi_get_tick();
  216. return true;
  217. }
  218. static bool bmp280_isMeasuring(Sensor* sensor) {
  219. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  220. return (bool)((unitemp_i2c_readReg(i2c_sensor, BMx280_REG_STATUS) & 0x08) >> 3);
  221. }
  222. bool unitemp_BMx280_alloc(Sensor* sensor, char* args) {
  223. UNUSED(args);
  224. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  225. BMx280_instance* bmx280_instance = malloc(sizeof(BMx280_instance));
  226. if(bmx280_instance == NULL) {
  227. FURI_LOG_E(APP_NAME, "Failed to allocation sensor %s instance", sensor->name);
  228. return false;
  229. }
  230. if(sensor->type == &BMP280) bmx280_instance->chip_id = BMP280_ID;
  231. if(sensor->type == &BME280) bmx280_instance->chip_id = BME280_ID;
  232. i2c_sensor->sensorInstance = bmx280_instance;
  233. i2c_sensor->minI2CAdr = BMx280_I2C_ADDR_MIN;
  234. i2c_sensor->maxI2CAdr = BMx280_I2C_ADDR_MAX;
  235. return true;
  236. }
  237. bool unitemp_BMx280_init(Sensor* sensor) {
  238. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  239. //Перезагрузка
  240. unitemp_i2c_writeReg(i2c_sensor, 0xE0, 0xB6);
  241. //Чтение ID датчика
  242. uint8_t id = unitemp_i2c_readReg(i2c_sensor, 0xD0);
  243. if(id != BMP280_ID && id != BME280_ID) {
  244. FURI_LOG_E(
  245. APP_NAME,
  246. "Sensor %s returned wrong ID 0x%02X, expected 0x%02X or 0x%02X",
  247. sensor->name,
  248. id,
  249. BMP280_ID,
  250. BME280_ID);
  251. return false;
  252. }
  253. //Настройка режимов работы
  254. if(id == BME280_ID) {
  255. unitemp_i2c_writeReg(i2c_sensor, BME280_REG_CTRL_HUM, BME280_HUM_OVERSAMPLING_1);
  256. unitemp_i2c_writeReg(
  257. i2c_sensor, BME280_REG_CTRL_HUM, unitemp_i2c_readReg(i2c_sensor, BME280_REG_CTRL_HUM));
  258. }
  259. unitemp_i2c_writeReg(
  260. i2c_sensor,
  261. BMx280_REG_CTRL_MEAS,
  262. BMx280_TEMP_OVERSAMPLING_2 | BMx280_PRESS_OVERSAMPLING_4 | BMx280_MODE_NORMAL);
  263. //Настройка периода опроса и фильтрации значений
  264. unitemp_i2c_writeReg(
  265. i2c_sensor,
  266. BMx280_REG_CONFIG,
  267. BMx280_STANDBY_TIME_500 | BMx280_FILTER_COEFF_16 | BMx280_SPI_3W_DISABLE);
  268. //Чтение калибровочных значений
  269. if(!bmx280_readCalValues(i2c_sensor)) {
  270. FURI_LOG_E(APP_NAME, "Failed to read calibration values sensor %s", sensor->name);
  271. return false;
  272. }
  273. return true;
  274. }
  275. bool unitemp_BMx280_deinit(Sensor* sensor) {
  276. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  277. //Перевод в сон
  278. unitemp_i2c_writeReg(i2c_sensor, BMx280_REG_CTRL_MEAS, BMx280_MODE_SLEEP);
  279. return true;
  280. }
  281. UnitempStatus unitemp_BMx280_update(Sensor* sensor) {
  282. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  283. BMx280_instance* instance = i2c_sensor->sensorInstance;
  284. uint32_t t = furi_get_tick();
  285. uint8_t buff[3];
  286. //Проверка инициализированности датчика
  287. unitemp_i2c_readRegArray(i2c_sensor, 0xF4, 2, buff);
  288. if(buff[0] == 0) {
  289. FURI_LOG_W(APP_NAME, "Sensor %s is not initialized!", sensor->name);
  290. return UT_SENSORSTATUS_ERROR;
  291. }
  292. while(bmp280_isMeasuring(sensor)) {
  293. if(furi_get_tick() - t > 100) {
  294. return UT_SENSORSTATUS_TIMEOUT;
  295. }
  296. }
  297. if(furi_get_tick() - instance->last_cal_update_time > BOSCH_CAL_UPDATE_INTERVAL) {
  298. bmx280_readCalValues(i2c_sensor);
  299. }
  300. if(!unitemp_i2c_readRegArray(i2c_sensor, 0xFA, 3, buff)) return UT_SENSORSTATUS_TIMEOUT;
  301. int32_t adc_T = ((int32_t)buff[0] << 12) | ((int32_t)buff[1] << 4) | ((int32_t)buff[2] >> 4);
  302. if(!unitemp_i2c_readRegArray(i2c_sensor, 0xF7, 3, buff)) return UT_SENSORSTATUS_TIMEOUT;
  303. int32_t adc_P = ((int32_t)buff[0] << 12) | ((int32_t)buff[1] << 4) | ((int32_t)buff[2] >> 4);
  304. if(!unitemp_i2c_readRegArray(i2c_sensor, 0xFD, 2, buff)) return UT_SENSORSTATUS_TIMEOUT;
  305. int32_t adc_H = ((uint16_t)buff[0] << 8) | buff[1];
  306. sensor->temp = BMx280_compensate_temperature(i2c_sensor, adc_T);
  307. sensor->pressure = BMx280_compensate_pressure(i2c_sensor, adc_P);
  308. sensor->hum = BMx280_compensate_humidity(i2c_sensor, adc_H);
  309. return UT_SENSORSTATUS_OK;
  310. }
  311. bool unitemp_BMx280_free(Sensor* sensor) {
  312. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  313. free(i2c_sensor->sensorInstance);
  314. return true;
  315. }