Skip to content

Commit aa97a7a

Browse files
committed
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.
1 parent e715642 commit aa97a7a

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

adafruit_bme680.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@
5252
_BME680_REG_SOFTRESET = const(0xE0)
5353
_BME680_REG_CTRL_GAS = const(0x71)
5454
_BME680_REG_CTRL_HUM = const(0x72)
55-
_BME280_REG_STATUS = const(0xF3)
55+
_BME680_REG_STATUS = const(0x73)
5656
_BME680_REG_CTRL_MEAS = const(0x74)
5757
_BME680_REG_CONFIG = const(0x75)
5858

59-
_BME680_REG_STATUS = const(0x1D)
59+
_BME680_REG_MEAS_STATUS = const(0x1D)
6060
_BME680_REG_PDATA = const(0x1F)
6161
_BME680_REG_TDATA = const(0x22)
6262
_BME680_REG_HDATA = const(0x25)
@@ -266,7 +266,7 @@ def _perform_reading(self):
266266
self._write(_BME680_REG_CTRL_MEAS, [ctrl])
267267
new_data = False
268268
while not new_data:
269-
data = self._read(_BME680_REG_STATUS, 15)
269+
data = self._read(_BME680_REG_MEAS_STATUS, 15)
270270
new_data = data[0] & 0x80 != 0
271271
time.sleep(0.005)
272272
self._last_reading = time.monotonic()
@@ -368,6 +368,11 @@ def __init__(self, spi, cs, baudrate=100000, debug=False, *, refresh_rate=10):
368368
super().__init__(refresh_rate=refresh_rate)
369369

370370
def _read(self, register, length):
371+
if register != _BME680_REG_STATUS:
372+
#_BME680_REG_STATUS exists in both SPI memory pages
373+
#For all other registers, we must set the correct memory page
374+
self._set_spi_mem_page(register)
375+
371376
register = (register | 0x80) & 0xFF # Read single, bit 7 high.
372377
with self._spi as spi:
373378
spi.write(bytearray([register])) #pylint: disable=no-member
@@ -378,6 +383,10 @@ def _read(self, register, length):
378383
return result
379384

380385
def _write(self, register, values):
386+
if register != _BME680_REG_STATUS:
387+
#_BME680_REG_STATUS exists in both SPI memory pages
388+
#For all other registers, we must set the correct memory page
389+
self._set_spi_mem_page(register)
381390
register &= 0x7F # Write, bit 7 low.
382391
with self._spi as spi:
383392
buffer = bytearray(2 * len(values))
@@ -387,3 +396,9 @@ def _write(self, register, values):
387396
spi.write(buffer) #pylint: disable=no-member
388397
if self._debug:
389398
print("\t$%02X <= %s" % (values[0], [hex(i) for i in values[1:]]))
399+
400+
def _set_spi_mem_page(self, register):
401+
spi_mem_page = 0x00
402+
if register < 0x80:
403+
spi_mem_page = 0x10
404+
self._write(_BME680_REG_STATUS, spi_mem_page)

0 commit comments

Comments
 (0)