Monday, August 18, 2008

BQ27200芯片的编程


BQ27200 is a commonly used chip in mobile phone like HP IPAQ 6828's battery. It is possible to make a program that interact with the chip in windows ce platform.

First, you will need PXA27X's DDK for windows CE 5.0 or bulverde board development files.

Then, you can start programming. The most important thing is to allocate virtual memory for addressing the chip.

void InitializeBulverde(){
vg_pGPIORegs = NULL;
vg_pI2CRegs = NULL;
vg_pClkRegs = NULL;
vg_pOSTRegs = NULL;

PUCHAR pVMem; // for virtual memory address
DWORD VMemSize; // Total size needed for virtual memory

// Total size needed for virtual memory :
// Page size is 4Kbytes
// Driver Globals is also 4Kbytes
VMemSize = (PAGE_SIZE*5);
////////////////////////////////////////
// Size needed for register space.
pVMem = (PUCHAR)VirtualAlloc(0, VMemSize, MEM_RESERVE, PAGE_NOACCESS);
if(!pVMem) {
MessageBox(NULL, TEXT("Virtual Allocate failed!"), TEXT("InitBatteryDriver"), MB_OK);
}

// Map register space to virtual memory.
// 1. GPIO register
if(!VirtualCopy(pVMem, (LPVOID)(BULVERDE_BASE_REG_PA_GPIO >> 8), PAGE_SIZE,
PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL)) {
NKDbgPrintfW(_T("InitBatteryDriver: Virtual Copy: GPIO space failed\r\n"), GetLastError());
MessageBox(NULL, TEXT("GPIO space failed"), TEXT("Virtual Copy"), MB_OK);
}
vg_pGPIORegs = (DWORD *)pVMem;
pVMem += PAGE_SIZE;

// 2. I2C register
if(!VirtualCopy(pVMem,(LPVOID)(BULVERDE_BASE_REG_PA_I2C >> 8), PAGE_SIZE,
PAGE_READWRITE | PAGE_PHYSICAL | PAGE_NOCACHE)) {
MessageBox(NULL, TEXT("I2C space failed"), TEXT("Virtual Copy"), MB_OK);
}
vg_pI2CRegs = (DWORD *)pVMem;
pVMem += PAGE_SIZE;

// 3. CLKMGR register
if(!VirtualCopy(pVMem,(LPVOID)(BULVERDE_BASE_REG_PA_CLKMGR >> 8), PAGE_SIZE,
PAGE_READWRITE | PAGE_PHYSICAL | PAGE_NOCACHE)) {
MessageBox(NULL, TEXT("CLKMGR space failed"), TEXT("Virtual Copy"), MB_OK);
}
vg_pClkRegs = (DWORD *)pVMem;
pVMem += PAGE_SIZE;

// 4. OST register
if(!VirtualCopy(pVMem,(LPVOID)(BULVERDE_BASE_REG_PA_OST >> 8), PAGE_SIZE,
PAGE_READWRITE | PAGE_PHYSICAL | PAGE_NOCACHE)) {
MessageBox(NULL, TEXT("OST space failed"), TEXT("Virtual Copy"), MB_OK);
}
vg_pOSTRegs = (DWORD *)pVMem;

XllpI2cInit((P_XLLP_I2C_T)vg_pI2CRegs, (P_XLLP_GPIO_T)vg_pGPIORegs, (P_XLLP_CLKMGR_T)vg_pClkRegs, (XLLP_UINT32_T) 0x1);

Then you can you can read the registry like this:

if (XllpI2CReadS((P_XLLP_I2C_T)vg_pI2CRegs, (XLLP_OST_T *)vg_pOSTRegs, BQ27200_SLAVE, (XLLP_UINT8_T *)&buff, 1, bSENDSTOP) == XLLP_TRUE) {
return L" Battdrvr: XllpI2CReadS() is failed \r\n";
}
and write like this:

if (XllpI2CWriteS((P_XLLP_I2C_T)vg_pI2CRegs, (XLLP_OST_T *)vg_pOSTRegs, BQ27200_SLAVE, (XLLP_UINT8_T *)&buff, 1, bSENDSTOP) == XLLP_TRUE) {
MessageBox(NULL, TEXT("XllpI2CWriteS() is failed"), TEXT("Battdrvr"), MB_OK);
}

Note, in driver programming, one should use NKDbgPrintfW to print out the debug information instead of fprintr(stderr ...). This is because, this is a real-time function and will be used only in WinCE programs that interact with the kernel. So it is better.

Ok. here I will publish the the battery calibration for BQ27200 called BQ27200Expert. It can display various information about the BQ27200 and also calibrate the battery.

Download the program from XDA!

http://forum.xda-developers.com/showpost.php?p=2527251&postcount=1525


No comments: