这些程序基于TQ2440平台,主要是实现键盘按键的运作,方便起见,只实现了上下左右四个按键。该部分硬件结构图如下;
从硬件图可以看出,按键连接EINT1、EINT2、EINT4和EINT0四个中断引脚。在WindowsCE中,我们只需要处理好这四个中断即可,如下详细介绍;
从这幅图中可以看出,EINT0~4的是由GPF0~4控制的,因此在驱动之前需要先配置这四个GPIO口,使其下降延触发,如下代码;
v_pIOPregs->GPFCON &= ~(0x3 << 0); /* Set EINT0(GPF0) as EINT0 */
v_pIOPregs->GPFCON |= (0x2 << 0);
v_pIOPregs->EXTINT0 &= ~(0x7 << 0); /* Configure EINT0 as Falling Edge Mode */
v_pIOPregs->EXTINT0 |= (0x2 << 0);
v_pIOPregs->GPFCON &= ~(0x3 << 2); /* Set EINT1(GPF0) as EINT1 */
v_pIOPregs->GPFCON |= (0x2 << 2);
v_pIOPregs->EXTINT0 &= ~(0x7 << 4); /* Configure EINT1 as Falling Edge Mode */
v_pIOPregs->EXTINT0 |= (0x2 << 4);
v_pIOPregs->GPFCON &= ~(0x3 << 4); /* Set EINT2(GPF2) as EINT */
v_pIOPregs->GPFCON |= (0x2 << 4);
v_pIOPregs->EXTINT0 &= ~(0x7 << 8); /* Configure EINT2 as Falling Edge Mode */
v_pIOPregs->EXTINT0 |= (0x2 << 8);
v_pIOPregs->GPFCON &= ~(0x3 << 8); /* Set EINT0(GPF4) as EINT4 */
v_pIOPregs->GPFCON |= (0x2 << 8);
v_pIOPregs->EXTINT0 &= ~(0x7 << 16); /* Configure EINT4 as Falling Edge Mod */
v_pIOPregs->EXTINT0 |= (0x2 << 16);
配置好之后,进行中断处理线程,如下代码;
IntEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!IntEvent)
{
RETAILMSG(1, (TEXT("ERROR: kEYBD: Failed to create event./r/n")));
return FALSE;
}
IRQ = 1; //IRQ_EINT1;
if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &IRQ, sizeof(UINT32), &g_KeySysIntr[0], sizeof(UINT32), NULL))
{
RETAILMSG(1, (TEXT("ERROR: kEYBD: Failed to request sysintr value./r/n")));
return FALSE;
}
IRQ = 32; //IRQ_EINT4;
if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &IRQ, sizeof(UINT32), &g_KeySysIntr[1], sizeof(UINT32), NULL))
{
RETAILMSG(1, (TEXT("ERROR: kEYBD: Failed to request sysintr value./r/n")));
return FALSE;
}
IRQ = 2; //IRQ_EINT2;
if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &IRQ, sizeof(UINT32), &g_KeySysIntr[2], sizeof(UINT32), NULL))
{
RETAILMSG(1, (TEXT("ERROR: kEYBD: Failed to request sysintr value./r/n")));
return FALSE;
}
IRQ = 0; //IRQ_EINT0;
if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &IRQ, sizeof(UINT32), &g_KeySysIntr[3], sizeof(UINT32), NULL))
{
RETAILMSG(1, (TEXT("ERROR: kEYBD: Failed to request sysintr value./r/n")));
return FALSE;
}
if (!InterruptInitialize(g_KeySysIntr[0], IntEvent, NULL, 0))
{
RETAILMSG(1,(TEXT("Fail to initialize userkey interrupt event/r/n")));
return FALSE;
}
if (!InterruptInitialize(g_KeySysIntr[1], IntEvent, NULL, 0))
{
RETAILMSG(1,(TEXT("Fail to initialize userkey interrupt event/r/n")));
return FALSE;
}
if (!InterruptInitialize(g_KeySysIntr[2], IntEvent, NULL, 0))
{
RETAILMSG(1,(TEXT("Fail to initialize userkey interrupt event/r/n")));
return FALSE;
}
if (!InterruptInitialize(g_KeySysIntr[3], IntEvent, NULL, 0))
{
RETAILMSG(1,(TEXT("Fail to initialize userkey interrupt event/r/n")));
return FALSE;
}
while(1)
{
WaitForSingleObject(IntEvent, INFINITE);
RETAILMSG(0,(L"INTMASK=%X, EINTMASK=%X, GPGCON=%X/r/n", v_pINTRregs->INTMSK, v_pIOPregs->EINTMASK, v_pIOPregs->GPFCON));
//EINT1 - K1 - VK_UP - 0x26
if(v_pINTRregs->INTMSK & (1< {
RETAILMSG(1,(TEXT("[Key1 - UP]/r/n")));
keybd_event(VK_UP ,0x26, 0, 0);
Sleep(30);
keybd_event(VK_UP ,0x26, KEYEVENTF_KEYUP, 0);
InterruptDone(g_KeySysIntr[0]);
}
//EINT4 - K2 - VK_DOWN - 0x28
if(v_pIOPregs->EINTMASK & (1< {
RETAILMSG(1,(TEXT("[Key2 - DOWN]/r/n")));
keybd_event(VK_DOWN ,0x28, 0, 0);
Sleep(30);
keybd_event(VK_DOWN ,0x28, KEYEVENTF_KEYUP, 0);
InterruptDone(g_KeySysIntr[1]);
}
//EINT2 - K3 - VK_LEFT - 0x25
if(v_pINTRregs->INTMSK & (1< {
RETAILMSG(1,(TEXT("[K3 - LEFT]/r/n")));
keybd_event(VK_LEFT ,0x25, 0, 0);
Sleep(30);
keybd_event(VK_LEFT ,0x25, KEYEVENTF_KEYUP, 0);
InterruptDone(g_KeySysIntr[2]);
}
//EINT0 - K4 - VK_RIGHT - 0x27
if(v_pINTRregs->INTMSK & (1< {
RETAILMSG(1,(TEXT("[K4 - RIGHT]/r/n")));
keybd_event(VK_RIGHT ,0x27, 0, 0);
Sleep(30);
keybd_event(VK_RIGHT ,0x27, KEYEVENTF_KEYUP, 0);
InterruptDone(g_KeySysIntr[3]);
}
}