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

📄 timer_test.c

📁 WinCE5.0BSP for Renesas SH7770
💻 C
字号:
//
//  Copyright(C) Renesas Technology Corp. 2002-2003. All rights reserved.
//
// Test program for Timer for ITS-DS7
//
// FILE     : TIMER_TEST.C
// CREATED  : 2002.06.27
// MODIFIED : 2003.06.20
// AUTHOR   : Renesas Technology Corp.
// HARDWARE : RENESAS ITS-DS7
// HISTORY  : 
//            2003.06.20
//            - Created release code.
//

#include <windows.h>
#include <winbase.h>
#include <pkfuncs.h>
#include <types.h>
#include <ceddk.h>
#include <PCIReg.h>
#include <halether.h>
#include <Iltiming.h>
#include <Nkintr.h>

#define NELEMS(a) (sizeof(a)/sizeof((a)[0]))

LARGE_INTEGER	resolution;

void Test_QueryPerformanceCounter(int s);
void Test_TickCount(int s);
void Test_All(int waitsec);
void PrintNowTime();

PTSTR func_text[] =
{
    TEXT("TickCount"),
    TEXT("QueryPerformanceCounter"),
    TEXT("TickCount&QueryPerformanceCounter"),
};

PTSTR usage_text[] =
{
    TEXT("usage:\r\n"),
    TEXT("       -t <time>        sleep time(sec)(default 10s)\r\n"),
    TEXT("       -f <function_id> 0:TickCount / 1:QueryPerformanceCounter / 2:All (default 0)\r\n"),
    TEXT("       -?               help\r\n"),
};

void
Usage(void)
{ int i;

    for (i = 0; i < NELEMS(usage_text); i++) {
        RETAILMSG(1, (usage_text[i]));
    }
}

int WINAPI
WinMain (
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
    LPWSTR lpCmdLine,
	int nCmdShow
	)
{
	PTSTR pOption;
	PTSTR pParameter;
	TCHAR ws[] = TEXT(" \t");
	int errors = 0;
	DWORD dwTimerFunc = 0;
	DWORD dwTime = 10;

	RETAILMSG(1, (TEXT("+timer test\r\n")));

    // parse the command line
    for (pOption = _tcstok(lpCmdLine, ws); pOption != NULL; pOption = _tcstok(NULL, ws)) {
        if (pOption[0] != '/' && pOption[0] != '-') {
            RETAILMSG(1, (TEXT("Unrecognized argument %s\r\n"), pOption));
            errors++;
            continue;
        }
        pParameter = _tcstok(NULL, ws);
        if (pParameter == NULL) {
            RETAILMSG(1, (TEXT("Missing parameter to option %s\r\n"), pOption));
        }
        switch (pOption[1]) {
        case 'f':
            dwTimerFunc = _ttoi(pParameter);
			if(dwTimerFunc < 0 || 2 < dwTimerFunc){
	            RETAILMSG(1, (TEXT("Bad function_id %d\r\n"),dwTimerFunc));
	            errors++;
			}
            break;

		case 't':
		    dwTime = _ttoi(pParameter);
			if(dwTime <= 0 || pParameter[0]=='-'){
	            RETAILMSG(1, (TEXT("Illegal time %d\r\n"),dwTime));
	            errors++;
			}
		    break;

        case '?':
        case 'h':
            Usage();
            errors++;
            break;

        default:
            RETAILMSG(1, (TEXT("Unrecognized option %s\r\n"), pOption));
            errors++;
            break;
        }
    }

    if (errors > 0) {
        // we've already issued complaint, now just exit
		RETAILMSG(1, (TEXT("-timer test\r\n")));
        return -1;
    }

	QueryPerformanceFrequency( &resolution );

	RETAILMSG(1, (TEXT("[%s test] start %d sec sleep after 5 sec.\r\n"), func_text[dwTimerFunc] ,dwTime));
	RETAILMSG(1, (TEXT("5\r\n")));
	Sleep(1000);
	RETAILMSG(1, (TEXT("4\r\n")));
	Sleep(1000);
	RETAILMSG(1, (TEXT("3\r\n")));
	Sleep(1000);
	RETAILMSG(1, (TEXT("2\r\n")));
	Sleep(1000);
	RETAILMSG(1, (TEXT("1\r\n")));
	Sleep(1000);
	PrintNowTime();

	switch(dwTimerFunc){
	case 0:
		Test_TickCount(dwTime);
		break;
	case 1:
		Test_QueryPerformanceCounter(dwTime);
		break;
	default:
		Test_All(dwTime);
		break;
	}
	PrintNowTime();

	RETAILMSG(1, (TEXT("-timer test\r\n")));
	return 0;
}

