Sunday, June 28, 2009

Porting of a BSP - SDRAM

While working on BSP of Windows CE, we will come across cases when we have to port an existing BSP to another hardware. This hardware may have only minor changes like certain peripherals and reducing and/or increasing of memory. I will explain what needs to be done for some of the scenarios involving porting in the upcoming post.

In thos post, I will explain how to systematically change the software when the SDRAM in a hardware is changed. For example, the original BSP had a 32-bit SDRAM interface. But the new hardware is having a 16-bit interface instead.

Before jumping on to changing the code (as the hardware guys will always say, "you just change this bit, it will work straight away...", no pun intented, because it was always like that in my experience and ended up doing a lot of rework ), you need to prepare somethings before doing that.

If you are having a ICE (In Circuit Emulator), that would be of great help, as you can test and troubleshoot a lot of key aspects involving this change.

Do some ground work before jumping in:
  • Familiarize yourself with the datasheet of the processor which you are using (I have been working on PXA27x on sometime, so will explain based on that).
  • Prepare some assembly level (or C if you are not familair with assembly routines), which you can run on ICE and check the hardware. Especially whether you are able to access the new SDRAM.
  • Run some preliminary tests on the SDRAM like writing certain portions of and verifying it.
  • Scrutinize and tune the SDRAM timing values as they play a very important role in system's performance.


Now lets get on to the practical side. In PXA27x processor, for changing the sdram access from 32-bit to 16-bit and vice-versa can be done by clearing and setting the DWID0 and DWID2 bits in MDCNFG register depending on which bank the SDRAM located.

Once you are through with these steps, you are confidently start the modification involved in OS. Till now what I explained, looks good in theory, but needs a lot of analysis and patience.


While putting together the changes required in the Windows CE BSP, these are the aspects that you need to concentrate on :
  • The memory initialisation for 16-bit interface
  • The oem address table change (if required to change the SDRAM size)
  • Memory parameters in the BIB files (especially config_cebase.bib)
In the mainstone platform the switch to 16-bit mode can be done by setting the macro xlli_SDRAM_16BIT. Check the related code in WINCE600\PLATFORM\COMMON\SRC\SOC\PXA27X_MS_V1\XLLP\SOURCE\xlli_lowlev_init.s.

In config_cebase.bib, change the memory size considering the new ranges and change the address mapping in oemaddrtab_cfg.inc.

With these changes, the bootloader and operating system should move into 16-bit SDRAM mode.


Consider any XScale PXA27x BSP based which is based on the Mainstone III platform. Thanks to the Mainstone BSP developers ofcousrse,

Thursday, June 18, 2009

Accessing DrvEscape

In display drivers, DrvEscape is a function to retrieve information from a device that is not available in DDI for example, issuing a DrvEscape to the PXA27X display driver with GETGXINFO parameters will fill in the GAPI data structure.

For accessing the DrvEscape routines, the following code segment can be used:


{
HDC hDC = NULL;
DWORD Status;
CEDEVICE_POWER_STATE pwrState = D0;
hDC = CreateDC(NULL, NULL, NULL, NULL);
if (NULL != hDC)
{
pwrState = D0;
Status= ExtEscape(hDC, IOCTL_POWER_SET,
0, NULL,
sizeof(pwrState), (LPSTR)&pwrState );
ReleaseDC(NULL,hDC);
}
}


This code will open the display driver and will issue a IOCTL_POWER_SET command to make the display ON. In the PXA27X display driver, uses two display buffers, one for the filling in of graphic data, another a blank buffer , which will be used during D2 to D4 modes.

So if the display driver is in D2-D4 mode, this DrvEscape will bring the driver into D0. This will be equivalent of using SetDevicePower, but Microsoft is not recommending the usage of such APIs as it restricts the device self-management.

For checking whether the bluetooth stack is up and initialised, you can make use of the manual reset event BTH_NAMEDEVENT_STACK_INITED (L"system/events/bluetooth/StackInitialized").

You can get more information on this page.

If you are experiencing system performance issues in ROM based File systems, here is a nice tweak to boost up the performance. Most of the times, this slowdown will be due to constant registry flushing in to ROM in background.

[HKEY_LOCAL_MACHINE\init\BootVars]
"RegistryFlags"=dword:2
If this variable is set to 1, it will enable aggressive flushing of registry on to ROM. Setting it to 2 will disable background flushing and can improve performance of system significantly.

Reference: http://msdn.microsoft.com/en-us/library/aa914382.aspx

In one of my previous posts, I mentioned about cloning OALIOCTL, was thinking of writing about it. But Bruce have already written about. And it was my reference too. So check it out

It is assumed that , the OAL code is cloned to the BSP under development, including the ioctl code. For the purpose of explaining the process, the Mainstone III reference platform will be used.

The BSP specific, ioctl will be located under the directory, MAINSTONEIII\SRC\OAL\OALLIB\ioctl.c.

Now lets assume that we need to implement an ioctl to flag the backing up of system alarm setting during shutdown.

  • Define a new function in ioctl.c as:
BOOL OALIoCtlHalAlarmSet
( UINT32 code, VOID *pInpBuffer,
UINT32 inpSize, VOID *pOutBuffer,
UINT32 outSize, UINT32 *pOutSize)
{
g_oalIoCtlUserAlarmSet = TRUE;
return(TRUE);
}

  • Define a macro for the new IOCTL.
#define FILE_DEVICE_CUST_OEM   32806

#define IOCTL_DDK_SET_ALARM_FLAG \
CTL_CODE(FILE_DEVICE_CUST_OEM, 0x0100, METHOD_BUFFERED, FILE_ANY_ACCESS)
  • Modify g_oalIoCtlTable variable in ioctl.c to add the new ioctl mapping:
const OAL_IOCTL_HANDLER g_oalIoCtlTable[] = {
.....
{
IOCTL_DDK_SET_ALARM_FLAG,
0,
OALIoCtlHalAlarmSet
},
#include
.....
}

Now we have a kernel ioctl, which can be invoked from any section coming under kernel address space. For making this IOCTL accessible from User space, we need to customise (clone) OALIOCTL, which will be described in another post in detail

On resuming from sleep mode of PXA27X, the usb client wont be working straight away. No insertion events will be detected and the device will be shown as an uknown device in PC.

For overcoming this situation, we have to clear the OTGPH bit in PSSR (Power Manager Sleep Status Register). This is a write to clear bit.

OTGPH will be set when entering sleep mode when writing the PWRMODE register and indicates that the OTG pad is retaining state. So after resuming, the USB OTG, UDC and UHC should be configured, then only clear the OTGPH bit.

Referece : Marvell PXA27X Processor Developer Manual, November 3 , 2006

For monitoring events for a network interface, the notification request ioctl IOCTL_NDISUIO_REQUEST_NOTIFICATION in NDISUIO driver can be used.

By using this Notification mechanism, we can capture events for each network interface in Windows CE such as

  • Bind
  • Unbind
  • Power Up
  • Power down
  • etc...
A sample implementation is provided in the NDISPWR driver in PUBLIC code
(WINCE600\PUBLIC\COMMON\OAK\DRIVERS\NETSAMP\NDISPWR)

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.

Wednesday, June 3, 2009

Enabling agressive scanning for Wifi

To enable aggressive scanning for wifi network (preferred or non-preferred ones) in Wireless Zero Configuration in Windows CE, use this registry setting:

[HKEY_LOCAL_MACHINE\Comm\WiFi]
ScanInterval: dword:5
This will set WZC to scan for network every 5 seconds. By default, it is set to 60 seconds. Try tuning this value as per your requirement.