📄 sbattif.c
字号:
if(fOk && fNewMapping) {
// initialize the memory mapped object
memcpy(&gpStatus->sps, &sps, sizeof(gpStatus->sps));
gpStatus->fSupportsChange = fSupportsChange;
gpStatus->fChanged = FALSE;
gpStatus->wMainLevels = wMainLevels;
gpStatus->wBackupLevels = wBackupLevels;
#ifdef PM_As3603
GetBatteryStatus(gpStatus);
#endif
}
// allow access to the battery buffer
UnlockBattery();
}
// clean up if necessary
if(!fOk) {
if(gpStatus != NULL && gpfnUnmapViewOfFile != NULL) gpfnUnmapViewOfFile(gpStatus);
if(ghFileMap != NULL) CloseHandle(ghFileMap);
if(ghMutex != NULL) CloseHandle(ghMutex);
gpStatus = NULL;
ghFileMap = NULL;
ghMutex = NULL;
}
DEBUGMSG(ZONE_PDD, (_T("%s: returning %d\r\n"), pszFname, fOk));
return fOk;
}
void WINAPI
BatteryPDDDeinitialize(void)
{
SETFNAME(_T("BatteryPDDDeinitialize"));
DEBUGMSG(ZONE_PDD, (_T("%s: invoked\r\n"), pszFname));
if(gpStatus != NULL && gpfnUnmapViewOfFile != NULL) gpfnUnmapViewOfFile(gpStatus);
if(ghFileMap != NULL) CloseHandle(ghFileMap);
if(ghMutex != NULL) CloseHandle(ghMutex);
gpStatus = NULL;
ghFileMap = NULL;
ghMutex = NULL;
#ifdef PM_As3603
PM_DeInitI2C();
if(gPowerKeyFlag > 0)
{
gPowerKeyFlag = 0;
if(ghPowerKeyThread)
{
// wait for the thread to exit
WaitForSingleObject(ghPowerKeyThread, INFINITE);
CloseHandle(ghPowerKeyThread);
}
}
#if 1
DeInitCharge();
#endif
FLA_Deinit();
#endif
}
void WINAPI
BatteryPDDResume(void)
{
SETFNAME(_T("BatteryPDDResume"));
DEBUGMSG(ZONE_PDD, (_T("%s: invoked\r\n"), pszFname));
}
void WINAPI
BatteryPDDPowerHandler(BOOL bOff)
{
SETFNAME(_T("BatteryPDDPowerHandler"));
UNREFERENCED_PARAMETER(bOff);
DEBUGMSG(ZONE_PDD | ZONE_RESUME, (_T("%s: invoked w/ bOff %d\r\n"), pszFname, bOff));
}
// This routine obtains the most current battery/power status available
// on the platform. It fills in the structures pointed to by its parameters
// and returns TRUE if successful. If there's an error, it returns FALSE.
BOOL WINAPI
BatteryPDDGetStatus(
PSYSTEM_POWER_STATUS_EX2 pstatus,
PBOOL pfBatteriesChangedSinceLastCall
)
{
BOOL fOk = TRUE;
SETFNAME(_T("BatteryPDDGetStatus"));
DEBUGMSG(ZONE_PDD, (_T("%s: invoked w/ pstatus 0x%08x, pfChange 0x%08x\r\n"),
pszFname, pstatus, pfBatteriesChangedSinceLastCall));
PREFAST_DEBUGCHK(gpStatus != NULL);
PREFAST_DEBUGCHK(pstatus != NULL);
PREFAST_DEBUGCHK(pfBatteriesChangedSinceLastCall != NULL);
#ifdef PM_As3603
GetBatteryStatus(gpStatus);
#endif
// get battery status information from the shared structure
LockBattery();
memcpy(pstatus, &gpStatus->sps, sizeof(*pstatus));
*pfBatteriesChangedSinceLastCall = gpStatus->fChanged;
if(*pfBatteriesChangedSinceLastCall) {
gpStatus->fChanged = FALSE; // reset changed flag if it's set
}
UnlockBattery();
DEBUGMSG(ZONE_PDD, (_T("%s: returning %d\r\n"), pszFname, fOk));
return (fOk);
}
// This routine indicates how many battery levels will be reported
// in the BatteryFlag and BackupBatteryFlag fields of the PSYSTEM_POWER_STATUS_EX2
// filed in by BatteryPDDGetStatus(). This number ranges from 0 through 3 --
// see the Platform Builder documentation for details. The main battery
// level count is reported in the low word of the return value; the count
// for the backup battery is in the high word.
LONG
BatteryPDDGetLevels(
void
)
{
LONG lLevels;
SETFNAME(_T("BatteryPDDPowerHandler"));
PREFAST_DEBUGCHK(gpStatus != NULL);
LockBattery();
lLevels = MAKELONG (gpStatus->wMainLevels, gpStatus->wBackupLevels);
UnlockBattery();
DEBUGMSG(ZONE_PDD, (_T("%s: returning %u (%d main levels, %d backup levels)\r\n"),
pszFname, lLevels, LOWORD(lLevels), HIWORD(lLevels)));
return lLevels;
}
// This routine returns TRUE to indicate that the pfBatteriesChangedSinceLastCall
// value filled in by BatteryPDDGetStatus() is valid. If there is no way to
// tell that the platform's batteries have been changed this routine should
// return FALSE.
BOOL
BatteryPDDSupportsChangeNotification(
void
)
{
BOOL fSupportsChange;
SETFNAME(_T("BatteryPDDPowerHandler"));
PREFAST_DEBUGCHK(gpStatus != NULL);
LockBattery();
fSupportsChange = gpStatus->fSupportsChange;
UnlockBattery();
DEBUGMSG(ZONE_PDD, (_T("%s: returning %d\r\n"), pszFname, fSupportsChange));
return fSupportsChange;
}
#ifdef PM_As3603
void GetBatteryStatus(PBATTERY_STATUS pStatus)
{
DWORD iTime;
int iAvgc;
#ifdef DEBUG_DISCHARGE
static DWORD s_time = 0;
SYSTEMTIME sysTime;
if(s_time == 0)
{
GetSystemTime(&sysTime);
s_time = sysTime.wHour * 3600 + sysTime.wMinute * 60 + sysTime.wSecond;
}
#endif
RETAILMSG(SHOW_DEBUG, (_T("%s in\r\n"), _T(__FUNCTION__)));
if(!((pStatus->sps.BatteryFlag) & BATTERY_FLAG_NO_BATTERY) && PM_NoBattery())
{
RETAILMSG(SHOW_DEBUG, (_T("PM_NoBattery\r\n")));
pStatus->sps.BatteryFlag = BATTERY_FLAG_NO_BATTERY;
RETAILMSG(SHOW_DEBUG, (_T("%s BATTERY_FLAG_NO_BATTERY\r\n"), _T(__FUNCTION__)));
return;
}
if(!((pStatus->sps.BatteryFlag) & BATTERY_FLAG_CHARGING) && PM_IsCharging())//charge
{
RETAILMSG(SHOW_DEBUG, (_T("PM_IsCharging\r\n")));
//usb供电不足,需要电池辅助
PM_ChargeStart();
pStatus->sps.ACLineStatus = AC_LINE_ONLINE;
pStatus->sps.BatteryFlag |= BATTERY_FLAG_CHARGING;
pStatus->fSupportsChange = FALSE;
}
else if((pStatus->sps.ACLineStatus) != AC_LINE_OFFLINE && !PM_ChargerDetected())//discharge
{
RETAILMSG(SHOW_DEBUG, (_T("Not PM_IsCharging\r\n")));
//usb供电不足,需要电池辅助
PM_ChargeStart();
if((pStatus->sps.BatteryLifePercent) >= 50)
{
pStatus->sps.BatteryFlag = BATTERY_FLAG_HIGH;
}
else
{
pStatus->sps.BatteryFlag = BATTERY_FLAG_LOW;
}
pStatus->sps.ACLineStatus = AC_LINE_OFFLINE;
pStatus->fSupportsChange = TRUE;
}
if(PM_ChargeUpdate(&iAvgc,&g_currentQ,&iTime))
{
RETAILMSG(SHOW_DEBUG, (_T("g_currentQ:%d\r\n"),g_currentQ));
iAvgc = 400;//mA
if(iAvgc > 0)
{
pStatus->sps.BatteryLifePercent = (UCHAR)(g_currentQ * 100 / FULLQ);//%
pStatus->sps.BatteryLifeTime = (100-(pStatus->sps.BatteryLifePercent))*(FULLQ/1000) * 36 / ((DWORD)iAvgc);//s
pStatus->sps.BatteryFullLifeTime = FULLQ * 36 / ((DWORD)iAvgc*10);//s
}
if((pStatus->sps.BatteryLifePercent) >= 50)
{
pStatus->sps.BatteryFlag = BATTERY_FLAG_HIGH;
}
else
{
pStatus->sps.BatteryFlag = BATTERY_FLAG_LOW;
}
if(PM_IsCharging())
{
pStatus->sps.BatteryFlag |= BATTERY_FLAG_CHARGING;
}
pStatus->sps.BatteryAverageCurrent = iAvgc;//mA
RETAILMSG(SHOW_DEBUG, (_T("pStatus->sps.BatteryAverageCurrent:%dmA\r\n"),iAvgc));
}
else
{
RETAILMSG(SHOW_DEBUG, (_T("g_currentQ not update!\r\n")));
}
if(g_currentQ == FULLQ)
{
RETAILMSG(SHOW_DEBUG, (_T("PM_ChargeIsFull\r\n")));
pStatus->sps.BatteryLifePercent = 100;
pStatus->sps.BatteryFlag = BATTERY_FLAG_HIGH;
pStatus->sps.ACLineStatus = AC_LINE_ONLINE;
}
//battery low warn
//battery protect , power off
if(!PM_ChargerDetected() && g_currentQ < TOO_LOWQ && (pStatus->sps.BatteryFlag != BATTERY_FLAG_CRITICAL))
{
RETAILMSG(SHOW_DEBUG, (_T("PM_ChargeIsEmpty\r\n")));
pStatus->sps.BatteryFlag = BATTERY_FLAG_CRITICAL;
ghLowQAccThread = CreateThread(NULL, 0, LowQAccThreadProc, NULL, 0, NULL);
#ifdef DEBUG_DISCHARGE
GetSystemTime(&sysTime);
s_time = sysTime.wHour * 3600 + sysTime.wMinute * 60 + sysTime.wSecond - s_time;
{
char str[100];
FILE* fp;
fp = fopen("\\NandFlash\\discharge_time.txt","wt");
if(fp)
{
sprintf(str,"discharge_time:%ds",s_time);
fwrite(str,1,strlen(str),fp);
fclose(fp);
}
}
#endif
}
RETAILMSG(SHOW_DEBUG, (_T("%s out\r\n"), _T(__FUNCTION__)));
}
int WINAPI PowerKeyThreadProc(LPVOID pvParam)
{
SYSTEMTIME sysTime;
DWORD clock = 0;
HANDLE hClockThread = NULL;
volatile PGPIO_REGS gpio8_1_base = NULL;
RETAILMSG(SHOW_DEBUG, (_T("Enter PowerKeyThreadProc\r\n")));
ASSERT(g_PowerKeyEvent != NULL);
gpio8_1_base = (PGPIO_REGS)(hw_map_io(GPIO8_BASE, sizeof(GPIO_REGS), 0));
RETAILMSG(SHOW_DEBUG, (_T("PowerKeyThreadProc:gpio8_1_base address:%x\r\n"),(DWORD)gpio8_1_base));
if(!gpio8_1_base)
{
return 0;
}
gpio8_1_base->dwGPIODir &= ~0x02;
gpio8_1_base->dwGPIOIS &= ~0x02;
gpio8_1_base->dwGPIOIBE |= 0x02;
gpio8_1_base->dwGPIOIC |= 0x02;
gpio8_1_base->dwGPIOIE |= 0x02;
while (gPowerKeyFlag)
{
DWORD dwStatus;
// wait gpio8-1 interrupt
RETAILMSG(SHOW_DEBUG, (_T("PowerKeyThreadProc:gPowerKeyEvent:%x\r\n"),gPowerKeyEvent));
dwStatus = WaitForSingleObject(gPowerKeyEvent, INFINITE);
RETAILMSG(SHOW_DEBUG, (_T("PowerKeyThreadProc:gPowerKeyEvent\r\n")));
gpio8_1_base->dwGPIOIC |= 0x02;//清中断
if(dwStatus == WAIT_OBJECT_0)
{
RETAILMSG(SHOW_DEBUG, (_T("PowerKeyThreadProc:WAIT_OBJECT_0\r\n")));
Sleep(50);
if((gpio8_1_base->Padding1[254] & 0x02) == 0x02)//上升沿
{
RETAILMSG(SHOW_DEBUG, (_T("PowerKeyThreadProc:rising edge\r\n")));
//usb是否插上
if(!PM_ChargerDetected())
{
if(clock == 0)
{
GetSystemTime(&sysTime);
clock = sysTime.wSecond * 1000 + sysTime.wMilliseconds;
RETAILMSG(SHOW_DEBUG, (_T("PowerKeyThreadProc:first clock:%d\r\n"),clock));
gClockFlag = 1;
hClockThread = CreateThread(NULL, 0, ClockThreadProc, &clock, 0, NULL);
}
}
else
{
MessageBox( NULL, POWER_OFF_USB,POWER_OFF_TITLE, MB_OK|MB_ICONWARNING);
}
}
else//下降沿
{
RETAILMSG(SHOW_DEBUG, (_T("PowerKeyThreadProc:falling edge\r\n")));
clock = 0;
if(gClockFlag > 0)
{
gClockFlag = 0;
if(hClockThread)
{
// wait for the thread to exit
WaitForSingleObject(hClockThread, INFINITE);
CloseHandle(hClockThread);
hClockThread = NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -