upm  0.8.0
Sensor/Actuator repository for libmraa (v1.1.1)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
bmp280.hpp
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 #include <mraa/i2c.hpp>
28 #include <mraa/spi.hpp>
29 #include <mraa/gpio.hpp>
30 
31 #include "upm/iPressureSensor.hpp"
32 #include "upm/iTemperatureSensor.hpp"
33 
34 #define BMP280_DEFAULT_I2C_BUS 0
35 #define BMP280_DEFAULT_SPI_BUS 0
36 #define BMP280_DEFAULT_ADDR 0x77
37 #define BMP280_DEFAULT_CHIPID 0x58
38 
39 namespace upm {
40 
75  class BMP280 : public ITemperatureSensor, public IPressureSensor {
76  public:
77  // special reset byte
78  const uint8_t BMP280_RESET_BYTE = 0xb6;
79 
80  // number of bytes of stored calibration data
81  const int CALIBRATION_BYTES = 26;
82 
86  typedef enum : uint8_t {
87  // Do not write into reserved bits.
88 
89  // read-only factory calibration data
90  REG_CALIB00 = 0x88,
91  REG_CALIB01 = 0x89,
92  REG_CALIB02 = 0x8a,
93  REG_CALIB03 = 0x8b,
94  REG_CALIB04 = 0x8c,
95  REG_CALIB05 = 0x8d,
96  REG_CALIB06 = 0x8e,
97  REG_CALIB07 = 0x8f,
98  REG_CALIB08 = 0x90,
99  REG_CALIB09 = 0x91,
100  REG_CALIB10 = 0x92,
101  REG_CALIB11 = 0x93,
102  REG_CALIB12 = 0x94,
103  REG_CALIB13 = 0x95,
104  REG_CALIB14 = 0x96,
105  REG_CALIB15 = 0x97,
106  REG_CALIB16 = 0x98,
107  REG_CALIB17 = 0x99,
108  REG_CALIB18 = 0x9a,
109  REG_CALIB19 = 0x9b,
110  REG_CALIB20 = 0x9c,
111  REG_CALIB21 = 0x9d,
112  REG_CALIB22 = 0x9e,
113  REG_CALIB23 = 0x9f,
114  REG_CALIB24 = 0xa0,
115  REG_CALIB25 = 0xa1,
116 
117  REG_CHIPID = 0xd0,
118  REG_RESET = 0xe0,
119  REG_STATUS = 0xf3,
120  REG_CTRL_MEAS = 0xf4,
121  REG_CONFIG = 0xf5,
122 
123  REG_PRESSURE_MSB = 0xf7,
124  REG_PRESSURE_LSB = 0xf8,
125  REG_PRESSURE_XLSB = 0xf9,
126 
127  REG_TEMPERATURE_MSB = 0xfa,
128  REG_TEMPERATURE_LSB = 0xfb,
129  REG_TEMPERATURE_XLSB = 0xfc
130  } BMP280_REGS_T;
131 
135  typedef enum {
136  CONFIG_SPI3W_EN = 0x01,
137 
138  // 0x02 reserved
139 
140  CONFIG_FILTER0 = 0x04,
141  CONFIG_FILTER1 = 0x08,
142  CONFIG_FILTER2 = 0x10,
143  _CONFIG_FILTER_MASK = 7,
144  _CONFIG_FILTER_SHIFT = 2,
145 
146  CONFIG_T_SB0 = 0x20,
147  CONFIG_T_SB1 = 0x40,
148  CONFIG_T_SB2 = 0x80,
149  _CONFIG_T_SB_MASK = 7,
150  _CONFIG_T_SB_SHIFT = 5
151  } CONFIG_BITS_T;
152 
156  typedef enum {
157  FILTER_OFF = 0, // 1 samples
158  FILTER_2 = 1, // 2
159  FILTER_4 = 2, // 5
160  FILTER_8 = 3, // 11
161  FILTER_16 = 4 // 22
162  } FILTER_T;
163 
167  typedef enum {
168  T_SB_0_5 = 0, // 0.5ms
169  T_SB_62_5 = 1, // 62.5ms
170  T_SB_125 = 2, // 125ms
171  T_SB_250 = 3,
172  T_SB_500 = 4,
173  T_SB_1000 = 5,
174  T_SB_2000 = 6, // bme280 - 10ms
175  T_SB_4000 = 7 // bme280 - 20ms
176  } T_SB_T;
177 
178 
182  typedef enum {
183  CTRL_MEAS_MODE0 = 0x01,
184  CTRL_MEAS_MODE1 = 0x02,
185  _CTRL_MEAS_MODE_MASK = 3,
186  _CTRL_MEAS_MODE_SHIFT = 0,
187 
188  CTRL_MEAS_OSRS_P0 = 0x04,
189  CTRL_MEAS_OSRS_P1 = 0x08,
190  CTRL_MEAS_OSRS_P2 = 0x10,
191  _CTRL_MEAS_OSRS_P_MASK = 7,
192  _CTRL_MEAS_OSRS_P_SHIFT = 2,
193 
194  CTRL_MEAS_OSRS_T0 = 0x04,
195  CTRL_MEAS_OSRS_T1 = 0x08,
196  CTRL_MEAS_OSRS_T2 = 0x10,
197  _CTRL_MEAS_OSRS_T_MASK = 7,
198  _CTRL_MEAS_OSRS_T_SHIFT = 5
199  } CTRL_MEAS_T;
200 
204  typedef enum {
205  MODE_SLEEP = 0,
206  MODE_FORCED = 1,
207  // 2 is also FORCED mode
208  MODE_NORMAL = 3
209  } MODES_T;
210 
214  typedef enum {
215  OSRS_P_SKIPPED = 0,
216  OSRS_P_OVERSAMPLING_1 = 1, // x1
217  OSRS_P_OVERSAMPLING_2 = 2, // x2
218  OSRS_P_OVERSAMPLING_4 = 3,
219  OSRS_P_OVERSAMPLING_8 = 4,
220  OSRS_P_OVERSAMPLING_16 = 5
221  } OSRS_P_T;
222 
226  typedef enum {
227  OSRS_T_SKIPPED = 0,
228  OSRS_T_OVERSAMPLING_1 = 1, // x1
229  OSRS_T_OVERSAMPLING_2 = 2, // x2
230  OSRS_T_OVERSAMPLING_4 = 3,
231  OSRS_T_OVERSAMPLING_8 = 4,
232  OSRS_T_OVERSAMPLING_16 = 5
233  } OSRS_T_T;
234 
238  typedef enum {
239  STATUS_IM_UPDATE = 0x01,
240  // 0x02-0x04 reserved
241  STATUS_MEASURING = 0x08
242  // 0x10-0x80 reserved
243  } STATUS_T;
244 
250  typedef enum {
251  USAGE_MODE_HANDHELD_LOW_POWER = 0,
252  USAGE_MODE_HANDHELD_DYNAMIC = 1,
253  USAGE_MODE_WEATHER_MONITOR = 2, // lowest power consumption
254  USAGE_MODE_FLOOR_CHG_DETECT = 3,
255  USAGE_MODE_DROP_DETECT = 4,
256  USAGE_MODE_INDOOR_NAV = 5 // highest resolution
257  } USAGE_MODE_T;
258 
275  BMP280(int bus=BMP280_DEFAULT_I2C_BUS, int addr=BMP280_DEFAULT_ADDR,
276  int cs=-1, uint8_t theChipID=BMP280_DEFAULT_CHIPID);
277 
281  virtual ~BMP280();
282 
286  virtual void update();
287 
293  uint8_t getChipID();
294 
298  void reset();
299 
310  float getTemperature(bool fahrenheit=false);
311 
318  float getPressure();
319 
328  float getAltitude(float seaLevelhPA=1013.25);
329 
339  virtual void setUsageMode(USAGE_MODE_T mode);
340 
350 
360 
368  void setTimerStandby(T_SB_T tsb);
369 
377  void setFilter(FILTER_T filter);
378 
387  void setMeasureMode(MODES_T mode);
388 
389 
390  // Interface support
391  const char *getModuleName()
392  {
393  return "BMP280";
394  };
395 
396  int getTemperatureCelsius()
397  {
398  return int(getTemperature(false));
399  };
400 
401  int getPressurePa()
402  {
403  return int(getPressure());
404  };
405 
406  protected:
407  mraa::I2c *m_i2c;
408  mraa::Spi *m_spi;
409  mraa::Gpio *m_gpioCS;
410 
411  uint8_t m_addr;
412 
413  // always stored in C
414  float m_temperature;
415 
416  // pressure in Pa
417  float m_pressure;
418 
419  // shared calibration data - set in temp conversion, used in
420  // pressure conversion.
421  int32_t m_t_fine;
422 
423  // current operating mode. MODE_FORCED requires special attention
424  // in update()
425  MODES_T m_mode;
426 
427  // return the status register
428  uint8_t getStatus();
429 
436  uint8_t readReg(uint8_t reg);
437 
445  int readRegs(uint8_t reg, uint8_t *buffer, int len);
446 
453  void writeReg(uint8_t reg, uint8_t val);
454 
455  // clear member data...
456  void clearData();
457 
458  // read the calibration data
459  virtual void readCalibrationData();
460 
461  // SPI chip select
462  void csOn();
463  void csOff();
464 
465  private:
466  // are we doing SPI?
467  bool m_isSPI;
468 
469  // calibration data temperature
470  uint16_t m_dig_T1;
471  int16_t m_dig_T2;
472  int16_t m_dig_T3;
473 
474  // calibration data pressure
475  uint16_t m_dig_P1;
476  int16_t m_dig_P2;
477  int16_t m_dig_P3;
478  int16_t m_dig_P4;
479  int16_t m_dig_P5;
480  int16_t m_dig_P6;
481  int16_t m_dig_P7;
482  int16_t m_dig_P8;
483  int16_t m_dig_P9;
484 
485  // Bosch supplied conversion/compensation functions from the
486  // datasheet.
487  int32_t bmp280_compensate_T_int32(int32_t adc_T);
488  uint32_t bmp280_compensate_P_int64(int32_t adc_P);
489  };
490 }
void writeReg(uint8_t reg, uint8_t val)
Definition: bmp280.cxx:266
CONFIG_BITS_T
Definition: bmp280.hpp:135
BMP280(int bus=BMP280_DEFAULT_I2C_BUS, int addr=BMP280_DEFAULT_ADDR, int cs=-1, uint8_t theChipID=BMP280_DEFAULT_CHIPID)
Definition: bmp280.cxx:55
int readRegs(uint8_t reg, uint8_t *buffer, int len)
Definition: bmp280.cxx:223
float getAltitude(float seaLevelhPA=1013.25)
Definition: bmp280.cxx:190
float getPressure()
Definition: bmp280.cxx:397
BMP280_REGS_T
Definition: bmp280.hpp:86
OSRS_T_T
Definition: bmp280.hpp:226
virtual void setUsageMode(USAGE_MODE_T mode)
Definition: bmp280.cxx:458
uint8_t readReg(uint8_t reg)
Definition: bmp280.cxx:196
Interface for Temperature Sensors.
Definition: iTemperatureSensor.hpp:34
OSRS_P_T
Definition: bmp280.hpp:214
void setMeasureMode(MODES_T mode)
Definition: bmp280.cxx:422
void reset()
Definition: bmp280.cxx:319
T_SB_T
Definition: bmp280.hpp:167
void setOversampleRatePressure(OSRS_P_T rate)
Definition: bmp280.cxx:433
virtual void update()
Definition: bmp280.cxx:139
virtual ~BMP280()
Definition: bmp280.cxx:127
STATUS_T
Definition: bmp280.hpp:238
CTRL_MEAS_T
Definition: bmp280.hpp:182
const char * getModuleName()
Definition: bmp280.hpp:391
void setTimerStandby(T_SB_T tsb)
Definition: bmp280.cxx:412
FILTER_T
Definition: bmp280.hpp:156
void setOversampleRateTemperature(OSRS_T_T rate)
Definition: bmp280.cxx:443
USAGE_MODE_T
Definition: bmp280.hpp:250
float getTemperature(bool fahrenheit=false)
Definition: bmp280.cxx:389
API for the BMP280 Digital Pressure Sensor.
Definition: bmp280.hpp:75
void setFilter(FILTER_T filter)
Definition: bmp280.cxx:402
uint8_t getChipID()
Definition: bmp280.cxx:313
MODES_T
Definition: bmp280.hpp:204
Interface for Pressue Sensors.
Definition: iPressureSensor.hpp:38