usermods: delay I2C pin allocation

delay i2C allocation / startup if global I2C pins = -1
This commit is contained in:
Frank
2022-12-18 23:12:38 +01:00
parent ef8da0d3e8
commit 18d649a3a5
3 changed files with 15 additions and 8 deletions

View File

@@ -66,7 +66,8 @@ private:
#define HW_PIN_SCL 5 #define HW_PIN_SCL 5
#define HW_PIN_SDA 4 #define HW_PIN_SDA 4
#endif #endif
int8_t ioPin[2] = {HW_PIN_SCL, HW_PIN_SDA}; // I2C pins: SCL, SDA...defaults to Arch hardware pins but overridden at setup() //int8_t ioPin[2] = {HW_PIN_SCL, HW_PIN_SDA}; // I2C pins: SCL, SDA...defaults to Arch hardware pins but overridden at setup()
int8_t ioPin[2] = {-1, -1}; // WLEDMM - I2C pins: wait until hw pins get allocated
bool initDone = false; bool initDone = false;
bool sensorFound = false; bool sensorFound = false;
@@ -137,7 +138,7 @@ public:
if (!pinManager.allocateMultiplePins(pins, 2, po)) return; if (!pinManager.allocateMultiplePins(pins, 2, po)) return;
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
if (pins[1].pin < 0 || pins[0].pin < 0) { sensorFound=false; return; } //WLEDMM bugfix - ensure that "final" GPIO are valid and no "-1" sneaks trough if (pins[1].pin < 0 || pins[0].pin < 0) { sensorFound=false; enabled=false; return; } //WLEDMM bugfix - ensure that "final" GPIO are valid and no "-1" sneaks trough
Wire.begin(pins[1].pin, pins[0].pin); // WLEDMM this might silently fail, which is OK as it just means that I2C bus is already running. Wire.begin(pins[1].pin, pins[0].pin); // WLEDMM this might silently fail, which is OK as it just means that I2C bus is already running.
#else #else
Wire.begin(); // WLEDMM - i2c pins on 8266 are fixed. Wire.begin(); // WLEDMM - i2c pins on 8266 are fixed.
@@ -181,7 +182,7 @@ public:
mqttInitialized = true; mqttInitialized = true;
} }
mqtt->publish(mqttLuminanceTopic.c_str(), 0, true, String(lux).c_str()); mqtt->publish(mqttLuminanceTopic.c_str(), 0, true, String(lux).c_str());
DEBUG_PRINTLN(F("Brightness: ") + String(lux) + F("lx")); DEBUG_PRINTLN(String("Brightness: ") + String(lux) + String("lx")); // WLEDMM fix compilation warning
} }
else else
{ {

View File

@@ -17,12 +17,12 @@ class RTCUsermod : public Usermod {
void setup() { void setup() {
PinManagerPinType pins[2] = { { i2c_scl, true }, { i2c_sda, true } }; PinManagerPinType pins[2] = { { i2c_scl, true }, { i2c_sda, true } };
#if defined(ARDUINO_ARCH_ESP32) && defined(HW_PIN_SDA) && defined(HW_PIN_SCL) #if defined(ARDUINO_ARCH_ESP32) && defined(HW_PIN_SDA) && defined(HW_PIN_SCL)
if (pins[0].pin < 0) pins[0].pin = HW_PIN_SCL; //WLEDMM //if (pins[0].pin < 0) pins[0].pin = HW_PIN_SCL; //WLEDMM
if (pins[1].pin < 0) pins[1].pin = HW_PIN_SDA; //WLEDMM //if (pins[1].pin < 0) pins[1].pin = HW_PIN_SDA; //WLEDMM
#endif #endif
if (pins[1].pin < 0 || pins[0].pin < 0) { disabled=true; return; } //WLEDMM bugfix - ensure that "final" GPIO are valid and no "-1" sneaks trough
if (!pinManager.allocateMultiplePins(pins, 2, PinOwner::HW_I2C)) { disabled = true; return; } if (!pinManager.allocateMultiplePins(pins, 2, PinOwner::HW_I2C)) { disabled = true; return; }
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
if (pins[1].pin < 0 || pins[0].pin < 0) { disabled=true; return; } //WLEDMM bugfix - ensure that "final" GPIO are valid and no "-1" sneaks trough
Wire.begin(pins[1].pin, pins[0].pin); // WLEDMM this might silently fail, which is OK as it just means that I2C bus is already running. Wire.begin(pins[1].pin, pins[0].pin); // WLEDMM this might silently fail, which is OK as it just means that I2C bus is already running.
#else #else
Wire.begin(); // WLEDMM - i2c pins on 8266 are fixed. Wire.begin(); // WLEDMM - i2c pins on 8266 are fixed.

View File

@@ -117,10 +117,15 @@ class MPU6050Driver : public Usermod {
#endif #endif
void setup() { void setup() {
DEBUG_PRINT_IMULN("mpu setup");
// WLEDMM begin // WLEDMM begin
USER_PRINTLN(F("mpu setup"));
#if 0 // WLEDMM: delay I2C pin alloc
int8_t hw_scl = i2c_scl<0 ? HW_PIN_SCL : i2c_scl; int8_t hw_scl = i2c_scl<0 ? HW_PIN_SCL : i2c_scl;
int8_t hw_sda = i2c_sda<0 ? HW_PIN_SDA : i2c_sda; int8_t hw_sda = i2c_sda<0 ? HW_PIN_SDA : i2c_sda;
#else
int8_t hw_scl = i2c_scl;
int8_t hw_sda = i2c_sda;
#endif
PinManagerPinType pins[2] = { { hw_scl, true }, { hw_sda, true } }; PinManagerPinType pins[2] = { { hw_scl, true }, { hw_sda, true } };
if ((hw_scl < 0) || (hw_sda < 0)) { if ((hw_scl < 0) || (hw_sda < 0)) {
@@ -129,6 +134,8 @@ class MPU6050Driver : public Usermod {
//return; //return;
} }
if (pins[1].pin < 0 || pins[0].pin < 0) { enabled=false; dmpReady = false; return; } //WLEDMM bugfix - ensure that "final" GPIO are valid and no "-1" sneaks trough
if (!pinManager.allocateMultiplePins(pins, 2, PinOwner::HW_I2C)) { if (!pinManager.allocateMultiplePins(pins, 2, PinOwner::HW_I2C)) {
enabled = false; enabled = false;
USER_PRINTF("mpu6050: failed to allocate I2C sda=%d scl=%d\n", hw_sda, hw_scl); USER_PRINTF("mpu6050: failed to allocate I2C sda=%d scl=%d\n", hw_sda, hw_scl);
@@ -139,7 +146,6 @@ class MPU6050Driver : public Usermod {
// join I2C bus (I2Cdev library doesn't do this automatically) // join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
if (pins[1].pin < 0 || pins[0].pin < 0) { enabled=false; dmpReady = false; return; } //WLEDMM bugfix - ensure that "final" GPIO are valid and no "-1" sneaks trough
Wire.begin(pins[1].pin, pins[0].pin); // WLEDMM fix - need to use proper pins, in case that Wire was not started yet. Call will silently fail if Wire is initialized already. Wire.begin(pins[1].pin, pins[0].pin); // WLEDMM fix - need to use proper pins, in case that Wire was not started yet. Call will silently fail if Wire is initialized already.
#else #else
Wire.begin(); // WLEDMM - i2c pins on 8266 are fixed. Wire.begin(); // WLEDMM - i2c pins on 8266 are fixed.