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

📄 d12testdlg.cpp

📁 PDIUSBD12套件说明及源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*
   //*************************************************************************
   //
   //                  P H I L I P S   P R O P R I E T A R Y
   //
   //           COPYRIGHT (c)   1998 BY PHILIPS SINGAPORE.
   //                     --  ALL RIGHTS RESERVED  --
   //
   // File Name:	D12TestDlg.CPP
   // Author:		Wenkai Du
   // Created:		14 Jan 98
   // Modified:
   // Revision:		1.4
   //
   //*************************************************************************
   //
   //*************************************************************************
   */

// D12TestDlg.cpp : implementation file
//
#include "stdafx.h"

#include <mmsystem.h>
#include "D12Test.h"
#include "D12TestDlg.h"
#include <devioctl.h>
#include "process.h"
#include "rwbulk.h"
#include "ioctl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

UINT InterruptIn(void * pParam) {  
    char temp [80],cc[180], c;
	unsigned int i;
    BOOLEAN bResult = FALSE;
    ULONG nBytes = 0;
	struct CThreadParam * threadParam;
	HANDLE hFile, hDevice;
	CD12TestDlg* testDlg;
	IO_BLOCK ioBlock;
	IO_REQUEST ioRequest;

	threadParam=(struct CThreadParam *)pParam;
	threadParam->bInThread = TRUE;

	testDlg = threadParam->testDlg;

	threadParam->pcIoBuffer = (unsigned char *)malloc (threadParam->uiLength + 16); 
				
	do {
		do {
			Sleep(2000);
			
			hFile = open_file(threadParam->pipe_name);

			if(hFile != INVALID_HANDLE_VALUE) {

				hDevice = open_dev();

				ioBlock.uOffset = 0;
				ioBlock.uLength = 1;
				ioBlock.pbyData = (PUCHAR)&c;
				ioBlock.uIndex = GET_FIRMWARE_VERSION;

				bResult = DeviceIoControl(hDevice,
						IOCTL_READ_REGISTERS,
						(PVOID)&ioBlock,
						sizeof(IO_BLOCK),
						(PVOID)&c,
						1,
						&nBytes,
						NULL);

				if (bResult != TRUE) {
					CloseHandle(hFile);
					CloseHandle(hDevice);

					threadParam->bInThread = FALSE;
					wsprintf (cc, "This version of firmware does not support this test application.\r\nPlease upgrade the firmware of the evaluation board.");
					testDlg->MessageBox((LPCSTR)cc, "Incompatible Firmware", MB_ICONSTOP);
					testDlg->EndDialog(0);

					return 0;
				}
			}
		} while(hFile == INVALID_HANDLE_VALUE && threadParam->bInThread == TRUE);

		threadParam->ulData[0] |= D12_DRIVERLOAD;
		threadParam->ulData[1] = c;
		threadParam->bUpdate = TRUE;

		if (hFile != INVALID_HANDLE_VALUE) {

			while(threadParam->bInThread == TRUE){

				bResult = ReadFile(hFile,
							  threadParam->pcIoBuffer,
							  threadParam->uiLength,
							  &nBytes,
							  NULL);
				
				if (bResult == TRUE) {
					threadParam->ulData[0] |= D12_KEYSTATUS;
					threadParam->ulData[2] = *(threadParam->pcIoBuffer);
					threadParam->bUpdate = TRUE;
				} 
				else {
					DeviceIoControl(hFile,
						IOCTL_D12_RESET_PIPE,
						0,
						0,
						0,
						0,
						&nBytes,
						NULL);

					threadParam->ulData[0] |= D12_DRIVERUNLOAD;
					threadParam->bUpdate = TRUE;

					break;
				}/* else ioctl failed */
			};

			CloseHandle(hFile);
			CloseHandle(hDevice);

		}// if valid hDevice

	} while(threadParam->bInThread == TRUE);

	free(threadParam->pcIoBuffer);
	threadParam->pcIoBuffer = NULL;

	threadParam->bInThread = FALSE;

    return 0; 
} 

UINT GenericOut(void * pParam) {  
    char temp [80],cc[80];
    BOOLEAN bResult = FALSE;
    ULONG nBytes = 0;
	struct CThreadParam * threadParam;
	HANDLE hFile;
	CD12TestDlg* testDlg;

	threadParam=(struct CThreadParam *)pParam;
	threadParam->bInThread = FALSE;
	testDlg = threadParam->testDlg;

	hFile = open_file(threadParam->pipe_name);
	if(hFile == INVALID_HANDLE_VALUE) {
		free(threadParam->pcIoBuffer);
		threadParam->pcIoBuffer = NULL;

		strcpy(temp, "Open driver failed! Can't write to ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, ".");
		testDlg->MessageBox((LPCSTR)temp, "Test Error");
		return 0;
	}

    if (hFile != INVALID_HANDLE_VALUE) {

		threadParam->bInThread = TRUE;
		
	        bResult = WriteFile(hFile,
	                      threadParam->pcIoBuffer,
                          threadParam->uiLength,
	                      &nBytes,
	                      NULL);
			
			if (bResult != TRUE) {
				DeviceIoControl(hFile,
					IOCTL_D12_RESET_PIPE,
					0,
					0,
					0,
					0,
					&nBytes,
					NULL);
				wsprintf (cc, "Writing ");
				strcat( cc, threadParam->pipe_name);
				strcat( cc, " failed! Aborted.");
				testDlg->MessageBox((LPCSTR)cc, "Test Error");
			}/* else ioctl failed */


    }// if valid hDevice

	//The thread terminated by itself, delete resources

	CloseHandle(hFile);

	free(threadParam->pcIoBuffer);
	threadParam->pcIoBuffer = NULL;

	threadParam->bInThread = FALSE;

    return 0; 
} 


UINT ReadPipe(void * pParam) {  
    char temp [80],cc[80];
	unsigned int i;
    BOOLEAN bResult = FALSE;
    ULONG nBytes = 0;
	struct CThreadParam * threadParam;
	CD12TestDlg* testDlg;
	HANDLE hFile, hDevice;
	DWORD time0, time1;
	IO_BLOCK ioBlock;
	IO_REQUEST ioRequest;

	threadParam=(struct CThreadParam *)pParam;
	threadParam->bInThread = FALSE;
	testDlg = threadParam->testDlg;
	threadParam->bUpdate = FALSE;

	hFile = open_file(threadParam->pipe_name);
	if(hFile == INVALID_HANDLE_VALUE) {
		strcpy(temp, "Open driver failed! Can't read from ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, ".");
		testDlg->MessageBox((LPCSTR)temp, "Test Error");
		return 0;
	}

	hDevice = open_dev();
	if(hDevice == INVALID_HANDLE_VALUE) {
		CloseHandle(hFile);
		strcpy(temp, "Open device handle failed! Can't read from ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, ".");
		testDlg->MessageBox((LPCSTR)temp, "Test Error");
		return 0;
	}

    threadParam->pcIoBuffer = (unsigned char *)malloc (threadParam->uiLength + 16); 
	threadParam->ulCount = 0;
	threadParam->total_time = 0;
	threadParam->cur_rate = 0;
	threadParam->max_rate = 0;

    if (hFile != INVALID_HANDLE_VALUE) {

		threadParam->bInThread = TRUE;
		testDlg->m_InStartBtn.EnableWindow(FALSE);
		testDlg->m_InStopBtn.EnableWindow(TRUE);
		
		do{

			ioRequest.uAddressL = 0;
			ioRequest.bAddressH = 0;
			ioRequest.uSize = threadParam->uiLength;
			ioRequest.bCommand = 0x81;	//start, read

			ioBlock.uOffset = 0;
			ioBlock.uLength = sizeof(IO_REQUEST);
			ioBlock.pbyData = (PUCHAR)&ioRequest;
			ioBlock.uIndex = SETUP_DMA_REQUEST;

			bResult = DeviceIoControl(hDevice,
					IOCTL_WRITE_REGISTERS,
					(PVOID)&ioBlock,
					sizeof(IO_BLOCK),
					NULL,
					0,
					&nBytes,
					NULL);

			if (bResult != TRUE) {
				threadParam->bInThread = FALSE;
				wsprintf (cc, "Requesting data transfer on ");
				strcat( cc, threadParam->pipe_name);
				strcat( cc, " failed! Aborted.");
				testDlg->MessageBox((LPCSTR)cc, "Test Error");
				break;
			}

			time0 = timeGetTime();
	        bResult = ReadFile(hFile,
	                      threadParam->pcIoBuffer,
                          threadParam->uiLength,
	                      &nBytes,
	                      NULL);
			time1 = timeGetTime();
			
			if (bResult != TRUE) {
				DeviceIoControl(hFile,
					IOCTL_D12_RESET_PIPE,
					0,
					0,
					0,
					0,
					&nBytes,
					NULL);
				threadParam->bInThread = FALSE;
				wsprintf (cc, "Reading ");
				strcat( cc, threadParam->pipe_name);
				strcat( cc, " failed! Aborted.");
				testDlg->MessageBox((LPCSTR)cc, "Test Error");
				break;
			} 
			else {
				threadParam->ulCount += nBytes;
				if(time1 != time0) {
					threadParam->cur_rate = (nBytes*1000)/(time1-time0-1);
					if(threadParam->cur_rate > threadParam->max_rate)
						threadParam->max_rate = threadParam->cur_rate;
					threadParam->total_time += (time1-time0-1);
					threadParam->bUpdate = TRUE;
				}
			}
		}while(threadParam->bInThread == TRUE);


    }// if valid hDevice

	//The thread terminated by itself, delete resources

	CloseHandle(hFile);
	CloseHandle(hDevice);

	free(threadParam->pcIoBuffer);
	threadParam->pcIoBuffer = NULL;

	testDlg->m_InStartBtn.EnableWindow(TRUE);
	testDlg->m_InStopBtn.EnableWindow(FALSE);

    return 0; 
} 

UINT WritePipe(void * pParam) {  
    char temp [80],cc[80];
    BOOLEAN bResult = FALSE;
    ULONG nBytes = 0, ulCount = 0, index, cnt, seq;
	struct CThreadParam * threadParam;
	CD12TestDlg* testDlg;
	unsigned char rNum;
	HANDLE hFile, hDevice;
	DWORD time0, time1;
	IO_BLOCK ioBlock;
	IO_REQUEST ioRequest;


	threadParam=(struct CThreadParam *)pParam;
	threadParam->bInThread = FALSE;
	testDlg = threadParam->testDlg;
	threadParam->bUpdate = FALSE;

	hFile = open_file(threadParam->pipe_name);
	if(hFile == INVALID_HANDLE_VALUE) {
		strcpy(temp, "Open driver failed! Can't write to ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, ".");
		testDlg->MessageBox((LPCSTR)temp, "Test Error");
		return 0;
	}

	hDevice = open_dev();
	if(hDevice == INVALID_HANDLE_VALUE) {
		CloseHandle(hFile);
		strcpy(temp, "Open device handle failed! Can't write to ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, ".");
		testDlg->MessageBox((LPCSTR)temp, "Test Error");
		return 0;
	}

    threadParam->pcIoBuffer = (unsigned char *)malloc (threadParam->uiLength + 16); 
	threadParam->ulCount = 0;
	threadParam->total_time = 0;
	threadParam->cur_rate = 0;
	threadParam->max_rate = 0;

    if (hFile != INVALID_HANDLE_VALUE) {
		int buffLen;

		// this is the size of incrementing pattern
		buffLen = threadParam->uiLength - sizeof(ulCount);

		threadParam->bInThread = TRUE;
		testDlg->m_OutStartBtn.EnableWindow(FALSE);
		testDlg->m_OutStopBtn.EnableWindow(TRUE);

		if(threadParam->uiLength == 0) {
		}
		else {

			do {
			
				if(buffLen > 0)
				{
					rNum = 0;

					for(index = 0; index < threadParam->uiLength; index++)
						*(threadParam->pcIoBuffer+index) = rNum++;
					
					cnt = threadParam->uiLength/64;
					
					for(index = 0; index < cnt ; index ++) {
						seq = ulCount*cnt + index;
							// stick unsigned long count at the beginning of buffer
						memcpy(threadParam->pcIoBuffer + index*64, &seq, 
							min(sizeof(ulCount), threadParam->uiLength));
					}
				}

				ioRequest.uAddressL = 0;
				ioRequest.bAddressH = 0;
				ioRequest.uSize = threadParam->uiLength;
				ioRequest.bCommand = 0x80;	//start, write

				ioBlock.uOffset = 0;
				ioBlock.uLength = sizeof(IO_REQUEST);
				ioBlock.pbyData = (PUCHAR)&ioRequest;
				ioBlock.uIndex = SETUP_DMA_REQUEST;

				bResult = DeviceIoControl(hDevice,
						IOCTL_WRITE_REGISTERS,
						(PVOID)&ioBlock,
						sizeof(IO_BLOCK),
						NULL,
						0,
						&nBytes,
						NULL);

				if (bResult != TRUE) {
					threadParam->bInThread = FALSE;
					wsprintf (cc, "Requesting data transfer on ");
					strcat( cc, threadParam->pipe_name);
					strcat( cc, " failed! Aborted.");
					testDlg->MessageBox((LPCSTR)cc, "Test Error");
					break;
				}

				time0 = timeGetTime();
				bResult = WriteFile(hFile,
							  threadParam->pcIoBuffer,
							  threadParam->uiLength,
							  &nBytes,
							  NULL);

				time1 = timeGetTime();
				if (bResult != TRUE) {
					DeviceIoControl(hFile,
						IOCTL_D12_RESET_PIPE,
						0,
						0,
						0,
						0,
						&nBytes,
						NULL);
					threadParam->bInThread = FALSE;
					wsprintf (cc, "Writing ");
					strcat( cc, threadParam->pipe_name);
					strcat( cc, " failed! Aborted.");
					testDlg->MessageBox((LPCSTR)cc, "Test Error");
					break;
				}

⌨️ 快捷键说明

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