BMP280.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #ifdef UNITEMP_DEBUG
  95. FURI_LOG_D(
  96. APP_NAME,
  97. "Sensor BMP280 (0x%02X) calibration values: T1: %d, T2: %d, T3: %d",
  98. i2c_sensor->currentI2CAdr,
  99. bmp280_instance->temp_cal.dig_T1,
  100. bmp280_instance->temp_cal.dig_T2,
  101. bmp280_instance->temp_cal.dig_T3);
  102. #endif
  103. if(!unitemp_i2c_readRegArray(
  104. i2c_sensor, PRESS_CAL_START_ADDR, 18, (uint8_t*)&bmp280_instance->press_cal))
  105. return false;
  106. #ifdef UNITEMP_DEBUG
  107. FURI_LOG_D(
  108. APP_NAME,
  109. "Sensor BMP280 (0x%02X): P1-9: %d, %d, %d, %d, %d, %d, %d, %d, %d",
  110. i2c_sensor->currentI2CAdr,
  111. bmp280_instance->press_cal.dig_P1,
  112. bmp280_instance->press_cal.dig_P2,
  113. bmp280_instance->press_cal.dig_P3,
  114. bmp280_instance->press_cal.dig_P4,
  115. bmp280_instance->press_cal.dig_P5,
  116. bmp280_instance->press_cal.dig_P6,
  117. bmp280_instance->press_cal.dig_P7,
  118. bmp280_instance->press_cal.dig_P8,
  119. bmp280_instance->press_cal.dig_P9);
  120. #endif
  121. bmp280_instance->last_cal_update_time = furi_get_tick();
  122. return true;
  123. }
  124. static bool bmp280_isMeasuring(Sensor* sensor) {
  125. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  126. return (bool)((unitemp_i2c_readReg(i2c_sensor, BMP280_REG_STATUS) & 0x08) >> 3);
  127. }
  128. bool unitemp_BMP280_alloc(Sensor* sensor, char* args) {
  129. UNUSED(args);
  130. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  131. BMP280_instance* bmp280_instance = malloc(sizeof(BMP280_instance));
  132. if(bmp280_instance == NULL) {
  133. FURI_LOG_E(APP_NAME, "Failed to allocation sensor %s instance", sensor->name);
  134. return false;
  135. }
  136. i2c_sensor->sensorInstance = bmp280_instance;
  137. i2c_sensor->minI2CAdr = 0x76;
  138. i2c_sensor->maxI2CAdr = 0x77;
  139. return true;
  140. }
  141. bool unitemp_BMP280_init(Sensor* sensor) {
  142. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  143. //Перезагрузка
  144. unitemp_i2c_writeReg(i2c_sensor, 0xE0, 0xB6);
  145. //Чтение ID датчика
  146. uint8_t id = unitemp_i2c_readReg(i2c_sensor, 0xD0);
  147. if(id != BMP280_ID) {
  148. FURI_LOG_E(
  149. APP_NAME,
  150. "Sensor %s returned wrong ID 0x%02X, expected 0x%02X",
  151. sensor->name,
  152. id,
  153. BMP280_ID);
  154. return false;
  155. }
  156. //Чтение калибровочных значений
  157. if(!bmp280_readCalValues(i2c_sensor)) {
  158. FURI_LOG_E(APP_NAME, "Failed to read calibration values sensor %s", sensor->name);
  159. return false;
  160. }
  161. //Настройка режимов работы
  162. unitemp_i2c_writeReg(
  163. i2c_sensor,
  164. BMP280_REG_CTRL_MEAS,
  165. BMP280_TEMP_OVERSAMPLING_2 | BMP280_PRESS_OVERSAMPLING_4 | BMP280_MODE_NORMAL);
  166. //Настройка периода опроса и фильтрации значений
  167. unitemp_i2c_writeReg(
  168. i2c_sensor,
  169. BMP280_REG_CONFIG,
  170. BMP280_STANDBY_TIME_500 | BMP280_FILTER_COEFF_16 | BMP280_SPI_3W_DISABLE);
  171. return true;
  172. }
  173. bool unitemp_BMP280_deinit(Sensor* sensor) {
  174. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  175. //Перевод в сон
  176. unitemp_i2c_writeReg(i2c_sensor, BMP280_REG_CTRL_MEAS, BMP280_MODE_SLEEP);
  177. return true;
  178. }
  179. UnitempStatus unitemp_BMP280_update(Sensor* sensor) {
  180. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  181. BMP280_instance* instance = i2c_sensor->sensorInstance;
  182. uint32_t t = furi_get_tick();
  183. if(furi_get_tick() - instance->last_cal_update_time > BMP280_CAL_UPDATE_INTERVAL) {
  184. bmp280_readCalValues(i2c_sensor);
  185. }
  186. uint8_t buff[3];
  187. //Проверка инициализированности датчика
  188. unitemp_i2c_readRegArray(i2c_sensor, 0xF4, 2, buff);
  189. if(buff[0] == 0) {
  190. FURI_LOG_W(APP_NAME, "Sensor %s is not initialized!", sensor->name);
  191. return UT_SENSORSTATUS_ERROR;
  192. }
  193. while(bmp280_isMeasuring(sensor)) {
  194. if(furi_get_tick() - t > 100) {
  195. return UT_SENSORSTATUS_TIMEOUT;
  196. }
  197. }
  198. if(!unitemp_i2c_readRegArray(i2c_sensor, 0xFA, 3, buff)) return UT_SENSORSTATUS_TIMEOUT;
  199. int32_t adc_T = ((int32_t)buff[0] << 12) | ((int32_t)buff[1] << 4) | ((int32_t)buff[2] >> 4);
  200. if(!unitemp_i2c_readRegArray(i2c_sensor, 0xF7, 3, buff)) return UT_SENSORSTATUS_TIMEOUT;
  201. int32_t adc_P = ((int32_t)buff[0] << 12) | ((int32_t)buff[1] << 4) | ((int32_t)buff[2] >> 4);
  202. sensor->temp = bmp280_compensate_T_double(i2c_sensor, adc_T);
  203. sensor->pressure = bmp280_compensate_P_int(i2c_sensor, adc_P);
  204. return UT_SENSORSTATUS_OK;
  205. }
  206. bool unitemp_BMP280_free(Sensor* sensor) {
  207. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  208. free(i2c_sensor->sensorInstance);
  209. return true;
  210. }