void Test_TickCount(int waitsec)
{
	DWORD	start_count,finish_count,duration;
	unsigned long waitms;

	waitms = waitsec*1000;

	start_count = GetTickCount();
	RETAILMSG(1, (TEXT("START STW now!! TickCount:%d\r\n"),start_count));

	Sleep(waitms);

	finish_count = GetTickCount();
	RETAILMSG(1, (TEXT("STOP STW now!! TickCount:%d\r\n"),finish_count));

	duration=(finish_count-start_count);
	RETAILMSG(1, (TEXT("Start:  %d Finish:  %d  Duration:  %d Expected:  %d\n"),
		start_count, finish_count, duration, waitms));
}

void Test_QueryPerformanceCounter(int waitsec)
{
	LARGE_INTEGER	start_count,finish_count,duration;
	unsigned long waitms;

	waitms = waitsec*1000;

	QueryPerformanceCounter( &start_count );
	RETAILMSG(1, (TEXT("START STW now!! QueryPerformanceCounter:%I64i\r\n"),start_count.QuadPart));

	Sleep(waitms);

	QueryPerformanceCounter( &finish_count );
	RETAILMSG(1, (TEXT("STOP STW now!! QueryPerformanceCounter:%I64i\r\n"),finish_count.QuadPart));

	duration.QuadPart=(finish_count.QuadPart-start_count.QuadPart)*1000/resolution.QuadPart;
	RETAILMSG(1, (TEXT("Resolution:  %I64i Start:  %I64i Finish:  %I64i  Duration:  %I64i Expected:  %d\n"),
		resolution.QuadPart, start_count.QuadPart, finish_count.QuadPart, 
		duration.QuadPart, waitms));
}

void Test_All(int waitsec)
{
	LARGE_INTEGER	start_perf,finish_perf,duration_perf;
	DWORD	start_tick,finish_tick,duration_tick;
	unsigned long waitms;

	waitms = waitsec*1000;

	start_tick = GetTickCount();
	QueryPerformanceCounter( &start_perf );
	RETAILMSG(1, (TEXT("START STW now!! TickCount:%d QueryPerformanceCounter:%I64i\r\n"),start_tick, start_perf.QuadPart));

	Sleep(waitms);

	finish_tick = GetTickCount();
	QueryPerformanceCounter( &finish_perf );
	RETAILMSG(1, (TEXT("STOP STW now!! TickCount:%d QueryPerformanceCounter:%I64i\r\n"),finish_tick, finish_perf.QuadPart));

	duration_tick=(finish_tick-start_tick);
	RETAILMSG(1, (TEXT("TickCount> Start:  %d Finish:  %d  Duration:  %d Expected:  %d\n"),
		start_tick, finish_tick, duration_tick, waitms));

	duration_perf.QuadPart=(finish_perf.QuadPart-start_perf.QuadPart)*1000/resolution.QuadPart;
	RETAILMSG(1, (TEXT("QueryPerformanceCounter> Resolution:  %I64i Start:  %I64i Finish:  %I64i  Duration:  %I64i Expected:  %d\n"),
		resolution.QuadPart, start_perf.QuadPart, finish_perf.QuadPart, 
		duration_perf.QuadPart, waitms));
}

void PrintNowTime()
{
	SYSTEMTIME	time;

	memset( &time, 0, sizeof(time) );
	// Get Time
	GetLocalTime( &time );
	RETAILMSG(1, (TEXT("RTC by reference[%04d/%02d/%02d %02d:%02d:%02d]\r\n")	
		,time.wYear,time.wMonth,time.wDay
		,time.wHour,time.wMinute,time.wSecond ));
}

⌨️ 快捷键说明

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