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

📄 ks95mspi.c

📁 ks8995m5口交换机启动eeprom配置源码
💻 C
字号:
//--------------------------------------------------------------------------
//  
//  KS95MSPI.C  (c) Copyright 2002 Micrel-Kendin Operations
//  
//--------------------------------------------------------------------------


#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>

#include "ks95mspi.h"

//-----------------------------------------------------------------------------------
// Modification History:
//
// 1. 04/09/02 v1.6 Added x command. It repeats the last command.
//
//-----------------------------------------------------------------------------------



// Global Variables

// Point to current register for read operations. Next-20-register commands 
// start from igRRegNdx+1
int igRRegNdx;  

// Point to current register for write operations. Next-20-register commands 
// start from igRRegNdx+1
int igWRegNdx;  

// Global variable for MIB overflow indication
int giMIBOverflow = FALSE; 

//
//	Every MIB reads will be accumulated into this table.
//
unsigned long ulMIB[PORTTOTAL][MIBCOUNTEROFFSET];

//
//  This array holds accumlated counter values
//
unsigned long ulMIBDroppedPacket[DROPPEDMIBTOTAL];

//
// This array holds the previous counter values. Compare
// the current counter readings again the previou values
// can determine whether overflow happened.
//
unsigned long ulMIBDroppedPacketPrev[DROPPEDMIBTOTAL];

//
// Previous commad
//
char sPrevKeybuf[ MAXCMDLINELEN + 1 ] = "";  // Command buffer for interactive mode
//
// Command tabls
//
// Expand the table entry to add new commands. The first field is the command string, the second
// field is the function that process the command. The function takes three integers as parameters.
//      
//  
//

//
//  Dump commands

//
cmdtable DumpCmds [] = {
	{ "at", DumpAnalogTestReg },
	{ "drop", DumpDroppedPacketMIB },
	{ "dt", DumpDigitalTestReg },
	{ "dtab", DumpDynaMACTable },
	{ "g", DumpGlobalReg },
	{ "mac", DumpMAC },
	{ "mib", DumpMIB },
	{ "all", DumpMIBAll},
	{ "n", DumpNext20Reg },
	{ "p", DumpPortCtlReg },
	{ "r", DumpReg },
	{ "stab", DumpStaticMACTable },
	{ "tos", DumpTOSReg },
	{ "vtab", DumpVLANReg },
	{ 0, 0 }
};


//
//  Read Commands
//
cmdtable ReadCmds [] = {
	{ "mib", ReadMIB },
	{ "n", ReadNextReg },
	{ "r", ReadReg },
	{ "cnt", ReadCnt },
	{ 0, 0 }
};

//
//  Write Commands
//
cmdtable WriteCmds [] = {
	{ "n", WriteToNextReg },
	{ "r", WriteToReg },
	{ "stab", WriteToStaticMACTable },
	{ "vtab", WriteToVLANTable },
	{ 0, 0 }
};


//
//  Update Commands
//
acmdtable UpdateCmds [] = {
	{ "vtab", UpdateVLANTab },
	{ "stab", UpdateSMACTab },
	{ 0, 0 }
};

//
//  MIB counter names
//
char *MIBNames[] = {
		"RxLoPriorityByte",
		"RxHiPriorityByte",
		"RxUndersizePkt",
		"RxFrgaments",
		"RxOversize",
		"RxJabbers",
		"RxSymbolError",
		"RxCRCerror",
		"RxAlignmentError",
		"RxControl8808Pkts",
		"RxPausePkts",
		"RxBroadcast",
		"RxMulticast",
		"RxUnicast",
		"Rx64Octets",
		"Rx65to127Octets",
		"Rx128to255Octets",
		"Rx256to511Octets",
		"Rx512to1023Octets",
		"Rx1024to1522Octets",
		"TxLoPriorityByte",
		"TxHiPriorityBye",
		"TxLateCollision",
		"TxPausePkts",
		"TxBroadcastPkts",
		"TxMulticastPkts",
		"TxUnicastPkts",
		"TxDeferred",
		"TxTotalCollision",
		"TxExcessiveCollision",
		"TxSingleCollision",
		"TxMultipleCollision",
		""
};


#ifdef DOSBOX
unsigned int  uParallelBase = 0x378;  // Default parallel port base    
unsigned short  uSPICPort; //= uParallelBase + 2;
unsigned short  uSPIDPort; //= uParallelBase;
unsigned short  uSPIQPort; //= uParallelBase + 1;
unsigned short  uSPISPort; //= uParallelBase + 2;
#endif


