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

📄 d12testdlg.cpp

📁 D12的驱动程序
💻 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)//从USB读数据
{  
	char temp [80],cc[180], c;
    BOOLEAN bResult = FALSE;
    ULONG nBytes = 0;
	struct CThreadParam * threadParam;
	HANDLE hFile, hDevice;
	CD12TestDlg* testDlg;
	int	Data;
	IO_BLOCK ioBlock;
	threadParam=(struct CThreadParam *)pParam;
	threadParam->bInThread = true;
	testDlg = threadParam->testDlg;
	int nTimes;		//add by zjl,count loop times

	nTimes = 0;
	threadParam->pcIoBuffer = (unsigned char *)malloc(threadParam->uiLength + 16); 
	
			hFile = open_file(threadParam->pipe_name);
		
	if(hFile == INVALID_HANDLE_VALUE){
		free(threadParam->pcIoBuffer);
		threadParam->pcIoBuffer = NULL;
		threadParam->bInThread = FALSE;
		strcpy(temp, "驱动程序打开失败! 不能写数据到 ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, "。");
		testDlg->MessageBox((LPCSTR)temp, "测试错误");
		return 0;
	}
	//start listen......
					hDevice = open_dev();
		
				ioBlock.uOffset = 0;
				ioBlock.uLength = 1;
				ioBlock.pbyData = (PUCHAR)&c;
				ioBlock.uIndex = GET_FIRMWARE_VERSION;
	while(threadParam->bInThread)
		{
				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, "固件版本不支持本测试。\r\n请升级仿真板固件。");
					testDlg->MessageBox((LPCSTR)cc, "固件不兼容", MB_ICONSTOP);
					testDlg->EndDialog(0);
					
					return 0;
				}
		threadParam->ulData[0] = D12_DRIVERLOAD;
		threadParam->ulData[1] = c;
		threadParam->bUpdate = TRUE;
		
			threadParam->threadFile1=hFile;
			bResult = ReadFile(hFile,
					threadParam->pcIoBuffer,
					threadParam->uiLength,
					&nBytes,
					NULL);
				Data=*(threadParam->pcIoBuffer);
//				threadParam->testDlg->m_Recieved_Data=((float)Data/255)*5;
				sprintf(temp," %x H",Data);
				threadParam->testDlg->m_Recieved_Data=temp;

			nTimes++;		//count 16384 times,then stop
			if(nTimes == 16384)
			{
				testDlg->MessageBox("count end", "count end");
				break;
			}
		}

	CloseHandle(hFile);
	CloseHandle(hDevice);		
	free(threadParam->pcIoBuffer);
	threadParam->pcIoBuffer = NULL;
	
	threadParam->bInThread = FALSE;
	
    return 0; 
} 

