// counter.cpp : 定义 DLL 应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <Pkfuncs.h>
#include "s2440.h"
volatile IOPreg * v_pIOPreg;
volatile INTreg * v_pINTreg;
UINT32 SysIntr;
HANDLE INTThread;//中断线程句柄
HANDLE INTEvent;//中断事件句柄
int num = 0;
extern "C" {BOOL VirtualCopy(LPVOID,LPVOID,DWORD,DWORD);}
DWORD WINAPI INTProcessThread (void);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch ( ul_reason_for_call )
{
case DLL_PROCESS_ATTACH:
RETAILMSG(1, (_T("COU_PROCESS_ATTACH\r\n")));
//DisableThreadLibraryCalls((HMODULE) hModule);
break;
case DLL_PROCESS_DETACH:
RETAILMSG(1, (_T("COU_PROCESS_DETACH\r\n")));
break;
}
return TRUE;
}
DWORD COU_Init(DWORD dwContext)
{
RETAILMSG(1,(_T("COU_INIT++\r\n")));
//IO寄存器初始化
v_pIOPreg = (volatile IOPreg *)VirtualAlloc(0, sizeof(IOPreg), MEM_RESERVE, PAGE_NOACCESS);
if (v_pIOPreg == NULL)
{
ERRORMSG(1,(_T("COU_InitAddrIO: VirtualAlloc failed!\r\n")));
return 0;
}
if (!VirtualCopy((PVOID)v_pIOPreg, (PVOID)(IOP_BASE), sizeof(IOPreg), PAGE_READWRITE | PAGE_NOCACHE))
{
ERRORMSG(1,(_T("COU_InitAddrIO: VirtualCopy failed!\r\n")));
VirtualFree((PVOID) v_pIOPreg, 0, MEM_RELEASE);
v_pIOPreg = NULL;
return 0;
}
//EINT寄存器初始化
v_pINTreg = (volatile INTreg *)VirtualAlloc(0, sizeof(INTreg), MEM_RESERVE, PAGE_NOACCESS);
if (v_pINTreg == NULL)
{
ERRORMSG(1,(_T("COU_InitAddrINT: VirtualAlloc failed!\r\n")));
return 0;
}
if (!VirtualCopy((PVOID)v_pINTreg, (PVOID)(INT_BASE), sizeof(INTreg), PAGE_READWRITE | PAGE_NOCACHE))
{
ERRORMSG(1,(_T("COU_InitAddrINT: VirtualCopy failed!\r\n")));
VirtualFree((PVOID) v_pINTreg, 0, MEM_RELEASE);
v_pINTreg = NULL;
return 0;
}
//IO寄存器设置
v_pIOPreg->rGPGCON &= ~(0x03 << 20);
v_pIOPreg->rGPGCON |= (0x02 << 20);//I/O设定,GPG10(EINT18)脚为EINT功能 20
v_pIOPreg->rGPGUP |= (0X01<<10);//I/O设定,GPG10脚不使用上拉电阻
v_pIOPreg->rEXTINT2 &= ~(0x07 << 8);
v_pIOPreg->rEXTINT2 |= (0x02 << 8);//中断设定,EINT2的EINT18为下降沿触发中断 8
v_pINTreg->rINTMSK &= ~(1 << 2);//中断屏蔽寄存器
v_pIOPreg->rEINTMASK &= ~(0x01 << 18);//开中断
//打开中断响应线程
INTThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)INTProcessThread, NULL, 0, NULL);
return 1;
}
DWORD COU_Deinit( DWORD dwContext )// future: pointer to the per disk structure
{
RETAILMSG(1,(_T("COU_Deinit++\r\n")));
InterruptDone(SysIntr);
InterruptDisable(SysIntr);
TerminateThread(INTThread,-1);
CloseHandle(INTThread);
CloseHandle(INTEvent);
VirtualFree((void*)v_pIOPreg, sizeof(IOPreg), MEM_RELEASE);
VirtualFree((void*)v_pINTreg, sizeof(INTreg), MEM_RELEASE);
return TRUE;
}
DWORD COU_Open(DWORD dwData,DWORD dwAccess,DWORD dwShareMode)
{
RETAILMSG(1,(_T("COU_Open++\r\n")));
/*//IO寄存器初始化
v_pIOPreg = (volatile IOPreg *)VirtualAlloc(0, sizeof(IOPreg), MEM_RESERVE, PAGE_NOACCESS);
if (v_pIOPreg == NULL)
{
ERRORMSG(1,(_T("COU_InitAddrIO: VirtualAlloc failed!\r\n")));
return 0;
}
if (!VirtualCopy((PVOID)v_pIOPreg, (PVOID)(IOP_BASE), sizeof(IOPreg), PAGE_READWRITE | PAGE_NOCACHE))
{
ERRORMSG(1,(_T("COU_InitAddrIO: VirtualCopy failed!\r\n")));
VirtualFree((PVOID) v_pIOPreg, 0, MEM_RELEASE);
v_pIOPreg = NULL;
return 0;
}
//EINT寄存器初始化
v_pINTreg = (volatile INTreg *)VirtualAlloc(0, sizeof(INTreg), MEM_RESERVE, PAGE_NOACCESS);
if (v_pINTreg == NULL)
{
ERRORMSG(1,(_T("COU_InitAddrINT: VirtualAlloc failed!\r\n")));
return 0;
}
if (!VirtualCopy((PVOID)v_pINTreg, (PVOID)(INT_BASE), sizeof(INTreg), PAGE_READWRITE | PAGE_NOCACHE))
{
ERRORMSG(1,(_T("COU_InitAddrINT: VirtualCopy failed!\r\n")));
VirtualFree((PVOID) v_pINTreg, 0, MEM_RELEASE);
v_pINTreg = NULL;
return 0;
}
//IO寄存器设置
v_pIOPreg->rGPGCON &= ~(0x03 << 20);
v_pIOPreg->rGPGCON |= (0x02 << 20);//I/O设定,GPG10(EINT18)脚为EINT功能 20
//v_pIOPreg->rGPGUP |= (0X01<<10);//I/O设定,GPG10脚不使用上拉电阻
v_pIOPreg->rEXTINT2 &= ~(0x07 << 8);
v_pIOPreg->rEXTINT2 |= (0x02 << 8);//中断设定,EINT2的EINT18为下降沿触发中断 8
//v_pINTreg->rINTMSK &= ~(1 << 2);//中断屏蔽寄存器
//v_pIOPreg->rEINTMASK &= ~(0x01 << 18);//开中断
//打开中断响应线程
INTThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)INTProcessThread, NULL, 0, NULL);
RETAILMSG(1,(_T("COU_Open--\r\n")));*/
return 1;
}
DWORD COU_Close(DWORD Handle)
{
RETAILMSG(1,(_T("COU_Close++\r\n")));
/*InterruptDisable(SysIntr);
TerminateThread(INTThread,-1);
CloseHandle(INTThread);
CloseHandle(INTEvent);
VirtualFree((void*)v_pIOPreg, sizeof(IOPreg), MEM_RELEASE);
VirtualFree((void*)v_pINTreg, sizeof(INTreg), MEM_RELEASE);
RETAILMSG(1,(_T("COU_Close--\r\n")));*/
return TRUE;
}
DWORD COU_IOControl(DWORD Handle,DWORD dwIoControlCode,PBYTE pInBuf,
DWORD nInBufSize,PBYTE pOutBuf,DWORD nOutBufSize,PDWORD pBytesReturned)
{
return 1;
}
DWORD COU_Read(DWORD Handle, LPVOID pBuffer, DWORD dwNumBytes)
{
//RETAILMSG(1,(_T("num: %d\r\n"),num));
memcpy(pBuffer, &num, sizeof(num));
return 1;
}
DWORD COU_Write(DWORD Handle, LPCVOID pBuffer, DWORD dwNumBytes)
{
RETAILMSG(1,(_T("COU_Write++\r\n")));
num = 0;
return 1;
}
DWORD COU_Seek(DWORD Handle, long lDistance, DWORD dwMoveMethod)
{
return 1;
}
void COU_PowerUp(void)
{}
void COU_PowerDown(void)
{}
DWORD WINAPI INTProcessThread (void)
{
RETAILMSG(1,(_T("INTProcessThread++\r\n")));
UINT32 IRQ = 46; //IRQ_EINT18 46
//动态映射IRQ
if (!KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &IRQ, sizeof(UINT32), &SysIntr, sizeof(UINT32), NULL))
{
RETAILMSG(1, (TEXT("ERROR: kEYBD: Failed to request sysintr value.\r\n")));
return FALSE;
}
//RETAILMSG(1,(_T("KernelIoControl_Done\r\n")));
//建立中断事件
INTEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!INTEvent)
{
RETAILMSG(1, (TEXT("ERROR: kEYBD: Failed to create event.\r\n")));
return FALSE;
}
//将中断事件与中断号联系起来
if (!InterruptInitialize(SysIntr, INTEvent, NULL, 0))
{
RETAILMSG(1,(TEXT("Fail to initialize interrupt event\r\n")));
return FALSE;
}
//RETAILMSG(1,(_T("InterruptInitialize_Done\r\n")));
while(1)
{
//RETAILMSG(1,(_T("count++\r\n")));
WaitForSingleObject(INTEvent, INFINITE);
//RETAILMSG(1,(_T("count--\r\n")));
num++;
//v_pIOPreg->rEINTMASK &= (0x1<<18);
InterruptDone(SysIntr);
}
RETAILMSG(1,(_T("INTProcessThread--\r\n")));
return TRUE;
}