Powered By Blogger
Showing posts with label power manager. Show all posts
Showing posts with label power manager. Show all posts
Sunday, June 7, 2009

Keeping system alive

In power manager, the PDD has a certain set of events that it monitors. For eg:

  • PM_BOOTPHASE2_EVENT
  • PM_SYSTEM_API_EVENT etc
For more information on events each PDD handles, check the Power manager code available in PUBLIC code (WINCE600\PUBLIC\COMMON\OAK\DRIVERS\PM\PDD).

In those events there is event PM_SYSTEM_TIMEIDLE_RESET (PowerManager/SystemIdleTimerReset), which can used to reset the system activity timer. Check out the following piece of code for doing so.


HANDLE hSysTmrReset = CreateEvent(NULL, FALSE, FALSE, _T("PowerManager/SystemIdleTimerReset"));
BOOL bExit = FALSE;
if (INVALID_HANDLE_VALUE != hSysTmrReset )
{
while (FALSE == bExit)
{
ResetEvent(hSysTmrReset);
Sleep(1000);
SetEvent(hSysTmrReset);

RETAILMSG(TRUE, (TEXT("Reset\r\n")));
Sleep(20000);
}
CloseHandle (hSysTmrReset );
}
return 0;

This system, code will keep the system alive by resetting the timer every 20 seconds. I have tested this code and seems to be working. But needs to analyze the system performance on doing so.

Tuesday, September 23, 2008

Querying that powerstate of an NIC

This one is a continuation of the last post, Here is a code for chcking the current power state of an NIC
// Returns TRUE if device is enabled otherwise FALSE
BOOL QueryNIC (wchar_t* InterfaceName)
{
TCHAR                           szName[MAX_PATH];
int                             nChars;
TCHAR*                          pszAdapter;
TCHAR                           multiSz[257];
DWORD                           cbBuffer = sizeof(multiSz);
CEDEVICE_POWER_STATE Dx = PwrDeviceUnspecified;    
BOOL bDevPowered = TRUE;

pszAdapter = new TCHAR[wcslen(InterfaceName)+1];
wcscpy_s(pszAdapter,wcslen(InterfaceName)+1,InterfaceName);
nChars = _sntprintf_s(szName,
                                                    MAX_PATH-1,
                                                   MAX_PATH-1,
                                                  _T("%s\\%s"),
                                                                  PMCLASS_NDIS_MINIPORT, 
                                                                   pszAdapter);
szName[MAX_PATH-1]=0;

if(nChars != (-1)) 
::GetDevicePower(szName, POWER_NAME, &Dx);

if (!IsValidDx(Dx))
{
bDevPowered = FALSE;
}
if (D4 == Dx)
{
bDevPowered = FALSE;
}
delete pszAdapter;
return bDevPowered ;
}

i have to thank one person (seriously I forgot this name... i am really bad a remembering names and numbers), he posted an article in codeproject.com regarding this functionality. Then I found more helpful code in completing the circle.. :) 
WINCE600\PUBLIC\COMMON\OAK\DRIVERS\NETSAMP\NDISPWR
This is a sample program given in windows ce 6.0 showing how to query and the set the power state of network interface using deviceIocontrols (specifically,  ndis ioctls for binding and unbinding the adapter after a power state change) and setDevicePower functions. 

Here is the code for controlling (enabling and disabling, its the same operation as in enabling and disabling a network interface in network connection applet in control panel) an NIC.

Pay attention to the high lighted part of code.

