Skip to content

Commit 14cc8ab

Browse files
authored
Merge pull request #6 from taradiddles/master
correct temperature readings for defective ICs + misc. improvements
2 parents 6e9c9c0 + 59631d7 commit 14cc8ab

File tree

3 files changed

+114
-12
lines changed

3 files changed

+114
-12
lines changed

README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ Usage Example
7373
print("")
7474
time.sleep(1.0)
7575
76+
Caveat: by default the library initializes the IC with constant temperature and pressure measurements at 64Hz with 64 samples. It is not possible to change the IC's mode, temperature_oversample_count or pressure_oversample_count on-the-fly so resetting the IC and operation parameteres is required. For instance, to set the mode to continuous pressure measurement at 1Hz with 2 samples:
77+
78+
.. code-block:: python3
79+
80+
dps310.reset()
81+
dps310.pressure_oversample_count = adafruit_dps310.SampleCount.COUNT_2
82+
dps310.pressure_rate = adafruit_dps310.Rate.RATE_1_HZ
83+
dps310.mode = adafruit_dps310.Mode.CONT_PRESSURE
84+
dps310.wait_pressure_ready()
85+
7686
7787
Contributing
7888
============

adafruit_dps310.py

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ class DPS310:
203203

204204
_calib_coeff_temp_src_bit = ROBit(_DPS310_TMPCOEFSRCE, 7)
205205

206+
_reg0e = RWBits(8, 0x0E, 0)
207+
_reg0f = RWBits(8, 0x0F, 0)
208+
_reg62 = RWBits(8, 0x62, 0)
209+
206210
def __init__(self, i2c_bus, address=_DPS310_DEFAULT_ADDRESS):
207211
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
208212

@@ -234,13 +238,9 @@ def __init__(self, i2c_bus, address=_DPS310_DEFAULT_ADDRESS):
234238
self.initialize()
235239

236240
def initialize(self):
237-
"""Reset the sensor to the default state"""
238-
239-
self._reset()
240-
self._read_calibration()
241+
"""Initialize the sensor to continuous measurement"""
241242

242-
# make sure we're using the temperature source used for calibration
243-
self._temp_measurement_src_bit = self._calib_coeff_temp_src_bit
243+
self.reset()
244244

245245
self.pressure_rate = Rate.RATE_64_HZ
246246
self.pressure_oversample_count = SampleCount.COUNT_64
@@ -249,17 +249,35 @@ def initialize(self):
249249
self.mode = Mode.CONT_PRESTEMP
250250

251251
# wait until we have at least one good measurement
252-
253-
while (self._temp_ready is False) or (self._pressure_ready is False):
254-
sleep(0.001)
255-
256-
def _reset(self):
257-
"""Perform a soft-reset on the sensor"""
252+
self.wait_temperature_ready()
253+
self.wait_pressure_ready()
254+
255+
# (https://github.com/Infineon/DPS310-Pressure-Sensor#temperature-measurement-issue)
256+
# similar to DpsClass::correctTemp(void) from infineon's c++ library
257+
def _correct_temp(self):
258+
"""Correct temperature readings on ICs with a fuse bit problem"""
259+
self._reg0e = 0xA5
260+
self._reg0f = 0x96
261+
self._reg62 = 0x02
262+
self._reg0e = 0
263+
self._reg0f = 0
264+
265+
# perform a temperature measurement
266+
# the most recent temperature will be saved internally
267+
# and used for compensation when calculating pressure
268+
_unused = self._raw_temperature
269+
270+
def reset(self):
271+
"""Reset the sensor"""
258272
self._reset_register = 0x89
259273
# wait for hardware reset to finish
260274
sleep(0.010)
261275
while not self._sensor_ready:
262276
sleep(0.001)
277+
self._correct_temp()
278+
self._read_calibration()
279+
# make sure we're using the temperature source used for calibration
280+
self._temp_measurement_src_bit = self._calib_coeff_temp_src_bit
263281

264282
@property
265283
def pressure(self):
@@ -296,11 +314,51 @@ def temperature_ready(self):
296314
"""Returns true if there is a temperature reading ready"""
297315
return self._temp_ready
298316

317+
def wait_temperature_ready(self):
318+
"""Wait until a temperature measurement is available.
319+
320+
To avoid waiting indefinitely this function raises an
321+
error if the sensor isn't configured for temperate measurements,
322+
ie. ``Mode.ONE_TEMPERATURE``, ``Mode.CONT_TEMP`` or ``Mode.CONT_PRESTEMP``.
323+
See the `Mode` documentation for details.
324+
"""
325+
if (
326+
self._mode_bits == Mode.IDLE
327+
or self._mode_bits == Mode.ONE_PRESSURE
328+
or self._mode_bits == Mode.CONT_PRESSURE
329+
):
330+
raise RuntimeError(
331+
"Sensor mode is set to idle or pressure measurement,\
332+
can't wait for a temperature measurement"
333+
)
334+
while self._temp_ready is False:
335+
sleep(0.001)
336+
299337
@property
300338
def pressure_ready(self):
301339
"""Returns true if pressure readings are ready"""
302340
return self._pressure_ready
303341

342+
def wait_pressure_ready(self):
343+
"""Wait until a pressure measurement is available
344+
345+
To avoid waiting indefinitely this function raises an
346+
error if the sensor isn't configured for pressure measurements,
347+
ie. ``Mode.ONE_PRESSURE``, ``Mode.CONT_PRESSURE`` or ``Mode.CONT_PRESTEMP``
348+
See the `Mode` documentation for details.
349+
"""
350+
if (
351+
self._mode_bits == Mode.IDLE
352+
or self._mode_bits == Mode.ONE_TEMPERATURE
353+
or self._mode_bits == Mode.CONT_TEMP
354+
):
355+
raise RuntimeError(
356+
"Sensor mode is set to idle or temperature measurement,\
357+
can't wait for a pressure measurement"
358+
)
359+
while self._pressure_ready is False:
360+
sleep(0.001)
361+
304362
@property
305363
def mode(self):
306364
"""The measurement mode. Must be a `Mode`. See the `Mode` documentation for details"""
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Configure the sensor for continuous measurement with rates,
3+
sampling counts and mode optmized for low power, as recommended
4+
in Infineon's datasheet:
5+
https://www.infineon.com/dgdl/Infineon-DPS310-DS-v01_00-EN.pdf
6+
"""
7+
8+
# (disable pylint warnings for adafruit_dps310.{SampleCount,Rate,Mode}.*
9+
# as they are generated dynamically)
10+
# pylint: disable=no-member
11+
12+
import time
13+
import board
14+
import busio
15+
import adafruit_dps310
16+
17+
i2c = busio.I2C(board.SCL, board.SDA)
18+
19+
dps310 = adafruit_dps310.DPS310(i2c)
20+
21+
dps310.reset()
22+
dps310.pressure_oversample_count = adafruit_dps310.SampleCount.COUNT_2
23+
dps310.pressure_rate = adafruit_dps310.Rate.RATE_1_HZ
24+
dps310.temperature_oversample_count = adafruit_dps310.SampleCount.COUNT_16
25+
dps310.temperature_rate = adafruit_dps310.Rate.RATE_1_HZ
26+
dps310.mode = adafruit_dps310.Mode.CONT_PRESTEMP
27+
dps310.wait_temperature_ready()
28+
dps310.wait_pressure_ready()
29+
30+
while True:
31+
print("Temperature = %.2f *C" % dps310.temperature)
32+
print("Pressure = %.2f hPa" % dps310.pressure)
33+
print("")
34+
time.sleep(10.0)

0 commit comments

Comments
 (0)