2019-05-06 23:53:29 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-05-06 23:53:29 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-05-11 23:02:10 +01:00
|
|
|
#include <stratosphere.hpp>
|
2020-11-08 12:16:50 +00:00
|
|
|
#include "boot_check_battery.hpp"
|
2019-06-22 08:10:21 +01:00
|
|
|
#include "boot_battery_icons.hpp"
|
|
|
|
#include "boot_boot_reason.hpp"
|
|
|
|
#include "boot_pmic_driver.hpp"
|
2020-11-08 12:16:50 +00:00
|
|
|
#include "boot_battery_driver.hpp"
|
|
|
|
#include "boot_charger_driver.hpp"
|
2019-06-22 08:10:21 +01:00
|
|
|
#include "boot_power_utils.hpp"
|
2019-05-06 23:53:29 +01:00
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::boot {
|
2019-05-07 07:08:28 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
namespace {
|
2019-05-07 07:08:28 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Value definitions. */
|
|
|
|
constexpr inline double BatteryLevelThresholdForBoot = 3.0;
|
|
|
|
constexpr inline double BatteryLevelThresholdForFullCharge = 99.0;
|
|
|
|
|
|
|
|
constexpr inline int BatteryVoltageThresholdConnected = 4000;
|
|
|
|
constexpr inline int BatteryVoltageThresholdDisconnected = 3650;
|
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Types. */
|
|
|
|
enum class CheckBatteryResult {
|
|
|
|
Success,
|
|
|
|
Shutdown,
|
|
|
|
Reboot,
|
|
|
|
};
|
2019-05-07 07:08:28 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
class BatteryChecker {
|
|
|
|
private:
|
2021-10-10 08:14:06 +01:00
|
|
|
boot::ChargerDriver &m_charger_driver;
|
|
|
|
boot::BatteryDriver &m_battery_driver;
|
|
|
|
const powctl::driver::impl::ChargeParameters &m_charge_parameters;
|
|
|
|
powctl::driver::impl::ChargeArbiter m_charge_arbiter;
|
|
|
|
powctl::ChargeCurrentState m_charge_current_state;
|
|
|
|
int m_fast_charge_current_limit;
|
|
|
|
int m_charge_voltage_limit;
|
|
|
|
int m_battery_compensation;
|
|
|
|
int m_voltage_clamp;
|
|
|
|
TimeSpan m_charging_done_interval;
|
|
|
|
bool m_has_start_time;
|
|
|
|
TimeSpan m_start_time;
|
2020-11-08 12:16:50 +00:00
|
|
|
private:
|
|
|
|
bool IsChargeDone();
|
|
|
|
void UpdateChargeDoneCurrent();
|
|
|
|
void ApplyArbiterRule();
|
|
|
|
void PrintBatteryStatus(float raw_charge, int voltage, int voltage_threshold);
|
|
|
|
CheckBatteryResult LoopCheckBattery(bool reboot_on_power_button_press, bool return_on_enough_battery, bool shutdown_on_full_battery, bool show_display, bool show_charging_display);
|
2019-05-07 07:08:28 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
void UpdateStartTime() {
|
|
|
|
/* Update start time. */
|
2021-10-10 08:14:06 +01:00
|
|
|
m_start_time = os::ConvertToTimeSpan(os::GetSystemTick());
|
|
|
|
m_has_start_time = true;
|
2020-11-08 12:16:50 +00:00
|
|
|
}
|
|
|
|
public:
|
2021-10-10 08:14:06 +01:00
|
|
|
BatteryChecker(boot::ChargerDriver &cd, boot::BatteryDriver &bd, const powctl::driver::impl::ChargeParameters &cp, int cvl) : m_charger_driver(cd), m_battery_driver(bd), m_charge_parameters(cp), m_charge_arbiter(cp.rules, cp.num_rules, cvl), m_charging_done_interval(TimeSpan::FromSeconds(2)), m_has_start_time(false) {
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Get parameters from charger. */
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.GetChargeCurrentState(std::addressof(m_charge_current_state)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.GetFastChargeCurrentLimit(std::addressof(m_fast_charge_current_limit)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.GetChargeVoltageLimit(std::addressof(m_charge_voltage_limit)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.GetBatteryCompensation(std::addressof(m_battery_compensation)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.GetVoltageClamp(std::addressof(m_voltage_clamp)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2019-05-07 07:08:28 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Update start time. */
|
|
|
|
this->UpdateStartTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckBatteryResult LoopCheckBattery(spl::BootReason boot_reason) {
|
|
|
|
if (boot_reason == spl::BootReason_RtcAlarm2) {
|
|
|
|
/* RTC Alarm 2 boot (QuasiOff) */
|
|
|
|
return this->LoopCheckBattery(true, false, true, false, false);
|
|
|
|
} else if (boot_reason == spl::BootReason_AcOk) {
|
|
|
|
/* ACOK boot */
|
|
|
|
return this->LoopCheckBattery(true, true, false, true, true);
|
|
|
|
} else {
|
|
|
|
/* Normal boot */
|
|
|
|
return this->LoopCheckBattery(false, true, false, true, false);
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 07:08:28 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
void UpdateCharger();
|
2019-06-22 08:10:21 +01:00
|
|
|
};
|
2019-05-07 07:08:28 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
void BatteryChecker::PrintBatteryStatus(float raw_charge, int voltage, int voltage_threshold) {
|
|
|
|
/* TODO: Print charge/voltage/threshold. */
|
|
|
|
AMS_UNUSED(raw_charge, voltage, voltage_threshold);
|
|
|
|
|
|
|
|
/* Get various battery metrics. */
|
2021-10-16 03:03:11 +01:00
|
|
|
int avg_current, current;
|
|
|
|
float temp, voltage_fuel_gauge_percentage;
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_battery_driver.GetAverageCurrent(std::addressof(avg_current)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_battery_driver.GetCurrent(std::addressof(current)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_battery_driver.GetTemperature(std::addressof(temp)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-10-16 03:03:11 +01:00
|
|
|
if (R_FAILED(m_battery_driver.GetVoltageFuelGaugePercentage(std::addressof(voltage_fuel_gauge_percentage)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
return;
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* TODO: Print the things we just got. */
|
2021-10-16 03:03:11 +01:00
|
|
|
AMS_UNUSED(avg_current, current, temp, voltage_fuel_gauge_percentage);
|
2019-05-07 07:08:28 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
bool BatteryChecker::IsChargeDone() {
|
|
|
|
/* Get the charger status. */
|
|
|
|
boot::ChargerStatus charger_status;
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.GetChargerStatus(std::addressof(charger_status)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2019-05-07 07:08:28 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* If charge status isn't done, we're not done. */
|
|
|
|
if (charger_status != boot::ChargerStatus_ChargeTerminationDone) {
|
|
|
|
return false;
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2019-05-07 07:08:28 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Return whether a done current of zero is acceptable. */
|
2021-10-10 08:14:06 +01:00
|
|
|
return m_charge_arbiter.IsBatteryDoneCurrentAcceptable(0);
|
2020-11-08 12:16:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BatteryChecker::UpdateChargeDoneCurrent() {
|
|
|
|
int done_current = 0;
|
2021-10-10 08:14:06 +01:00
|
|
|
if (m_has_start_time && (os::ConvertToTimeSpan(os::GetSystemTick()) - m_start_time) >= m_charging_done_interval) {
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Get the current. */
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_battery_driver.GetCurrent(std::addressof(done_current)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2020-11-08 12:16:50 +00:00
|
|
|
} else {
|
|
|
|
/* Get the charger status. */
|
|
|
|
boot::ChargerStatus charger_status;
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.GetChargerStatus(std::addressof(charger_status)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2019-05-07 07:08:28 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* If the charger status isn't done, don't update. */
|
|
|
|
if (charger_status != boot::ChargerStatus_ChargeTerminationDone) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Update done current. */
|
2021-10-10 08:14:06 +01:00
|
|
|
m_charge_arbiter.SetBatteryDoneCurrent(done_current);
|
2020-11-08 12:16:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BatteryChecker::UpdateCharger() {
|
|
|
|
/* Get the battery temperature. */
|
|
|
|
float temp;
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_battery_driver.GetTemperature(std::addressof(temp)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
2019-05-07 07:08:28 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Update the temperature level. */
|
|
|
|
powctl::BatteryTemperatureLevel temp_level;
|
2021-10-10 08:14:06 +01:00
|
|
|
if (temp < static_cast<float>(m_charge_parameters.temp_min)) {
|
2020-11-08 12:16:50 +00:00
|
|
|
temp_level = powctl::BatteryTemperatureLevel::TooLow;
|
2021-10-10 08:14:06 +01:00
|
|
|
} else if (temp < static_cast<float>(m_charge_parameters.temp_low)) {
|
2020-11-08 12:16:50 +00:00
|
|
|
temp_level = powctl::BatteryTemperatureLevel::Low;
|
2021-10-10 08:14:06 +01:00
|
|
|
} else if (temp < static_cast<float>(m_charge_parameters.temp_high)) {
|
2020-11-08 12:16:50 +00:00
|
|
|
temp_level = powctl::BatteryTemperatureLevel::Medium;
|
2021-10-10 08:14:06 +01:00
|
|
|
} else if (temp < static_cast<float>(m_charge_parameters.temp_max)) {
|
2020-11-08 12:16:50 +00:00
|
|
|
temp_level = powctl::BatteryTemperatureLevel::High;
|
|
|
|
} else {
|
|
|
|
temp_level = powctl::BatteryTemperatureLevel::TooHigh;
|
2019-05-07 07:08:28 +01:00
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
m_charge_arbiter.SetBatteryTemperatureLevel(temp_level);
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Update average voltage. */
|
|
|
|
int avg_v_cell;
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_battery_driver.GetAverageVCell(std::addressof(avg_v_cell)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
m_charge_arbiter.SetBatteryAverageVCell(avg_v_cell);
|
2020-11-08 12:16:50 +00:00
|
|
|
|
2021-10-16 03:03:11 +01:00
|
|
|
/* Update voltage fuel gauge percentage. */
|
|
|
|
float vfgp;
|
|
|
|
if (R_FAILED(m_battery_driver.GetVoltageFuelGaugePercentage(std::addressof(vfgp)))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
2019-05-07 07:08:28 +01:00
|
|
|
}
|
2021-10-16 03:03:11 +01:00
|
|
|
m_charge_arbiter.SetBatteryVoltageFuelGaugePercentage(vfgp);
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Update charge done current. */
|
|
|
|
this->UpdateChargeDoneCurrent();
|
|
|
|
|
|
|
|
/* Update arbiter power state. */
|
2021-10-10 08:14:06 +01:00
|
|
|
m_charge_arbiter.SetPowerState(powctl::PowerState::ShutdownChargeMain);
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Apply the newly selected rule. */
|
|
|
|
this->ApplyArbiterRule();
|
2019-05-07 07:08:28 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
void BatteryChecker::ApplyArbiterRule() {
|
|
|
|
/* Get the selected rule. */
|
2021-10-10 08:14:06 +01:00
|
|
|
const auto *rule = m_charge_arbiter.GetSelectedRule();
|
2020-11-08 12:16:50 +00:00
|
|
|
AMS_ASSERT(rule != nullptr);
|
|
|
|
|
|
|
|
/* Check if we need to perform charger initialization. */
|
|
|
|
const bool reinit_charger = rule->reinitialize_charger;
|
2021-10-10 08:14:06 +01:00
|
|
|
const auto cur_charge_current_state = m_charge_current_state;
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Set the charger to not charging while we make changes. */
|
|
|
|
if (!reinit_charger || cur_charge_current_state != powctl::ChargeCurrentState_NotCharging) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.SetChargeCurrentState(powctl::ChargeCurrentState_NotCharging))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
m_charge_current_state = powctl::ChargeCurrentState_NotCharging;
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Update start time. */
|
|
|
|
this->UpdateStartTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process fast charge current limit when rule is smaller. */
|
|
|
|
const auto rule_fast_charge_current_limit = rule->fast_charge_current_limit;
|
2021-10-10 08:14:06 +01:00
|
|
|
const auto cur_fast_charge_current_limit = m_fast_charge_current_limit;
|
2020-11-08 12:16:50 +00:00
|
|
|
if (rule_fast_charge_current_limit < cur_fast_charge_current_limit) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.SetFastChargeCurrentLimit(rule_fast_charge_current_limit))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
m_fast_charge_current_limit = rule_fast_charge_current_limit;
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Update start time. */
|
|
|
|
this->UpdateStartTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process charge voltage limit when rule is smaller. */
|
2021-10-10 08:14:06 +01:00
|
|
|
const auto rule_charge_voltage_limit = std::min(rule->charge_voltage_limit, m_charge_arbiter.GetChargeVoltageLimit());
|
|
|
|
const auto cur_charge_voltage_limit = m_charge_voltage_limit;
|
2020-11-08 12:16:50 +00:00
|
|
|
if (rule_charge_voltage_limit < cur_charge_voltage_limit) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.SetChargeVoltageLimit(rule_charge_voltage_limit))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
m_charge_voltage_limit = rule_charge_voltage_limit;
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Update start time. */
|
|
|
|
this->UpdateStartTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process battery compensation when rule is smaller. */
|
|
|
|
const auto rule_battery_compensation = rule->battery_compensation;
|
2021-10-10 08:14:06 +01:00
|
|
|
const auto cur_battery_compensation = m_battery_compensation;
|
2020-11-08 12:16:50 +00:00
|
|
|
if (rule_battery_compensation < cur_battery_compensation) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.SetBatteryCompensation(rule_battery_compensation))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
m_battery_compensation = rule_battery_compensation;
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Update start time. */
|
|
|
|
this->UpdateStartTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process voltage clamp when rule is smaller. */
|
|
|
|
const auto rule_voltage_clamp = rule->voltage_clamp;
|
2021-10-10 08:14:06 +01:00
|
|
|
const auto cur_voltage_clamp = m_voltage_clamp;
|
2020-11-08 12:16:50 +00:00
|
|
|
if (rule_voltage_clamp < cur_voltage_clamp) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.SetVoltageClamp(rule_voltage_clamp))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
m_voltage_clamp = rule_voltage_clamp;
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Update start time. */
|
|
|
|
this->UpdateStartTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process voltage clamp when rule is larger. */
|
|
|
|
if (rule_voltage_clamp > cur_voltage_clamp) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.SetVoltageClamp(rule_voltage_clamp))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
m_voltage_clamp = rule_voltage_clamp;
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Update start time. */
|
|
|
|
this->UpdateStartTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process battery compensation when rule is larger. */
|
|
|
|
if (rule_battery_compensation > cur_battery_compensation) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.SetBatteryCompensation(rule_battery_compensation))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
m_battery_compensation = rule_battery_compensation;
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Update start time. */
|
|
|
|
this->UpdateStartTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process fast charge current limit when rule is larger. */
|
|
|
|
if (rule_fast_charge_current_limit > cur_fast_charge_current_limit) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.SetFastChargeCurrentLimit(rule_fast_charge_current_limit))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
m_fast_charge_current_limit = rule_fast_charge_current_limit;
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Update start time. */
|
|
|
|
this->UpdateStartTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process charge voltage limit when rule is larger. */
|
|
|
|
if (rule_charge_voltage_limit > cur_charge_voltage_limit) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.SetChargeVoltageLimit(rule_charge_voltage_limit))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
m_charge_voltage_limit = rule_charge_voltage_limit;
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Update start time. */
|
|
|
|
this->UpdateStartTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're not charging and we expect to reinitialize the charger, do so. */
|
|
|
|
if (cur_charge_current_state != powctl::ChargeCurrentState_Charging && reinit_charger) {
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_charger_driver.SetChargeCurrentState(powctl::ChargeCurrentState_Charging))) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
2021-10-10 08:14:06 +01:00
|
|
|
m_charge_current_state = powctl::ChargeCurrentState_Charging;
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Update start time. */
|
|
|
|
this->UpdateStartTime();
|
|
|
|
}
|
2019-05-07 07:08:28 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
CheckBatteryResult BatteryChecker::LoopCheckBattery(bool reboot_on_power_button_press, bool return_on_enough_battery, bool shutdown_on_full_battery, bool show_display, bool show_charging_display) {
|
|
|
|
/* Ensure that if we show a charging icon, we stop showing it when we're done. */
|
2019-06-22 08:10:21 +01:00
|
|
|
bool is_showing_charging_icon = false;
|
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
if (is_showing_charging_icon) {
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::EndShowChargingIcon();
|
|
|
|
is_showing_charging_icon = false;
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Show the charging display, if we should. */
|
|
|
|
if (show_charging_display) {
|
|
|
|
/* Get the raw battery charge. */
|
|
|
|
float raw_battery_charge;
|
2021-10-16 00:30:27 +01:00
|
|
|
if (R_FAILED(m_battery_driver.GetChargePercentage(std::addressof(raw_battery_charge)))) {
|
2019-06-22 08:10:21 +01:00
|
|
|
return CheckBatteryResult::Shutdown;
|
|
|
|
}
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Display the battery with the appropriate percentage. */
|
|
|
|
const auto battery_charge = powctl::impl::ConvertBatteryChargePercentage(raw_battery_charge);
|
|
|
|
boot::StartShowChargingIcon(battery_charge);
|
2019-06-22 08:10:21 +01:00
|
|
|
is_showing_charging_icon = true;
|
|
|
|
}
|
2019-05-07 07:08:28 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Loop, checking the battery status. */
|
|
|
|
TimeSpan last_progress_time = TimeSpan(0);
|
2019-06-22 08:10:21 +01:00
|
|
|
while (true) {
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Get the raw battery charge. */
|
|
|
|
float raw_battery_charge;
|
2021-10-16 00:30:27 +01:00
|
|
|
if (R_FAILED(m_battery_driver.GetChargePercentage(std::addressof(raw_battery_charge)))) {
|
2019-06-22 08:10:21 +01:00
|
|
|
return CheckBatteryResult::Shutdown;
|
|
|
|
}
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Get the average vcell. */
|
|
|
|
int battery_voltage;
|
2021-10-10 08:14:06 +01:00
|
|
|
if (R_FAILED(m_battery_driver.GetAverageVCell(std::addressof(battery_voltage)))) {
|
2019-06-22 08:10:21 +01:00
|
|
|
return CheckBatteryResult::Shutdown;
|
|
|
|
}
|
2019-05-06 23:53:29 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Get whether we're connected to charger. */
|
2019-06-22 08:10:21 +01:00
|
|
|
bool ac_ok;
|
2020-11-08 12:16:50 +00:00
|
|
|
if (R_FAILED((boot::PmicDriver().GetAcOk(std::addressof(ac_ok))))) {
|
2019-06-22 08:10:21 +01:00
|
|
|
return CheckBatteryResult::Shutdown;
|
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Decide on a battery voltage threshold. */
|
|
|
|
const auto battery_voltage_threshold = ac_ok ? BatteryVoltageThresholdConnected : BatteryVoltageThresholdDisconnected;
|
|
|
|
|
|
|
|
/* Check if we should return. */
|
|
|
|
if (return_on_enough_battery) {
|
|
|
|
if (raw_battery_charge >= BatteryLevelThresholdForBoot || battery_voltage >= battery_voltage_threshold) {
|
|
|
|
this->PrintBatteryStatus(raw_battery_charge, battery_voltage, battery_voltage_threshold);
|
|
|
|
return CheckBatteryResult::Success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, check if we should shut down. */
|
|
|
|
if (shutdown_on_full_battery) {
|
|
|
|
if (raw_battery_charge >= BatteryLevelThresholdForFullCharge || this->IsChargeDone()) {
|
|
|
|
return CheckBatteryResult::Shutdown;
|
|
|
|
}
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Perform periodic printing. */
|
|
|
|
constexpr TimeSpan PrintProgressInterval = TimeSpan::FromSeconds(10);
|
|
|
|
const auto cur_time = os::ConvertToTimeSpan(os::GetSystemTick());
|
|
|
|
if ((cur_time - last_progress_time) >= PrintProgressInterval) {
|
|
|
|
last_progress_time = cur_time;
|
|
|
|
this->PrintBatteryStatus(raw_battery_charge, battery_voltage, battery_voltage_threshold);
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* If we've gotten to this point, we have insufficient battery to boot. If we aren't charging, show low battery and shutdown. */
|
2019-06-22 08:10:21 +01:00
|
|
|
if (!ac_ok) {
|
2020-11-08 12:16:50 +00:00
|
|
|
this->PrintBatteryStatus(raw_battery_charge, battery_voltage, battery_voltage_threshold);
|
|
|
|
if (show_display && !is_showing_charging_icon) {
|
|
|
|
boot::ShowLowBatteryIcon();
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
|
|
|
return CheckBatteryResult::Shutdown;
|
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Check if we should reboot due to a power button press. */
|
|
|
|
if (reboot_on_power_button_press) {
|
|
|
|
/* Get the power button value. */
|
2019-06-22 08:10:21 +01:00
|
|
|
bool power_button_pressed;
|
2020-11-08 12:16:50 +00:00
|
|
|
if (R_FAILED((boot::PmicDriver().GetPowerButtonPressed(std::addressof(power_button_pressed))))) {
|
2019-06-22 08:10:21 +01:00
|
|
|
return CheckBatteryResult::Shutdown;
|
|
|
|
}
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
/* Handle the press (or not). */
|
2019-06-22 08:10:21 +01:00
|
|
|
if (power_button_pressed) {
|
|
|
|
return CheckBatteryResult::Reboot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* If we got to this point, we should show the low-battery charging screen. */
|
|
|
|
if (show_display && !is_showing_charging_icon) {
|
|
|
|
boot::StartShowLowBatteryChargingIcon();
|
2019-06-22 08:10:21 +01:00
|
|
|
is_showing_charging_icon = true;
|
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Wait a bit before checking again. */
|
|
|
|
constexpr auto BatteryChargeCheckInterval = TimeSpan::FromMilliSeconds(20);
|
|
|
|
os::SleepThread(BatteryChargeCheckInterval);
|
|
|
|
|
|
|
|
/* Update the charger. */
|
|
|
|
this->UpdateCharger();
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2019-05-06 23:53:29 +01:00
|
|
|
}
|
2020-11-08 12:16:50 +00:00
|
|
|
|
2019-05-06 23:53:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
void CheckBatteryCharge() {
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Open a sessions for the charger/battery. */
|
|
|
|
boot::ChargerDriver charger_driver;
|
|
|
|
boot::BatteryDriver battery_driver;
|
2019-05-06 23:53:29 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Check if the battery is removed. */
|
2019-06-22 08:10:21 +01:00
|
|
|
{
|
2020-11-08 12:16:50 +00:00
|
|
|
bool removed = false;
|
|
|
|
if (R_FAILED(battery_driver.IsBatteryRemoved(std::addressof(removed))) || removed) {
|
|
|
|
boot::ShutdownSystem();
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Get the boot reason. */
|
|
|
|
const auto boot_reason = boot::GetBootReason();
|
2019-05-06 23:53:29 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Initialize the charger driver. */
|
|
|
|
if (R_FAILED(charger_driver.Initialize(boot_reason != spl::BootReason_RtcAlarm2)))
|
2019-06-22 08:10:21 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Check that the charger input limit is greater than 150 milli-amps. */
|
|
|
|
{
|
|
|
|
int input_current_limit_ma;
|
|
|
|
if (R_FAILED(charger_driver.GetInputCurrentLimit(std::addressof(input_current_limit_ma)))) {
|
|
|
|
boot::ShutdownSystem();
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2020-11-08 12:16:50 +00:00
|
|
|
|
|
|
|
if (input_current_limit_ma <= 150) {
|
|
|
|
charger_driver.SetChargerConfiguration(powctl::ChargerConfiguration_ChargeDisable);
|
|
|
|
boot::ShutdownSystem();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the charge parameters. */
|
|
|
|
const auto &charge_parameters = powctl::driver::impl::GetChargeParameters();
|
|
|
|
|
|
|
|
/* Get the charge voltage limit. */
|
|
|
|
int charge_voltage_limit_mv;
|
2021-10-16 03:03:11 +01:00
|
|
|
if (boot_reason != spl::BootReason_RtcAlarm2 || charge_parameters.unknown_x_table == nullptr || charge_parameters.x_table_size == 0) {
|
2020-11-08 12:16:50 +00:00
|
|
|
charge_voltage_limit_mv = charge_parameters.default_charge_voltage_limit;
|
2019-05-07 07:08:28 +01:00
|
|
|
} else {
|
2020-11-08 12:16:50 +00:00
|
|
|
if (R_FAILED(charger_driver.GetChargeVoltageLimit(std::addressof(charge_voltage_limit_mv)))) {
|
|
|
|
boot::ShutdownSystem();
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2019-05-07 07:08:28 +01:00
|
|
|
}
|
2019-05-06 23:53:29 +01:00
|
|
|
|
2020-11-08 12:16:50 +00:00
|
|
|
/* Create and update a battery checker. */
|
|
|
|
BatteryChecker battery_checker(charger_driver, battery_driver, charge_parameters, charge_voltage_limit_mv);
|
|
|
|
battery_checker.UpdateCharger();
|
|
|
|
|
|
|
|
/* Set the display brightness to 25%. */
|
|
|
|
boot::SetDisplayBrightness(25);
|
|
|
|
|
|
|
|
/* Check the battery. */
|
|
|
|
const CheckBatteryResult check_result = battery_checker.LoopCheckBattery(boot_reason);
|
|
|
|
|
|
|
|
/* Set the display brightness to 100%. */
|
|
|
|
boot::SetDisplayBrightness(100);
|
|
|
|
|
|
|
|
/* Handle the check result. */
|
|
|
|
switch (check_result) {
|
2019-06-22 08:10:21 +01:00
|
|
|
case CheckBatteryResult::Success:
|
|
|
|
break;
|
|
|
|
case CheckBatteryResult::Shutdown:
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::ShutdownSystem();
|
2019-06-22 08:10:21 +01:00
|
|
|
break;
|
|
|
|
case CheckBatteryResult::Reboot:
|
2020-11-08 12:16:50 +00:00
|
|
|
boot::RebootSystem();
|
2019-06-22 08:10:21 +01:00
|
|
|
break;
|
2019-10-24 10:30:10 +01:00
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
2019-06-22 08:10:21 +01:00
|
|
|
}
|
2019-05-06 23:53:29 +01:00
|
|
|
}
|
2019-06-22 08:10:21 +01:00
|
|
|
|
2019-05-06 23:53:29 +01:00
|
|
|
}
|