void ControlNIC(wchar_t* InterfaceName,BOOL bEnable)
{
HANDLE                          hNdisPwr;
NDISPWR_SAVEPOWERSTATE SavePowerState;
TCHAR                           szName[MAX_PATH];
int                             nChars;
TCHAR*                          pszAdapter;
TCHAR                           multiSz[257];
DWORD                           cbBuffer = sizeof(multiSz);

hNdisPwr = CreateFile((PTCHAR)NDISPWR_DEVICE_NAME,
                                                          0x00,0x00,NULL,
                                                         OPEN_EXISTING,
                                                       FILE_ATTRIBUTE_NORMAL
                                                                | FILE_FLAG_OVERLAPPED,
                                                        (HANDLE)INVALID_HANDLE_VALUE);

if (hNdisPwr != INVALID_HANDLE_VALUE)
{
SavePowerState.pwcAdapterName = InterfaceName;
if (bEnable)
{
SavePowerState.CePowerState   = PwrDeviceUnspecified;
}
else
{
SavePowerState.CePowerState   = D4;
}

BOOL ret = DeviceIoControl(hNdisPwr,
                                                    IOCTL_NPW_SAVE_POWER_STATE,
                                                    &SavePowerState,
                                                    sizeof(NDISPWR_SAVEPOWERSTATE),
                                                    NULL,0x00,NULL,NULL);
// TODO: take care of the return value 'ret '

CloseHandle(hNdisPwr);

                // Just composing a Class-Qualified Device Name, to pass on to SetDevicePower
pszAdapter = new TCHAR[wcslen(InterfaceName)+1];
wcscpy_s(pszAdapter,wcslen(InterfaceName)+1,InterfaceName);
nChars = _sntprintf_s(szName,
                                                             MAX_PATH-1,
                                                             MAX_PATH-1,
                                                             _T("%s\\%s"),
                                                           PMCLASS_NDIS_MINIPORT,
                                                           pszAdapter);
szName[MAX_PATH-1]=0;

DWORD res = 0;
if(bEnable)
{
res = SetDevicePower(szName, 
                                                                     POWER_NAME, 
                                                                    PwrDeviceUnspecified);
}
else
{
res = SetDevicePower(szName, 
                                                                      POWER_NAME, 
                                                                      D4);
}

// TODO: take care of the return value 'res'

StringCchCopy(multiSz, (cbBuffer / sizeof(TCHAR))-1, pszAdapter);
multiSz[::_tcslen(multiSz)+1] = _T('\0'); // Multi sz needs an extra null

BOOL bRes;

if (bEnable)
{
bRes = DoNdisIOControl(IOCTL_NDIS_BIND_ADAPTER,
                                                                          multiSz,
                                                                          (_tcslen(multiSz)+2) * sizeof(TCHAR),
                                                                         NULL,NULL);
}
else
{
bRes = DoNdisIOControl(IOCTL_NDIS_UNBIND_ADAPTER,
                                                                            multiSz, 
                                                                           (_tcslen(multiSz)+2) * sizeof(TCHAR), 
                                                                          NULL,NULL);
}
// TODO: take care of the return value 'bRes '
}

}
While changing the power state of a device, be sure for bind and unbind an adapter so that NDIS can take care of the protocol drivers. 

Wednesday, September 17, 2008

Adding Low Power modes in PXA - P4

So far,we have discussed about how to put pxa into low power mode, how to wake it up, and briefly on how operating system restore things to the prior state after a transition to low power mode. Now lets put the bits and pieces together and see how windows ce does the low power mode transition in case of PXA.


As I have mentioned in the previous posts, we need to add the wakeup configuration first and then put the processor in to the low power mode.

For purpoes of explaining the concept, I will how to add a wakeup for GPIO 11 which is connected to the activity line of a communication module.

Adding the wakeup source
1) In OAL interrupt code(intr.c and bsp_cfg.h), add the code for creating a new interrupt corresponding to the wakeup source.

bsp_cfg.h - Add the new sysintr for the wakeup line

....
#define SYSINTR_COMMMOD_WAKEPUP (SYSINTR_FIRMWARE+22)
....


intr.c - map the sysintr to gpio 11 and add code for managing that
BOOL BSPIntrInit()
{
....
OALIntrStaticTranslate (SYSINTR_COMMMOD_WAKEPUP,IRQ_GPIOXX_2_GPIO11);
....
}


2) In OAL power code (power.c), add code for setting the pwer register. In mainstone bsp, the wakeups are register while OS starts (BSPPowerOffInit function)and the pwer value is written only while going into low power mode (BSPPowerOff).

BSPSetWakeSrc function - add a case for gpio 11 and set the gpio11 bit in the pPwer global variable. This variable will be written to the actual PWER register while going into suspend.

......
case IRQ_GPIOXX_2_GPIO11:
pPwer->gpio11 = 1;
OALMSG(1, ( L"BSPSetWakeSrc: IRQ Communication module source\r\n", irq));
break;
......


BSPGetWakeSrc function - add case for gpio 11 here also.This function to return the IRQ value if the PEDR (Power manager edge detect register) bit for gpio 11 is set.

....
else if (pPedr->gpio11)
{
*pSrcIrq = IRQ_GPIOXX_2_GPIO11;
}
....

BSPPowerOffInit function - use the OALIoCtlHalEnableWake function to set the global variable (g_oalWakeMask, an array) with the sysintr value corresponding to gpio11.
UINT32 commSysIntr = SYSINTR_COMMMOD_WAKEPUP;
...

OALIoCtlHalEnableWake(IOCTL_HAL_ENABLE_WAKE, &commSysIntr, sizeof(commSysIntr), NULL, 0, NULL);
...



While the BSPPowerOff function finds out which sysintr is mapped as a wakeup using OALPowerWakeSource and OALIntrTranslateSysIntr functions and sets the pwer bits with BSPSetWakeSrc function call.

