nRF51822, BME280, SSD1306_i2c, BLE400

Dependencies:   mbed SSD1306_128x64_I2C

Committer:
mamont090671
Date:
Thu Nov 28 11:43:48 2019 +0000
Revision:
0:6e40dd247a04
BME280, nRF51822, SSD1306_i2c, BLE400;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mamont090671 0:6e40dd247a04 1 /*
mamont090671 0:6e40dd247a04 2 bme280.h - driver for Bosch Sensortec BME280 combined humidity and pressure sensor.
mamont090671 0:6e40dd247a04 3
mamont090671 0:6e40dd247a04 4 Copyright (c) 2015 Elektor
mamont090671 0:6e40dd247a04 5
mamont090671 0:6e40dd247a04 6 26/11/2015 - CPV, Initial release.
mamont090671 0:6e40dd247a04 7
mamont090671 0:6e40dd247a04 8 This library is free software; you can redistribute it and/or
mamont090671 0:6e40dd247a04 9 modify it under the terms of the GNU Lesser General Public
mamont090671 0:6e40dd247a04 10 License as published by the Free Software Foundation; either
mamont090671 0:6e40dd247a04 11 version 2.1 of the License, or (at your option) any later version.
mamont090671 0:6e40dd247a04 12
mamont090671 0:6e40dd247a04 13 This library is distributed in the hope that it will be useful,
mamont090671 0:6e40dd247a04 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
mamont090671 0:6e40dd247a04 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
mamont090671 0:6e40dd247a04 16 Lesser General Public License for more details.
mamont090671 0:6e40dd247a04 17
mamont090671 0:6e40dd247a04 18 You should have received a copy of the GNU Lesser General
mamont090671 0:6e40dd247a04 19 Public License along with this library; if not, write to the
mamont090671 0:6e40dd247a04 20 Free Software Foundation, Inc., 59 Temple Place, Suite 330,
mamont090671 0:6e40dd247a04 21 Boston, MA 02111-1307 USA
mamont090671 0:6e40dd247a04 22
mamont090671 0:6e40dd247a04 23 */
mamont090671 0:6e40dd247a04 24
mamont090671 0:6e40dd247a04 25 #ifndef __BME280_H__
mamont090671 0:6e40dd247a04 26 #define __BME280_H__
mamont090671 0:6e40dd247a04 27
mamont090671 0:6e40dd247a04 28 #include <stdint.h>
mamont090671 0:6e40dd247a04 29
mamont090671 0:6e40dd247a04 30
mamont090671 0:6e40dd247a04 31 #define BME280_ALLOW_FLOAT (1)
mamont090671 0:6e40dd247a04 32
mamont090671 0:6e40dd247a04 33 #if BME280_ALLOW_FLOAT!=0
mamont090671 0:6e40dd247a04 34 typedef double temperature_t;
mamont090671 0:6e40dd247a04 35 typedef double pressure_t;
mamont090671 0:6e40dd247a04 36 typedef double humidity_t;
mamont090671 0:6e40dd247a04 37 #else
mamont090671 0:6e40dd247a04 38 typedef int32_t temperature_t;
mamont090671 0:6e40dd247a04 39 typedef uint32_t pressure_t;
mamont090671 0:6e40dd247a04 40 typedef uint32_t humidity_t;
mamont090671 0:6e40dd247a04 41 #endif /* BME280_ALLOW_FLOAT */
mamont090671 0:6e40dd247a04 42
mamont090671 0:6e40dd247a04 43
mamont090671 0:6e40dd247a04 44 // Two possible addresses, depending on level on SDO pin.
mamont090671 0:6e40dd247a04 45 #define BME280_I2C_ADDRESS1 (0x76)
mamont090671 0:6e40dd247a04 46 #define BME280_I2C_ADDRESS2 (0x77)
mamont090671 0:6e40dd247a04 47
mamont090671 0:6e40dd247a04 48 // Calibration registers.
mamont090671 0:6e40dd247a04 49 #define BME280_CAL_T1 (0x88)
mamont090671 0:6e40dd247a04 50 #define BME280_CAL_T2 (0x8a)
mamont090671 0:6e40dd247a04 51 #define BME280_CAL_T3 (0x8c)
mamont090671 0:6e40dd247a04 52 #define BME280_CAL_P1 (0x8e)
mamont090671 0:6e40dd247a04 53 #define BME280_CAL_P2 (0x90)
mamont090671 0:6e40dd247a04 54 #define BME280_CAL_P3 (0x92)
mamont090671 0:6e40dd247a04 55 #define BME280_CAL_P4 (0x94)
mamont090671 0:6e40dd247a04 56 #define BME280_CAL_P5 (0x96)
mamont090671 0:6e40dd247a04 57 #define BME280_CAL_P6 (0x98)
mamont090671 0:6e40dd247a04 58 #define BME280_CAL_P7 (0x9a)
mamont090671 0:6e40dd247a04 59 #define BME280_CAL_P8 (0x9c)
mamont090671 0:6e40dd247a04 60 #define BME280_CAL_P9 (0x9e)
mamont090671 0:6e40dd247a04 61 #define BME280_CAL_H1 (0xa1) /* 8 bits */
mamont090671 0:6e40dd247a04 62 #define BME280_CAL_H2 (0xe1)
mamont090671 0:6e40dd247a04 63 #define BME280_CAL_H3 (0xe3) /* 8 bits */
mamont090671 0:6e40dd247a04 64 #define BME280_CAL_H4 (0xe4) /* 12 bits, combined with H45 */
mamont090671 0:6e40dd247a04 65 #define BME280_CAL_H45 (0xe5) /* 12 bits, combined with H5 */
mamont090671 0:6e40dd247a04 66 #define BME280_CAL_H5 (0xe6) /* 8 bits */
mamont090671 0:6e40dd247a04 67 #define BME280_CAL_H6 (0xe7) /* 8 bits */
mamont090671 0:6e40dd247a04 68
mamont090671 0:6e40dd247a04 69 // Control registers.
mamont090671 0:6e40dd247a04 70 #define BME280_ID_REGISTER (0xd0) /* 8 bits */
mamont090671 0:6e40dd247a04 71 #define BME280_RESET_REGISTER (0xe0) /* 8 bits */
mamont090671 0:6e40dd247a04 72 #define BME280_CTRL_HUM_REGISTER (0xf2) /* 8 bits */
mamont090671 0:6e40dd247a04 73 #define BME280_STATUS_REGISTER (0xf3) /* 8 bits */
mamont090671 0:6e40dd247a04 74 #define BME280_CTRL_MEAS_REGISTER (0xf4) /* 8 bits */
mamont090671 0:6e40dd247a04 75 #define BME280_CONFIG_REGISTER (0xf5) /* 8 bits */
mamont090671 0:6e40dd247a04 76
mamont090671 0:6e40dd247a04 77 // Measurement registers.
mamont090671 0:6e40dd247a04 78 #define BME280_PRESSURE (0xf7) /* 20 bits */
mamont090671 0:6e40dd247a04 79 #define BME280_PRESSURE_MSB (0xf7) /* 8 bits */
mamont090671 0:6e40dd247a04 80 #define BME280_PRESSURE_LSB (0xf8) /* 8 bits */
mamont090671 0:6e40dd247a04 81 #define BME280_PRESSURE_XLSB (0xf9) /* 8 bits */
mamont090671 0:6e40dd247a04 82 #define BME280_TEMPERATURE (0xfa) /* 20 bits */
mamont090671 0:6e40dd247a04 83 #define BME280_TEMPERATURE_MSB (0xfa) /* 8 bits */
mamont090671 0:6e40dd247a04 84 #define BME280_TEMPERATURE_LSB (0xfb) /* 8 bits */
mamont090671 0:6e40dd247a04 85 #define BME280_TEMPERATURE_XLSB (0xfc) /* 8 bits */
mamont090671 0:6e40dd247a04 86 #define BME280_HUMIDITY (0xfd) /* 16 bits */
mamont090671 0:6e40dd247a04 87 #define BME280_HUMIDITY_MSB (0xfd) /* 8 bits */
mamont090671 0:6e40dd247a04 88 #define BME280_HUMIDITY_LSB (0xfe) /* 8 bits */
mamont090671 0:6e40dd247a04 89
mamont090671 0:6e40dd247a04 90 // It is recommended to read all the measurements in one go.
mamont090671 0:6e40dd247a04 91 #define BME280_MEASUREMENT_REGISTER (BME280_PRESSURE)
mamont090671 0:6e40dd247a04 92 #define BME280_MEASUREMENT_SIZE (8)
mamont090671 0:6e40dd247a04 93
mamont090671 0:6e40dd247a04 94 // Values for osrs_p & osrs_t fields of CTRL_MEAS register.
mamont090671 0:6e40dd247a04 95 #define BME280_SKIP (0)
mamont090671 0:6e40dd247a04 96 #define BME280_OVERSAMPLING_1X (1)
mamont090671 0:6e40dd247a04 97 #define BME280_OVERSAMPLING_2X (2)
mamont090671 0:6e40dd247a04 98 #define BME280_OVERSAMPLING_4X (3)
mamont090671 0:6e40dd247a04 99 #define BME280_OVERSAMPLING_8X (4)
mamont090671 0:6e40dd247a04 100 #define BME280_OVERSAMPLING_16X (5)
mamont090671 0:6e40dd247a04 101
mamont090671 0:6e40dd247a04 102 // Values for mode field of CTRL_MEAS register.
mamont090671 0:6e40dd247a04 103 #define BME280_MODE_SLEEP (0)
mamont090671 0:6e40dd247a04 104 #define BME280_MODE_FORCED (1)
mamont090671 0:6e40dd247a04 105 #define BME280_MODE_NORMAL (3)
mamont090671 0:6e40dd247a04 106
mamont090671 0:6e40dd247a04 107 // Value for RESET register.
mamont090671 0:6e40dd247a04 108 #define BME280_RESET (0xb6)
mamont090671 0:6e40dd247a04 109
mamont090671 0:6e40dd247a04 110 // Value of ID register.
mamont090671 0:6e40dd247a04 111 #define BME280_ID (0x60)
mamont090671 0:6e40dd247a04 112
mamont090671 0:6e40dd247a04 113 // Values for t_sb field of CONFIG register
mamont090671 0:6e40dd247a04 114 #define BME280_STANDBY_500_US (0)
mamont090671 0:6e40dd247a04 115 #define BME280_STANDBY_10_MS (6)
mamont090671 0:6e40dd247a04 116 #define BME280_STANDBY_20_MS (7)
mamont090671 0:6e40dd247a04 117 #define BME280_STANDBY_63_MS (1)
mamont090671 0:6e40dd247a04 118 #define BME280_STANDBY_125_MS (2)
mamont090671 0:6e40dd247a04 119 #define BME280_STANDBY_250_MS (3)
mamont090671 0:6e40dd247a04 120 #define BME280_STANDBY_500_MS (4)
mamont090671 0:6e40dd247a04 121 #define BME280_STANDBY_1000_MS (5)
mamont090671 0:6e40dd247a04 122
mamont090671 0:6e40dd247a04 123 // Values for filter field of CONFIG register
mamont090671 0:6e40dd247a04 124 #define BME280_FILTER_OFF (0)
mamont090671 0:6e40dd247a04 125 #define BME280_FILTER_COEFF_2 (1)
mamont090671 0:6e40dd247a04 126 #define BME280_FILTER_COEFF_4 (2)
mamont090671 0:6e40dd247a04 127 #define BME280_FILTER_COEFF_8 (3)
mamont090671 0:6e40dd247a04 128 #define BME280_FILTER_COEFF_16 (4)
mamont090671 0:6e40dd247a04 129
mamont090671 0:6e40dd247a04 130
mamont090671 0:6e40dd247a04 131 // I2C write function must support repeated start to avoid interruption of transactions.
mamont090671 0:6e40dd247a04 132 // User-provided function to write data_size bytes from buffer p_data to I2C device at bus address i2c_address.
mamont090671 0:6e40dd247a04 133 // Provide empty function if not used.
mamont090671 0:6e40dd247a04 134 extern void i2cWrite(uint8_t i2c_address, uint8_t *p_data, uint8_t data_size, uint8_t repeated_start);
mamont090671 0:6e40dd247a04 135 // User-provided function to read data_size bytes from I2C device at bus address i2c_address to buffer p_data.
mamont090671 0:6e40dd247a04 136 // Provide empty function if not used.
mamont090671 0:6e40dd247a04 137 extern void i2cRead(uint8_t i2c_address, uint8_t *p_data, uint8_t data_size);
mamont090671 0:6e40dd247a04 138
mamont090671 0:6e40dd247a04 139 // SPI functions must activate (make low) BME280 CSB pin before doing a transfer
mamont090671 0:6e40dd247a04 140 // and deactivate it (make high) when done.
mamont090671 0:6e40dd247a04 141 // User-provided function to write data_size bytes from buffer p_data to SPI device.
mamont090671 0:6e40dd247a04 142 // Provide empty function if not used.
mamont090671 0:6e40dd247a04 143 extern void spiWrite(uint8_t *p_data, uint8_t data_size);
mamont090671 0:6e40dd247a04 144 // User-provided function to read data_size bytes from SPI device to buffer p_data.
mamont090671 0:6e40dd247a04 145 // Provide empty function if not used.
mamont090671 0:6e40dd247a04 146 extern void spiRead(uint8_t *p_data, uint8_t data_size);
mamont090671 0:6e40dd247a04 147
mamont090671 0:6e40dd247a04 148
mamont090671 0:6e40dd247a04 149 class BME280
mamont090671 0:6e40dd247a04 150 {
mamont090671 0:6e40dd247a04 151 public:
mamont090671 0:6e40dd247a04 152 BME280(void);
mamont090671 0:6e40dd247a04 153 uint8_t begin(uint8_t i2cAddress=0); // I2C address not set means SPI.
mamont090671 0:6e40dd247a04 154
mamont090671 0:6e40dd247a04 155 void readCalibrationData(void);
mamont090671 0:6e40dd247a04 156 uint8_t readId(void);
mamont090671 0:6e40dd247a04 157 uint8_t readFrom(uint8_t reg, uint8_t data_size, uint8_t *p_data);
mamont090671 0:6e40dd247a04 158
mamont090671 0:6e40dd247a04 159 void read(void);
mamont090671 0:6e40dd247a04 160
mamont090671 0:6e40dd247a04 161 // If using floating points, values are scaled, if not then
mamont090671 0:6e40dd247a04 162 // temperature is 100*T, pressure is Pa en humidity is 1024*%RH
mamont090671 0:6e40dd247a04 163 temperature_t temperature(void) { return _temperature; } // [degrees Celsius] or 100*[degrees Celsius]
mamont090671 0:6e40dd247a04 164 pressure_t pressure(void) { return _pressure; } // [Pa]
mamont090671 0:6e40dd247a04 165 humidity_t humidity(void) { return _humidity; } // [%RH] or 1024*[%RH]
mamont090671 0:6e40dd247a04 166
mamont090671 0:6e40dd247a04 167 void writeControlRegisters(uint8_t osrs_t, uint8_t osrs_p, uint8_t osrs_h, uint8_t mode);
mamont090671 0:6e40dd247a04 168 void writeConfigRegister(uint8_t t_sb, uint8_t filter, uint8_t spi);
mamont090671 0:6e40dd247a04 169
mamont090671 0:6e40dd247a04 170 void reset(void);
mamont090671 0:6e40dd247a04 171
mamont090671 0:6e40dd247a04 172 private:
mamont090671 0:6e40dd247a04 173 uint8_t _i2c_address;
mamont090671 0:6e40dd247a04 174
mamont090671 0:6e40dd247a04 175 void busWrite(uint8_t *p_data, uint8_t data_size, uint8_t repeated_start);
mamont090671 0:6e40dd247a04 176 void busRead(uint8_t *p_data, uint8_t data_size);
mamont090671 0:6e40dd247a04 177
mamont090671 0:6e40dd247a04 178 // Calibration data.
mamont090671 0:6e40dd247a04 179 uint16_t _dig_T1;
mamont090671 0:6e40dd247a04 180 int16_t _dig_T2;
mamont090671 0:6e40dd247a04 181 int16_t _dig_T3;
mamont090671 0:6e40dd247a04 182 uint16_t _dig_P1;
mamont090671 0:6e40dd247a04 183 int16_t _dig_P2;
mamont090671 0:6e40dd247a04 184 int16_t _dig_P3;
mamont090671 0:6e40dd247a04 185 int16_t _dig_P4;
mamont090671 0:6e40dd247a04 186 int16_t _dig_P5;
mamont090671 0:6e40dd247a04 187 int16_t _dig_P6;
mamont090671 0:6e40dd247a04 188 int16_t _dig_P7;
mamont090671 0:6e40dd247a04 189 int16_t _dig_P8;
mamont090671 0:6e40dd247a04 190 int16_t _dig_P9;
mamont090671 0:6e40dd247a04 191 uint8_t _dig_H1;
mamont090671 0:6e40dd247a04 192 int16_t _dig_H2;
mamont090671 0:6e40dd247a04 193 uint8_t _dig_H3;
mamont090671 0:6e40dd247a04 194 int16_t _dig_H4;
mamont090671 0:6e40dd247a04 195 int16_t _dig_H5;
mamont090671 0:6e40dd247a04 196 int8_t _dig_H6;
mamont090671 0:6e40dd247a04 197 void clearCalibrationData(void);
mamont090671 0:6e40dd247a04 198
mamont090671 0:6e40dd247a04 199 uint8_t readUint8(uint8_t reg);
mamont090671 0:6e40dd247a04 200 uint16_t readUint16(uint8_t reg);
mamont090671 0:6e40dd247a04 201
mamont090671 0:6e40dd247a04 202 int32_t assembleRawValue(uint8_t *p_data, uint8_t has_xlsb);
mamont090671 0:6e40dd247a04 203
mamont090671 0:6e40dd247a04 204 int32_t _t_fine;
mamont090671 0:6e40dd247a04 205
mamont090671 0:6e40dd247a04 206 temperature_t compensateTemperature(int32_t adc_T);
mamont090671 0:6e40dd247a04 207 pressure_t compensatePressure(int32_t adc_P);
mamont090671 0:6e40dd247a04 208 humidity_t compensateHumidity(int32_t adc_H);
mamont090671 0:6e40dd247a04 209
mamont090671 0:6e40dd247a04 210 temperature_t _temperature;
mamont090671 0:6e40dd247a04 211 pressure_t _pressure;
mamont090671 0:6e40dd247a04 212 humidity_t _humidity;
mamont090671 0:6e40dd247a04 213 };
mamont090671 0:6e40dd247a04 214
mamont090671 0:6e40dd247a04 215
mamont090671 0:6e40dd247a04 216 #endif /* __BME280_H__*/