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

📄 sicommon_amcc_win.cpp

📁 The PCI Local bus concept was developed to break the PC data I/O bottleneck and clearly opens the d
💻 CPP
📖 第 1 页 / 共 2 页
字号:
///////////////////////////////////////////////////////////////////////////////
//	sicommon_amcc.cpp
//
//	Description:
//		Common hardware access routines for AMCC. These calls are AMCC specific.
//		These are aka API functions.
//
//	Revision History:
//		2002-06-14: AV & LEY
//			Created
//		2004-03-15: RES (Stermon)
//          Modified for NVRAM Only
///////////////////////////////////////////////////////////////////////////////

#include <stdio.h>		// for sprintf
#include "sicommon_amcc.h"

#include <winioctl.h>
#include "siddkapi_win.h"

///////////////////////////////////////////////////////////////////////////////
// Performance timers. These globals are set used each time an API function is 
//	entered and exited. Read the difference to determine the relative times
//	of each API function execution. They can give numbers such as words
//	transferred per second, etc. Note that these values are only valid on
//	successful completion of the function. For error conditions, these are
//	invalid. Although you can get these values throuhg use of extern and 
//	perform subtraction yourself, use SIHW_AMCC_ConvertBenchmarkToSeconds 
//	instead to get the actual time.

UINT32 gSI_AMCC_BenchmarkTime0, gSI_AMCC_BenchmarkTime1;

///////////////////////////////////////////////////////////////////////////////
//	INT32 SIHW_AMCC_GetBenchmarkTime
//
//	Description:
//		Returns the benchmark time difference in seconds. Note that the 
//		parameters are upper UINT32 and lower UINT32 since the benchmark time
//		is a 64 bit entity.
//
//	Parameters:
//		UINT32 elapsedTime	: 32 bit number for number of msec elapsed.

INT32 SIHW_AMCC_ConvertBenchmarkToSeconds ( UINT32 *elapsedMsec )
{
	*elapsedMsec = gSI_AMCC_BenchmarkTime1 - gSI_AMCC_BenchmarkTime0;

	return e_Err_NoError;
}

///////////////////////////////////////////////////////////////////////////////
// INT32 SI_AMCC_Sleep
//
// Description:
// 		Puts this process in sleep for sleepMSec
//
// Parameters:
// 		UINT32 sleepMSec	: number of milliseconds to sleep.

INT32 SI_AMCC_Sleep( UINT32 sleepMSec )
{
	Sleep(sleepMSec);
	return e_Err_NoError;
}

///////////////////////////////////////////////////////////////////////////////
//	INT32 SI_AMCC_OverlappedCreate
//
//	Description:
//		
//
//	Parameters:
//		

