2021-09-27 16:40:36 -04:00
|
|
|
import board
|
|
|
|
import busio
|
|
|
|
import digitalio
|
|
|
|
|
|
|
|
cs = digitalio.DigitalInOut(board.SS)
|
|
|
|
cs.direction = digitalio.Direction.OUTPUT
|
|
|
|
cs.value = True
|
|
|
|
|
|
|
|
BME680_SPI_REGISTER = 0x73
|
|
|
|
BME680_CHIPID_REGISTER = 0xD0
|
|
|
|
BME680_CHIPID = 0x61
|
|
|
|
SPI_HERZ = 0x500000
|
|
|
|
|
|
|
|
spi = busio.SPI(board.SCK, MISO=board.MISO, MOSI=board.MOSI)
|
|
|
|
|
2021-09-28 17:13:04 -04:00
|
|
|
|
2021-09-27 16:40:36 -04:00
|
|
|
def readByte(addr):
|
|
|
|
value = -1
|
|
|
|
while not spi.try_lock():
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
spi.configure(baudrate=500000, phase=0, polarity=0)
|
2021-09-28 17:13:04 -04:00
|
|
|
|
2021-09-27 16:40:36 -04:00
|
|
|
cs.value = False
|
|
|
|
result = bytearray(1)
|
|
|
|
result[0] = addr | 0x80
|
|
|
|
spi.write(result)
|
|
|
|
spi.readinto(result)
|
|
|
|
value = result[0]
|
|
|
|
return value
|
|
|
|
finally:
|
|
|
|
spi.unlock()
|
|
|
|
cs.value = True
|
|
|
|
|
2021-09-28 17:13:04 -04:00
|
|
|
|
2021-09-27 16:40:36 -04:00
|
|
|
def writeByte(addr, value):
|
|
|
|
while not spi.try_lock():
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
spi.configure(baudrate=500000, phase=0, polarity=0)
|
2021-09-28 17:13:04 -04:00
|
|
|
|
2021-09-27 16:40:36 -04:00
|
|
|
cs.value = False
|
|
|
|
result = bytearray(2)
|
|
|
|
result[0] = addr & ~0x80
|
|
|
|
result[1] = value
|
|
|
|
spi.write(result)
|
|
|
|
finally:
|
|
|
|
spi.unlock()
|
|
|
|
|
2021-09-28 17:13:04 -04:00
|
|
|
|
2021-09-27 16:40:36 -04:00
|
|
|
# put the device in the correct mode to read the ID
|
|
|
|
reg = readByte(BME680_SPI_REGISTER)
|
2021-09-28 17:13:04 -04:00
|
|
|
if (reg & 16) != 0:
|
2021-09-27 16:40:36 -04:00
|
|
|
writeByte(BME680_SPI_REGISTER, reg & ~16)
|
2021-09-28 17:13:04 -04:00
|
|
|
|
2021-09-27 16:40:36 -04:00
|
|
|
id = readByte(BME680_CHIPID_REGISTER)
|
|
|
|
|
|
|
|
print(f"id is {id}, expected {BME680_CHIPID}")
|