BMP280_DEV.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. BMP280_DEV is an I2C/SPI compatible library for the Bosch BMP280 barometer.
  3. Copyright (C) Martin Lindupp 2019
  4. V1.0.0 -- Initial release
  5. V1.0.1 -- Added ESP32 HSPI support and change library to unique name
  6. V1.0.2 -- Modification to allow external creation of HSPI object on ESP32
  7. V1.0.3 -- Changed library name in the library.properties file
  8. V1.0.5 -- Fixed bug in BMP280_DEV::getTemperature() function, thanks to Jon M.
  9. V1.0.6 -- Merged multiple instances and initialisation pull requests by sensslen
  10. V1.0.8 -- Used default arguments for begin() member function and
  11. added example using multiple BMP280 devices with SPI comms in NORMAL mode
  12. V1.0.9 -- Moved writeMask to Device class and improved measurement detection code
  13. V1.0.10 -- Modification to allow user-defined pins for I2C operation on the ESP8266
  14. V1.0.12 -- Allow sea level pressure calibration using setSeaLevelPressure() function
  15. V1.0.14 -- Fix uninitialised structures, thanks to David Jade investigating and
  16. flagging up this issue
  17. V1.0.16 -- Modification to allow user-defined pins for I2C operation on the ESP32
  18. V1.0.17 -- Added getCurrentTemperature(), getCurrentPressure(), getCurrentTempPres()
  19. getCurrentAltitude() and getCurrentMeasurements() functions,
  20. to allow the BMP280 to be read directly without checking the measuring bit
  21. V1.0.18 -- Initialise "device" constructor member variables in the same order they are declared
  22. The MIT License (MIT)
  23. Permission is hereby granted, free of charge, to any person obtaining a copy
  24. of this software and associated documentation files (the "Software"), to deal
  25. in the Software without restriction, including without limitation the rights
  26. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  27. copies of the Software, and to permit persons to whom the Software is
  28. furnished to do so, subject to the following conditions:
  29. The above copyright notice and this permission notice shall be included in all
  30. copies or substantial portions of the Software.
  31. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  32. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  33. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  34. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  35. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  36. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  37. SOFTWARE.
  38. */
  39. #ifndef BMP280_DEV_h
  40. #define BMP280_DEV_h
  41. #include <Device.h>
  42. ////////////////////////////////////////////////////////////////////////////////
  43. // BMP280_DEV Definitions
  44. ////////////////////////////////////////////////////////////////////////////////
  45. #define BMP280_I2C_ADDR 0x77 // The BMP280 I2C address
  46. #define BMP280_I2C_ALT_ADDR 0x76 // The BMP280 I2C alternate address
  47. #define DEVICE_ID 0x58 // The BMP280 device ID
  48. #define RESET_CODE 0xB6 // The BMP280 reset code
  49. enum SPIPort { BMP280_SPI0, BMP280_SPI1 };
  50. ////////////////////////////////////////////////////////////////////////////////
  51. // BMP280_DEV Registers
  52. ////////////////////////////////////////////////////////////////////////////////
  53. enum {
  54. BMP280_TRIM_PARAMS = 0x88, // Trim parameter registers' base sub-address
  55. BMP280_DEVICE_ID = 0xD0, // Device ID register sub-address
  56. BMP280_RESET = 0xE0, // Reset register sub-address
  57. BMP280_STATUS = 0xF3, // Status register sub-address
  58. BMP280_CTRL_MEAS = 0xF4, // Control and measurement register sub-address
  59. BMP280_CONFIG = 0xF5, // Configuration register sub-address
  60. BMP280_PRES_MSB = 0xF7, // Pressure Most Significant Byte (MSB) register sub-address
  61. BMP280_PRES_LSB = 0xF8, // Pressure Least Significant Byte (LSB) register sub-address
  62. BMP280_PRES_XLSB = 0xF9, // Pressure eXtended Least Significant Byte (XLSB) register sub-address
  63. BMP280_TEMP_MSB = 0xFA, // Pressure Most Significant Byte (MSB) register sub-address
  64. BMP280_TEMP_LSB = 0xFB, // Pressure Least Significant Byte (LSB) register sub-address
  65. BMP280_TEMP_XLSB = 0xFC // Pressure eXtended Least Significant Byte (XLSB) register sub-address
  66. };
  67. ////////////////////////////////////////////////////////////////////////////////
  68. // BMP280_DEV Modes
  69. ////////////////////////////////////////////////////////////////////////////////
  70. enum Mode {
  71. SLEEP_MODE = 0x00, // Device mode bitfield in the control and measurement register
  72. FORCED_MODE = 0x01,
  73. NORMAL_MODE = 0x03
  74. };
  75. ////////////////////////////////////////////////////////////////////////////////
  76. // BMP280_DEV Register bit field Definitions
  77. ////////////////////////////////////////////////////////////////////////////////
  78. enum Oversampling {
  79. OVERSAMPLING_SKIP = 0x00, // Oversampling bit fields in the control and measurement register
  80. OVERSAMPLING_X1 = 0x01,
  81. OVERSAMPLING_X2 = 0x02,
  82. OVERSAMPLING_X4 = 0x03,
  83. OVERSAMPLING_X8 = 0x04,
  84. OVERSAMPLING_X16 = 0x05
  85. };
  86. enum IIRFilter {
  87. IIR_FILTER_OFF = 0x00, // Infinite Impulse Response (IIR) filter bit field in the configuration register
  88. IIR_FILTER_2 = 0x01,
  89. IIR_FILTER_4 = 0x02,
  90. IIR_FILTER_8 = 0x03,
  91. IIR_FILTER_16 = 0x04
  92. };
  93. enum TimeStandby {
  94. TIME_STANDBY_05MS = 0x00, // Time standby bit field in the configuration register
  95. TIME_STANDBY_62MS = 0x01,
  96. TIME_STANDBY_125MS = 0x02,
  97. TIME_STANDBY_250MS = 0x03,
  98. TIME_STANDBY_500MS = 0x04,
  99. TIME_STANDBY_1000MS = 0x05,
  100. TIME_STANDBY_2000MS = 0x06,
  101. TIME_STANDBY_4000MS = 0x07
  102. };
  103. ////////////////////////////////////////////////////////////////////////////////
  104. // BMP280_DEV Class definition
  105. ////////////////////////////////////////////////////////////////////////////////
  106. class BMP280_DEV : public Device { // Derive the BMP280_DEV class from the Device class
  107. public:
  108. BMP280_DEV(); // BMP280_DEV object for I2C operation
  109. #ifdef ARDUINO_ARCH_ESP8266
  110. BMP280_DEV(uint8_t sda, uint8_t scl); // BMP280_DEV object for ESP8266 I2C operation with user-defined pins
  111. #endif
  112. BMP280_DEV(uint8_t cs); // BMP280_DEV object for SPI operation
  113. #ifdef ARDUINO_ARCH_ESP32
  114. BMP280_DEV(uint8_t sda, uint8_t scl); // BMP280_DEV object for ESP32 I2C operation with user-defined pins
  115. BMP280_DEV(uint8_t cs, uint8_t spiPort, SPIClass& spiClass); // BMP280_DEV object for SPI1 with supplied SPIClass object
  116. #endif
  117. uint8_t begin(Mode mode = SLEEP_MODE, // Initialise the barometer with arguments
  118. Oversampling presOversampling = OVERSAMPLING_X16,
  119. Oversampling tempOversampling = OVERSAMPLING_X2,
  120. IIRFilter iirFilter = IIR_FILTER_OFF,
  121. TimeStandby timeStandby = TIME_STANDBY_05MS);
  122. uint8_t begin(Mode mode, uint8_t addr); // Initialise the barometer specifying start mode and I2C addrss
  123. uint8_t begin(uint8_t addr); // Initialise the barometer specifying I2C address with default initialisation
  124. void reset(); // Soft reset the barometer
  125. void startNormalConversion(); // Start continuous measurement in NORMAL_MODE
  126. void startForcedConversion(); // Start a one shot measurement in FORCED_MODE
  127. void stopConversion(); // Stop the conversion and return to SLEEP_MODE
  128. void setPresOversampling(Oversampling presOversampling); // Set the pressure oversampling: OFF, X1, X2, X4, X8, X16
  129. void setTempOversampling(Oversampling tempOversampling); // Set the temperature oversampling: OFF, X1, X2, X4, X8, X16
  130. void setIIRFilter(IIRFilter iirFilter); // Set the IIR filter setting: OFF, 2, 4, 8, 16
  131. void setTimeStandby(TimeStandby timeStandby); // Set the time standby measurement interval: 0.5, 62, 125, 250, 500ms, 1s, 2s, 4s
  132. void setSeaLevelPressure(float pressure = 1013.23f); // Set the sea level pressure value
  133. void getCurrentTemperature(float &temperature); // Get the current temperature measurement without checking the measuring bit
  134. uint8_t getTemperature(float &temperature); // Get a temperature measurement
  135. void getCurrentPressure(float &pressure); // Get the current pressure without checking the measuring bit
  136. uint8_t getPressure(float &pressure); // Get a pressure measurement
  137. void getCurrentTempPres(float &temperature, float &pressure); // Get the current temperature and pressure without checking the measuring bit
  138. uint8_t getTempPres(float &temperature, float &pressure); // Get a temperature and pressure measurement
  139. void getCurrentAltitude(float &altitude); // Get the current altitude without checking the measuring bit
  140. uint8_t getAltitude(float &altitude); // Get an altitude measurement
  141. void getCurrentMeasurements(float &temperature, float &pressure, float &altitude); // Get all measurements without checking the measuring bit
  142. uint8_t getMeasurements(float &temperature, float &pressure, float &altitude); // Get temperature, pressure and altitude measurements
  143. protected:
  144. private:
  145. void setMode(Mode mode); // Set the barometer mode
  146. void setCtrlMeasRegister(Mode mode, Oversampling presOversampling, Oversampling tempOversamping); // Set the BMP280 control and measurement register
  147. void setConfigRegister(IIRFilter iirFilter, TimeStandby timeStandby); // Set the BMP280 configuration register
  148. uint8_t dataReady(); // Checks if a measurement is ready
  149. struct { // The BMP280 compensation trim parameters (coefficients)
  150. uint16_t dig_T1;
  151. int16_t dig_T2;
  152. int16_t dig_T3;
  153. uint16_t dig_P1;
  154. int16_t dig_P2;
  155. int16_t dig_P3;
  156. int16_t dig_P4;
  157. int16_t dig_P5;
  158. int16_t dig_P6;
  159. int16_t dig_P7;
  160. int16_t dig_P8;
  161. int16_t dig_P9;
  162. } params;
  163. union { // Copy of the BMP280's configuration register
  164. struct {
  165. uint8_t spi3w_en : 1;
  166. uint8_t : 1;
  167. uint8_t filter : 3;
  168. uint8_t t_sb : 3;
  169. } bit;
  170. uint8_t reg;
  171. } config = { .reg = 0 };
  172. union { // Copy of the BMP280's control and measurement register
  173. struct {
  174. uint8_t mode : 2;
  175. uint8_t osrs_p : 3;
  176. uint8_t osrs_t : 3;
  177. } bit;
  178. uint8_t reg;
  179. } ctrl_meas = { .reg = 0 };
  180. union { // Copy of the BMP280's status register
  181. struct {
  182. uint8_t im_update : 1;
  183. uint8_t : 2;
  184. uint8_t measuring : 1;
  185. } bit;
  186. uint8_t reg;
  187. } status = { .reg = 0 };
  188. int32_t t_fine; // Bosch t_fine variable
  189. int32_t bmp280_compensate_T_int32(int32_t adc_T); // Bosch temperature compensation function
  190. uint32_t bmp280_compensate_P_int64(int32_t adc_P); // Bosch pressure compensation function
  191. bool previous_measuring; // Previous measuring state
  192. float sea_level_pressure = 1013.23f; // Sea level pressure
  193. };
  194. #endif