INT32 SI_AMCC_OverlappedCreate(PAMCCDevice pAMCCDev)
{
	// thread is invalid when null (damn M$)
	pAMCCDev->hCallbackThread = NULL;	

	// clear out all current handles
	pAMCCDev->hCallbackEvent = INVALID_HANDLE_VALUE;

	pAMCCDev->pOverlappedMisc = NULL;
	pAMCCDev->hEventMisc = INVALID_HANDLE_VALUE;

	pAMCCDev->pOverlappedBMWrite = NULL;
	pAMCCDev->hEventBMWrite = INVALID_HANDLE_VALUE;

	pAMCCDev->pOverlappedBMRead = NULL;
	pAMCCDev->hEventBMRead = INVALID_HANDLE_VALUE;

	// Create an event to be used for async transfers
	pAMCCDev->hEventBMRead =
		CreateEvent
		(
			NULL,	// security
			FALSE,	// manual reset
			FALSE,	// initial state is non-signaled 
			NULL	// name string
		);
	// the system didn't return a handle
	if ( pAMCCDev->hEventBMRead == INVALID_HANDLE_VALUE ) 
		return e_Err_WindowsHandle;

	// Set up the overlapped structure
	pAMCCDev->overlappedBMRead.hEvent = pAMCCDev->hEventBMRead;
	pAMCCDev->pOverlappedBMRead = &pAMCCDev->overlappedBMRead;

	// Create an event to be used for async transfers
	pAMCCDev->hEventBMWrite =
		CreateEvent
		(
			NULL,	// security
			FALSE,	// manual reset
			FALSE,	// initial state is non-signaled 
			NULL	// name string
		);
	// the system didn't return a handle
	if ( pAMCCDev->hEventBMWrite == INVALID_HANDLE_VALUE ) 
		return e_Err_WindowsHandle;

	// Set up the overlapped structure
	pAMCCDev->overlappedBMWrite.hEvent = pAMCCDev->hEventBMWrite;
	pAMCCDev->pOverlappedBMWrite = &pAMCCDev->overlappedBMWrite;

	// Create an event to be used for async transfers
	pAMCCDev->hEventMisc =
		CreateEvent
		(
			NULL,	// security
			FALSE,	// manual reset
			FALSE,	// initial state is non-signaled 
			NULL	// name string
		);
	// the system didn't return a handle
	if ( pAMCCDev->hEventMisc == INVALID_HANDLE_VALUE ) 
		return e_Err_WindowsHandle;

	// Set up the overlapped structure
	pAMCCDev->overlappedMisc.hEvent = pAMCCDev->hEventMisc;
	pAMCCDev->pOverlappedMisc = &pAMCCDev->overlappedMisc;
	
	// Create an event to be used for interrupt based callbacks
	pAMCCDev->hCallbackEvent = 
		CreateEvent
		(
			NULL,	// security
			FALSE,	// FALSE manual reset
			FALSE,	// initial state is non-signaled 
			NULL	// name string
		);

	// the system didn't return a handle
	if ( pAMCCDev->hCallbackEvent == INVALID_HANDLE_VALUE ) 
		return e_Err_WindowsHandle;

	return e_Err_NoError;
}

///////////////////////////////////////////////////////////////////////////////
//	INT32 SI_AMCC_OverlappedDestroy
//
//	Description:
//		
//
//	Parameters:
//		

INT32 SI_AMCC_OverlappedDestroy(PAMCCDevice pAMCCDev)
{
	// kill any existing thread
	if ( pAMCCDev->hCallbackThread != NULL )
		CloseHandle(pAMCCDev->hCallbackThread);
	// thread is invalid when null (damn M$)
	pAMCCDev->hCallbackThread = NULL;	

	// invalidate all pointers
	pAMCCDev->pOverlappedMisc = NULL;
	pAMCCDev->pOverlappedBMWrite = NULL;
	pAMCCDev->pOverlappedBMRead = NULL;

	// close all handles
	if ( pAMCCDev->hCallbackEvent != INVALID_HANDLE_VALUE )
		CloseHandle(pAMCCDev->hCallbackEvent);
	pAMCCDev->hCallbackEvent = INVALID_HANDLE_VALUE;

	if ( pAMCCDev->hEventMisc != INVALID_HANDLE_VALUE )
		CloseHandle(pAMCCDev->hEventMisc);
	pAMCCDev->hEventMisc = INVALID_HANDLE_VALUE;

	if ( pAMCCDev->hEventBMWrite != INVALID_HANDLE_VALUE )
		CloseHandle(pAMCCDev->hEventBMWrite);
	pAMCCDev->hEventBMWrite = INVALID_HANDLE_VALUE;

	if ( pAMCCDev->hEventBMRead != INVALID_HANDLE_VALUE )
		CloseHandle(pAMCCDev->hEventBMRead);
	pAMCCDev->hEventBMRead = INVALID_HANDLE_VALUE;

	return e_Err_NoError;
}

///////////////////////////////////////////////////////////////////////////////
//	INT32 SI_AMCC_OpenDriverWDM
//
//	Description:
//		Open driver for WDM
//
//	Parameters:
//		char driverName[]	: name of the driver (SIWDM).
//		int board			: which board to open. 0=first, 3=fourth, etc.
//		PAMCCDevice pAMCCDev	: handle to driver upon successful open.
// 