When system resumes, BSPGetWakeSrc is used to get the wake cause and is stored in g_oalWakeSource variable which can be retrieved using kernel ioctl IOCTL_HAL_GET_WAKE_SOURCE



Wakeup Sources

When PXA is in low power modes such as Standby, Sleep or DeepSleep, the processor can be brought out using "Wakeups".

Wakeup is an "external or selected internal event". For example, assume that the power button is connected to GPIO0 of the processor, pressing of power button can be a wakeup source to the processor. Not all the GPIOs can act as wakeup for processor. For PXA the GPIO <116,> can act as wakeup sources. Also there are other wakeup sources such as USB host & client, RTC are wakeup sources.

Now we may not need to wakeup the processor for activity from any of these sources. Hence we can select the required wakeup source using the Power Manager Wake-up Enable (PWER) register. Also for GPIO wakeup sources, the edges needs to be configured using the Power Manager Rising Edge Detect Enable (PRER) and Falling Edge Detect Enable (PFER) register, so that wakeup will only happen for the selected edge of the transition on the GPIO.

NOTE: Take special care in configuring the direction of the GPIO which is chosen as a wakeup. If a wakeup source is configured for a GPIO which is configured as output, some undesirable results can happen.

For keypad and usim wakup there are seperate set of registers too for configuring specifics.

So in short, while configuring a gpio wake source in pxa, the following things should be taken into consideration:
  • PWER, selecting the wakeup source
  • PFER and PRER, fo which edge of GPIO transition , system needs to wakeup
  • GPDR, gpio should be configured as input

Tuesday, September 16, 2008

Adding Low Power modes in PXA - P2

Power Modes in PXA

PXA has the following power modes
  • Normal mode
  • Idle mode
  • Deep idle mode
  • Standby mode
  • Sleep mode
  • Deep Sleep mode
Each mode is characterized by the power domains that will be kept active and the clocks that will be running. For example in Sleep mode all the external low voltage power domains (i.e. domains controlled by PWR_EN pin of PXA) is disabled. While on the other hand, in Deep Sleep mode both the external low voltage and high voltage power domains are disabled as both PWR_EN and SYS_EN pins are de-asserted while entering the low power mode. The choice of which power mode to use for each depends solely on the power consumption requirements of the product.

In the beginning of the project, we mapped the Standby mode of PXA to POWER_STATE_SUSPEND state of Windows CE. But as we had a tight power consumption requirement for the Suspend mode, we had to re map the state to Sleep mode of PXA instead of standby.

Transition of Power Modes

For initiating a power mode transition in PXA, we need write in to the coprocessor 14 , register C7 (PWRMODE). Writing the bit pattern for each pattern corresponding to the power mode in to the PWRMODE[M] bits initiates the transition. For eg:

XllpPmEnterDeepSleep FUNCTION

ldr r3, =0x00000007
mcr p14, 0, r3, c7, c0, 0
nop
.......


A call to this assembly routine will initiate a transition to deep sleep mode.

Context Restoration


While going into low power modes such as Sleep or Deep Sleep, the processor contents can get corrupted.

Lets take the example of Sleep mode. In Sleep mode, the PC (Program Counter) value is corrupted along with some other set of registers. So in order to tackle this situation, operating systems are doing a context storage and restoration while going into low power mode. It stores the status of all the required registers such as GPIO configurations, level, stack address, core configuration registers in to a memory area. The address of this memory area is kept in a register which will be retained during the low power mode. In the case of PXA, it is the Power Manager Scratch Pad Register (PSPR). This register value will be maintained during a sleep reset, but will be cleared during GPIO, power on , hardware or watch dog resets.

So while going in to sleep mode, the required information is stored in a data area in RAM and the address to this area is stored in PSPR register. While resuming from sleep mode, the boot loader will check the PSPR register and will restore the configuration that is stored in the data area.

NOTE: bear in mind that the data area is in RAM, so the RAM needs to be properly initialized after a sleep reset, otherwise the boot loader can hand while trying to access the restoration area.

Monday, September 15, 2008

Adding Low Power modes in PXA - P1

For the windows ce project that we were working on had a lot of power states. As it was a PXA based board, with Mainstone III as the reference platform, we had to map these states to that of PXA.

The low power mode mappings were chosen as below:

  • POWER_STATE_SUSPEND - Sleep mode of PXA
  • POWER_STATE_OFF - Deep Sleep mode PXA
As you may have noticed in the Mainstone BSP's OAL code (the common soc code for pxa, i.e., WINCE600\PLATFORM\COMMON\SRC\SOC\PXA27X_MS_V1\OAL\POWER\off.c), the OemPowerOff Function uses the Deep Sleep mode of PXA. This function will be invoked from kernel when power manager issues a power off system.