//--------------------------------------------------------------------------
//  
//  void ConvertToLowerRemoveExtraBlanks
//  
//  Description:
//		Convert the input string lower case and remove excess blanks.
//
//  Parameters:
//      char        *sCmd       Input string to be converted
//
//  Return Vlaue:   
//      None
//  
//--------------------------------------------------------------------------
void ConvertToLowerRemoveExtraBlanks ( char *sCmd )
{

	char	*sPtr = sCmd;   // Pointer to the input string
	int		i = 0;          // Index
	
	
	// Remove leading blanks
	while ( *sPtr == BLANK )
		sPtr++;

	while ( *sPtr )
	{

		if ( isupper ( *sPtr ) )
			*sPtr = tolower ( *sPtr );

		sCmd [ i ] = *sPtr++;

		// Remove trailing blanks
		if ( sCmd [ i ] == BLANK )
			while ( *sPtr == BLANK )
				sPtr++;

		i++;
	}

    //
    //  Command lines from file have 0x0a at the end
    //
	if ( sCmd [ i - 1] == BLANK || sCmd [ i -1 ] == 0x0a )
		sCmd [ i - 1] = '\0';
	else 
		sCmd [ i ] = '\0';

	return;
}


//--------------------------------------------------------------------------
//  
//  int	GetHex
//  
//  Description:
//		This function converts a string to hexdecimal value. It performs sanity
//		checking to the input string.
//
//  Parameters:
//		char		    *sHexStr	Input string to be converted
//
//		unsigned int	*uHex 		Point to converted value
//  
//  Return Vlaue: 
//      0 if successfully converted, others error
//  
//--------------------------------------------------------------------------
int	GetHex ( char *sHexStr, unsigned int *uHex )
{

	unsigned int	uStrlen;	// String length 
	unsigned int	uTemp;      // Temporary variable holding the converted value
	unsigned int	i;          // Loop index

	uStrlen = strlen ( sHexStr );

	// We can get up to 8 hexdecimal digits for 32-bit variable
	if ( uStrlen < 1 || uStrlen > 8 )
		return FAIL;

	// Check to see if there is any non-hex character
	for ( i = 0; i < uStrlen; i++ )
	{
		if ( !isxdigit ( sHexStr [i] ) )
			return FAIL;
	}

	if ( sscanf ( sHexStr, " %x", &uTemp ) )
	{
		*uHex = uTemp;

		return SUCCESS;
	}

	return FAIL;

}


//--------------------------------------------------------------------------
//  
//  char *GetValue
//  
//  Description:
//		Convert numeric string to decimal value.
//
//  Parameters:
//		char		    *sBuf	    Input string to be converted. It points to
//                                  the blank in front of the parameter.
//
//		int	            *iValue 	Point to converted value
//  
//  Return Vlaue: 
//      Pointer to next parameter if there is one. 0 if there is no more parameters.
//  
//--------------------------------------------------------------------------
char *GetValue ( char *sBuf, int *iValue )
{

	char * sPtr;            // Pointer to a string
	unsigned int uTemp;   // Temporary variable

    //
    //  Safty
    //
	if ( !sBuf )
	{
		*iValue = FAIL;

		return NULL;
	}

	sPtr = strchr ( sBuf + 1, BLANK );

	if ( sPtr ) //there is another one after this
	{
        // Make the sting null ended.
		*sPtr = '\0';

		if ( GetHex ( sBuf + 1, &uTemp ) == SUCCESS )
		{
			*iValue = uTemp;
		}
		else
		{
			*iValue = NOPARAMETER;

		}

		*sPtr = BLANK; // Restore BLANK

	}
	else
	{
		if ( GetHex ( sBuf + 1, &uTemp ) == SUCCESS ) // Got good value
			*iValue = uTemp;
			
		else
			*iValue = NOPARAMETER;

	}
		
	return sPtr;
}


//--------------------------------------------------------------------------
//  
//  int  ElapseTime
//  
//  Description:
//		Get the number of clock ticks.
//
//  Parameters:
//      int     iOn     1 for staring the ticking, 0 for the getting elapse ticks
//  
//  Return Vlaue: 
//      Number of ticks
//  
//--------------------------------------------------------------------------
int  ElapseTime ( int iOn )
{
#ifdef LINUX
	struct tms t_start, t_end;
#endif
	static clock_t start, end;

	if ( iOn )
	{
		start = 
            
#ifdef LINUX            
            times (&t_start);
#endif

#ifdef DOSBOX
            clock (); 
#endif
	}
	else
	{
		end = 

#ifdef LINUX            
            times (&t_end);
#endif

#ifdef DOSBOX
            clock ();
#endif

		return ( end - start );
	}

	return 0;

}


