From 9e5b3318442f978c74a115c46682efaef523534b Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Sun, 11 Feb 2018 13:28:58 +0100 Subject: [PATCH] hw SPI for bme --- src/BME280.cpp | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/BME280.cpp b/src/BME280.cpp index ac7accd..b13865e 100644 --- a/src/BME280.cpp +++ b/src/BME280.cpp @@ -10,24 +10,26 @@ BME280::BME280() { } bool BME280::begin(void) { -// SPI.begin(BME_SCK, BME_MISO, BME_MOSI, BME_CS); + SPI.begin(BME_SCK, BME_MISO, BME_MOSI, BME_CS); pinMode(18, OUTPUT); digitalWrite(18, HIGH); // disable LoRa_CS pinMode(BME_CS, OUTPUT); digitalWrite(BME_CS, LOW); // enable bme280 CS delay(100); - bme = new Adafruit_BME280(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI - valid = bme->begin(); - if (!valid) bme->init(); - if (!valid) return false; +// bme = new Adafruit_BME280(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI + bme = new Adafruit_BME280(BME_CS); // hardware SPI bme->setSampling(Adafruit_BME280::sensor_mode::MODE_NORMAL, - Adafruit_BME280::sensor_sampling::SAMPLING_X16, - Adafruit_BME280::sensor_sampling::SAMPLING_X16, - Adafruit_BME280::sensor_sampling::SAMPLING_X16, + Adafruit_BME280::sensor_sampling::SAMPLING_X8, + Adafruit_BME280::sensor_sampling::SAMPLING_X8, + Adafruit_BME280::sensor_sampling::SAMPLING_X8, Adafruit_BME280::sensor_filter::FILTER_OFF, Adafruit_BME280::standby_duration::STANDBY_MS_250); + valid = bme->begin(); + if (!valid) bme->init(); + + if (!valid) return false; return true; } @@ -44,20 +46,28 @@ bool BME280::reinit() { pinMode(18, OUTPUT); digitalWrite(18, HIGH); // disable LoRa_CS pinMode(BME_CS, OUTPUT); - digitalWrite(BME_CS, LOW); // enable bme280 CS +// digitalWrite(BME_CS, LOW); // enable bme280 CS int retries = 0; valid = false; while (!valid) { if (retries > 9) break; + /* reset chip */ + Serial.println("resetting BME280"); + SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0)); + digitalWrite(BME_CS, LOW); + constexpr byte BME280_REGISTER_SOFTRESET = 0xE0; + SPI.transfer(BME280_REGISTER_SOFTRESET & ~0x80); + SPI.transfer(0xB6); + digitalWrite(BME_CS, HIGH); + SPI.endTransaction(); // release the SPI bus + delay(300); // wait for chip to wake up + valid = bme->init(); retries++; - delay(100); float p = bme->readPressure() / 100.0F; if (p < 300 || p > 1100) valid = false; -// constexpr byte BME280_REGISTER_SOFTRESET = 0xE0; -// bme->write8(BME280_REGISTER_SOFTRESET, 0xB6); } if (!valid) { Serial.println("Could not find a valid BME280 sensor, check wiring!");