BMP180.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "BMP180.h"
  16. #include "../interfaces/I2CSensor.h"
  17. typedef struct {
  18. int16_t AC1;
  19. int16_t AC2;
  20. int16_t AC3;
  21. uint16_t AC4;
  22. uint16_t AC5;
  23. uint16_t AC6;
  24. int16_t B1;
  25. int16_t B2;
  26. int16_t MB;
  27. int16_t MC;
  28. int16_t MD;
  29. } BMP180_cal;
  30. typedef struct {
  31. //Калибровочные значения
  32. BMP180_cal bmp180_cal;
  33. } BMP180_instance;
  34. const SensorType BMP180 = {
  35. .typename = "BMP180",
  36. .interface = &I2C,
  37. .datatype = UT_TEMPERATURE | UT_PRESSURE,
  38. .pollingInterval = 1000,
  39. .allocator = unitemp_BMP180_I2C_alloc,
  40. .mem_releaser = unitemp_BMP180_I2C_free,
  41. .initializer = unitemp_BMP180_init,
  42. .deinitializer = unitemp_BMP180_I2C_deinit,
  43. .updater = unitemp_BMP180_I2C_update};
  44. bool unitemp_BMP180_I2C_alloc(Sensor* sensor, char* args) {
  45. UNUSED(args);
  46. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  47. //Адреса на шине I2C (7 бит)
  48. i2c_sensor->minI2CAdr = 0x77 << 1;
  49. i2c_sensor->maxI2CAdr = 0x77 << 1;
  50. BMP180_instance* bmx180_instance = malloc(sizeof(BMP180_instance));
  51. i2c_sensor->sensorInstance = bmx180_instance;
  52. return true;
  53. }
  54. bool unitemp_BMP180_I2C_free(Sensor* sensor) {
  55. UNUSED(sensor);
  56. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  57. free(i2c_sensor->sensorInstance);
  58. return true;
  59. }
  60. bool unitemp_BMP180_init(Sensor* sensor) {
  61. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  62. //Перезагрузка
  63. if(!unitemp_i2c_writeReg(i2c_sensor, 0xE0, 0xB6)) return false;
  64. furi_delay_ms(100);
  65. //Проверка ID
  66. uint8_t id = unitemp_i2c_readReg(i2c_sensor, 0xD0);
  67. if(id != 0x55) {
  68. FURI_LOG_E(
  69. APP_NAME, "Sensor %s returned wrong ID 0x%02X, expected 0x55", sensor->name, id);
  70. return false;
  71. }
  72. BMP180_instance* bmp180_instance = i2c_sensor->sensorInstance;
  73. uint8_t buff[22] = {0};
  74. //Чтение калибровочных значений
  75. if(!unitemp_i2c_readRegArray(i2c_sensor, 0xAA, 22, buff)) return false;
  76. bmp180_instance->bmp180_cal.AC1 = (buff[0] << 8) | buff[1];
  77. bmp180_instance->bmp180_cal.AC2 = (buff[2] << 8) | buff[3];
  78. bmp180_instance->bmp180_cal.AC3 = (buff[4] << 8) | buff[5];
  79. bmp180_instance->bmp180_cal.AC4 = (buff[6] << 8) | buff[7];
  80. bmp180_instance->bmp180_cal.AC5 = (buff[8] << 8) | buff[9];
  81. bmp180_instance->bmp180_cal.AC6 = (buff[10] << 8) | buff[11];
  82. bmp180_instance->bmp180_cal.B1 = (buff[12] << 8) | buff[13];
  83. bmp180_instance->bmp180_cal.B2 = (buff[14] << 8) | buff[15];
  84. bmp180_instance->bmp180_cal.MB = (buff[16] << 8) | buff[17];
  85. bmp180_instance->bmp180_cal.MC = (buff[18] << 8) | buff[19];
  86. bmp180_instance->bmp180_cal.MD = (buff[20] << 8) | buff[21];
  87. UNITEMP_DEBUG(
  88. "Sensor BMP180 (0x%02X) calibration values: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
  89. i2c_sensor->currentI2CAdr,
  90. bmp180_instance->bmp180_cal.AC1,
  91. bmp180_instance->bmp180_cal.AC2,
  92. bmp180_instance->bmp180_cal.AC3,
  93. bmp180_instance->bmp180_cal.AC4,
  94. bmp180_instance->bmp180_cal.AC5,
  95. bmp180_instance->bmp180_cal.AC6,
  96. bmp180_instance->bmp180_cal.B1,
  97. bmp180_instance->bmp180_cal.B2,
  98. bmp180_instance->bmp180_cal.MB,
  99. bmp180_instance->bmp180_cal.MC,
  100. bmp180_instance->bmp180_cal.MD);
  101. return true;
  102. }
  103. bool unitemp_BMP180_I2C_deinit(Sensor* sensor) {
  104. //Нечего деинициализировать
  105. UNUSED(sensor);
  106. return true;
  107. }
  108. UnitempStatus unitemp_BMP180_I2C_update(Sensor* sensor) {
  109. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  110. BMP180_instance* bmp180_instance = i2c_sensor->sensorInstance;
  111. //Чтение температуры
  112. if(!unitemp_i2c_writeReg(i2c_sensor, 0xF4, 0x2E)) return UT_SENSORSTATUS_TIMEOUT;
  113. furi_delay_ms(5);
  114. uint8_t buff[3] = {0};
  115. if(!unitemp_i2c_readRegArray(i2c_sensor, 0xF6, 2, buff)) return UT_SENSORSTATUS_TIMEOUT;
  116. int32_t UT = ((uint16_t)buff[0] << 8) + buff[1];
  117. int32_t X1 = (UT - bmp180_instance->bmp180_cal.AC6) * bmp180_instance->bmp180_cal.AC5 >> 15;
  118. int32_t X2 = (bmp180_instance->bmp180_cal.MC << 11) / (X1 + bmp180_instance->bmp180_cal.MD);
  119. int32_t B5 = X1 + X2;
  120. sensor->temp = ((B5 + 8) / 16) * 0.1f;
  121. //Чтение давления
  122. if(!unitemp_i2c_writeReg(i2c_sensor, 0xF4, 0x34 + (0b11 << 6))) return UT_SENSORSTATUS_TIMEOUT;
  123. furi_delay_ms(26);
  124. if(!unitemp_i2c_readRegArray(i2c_sensor, 0xF6, 3, buff)) return UT_SENSORSTATUS_TIMEOUT;
  125. uint32_t UP = ((buff[0] << 16) + (buff[1] << 8) + buff[2]) >> (8 - 0b11);
  126. int32_t B6, X3, B3, P;
  127. uint32_t B4, B7;
  128. B6 = B5 - 4000;
  129. X1 = (bmp180_instance->bmp180_cal.B2 * ((B6 * B6) >> 12)) >> 11;
  130. X2 = (bmp180_instance->bmp180_cal.AC2 * B6) >> 11;
  131. X3 = X1 + X2;
  132. B3 = (((bmp180_instance->bmp180_cal.AC1 * 4 + X3) << 0b11) + 2) >> 2;
  133. X1 = (bmp180_instance->bmp180_cal.AC3 * B6) >> 13;
  134. X2 = (bmp180_instance->bmp180_cal.B1 * ((B6 * B6) >> 12)) >> 16;
  135. X3 = ((X1 + X2) + 2) >> 2;
  136. B4 = (bmp180_instance->bmp180_cal.AC4 * (unsigned long)(X3 + 32768)) >> 15;
  137. B7 = ((unsigned long)UP - B3) * (50000 >> 0b11);
  138. if(B7 < 0x80000000)
  139. P = (B7 * 2) / B4;
  140. else
  141. P = (B7 / B4) * 2;
  142. X1 = (P >> 8) * (P >> 8);
  143. X1 = (X1 * 3038) >> 16;
  144. X2 = (-7357 * (P)) >> 16;
  145. P = P + ((X1 + X2 + 3791) >> 4);
  146. sensor->pressure = P;
  147. return UT_SENSORSTATUS_OK;
  148. }