From 6f5de533822359be39e58d5297cd191868e26f54 Mon Sep 17 00:00:00 2001 From: KazushiM <85604869+KazushiMe@users.noreply.github.com> Date: Sun, 15 May 2022 17:48:24 +0800 Subject: [PATCH] [sys-clk-OC] Disable unsafe frequencies by default; Remove emulator-mode; Move fast-charging handler to sysmodule --- README.md | 5 +- Source/sys-clk-OC/README.md | 14 ++-- Source/sys-clk-OC/build.sh | 2 +- .../sys-clk-OC/common/include/sysclk/clocks.h | 5 +- .../sys-clk-OC/common/include/sysclk/config.h | 17 ++++ Source/sys-clk-OC/manager/src/status_tab.cpp | 2 +- .../overlay/src/ui/gui/misc_gui.cpp | 69 ++++++++-------- .../sys-clk-OC/overlay/src/ui/gui/misc_gui.h | 46 +++++++---- .../sysmodule/src/clock_manager.cpp | 81 ++++++++++++------- .../sys-clk-OC/sysmodule/src/clock_manager.h | 60 ++++++++++++++ Source/sys-clk-OC/sysmodule/src/clocks.cpp | 12 ++- Source/sys-clk-OC/sysmodule/src/clocks.h | 4 +- Source/sys-clk-OC/sysmodule/src/config.cpp | 3 +- Source/sys-clk-OC/sysmodule/src/config.h | 2 +- 14 files changed, 222 insertions(+), 100 deletions(-) diff --git a/README.md b/README.md index b0aba0d7..995fa734 100644 --- a/README.md +++ b/README.md @@ -65,10 +65,11 @@ For users in China mainland facing connection or downloading issues on GitHub, g - **CPU/GPU Overclock** - - Safe: CPU/GPU @ 1785/921 MHz (HOS maximum) + - Safe: CPU/GPU @ 1963/921 MHz - It has been proved safe without charger (not reaching battery power draw threshold) - - Unsafe: CPU/GPU @ 2397/1305 MHz + - Unsafe: CPU/GPU up to 2397/1305 MHz + - **Disabled by default**, toggle "Allow Unsafe Frequencies" on in overlay or add `allow_unsafe_freq=1` to `config.ini`
- Without chargers, CPU/GPU would be capped @ 1963/921 MHz or -/460 MHz (Emulator mode). diff --git a/Source/sys-clk-OC/README.md b/Source/sys-clk-OC/README.md index bfe2c563..f6fc2777 100644 --- a/Source/sys-clk-OC/README.md +++ b/Source/sys-clk-OC/README.md @@ -74,11 +74,11 @@ From Hekate Minerva module [sys_sdrammtc.c](https://github.com/CTCaer/hekate/blo To protect the battery from excessive strain, clocks requested from config may be capped before applying, depending on your current profile: -| | Handheld | Handheld (Emulator) | Charging (USB) | Charging (Official) | Docked | -|:-------:|:--------:|:-------------------:|:--------------:|:-------------------:|:------:| -| **MEM** | - | - | - | - | - | -| **CPU** | 1963 | - | - | - | - | -| **GPU** | 921 | 460 | 1267 | - | - | +| | Handheld | Charging (USB) | Charging (Official) | Docked | +|:-------:|:--------:|:--------------:|:-------------------:|:------:| +| **MEM** | - | - | - | - | +| **CPU** | - | - | - | - | +| **GPU** | 921 | 1267 | - | - | ## Installation @@ -180,8 +180,10 @@ The `[values]` section allows you to alter timings in sys-clk, you should not ne | Key | Desc | Default | |:-----------------------:|-------------------------------------------------------------------------------|:-------:| -|**auto_cpu_boost** | Auto-boost CPU when system Core #3 utilization ≥ 95% | ON | +|**allow_unsafe_freq** | Allow unsafe frequencies (CPU > 1963.5 MHz, GPU > 921.6 MHz) | OFF | +|**auto_cpu_boost** | Auto-boost CPU when system Core #3 utilization ≥ 95% | ON | |**sync_reversenx_mode** | Sync nominal profile (mode) with ReverseNX (-Tool and -RT) | ON | +|**disable_fast_charging**| Disable Fast Charging (2000mA -> 500 mA) | OFF | |**temp_log_interval_ms** | Defines how often sys-clk log temperatures, in milliseconds (`0` to disable) | 0 ms | |**csv_write_interval_ms**| Defines how often sys-clk writes to the CSV, in milliseconds (`0` to disable) | 0 ms | |**poll_interval_ms** | Defines how fast sys-clk checks and applies profiles, in milliseconds | 500 ms | diff --git a/Source/sys-clk-OC/build.sh b/Source/sys-clk-OC/build.sh index 7a4929e5..00acbc6a 100755 --- a/Source/sys-clk-OC/build.sh +++ b/Source/sys-clk-OC/build.sh @@ -3,7 +3,7 @@ set -e ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" DIST_DIR="$ROOT_DIR/dist" -CORES=8 +CORES=$(nproc --all) if [[ -n "$1" ]]; then DIST_DIR="$1" diff --git a/Source/sys-clk-OC/common/include/sysclk/clocks.h b/Source/sys-clk-OC/common/include/sysclk/clocks.h index 4b5c0ae3..1b4badcb 100644 --- a/Source/sys-clk-OC/common/include/sysclk/clocks.h +++ b/Source/sys-clk-OC/common/include/sysclk/clocks.h @@ -67,7 +67,7 @@ typedef struct { bool systemCoreBoostCPU; bool gotBoostCPUFreq; - bool handheldEmulatorMode; + bool allowUnsafeFreq; ReverseNXMode reverseNXMode; uint32_t maxMEMFreq; uint32_t boostCPUFreq; @@ -81,8 +81,7 @@ typedef struct }; } SysClkTitleProfileList; -#define SYSCLK_CPU_HANDHELD_MAX_HZ 1963500000U -#define SYSCLK_GPU_HANDHELD_EMULATOR_HZ 460800000U +#define SYSCLK_CPU_SAFE_MAX_HZ 1963500000U #define SYSCLK_GPU_HANDHELD_MAX_HZ 921600000U #define SYSCLK_GPU_CHARGING_USB_MAX_HZ 1267200000U diff --git a/Source/sys-clk-OC/common/include/sysclk/config.h b/Source/sys-clk-OC/common/include/sysclk/config.h index 531f72e2..3552ebd6 100644 --- a/Source/sys-clk-OC/common/include/sysclk/config.h +++ b/Source/sys-clk-OC/common/include/sysclk/config.h @@ -19,6 +19,9 @@ typedef enum { SysClkConfigValue_CsvWriteIntervalMs, SysClkConfigValue_AutoCPUBoost, SysClkConfigValue_SyncReverseNXMode, + SysClkConfigValue_AllowUnsafeFrequencies, + SysClkConfigValue_DisableFastCharging, + SysClkConfigValue_ChargingLimitPercentage, SysClkConfigValue_EnumMax, } SysClkConfigValue; @@ -40,6 +43,12 @@ static inline const char* sysclkFormatConfigValue(SysClkConfigValue val, bool pr return pretty ? "Enable Auto CPU Boost" : "auto_cpu_boost"; case SysClkConfigValue_SyncReverseNXMode: return pretty ? "Enable ReverseNX Mode Sync" : "sync_reversenx_mode"; + case SysClkConfigValue_AllowUnsafeFrequencies: + return pretty ? "Allow Unsafe Frequencies" : "allow_unsafe_freq"; + case SysClkConfigValue_DisableFastCharging: + return pretty ? "Disable Fast Charging" : "disable_fast_charging"; + case SysClkConfigValue_ChargingLimitPercentage: + return pretty ? "Charging Limit (%%)" : "charging_limit_perc"; default: return NULL; } @@ -53,10 +62,14 @@ static inline uint64_t sysclkDefaultConfigValue(SysClkConfigValue val) return 500ULL; case SysClkConfigValue_TempLogIntervalMs: case SysClkConfigValue_CsvWriteIntervalMs: + case SysClkConfigValue_AllowUnsafeFrequencies: + case SysClkConfigValue_DisableFastCharging: return 0ULL; case SysClkConfigValue_AutoCPUBoost: case SysClkConfigValue_SyncReverseNXMode: return 1ULL; + case SysClkConfigValue_ChargingLimitPercentage: + return 100ULL; default: return 0ULL; } @@ -73,7 +86,11 @@ static inline uint64_t sysclkValidConfigValue(SysClkConfigValue val, uint64_t in return true; case SysClkConfigValue_AutoCPUBoost: case SysClkConfigValue_SyncReverseNXMode: + case SysClkConfigValue_AllowUnsafeFrequencies: + case SysClkConfigValue_DisableFastCharging: return (input & 0x1) == input; + case SysClkConfigValue_ChargingLimitPercentage: + return (input <= 100 && input >= 20); default: return false; } diff --git a/Source/sys-clk-OC/manager/src/status_tab.cpp b/Source/sys-clk-OC/manager/src/status_tab.cpp index 46464626..4d39d624 100644 --- a/Source/sys-clk-OC/manager/src/status_tab.cpp +++ b/Source/sys-clk-OC/manager/src/status_tab.cpp @@ -205,7 +205,7 @@ void StatusTab::updateWarningForProfile(SysClkProfile profile, bool animated) case SysClkProfile_Handheld: if (this->warningLabel->isHidden()) this->warningLabel->show([](){}); - this->warningLabel->setText("\uE140 Handheld Mode: Max CPU freq: " + formatFreq(SYSCLK_CPU_HANDHELD_MAX_HZ) + ", Max GPU freq: " + formatFreq(SYSCLK_GPU_HANDHELD_MAX_HZ)); + this->warningLabel->setText("\uE140 Handheld Mode: Max GPU freq: " + formatFreq(SYSCLK_GPU_HANDHELD_MAX_HZ)); break; case SysClkProfile_HandheldChargingUSB: if (this->warningLabel->isHidden()) diff --git a/Source/sys-clk-OC/overlay/src/ui/gui/misc_gui.cpp b/Source/sys-clk-OC/overlay/src/ui/gui/misc_gui.cpp index 277f1dea..3880a49e 100644 --- a/Source/sys-clk-OC/overlay/src/ui/gui/misc_gui.cpp +++ b/Source/sys-clk-OC/overlay/src/ui/gui/misc_gui.cpp @@ -49,46 +49,46 @@ void MiscGui::updateConfigToggle(tsl::elm::ToggleListItem *toggle, SysClkConfigV void MiscGui::listUI() { sysclkIpcGetConfigValues(this->configList); + this->listElement->addItem(new tsl::elm::CategoryHeader("Config")); + this->unsafeFreqToggle = addConfigToggle(SysClkConfigValue_AllowUnsafeFrequencies, "Allow Unsafe Frequencies"); this->cpuBoostToggle = addConfigToggle(SysClkConfigValue_AutoCPUBoost, "Auto CPU Boost"); this->syncModeToggle = addConfigToggle(SysClkConfigValue_SyncReverseNXMode, "Sync ReverseNX Mode"); + this->fastChargingToggle = addConfigToggle(SysClkConfigValue_DisableFastCharging, "Disable Fast Charging"); + + // this->chargingLimitHeader = new tsl::elm::CategoryHeader(""); + // this->listElement->addItem(this->chargingLimitHeader); + // this->chargingLimitBar = new MultiStepTrackBar("", 100 - 20 + 1); + // this->chargingLimitBar->setProgress((this->configList->values[SysClkConfigValue_ChargingLimitPercentage] - 20.0F) * 100 / (100 - 20)); + // this->chargingLimitBar->setValueChangedListener([this](u8 val) { + // this->configList->values[SysClkConfigValue_ChargingLimitPercentage] = val + 20; + + // snprintf(chargingLimitBarDesc, 50, "Battery Charging Limit: %lu%%", this->configList->values[SysClkConfigValue_ChargingLimitPercentage]); + // this->chargingLimitHeader->setText(chargingLimitBarDesc); + // Result rc = sysclkIpcSetConfigValues(this->configList); + // if (R_FAILED(rc)) + // FatalGui::openWithResultCode("sysclkIpcSetConfigValues", rc); + + // this->lastContextUpdate = armGetSystemTick(); + // }); + // this->listElement->addItem(this->chargingLimitBar); + + this->listElement->addItem(new tsl::elm::CategoryHeader("Temporary toggles")); - // Charging this->chargingToggle = new tsl::elm::ToggleListItem("Charging", false); chargingToggle->setStateChangedListener([this](bool state) { - if (PsmChargingToggler(state)) - { - this->chargingToggle->setState(state); - this->fastChargingToggle->setState(this->PsmIsFastCharging()); - } - else - { - this->chargingToggle->setState(!state); - } + PsmChargingToggler(&state); + this->chargingToggle->setState(state); }); this->listElement->addItem(this->chargingToggle); - // FastCharging - this->fastChargingToggle = new tsl::elm::ToggleListItem("Fast Charging", false); - fastChargingToggle->setStateChangedListener([this](bool state) { - if (PsmFastChargingToggler(state)) - { - this->fastChargingToggle->setState(state); - } - else - { - this->fastChargingToggle->setState(!state); - } - }); - this->listElement->addItem(this->fastChargingToggle); - - // Backlight this->backlightToggle = new tsl::elm::ToggleListItem("Screen Backlight", false); backlightToggle->setStateChangedListener([this](bool state) { LblUpdate(true); }); this->listElement->addItem(this->backlightToggle); + this->listElement->addItem(new tsl::elm::CategoryHeader("Battery & Charging Info")); this->listElement->addItem(new tsl::elm::CustomDrawer([this](tsl::gfx::Renderer *renderer, s32 x, s32 y, s32 w, s32 h) { renderer->drawString(this->infoOutput, false, x, y, SMALL_TEXT_SIZE, DESC_COLOR); }), SMALL_TEXT_SIZE * 13); @@ -97,21 +97,24 @@ void MiscGui::listUI() void MiscGui::refresh() { BaseMenuGui::refresh(); - if (this->context) { - sysclkIpcGetConfigValues(this->configList); - updateConfigToggle(this->cpuBoostToggle, SysClkConfigValue_AutoCPUBoost); - updateConfigToggle(this->syncModeToggle, SysClkConfigValue_SyncReverseNXMode); - } - - if (++frameCounter >= 60) + if (this->context && ++frameCounter >= 60) { frameCounter = 0; + sysclkIpcGetConfigValues(this->configList); + updateConfigToggle(this->unsafeFreqToggle, SysClkConfigValue_AllowUnsafeFrequencies); + updateConfigToggle(this->cpuBoostToggle, SysClkConfigValue_AutoCPUBoost); + updateConfigToggle(this->syncModeToggle, SysClkConfigValue_SyncReverseNXMode); + updateConfigToggle(this->fastChargingToggle, SysClkConfigValue_DisableFastCharging); + + // this->chargingLimitBar->setProgress(this->configList->values[SysClkConfigValue_ChargingLimitPercentage] - 20); + // snprintf(chargingLimitBarDesc, 50, "Battery Charging Limit: %u%%", u8(this->configList->values[SysClkConfigValue_ChargingLimitPercentage])); + // this->chargingLimitHeader->setText(chargingLimitBarDesc); + PsmUpdate(); LblUpdate(); this->backlightToggle->setState(lblstatus); I2cGetInfo(this->i2cInfo); PrintInfo(this->infoOutput, sizeof(this->infoOutput)); this->chargingToggle->setState(this->PsmIsCharging()); - this->fastChargingToggle->setState(this->PsmIsFastCharging()); } } diff --git a/Source/sys-clk-OC/overlay/src/ui/gui/misc_gui.h b/Source/sys-clk-OC/overlay/src/ui/gui/misc_gui.h index eaa710d9..65220b3e 100644 --- a/Source/sys-clk-OC/overlay/src/ui/gui/misc_gui.h +++ b/Source/sys-clk-OC/overlay/src/ui/gui/misc_gui.h @@ -13,6 +13,23 @@ #include "base_menu_gui.h" #include +class MultiStepTrackBar : public tsl::elm::StepTrackBar { +public: + MultiStepTrackBar(const char icon[3], size_t numSteps) + : tsl::elm::StepTrackBar(icon, numSteps) { } + + virtual ~MultiStepTrackBar() {} + + virtual inline u8 getProgress() override { + return this->m_value / (100.0F / (this->m_numSteps - 1)); + } + + virtual void setProgress(u8 value) override { + value = std::min(value, u8(this->m_numSteps - 1)); + this->m_value = value * (100.0F / (this->m_numSteps - 1)); + } +}; + class MiscGui : public BaseMenuGui { public: @@ -149,11 +166,6 @@ class MiscGui : public BaseMenuGui return PsmIsChargerConnected() && ((this->chargeInfo->unk_x14 >> 8) & 1); } - bool PsmIsFastCharging() - { - return this->chargeInfo->ChargeCurrentLimit > 768; - } - bool PsmIsEnoughPowerSupplied() { return this->isEnoughPowerSupplied; @@ -289,21 +301,17 @@ class MiscGui : public BaseMenuGui ); } - bool PsmChargingToggler(bool enable) + void PsmChargingToggler(bool* enable) { if (!PsmIsChargerConnected()) - return false; + { + *enable = false; + return; + } - PsmUpdate(enable ? 2 : 3); + PsmUpdate(*enable ? 2 : 3); - return PsmIsCharging() == enable; - } - - bool PsmFastChargingToggler(bool enable) - { - PsmUpdate(enable ? 10 : 11); - - return PsmIsFastCharging() == enable; + *enable = (PsmIsCharging() == *enable); } void LblUpdate(bool shouldSwitch = false) @@ -325,8 +333,12 @@ class MiscGui : public BaseMenuGui tsl::elm::ToggleListItem* addConfigToggle(SysClkConfigValue, std::string); void updateConfigToggle(tsl::elm::ToggleListItem*, SysClkConfigValue); + void updateLiftChargingLimitToggle(); - tsl::elm::ToggleListItem *cpuBoostToggle, *syncModeToggle, *chargingToggle, *fastChargingToggle, *backlightToggle; + tsl::elm::ToggleListItem *unsafeFreqToggle, *cpuBoostToggle, *syncModeToggle, *chargingToggle, *fastChargingToggle, *backlightToggle; + // tsl::elm::CategoryHeader *chargingLimitHeader; + // MultiStepTrackBar *chargingLimitBar; + // char chargingLimitBarDesc[50] = ""; SysClkConfigValueList* configList; ChargeInfo* chargeInfo; diff --git a/Source/sys-clk-OC/sysmodule/src/clock_manager.cpp b/Source/sys-clk-OC/sysmodule/src/clock_manager.cpp index 47d2272e..e057356a 100644 --- a/Source/sys-clk-OC/sysmodule/src/clock_manager.cpp +++ b/Source/sys-clk-OC/sysmodule/src/clock_manager.cpp @@ -60,7 +60,7 @@ ClockManager::ClockManager() this->oc = new SysClkOcExtra; this->oc->systemCoreBoostCPU = false; this->oc->gotBoostCPUFreq = false; - this->oc->handheldEmulatorMode = false; + this->oc->allowUnsafeFreq = false; this->oc->reverseNXMode = ReverseNX_NotFound; this->oc->maxMEMFreq = 0; this->oc->boostCPUFreq = 1785'000'000; @@ -148,7 +148,7 @@ uint32_t ClockManager::GetHz(SysClkModule module) if (hz) { /* Considering realProfile frequency limit */ - hz = Clocks::GetNearestHz(module, this->context->realProfile, hz); + hz = Clocks::GetNearestHz(module, this->context->realProfile, hz, this->oc->allowUnsafeFreq); if (module == SysClkModule_MEM && hz == MAX_MEM_CLOCK) { @@ -165,23 +165,6 @@ uint32_t ClockManager::GetHz(SysClkModule module) } } - /* Handle Handheld Emulator-Mode limit */ - if (this->context->realProfile == SysClkProfile_Handheld) - { - switch (module) - { - case SysClkModule_CPU: - this->oc->handheldEmulatorMode = (hz > SYSCLK_CPU_HANDHELD_MAX_HZ); - break; - case SysClkModule_GPU: - if (this->oc->handheldEmulatorMode) - hz = std::min(hz, SYSCLK_GPU_HANDHELD_EMULATOR_HZ); - break; - default: - break; - } - } - /* Handle CPU Auto Boost, no user-defined hz required */ if (module == SysClkModule_CPU) { @@ -199,7 +182,11 @@ void ClockManager::Tick() { std::scoped_lock lock{this->contextMutex}; - if ((this->RefreshContext() || this->config->Refresh()) && this->context->enabled) + bool setClock = false; + setClock |= this->config->Refresh(); + setClock |= this->RefreshContext(); + setClock &= this->context->enabled; + if (setClock) { for (unsigned int module = 0; module < SysClkModule_EnumMax; module++) { @@ -344,26 +331,64 @@ bool ClockManager::CheckReverseNXRT() { bool shouldAdjustProfile = false; - ReverseNXMode getMode = this->GetConfig()->GetReverseNXRTModeAndClear(); + ReverseNXMode getMode = this->GetConfig()->GetReverseNXRTMode(); if (getMode) { - this->oc->reverseNXMode = getMode; + this->GetConfig()->SetReverseNXRTMode(ReverseNX_GotValue); + this->oc->reverseNXMode = (getMode == ReverseNX_RTResetToDefault) ? + ReverseNX_SystemDefault : getMode; shouldAdjustProfile = true; } - if (getMode == ReverseNX_RTResetToDefault) - { - this->oc->reverseNXMode = ReverseNX_SystemDefault; - } - return shouldAdjustProfile; } +void ClockManager::ChargingHandler() +{ + smInitialize(); + psmInitialize(); + ChargeInfo* chargeInfoField = new ChargeInfo; + Service* session = psmGetServiceSession(); + serviceDispatchOut(session, GetBatteryChargeInfoFields, *(chargeInfoField)); + + bool fastChargingEnabled = chargeInfoField->ChargeCurrentLimit > 768; + bool fastChargingConfig = !(this->GetConfig()->GetConfigValue(SysClkConfigValue_DisableFastCharging)); + if (fastChargingEnabled != fastChargingConfig) + { + serviceDispatch(session, fastChargingConfig ? EnableFastBatteryCharging : DisableFastBatteryCharging); + } + + // bool isChargerConnected = (chargeInfoField->ChargerType != ChargerType_None); + // if (isChargerConnected) + // { + // u32 chargeNow = 0; + + // if (R_SUCCEEDED(psmGetBatteryChargePercentage(&chargeNow))) + // { + // bool isCharging = ((chargeInfoField->unk_x14 >> 8) & 1); + // u32 chargeLimit = this->GetConfig()->GetConfigValue(SysClkConfigValue_ChargingLimitPercentage); + // if (isCharging && chargeLimit < chargeNow) { + // serviceDispatch(session, DisableBatteryCharging); + // } else if (!isCharging && chargeNow < 100 && chargeLimit > chargeNow) { + // serviceDispatch(session, EnableBatteryCharging); + // } + // } + // } + + delete chargeInfoField; + psmExit(); + smExit(); +} + bool ClockManager::RefreshContext() { + ChargingHandler(); + bool hasChanged = false; - bool enabled = this->GetConfig()->Enabled(); bool isReverseNXSyncEnabled = this->GetConfig()->GetConfigValue(SysClkConfigValue_SyncReverseNXMode); + this->oc->allowUnsafeFreq = this->GetConfig()->GetConfigValue(SysClkConfigValue_AllowUnsafeFrequencies); + + bool enabled = this->GetConfig()->Enabled(); if(enabled != this->context->enabled) { this->context->enabled = enabled; diff --git a/Source/sys-clk-OC/sysmodule/src/clock_manager.h b/Source/sys-clk-OC/sysmodule/src/clock_manager.h index 68556265..774b6e25 100644 --- a/Source/sys-clk-OC/sysmodule/src/clock_manager.h +++ b/Source/sys-clk-OC/sysmodule/src/clock_manager.h @@ -32,6 +32,64 @@ class ClockManager SysClkContext GetCurrentContext(); Config* GetConfig(); + typedef enum { + PDCtrler_NewPDO = 1, //Received new Power Data Object + PDCtrler_NoPD = 2, //No Power Delivery source is detected + PDCtrler_AcceptedRDO = 3 //Received and accepted Request Data Object + } ChargeInfoPDCtrler; //BM92T series + + typedef enum { + PowerRole_Sink = 1, + PowerRole_Source = 2 + } ChargeInfoPowerRole; + + typedef enum { + ChargerType_None = 0, + ChargerType_PD = 1, + ChargerType_TypeC_1500mA = 2, + ChargerType_TypeC_3000mA = 3, + ChargerType_DCP = 4, + ChargerType_CDP = 5, + ChargerType_SDP = 6, + ChargerType_Apple_500mA = 7, + ChargerType_Apple_1000mA = 8, + ChargerType_Apple_2000mA = 9 + } ChargeInfoChargerType; + + typedef enum { + Flags_NoHub = BIT(0), //If hub is disconnected + Flags_Rail = BIT(8), //At least one Joy-con is charging from rail + Flags_SPDSRC = BIT(12), //OTG + Flags_ACC = BIT(16) //Accessory + } ChargeInfoFlags; + + typedef struct { + int32_t InputCurrentLimit; //Input (Sink) current limit in mA + int32_t VBUSCurrentLimit; //Output (Source/VBUS/OTG) current limit in mA + int32_t ChargeCurrentLimit; //Battery charging current limit in mA (512mA when Docked, 768mA when BatteryTemperature < 17.0 C) + int32_t ChargeVoltageLimit; //Battery charging voltage limit in mV (3952mV when BatteryTemperature >= 51.0 C) + int32_t unk_x10; //Possibly an emum, getting the same value as PowerRole in all tested cases + int32_t unk_x14; //Possibly flags + ChargeInfoPDCtrler PDCtrlerState; //Power Delivery Controller State + int32_t BatteryTemperature; //Battery temperature in milli C + int32_t RawBatteryCharge; //Raw battery charged capacity per cent-mille (i.e. 100% = 100000 pcm) + int32_t VoltageAvg; //Voltage avg in mV (more in Notes) + int32_t BatteryAge; //Battery age (capacity full / capacity design) per cent-mille (i.e. 100% = 100000 pcm) + ChargeInfoPowerRole PowerRole; + ChargeInfoChargerType ChargerType; + int32_t ChargerVoltageLimit; //Charger and external device voltage limit in mV + int32_t ChargerCurrentLimit; //Charger and external device current limit in mA + ChargeInfoFlags Flags; //Unknown flags + } ChargeInfo; + + typedef enum { + EnableBatteryCharging = 2, + DisableBatteryCharging = 3, + EnableFastBatteryCharging = 10, + DisableFastBatteryCharging = 11, + GetBatteryChargeInfoFields = 17, + } IPsmServerCmd; + protected: ClockManager(); virtual ~ClockManager(); @@ -58,4 +116,6 @@ class ClockManager void CheckReverseNXTool(); bool CheckReverseNXRT(); + void ChargingHandler(); + }; diff --git a/Source/sys-clk-OC/sysmodule/src/clocks.cpp b/Source/sys-clk-OC/sysmodule/src/clocks.cpp index 52b88e25..e66cdba9 100644 --- a/Source/sys-clk-OC/sysmodule/src/clocks.cpp +++ b/Source/sys-clk-OC/sysmodule/src/clocks.cpp @@ -282,10 +282,10 @@ std::uint32_t Clocks::GetCurrentHz(SysClkModule module) return hz; } -std::uint32_t Clocks::GetNearestHz(SysClkModule module, SysClkProfile profile, std::uint32_t inHz) +std::uint32_t Clocks::GetNearestHz(SysClkModule module, SysClkProfile profile, std::uint32_t inHz, bool allowUnsafe) { std::uint32_t hz = GetNearestHz(module, inHz); - std::uint32_t maxHz = GetMaxAllowedHz(module, profile); + std::uint32_t maxHz = GetMaxAllowedHz(module, profile, allowUnsafe); if(maxHz != 0) { @@ -295,11 +295,15 @@ std::uint32_t Clocks::GetNearestHz(SysClkModule module, SysClkProfile profile, s return hz; } -std::uint32_t Clocks::GetMaxAllowedHz(SysClkModule module, SysClkProfile profile) +std::uint32_t Clocks::GetMaxAllowedHz(SysClkModule module, SysClkProfile profile, bool allowUnsafe) { switch (module) { + case SysClkModule_CPU: + if (!allowUnsafe) + return SYSCLK_CPU_SAFE_MAX_HZ; + break; case SysClkModule_GPU: - if (profile == SysClkProfile_Handheld) + if (profile == SysClkProfile_Handheld || !allowUnsafe) return SYSCLK_GPU_HANDHELD_MAX_HZ; if (profile == SysClkProfile_HandheldChargingUSB) return SYSCLK_GPU_CHARGING_USB_MAX_HZ; diff --git a/Source/sys-clk-OC/sysmodule/src/clocks.h b/Source/sys-clk-OC/sysmodule/src/clocks.h index 062ab032..6470f69f 100644 --- a/Source/sys-clk-OC/sysmodule/src/clocks.h +++ b/Source/sys-clk-OC/sysmodule/src/clocks.h @@ -27,7 +27,7 @@ class Clocks static const char* GetProfileName(SysClkProfile profile, bool pretty); static const char* GetModuleName(SysClkModule module, bool pretty); static const char* GetThermalSensorName(SysClkThermalSensor sensor, bool pretty); - static std::uint32_t GetNearestHz(SysClkModule module, SysClkProfile profile, std::uint32_t inHz); + static std::uint32_t GetNearestHz(SysClkModule module, SysClkProfile profile, std::uint32_t inHz, bool allowUnsafe); static std::uint32_t GetTemperatureMilli(SysClkThermalSensor sensor); protected: @@ -36,5 +36,5 @@ class Clocks static PcvModuleId GetPcvModuleId(SysClkModule sysclkModule); static std::uint32_t GetNearestHz(SysClkModule module, std::uint32_t inHz); static void GetList(SysClkModule module, std::uint32_t **outClocks); - static std::uint32_t GetMaxAllowedHz(SysClkModule module, SysClkProfile profile); + static std::uint32_t GetMaxAllowedHz(SysClkModule module, SysClkProfile profile, bool allowUnsafe); }; diff --git a/Source/sys-clk-OC/sysmodule/src/config.cpp b/Source/sys-clk-OC/sysmodule/src/config.cpp index fcb193e9..d77fe7fa 100644 --- a/Source/sys-clk-OC/sysmodule/src/config.cpp +++ b/Source/sys-clk-OC/sysmodule/src/config.cpp @@ -483,10 +483,9 @@ bool Config::SetConfigValues(SysClkConfigValueList* configValues, bool immediate return true; } -ReverseNXMode Config::GetReverseNXRTModeAndClear() { +ReverseNXMode Config::GetReverseNXRTMode() { std::scoped_lock lock{this->reverseNXRTMutex}; ReverseNXMode mode = this->reverseNXRTMode; - this->reverseNXRTMode = ReverseNX_GotValue; return mode; } diff --git a/Source/sys-clk-OC/sysmodule/src/config.h b/Source/sys-clk-OC/sysmodule/src/config.h index bd3be4bb..35360eee 100644 --- a/Source/sys-clk-OC/sysmodule/src/config.h +++ b/Source/sys-clk-OC/sysmodule/src/config.h @@ -47,7 +47,7 @@ class Config const char* GetConfigValueName(SysClkConfigValue val, bool pretty); void GetConfigValues(SysClkConfigValueList* out_configValues); bool SetConfigValues(SysClkConfigValueList* configValues, bool immediate); - ReverseNXMode GetReverseNXRTModeAndClear(); + ReverseNXMode GetReverseNXRTMode(); void SetReverseNXRTMode(ReverseNXMode); protected: void Load();