BMx280.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. Unitemp - Universal temperature reader
  3. Copyright (C) 2022-2023 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. UNITEMP_DEBUG(
  166. "Sensor BMx280 (0x%02X) T1-T3: %d, %d, %d",
  167. i2c_sensor->currentI2CAdr,
  168. bmx280_instance->temp_cal.dig_T1,
  169. bmx280_instance->temp_cal.dig_T2,
  170. bmx280_instance->temp_cal.dig_T3);
  171. if(!unitemp_i2c_readRegArray(
  172. i2c_sensor, PRESS_CAL_START_ADDR, 18, (uint8_t*)&bmx280_instance->press_cal))
  173. return false;
  174. UNITEMP_DEBUG(
  175. "Sensor BMx280 (0x%02X): P1-P9: %d, %d, %d, %d, %d, %d, %d, %d, %d",
  176. i2c_sensor->currentI2CAdr,
  177. bmx280_instance->press_cal.dig_P1,
  178. bmx280_instance->press_cal.dig_P2,
  179. bmx280_instance->press_cal.dig_P3,
  180. bmx280_instance->press_cal.dig_P4,
  181. bmx280_instance->press_cal.dig_P5,
  182. bmx280_instance->press_cal.dig_P6,
  183. bmx280_instance->press_cal.dig_P7,
  184. bmx280_instance->press_cal.dig_P8,
  185. bmx280_instance->press_cal.dig_P9);
  186. if(bmx280_instance->chip_id == BME280_ID) {
  187. uint8_t buff[7] = {0};
  188. if(!unitemp_i2c_readRegArray(i2c_sensor, HUM_CAL_H1_ADDR, 1, buff)) return false;
  189. bmx280_instance->hum_cal.dig_H1 = buff[0];
  190. if(!unitemp_i2c_readRegArray(i2c_sensor, HUM_CAL_H2_ADDR, 7, buff)) return false;
  191. bmx280_instance->hum_cal.dig_H2 = (uint16_t)(buff[0] | ((uint16_t)buff[1] << 8));
  192. bmx280_instance->hum_cal.dig_H3 = buff[2];
  193. bmx280_instance->hum_cal.dig_H4 = ((int16_t)buff[3] << 4) | (buff[4] & 0x0F);
  194. bmx280_instance->hum_cal.dig_H5 = (buff[4] & 0x0F) | ((int16_t)buff[5] << 4);
  195. bmx280_instance->hum_cal.dig_H6 = buff[6];
  196. UNITEMP_DEBUG(
  197. "Sensor BMx280 (0x%02X): H1-H6: %d, %d, %d, %d, %d, %d",
  198. i2c_sensor->currentI2CAdr,
  199. bmx280_instance->hum_cal.dig_H1,
  200. bmx280_instance->hum_cal.dig_H2,
  201. bmx280_instance->hum_cal.dig_H3,
  202. bmx280_instance->hum_cal.dig_H4,
  203. bmx280_instance->hum_cal.dig_H5,
  204. bmx280_instance->hum_cal.dig_H6);
  205. }
  206. bmx280_instance->last_cal_update_time = furi_get_tick();
  207. return true;
  208. }
  209. static bool bmp280_isMeasuring(Sensor* sensor) {
  210. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  211. return (bool)((unitemp_i2c_readReg(i2c_sensor, BMx280_REG_STATUS) & 0x08) >> 3);
  212. }
  213. bool unitemp_BMx280_alloc(Sensor* sensor, char* args) {
  214. UNUSED(args);
  215. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  216. BMx280_instance* bmx280_instance = malloc(sizeof(BMx280_instance));
  217. if(bmx280_instance == NULL) {
  218. FURI_LOG_E(APP_NAME, "Failed to allocation sensor %s instance", sensor->name);
  219. return false;
  220. }
  221. if(sensor->type == &BMP280) bmx280_instance->chip_id = BMP280_ID;
  222. if(sensor->type == &BME280) bmx280_instance->chip_id = BME280_ID;
  223. i2c_sensor->sensorInstance = bmx280_instance;
  224. i2c_sensor->minI2CAdr = BMx280_I2C_ADDR_MIN;
  225. i2c_sensor->maxI2CAdr = BMx280_I2C_ADDR_MAX;
  226. return true;
  227. }
  228. bool unitemp_BMx280_init(Sensor* sensor) {
  229. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  230. //Перезагрузка
  231. unitemp_i2c_writeReg(i2c_sensor, 0xE0, 0xB6);
  232. //Чтение ID датчика
  233. uint8_t id = unitemp_i2c_readReg(i2c_sensor, 0xD0);
  234. if(id != BMP280_ID && id != BME280_ID) {
  235. FURI_LOG_E(
  236. APP_NAME,
  237. "Sensor %s returned wrong ID 0x%02X, expected 0x%02X or 0x%02X",
  238. sensor->name,
  239. id,
  240. BMP280_ID,
  241. BME280_ID);
  242. return false;
  243. }
  244. //Настройка режимов работы
  245. if(id == BME280_ID) {
  246. unitemp_i2c_writeReg(i2c_sensor, BME280_REG_CTRL_HUM, BME280_HUM_OVERSAMPLING_1);
  247. unitemp_i2c_writeReg(
  248. i2c_sensor, BME280_REG_CTRL_HUM, unitemp_i2c_readReg(i2c_sensor, BME280_REG_CTRL_HUM));
  249. }
  250. unitemp_i2c_writeReg(
  251. i2c_sensor,
  252. BMx280_REG_CTRL_MEAS,
  253. BMx280_TEMP_OVERSAMPLING_2 | BMx280_PRESS_OVERSAMPLING_4 | BMx280_MODE_NORMAL);
  254. //Настройка периода опроса и фильтрации значений
  255. unitemp_i2c_writeReg(
  256. i2c_sensor,
  257. BMx280_REG_CONFIG,
  258. BMx280_STANDBY_TIME_500 | BMx280_FILTER_COEFF_16 | BMx280_SPI_3W_DISABLE);
  259. //Чтение калибровочных значений
  260. if(!bmx280_readCalValues(i2c_sensor)) {
  261. FURI_LOG_E(APP_NAME, "Failed to read calibration values sensor %s", sensor->name);
  262. return false;
  263. }
  264. return true;
  265. }
  266. bool unitemp_BMx280_deinit(Sensor* sensor) {
  267. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  268. //Перевод в сон
  269. unitemp_i2c_writeReg(i2c_sensor, BMx280_REG_CTRL_MEAS, BMx280_MODE_SLEEP);
  270. return true;
  271. }
  272. UnitempStatus unitemp_BMx280_update(Sensor* sensor) {
  273. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  274. BMx280_instance* instance = i2c_sensor->sensorInstance;
  275. uint32_t t = furi_get_tick();
  276. uint8_t buff[3];
  277. //Проверка инициализированности датчика
  278. unitemp_i2c_readRegArray(i2c_sensor, 0xF4, 2, buff);
  279. if(buff[0] == 0) {
  280. FURI_LOG_W(APP_NAME, "Sensor %s is not initialized!", sensor->name);
  281. return UT_SENSORSTATUS_ERROR;
  282. }
  283. while(bmp280_isMeasuring(sensor)) {
  284. if(furi_get_tick() - t > 100) {
  285. return UT_SENSORSTATUS_TIMEOUT;
  286. }
  287. }
  288. if(furi_get_tick() - instance->last_cal_update_time > BOSCH_CAL_UPDATE_INTERVAL) {
  289. bmx280_readCalValues(i2c_sensor);
  290. }
  291. if(!unitemp_i2c_readRegArray(i2c_sensor, 0xFA, 3, buff)) return UT_SENSORSTATUS_TIMEOUT;
  292. int32_t adc_T = ((int32_t)buff[0] << 12) | ((int32_t)buff[1] << 4) | ((int32_t)buff[2] >> 4);
  293. if(!unitemp_i2c_readRegArray(i2c_sensor, 0xF7, 3, buff)) return UT_SENSORSTATUS_TIMEOUT;
  294. int32_t adc_P = ((int32_t)buff[0] << 12) | ((int32_t)buff[1] << 4) | ((int32_t)buff[2] >> 4);
  295. if(!unitemp_i2c_readRegArray(i2c_sensor, 0xFD, 2, buff)) return UT_SENSORSTATUS_TIMEOUT;
  296. int32_t adc_H = ((uint16_t)buff[0] << 8) | buff[1];
  297. sensor->temp = BMx280_compensate_temperature(i2c_sensor, adc_T);
  298. sensor->pressure = BMx280_compensate_pressure(i2c_sensor, adc_P);
  299. sensor->hum = BMx280_compensate_humidity(i2c_sensor, adc_H);
  300. return UT_SENSORSTATUS_OK;
  301. }
  302. bool unitemp_BMx280_free(Sensor* sensor) {
  303. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  304. free(i2c_sensor->sensorInstance);
  305. return true;
  306. }