Now with the new requirement for our software, we had to modify the existing OAL code and the power manager to add and map the new states to that of the PXA.

In this post I will walk you through on steps involved in adding a wakeup source in OAL.

We can splits the process in to two parts
  1. Mapping a SYSINTR value to the wakeup
  2. Adding the wakeup configuration specific to the processor
The detailed steps, I will explain in next post...

Sunday, September 7, 2008

Making a device "Power-Managed"

Power manager uses IOCTLs to interact with Power-Managed devices. So inorder to make a device power managed implement certain set of IOCTLs in the driver with registry changes.

For the purpose of explaining, i am taking the sample of the Power Button driver implemented in Mainstone platform available in windows ce 6.0. Please bear in mind that this driver is already having a function which monitors system power state transitions using Notification Queues. But I am not going to make use of them.

Here I will briefly describe the steps involved in doing the same. We can divide the changes in to two parts:
  1. Change in registry settings for driver
  2. Implementing IOCTLs

Change in registry settings for driver

Now as the documentation for power manager, each power managed device needs to be classified under a certain category i.e Generic power-manageable devices, Power-manageable block devices and Power-manageable NDIS miniports. Each class is identified by a certain GUID(defined in pm.h). Associating a device to a power manager class is really easy, just by adding a registry entry to the device driver entry, as follows.

I will make this power button driver as a generic device
[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\PWB]
"Prefix"="PWB"
"Dll"="mainstoneii_pwrbutton.dll"
"Order"=dword:3
"IClass"="{A32942B7-920C-486b-B0E6-92A702A99B35}" ;; we are making it as generic device over here

When device manager loads the driver, it will associate the device to the class.

Now power manager will start issuing the IOCTLs to the driver and query the capabilities of the driver. The details I will explain in the next section.
Note : We should atleast implement IOCTL_POWER_CAPABILITIES in a driver to make it power managed

Implementing IOCTLs

The driver needs to implement the following IOCTLs for interacting with power manager:
  • IOCTL_POWER_CAPABILITIES **
  • IOCTL_POWER_GET
  • IOCTL_POWER_QUERY *
  • IOCTL_POWER_SET
  • IOCTL_REGISTER_POWER_RELATIONSHIP *
** - This IOCTL is mandatory for the entire thing to work
* - rather optional


When power manager receives a request to register a device driver under a particular device class, it first passes IOCTL_POWER_CAPABILITIES to the driver. If the call returns false, the driver wont be registered otherwise it will be. This IOCTL should return the device capabilities.

BOOL IOControl( ............. )
{

................
case IOCTL_POWER_CAPABILITIES :
if (NULL != pOutBuf)
{
memcpy(pOutBuf, &PWBDrvPowerCaps, sizeof(PWBDrvPowerCaps));
*pdwBytesTransferred = sizeof(PWBDrvPowerCaps);
OutBufLen = sizeof(PWBDrvPowerCaps);
dwErr = ERROR_SUCCESS;
}
else
{
dwErr = ERROR_INVALID_PARAMETER;
}
break; // IOCTL_POWER_CAPABILITIES
...............
if(dwErr != ERROR_SUCCESS) {
bRc = FALSE;
} else {
bRc = TRUE;
}

return bRc;

}


The variable PWBDrvPowerCaps contains the device power capabilities of the driver in a POWER_CAPABILITIES structure.

POWER_CAPABILITIES PWBDrvPowerCaps =
{
DX_MASK(D0) | DX_MASK(D2) | DX_MASK(D3) | DX_MASK(D4),
0,
0,
{
0x00000000, // D0
0x00000000, // D1
0x00000000, // D2
0x00000000, // D3
0x00000000 // D4
},
{
0x00000000, // D0
0x00000000, // D1
0x00000000, // D2
0x00000000, // D3
0x00000000 // D4
},
0
};


For information on the meaning of each parameter please refer MSDN. After returning true from the IOCTL, power manager will register this device under the requested class in registry.

From now on wards, when ever a state change occurs (or is requested) for the device or system, power manager will send the IOCTL_POWER_SET to the driver and the driver can take care of the same.

case IOCTL_POWER_SET:
if ( pOutBuf != NULL )
{
SetPwbPowerState(*(PCEDEVICE_POWER_STATE) pOutBuf);
dwErr = ERROR_SUCCESS;
}
else
dwErr = ERROR_INVALID_PARAMETER;
break; // IOCTL_POWER_SET


In SetPwbPowerState(), you can define the power depending on the paramater.