sysclk: new battery and pmic temp features

This commit is contained in:
souldbminersmwc
2025-12-18 21:41:42 -05:00
parent cce7e09fb7
commit f96580d852
6 changed files with 349 additions and 97 deletions

View File

@@ -33,6 +33,7 @@
#include <algorithm> // for std::clamp
#include <math.h>
#include <numeric>
#include "batLib.h"
#define HOSSVC_HAS_CLKRST (hosversionAtLeast(8,0,0))
#define HOSSVC_HAS_TC (hosversionAtLeast(5,0,0))
@@ -40,8 +41,8 @@
#define systemtickfrequency 19200000
#define systemtickfrequencyF 19200000.0f
#define CPU_TICK_WAIT (1000'000ULL)
#define CPU_TICK_WAIT (1'000'000'000 / 60)
float fanTemp = 0;
Result nvCheck = 1;
Thread gpuLThread;
@@ -49,21 +50,20 @@ Thread cpuCore0Thread;
Thread cpuCore1Thread;
Thread cpuCore2Thread;
Thread cpuCore3Thread;
Thread MISCThread;
FanController fanController;
Result fanCheck = 1;
u8 fanSpeed = 0;
uint32_t GPU_Load_u = 0, fd = 0;
BatteryChargeInfo info;
static SysClkSocType g_socType = SysClkSocType_Erista;
static HorizonOCConsoleType g_consoleType = HorizonOCConsoleType_Unknown;
uint64_t idletick0 = systemtickfrequency;
uint64_t idletick1 = systemtickfrequency;
uint64_t idletick2 = systemtickfrequency;
uint64_t idletick3 = systemtickfrequency;
std::atomic<uint64_t> idletick0{systemtickfrequency};
std::atomic<uint64_t> idletick1{systemtickfrequency};
std::atomic<uint64_t> idletick2{systemtickfrequency};
std::atomic<uint64_t> idletick3{systemtickfrequency};
u32 cpu0, cpu1, cpu2, cpu3, cpuAvg;
const char* Board::GetModuleName(SysClkModule module, bool pretty)
@@ -116,48 +116,16 @@ PcvModuleId Board::GetPcvModuleId(SysClkModule sysclkModule)
return pcvModuleId;
}
void CheckCore0(void*) {
while(true) {
uint64_t idletick_a0 = 0;
uint64_t idletick_b0 = 0;
svcGetInfo(&idletick_b0, InfoType_IdleTickCount, INVALID_HANDLE, 0);
void CheckCore(void* idletick_ptr) {
std::atomic<uint64_t>* idletick = (std::atomic<uint64_t>*)idletick_ptr;
while (true) {
uint64_t idletick_a;
uint64_t idletick_b;
svcGetInfo(&idletick_b, InfoType_IdleTickCount, INVALID_HANDLE, -1);
svcSleepThread(CPU_TICK_WAIT);
svcGetInfo(&idletick_a0, InfoType_IdleTickCount, INVALID_HANDLE, 0);
idletick0 = idletick_a0 - idletick_b0;
}
}
void CheckCore1(void*) {
while(true) {
uint64_t idletick_a1 = 0;
uint64_t idletick_b1 = 0;
svcGetInfo(&idletick_b1, InfoType_IdleTickCount, INVALID_HANDLE, 1);
svcSleepThread(CPU_TICK_WAIT);
svcGetInfo(&idletick_a1, InfoType_IdleTickCount, INVALID_HANDLE, 1);
idletick1 = idletick_a1 - idletick_b1;
}
}
void CheckCore2(void*) {
while(true) {
uint64_t idletick_a2 = 0;
uint64_t idletick_b2 = 0;
svcGetInfo(&idletick_b2, InfoType_IdleTickCount, INVALID_HANDLE, 2);
svcSleepThread(CPU_TICK_WAIT);
svcGetInfo(&idletick_a2, InfoType_IdleTickCount, INVALID_HANDLE, 2);
idletick2 = idletick_a2 - idletick_b2;
}
}
void CheckCore3(void*) {
while(true) {
uint64_t idletick_a3 = 0;
uint64_t idletick_b3 = 0;
svcGetInfo(&idletick_b3, InfoType_IdleTickCount, INVALID_HANDLE, 3);
svcSleepThread(CPU_TICK_WAIT);
svcGetInfo(&idletick_a3, InfoType_IdleTickCount, INVALID_HANDLE, 3);
idletick3 = idletick_a3 - idletick_b3;
}
svcGetInfo(&idletick_a, InfoType_IdleTickCount, INVALID_HANDLE, -1);
idletick->store(idletick_a - idletick_b, std::memory_order_release);
}
}
void gpuLoadThread(void*) {
@@ -174,14 +142,6 @@ void gpuLoadThread(void*) {
} while(true);
}
void miscThread(void*) {
float temp;
for(;;) {
fanControllerGetRotationSpeedLevel(&fanController, &temp);
fanSpeed = (u8)temp;
}
}
void Board::Initialize()
{
@@ -230,22 +190,16 @@ void Board::Initialize()
threadCreate(&gpuLThread, gpuLoadThread, NULL, NULL, 0x1000, 0x3F, -2);
threadStart(&gpuLThread);
threadCreate(&cpuCore0Thread, CheckCore0, NULL, NULL, 0x1000, 0x10, 0);
threadStart(&cpuCore0Thread);
threadCreate(&cpuCore1Thread, CheckCore1, NULL, NULL, 0x1000, 0x10, 1);
threadStart(&cpuCore1Thread);
threadCreate(&cpuCore2Thread, CheckCore2, NULL, NULL, 0x1000, 0x10, 2);
threadStart(&cpuCore2Thread);
threadCreate(&cpuCore3Thread, CheckCore3, NULL, NULL, 0x1000, 0x10, 3);
threadStart(&cpuCore3Thread);
threadCreate(&MISCThread, miscThread, NULL, NULL, 0x1000, 0x3F, -2);
threadStart(&MISCThread);
threadCreate(&cpuCore0Thread, CheckCore, &idletick0, NULL, 0x1000, 0x10, 0);
threadCreate(&cpuCore1Thread, CheckCore, &idletick1, NULL, 0x1000, 0x10, 1);
threadCreate(&cpuCore2Thread, CheckCore, &idletick2, NULL, 0x1000, 0x10, 2);
threadCreate(&cpuCore3Thread, CheckCore, &idletick3, NULL, 0x1000, 0x10, 3);
threadStart(&cpuCore0Thread);
threadStart(&cpuCore1Thread);
threadStart(&cpuCore2Thread);
threadStart(&cpuCore3Thread);
batteryInfoInitialize();
FetchHardwareInfos();
}
@@ -276,9 +230,10 @@ void Board::Exit()
threadClose(&cpuCore1Thread);
threadClose(&cpuCore2Thread);
threadClose(&cpuCore3Thread);
threadClose(&MISCThread);
fanExit();
rgltrExit();
batteryInfoExit();
}
SysClkProfile Board::GetProfile()
@@ -578,6 +533,13 @@ std::uint32_t Board::GetTemperatureMilli(SysClkThermalSensor sensor)
ASSERT_RESULT_OK(rc, "tcGetSkinTemperatureMilliC");
}
}
else if (sensor == HorizonOCThermalSensor_Battery) {
batteryInfoGetChargeInfo(&info);
millis = batteryInfoGetTemperatureMiliCelsius(&info);
}
else if (sensor == HorizonOCThermalSensor_PMIC) {
millis = 50000;
}
else
{
ASSERT_ENUM_VALID(SysClkThermalSensor, sensor);
@@ -612,7 +574,10 @@ std::uint32_t Board::GetPartLoad(SysClkPartLoad loadSource)
case HocClkPartLoad_GPU:
return GPU_Load_u;
case HocClkPartLoad_CPUAvg:
return (idletick0 + idletick1 + idletick2 + idletick3) / 4;
return idletick0;
case HocClkPartLoad_BAT:
batteryInfoGetChargeInfo(&info);
return info.RawBatteryCharge;
default:
ASSERT_ENUM_VALID(SysClkPartLoad, loadSource);
}
@@ -700,7 +665,7 @@ std::uint32_t Board::GetVoltage(HocClkVoltage voltage)
{
RgltrSession session;
Result rc = 0;
u32 out;
u32 out = 0;
switch(voltage)
{
case HocClkVoltage_SOC:
@@ -747,6 +712,10 @@ std::uint32_t Board::GetVoltage(HocClkVoltage voltage)
rgltrGetVoltage(&session, &out);
rgltrCloseSession(&session);
break;
case HocClkVoltage_Battery:
batteryInfoGetChargeInfo(&info);
out = info.VoltageAvg;
break;
default:
ASSERT_ENUM_VALID(HocClkVoltage, voltage);
}
@@ -755,5 +724,6 @@ std::uint32_t Board::GetVoltage(HocClkVoltage voltage)
}
u8 Board::GetFanRotationLevel() {
return fanSpeed;
fanControllerGetRotationSpeedLevel(&fanController, &fanTemp);
return (u8)fanTemp;
}