@@ 80,6 80,7 @@ err_t i2c_write8(uint8_t i2c_addr, uint8_t reg, uint8_t value)
}
uint8_t i2c_read8(uint8_t i2c_addr, uint8_t reg) {
+ i2c_read_buf[0] = 0;
i2c_write_len = 2;
i2c_read_len = 1;
i2c_write_buf[0] = i2c_addr << 1;
@@ 400,14 401,11 @@ void measure_and_accumulate_current() {
}
int is_plugged_in = 0;
+int wall_power_res = 0;
void read_wall_power_status(void) {
- uint8_t res = i2c_read8(STUSB4500_ADDRESS, 0x11);
- if ((1 << 4) & res) {
- is_plugged_in = 1;
- } else {
- is_plugged_in = 0;
- }
+ wall_power_res = i2c_read8(STUSB4500_ADDRESS, 0x0E);
+ is_plugged_in = wall_power_res & 1;
}
void turn_som_power_on(void) {
@@ 690,21 688,21 @@ void handle_commands() {
else if (remote_cmd == 's') {
// get charger system state
if (state == ST_CHARGE) {
- sprintf(uartBuffer,"normal,ext:%d\n\nlpc fw: "FW_REV"\r",is_plugged_in);
+ sprintf(uartBuffer,"normal,ext:%d\n\nlpc fw: "FW_REV"\r",wall_power_res);
} else if (state == ST_OVERVOLTED) {
- sprintf(uartBuffer,"balancing,ext:%d\n\nlpc fw: "FW_REV"\r",is_plugged_in);
+ sprintf(uartBuffer,"balancing,ext:%d\n\nlpc fw: "FW_REV"\r",wall_power_res);
} else if (state == ST_COOLDOWN) {
- sprintf(uartBuffer,"cooldown,ext:%d\n\nlpc fw: "FW_REV"\r",is_plugged_in);
+ sprintf(uartBuffer,"cooldown,ext:%d\n\nlpc fw: "FW_REV"\r",wall_power_res);
} else if (state == ST_UNDERVOLTED) {
- sprintf(uartBuffer,"undervolted,ext:%d\n\nlpc fw: "FW_REV"\r",is_plugged_in);
+ sprintf(uartBuffer,"undervolted,ext:%d\n\nlpc fw: "FW_REV"\r",wall_power_res);
} else if (state == ST_MISSING) {
sprintf(uartBuffer,"cells missing:%d\n\nlpc fw: "FW_REV"\r",missing_reason);
} else if (state == ST_FULLY_CHARGED) {
- sprintf(uartBuffer,"full charge,ext:%d\n\nlpc fw: "FW_REV"\r",is_plugged_in);
+ sprintf(uartBuffer,"full charge,ext:%d\n\nlpc fw: "FW_REV"\r",wall_power_res);
} else if (state == ST_POWERSAVE) {
- sprintf(uartBuffer,"powersave,ext:%d\n\nlpc fw: "FW_REV"\r",is_plugged_in);
+ sprintf(uartBuffer,"powersave,ext:%d\n\nlpc fw: "FW_REV"\r",wall_power_res);
} else {
- sprintf(uartBuffer,"unknown,ext:%d\n\nlpc fw: "FW_REV"\r",is_plugged_in);
+ sprintf(uartBuffer,"unknown,ext:%d\n\nlpc fw: "FW_REV"\r",wall_power_res);
}
uartSend((uint8_t*)uartBuffer, strlen(uartBuffer));
@@ 1116,19 1114,16 @@ int main(void)
next_state = ST_POWERSAVE;
cycles_in_state = 0;
}
- } else if (num_missing_cells > 0 && is_plugged_in) {
- // Cells can reasonably be 'missing' now, so only go to the missing
- // cells state if there's some missing and we appear to be on wallpower.
- // If we're not on wallpower, then we treat this the same as
- // undervoltage (since that state is a bit more power-concious).
+ } else if (num_missing_cells >= 1) {
if (cycles_in_state > 5) {
missing_reason = num_missing_cells;
// if cells were unplugged, we don't know the capacity anymore.
+ reached_full_charge = 0;
next_state = ST_MISSING;
cycles_in_state = 0;
}
}
- else if (current >= 0 && (num_undervolted_cells > 0 || num_missing_cells > 0)) {
+ else if (num_missing_cells == 0 && current >= 0 && num_undervolted_cells > 0) {
// when transitioning to undervoltage, we assume we reached the bottom
// of usable capacity, so record it, but only if we reached top charge
// once, or our counter will be off.
@@ 1168,9 1163,7 @@ int main(void)
reset_discharge_bits();
deep_sleep_seconds(POWERSAVE_SLEEP_SECONDS);
- if (!is_plugged_in &&
- (num_undervolted_critical_cells >= 1 ||
- num_missing_cells >= 1)) {
+ if (!is_plugged_in && (num_undervolted_critical_cells >= 1 || num_undervolted_cells >= 4)) {
turn_som_power_off();
}
@@ 1186,8 1179,7 @@ int main(void)
next_state = ST_COOLDOWN;
cycles_in_state = 0;
- } else if (cycles_in_state > 5) {
- // don't discharge for more than 5 cycles
+ } else if (cycles_in_state > 10) {
next_state = ST_COOLDOWN;
cycles_in_state = 0;
}
@@ 1196,9 1188,7 @@ int main(void)
// avoid overheating the resistors
reset_discharge_bits();
- // Go a little more aggressively than usual back into charge, since we've
- // got the resistors for it :)
- if (cycles_in_state > 2) {
+ if (cycles_in_state > 5) {
next_state = ST_CHARGE;
cycles_in_state = 0;
}