diff --git a/variants/lilygo_tdeck_pro/TDeckBoard.cpp b/variants/lilygo_tdeck_pro/TDeckBoard.cpp index cf1ddf9..12bfc0a 100644 --- a/variants/lilygo_tdeck_pro/TDeckBoard.cpp +++ b/variants/lilygo_tdeck_pro/TDeckBoard.cpp @@ -6,15 +6,23 @@ uint32_t deviceOnline = 0x00; void TDeckBoard::begin() { - ESP32Board::begin(); - Serial.println("TDeckBoard::begin() - starting"); - // Enable peripheral power (keyboard, sensors, etc.) + // Enable peripheral power (keyboard, sensors, etc.) FIRST + // This powers the BQ27220 fuel gauge and other I2C devices pinMode(PIN_PERF_POWERON, OUTPUT); digitalWrite(PIN_PERF_POWERON, HIGH); + delay(50); // Allow peripherals to power up before I2C init Serial.println("TDeckBoard::begin() - peripheral power enabled"); + // Initialize I2C with correct pins for T-Deck Pro + Wire.begin(I2C_SDA, I2C_SCL); + Wire.setClock(100000); // 100kHz for reliable fuel gauge communication + Serial.println("TDeckBoard::begin() - I2C initialized"); + + // Now call parent class begin (after power and I2C are ready) + ESP32Board::begin(); + // Enable LoRa module power #ifdef P_LORA_EN pinMode(P_LORA_EN, OUTPUT); @@ -41,5 +49,57 @@ void TDeckBoard::begin() { rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1); } + // Test BQ27220 communication + #if HAS_BQ27220 + uint16_t voltage = getBattMilliVolts(); + Serial.print("TDeckBoard::begin() - Battery voltage: "); + Serial.print(voltage); + Serial.println(" mV"); + #endif + Serial.println("TDeckBoard::begin() - complete"); +} + +uint16_t TDeckBoard::getBattMilliVolts() { + #if HAS_BQ27220 + Wire.beginTransmission(BQ27220_I2C_ADDR); + Wire.write(BQ27220_REG_VOLTAGE); + if (Wire.endTransmission(false) != 0) { + Serial.println("BQ27220: I2C error reading voltage"); + return 0; + } + + uint8_t count = Wire.requestFrom((uint8_t)BQ27220_I2C_ADDR, (uint8_t)2); + if (count != 2) { + Serial.println("BQ27220: Read error - wrong byte count"); + return 0; + } + + uint16_t voltage = Wire.read(); + voltage |= (Wire.read() << 8); + return voltage; + #else + return 0; + #endif +} + +uint8_t TDeckBoard::getBatteryPercent() { + #if HAS_BQ27220 + Wire.beginTransmission(BQ27220_I2C_ADDR); + Wire.write(BQ27220_REG_SOC); + if (Wire.endTransmission(false) != 0) { + return 0; + } + + uint8_t count = Wire.requestFrom((uint8_t)BQ27220_I2C_ADDR, (uint8_t)2); + if (count != 2) { + return 0; + } + + uint16_t soc = Wire.read(); + soc |= (Wire.read() << 8); + return (uint8_t)min(soc, (uint16_t)100); + #else + return 0; + #endif } \ No newline at end of file diff --git a/variants/lilygo_tdeck_pro/TDeckBoard.h b/variants/lilygo_tdeck_pro/TDeckBoard.h index ac820e3..980e478 100644 --- a/variants/lilygo_tdeck_pro/TDeckBoard.h +++ b/variants/lilygo_tdeck_pro/TDeckBoard.h @@ -39,50 +39,10 @@ public: } // Read battery voltage from BQ27220 fuel gauge via I2C - // Returns 0 silently if fuel gauge not present/responding - uint16_t getBattMilliVolts() { - #if HAS_BQ27220 - Wire.beginTransmission(BQ27220_I2C_ADDR); - Wire.write(BQ27220_REG_VOLTAGE); - if (Wire.endTransmission(false) != 0) { - return 0; // I2C error - fuel gauge not responding - } - - uint8_t count = Wire.requestFrom((uint8_t)BQ27220_I2C_ADDR, (uint8_t)2); - if (count != 2) { - return 0; // Read error - } - - uint16_t voltage = Wire.read(); - voltage |= (Wire.read() << 8); - return voltage; // Already in mV - #else - return 0; - #endif - } + uint16_t getBattMilliVolts() override; // Read state of charge percentage from BQ27220 - // Returns 0 silently if fuel gauge not present/responding - uint8_t getBatteryPercent() { - #if HAS_BQ27220 - Wire.beginTransmission(BQ27220_I2C_ADDR); - Wire.write(BQ27220_REG_SOC); - if (Wire.endTransmission(false) != 0) { - return 0; - } - - uint8_t count = Wire.requestFrom((uint8_t)BQ27220_I2C_ADDR, (uint8_t)2); - if (count != 2) { - return 0; - } - - uint16_t soc = Wire.read(); - soc |= (Wire.read() << 8); - return (uint8_t)min(soc, (uint16_t)100); - #else - return 0; - #endif - } + uint8_t getBatteryPercent(); const char* getManufacturerName() const { return "LilyGo T-Deck Pro"; diff --git a/variants/lilygo_tdeck_pro/target.cpp b/variants/lilygo_tdeck_pro/target.cpp index 04653ae..e24d424 100644 --- a/variants/lilygo_tdeck_pro/target.cpp +++ b/variants/lilygo_tdeck_pro/target.cpp @@ -32,15 +32,12 @@ bool radio_init() { Serial.println("radio_init() - starting"); // NOTE: board.begin() is called by main.cpp setup() before radio_init() - // So we don't call it here to avoid duplicate initialization + // I2C is already initialized there with correct pins fallback_clock.begin(); Serial.println("radio_init() - fallback_clock started"); - // Initialize I2C with correct pins for T-Deck Pro - Wire.begin(I2C_SDA, I2C_SCL); - Serial.println("radio_init() - I2C initialized"); - + // Wire already initialized in board.begin() - just use it for RTC rtc_clock.begin(Wire); Serial.println("radio_init() - rtc_clock started"); diff --git a/variants/lilygo_tdeck_pro/variant.h b/variants/lilygo_tdeck_pro/variant.h index 36b0877..7853171 100644 --- a/variants/lilygo_tdeck_pro/variant.h +++ b/variants/lilygo_tdeck_pro/variant.h @@ -49,6 +49,10 @@ #define I2C_SDA 13 #define I2C_SCL 14 +// Aliases for ESP32Board base class compatibility +#define PIN_BOARD_SDA I2C_SDA +#define PIN_BOARD_SCL I2C_SCL + // I2C Device Addresses #define I2C_ADDR_TOUCH 0x1A // CST328/CST3530 #define I2C_ADDR_GYROSCOPE 0x28 // BHI260AP