added spi fixes

This commit is contained in:
Jan Hoffmann 2019-06-20 18:26:48 +02:00
parent 4ea4fa8ae7
commit 7e39c445f0
2 changed files with 687 additions and 778 deletions

File diff suppressed because it is too large Load Diff

View File

@ -3,12 +3,12 @@
* *
* Designed specifically to work with the Adafruit BME280 Breakout * Designed specifically to work with the Adafruit BME280 Breakout
* ----> http://www.adafruit.com/products/2650 * ----> http://www.adafruit.com/products/2650
* *
* These sensors use I2C or SPI to communicate, 2 or 4 pins are required * These sensors use I2C or SPI to communicate, 2 or 4 pins are required
* to interface. * to interface.
* *
* Adafruit invests time and resources providing this open source code, * Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing * please support Adafruit and open-source hardware by purchasing
* products from Adafruit! * products from Adafruit!
* *
* Written by Kevin "KTOWN" Townsend for Adafruit Industries. * Written by Kevin "KTOWN" Townsend for Adafruit Industries.
@ -21,100 +21,88 @@
#ifndef __BME280_H__ #ifndef __BME280_H__
#define __BME280_H__ #define __BME280_H__
#if (ARDUINO >= 100) #include "Arduino.h"
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
#include <SPI.h>
#include <Wire.h> #include <Wire.h>
/**************************************************************************/ /*!
/*! * @brief default I2C address
@brief default I2C address */
*/ #define BME280_ADDRESS (0x77) // Primary I2C Address
/**************************************************************************/ /*!
#define BME280_ADDRESS (0x77) // Primary I2C Address * @brief alternate I2C address
/**************************************************************************/ */
/*! #define BME280_ADDRESS_ALTERNATE (0x76) // Alternate Address
@brief alternate I2C address
*/ /*!
/**************************************************************************/ * @brief Register addresses
#define BME280_ADDRESS_ALTERNATE (0x76) // Alternate Address */
/*=========================================================================*/ enum {
BME280_REGISTER_DIG_T1 = 0x88,
BME280_REGISTER_DIG_T2 = 0x8A,
BME280_REGISTER_DIG_T3 = 0x8C,
BME280_REGISTER_DIG_P1 = 0x8E,
BME280_REGISTER_DIG_P2 = 0x90,
BME280_REGISTER_DIG_P3 = 0x92,
BME280_REGISTER_DIG_P4 = 0x94,
BME280_REGISTER_DIG_P5 = 0x96,
BME280_REGISTER_DIG_P6 = 0x98,
BME280_REGISTER_DIG_P7 = 0x9A,
BME280_REGISTER_DIG_P8 = 0x9C,
BME280_REGISTER_DIG_P9 = 0x9E,
BME280_REGISTER_DIG_H1 = 0xA1,
BME280_REGISTER_DIG_H2 = 0xE1,
BME280_REGISTER_DIG_H3 = 0xE3,
BME280_REGISTER_DIG_H4 = 0xE4,
BME280_REGISTER_DIG_H5 = 0xE5,
BME280_REGISTER_DIG_H6 = 0xE7,
BME280_REGISTER_CHIPID = 0xD0,
BME280_REGISTER_VERSION = 0xD1,
BME280_REGISTER_SOFTRESET = 0xE0,
BME280_REGISTER_CAL26 = 0xE1, // R calibration stored in 0xE1-0xF0
BME280_REGISTER_CONTROLHUMID = 0xF2,
BME280_REGISTER_STATUS = 0XF3,
BME280_REGISTER_CONTROL = 0xF4,
BME280_REGISTER_CONFIG = 0xF5,
BME280_REGISTER_PRESSUREDATA = 0xF7,
BME280_REGISTER_TEMPDATA = 0xFA,
BME280_REGISTER_HUMIDDATA = 0xFD
};
/**************************************************************************/ /**************************************************************************/
/*! /*!
@brief Register addresses
*/
/**************************************************************************/
enum
{
BME280_REGISTER_DIG_T1 = 0x88,
BME280_REGISTER_DIG_T2 = 0x8A,
BME280_REGISTER_DIG_T3 = 0x8C,
BME280_REGISTER_DIG_P1 = 0x8E,
BME280_REGISTER_DIG_P2 = 0x90,
BME280_REGISTER_DIG_P3 = 0x92,
BME280_REGISTER_DIG_P4 = 0x94,
BME280_REGISTER_DIG_P5 = 0x96,
BME280_REGISTER_DIG_P6 = 0x98,
BME280_REGISTER_DIG_P7 = 0x9A,
BME280_REGISTER_DIG_P8 = 0x9C,
BME280_REGISTER_DIG_P9 = 0x9E,
BME280_REGISTER_DIG_H1 = 0xA1,
BME280_REGISTER_DIG_H2 = 0xE1,
BME280_REGISTER_DIG_H3 = 0xE3,
BME280_REGISTER_DIG_H4 = 0xE4,
BME280_REGISTER_DIG_H5 = 0xE5,
BME280_REGISTER_DIG_H6 = 0xE7,
BME280_REGISTER_CHIPID = 0xD0,
BME280_REGISTER_VERSION = 0xD1,
BME280_REGISTER_SOFTRESET = 0xE0,
BME280_REGISTER_CAL26 = 0xE1, // R calibration stored in 0xE1-0xF0
BME280_REGISTER_CONTROLHUMID = 0xF2,
BME280_REGISTER_STATUS = 0XF3,
BME280_REGISTER_CONTROL = 0xF4,
BME280_REGISTER_CONFIG = 0xF5,
BME280_REGISTER_PRESSUREDATA = 0xF7,
BME280_REGISTER_TEMPDATA = 0xFA,
BME280_REGISTER_HUMIDDATA = 0xFD
};
/**************************************************************************/
/*!
@brief calibration data @brief calibration data
*/ */
/**************************************************************************/ /**************************************************************************/
typedef struct typedef struct {
{ uint16_t dig_T1; ///< temperature compensation value
uint16_t dig_T1; ///< temperature compensation value int16_t dig_T2; ///< temperature compensation value
int16_t dig_T2; ///< temperature compensation value int16_t dig_T3; ///< temperature compensation value
int16_t dig_T3; ///< temperature compensation value
uint16_t dig_P1; ///< pressure compensation value uint16_t dig_P1; ///< pressure compensation value
int16_t dig_P2; ///< pressure compensation value int16_t dig_P2; ///< pressure compensation value
int16_t dig_P3; ///< pressure compensation value int16_t dig_P3; ///< pressure compensation value
int16_t dig_P4; ///< pressure compensation value int16_t dig_P4; ///< pressure compensation value
int16_t dig_P5; ///< pressure compensation value int16_t dig_P5; ///< pressure compensation value
int16_t dig_P6; ///< pressure compensation value int16_t dig_P6; ///< pressure compensation value
int16_t dig_P7; ///< pressure compensation value int16_t dig_P7; ///< pressure compensation value
int16_t dig_P8; ///< pressure compensation value int16_t dig_P8; ///< pressure compensation value
int16_t dig_P9; ///< pressure compensation value int16_t dig_P9; ///< pressure compensation value
uint8_t dig_H1; ///< humidity compensation value uint8_t dig_H1; ///< humidity compensation value
int16_t dig_H2; ///< humidity compensation value int16_t dig_H2; ///< humidity compensation value
uint8_t dig_H3; ///< humidity compensation value uint8_t dig_H3; ///< humidity compensation value
int16_t dig_H4; ///< humidity compensation value int16_t dig_H4; ///< humidity compensation value
int16_t dig_H5; ///< humidity compensation value int16_t dig_H5; ///< humidity compensation value
int8_t dig_H6; ///< humidity compensation value int8_t dig_H6; ///< humidity compensation value
} bme280_calib_data; } bme280_calib_data;
/*=========================================================================*/ /*=========================================================================*/
/* /*
@ -139,219 +127,213 @@ class Adafruit_BME280_Unified : public Adafruit_Sensor
*/ */
/**************************************************************************/ /**************************************************************************/
/*! /*!
@brief Class that stores state and functions for interacting with BME280 IC @brief Class that stores state and functions for interacting with BME280 IC
*/ */
/**************************************************************************/ /**************************************************************************/
class Adafruit_BME280 { class Adafruit_BME280 {
public: public:
/**************************************************************************/ /**************************************************************************/
/*! /*!
@brief sampling rates @brief sampling rates
*/ */
/**************************************************************************/ /**************************************************************************/
enum sensor_sampling { enum sensor_sampling {
SAMPLING_NONE = 0b000, SAMPLING_NONE = 0b000,
SAMPLING_X1 = 0b001, SAMPLING_X1 = 0b001,
SAMPLING_X2 = 0b010, SAMPLING_X2 = 0b010,
SAMPLING_X4 = 0b011, SAMPLING_X4 = 0b011,
SAMPLING_X8 = 0b100, SAMPLING_X8 = 0b100,
SAMPLING_X16 = 0b101 SAMPLING_X16 = 0b101
}; };
/**************************************************************************/ /**************************************************************************/
/*! /*!
@brief power modes @brief power modes
*/ */
/**************************************************************************/ /**************************************************************************/
enum sensor_mode { enum sensor_mode {
MODE_SLEEP = 0b00, MODE_SLEEP = 0b00,
MODE_FORCED = 0b01, MODE_FORCED = 0b01,
MODE_NORMAL = 0b11 MODE_NORMAL = 0b11
}; };
/**************************************************************************/ /**************************************************************************/
/*! /*!
@brief filter values @brief filter values
*/ */
/**************************************************************************/ /**************************************************************************/
enum sensor_filter { enum sensor_filter {
FILTER_OFF = 0b000, FILTER_OFF = 0b000,
FILTER_X2 = 0b001, FILTER_X2 = 0b001,
FILTER_X4 = 0b010, FILTER_X4 = 0b010,
FILTER_X8 = 0b011, FILTER_X8 = 0b011,
FILTER_X16 = 0b100 FILTER_X16 = 0b100
}; };
/**************************************************************************/ /**************************************************************************/
/*! /*!
@brief standby duration in ms @brief standby duration in ms
*/ */
/**************************************************************************/ /**************************************************************************/
enum standby_duration { enum standby_duration {
STANDBY_MS_0_5 = 0b000, STANDBY_MS_0_5 = 0b000,
STANDBY_MS_10 = 0b110, STANDBY_MS_10 = 0b110,
STANDBY_MS_20 = 0b111, STANDBY_MS_20 = 0b111,
STANDBY_MS_62_5 = 0b001, STANDBY_MS_62_5 = 0b001,
STANDBY_MS_125 = 0b010, STANDBY_MS_125 = 0b010,
STANDBY_MS_250 = 0b011, STANDBY_MS_250 = 0b011,
STANDBY_MS_500 = 0b100, STANDBY_MS_500 = 0b100,
STANDBY_MS_1000 = 0b101 STANDBY_MS_1000 = 0b101
}; };
// constructors
Adafruit_BME280(void);
Adafruit_BME280(int8_t cspin);
Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin);
bool begin(void);
bool begin(TwoWire *theWire);
bool begin(uint8_t addr);
bool begin(uint8_t addr, TwoWire *theWire);
bool init();
void setSampling(sensor_mode mode = MODE_NORMAL, // constructors
sensor_sampling tempSampling = SAMPLING_X16, Adafruit_BME280();
sensor_sampling pressSampling = SAMPLING_X16, Adafruit_BME280(int8_t cspin, SPIClass *theSPI = &SPI)
sensor_sampling humSampling = SAMPLING_X16, Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin,
sensor_filter filter = FILTER_OFF, int8_t sckpin);
standby_duration duration = STANDBY_MS_0_5
);
void takeForcedMeasurement();
float readTemperature(void);
float readPressure(void);
float readHumidity(void);
float readAltitude(float seaLevel);
float seaLevelForAltitude(float altitude, float pressure);
uint32_t sensorID(void);
protected:
TwoWire *_wire; //!< pointer to a TwoWire object
void readCoefficients(void);
bool isReadingCalibration(void);
uint8_t spixfer(uint8_t x);
void write8(byte reg, byte value); bool begin();
uint8_t read8(byte reg); bool begin(TwoWire *theWire);
uint16_t read16(byte reg); bool begin(uint8_t addr);
uint32_t read24(byte reg); bool begin(uint8_t addr, TwoWire *theWire);
int16_t readS16(byte reg); bool init();
uint16_t read16_LE(byte reg); // little endian
int16_t readS16_LE(byte reg); // little endian
uint8_t _i2caddr; //!< I2C addr for the TwoWire interface void setSampling(sensor_mode mode = MODE_NORMAL,
int32_t _sensorID; //!< ID of the BME Sensor sensor_sampling tempSampling = SAMPLING_X16,
int32_t t_fine; //!< temperature with high resolution, stored as an attribute as this is used for temperature compensation reading humidity and pressure sensor_sampling pressSampling = SAMPLING_X16,
sensor_sampling humSampling = SAMPLING_X16,
sensor_filter filter = FILTER_OFF,
standby_duration duration = STANDBY_MS_0_5);
int8_t _cs; //!< for the SPI interface void takeForcedMeasurement();
int8_t _mosi; //!< for the SPI interface float readTemperature(void);
int8_t _miso; //!< for the SPI interface float readPressure(void);
int8_t _sck; //!< for the SPI interface float readHumidity(void);
bme280_calib_data _bme280_calib; //!< here calibration data is stored float readAltitude(float seaLevel);
float seaLevelForAltitude(float altitude, float pressure);
uint32_t sensorID(void);
protected:
TwoWire *_wire; //!< pointer to a TwoWire object
SPIClass *_spi; //!< pointer to SPI object
void readCoefficients(void);
bool isReadingCalibration(void);
uint8_t spixfer(uint8_t x);
/**************************************************************************/ void write8(byte reg, byte value);
/*! uint8_t read8(byte reg);
@brief config register uint16_t read16(byte reg);
*/ uint32_t read24(byte reg);
/**************************************************************************/ int16_t readS16(byte reg);
struct config { uint16_t read16_LE(byte reg); // little endian
// inactive duration (standby time) in normal mode int16_t readS16_LE(byte reg); // little endian
// 000 = 0.5 ms
// 001 = 62.5 ms
// 010 = 125 ms
// 011 = 250 ms
// 100 = 500 ms
// 101 = 1000 ms
// 110 = 10 ms
// 111 = 20 ms
unsigned int t_sb : 3; ///< inactive duration (standby time) in normal mode
// filter settings uint8_t _i2caddr; //!< I2C addr for the TwoWire interface
// 000 = filter off int32_t _sensorID; //!< ID of the BME Sensor
// 001 = 2x filter int32_t t_fine; //!< temperature with high resolution, stored as an attribute
// 010 = 4x filter //!< as this is used for temperature compensation reading
// 011 = 8x filter //!< humidity and pressure
// 100 and above = 16x filter
unsigned int filter : 3; ///< filter settings
// unused - don't set int8_t _cs; //!< for the SPI interface
unsigned int none : 1; ///< unused - don't set int8_t _mosi; //!< for the SPI interface
unsigned int spi3w_en : 1; ///< unused - don't set int8_t _miso; //!< for the SPI interface
int8_t _sck; //!< for the SPI interface
/// @return combined config register bme280_calib_data _bme280_calib; //!< here calibration data is stored
unsigned int get() {
return (t_sb << 5) | (filter << 2) | spi3w_en;
}
};
config _configReg; //!< config register object
/**************************************************************************/
/**************************************************************************/ /*!
/*! @brief config register
@brief ctrl_meas register */
*/ /**************************************************************************/
/**************************************************************************/ struct config {
struct ctrl_meas { // inactive duration (standby time) in normal mode
// temperature oversampling // 000 = 0.5 ms
// 000 = skipped // 001 = 62.5 ms
// 001 = x1 // 010 = 125 ms
// 010 = x2 // 011 = 250 ms
// 011 = x4 // 100 = 500 ms
// 100 = x8 // 101 = 1000 ms
// 101 and above = x16 // 110 = 10 ms
unsigned int osrs_t : 3; ///< temperature oversampling // 111 = 20 ms
unsigned int t_sb : 3; ///< inactive duration (standby time) in normal mode
// pressure oversampling // filter settings
// 000 = skipped // 000 = filter off
// 001 = x1 // 001 = 2x filter
// 010 = x2 // 010 = 4x filter
// 011 = x4 // 011 = 8x filter
// 100 = x8 // 100 and above = 16x filter
// 101 and above = x16 unsigned int filter : 3; ///< filter settings
unsigned int osrs_p : 3; ///< pressure oversampling
// device mode // unused - don't set
// 00 = sleep unsigned int none : 1; ///< unused - don't set
// 01 or 10 = forced unsigned int spi3w_en : 1; ///< unused - don't set
// 11 = normal
unsigned int mode : 2; ///< device mode
/// @return combined ctrl register /// @return combined config register
unsigned int get() { unsigned int get() { return (t_sb << 5) | (filter << 2) | spi3w_en; }
return (osrs_t << 5) | (osrs_p << 2) | mode; };
} config _configReg; //!< config register object
};
ctrl_meas _measReg; //!< measurement register object
/**************************************************************************/
/**************************************************************************/ /*!
/*! @brief ctrl_meas register
@brief ctrl_hum register */
*/ /**************************************************************************/
/**************************************************************************/ struct ctrl_meas {
struct ctrl_hum { // temperature oversampling
/// unused - don't set // 000 = skipped
unsigned int none : 5; // 001 = x1
// 010 = x2
// 011 = x4
// 100 = x8
// 101 and above = x16
unsigned int osrs_t : 3; ///< temperature oversampling
// pressure oversampling // pressure oversampling
// 000 = skipped // 000 = skipped
// 001 = x1 // 001 = x1
// 010 = x2 // 010 = x2
// 011 = x4 // 011 = x4
// 100 = x8 // 100 = x8
// 101 and above = x16 // 101 and above = x16
unsigned int osrs_h : 3; ///< pressure oversampling unsigned int osrs_p : 3; ///< pressure oversampling
/// @return combined ctrl hum register // device mode
unsigned int get() { // 00 = sleep
return (osrs_h); // 01 or 10 = forced
} // 11 = normal
}; unsigned int mode : 2; ///< device mode
ctrl_hum _humReg; //!< hum register object
/// @return combined ctrl register
unsigned int get() { return (osrs_t << 5) | (osrs_p << 2) | mode; }
};
ctrl_meas _measReg; //!< measurement register object
/**************************************************************************/
/*!
@brief ctrl_hum register
*/
/**************************************************************************/
struct ctrl_hum {
/// unused - don't set
unsigned int none : 5;
// pressure oversampling
// 000 = skipped
// 001 = x1
// 010 = x2
// 011 = x4
// 100 = x8
// 101 and above = x16
unsigned int osrs_h : 3; ///< pressure oversampling
/// @return combined ctrl hum register
unsigned int get() { return (osrs_h); }
};
ctrl_hum _humReg; //!< hum register object
}; };
#endif #endif