⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s3cjoystick.cpp

📁 SMDK2440 导航杆(joystick) Driver
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//

/*++

THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Module Name:

    Drv.c   Power Controller Driver

Abstract:

   Streams interface driver (MDD)

Functions:

Notes:

--*/

#include <windows.h>
#include <Winuser.h>
#include <bsp.h>


volatile S3C2440A_IOPORT_REG	*s2440IOP = NULL;
volatile S3C2440A_INTR_REG		*s2440INT = NULL;
volatile BSP_ARGS               *g_pBSPArgs = NULL;

void joystickActThread();
void joystickInterruptDone(UINT32 irq);
HANDLE g_hJoystickActThreads;
HANDLE g_hJoystickActEvents;
DWORD g_dwJoystickActIntr = IRQ_EINT14;
DWORD g_dwJoystickActSysIntr = SYSINTR_UNDEFINED;


// IRQ_EINT14,   IRQ_EINT16,   IRQ_EINT17,   IRQ_EINT18,   IRQ_EINT20
// JSKRETURN       JSKUP         JSKDOWN       JSKRIGHT      JSKLEFT
// VK_RETURN     VK_UP         VK_DOWN       VK_RIGHT      VK_LEFT
DWORD joystickMAP[][2]=
{
	{VK_RETURN ,IRQ_EINT14},
	{VK_UP ,IRQ_EINT16},
	{VK_DOWN ,IRQ_EINT17},
	{VK_RIGHT ,IRQ_EINT18},
	{VK_LEFT ,IRQ_EINT20}
};
	


BOOL
DllEntry(
    HINSTANCE   hinstDll,             /*@parm Instance pointer. */
    DWORD   dwReason,                 /*@parm Reason routine is called. */
    LPVOID  lpReserved                /*@parm system parameter. */
    )
{
    if ( dwReason == DLL_PROCESS_ATTACH ) {
        DEBUGREGISTER(hinstDll);
        RETAILMSG (1, (TEXT("PWR: Process Attach\r\n")));
    }

    if ( dwReason == DLL_PROCESS_DETACH ) {
        RETAILMSG (1, (TEXT("PWR: Process Detach\r\n")));
    }

    return(TRUE);
}




/*++

Called by Device Manager to initialize the streams interface in response to ActivateDevice.
We passed ActivateDevice a pointer to our device context, but must read it out of the registry as "ClientInfo".

Returns context used in XXX_Open, XXX_PowerDown, XXX_PowerUp, and XXX_Deinit

--*/
BOOL JSK_Init(
  LPCTSTR pContext,
  DWORD dwBusContext
)
{
	RETAILMSG(1,(TEXT("[JSK] INIT Start\r\n")));
	
	DWORD threadID;                         
	BOOL bSuccess;
	g_pBSPArgs = ((BSP_ARGS *) IMAGE_SHARE_ARGS_UA_START);

	
    // GPIO Virtual alloc
	s2440IOP = (volatile S3C2440A_IOPORT_REG *) VirtualAlloc(0,sizeof(S3C2440A_IOPORT_REG),MEM_RESERVE, PAGE_NOACCESS);
	if(s2440IOP == NULL) {
		RETAILMSG(1,(TEXT("For s2440IOP: VirtualAlloc failed!\r\n")));
	}
	else {
		if(!VirtualCopy((PVOID)s2440IOP,(PVOID)(S3C2440A_BASE_REG_PA_IOPORT >> 8),sizeof(S3C2440A_IOPORT_REG),PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE )) {
			RETAILMSG(1,(TEXT("For s2440IOP: VirtualCopy failed!\r\n")));
		}
	}
	
	// Interrupt Virtual alloc
	s2440INT = (volatile S3C2440A_INTR_REG *) VirtualAlloc(0,sizeof(S3C2440A_INTR_REG),MEM_RESERVE, PAGE_NOACCESS);
	if(s2440INT == NULL) {
		RETAILMSG(1,(TEXT("For s2440INT: VirtualAlloc failed!\r\n")));
	}
	else {
		if(!VirtualCopy((PVOID)s2440INT,(PVOID)(S3C2440A_BASE_REG_PA_INTR >> 8),sizeof(S3C2440A_INTR_REG),PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE )) {
			RETAILMSG(1,(TEXT("For s2440INT: VirtualCopy failed!\r\n")));
		}
	}
	
	s2440IOP->GPGDAT  |=  (0x5D << 6);
	s2440IOP->GPGUP   |=  (0x5D << 6);
	s2440IOP->GPGCON  = (s2440IOP->GPGCON & (~(0x33F3 << 12)))|(0x22A2<<12);
	
    s2440IOP->EXTINT2 = (s2440IOP->EXTINT2 & (~(0xF0FFF)))|(0x20222);		
    
    s2440IOP->EXTINT1 =(s2440IOP->EXTINT1& (~(0xF<< 24)))|(0x2 << 24);
    
    s2440IOP->EINTMASK &=  ~(0x5D << 14);
    if (s2440IOP->EINTPEND & (0x5D << 14))
    {
    	s2440IOP->EINTPEND|=(0x5D << 14);
    };
    
    g_hJoystickActEvents = CreateEvent(NULL, FALSE, FALSE, NULL);
  
    if (!g_hJoystickActEvents)
    {
    	RETAILMSG(1, (TEXT("[JSK] ERROR: Failed to create Event.\r\n")));
    	return FALSE;
    }
    	
    if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &g_dwJoystickActIntr, sizeof(UINT32), &g_dwJoystickActSysIntr, sizeof(UINT32), NULL))
    {
        RETAILMSG(1, (TEXT("[JSK] ERROR: Failed to request sysintr value for Intr interrupt.\r\n")));
        return FALSE;
    }
	bSuccess = InterruptInitialize(g_dwJoystickActSysIntr, g_hJoystickActEvents, NULL, 0);
    if (!bSuccess) 
    {
        RETAILMSG(1,(TEXT("Fail to initialize interrupt event\r\n")));
        return FALSE;
    } 
    //InterruptDone(g_dwJoystickActSysIntr);
   	
    g_hJoystickActThreads = CreateThread(NULL,
                                 0,
                                 (LPTHREAD_START_ROUTINE)joystickActThread,
                                 0,
                                 0,
                                 &threadID);
    
    if (g_hJoystickActThreads==NULL ) {
    	RETAILMSG(1,(TEXT("[JSK] Error : Failed to Create JSK Act thread.\r\n")));
    	return FALSE;
    }
	RETAILMSG(1,(TEXT("[JSK] INIT End. \r\n")));
	return true;
}



