sht3x: reads should be retried with at least 0.5s pause

SHT3x would return a read NACK on host retries.
When the MCU receives the I2C CMD, it reads out data.
SHT3x clears the data buffer.
The MCU fails to deliver a response to the host.
The host retries, the device returns NACK,
then the MCU goes into the shutdown state.

Make sure there is at least 0.5s between retries.

Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>
This commit is contained in:
Timofey Titovets 2025-07-03 21:50:35 +02:00 committed by KevinOConnor
parent 37ddab223f
commit 2585accfeb
2 changed files with 17 additions and 3 deletions

View File

@ -217,8 +217,8 @@ class MCU_I2C:
def i2c_write_wait_ack(self, data, minclock=0, reqclock=0):
self.i2c_write_cmd.send_wait_ack([self.oid, data],
minclock=minclock, reqclock=reqclock)
def i2c_read(self, write, read_len):
return self.i2c_read_cmd.send([self.oid, write, read_len])
def i2c_read(self, write, read_len, retry=True):
return self.i2c_read_cmd.send([self.oid, write, read_len], retry)
def MCU_I2C_from_config(config, default_addr=None, default_speed=100000):
# Load bus parameters

View File

@ -56,6 +56,7 @@ class SHT3X:
self.reactor = self.printer.get_reactor()
self.i2c = bus.MCU_I2C_from_config(
config, default_addr=SHT3X_I2C_ADDR, default_speed=100000)
self._error = self.i2c.get_mcu().error
self.report_time = config.getint('sht3x_report_time', 1, minval=1)
self.deviceId = config.get('sensor_type')
self.temp = self.min_temp = self.max_temp = self.humidity = 0.
@ -105,7 +106,20 @@ class SHT3X:
def _sample_sht3x(self, eventtime):
try:
# Read measurment
params = self.i2c.i2c_read(SHT3X_CMD['OTHER']['FETCH'], 6)
retries = 5
params = None
error = None
while retries > 0 and params is None:
try:
params = self.i2c.i2c_read(
SHT3X_CMD['OTHER']['FETCH'], 6, retry=False
)
except self._error as e:
error = e
self.reactor.pause(self.reactor.monotonic() + .5)
retries -= 1
if params is None:
raise error
response = bytearray(params['response'])
rtemp = response[0] << 8