INT32 SI_AMCC_OpenDriverWDM
(
	char driverName[], UINT32 board, 
	PAMCCDevice pAMCCDev
)
{
	char driverNameTemp[256];

	sprintf(driverNameTemp, "\\\\.\\%s%d", driverName, board);

	// try to get a handle to it
	pAMCCDev->hDevice = 
		CreateFile
		(
			driverNameTemp,
			// if data goes both ways use READ and WRITE
			GENERIC_WRITE | GENERIC_READ,
			// for multiple program access to the device
			FILE_SHARE_WRITE | FILE_SHARE_READ,
			// Security attributes go here, but don't really apply to devices
			NULL,
			// A must for devices
			OPEN_EXISTING,
			//O-LAPPED now on EVERY call
			FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_OVERLAPPED,
			// template file, not used by the driver
			NULL
		);

	// if device exists, create a board object for it
	if( pAMCCDev->hDevice == INVALID_HANDLE_VALUE) 
		return e_Err_DeviceNotFound;

	pAMCCDev->hCallbackEventDriver = pAMCCDev->hCallbackEvent;

	return e_Err_NoError;
}

///////////////////////////////////////////////////////////////////////////////
//	INT32 SI_AMCC_OpenDriverNT4
//
//	Description:
//		Open driver for NT4
//
//	Parameters:
//		char driverName[]	: name of the driver (SIDRV).
//		int board			: which board to open. 0=first, 3=fourth, etc.
//		PAMCCDevice pAMCCDev	: handle to driver upon successful open.
// 

INT32 SI_AMCC_OpenDriverNT4
(
	char driverName[], UINT32 board, 
	PAMCCDevice pAMCCDev
)
{
	char driverNameTemp[256];

	sprintf(driverNameTemp, "\\\\.\\%s%d", driverName, board + 1);

	// try to get a handle to it
	pAMCCDev->hDevice = 
		CreateFile
		(
			driverNameTemp,
			// if data goes both ways use READ and WRITE
			GENERIC_WRITE | GENERIC_READ,
			// for multiple program access to the device
			FILE_SHARE_WRITE | FILE_SHARE_READ,
			// Security attributes go here, but don't really apply to devices
			NULL,
			// A must for devices
			OPEN_EXISTING,
			//O-LAPPED now on EVERY call
			FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_OVERLAPPED,
			// template file, not used by the driver
			NULL
		);

	// if device exists, create a board object for it
	if( pAMCCDev->hDevice == INVALID_HANDLE_VALUE) 
		return e_Err_DeviceNotFound;

	pAMCCDev->hCallbackEventDriver = pAMCCDev->hCallbackEvent;

	return e_Err_NoError;
}

///////////////////////////////////////////////////////////////////////////////
//	INT32 SI_AMCC_OpenDriverWin95
//
//	Description:
//		Opening 95 driver is very twisted.
//
//	Parameters:
//		char driverName[]	: name of the driver (SIDRV95.VXD).
//		int board			: which board to open. 0=first, 3=fourth, etc.
//		PAMCCDevice pAMCCDev	: handle to driver upon successful open.
// 
INT32 SI_AMCC_OpenDriverWin95
(
	char driverName[], UINT32 board, 
	PAMCCDevice pAMCCDev
)
{
	DWORD returnedBytes, numberOfBoardsFound = 0;				
	char driverNameTemp[256];

	sprintf(driverNameTemp, "\\\\.\\%s", driverName);
	// 1. see if the driver file is present
	pAMCCDev->hDevice = 
		CreateFile
		(
			driverNameTemp,	
			// if data goes both ways use READ and WRITE
			GENERIC_WRITE | GENERIC_READ,
			// for multiple program access to the device
			FILE_SHARE_WRITE | FILE_SHARE_READ,
			// Security attributes go here
			NULL,
			// A must for devices
			OPEN_EXISTING,
			//O-LAPPED now on EVERY call
			FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_OVERLAPPED, 
			// template file, not used by the driver
			NULL
		);
	if (pAMCCDev->hDevice == INVALID_HANDLE_VALUE)
		return e_Err_DeviceNotFound;

	// 2. Here call the VxD to see how many boards were found
	if
	(	!DeviceIoControl
		(
			pAMCCDev->hDevice,
			IOCTL_SHELDON_NUMBER_OF_BOARDS,
			NULL,
			0,
			&numberOfBoardsFound,
			sizeof(UINT32),
			&returnedBytes,
			NULL
		)
	)
	{
		CloseHandle(pAMCCDev->hDevice);

⌨️ 快捷键说明

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