前言
目前android設備越來越多,對于快速充電和長時間待機的需求就不言而喻,對應于此的就是各大手機廠商不斷突破大功率充電新聞頻繁的出現,在個人目前遇到的快充方案中,基本上在大的架構下屬于同一種型別,故分析記錄下來,
基本原理
充電簡單粗暴點來說就是把電流灌到電池里面去,那么最簡單的方法就是直接拿一個電源接在電池的正負極,只要電源電壓高于電池電壓就可以把電流灌進去,就如同直接打開水龍頭開關接水一樣,
但是這樣會存在很多問題,例如:電池此時的電壓很小,電源電壓很高,一懟上電池上的電流就會變得非常大,很可能啥訓電池,所以需要根據電池的電壓來調節輸入電源的電壓,這樣又會出現充一會后就調一下電壓,太麻煩了,因此可以使用計算機來完成這些動作,通過一顆鋰電池充放電芯片來管理充放電的程序,
使用鋰電池充電芯片,整個充電程序大致分成了三個階段:分別是預充、恒流、恒壓,

上圖是各個階段電池電流,電壓的狀態,
但是該方案存在轉化效率較低的問題,特別是在大功率的情況下,損耗太大,因此在手機里不常采用這個方案,不過無論怎樣,基本的充電曲線還是和上圖保持一致的,
基本硬體架構
現在手機充電的基本架構如下圖所示

電源輸入
首先是電源的輸入,目前手機上輸入電源普遍支持有線和無線兩種方式,在該架構下并不是簡單讓電源輸入一個固定電壓完成充電,而在由ap在不同階段調節輸入電源的功率,因此電源的提供需要支持調壓的程序,現在在無線中常使用qi協議,而有線中常使用pd協議,
充電模塊
充電模塊現在主要由main charger(充電芯片)和charger pump(電荷泵芯片)構成,因為charger pump在大電流的情況下效率很高,整個充電程序依舊和上面的充電曲線基本一致,只不過此時的cc階段和前半段的cv階段由charger pump來完成,其余的由main charger來完成
main charger芯片在自身集成了數字邏輯,所以可以自動的切換各個階段,而charger pump則不是這樣,它只是一個模擬器件,我們可以把它理解成一個開關(經過它以后電流升一倍,電壓降一半,譬如輸入10v,5a輸出就是5v,10a)
故在使用程序中通過ap的程式來模擬充電階段,比如我們規定在cc階段下的電流是6a,而此時ap采集到的電流是4a,那我們就通過協議去增大電源的輸入;采集到的電流大于6a則降低電源的輸入,
電量計模塊
電量計模塊主要是獲取電池的電量資訊,然后上層可以獲得電池剩余電量百分比等,
軟體架構
android手機內核采用的是linux內核,android有很多層,單單對于充電功能來說可以把它分為2層,一層是kernel里的驅動和邏輯實作,另一層是上層,android界面上顯示和充電相關的資訊就需要從kernel這一層拿,
在內核中一切皆檔案,那對于充電這部分來說也不例外,上層獲取的資訊都是從/sys/class/power supply路徑獲取的,這個路徑是由代碼決定的(原始碼路徑:/system/core/healthd/BatteryMonitor.cpp)

在充電資訊發生改變的時候kernel就會呼叫uevent,上層就會接收到,然后執行下面的函式更新狀態,
點擊查看代碼
bool BatteryMonitor::update(void) {
bool logthis;
initBatteryProperties(&props);
if (!mHealthdConfig->batteryPresentPath.isEmpty())
props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
else
props.batteryPresent = mBatteryDevicePresent;
props.batteryLevel = mBatteryFixedCapacity ?
mBatteryFixedCapacity :
getIntField(mHealthdConfig->batteryCapacityPath);
props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
if (!mHealthdConfig->batteryFullChargePath.isEmpty())
props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
props.batteryTemperature = mBatteryFixedTemperature ?
mBatteryFixedTemperature :
getIntField(mHealthdConfig->batteryTemperaturePath);
std::string buf;
if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
props.batteryStatus = getBatteryStatus(buf.c_str());
if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
props.batteryHealth = getBatteryHealth(buf.c_str());
if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
props.batteryTechnology = String8(buf.c_str());
unsigned int i;
double MaxPower = 0;
for (i = 0; i < mChargerNames.size(); i++) {
String8 path;
path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
mChargerNames[i].string());
if (getIntField(path)) {
path.clear();
path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
mChargerNames[i].string());
switch(readPowerSupplyType(path)) {
case ANDROID_POWER_SUPPLY_TYPE_AC:
props.chargerAcOnline = true;
break;
case ANDROID_POWER_SUPPLY_TYPE_USB:
props.chargerUsbOnline = true;
break;
case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
props.chargerWirelessOnline = true;
break;
default:
KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
mChargerNames[i].string());
}
path.clear();
path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
mChargerNames[i].string());
int ChargingCurrent =
(access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
path.clear();
path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
mChargerNames[i].string());
int ChargingVoltage =
(access(path.string(), R_OK) == 0) ? getIntField(path) :
DEFAULT_VBUS_VOLTAGE;
double power = ((double)ChargingCurrent / MILLION) *
((double)ChargingVoltage / MILLION);
if (MaxPower < power) {
props.maxChargingCurrent = ChargingCurrent;
props.maxChargingVoltage = ChargingVoltage;
MaxPower = power;
}
}
}
logthis = !healthd_board_battery_update(&props);
if (logthis) {
char dmesgline[256];
size_t len;
if (props.batteryPresent) {
snprintf(dmesgline, sizeof(dmesgline),
"battery l=%d v=%d t=%s%d.%d h=%d st=%d",
props.batteryLevel, props.batteryVoltage,
props.batteryTemperature < 0 ? "-" : "",
abs(props.batteryTemperature / 10),
abs(props.batteryTemperature % 10), props.batteryHealth,
props.batteryStatus);
len = strlen(dmesgline);
if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
" c=%d", props.batteryCurrent);
}
if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
" fc=%d", props.batteryFullCharge);
}
if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
" cc=%d", props.batteryCycleCount);
}
} else {
len = snprintf(dmesgline, sizeof(dmesgline),
"battery none");
}
snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
props.chargerAcOnline ? "a" : "",
props.chargerUsbOnline ? "u" : "",
props.chargerWirelessOnline ? "w" : "");
KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
}
healthd_mode_ops->battery_update(&props);
return props.chargerAcOnline | props.chargerUsbOnline |
props.chargerWirelessOnline;
}
例如

此時我進入到test_usb節點下,type為usb,然后online節點則表示usb連接是否在線,此時為1,則表示usb連接,充電圖示會亮起來,

然后通過命令echo 0 > online ,強制將該值寫為0,充電圖示消失,

故在手機端充電相關的開發基本圍繞著power supply架構展開的,下面依次從kernel到上層應用進行分析,
power supply架構
更新ing
內容持續更新,基本上都是個人的理解,有錯誤可一起討論
個人微信
本文來自博客園,作者:Air-Liu,轉載請注明原文鏈接:https://www.cnblogs.com/ghost-98210/p/15745696.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/397116.html
標籤:嵌入式
上一篇:STC8A,STC8G,STC8H系列的IRC內部振蕩源頻率調節
下一篇:簡易Windows部署服務

