From aa97a7a5a62090a9db954ef28d7bb3a537f13e88 Mon Sep 17 00:00:00 2001 From: Jeff Raber Date: Wed, 13 Mar 2019 01:52:39 -0500 Subject: [PATCH] Change SPI memory page as needed when reading/writing through SPI In SPI mode, we must change the memory page depending on which register is being read/written. Renamed const _BME680_REG_STATUS to _BME680_REG_MEAS_STATUS to more closely match the name in the datasheet. --- adafruit_bme680.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/adafruit_bme680.py b/adafruit_bme680.py index 2e2bae7..f504d35 100644 --- a/adafruit_bme680.py +++ b/adafruit_bme680.py @@ -52,11 +52,11 @@ _BME680_BME680_RES_WAIT_0 = const(0x5A) _BME680_REG_SOFTRESET = const(0xE0) _BME680_REG_CTRL_GAS = const(0x71) _BME680_REG_CTRL_HUM = const(0x72) -_BME280_REG_STATUS = const(0xF3) +_BME680_REG_STATUS = const(0x73) _BME680_REG_CTRL_MEAS = const(0x74) _BME680_REG_CONFIG = const(0x75) -_BME680_REG_STATUS = const(0x1D) +_BME680_REG_MEAS_STATUS = const(0x1D) _BME680_REG_PDATA = const(0x1F) _BME680_REG_TDATA = const(0x22) _BME680_REG_HDATA = const(0x25) @@ -266,7 +266,7 @@ class Adafruit_BME680: self._write(_BME680_REG_CTRL_MEAS, [ctrl]) new_data = False while not new_data: - data = self._read(_BME680_REG_STATUS, 15) + data = self._read(_BME680_REG_MEAS_STATUS, 15) new_data = data[0] & 0x80 != 0 time.sleep(0.005) self._last_reading = time.monotonic() @@ -368,6 +368,11 @@ class Adafruit_BME680_SPI(Adafruit_BME680): super().__init__(refresh_rate=refresh_rate) def _read(self, register, length): + if register != _BME680_REG_STATUS: + #_BME680_REG_STATUS exists in both SPI memory pages + #For all other registers, we must set the correct memory page + self._set_spi_mem_page(register) + register = (register | 0x80) & 0xFF # Read single, bit 7 high. with self._spi as spi: spi.write(bytearray([register])) #pylint: disable=no-member @@ -378,6 +383,10 @@ class Adafruit_BME680_SPI(Adafruit_BME680): return result def _write(self, register, values): + if register != _BME680_REG_STATUS: + #_BME680_REG_STATUS exists in both SPI memory pages + #For all other registers, we must set the correct memory page + self._set_spi_mem_page(register) register &= 0x7F # Write, bit 7 low. with self._spi as spi: buffer = bytearray(2 * len(values)) @@ -387,3 +396,9 @@ class Adafruit_BME680_SPI(Adafruit_BME680): spi.write(buffer) #pylint: disable=no-member if self._debug: print("\t$%02X <= %s" % (values[0], [hex(i) for i in values[1:]])) + + def _set_spi_mem_page(self, register): + spi_mem_page = 0x00 + if register < 0x80: + spi_mem_page = 0x10 + self._write(_BME680_REG_STATUS, spi_mem_page)