Fix wire1
This commit is contained in:
1 parent
a5fad9573a
commit
9717ef5549
3 files changed
+78
-9
No files matched your search
Binary file not shown.
@@ -45,14 +45,17 @@ bool CultivarEnv::begin(TwoWire &wire, uint8_t sda, uint8_t scl, uint8_t bme_add
|
||||
_scl = scl;
|
||||
_bme_addr = bme_addr;
|
||||
|
||||
#ifdef ARDUINO_ARCH_ZEPHYR
|
||||
_wire->begin();
|
||||
#else
|
||||
#ifndef ARDUINO_ARCH_ZEPHYR
|
||||
_wire->begin(_sda, _scl);
|
||||
_wire->setClock(100000);
|
||||
#endif
|
||||
_wire->setClock(100000); // 100 kHz — begin() resets clock; re-apply before init
|
||||
|
||||
bme_ok = _initBME690();
|
||||
|
||||
#ifdef ARDUINO_ARCH_ZEPHYR
|
||||
_wire->begin(); // SCD4x Sensirion lib uses Wire — init after BME690 native i2c_transfer calls
|
||||
#endif
|
||||
|
||||
scd4x_ok = _initSCD4x();
|
||||
|
||||
return bme_ok;
|
||||
@@ -62,6 +65,13 @@ bool CultivarEnv::begin(TwoWire &wire, uint8_t sda, uint8_t scl, uint8_t bme_add
|
||||
bool CultivarEnv::_initBME690() {
|
||||
_i2c_handle.wire = _wire;
|
||||
_i2c_handle.addr = _bme_addr;
|
||||
#ifdef ARDUINO_ARCH_ZEPHYR
|
||||
_i2c_handle.i2c_dev = DEVICE_DT_GET(DT_NODELABEL(i2c4)); // Wire1 = i2c4 (0x40008400) on UNO Q
|
||||
if (!device_is_ready(_i2c_handle.i2c_dev)) {
|
||||
CULTIVAR_SERIAL_PRINTF("[cultivar_env] Zephyr i2c4 (Wire1) device not ready\n");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
_dev.intf = BME69X_I2C_INTF;
|
||||
_dev.read = _i2cRead;
|
||||
@@ -70,8 +80,20 @@ bool CultivarEnv::_initBME690() {
|
||||
_dev.intf_ptr = &_i2c_handle;
|
||||
_dev.amb_temp = 25;
|
||||
|
||||
#ifdef ARDUINO_ARCH_ZEPHYR
|
||||
// STM32 doesn't power-cycle on reset — soft-reset the sensor so
|
||||
// bme69x_init reads calibration from a clean state.
|
||||
int8_t sr = bme69x_soft_reset(&_dev);
|
||||
CULTIVAR_SERIAL_PRINTF("[cultivar_env] soft_reset=%d\n", sr);
|
||||
_dev.delay_us(BME69X_PERIOD_RESET, _dev.intf_ptr); // 10ms reset recovery per datasheet
|
||||
#endif
|
||||
|
||||
bme_status = bme69x_init(&_dev);
|
||||
if (bme_status != BME69X_OK) return false;
|
||||
if (bme_status != BME69X_OK) {
|
||||
CULTIVAR_SERIAL_PRINTF("[cultivar_env] bme69x_init failed: status=%d intf_rslt=%d\n",
|
||||
bme_status, (int)_dev.intf_rslt);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_dev.variant_id != BME690_VARIANT_GAS_HIGH) {
|
||||
CULTIVAR_SERIAL_PRINTF("[cultivar_env] Warning: variant_id=0x%02X (expected 0x02 for BME690)\n",
|
||||
@@ -116,23 +138,30 @@ bool CultivarEnv::_readAmbient(CultivarAmbient &out) {
|
||||
out.valid = false;
|
||||
if (!bme_ok) return false;
|
||||
|
||||
struct bme69x_conf saved_conf = _conf; // save — restore after ambient read
|
||||
|
||||
_conf.os_temp = BME69X_OS_8X;
|
||||
_conf.os_pres = BME69X_OS_16X;
|
||||
_conf.os_hum = BME69X_OS_4X;
|
||||
_conf.filter = BME69X_FILTER_SIZE_7;
|
||||
int8_t r = bme69x_set_conf(&_conf, &_dev);
|
||||
if (r != BME69X_OK) return false;
|
||||
if (r != BME69X_OK) { _conf = saved_conf; return false; }
|
||||
|
||||
_heatr.enable = BME69X_DISABLE;
|
||||
_heatr.heatr_temp = 0;
|
||||
_heatr.heatr_dur = 0;
|
||||
r = bme69x_set_heatr_conf(BME69X_FORCED_MODE, &_heatr, &_dev);
|
||||
if (r != BME69X_OK) return false;
|
||||
if (r != BME69X_OK) { _conf = saved_conf; return false; }
|
||||
|
||||
delay(CULTIVAR_AMBIENT_SETTLE_MS);
|
||||
|
||||
struct bme69x_data data;
|
||||
if (!_forcedRead(data)) return false;
|
||||
bool ok = _forcedRead(data);
|
||||
|
||||
_conf = saved_conf; // restore sweep config
|
||||
bme69x_set_conf(&_conf, &_dev);
|
||||
|
||||
if (!ok) return false;
|
||||
|
||||
out.temperature = data.temperature;
|
||||
out.humidity = data.humidity;
|
||||
@@ -395,24 +424,60 @@ void CultivarEnv::scanI2C() {
|
||||
int8_t CultivarEnv::_i2cRead(uint8_t reg_addr, uint8_t *data,
|
||||
uint32_t len, void *intf_ptr) {
|
||||
CultivarI2CHandle *h = (CultivarI2CHandle *)intf_ptr;
|
||||
#ifdef ARDUINO_ARCH_ZEPHYR
|
||||
// Zephyr Wire always sends STOP between endTransmission and requestFrom —
|
||||
// stopBit param is ARG_UNUSED. BME690 requires write+read as a single
|
||||
// atomic transaction (repeated start). Use i2c_transfer with two msgs.
|
||||
struct i2c_msg msgs[2];
|
||||
msgs[0].buf = ®_addr;
|
||||
msgs[0].len = 1;
|
||||
msgs[0].flags = I2C_MSG_WRITE; // no STOP
|
||||
msgs[1].buf = data;
|
||||
msgs[1].len = (uint32_t)len;
|
||||
msgs[1].flags = I2C_MSG_RESTART | I2C_MSG_READ | I2C_MSG_STOP; // repeated start + read + STOP
|
||||
int rc = i2c_transfer(h->i2c_dev, msgs, 2, h->addr);
|
||||
if (rc != 0) {
|
||||
CULTIVAR_SERIAL_PRINTF("[cultivar_env] i2c_transfer read reg=0x%02X len=%u rc=%d\n", reg_addr, len, rc);
|
||||
}
|
||||
return (rc == 0) ? BME69X_OK : -1;
|
||||
#else
|
||||
h->wire->beginTransmission(h->addr);
|
||||
h->wire->write(reg_addr);
|
||||
if (h->wire->endTransmission(true) != 0) return -1; // true=STOP; Zephyr Wire may not support repeated starts
|
||||
if (h->wire->endTransmission(false) != 0) return -1;
|
||||
h->wire->requestFrom(h->addr, (uint8_t)len);
|
||||
for (uint32_t i = 0; i < len; i++) {
|
||||
if (!h->wire->available()) return -1;
|
||||
data[i] = h->wire->read();
|
||||
}
|
||||
return BME69X_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
int8_t CultivarEnv::_i2cWrite(uint8_t reg_addr, const uint8_t *data,
|
||||
uint32_t len, void *intf_ptr) {
|
||||
CultivarI2CHandle *h = (CultivarI2CHandle *)intf_ptr;
|
||||
#ifdef ARDUINO_ARCH_ZEPHYR
|
||||
// Use i2c_transfer for consistency — Wire.endTransmission uses i2c_write
|
||||
// internally anyway, but this avoids the ring buffer overhead.
|
||||
if (len + 1 > 32) return -1; // Bosch driver never writes more than ~17 bytes
|
||||
uint8_t buf[32];
|
||||
buf[0] = reg_addr;
|
||||
memcpy(buf + 1, data, len);
|
||||
struct i2c_msg msg;
|
||||
msg.buf = buf;
|
||||
msg.len = len + 1;
|
||||
msg.flags = I2C_MSG_WRITE | I2C_MSG_STOP;
|
||||
int rc = i2c_transfer(h->i2c_dev, &msg, 1, h->addr);
|
||||
if (rc != 0) {
|
||||
CULTIVAR_SERIAL_PRINTF("[cultivar_env] i2c_transfer write reg=0x%02X len=%u rc=%d\n", reg_addr, len + 1, rc);
|
||||
}
|
||||
return (rc == 0) ? BME69X_OK : -1;
|
||||
#else
|
||||
h->wire->beginTransmission(h->addr);
|
||||
h->wire->write(reg_addr);
|
||||
for (uint32_t i = 0; i < len; i++) h->wire->write(data[i]);
|
||||
return (h->wire->endTransmission() == 0) ? BME69X_OK : -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
void CultivarEnv::_delayUs(uint32_t period, void *intf_ptr) {
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#define CULTIVAR_ENV_SDA 0
|
||||
#define CULTIVAR_ENV_SCL 0
|
||||
#define CULTIVAR_ENV_WIRE Wire1
|
||||
#include <zephyr/drivers/i2c.h>
|
||||
#else
|
||||
#define CULTIVAR_SERIAL_PRINTF(fmt, ...) Serial.printf(fmt, ##__VA_ARGS__)
|
||||
// ── Default I2C pins (ESP32S3 dev board — override for production) ─────────
|
||||
@@ -146,6 +147,9 @@ struct CultivarEnvPayload {
|
||||
struct CultivarI2CHandle {
|
||||
TwoWire *wire;
|
||||
uint8_t addr;
|
||||
#ifdef ARDUINO_ARCH_ZEPHYR
|
||||
const struct device *i2c_dev; // Zephyr native I2C device for atomic write-read
|
||||
#endif
|
||||
};
|
||||
|
||||
// ── Main library class ────────────────────────────────────────────────────
|
||||
|
||||
Reference in new issue
Block a user