From d9dba2b1825c4e99c94c7587bd9230cc1eccf906 Mon Sep 17 00:00:00 2001 From: Kostas Missos Date: Sun, 1 Jul 2018 04:58:37 +0300 Subject: [PATCH] Fix timing issues with battery desync fix --- ipl/bq24193.c | 26 ++++---------------------- ipl/main.c | 37 +++++++++++++++++++++++++------------ 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/ipl/bq24193.c b/ipl/bq24193.c index f33d41f..23a36f1 100644 --- a/ipl/bq24193.c +++ b/ipl/bq24193.c @@ -132,11 +132,11 @@ int bq24193_get_property(enum BQ24193_reg_prop prop, int *value) data = i2c_recv_byte(I2C_1, BQ24193_I2C_ADDR, BQ24193_FaultReg); *value = data & BQ24193_FAULT_THERM_MASK; break; - case BQ24193_DevID: //Current now. + case BQ24193_DevID: // Dev ID. data = i2c_recv_byte(I2C_1, BQ24193_I2C_ADDR, BQ24193_VendorPart); *value = data & BQ24193_VENDORPART_DEV_MASK; break; - case BQ24193_ProductNumber: //Current avg. + case BQ24193_ProductNumber: // Product number. data = i2c_recv_byte(I2C_1, BQ24193_I2C_ADDR, BQ24193_VendorPart); *value = (data & BQ24193_VENDORPART_PN_MASK) >> 3; break; @@ -146,24 +146,6 @@ int bq24193_get_property(enum BQ24193_reg_prop prop, int *value) return 0; } -static int _bq24193_write_verify_reg(u8 reg, u8 value) -{ - int retries = 8; - int ret; - u8 read_value; - - do { - ret = i2c_send_byte(I2C_1, BQ24193_I2C_ADDR, reg, value); - read_value = i2c_recv_byte(I2C_1, BQ24193_I2C_ADDR, reg); - if (read_value != value) { - ret = -1; - retries--; - } - } while (retries && read_value != value); - - return ret; -} - void bq24193_fake_battery_removal() { u8 value; @@ -171,10 +153,10 @@ void bq24193_fake_battery_removal() // Disable watchdog to keep BATFET disabled. value = i2c_recv_byte(I2C_1, BQ24193_I2C_ADDR, BQ24193_ChrgTermTimer); value &= ~BQ24193_CHRGTERM_WATCHDOG_MASK; - _bq24193_write_verify_reg(BQ24193_ChrgTermTimer, value); + i2c_send_byte(I2C_1, BQ24193_I2C_ADDR, BQ24193_ChrgTermTimer, value); // Force BATFET to disabled state. This disconnects the battery from the system. value = i2c_recv_byte(I2C_1, BQ24193_I2C_ADDR, BQ24193_Misc); value |= BQ24193_MISC_BATFET_DI_MASK; - _bq24193_write_verify_reg(BQ24193_Misc, value); + i2c_send_byte(I2C_1, BQ24193_I2C_ADDR, BQ24193_Misc, value); } diff --git a/ipl/main.c b/ipl/main.c index 0f67590..178badc 100755 --- a/ipl/main.c +++ b/ipl/main.c @@ -1746,8 +1746,9 @@ void fix_battery_desync() gfx_clear_grey(&gfx_ctxt, 0x1B); gfx_con_setpos(&gfx_con, 0, 0); - gfx_printf(&gfx_con, "%kAre you really sure?\nThis will wipe your battery stats completely!\n", 0xFFFFDD00); - gfx_printf(&gfx_con, "\nAdditionally you may need to reconfigure,\nyour time and date settings.\n%k", 0xFFCCCCCC); + gfx_printf(&gfx_con, "%k\nThis will wipe your battery stats completely!\n" + "%kAnd it may not power on without physically\nremoving and re-inserting the battery.\n%k" + "\nAre you really sure?%k\n", 0xFFFFDD00, 0xFFFF0000, 0xFFFFDD00, 0xFFCCCCCC); gfx_puts(&gfx_con, "\nPress POWER to Continue.\nPress VOL to go to the menu.\n\n\n"); u32 btn = btn_wait(); @@ -1755,20 +1756,32 @@ void fix_battery_desync() { gfx_clear_grey(&gfx_ctxt, 0x1B); gfx_con_setpos(&gfx_con, 0, 0); - gfx_printf(&gfx_con, "%kDisconnect the USB cable and wait 30 seconds.\naAfter that press any key!%k\n\n", 0xFFFFDD00, 0xFFCCCCCC); - gfx_printf(&gfx_con, "%k* After this process is done,\n connect the USB cable to power-on.\n\n%k", 0xFF00DDFF, 0xFFCCCCCC); - sleep(500000); - btn_wait(); + gfx_printf(&gfx_con, "%kKeep the USB cable connected!%k\n\n", 0xFFFFDD00, 0xFFCCCCCC); + gfx_con_getpos(&gfx_con, &gfx_con.savedx, &gfx_con.savedy); + + u8 value = 30; + while (value > 0) + { + gfx_con_setpos(&gfx_con, gfx_con.savedx, gfx_con.savedy); + gfx_printf(&gfx_con, "%kWait... (%ds) %k", 0xFF888888, value, 0xFFCCCCCC); + sleep(1000000); + value--; + } + gfx_con_setpos(&gfx_con, gfx_con.savedx, gfx_con.savedy); //Check if still connected. max17050_get_property(MAX17050_AvgCurrent, &avgCurrent); - if (avgCurrent < -100000) - { - bq24193_fake_battery_removal(); - gfx_printf(&gfx_con, "If the device did not powered off,\ndo it from hekate menu"); - } + if ((avgCurrent / 1000) < -10) + EPRINTF("You need to be connected to a wall adapter\nor PC to apply this fix!"); else - EPRINTF("You need to be disconnected from USB,\nto apply this fix!"); + { + // Apply fix. + bq24193_fake_battery_removal(); + gfx_printf(&gfx_con, "Done! \n" + "%k1. Remove the USB cable\n" + "2. Press POWER for 15s.\n" + "3. Reconnect the USB to power-on!%k\n", 0xFFFFDD00, 0xFFCCCCCC); + } sleep(500000); btn_wait(); }