diff --git a/src/helpers/sensors/EnvironmentSensorManager.cpp b/src/helpers/sensors/EnvironmentSensorManager.cpp index 3c7c9cd2..13300291 100644 --- a/src/helpers/sensors/EnvironmentSensorManager.cpp +++ b/src/helpers/sensors/EnvironmentSensorManager.cpp @@ -130,10 +130,20 @@ static Adafruit_INA260 INA260; #ifndef TELEM_INA226_ADDRESS #define TELEM_INA226_ADDRESS 0x44 #endif +// CUSTOM (TeTeHacko): overridable -- LaskaKit's INA226 modules carry a 10 mOhm +// shunt (marked R010), not 0.1 Ohm; with the wrong value current reads 10x low. +#ifndef TELEM_INA226_SHUNT_VALUE #define TELEM_INA226_SHUNT_VALUE 0.100 +#endif +#ifndef TELEM_INA226_MAX_AMP #define TELEM_INA226_MAX_AMP 0.8 +#endif #include static INA226 INA226(TELEM_INA226_ADDRESS, TELEM_WIRE); +// CUSTOM (TeTeHacko): INA226 modules ship at 0x40 as often as at 0x44. +#define TELEM_INA226_ALT_ADDR(addr) ((uint8_t)((addr) == 0x40 ? 0x44 : 0x40)) +static class INA226 INA226_ALT(TELEM_INA226_ALT_ADDR(TELEM_INA226_ADDRESS), TELEM_WIRE); +static class INA226* active_ina226 = &INA226; // `class`: the object INA226 hides the type #endif #if ENV_INCLUDE_MLX90614 @@ -189,6 +199,16 @@ static void scanI2CBus(TwoWire* wire, bool found[128]) { // 0 for single-output sensors. // ============================================================ +// Sensors sleep between reads -- CUSTOM (TeTeHacko). Telemetry is read only when +// someone asks, but several drivers leave their chip converting continuously +// after begin(): INA219 ~0.7 mA, INA226/INA260/INA3221 ~0.3-0.35 mA, BMP280 in +// normal mode. Each one is put to sleep after init and runs one triggered (or +// forced) conversion per query; power-down keeps the configuration. On a 0.5 W +// solar node in December a single INA219 was 15-20 % of the budget. The ones +// not touched already sleep on their own (BME280 forced, BME680 performReading, +// SHTC3, SHT4x, AHTX0, LPS22HB one-shot, BMP085). Idea and first patch (INA219): +// Alkel Brostl. + #if ENV_INCLUDE_AHTX0 static uint8_t init_ahtx0(TwoWire* wire, uint8_t addr) { return AHTX0.begin(wire, 0, addr) ? 1 : 0; @@ -242,9 +262,19 @@ static void query_bme280(uint8_t ch, uint8_t, CayenneLPP& lpp) { #if ENV_INCLUDE_BMP280 static uint8_t init_bmp280(TwoWire*, uint8_t addr) { // BMP280 static instance was constructed with TELEM_WIRE; begin() uses it. - return BMP280.begin(addr) ? 1 : 0; + if (!BMP280.begin(addr)) return 0; + // CUSTOM (TeTeHacko): forced mode, see "Sensors sleep between reads" above. + // begin() leaves it in normal mode with 1 ms standby, i.e. measuring all the + // time; same oversampling as begin()'s defaults, so the readings do not change. + BMP280.setSampling(Adafruit_BMP280::MODE_FORCED, + Adafruit_BMP280::SAMPLING_X16, + Adafruit_BMP280::SAMPLING_X16, + Adafruit_BMP280::FILTER_OFF, + Adafruit_BMP280::STANDBY_MS_1); + return 1; } static void query_bmp280(uint8_t ch, uint8_t, CayenneLPP& lpp) { + if (!BMP280.takeForcedMeasurement()) return; lpp.addTemperature(ch, BMP280.readTemperature()); lpp.addBarometricPressure(ch, BMP280.readPressure() / 100); lpp.addAltitude(ch, BMP280.readAltitude(TELEM_BMP280_SEALEVELPRESSURE_HPA)); @@ -303,9 +333,20 @@ static uint8_t init_ina3221(TwoWire* wire, uint8_t addr) { for (int i = 0; i < TELEM_INA3221_NUM_CHANNELS; i++) { if (INA3221.isChannelEnabled(i)) enabled++; } + INA3221.setMode(INA3221_MODE_POWER_DOWN); // sleeps between reads, see above return enabled > 0 ? enabled : 1; } static void query_ina3221(uint8_t ch, uint8_t sub_ch, CayenneLPP& lpp) { + // One triggered pass over every enabled channel (bus + shunt, 1 ms each at + // begin()'s settings), once per channel asked for -- a few ms each, and the + // chip is back in power-down in between. + INA3221.getFlags(); // reading the mask/enable register clears a stale ready flag + INA3221.setMode(INA3221_MODE_SHUNT_BUS_TRIG); + uint32_t start = millis(); + while (!(INA3221.getFlags() & INA3221_CONV_READY)) { + if (millis() - start > 30) { INA3221.setMode(INA3221_MODE_POWER_DOWN); return; } + delay(1); + } // sub_ch is the index of the nth enabled hardware channel. uint8_t seen = 0; for (int i = 0; i < TELEM_INA3221_NUM_CHANNELS; i++) { @@ -316,27 +357,42 @@ static void query_ina3221(uint8_t ch, uint8_t sub_ch, CayenneLPP& lpp) { lpp.addVoltage(ch, v); lpp.addCurrent(ch, c); lpp.addPower(ch, v * c); - return; + break; } seen++; } } + INA3221.setMode(INA3221_MODE_POWER_DOWN); } #endif +#if ENV_INCLUDE_INA219 || ENV_INCLUDE_INA226 +// CUSTOM (TeTeHacko): TI's INA226/INA260/INA3221 answer 0x5449 ("TI") in register +// 0xFE; the INA219 has no such register. Neither Adafruit's INA219::begin() nor +// robtillaart's INA226::begin() checks the chip, so whichever table entry came +// first claimed any device on its address: an INA226 module at 0x40 was read as +// an INA219 (5.05 V bus reported as 2.02 V, current 0 -- bench x4, 12. 11. 2026), +// and the INA226 driver would happily configure an SHT4x at 0x44. +static uint16_t ti_read16(TwoWire* wire, uint8_t addr, uint8_t reg) { + wire->beginTransmission(addr); + wire->write(reg); + if (wire->endTransmission() != 0) return 0xFFFF; + if (wire->requestFrom(addr, (uint8_t)2) != 2) return 0xFFFF; + uint16_t hi = wire->read(); + return (hi << 8) | wire->read(); +} +#define TI_MANUFACTURER_ID 0x5449 +#endif + #if ENV_INCLUDE_INA219 static Adafruit_INA219 INA219_ALT((TELEM_INA219_ADDRESS == 0x40) ? 0x41 : 0x40); static Adafruit_INA219* active_ina219 = &INA219; -// CUSTOM (TeTeHacko): the INA219 sleeps between reads. begin() leaves it in -// continuous conversion, ~0.7 mA typ / 1 mA max (datasheet), for a value that is -// read only when someone asks for telemetry; power-down is ~6 uA and keeps the -// calibration. On a 0.5 W solar node in December that is 15-20 % of the budget. -// Idea and first patch: Alkel Brostl. static uint8_t init_ina219(TwoWire* wire, uint8_t addr) { + if (ti_read16(wire, addr, 0xFE) == TI_MANUFACTURER_ID) return 0; // an INA226/260/3221, not ours active_ina219 = (addr == TELEM_INA219_ADDRESS) ? &INA219 : &INA219_ALT; if (!active_ina219->begin(wire)) return 0; - active_ina219->powerSave(true); + active_ina219->powerSave(true); // sleeps between reads, see above return active_ina219->success() ? 1 : 0; } static void query_ina219(uint8_t ch, uint8_t, CayenneLPP& lpp) { @@ -354,26 +410,51 @@ static void query_ina219(uint8_t ch, uint8_t, CayenneLPP& lpp) { #if ENV_INCLUDE_INA260 static uint8_t init_ina260(TwoWire* wire, uint8_t addr) { - return INA260.begin(addr, wire) ? 1 : 0; + if (!INA260.begin(addr, wire)) return 0; + INA260.setMode(INA260_MODE_SHUTDOWN); // sleeps between reads, see above + return 1; } static void query_ina260(uint8_t ch, uint8_t, CayenneLPP& lpp) { + INA260.setMode(INA260_MODE_TRIGGERED); // one conversion, 2 x 1.1 ms at the defaults + uint32_t start = millis(); + while (!INA260.conversionReady()) { + if (millis() - start > 20) { INA260.setMode(INA260_MODE_SHUTDOWN); return; } + delay(1); + } lpp.addVoltage(ch, INA260.readBusVoltage() / 1000.0f); lpp.addCurrent(ch, INA260.readCurrent() / 1000.0f); lpp.addPower(ch, INA260.readPower() / 1000.0f); + INA260.setMode(INA260_MODE_SHUTDOWN); } #endif #if ENV_INCLUDE_INA226 -static uint8_t init_ina226(TwoWire*, uint8_t) { - // INA226 static instance was constructed with address and wire. - if (!INA226.begin()) return 0; - INA226.setMaxCurrentShunt(TELEM_INA226_MAX_AMP, TELEM_INA226_SHUNT_VALUE); - return 1; +static uint8_t init_ina226(TwoWire*, uint8_t addr) { + // Both static instances were constructed with their address and wire. + active_ina226 = (addr == TELEM_INA226_ADDRESS) ? &INA226 : &INA226_ALT; + class INA226* ina = active_ina226; + if (!ina->begin()) return 0; + // Die ID register: device 0x226 in bits 15-4, silicon revision in 3-0 (x4's + // module reads 0x2261), so compare the device part only. + if (ina->getManufacturerID() != TI_MANUFACTURER_ID || (ina->getDieID() >> 4) != 0x226) return 0; + // The chip keeps its configuration across an MCU reset, and begin() does not + // touch it: whatever a previous firmware wrote stays (an INA219 driver's + // config meant 128 averages, ~670 ms per conversion). Start from the defaults. + if (!ina->reset()) return 0; + ina->setMaxCurrentShunt(TELEM_INA226_MAX_AMP, TELEM_INA226_SHUNT_VALUE); + return ina->shutDown() ? 1 : 0; // sleeps between reads, see above } static void query_ina226(uint8_t ch, uint8_t, CayenneLPP& lpp) { - lpp.addVoltage(ch, INA226.getBusVoltage()); - lpp.addCurrent(ch, INA226.getCurrent_mA() / 1000.0f); - lpp.addPower(ch, INA226.getPower_mW() / 1000.0f); + class INA226* ina = active_ina226; + // one shunt+bus conversion: 2 x 1.1 ms at the defaults + if (!ina->setModeShuntBusTrigger() || !ina->waitConversionReady(20)) { + ina->shutDown(); + return; + } + lpp.addVoltage(ch, ina->getBusVoltage()); + lpp.addCurrent(ch, ina->getCurrent_mA() / 1000.0f); + lpp.addPower(ch, ina->getPower_mW() / 1000.0f); + ina->shutDown(); } #endif @@ -563,6 +644,7 @@ static const SensorDef SENSOR_TABLE[] = { #endif #if ENV_INCLUDE_INA226 { TELEM_INA226_ADDRESS, "INA226", init_ina226, query_ina226 }, + { TELEM_INA226_ALT_ADDR(TELEM_INA226_ADDRESS), "INA226", init_ina226, query_ina226 }, #endif #if ENV_INCLUDE_MLX90614 { TELEM_MLX90614_ADDRESS,"MLX90614", init_mlx90614, query_mlx90614 }, @@ -581,6 +663,9 @@ static const SensorDef SENSOR_TABLE[] = { #undef TELEM_BOSCH_ALT_ADDR #undef TELEM_INA219_ALT_ADDR +#ifdef TELEM_INA226_ALT_ADDR +#undef TELEM_INA226_ALT_ADDR +#endif static const size_t SENSOR_TABLE_SIZE = (sizeof(SENSOR_TABLE) / sizeof(SENSOR_TABLE[0])) - 1; @@ -633,7 +718,7 @@ bool EnvironmentSensorManager::begin() { MESH_DEBUG_PRINTLN("Found %s at address: %02X", def.name, def.address); detected[def.address] = false; // consumed; later entries must not re-claim this device for (uint8_t sub = 0; sub < n && _active_sensor_count < MAX_ACTIVE_SENSORS; sub++) { - _active_sensors[_active_sensor_count++] = { def.query, sub }; + _active_sensors[_active_sensor_count++] = { def.query, sub, def.name, def.address }; } } @@ -669,13 +754,19 @@ int EnvironmentSensorManager::scanI2C(char* dest, size_t max_len) { dest[0] = 0; for (uint8_t addr = 0x08; addr < 0x78; addr++) { if (!found[addr]) continue; + // The driver that claimed the address; else the table's first candidate, marked + // with '?' -- several chips share addresses (0x40: INA219/INA226/INA260). const char* name = NULL; - for (size_t i = 0; i < SENSOR_TABLE_SIZE; i++) { + bool claimed = false; + for (int j = 0; j < _active_sensor_count; j++) { + if (_active_sensors[j].address == addr) { name = _active_sensors[j].name; claimed = true; break; } + } + for (size_t i = 0; !name && i < SENSOR_TABLE_SIZE; i++) { if (SENSOR_TABLE[i].address == addr) { name = SENSOR_TABLE[i].name; break; } } char one[48]; - snprintf(one, sizeof(one), "%s0x%02X%s%s", n ? " " : "", addr, - name ? "=" : "", name ? name : ""); + snprintf(one, sizeof(one), "%s0x%02X%s%s%s", n ? " " : "", addr, + name ? "=" : "", name ? name : "", (name && !claimed) ? "?" : ""); used = appendWhole(dest, used, max_len, one); n++; } diff --git a/src/helpers/sensors/EnvironmentSensorManager.h b/src/helpers/sensors/EnvironmentSensorManager.h index 766ee531..254f0f57 100644 --- a/src/helpers/sensors/EnvironmentSensorManager.h +++ b/src/helpers/sensors/EnvironmentSensorManager.h @@ -13,6 +13,8 @@ protected: struct ActiveSensor { void (*query)(uint8_t channel, uint8_t sub_channel, CayenneLPP& telemetry); uint8_t sub_channel; + const char* name; // CUSTOM (TeTeHacko): the driver that claimed the address, + uint8_t address; // for `i2c` -- the table's first name there can be a different chip }; ActiveSensor _active_sensors[MAX_ACTIVE_SENSORS];