UINT GenericOut(void * pParam)//往USB写数据
{  
    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, "驱动程序打开失败! 不能写数据到 ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, "。");
		testDlg->MessageBox((LPCSTR)temp, "测试错误");
		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, "写 ");
			strcat( cc, threadParam->pipe_name);
			strcat( cc, " 失败! 已放弃.");
			testDlg->MessageBox((LPCSTR)cc, "测试错误");
		}/* 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];
    BOOLEAN bResult = FALSE;
    ULONG nBytes = 0;
	struct CThreadParam * threadParam;
	CD12TestDlg* testDlg;
	HANDLE hFile, hDevice;
	DWORD time0, time1;
	IO_BLOCK ioBlock;
	IO_REQUEST ioRequest;
//	int nTimes;				// count read times
	CString tmps;			//add zjl
	unsigned int i;		//add zjl
//	FILE *F_data;
//	nTimes = 0;
//	F_data = fopen("usbdata.dat","w+");

	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, "驱动程序打开失败! 不能从 ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, " 中读出数据。");
		testDlg->MessageBox((LPCSTR)temp, "测试错误");
		return 0;
	}
	
	hDevice = open_dev();
	if(hDevice == INVALID_HANDLE_VALUE) {
		CloseHandle(hFile);
		strcpy(temp, "设备句柄打开失败! 不能读 ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, "。");
		testDlg->MessageBox((LPCSTR)temp, "Test Error");
		return 0;
	}
	
    threadParam->pcIoBuffer = (unsigned char *)malloc (threadParam->uiLength + 16); 
	//init pcIoBuffer
//	n = threadParam->uiLength;

	threadParam->ulCount = 0;
	threadParam->total_time = 0;
	threadParam->cur_rate = 0;
	threadParam->max_rate = 0;
	
    if (hFile != INVALID_HANDLE_VALUE) {
		
		threadParam->bInThread = TRUE;
		threadParam->threadFile1=hFile;
		testDlg->m_InStartBtn.EnableWindow(FALSE);
		testDlg->m_InStopBtn.EnableWindow(TRUE);
		testDlg->m_LoopStartBtn.EnableWindow(FALSE);
		testDlg->m_LoopStopBtn.EnableWindow(FALSE);
		threadParam->Last_time=timeGetTime();
	//-----------------------------------------
			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, "在 ");
				strcat( cc, threadParam->pipe_name);
				strcat( cc, " 上请求数据传送失败! 已放弃。");
				testDlg->MessageBox((LPCSTR)cc, "测试错误");
//				break;
			}*/		
	//------------------------------------------
		
		do{

			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;
				threadParam->total_time += (time1-(threadParam->Last_time));
				threadParam->Last_time=time1;
//				if(time1 != time0 && (time1-time0-1)!=0)
//				{
//					threadParam->cur_rate = (nBytes*1000)/(time1-time0);
//					if(threadParam->cur_rate > threadParam->max_rate)
//						threadParam->max_rate = threadParam->cur_rate;
					threadParam->bUpdate = TRUE;
//				}
/*	//-------Add code to show the readBuf data to the Edit_Window
				
		//		UpdateData(true);
		//		tmps.Format("Length=%d:",threadParam->uiLength);
				tmps.Format("Length=%d:",nBytes);
				testDlg->m_ReadPipe2 += tmps;
				for(i=0;i<threadParam->uiLength;i++)
				{
					tmps.Empty();
					if(threadParam->pcIoBuffer[i] > 15)
						tmps.Format("%x ",threadParam->pcIoBuffer[i]);
					else tmps.Format("0%x ",threadParam->pcIoBuffer[i]);
					testDlg->m_ReadPipe2 += tmps;
				}
				testDlg->m_ReadPipe2 += "<End>\r\n\r\n";
				if(testDlg->m_ReadPipe2.GetLength() > 100000) testDlg->m_ReadPipe2.Empty();
		//		UpdateData(false);
*/	//----------------End-------------------------------
			}
			if(threadParam->ulCount >= 1048576) 
				{
					threadParam->bInThread = FALSE;
					testDlg->MessageBox("read end !","Read End");
//					fwrite(threadParam->pcIoBuffer,threadParam->ulCount,1,F_data);
				}
		}while(threadParam->bInThread == TRUE);
//		fclose(F_data);		
    }// if valid hDevice
	
	//The thread terminated by itself, delete resources
	
	CloseHandle(hFile);
	CloseHandle(hDevice);
	threadParam->threadFile1=NULL;
	threadParam->hThread=NULL;
	
	free(threadParam->pcIoBuffer);
	threadParam->pcIoBuffer = NULL;
	
	testDlg->m_InStartBtn.EnableWindow(TRUE);
	testDlg->m_InStopBtn.EnableWindow(FALSE);

	if (testDlg->m_MainWrite.bInThread==FALSE) {
		testDlg->m_LoopStartBtn.EnableWindow(TRUE);
	}
    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, "驱动程序打开失败! 不能写数据到 ");
		strcat(temp, threadParam->pipe_name);
		strcat(temp, " 。");
		testDlg->MessageBox((LPCSTR)temp, "测试错误");
		return 0;
		
	}
	
	hDevice = open_dev();
	if(hDevice == INVALID_HANDLE_VALUE)
	{
		CloseHandle(hFile);
		strcpy(temp, "设备句柄打开失败! 不能写入 ");
		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;
		threadParam->threadFile1=hFile;
		testDlg->m_OutStartBtn.EnableWindow(FALSE);
		testDlg->m_OutStopBtn.EnableWindow(TRUE);
		testDlg->m_LoopStartBtn.EnableWindow(FALSE);
		testDlg->m_LoopStopBtn.EnableWindow(FALSE);
		
		if(threadParam->uiLength == 0)
		{
		}
		else
		{
			threadParam->Last_time=timeGetTime();
			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);
					wsprintf (cc, threadParam->pipe_name);
					strcat( cc, " 请求数据传送失败! 已放弃。");
					testDlg->MessageBox((LPCSTR)cc, "测试错误");
					break;
				}
				
				time0 = timeGetTime();
				bResult = WriteFile(hFile,
					threadParam->pcIoBuffer,
					threadParam->uiLength,
					&nBytes,

⌨️ 快捷键说明

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