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

📄 samplib.cpp

📁 The PCI Local bus concept was developed to break the PC data I/O bottleneck and clearly opens the d
💻 CPP
字号:
///////////////////////////////////////////////////////////////////////////////
//	samplib.cpp
//
//	Description:
//		Main entry point for the DLL sample program for WDM driver.
//		The main function of this file is to open the driver to obtain the 
//		handle and pass on the control to the main loop in sisamplibf.cpp.
//
//	Revision History:
//		2002-03-07: mik
//			Created.
//		2004-03-15: RES (Stermon)
//          Modified for NVRAM Only
///////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include "samplib.h"

///////////////////////////////////////////////////////////////////////////////
// globals 

bool gStarted = false;

AMCCDevice 
	gAMCCDev[4], 
	*pAMCCDev[] = { &gAMCCDev[0], &gAMCCDev[1], &gAMCCDev[2], &gAMCCDev[3] };

///////////////////////////////////////////////////////////////////////////////

#ifdef WIN32

void ShowMessage( char *msg, char *header, UINT32 type )
{
	MessageBox(NULL, msg, header, MB_OK);
}

BOOL WINAPI DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
{
	switch (fdwReason)
	{
	case DLL_PROCESS_ATTACH:
		return SI_DLL_Entry();
		
	case DLL_THREAD_ATTACH:
		/* Called each time a thread is created in a process that has
		already loaded (attached to) this DLL. Does not get called
		for each thread that exists in the process before it loaded
		the DLL.

		Do thread-specific initialization here.
		*/
		break;

	case DLL_THREAD_DETACH:
		/* Same as above, but called when a thread in the process
		exits.

		Do thread-specific cleanup here.
		*/
		break;

	case DLL_PROCESS_DETACH:
		/* Code from _WEP inserted here.  This code may (like the
		LibMain) not be necessary.  Check to make certain that the
		operating system is not doing it for you.
		*/
		SI_DLL_Exit();		
		break;

	}	// end switch

	return true;
}
#endif

///////////////////////////////////////////////////////////////////////////////
// Linux will use these

#ifdef LINUX

void ShowMessage( char *msg, char *header, UINT32 type )
{
	FILE *fp;
	char *filename = "/tmp/samplib.txt";
	
	// if type == 0, then clear file before writing.
	if (type == 0)
		fp = fopen(filename, "wt");
	else
		fp = fopen(filename, "at");
		
	if (fp==NULL)
		return;
		
	fprintf(fp, "%s\n%s\n\n", header, msg);
	
	fclose(fp);
}

void _init()
{
	SI_DLL_Entry();
}

void _fini()
{
	SI_DLL_Exit();
}
#endif

///////////////////////////////////////////////////////////////////////////////

bool SI_DLL_Entry()
{
	INT32 board, error;
			 
	if (gStarted)
		return true;

	// only do this code when the 1st process attaches
	gStarted = true;

#ifdef LINUX
	// clear message file before starting
	ShowMessage("", "", 0);	// clear 
#endif	

	// check for up to four boards
	for(board=0;board<4;board++)
	{
		error = SI_AMCC_OpenDriver( board, pAMCCDev[board] );
		if (error != e_Err_NoError)
			break;
	} // end for loop

	if (board == 0)
	{
		ShowMessage( "No boards found.", "sisamplib", 1 );
		gStarted = false;
		return false;
	}
	return true;
}

///////////////////////////////////////////////////////////////////////////////

INT32 SI_DLL_Exit()
{
	int board;

	// windows is guaranteed to have it already started, linux is not.
	if (!gStarted)
		if (! SI_DLL_Entry() )
			return e_Err_DeviceNotFound;

	for(board=0; board<4; board++)
		// When done, close the handle to the driver
		SI_AMCC_CloseDriver( pAMCCDev[board] );

	return true;   // successful
}

///////////////////////////////////////////////////////////////////////////////

DLLReturnType  
Sample_ReadConfigurationSpace
(
	INT32 board, 
	UINT32 count, 
	UINT32 offset, 
	UINT32 *data
)
{
	// windows is guaranteed to have it already started, linux is not.
	if (!gStarted)
		if (! SI_DLL_Entry() )
			return e_Err_DeviceNotFound;

	return 
		SI_AMCC_ReadPCI_ConfSpace
		(
			pAMCCDev[board], 
			count, offset, data
		);
}

///////////////////////////////////////////////////////////////////////////////

#define NVRAM_WRITE_MAX_WAIT 15

DLLReturnType  
Sample_WriteNVRAM
(
	INT32 board, 
	UINT32 addrNVRam, 
	UINT32 *data
)
{

	int ret,r_ret,wait;
	UINT32 r_data;

	// windows is guaranteed to have it already started, linux is not.
	if (!gStarted)
		if (! SI_DLL_Entry() )
			return e_Err_DeviceNotFound;

////////////////////////////////////////////
// Original code
////////////////////////////////////////////
//	return 
//		SI_AMCC_WritePCI_NVWord
//		(
//			pAMCCDev[board], 
//			1, addrNVRam, data
//		);

////////////////////////////////////////////
// New code
////////////////////////////////////////////
	ret = SI_AMCC_WritePCI_NVWord
		(
			pAMCCDev[board], 
			1, addrNVRam, data
		);

	if( ret == e_Err_NoError ) {
		if( *data == 0x000000ff ) {
			Sleep( NVRAM_WRITE_MAX_WAIT );
		}
		else {
			wait = 0;
			while( wait <= NVRAM_WRITE_MAX_WAIT ) {
				Sleep( 1 );
				r_ret = Sample_ReadNVRAM( board, addrNVRam, &r_data );
				if( r_ret == e_Err_NoError ) {
					if( r_data == *data )
						break;
				}
				wait++;
			}
		}
	}

	return ret;
////////////////////////////////////////////
// End of New code
////////////////////////////////////////////
}

///////////////////////////////////////////////////////////////////////////////

DLLReturnType  
Sample_ReadNVRAM
(
	INT32 board, 
	UINT32 addrNVRam, 
	UINT32 *data
)
{
	// windows is guaranteed to have it already started, linux is not.
	if (!gStarted)
		if (! SI_DLL_Entry() )
			return e_Err_DeviceNotFound;

	return 
		SI_AMCC_ReadPCI_NVWord
		(
			pAMCCDev[board], 
			1, addrNVRam, data
		);
}

⌨️ 快捷键说明

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