DWORD JSK_Open(
  DWORD hDeviceContext,
  DWORD AccessCode,
  DWORD ShareMode 
)
{
	return 0;
}

BOOL JSK_Close(
  DWORD hOpenContext 
)
{
	return true;
}

DWORD JSK_Write(
  DWORD hOpenContext,
  LPCVOID pBuffer,
  DWORD Count 
)
{
	return 0;
}


DWORD JSK_Read(
  DWORD hOpenContext,
  LPVOID pBuffer,
  DWORD Count 
)
{
	return 0;
}

BOOL JSK_IOControl(
  DWORD hOpenContext,
  DWORD dwCode,
  PBYTE pBufIn,
  DWORD dwLenIn,
  PBYTE pBufOut,
  DWORD dwLenOut,
  PDWORD pdwActualOut 
)
{
	return true;
}


DWORD JSK_Seek(
  DWORD hOpenContext,
  long Amount,
  WORD Type 
)
{
	return 0;
}


void JSK_PowerDown(
  DWORD hDeviceContext 
)
{
	return;
}
void JSK_PowerUp(
  DWORD hDeviceContext 
)
{
	return;
}

BOOL JSK_Deinit(
  DWORD hDeviceContext 
)
{
	VirtualFree((void*)g_pBSPArgs, 0, MEM_RELEASE);
	VirtualFree((void*)s2440IOP, 0, MEM_RELEASE);
	VirtualFree((void*)s2440INT, 0, MEM_RELEASE);
	return true;
}

void joystickActThread()
{
	DWORD	dwEvent;

	while (1) 
	{
						
  		dwEvent =WaitForSingleObject(g_hJoystickActEvents, INFINITE);
  		switch (dwEvent)
  		{	
    		case WAIT_OBJECT_0:
    			SetKMode(1);
    			keybd_event((BYTE)joystickMAP[g_pBSPArgs->nJSKKey][0],0,0,0 );
    			joystickInterruptDone(joystickMAP[g_pBSPArgs->nJSKKey][1]);  
    			SetKMode(0);  			    
    			break;
			case WAIT_TIMEOUT:
				RETAILMSG(1,(TEXT("[JSK] timout. \r\n")));
				break;
    		default:
      			RETAILMSG(1,(TEXT("[JSK] Error=%x. \r\n"),GetLastError()));
      		 	return;
 		 }
	}
}

void joystickInterruptDone(UINT32 irq)
{
	UINT32 mask;
	// Use external mask register
    mask = 1 << (irq - IRQ_EINT4 + 4);
    s2440IOP->EINTPEND = mask;
    s2440IOP->EINTMASK &=(~mask);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -