mini2440 看门狗驱动 WatchDog Driver

友善官方提供的看门狗长时间运行后,会造成内存泄漏,从而导致系统重启,给应用开发人员带来了一些麻烦。

我重新写了一下看门狗的驱动,很简单。就是把原有的每次喂狗时分配内存改为初始化时分配。

volatile WATCHreg	*s2440WDT = 0;

static BOOL FeedIt()
{

	s2440WDT->rWTCON = 0x8021; // Init Value
	s2440WDT->rWTDAT = s2440WDT->rWTCNT = 0xFEA9;
	s2440WDT->rWTCON = ( 1 << 0) | (0x03 << 3 ) | (1 << 5) | (0x5A << 8);
	RETAILMSG(1,(TEXT("WDT: feed ok\r\n")));
	return TRUE;
}

BOOL WINAPI  
DllEntry(HANDLE	hinstDLL, 
			DWORD dwReason, 
			LPVOID  Reserved/* lpvReserved */)
{
	switch(dwReason)
	{
	case DLL_PROCESS_ATTACH:
		DEBUGREGISTER((HINSTANCE)hinstDLL);
		break;
	case DLL_THREAD_ATTACH:
		break;
	case DLL_THREAD_DETACH:
		break;
	case DLL_PROCESS_DETACH:
		break;
#ifdef UNDER_CE
	case DLL_PROCESS_EXITING:
		break;
	case DLL_SYSTEM_STARTED:
		break;
#endif
	}

	return TRUE;
}


BOOL WDT_Deinit(DWORD hDeviceContext)
{
	
	return TRUE;
}
 

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

void WDT_PowerDown(DWORD hDeviceContext)
{
} 

void WDT_PowerUp(DWORD hDeviceContext)
{
} 

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

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

DWORD WDT_Init(DWORD dwContext)
{

  do {
		s2440WDT = (volatile WATCHreg *) VirtualAlloc(0,sizeof(WATCHreg),MEM_RESERVE, PAGE_NOACCESS);
		if (s2440WDT == 0) {
			break;
		}

		if (!VirtualCopy((PVOID)s2440WDT,(PVOID)(WATCH_BASE),sizeof(WATCHreg),PAGE_READWRITE | PAGE_NOCACHE )) {
			break;
		}

		//ret = TRUE;
		RETAILMSG(1,(TEXT("WDT: VirtualAlloc ok\r\n")));
	} while (0);
	RETAILMSG(1,(TEXT("WDT: Init\r\n")));
	return TRUE;
}

BOOL WDT_Close(DWORD hOpenContext)
{
	if (s2440WDT) {
		VirtualFree((void*)s2440WDT, sizeof(WATCHreg), MEM_RELEASE);
		s2440WDT = 0;
		RETAILMSG(1,(TEXT("WDT: VirtualFree ok\r\n")));
	}
	RETAILMSG(1,(TEXT("WDT: Close\r\n")));
	return TRUE;
} 

// Beaf is here

DWORD WDT_Open(DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode)
{
	RETAILMSG(1,(TEXT("WDT: Open\r\n")));
	FeedIt();
	return TRUE;
} 

DWORD WDT_Write(DWORD hOpenContext, LPCVOID pSourceBytes, DWORD NumberOfBytes)
{
	RETAILMSG(1,(TEXT("WDT: Write\r\n")));
	FeedIt();
	return TRUE;
}

posted @ 2010-09-15 15:46  李玉宝  阅读(943)  评论(1编辑  收藏  举报