//--------------------------------------------------------------------------
//  
//  int main
//  
//  Description:
//		main routine. Script mode if the argument count is 2, 
//      otherwise it is interactive mode.
//
//      The first argument is the scrip file name.
//
//  Parameters:
//      int     argc        argument count
//      char    *argv[]     arguments
//
//  
//  Return Vlaue: 
//      Doesn't matter
//  
//--------------------------------------------------------------------------
int main ( int argc, char *argv[] )
{

#define SCRIPTRESULTFILE    "ks8995m.out"   // This is the execution log for the scrip mode

	FILE * pInFile = NULL;      // Input file
    FILE * pOutFile = NULL;     // Execution log

	char acBuffer[ MAXLINE ] ;  //  Command buffer for script mode

	char sKeybuf[ MAXCMDLINELEN + 1 ];  // Command buffer for interactive mode

#ifdef DOSBOX

	//  Read in the configurations from the file
    //
    //  Get parallel port base address from configuration file
    //
	if ( ProcessConfig () )
	{
		printf ("\nIncorrect configuration setting");

		exit (1);
	}

    uSPICPort = uParallelBase + 2;
    uSPIDPort = uParallelBase;
    uSPIQPort = uParallelBase + 1;
    uSPISPort = uParallelBase + 2;

#endif

    if (argc == 2) // Scipt mode
    {

        // Open script file
	    pInFile = fopen( argv[ 1 ], "r" ) ;
	
	    if ( !pInFile )
	    {
	        printf( "\nScript file \"%s\" open error\n", argv[1] ) ;
	        exit(1) ;
	    }

        // Open log file
        pOutFile = fopen( SCRIPTRESULTFILE, "w" ) ;

	    if ( !pOutFile )
	    {
	        printf( "\nScript output file \"%s\" open error\n", SCRIPTRESULTFILE ) ;

            /* Close stream */
			//if ( pInFile )
   			//	fclose( pInFile );
	    
	        //exit(1) ;
	    }

#ifdef LINUX
        //
        //  Init Dragonball SPIM
        //
	    if ( InitSPIM () )
#endif

#ifdef DOSBOX
	    if (( GetChipID() ) != KS8995MID )
#endif
	    {
	        printf ("\n Failed to initialize SPIM\n");

#ifdef DOSBOX
			if ( uParallelBase != 0x278 && uParallelBase != 0x378 &&
				 uParallelBase != 0x3bc )
            {
                if ( pOutFile )
				    fprintf ( pOutFile, "\n!!! %x is not a typical parallel port address",
						uParallelBase);
            }
			else
				if ( pOutFile )
                    fprintf ( pOutFile, "\nParallel port base address = 0x%x", uParallelBase );
#endif

       		if ( pInFile && pOutFile )
			{
                fprintf ( pOutFile, "\n Failed to initialize SPIM\n", acBuffer );
				fclose ( pInFile );
				fclose ( pOutFile );
			}

			exit (1);
	    }

	    while (fgets( acBuffer, MAXLINE, pInFile ))
	    {
            // Skip all comment and NULL lines
            if ( acBuffer[0] != '#' && acBuffer[0] != '\0' )   
            {
		        // Shaping the command string. converting the string to all lowercase and removing the extra blanks.
		        ConvertToLowerRemoveExtraBlanks ( acBuffer );

				if ( strlen ( acBuffer ) )
				{

					switch ( SPICmds ( acBuffer ) )
					{
						case SUCCESS :
							if ( pInFile && pOutFile )
								fprintf ( pOutFile, "\n%s", acBuffer );
							break;

						case CMDERROR :
							if ( pInFile && pOutFile )
								fprintf ( pOutFile, "\nCommand error => %s", acBuffer );

							printf ( "\nCommand error => %s", acBuffer );
							break;

						case PARMERROR:

							if ( pInFile && pOutFile )
								fprintf ( pOutFile, "\nParameter error = > %s", acBuffer );

							printf ( "\nParameter error => %s", acBuffer );
							break;

						default :
							break;
					}
				}
            }
        }

		printf ("\n");

        if ( pInFile )
            fclose ( pInFile );

        if ( pInFile )
            fclose ( pOutFile );

        exit(1);
    }


    //
    //
    //  Interactive mode
    //
    //

#ifdef LINUX
	if ( InitSPIM () )
#endif

#ifdef DOSBOX
	if (( GetChipID() ) != KS8995MID )
#endif
	{
	    printf ("\n Failed to initialize SPIM\n");

#ifdef DOSBOX
		if ( uParallelBase != 0x278 && uParallelBase != 0x378 &&
			 uParallelBase != 0x3bc )
			 printf ("\n!!! %x is not a typical parallel port address",
					uParallelBase);
		else
			printf ("\nParallel port base address = 0x%x", uParallelBase );
#endif

		exit (1);
	}

	printf("\nKS8995M SPI Program %s (c)Copyright 2002 Micrel-Kendin Operations\n", VERSIONNO );

	while (1)
	{
		printf ("\n\nSPI>" );

#ifdef LINUX
		printf ("\r" );
		
#endif

		gets ( sKeybuf );

		//printf ("\nthe command is %s\n", sKeybuf );

		// Shaping the command string. converting the string to all lowercase and removing the extra blanks.
		ConvertToLowerRemoveExtraBlanks ( sKeybuf );


		if (!strcmp ( sKeybuf, "quit") || !strcmp (sKeybuf, "q"))
			break;

		if ( !strcmp ( sKeybuf, "x" ) &&  strlen (sPrevKeybuf) )  // Repeat command
		{  
			strcpy (sKeybuf, sPrevKeybuf);	// Use previous stored command
			printf ("\nRepeat command %s\n", sKeybuf );
		}


		switch ( SPICmds ( sKeybuf ) )
		{
			case CMDERROR :
				DisplayCmdList ();
				break;
			case PARMERROR:
				printf ("\nInvalid parameter(s)");
				break;
			default :
                // Preserve the previous command
                strcpy (sPrevKeybuf,  sKeybuf);
			break;
		}

	}

}


⌨️ 快捷键说明

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