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

@ -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
*/ */
/**************************************************************************/ enum {
#define BME280_ADDRESS_ALTERNATE (0x76) // Alternate Address 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,
@brief Register addresses BME280_REGISTER_DIG_P3 = 0x92,
*/ BME280_REGISTER_DIG_P4 = 0x94,
/**************************************************************************/ BME280_REGISTER_DIG_P5 = 0x96,
enum BME280_REGISTER_DIG_P6 = 0x98,
{ BME280_REGISTER_DIG_P7 = 0x9A,
BME280_REGISTER_DIG_T1 = 0x88, BME280_REGISTER_DIG_P8 = 0x9C,
BME280_REGISTER_DIG_T2 = 0x8A, BME280_REGISTER_DIG_P9 = 0x9E,
BME280_REGISTER_DIG_T3 = 0x8C,
BME280_REGISTER_DIG_P1 = 0x8E, BME280_REGISTER_DIG_H1 = 0xA1,
BME280_REGISTER_DIG_P2 = 0x90, BME280_REGISTER_DIG_H2 = 0xE1,
BME280_REGISTER_DIG_P3 = 0x92, BME280_REGISTER_DIG_H3 = 0xE3,
BME280_REGISTER_DIG_P4 = 0x94, BME280_REGISTER_DIG_H4 = 0xE4,
BME280_REGISTER_DIG_P5 = 0x96, BME280_REGISTER_DIG_H5 = 0xE5,
BME280_REGISTER_DIG_P6 = 0x98, BME280_REGISTER_DIG_H6 = 0xE7,
BME280_REGISTER_DIG_P7 = 0x9A,
BME280_REGISTER_DIG_P8 = 0x9C,
BME280_REGISTER_DIG_P9 = 0x9E,
BME280_REGISTER_DIG_H1 = 0xA1, BME280_REGISTER_CHIPID = 0xD0,
BME280_REGISTER_DIG_H2 = 0xE1, BME280_REGISTER_VERSION = 0xD1,
BME280_REGISTER_DIG_H3 = 0xE3, BME280_REGISTER_SOFTRESET = 0xE0,
BME280_REGISTER_DIG_H4 = 0xE4,
BME280_REGISTER_DIG_H5 = 0xE5,
BME280_REGISTER_DIG_H6 = 0xE7,
BME280_REGISTER_CHIPID = 0xD0, BME280_REGISTER_CAL26 = 0xE1, // R calibration stored in 0xE1-0xF0
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_CONTROLHUMID = 0xF2, BME280_REGISTER_CONTROL = 0xF4,
BME280_REGISTER_STATUS = 0XF3, BME280_REGISTER_CONFIG = 0xF5,
BME280_REGISTER_CONTROL = 0xF4, BME280_REGISTER_PRESSUREDATA = 0xF7,
BME280_REGISTER_CONFIG = 0xF5, BME280_REGISTER_TEMPDATA = 0xFA,
BME280_REGISTER_PRESSUREDATA = 0xF7, BME280_REGISTER_HUMIDDATA = 0xFD
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;
/*=========================================================================*/ /*=========================================================================*/
/* /*
@ -144,214 +132,208 @@ class Adafruit_BME280_Unified : public Adafruit_Sensor
*/ */
/**************************************************************************/ /**************************************************************************/
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 // constructors
Adafruit_BME280(void); Adafruit_BME280();
Adafruit_BME280(int8_t cspin); Adafruit_BME280(int8_t cspin, SPIClass *theSPI = &SPI)
Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin); Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin,
int8_t sckpin);
bool begin(void); bool begin();
bool begin(TwoWire *theWire); bool begin(TwoWire *theWire);
bool begin(uint8_t addr); bool begin(uint8_t addr);
bool begin(uint8_t addr, TwoWire *theWire); bool begin(uint8_t addr, TwoWire *theWire);
bool init(); bool init();
void setSampling(sensor_mode mode = MODE_NORMAL, void setSampling(sensor_mode mode = MODE_NORMAL,
sensor_sampling tempSampling = SAMPLING_X16, sensor_sampling tempSampling = SAMPLING_X16,
sensor_sampling pressSampling = SAMPLING_X16, sensor_sampling pressSampling = SAMPLING_X16,
sensor_sampling humSampling = SAMPLING_X16, sensor_sampling humSampling = SAMPLING_X16,
sensor_filter filter = FILTER_OFF, sensor_filter filter = FILTER_OFF,
standby_duration duration = STANDBY_MS_0_5 standby_duration duration = STANDBY_MS_0_5);
);
void takeForcedMeasurement(); void takeForcedMeasurement();
float readTemperature(void); float readTemperature(void);
float readPressure(void); float readPressure(void);
float readHumidity(void); float readHumidity(void);
float readAltitude(float seaLevel); float readAltitude(float seaLevel);
float seaLevelForAltitude(float altitude, float pressure); float seaLevelForAltitude(float altitude, float pressure);
uint32_t sensorID(void); uint32_t sensorID(void);
protected: protected:
TwoWire *_wire; //!< pointer to a TwoWire object TwoWire *_wire; //!< pointer to a TwoWire object
void readCoefficients(void); SPIClass *_spi; //!< pointer to SPI object
bool isReadingCalibration(void); void readCoefficients(void);
uint8_t spixfer(uint8_t x); bool isReadingCalibration(void);
uint8_t spixfer(uint8_t x);
void write8(byte reg, byte value); void write8(byte reg, byte value);
uint8_t read8(byte reg); uint8_t read8(byte reg);
uint16_t read16(byte reg); uint16_t read16(byte reg);
uint32_t read24(byte reg); uint32_t read24(byte reg);
int16_t readS16(byte reg); int16_t readS16(byte reg);
uint16_t read16_LE(byte reg); // little endian uint16_t read16_LE(byte reg); // little endian
int16_t readS16_LE(byte reg); // little endian int16_t readS16_LE(byte reg); // little endian
uint8_t _i2caddr; //!< I2C addr for the TwoWire interface uint8_t _i2caddr; //!< I2C addr for the TwoWire interface
int32_t _sensorID; //!< ID of the BME Sensor int32_t _sensorID; //!< ID of the BME Sensor
int32_t t_fine; //!< temperature with high resolution, stored as an attribute as this is used for temperature compensation reading humidity and pressure int32_t t_fine; //!< temperature with high resolution, stored as an attribute
//!< as this is used for temperature compensation reading
//!< humidity and pressure
int8_t _cs; //!< for the SPI interface int8_t _cs; //!< for the SPI interface
int8_t _mosi; //!< for the SPI interface int8_t _mosi; //!< for the SPI interface
int8_t _miso; //!< for the SPI interface int8_t _miso; //!< for the SPI interface
int8_t _sck; //!< for the SPI interface int8_t _sck; //!< for the SPI interface
bme280_calib_data _bme280_calib; //!< here calibration data is stored bme280_calib_data _bme280_calib; //!< here calibration data is stored
/**************************************************************************/
/*!
@brief config register
*/
/**************************************************************************/
struct config {
// inactive duration (standby time) in normal mode
// 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
/*! // 000 = filter off
@brief config register // 001 = 2x filter
*/ // 010 = 4x filter
/**************************************************************************/ // 011 = 8x filter
struct config { // 100 and above = 16x filter
// inactive duration (standby time) in normal mode unsigned int filter : 3; ///< filter settings
// 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 // unused - don't set
// 000 = filter off unsigned int none : 1; ///< unused - don't set
// 001 = 2x filter unsigned int spi3w_en : 1; ///< unused - don't set
// 010 = 4x filter
// 011 = 8x filter
// 100 and above = 16x filter
unsigned int filter : 3; ///< filter settings
// unused - don't set /// @return combined config register
unsigned int none : 1; ///< unused - don't set unsigned int get() { return (t_sb << 5) | (filter << 2) | spi3w_en; }
unsigned int spi3w_en : 1; ///< unused - don't set };
config _configReg; //!< config register object
/// @return combined config register /**************************************************************************/
unsigned int get() { /*!
return (t_sb << 5) | (filter << 2) | spi3w_en; @brief ctrl_meas register
} */
}; /**************************************************************************/
config _configReg; //!< config register object struct ctrl_meas {
// temperature oversampling
// 000 = skipped
// 001 = x1
// 010 = x2
// 011 = x4
// 100 = x8
// 101 and above = x16
unsigned int osrs_t : 3; ///< temperature oversampling
// pressure oversampling
// 000 = skipped
// 001 = x1
// 010 = x2
// 011 = x4
// 100 = x8
// 101 and above = x16
unsigned int osrs_p : 3; ///< pressure oversampling
/**************************************************************************/ // device mode
/*! // 00 = sleep
@brief ctrl_meas register // 01 or 10 = forced
*/ // 11 = normal
/**************************************************************************/ unsigned int mode : 2; ///< device mode
struct ctrl_meas {
// temperature oversampling
// 000 = skipped
// 001 = x1
// 010 = x2
// 011 = x4
// 100 = x8
// 101 and above = x16
unsigned int osrs_t : 3; ///< temperature oversampling
// pressure oversampling /// @return combined ctrl register
// 000 = skipped unsigned int get() { return (osrs_t << 5) | (osrs_p << 2) | mode; }
// 001 = x1 };
// 010 = x2 ctrl_meas _measReg; //!< measurement register object
// 011 = x4
// 100 = x8
// 101 and above = x16
unsigned int osrs_p : 3; ///< pressure oversampling
// device mode /**************************************************************************/
// 00 = sleep /*!
// 01 or 10 = forced @brief ctrl_hum register
// 11 = normal */
unsigned int mode : 2; ///< device mode /**************************************************************************/
struct ctrl_hum {
/// unused - don't set
unsigned int none : 5;
/// @return combined ctrl register // pressure oversampling
unsigned int get() { // 000 = skipped
return (osrs_t << 5) | (osrs_p << 2) | mode; // 001 = x1
} // 010 = x2
}; // 011 = x4
ctrl_meas _measReg; //!< measurement register object // 100 = x8
// 101 and above = x16
unsigned int osrs_h : 3; ///< pressure oversampling
/// @return combined ctrl hum register
/**************************************************************************/ unsigned int get() { return (osrs_h); }
/*! };
@brief ctrl_hum register ctrl_hum _humReg; //!< hum register object
*/
/**************************************************************************/
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