From d0d075a1b41371bff54463b7122683332e980e14 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:11:08 +0200 Subject: [PATCH] Refactored I2C drivers SHT3x and iAQ --- CHANGELOG.md | 2 +- RELEASENOTES.md | 2 +- tasmota/tasmota_xsns_sensor/xsns_14_sht3x.ino | 33 ++++++++++++++++++- tasmota/tasmota_xsns_sensor/xsns_66_iAQ.ino | 6 ++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44b42f1c1..b2ed4dc3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ All notable changes to this project will be documented in this file. ### Breaking Changed ### Changed -- Refactored I2C drivers HTU21, BH1750 and HYT +- Refactored I2C drivers HTU21, BH1750, SHT3x, iAQ and HYT - Add command entered to command error and command unknown message - ESP32 platform update from 2024.08.11 to 2024.09.10 and Framework (Arduino Core) from v3.0.4 to v3.0.5 (#22163) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index b6140d0ba..5da816b04 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -147,7 +147,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl https://ota.tasm - Add command entered to command error and command unknown message - Energy BL09xx command ``CurrentSet`` input changed from Ampere to milliAmpere - Energy force Apparent Power equals Active Power when (Calculated) Apparent Power is less than Active Power [#20653](https://github.com/arendst/Tasmota/issues/20653) -- Refactored I2C drivers HTU21, BH1750 and HYT +- Refactored I2C drivers HTU21, BH1750, SHT3x, iAQ and HYT ### Fixed - Crash when calling TasmotaSerial destructor when initialized with incorrect arguments [#22036](https://github.com/arendst/Tasmota/issues/22036) diff --git a/tasmota/tasmota_xsns_sensor/xsns_14_sht3x.ino b/tasmota/tasmota_xsns_sensor/xsns_14_sht3x.ino index 7aca6e288..c852a3f40 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_14_sht3x.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_14_sht3x.ino @@ -69,7 +69,7 @@ uint8_t Sht3xComputeCrc(uint8_t data[], uint8_t len) { bool Sht3xRead(uint32_t sensor) { if (sht3x_sensors[sensor].valid) { sht3x_sensors[sensor].valid--; } - +/* TwoWire& myWire = I2cGetWire(sht3x_sensors[sensor].bus); if (&myWire == nullptr) { return false; } // No valid I2c bus uint32_t type = sht3x_sensors[sensor].type; @@ -97,12 +97,43 @@ bool Sht3xRead(uint32_t sensor) { if (myWire.endTransmission() != 0) { // Stop I2C transmission return false; } +*/ + uint32_t type = sht3x_sensors[sensor].type; + uint8_t i2c_address = sht3x_sensors[sensor].address; + uint8_t i2c_bus = sht3x_sensors[sensor].bus; + switch (type) { + case SHT3X_TYPE_SHT3X: + // TODO: Clock stretching is used for SHT3x but not for SHTC3. Why? + if (!I2cWrite8(i2c_address, 0x2C, 0x06, i2c_bus)) { // Enable clock stretching / High repeatability measurement + return false; + } + break; + case SHT3X_TYPE_SHTCX: + if (!I2cWrite8(i2c_address, 0x35, 0x17, i2c_bus)) { // Wake from sleep + return false; + } + // TODO: Clock stretching is used for SHT3x but not for SHTC3. Why? + if (!I2cWrite8(i2c_address, 0x78, 0x66, i2c_bus)) { // Disable clock stretching / Normal mode measurement + return false; + } + break; + case SHT3X_TYPE_SHT4X: + if (!I2cWrite0(i2c_address, 0xFD, i2c_bus)) { // High repeatability measurement + return false; + } + break; + } + delay(30); // Timing verified with logic analyzer (10 is too short) uint8_t data[6]; +/* myWire.requestFrom(i2c_address, (uint8_t)6); // Request 6 bytes of data for (uint32_t i = 0; i < 6; i++) { data[i] = myWire.read(); // temperature (MSB, LSB, CRC), humidity (MSB, LSB, CRC) }; +*/ + I2cReadBuffer0(i2c_address, data, 6, i2c_bus); + if ((Sht3xComputeCrc(&data[0], 2) != data[2]) || (Sht3xComputeCrc(&data[3], 2) != data[5])) { return false; } diff --git a/tasmota/tasmota_xsns_sensor/xsns_66_iAQ.ino b/tasmota/tasmota_xsns_sensor/xsns_66_iAQ.ino index 04dab95dd..d330beec1 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_66_iAQ.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_66_iAQ.ino @@ -47,6 +47,7 @@ struct { } iAQ; bool IAQ_Read(void) { +/* TwoWire& myWire = I2cGetWire(iAQ.i2c_bus); if (&myWire == nullptr) { return false; } // No valid I2c bus @@ -56,6 +57,11 @@ bool IAQ_Read(void) { for (uint32_t i = 0; i < 9; i++) { buf[i] = myWire.read(); } +*/ + uint8_t buf[9]; + buf[2] = IAQ_STATUS_I2C_ERR; // populate entry with error code + I2cReadBuffer0(iAQ.i2c_address, buf, sizeof(buf), iAQ.i2c_bus); + // AddLog(LOG_LEVEL_DEBUG, "iAQ: buffer %x %x %x %x %x %x %x %x %x ", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8]); if (IAQ_STATUS_I2C_ERR == buf[2]) { return false;