BMP280.c 8.9 KB

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