BMP280.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "BMP280.h"
  16. const SensorType BMP280 = {
  17. .typename = "BMP280",
  18. .interface = &I2C,
  19. .datatype = UT_DATA_TYPE_TEMP_PRESS,
  20. .pollingInterval = 500,
  21. .allocator = unitemp_BMP280_alloc,
  22. .mem_releaser = unitemp_BMP280_free,
  23. .initializer = unitemp_BMP280_init,
  24. .deinitializer = unitemp_BMP280_deinit,
  25. .updater = unitemp_BMP280_update};
  26. //Интервал обновления калибровочных значений
  27. #define BMP280_CAL_UPDATE_INTERVAL 60000
  28. #define TEMP_CAL_START_ADDR 0x88
  29. #define PRESS_CAL_START_ADDR 0x8E
  30. #define BMP280_ID 0x58
  31. #define BMP280_REG_STATUS 0xF3
  32. #define BMP280_REG_CTRL_MEAS 0xF4
  33. #define BMP280_REG_CONFIG 0xF5
  34. //Преддескретизация температуры
  35. #define BMP280_TEMP_OVERSAMPLING_SKIP 0b00000000
  36. #define BMP280_TEMP_OVERSAMPLING_1 0b00100000
  37. #define BMP280_TEMP_OVERSAMPLING_2 0b01000000
  38. #define BMP280_TEMP_OVERSAMPLING_4 0b01100000
  39. #define BMP280_TEMP_OVERSAMPLING_8 0b10000000
  40. #define BMP280_TEMP_OVERSAMPLING_16 0b10100000
  41. //Преддескретизация давления
  42. #define BMP280_PRESS_OVERSAMPLING_SKIP 0b00000000
  43. #define BMP280_PRESS_OVERSAMPLING_1 0b00000100
  44. #define BMP280_PRESS_OVERSAMPLING_2 0b00001000
  45. #define BMP280_PRESS_OVERSAMPLING_4 0b00001100
  46. #define BMP280_PRESS_OVERSAMPLING_8 0b00010000
  47. #define BMP280_PRESS_OVERSAMPLING_16 0b00010100
  48. //Режимы работы датчика
  49. #define BMP280_MODE_SLEEP 0b00000000 //Спит и мало кушает
  50. #define BMP280_MODE_FORCED 0b00000001 //Обновляет значения 1 раз, после чего уходит в сон
  51. #define BMP280_MODE_NORMAL 0b00000011 //Регулярно обновляет значения
  52. //Период обновления в нормальном режиме
  53. #define BMP280_STANDBY_TIME_0_5 0b00000000
  54. #define BMP280_STANDBY_TIME_62_5 0b00100000
  55. #define BMP280_STANDBY_TIME_125 0b01000000
  56. #define BMP280_STANDBY_TIME_250 0b01100000
  57. #define BMP280_STANDBY_TIME_500 0b10000000
  58. #define BMP280_STANDBY_TIME_1000 0b10100000
  59. #define BMP280_STANDBY_TIME_2000 0b11000000
  60. #define BMP280_STANDBY_TIME_4000 0b11100000
  61. //Коэффициент фильтрации значений
  62. #define BMP280_FILTER_COEFF_1 0b00000000
  63. #define BMP280_FILTER_COEFF_2 0b00000100
  64. #define BMP280_FILTER_COEFF_4 0b00001000
  65. #define BMP280_FILTER_COEFF_8 0b00001100
  66. #define BMP280_FILTER_COEFF_16 0b00010000
  67. //Разрешить работу по SPI
  68. #define BMP280_SPI_3W_ENABLE 0b00000001
  69. #define BMP280_SPI_3W_DISABLE 0b00000000
  70. static double bmp280_compensate_T_double(I2CSensor* i2c_sensor, int32_t adc_T) {
  71. BMP280_instance* bmp280_instance = (BMP280_instance*)i2c_sensor->sensorInstance;
  72. double var1, var2, T;
  73. var1 = (((double)adc_T) / (double)16384.0 -
  74. ((double)bmp280_instance->temp_cal.dig_T1) / (double)1024.0) *
  75. ((double)bmp280_instance->temp_cal.dig_T2);
  76. var2 = ((((double)adc_T) / (double)131072.0 -
  77. ((double)bmp280_instance->temp_cal.dig_T1) / (double)8192.0) *
  78. (((double)adc_T) / (double)131072.0 -
  79. ((double)bmp280_instance->temp_cal.dig_T1) / (double)8192.0)) *
  80. ((double)bmp280_instance->temp_cal.dig_T3);
  81. bmp280_instance->t_fine = var1 + var2;
  82. T = (var1 + var2) / (double)5120.0;
  83. return T;
  84. }
  85. static uint32_t bmp280_compensate_P_int(I2CSensor* i2c_sensor, int32_t adc_P) {
  86. BMP280_instance* bmp280_instance = (BMP280_instance*)i2c_sensor->sensorInstance;
  87. int64_t value_1 = (bmp280_instance->t_fine) - 128000;
  88. int64_t value_2 = value_1 * value_1 * (int64_t)bmp280_instance->press_cal.dig_P6;
  89. value_2 = value_2 + ((value_1 * (int64_t)bmp280_instance->press_cal.dig_P5) << 17);
  90. value_2 = value_2 + (((int64_t)bmp280_instance->press_cal.dig_P4) << 35);
  91. value_1 = ((value_1 * value_1 * (int64_t)bmp280_instance->press_cal.dig_P3) >> 8) +
  92. ((value_1 * (int64_t)bmp280_instance->press_cal.dig_P2) << 12);
  93. value_1 = (((((int64_t)1) << 47) + value_1)) * ((int64_t)bmp280_instance->press_cal.dig_P1) >>
  94. 33;
  95. if(!value_1) return 0; // Avoid division by zero
  96. int64_t p = 1048576 - adc_P;
  97. p = (((p << 31) - value_2) * 3125) / value_1;
  98. value_1 = (((int64_t)bmp280_instance->press_cal.dig_P9) * (p >> 13) * (p >> 13)) >> 25;
  99. value_2 = (((int64_t)bmp280_instance->press_cal.dig_P8) * p) >> 19;
  100. p = ((p + value_1 + value_2) >> 8) + (((int64_t)bmp280_instance->press_cal.dig_P7) << 4);
  101. return p / 256.0;
  102. }
  103. static bool bmp280_readCalValues(I2CSensor* i2c_sensor) {
  104. BMP280_instance* bmp280_instance = (BMP280_instance*)i2c_sensor->sensorInstance;
  105. if(!unitemp_i2c_readRegArray(
  106. i2c_sensor, TEMP_CAL_START_ADDR, 6, (uint8_t*)&bmp280_instance->temp_cal))
  107. return false;
  108. #ifdef UNITEMP_DEBUG
  109. FURI_LOG_D(
  110. APP_NAME,
  111. "Sensor BMP280 (0x%02X) calibration values: T1: %d, T2: %d, T3: %d",
  112. i2c_sensor->currentI2CAdr,
  113. bmp280_instance->temp_cal.dig_T1,
  114. bmp280_instance->temp_cal.dig_T2,
  115. bmp280_instance->temp_cal.dig_T3);
  116. #endif
  117. if(!unitemp_i2c_readRegArray(
  118. i2c_sensor, PRESS_CAL_START_ADDR, 18, (uint8_t*)&bmp280_instance->press_cal))
  119. return false;
  120. #ifdef UNITEMP_DEBUG
  121. FURI_LOG_D(
  122. APP_NAME,
  123. "Sensor BMP280 (0x%02X): P1-9: %d, %d, %d, %d, %d, %d, %d, %d, %d",
  124. i2c_sensor->currentI2CAdr,
  125. bmp280_instance->press_cal.dig_P1,
  126. bmp280_instance->press_cal.dig_P2,
  127. bmp280_instance->press_cal.dig_P3,
  128. bmp280_instance->press_cal.dig_P4,
  129. bmp280_instance->press_cal.dig_P5,
  130. bmp280_instance->press_cal.dig_P6,
  131. bmp280_instance->press_cal.dig_P7,
  132. bmp280_instance->press_cal.dig_P8,
  133. bmp280_instance->press_cal.dig_P9);
  134. #endif
  135. bmp280_instance->last_cal_update_time = furi_get_tick();
  136. return true;
  137. }
  138. static bool bmp280_isMeasuring(Sensor* sensor) {
  139. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  140. return (bool)((unitemp_i2c_readReg(i2c_sensor, BMP280_REG_STATUS) & 0x08) >> 3);
  141. }
  142. bool unitemp_BMP280_alloc(Sensor* sensor, char* args) {
  143. UNUSED(args);
  144. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  145. BMP280_instance* bmp280_instance = malloc(sizeof(BMP280_instance));
  146. if(bmp280_instance == NULL) {
  147. FURI_LOG_E(APP_NAME, "Failed to allocation sensor %s instance", sensor->name);
  148. return false;
  149. }
  150. i2c_sensor->sensorInstance = bmp280_instance;
  151. i2c_sensor->minI2CAdr = 0x76;
  152. i2c_sensor->maxI2CAdr = 0x77;
  153. return true;
  154. }
  155. bool unitemp_BMP280_init(Sensor* sensor) {
  156. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  157. //Перезагрузка
  158. unitemp_i2c_writeReg(i2c_sensor, 0xE0, 0xB6);
  159. //Чтение ID датчика
  160. uint8_t id = unitemp_i2c_readReg(i2c_sensor, 0xD0);
  161. if(id != BMP280_ID) {
  162. FURI_LOG_E(
  163. APP_NAME,
  164. "Sensor %s returned wrong ID 0x%02X, expected 0x%02X",
  165. sensor->name,
  166. id,
  167. BMP280_ID);
  168. return false;
  169. }
  170. //Чтение калибровочных значений
  171. if(!bmp280_readCalValues(i2c_sensor)) {
  172. FURI_LOG_E(APP_NAME, "Failed to read calibration values sensor %s", sensor->name);
  173. return false;
  174. }
  175. //Настройка режимов работы
  176. unitemp_i2c_writeReg(
  177. i2c_sensor,
  178. BMP280_REG_CTRL_MEAS,
  179. BMP280_TEMP_OVERSAMPLING_2 | BMP280_PRESS_OVERSAMPLING_4 | BMP280_MODE_NORMAL);
  180. //Настройка периода опроса и фильтрации значений
  181. unitemp_i2c_writeReg(
  182. i2c_sensor,
  183. BMP280_REG_CONFIG,
  184. BMP280_STANDBY_TIME_500 | BMP280_FILTER_COEFF_16 | BMP280_SPI_3W_DISABLE);
  185. return true;
  186. }
  187. bool unitemp_BMP280_deinit(Sensor* sensor) {
  188. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  189. //Перевод в сон
  190. unitemp_i2c_writeReg(i2c_sensor, BMP280_REG_CTRL_MEAS, BMP280_MODE_SLEEP);
  191. return true;
  192. }
  193. UnitempStatus unitemp_BMP280_update(Sensor* sensor) {
  194. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  195. BMP280_instance* instance = i2c_sensor->sensorInstance;
  196. uint32_t t = furi_get_tick();
  197. if(furi_get_tick() - instance->last_cal_update_time > BMP280_CAL_UPDATE_INTERVAL) {
  198. bmp280_readCalValues(i2c_sensor);
  199. }
  200. uint8_t buff[3];
  201. //Проверка инициализированности датчика
  202. unitemp_i2c_readRegArray(i2c_sensor, 0xF4, 2, buff);
  203. if(buff[0] == 0) {
  204. FURI_LOG_W(APP_NAME, "Sensor %s is not initialized!", sensor->name);
  205. return UT_SENSORSTATUS_ERROR;
  206. }
  207. while(bmp280_isMeasuring(sensor)) {
  208. if(furi_get_tick() - t > 100) {
  209. return UT_SENSORSTATUS_TIMEOUT;
  210. }
  211. }
  212. if(!unitemp_i2c_readRegArray(i2c_sensor, 0xFA, 3, buff)) return UT_SENSORSTATUS_TIMEOUT;
  213. int32_t adc_T = ((int32_t)buff[0] << 12) | ((int32_t)buff[1] << 4) | ((int32_t)buff[2] >> 4);
  214. if(!unitemp_i2c_readRegArray(i2c_sensor, 0xF7, 3, buff)) return UT_SENSORSTATUS_TIMEOUT;
  215. int32_t adc_P = ((int32_t)buff[0] << 12) | ((int32_t)buff[1] << 4) | ((int32_t)buff[2] >> 4);
  216. sensor->temp = bmp280_compensate_T_double(i2c_sensor, adc_T);
  217. sensor->pressure = bmp280_compensate_P_int(i2c_sensor, adc_P);
  218. return UT_SENSORSTATUS_OK;
  219. }
  220. bool unitemp_BMP280_free(Sensor* sensor) {
  221. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  222. free(i2c_sensor->sensorInstance);
  223. return true;
  224. }