📄 kscmds.c
字号:
//--------------------------------------------------------------------------
//
// KSCMDS.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"
#ifdef DOSBOX
#include <conio.h>
#endif
//-----------------------------------------------------------------------------------
// Modification History:
//
// 1. v1.6 Fixed the problem that ddtab command cannot display the last entry in the table
// 1. v1.7 Fixed revision number display problem.
//
//-----------------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
//
//
// This module contains routines for command handling. They are independent to low
// level hardware routines. Both Dragonball and parallel port emulation use these
// command handling routines.
//
//
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
// int GetChipID
//
// Description:
//
// Command:
//
// id Read KS8995M chip ID from register 0
//
// Assuming this function is called after SPIM is initialized
//
// Parameters:
// None
//
// Return Vlaue:
// Chip ID (0x95).
//
//--------------------------------------------------------------------------
int GetChipID ()
{
return ( ReadData ( KS8995M_CHIP_ID0 ) );
}
//--------------------------------------------------------------------------
//
// void Start95MSwitch
//
// Description:
//
// Command:
//
// s Start KS8995M switch
//
//
// Parameters:
// None
//
// Return Vlaue:
// None
//
//--------------------------------------------------------------------------
void Start95MSwitch ()
{
WriteData ( KS8995M_CHIP_ID1, 1 );
printf ("\nKS8995M has been started!");
return;
}
//--------------------------------------------------------------------------
//
// int DumpAnalogTestReg
//
// Description:
//
// Command:
//
// dat Dump analog test registers.
//
// Parameters:
// int elem1 NOPARAMETER
// int elem2 NOPARAMETER
// int elem3 NOPARAMETER
//
// Return Vlaue:
// None
//
//--------------------------------------------------------------------------
int DumpAnalogTestReg ( int elem1, int elem2, int elem3 )
{
int iNdx;
if ( elem1 != NOPARAMETER || elem2 != NOPARAMETER || elem3 != NOPARAMETER )
return PARMERROR;
// Control registers
for ( iNdx = ATESTREGBEGIN; iNdx < ATESTREGEND; iNdx++ )
printf ( "\nTOS Digital Testing Control Register (0x%02x) = 0x%02x", iNdx, ReadData ( (BYTE) iNdx ));
// Status register
printf ( "\nTOS Digital Testing Status Register (0x%02x) = 0x%02x", iNdx, ReadData ( (BYTE) iNdx ) );
return SUCCESS;
}
//--------------------------------------------------------------------------
//
// unsigned long GetMIBCounter
//
// Description:
// Call low level routines to read MIB counter values.
//
// Parameters:
// bPort Port number, starts with 1, not 0
// BYTE bMIBNdx Index to MIB counter
//
// Return Vlaue:
// Counter value
//
//--------------------------------------------------------------------------
unsigned long GetMIBCounter ( BYTE bPort, BYTE bMIBNdx )
{
unsigned long ulTemp;
WriteData ( INDIRECTCTL1, INDIRECTREAD | INDIRECTMIBCNT );
WriteData ( INDIRECTCTL2, (BYTE) (bMIBNdx + ( bPort - 1 ) * MIBCOUNTEROFFSET) );
do {
ulTemp = ( ReadData ( INDIRECTDATABASE + 5 ) ) << 24 ;
if ( ulTemp & MIBOVERFLOWMASK )
{
giMIBOverflow = TRUE;
}
} while ( !(ulTemp & MIBVALIDMASK ) );
ulTemp += ReadData ( INDIRECTDATABASE + 6 ) << 16;
ulTemp += ReadData ( INDIRECTDATABASE + 7 ) << 8;
ulTemp += ReadData ( INDIRECTDATABASE + 8 );
ulTemp &= ~( MIBVALIDMASK | MIBOVERFLOWMASK);
return ulTemp;
}
//--------------------------------------------------------------------------
//
// void GetDroppedPacketMIB
//
// Description:
// Read dropped packet MIB counters
//
// Parameters:
// int iGenerateOutput 1 - Display results 0 - not to display results
//
// Return Vlaue:
// None
//
//--------------------------------------------------------------------------
void GetDroppedPacketMIB ( int iGenerateOutput )
{
#define ELAPSE 30 // 30 Clock ticks; 100 ticks per second
#define DROPPEDPACKETMIBMASK 0x0000ffff // 16-bit mask
unsigned long ulTemp; // To hold a 30-bit integer
int iNdx; // Loop index
ElapseTime ( TRUE ); // Start the ticks
while ( ElapseTime ( FALSE ) < ELAPSE );
for ( iNdx = 0; iNdx < DROPPEDMIBTOTAL; iNdx++ )
{
WriteData ( INDIRECTCTL1, INDIRECTDROPMIBCNT | INDIRECTREAD | INDIRECTMIBCNT );
WriteData ( INDIRECTCTL2, (BYTE) iNdx );
// It is a 16-bit counter
ReadData (INDIRECTDATABASE + 6);
ulTemp = ReadData (INDIRECTDATABASE + 7) << 8;
ulTemp += ReadData (INDIRECTDATABASE + 8);
if ( ulTemp < ulMIBDroppedPacketPrev [ iNdx ] ) // Overflow
{
ulMIBDroppedPacket [ iNdx ] += ( 1 << 16 ); // 16-bit
}
ulMIBDroppedPacket [ iNdx ] &= (unsigned long )~DROPPEDPACKETMIBMASK;
ulMIBDroppedPacket [ iNdx ] |= ulTemp;
ulMIBDroppedPacketPrev [ iNdx ] = ulTemp;
if ( iGenerateOutput )
{
if ( iNdx < 5 )
printf ( "\nPort %.1d Tx Drop Packets = %lu", iNdx + 1, ulMIBDroppedPacket [ iNdx ] );
else
printf ( "\nPort %.1d Rx Drop Packets = %lu", iNdx - 4 , ulMIBDroppedPacket [ iNdx ] );
}
}
return;
}
//--------------------------------------------------------------------------
//
// int DumpDroppedPacketMIB
//
// Description:
//
// Command:
//
// ddrop (s) Dump dropped packet MIB counters for all ports every (s)
// seconds until 'q' key is detected. Display only once if
// (s) is not specified.
//
// Parameters:
// int elem1 Repeat every number of seconds if specified
// int elem2 NOPARAMETER
// int elem3 NOPARAMETER
//
// Return Vlaue:
// SUCCESS or PARMERROR
//
//--------------------------------------------------------------------------
int DumpDroppedPacketMIB ( int elem1, int elem2, int elem3 )
{
int ch =0;
time_t tStart, tEnd;
int i;
if ( elem2 != NOPARAMETER || elem3 != NOPARAMETER )
return PARMERROR;
for ( i = 0; i < DROPPEDMIBTOTAL; i++)
{
ulMIBDroppedPacketPrev [ i ] = 0L;
ulMIBDroppedPacketPrev [ i ] = 0L;
}
GetDroppedPacketMIB ( TRUE );
printf ("\n");
if ( elem1 > 0 )
{
#ifdef LINUX
EnableLINUXkbhit();
#endif
time (&tStart);
tEnd = tStart;
while(ch != 'q') {
if ( tEnd - tStart >= elem1 )
{
GetDroppedPacketMIB ( TRUE );
printf ("\n");
tStart = tEnd;
}
else
{
GetDroppedPacketMIB ( FALSE );
}
time (&tEnd);
if(_kbhit()) {
ch = _getch();
//printf("you hit %c\n",ch);
}
}
#ifdef LINUX
DisableLINUXkbhit();
#endif
}
return SUCCESS;
}
//--------------------------------------------------------------------------
//
// int DumpDigitalTestReg
//
// Description:
//
// Command:
//
// ddt Dump digital test registers.
//
// Parameters:
// int elem1 NOPARAMETER
// int elem2 NOPARAMETER
// int elem3 NOPARAMETER
//
// Return Vlaue:
// SUCCESS or PARMERROR
//
//--------------------------------------------------------------------------
int DumpDigitalTestReg ( int elem1, int elem2, int elem3 )
{
int iNdx;
if ( elem1 != NOPARAMETER || elem2 != NOPARAMETER || elem3 != NOPARAMETER )
return PARMERROR;
for ( iNdx = DTESTREGBEGIN; iNdx <= DTESTREGEND - 2; iNdx++ )
printf ( "\nTOS Digital Testing Status Register (0x%02x) = 0x%02x", iNdx, ReadData ( (BYTE) iNdx ) );
for ( ; iNdx <= DTESTREGEND; iNdx++ )
printf ( "\nTOS Digital Testing Control Register (0x%02x) = 0x%02x", iNdx, ReadData ( (BYTE) iNdx ) );
return SUCCESS;
}
//--------------------------------------------------------------------------
//
// int GetDynaMACEntryCnt
//
// Description:
// Get number of dynamic MAC address table entries
//
//
// Parameters:
// None
//
// Return Vlaue:
// Number of dynamic MAC address table entries
//
//--------------------------------------------------------------------------
int GetDynaMACEntryCnt ()
{
BYTE bData68;
BYTE bData63;
BYTE bData55;
unsigned int uTemp;
WriteData ( INDIRECTCTL1, INDIRECTREAD | INDIRECTDMACTAB );
WriteData ( INDIRECTCTL2, 0 );
// Read until bit 55 is not one
bData68 = ReadData ( INDIRECTDATABASE );
bData63 = ReadData ( INDIRECTDATABASE + 1 );
do {
bData55 = ReadData ( INDIRECTDATABASE + 2 );
} while ( bData55 & DMACTABREADY );
if ( !(bData68 & VALIDMACENTRY) ) // There are valid entries in the table
{
// Form the 10-bit value with bits from two bytes read
uTemp = bData68 & 0x0f;
uTemp = (uTemp << 6) + (bData63 >> 2) + 1 ;
return ( uTemp ); // Form the 10-bit value with bits from two bytes read
}
else
return 0;
}
//--------------------------------------------------------------------------
//
// int DumpDynaMACTable
//
// Description:
// Command:
//
// ddtab n1 n2 Dump a range (from n1 to n2) of dynamic MAC address table entries.
//
// Parameters:
// int elem1 starting number
// int elem2 ending number
// int elem3 NOPARAMETER
//
// Return Vlaue:
// Number of dynamic MAC address table entries
//
//--------------------------------------------------------------------------
int DumpDynaMACTable ( int elem1, int elem2, int elem3 )
{
union utag {
BYTE data;
struct stag {
#ifdef LINUX
unsigned DataReady : 1;
unsigned SourcePort : 3;
unsigned Fid : 4;
#else
unsigned Fid : 4;
unsigned SourcePort : 3;
unsigned DataReady : 1;
#endif
} field;
} bData55;
union dmactabutag {
BYTE data;
struct dmactabstag {
#ifdef LINUX
unsigned filler : 6;
unsigned TimeStamp : 2;
#else
unsigned TimeStamp : 2;
unsigned filler : 6;
#endif
} field;
} bData63;
time_t tStart, tEnd;
int ch = 0;
int iEntryCnt;
int iNdxfrom = 0;
int iNdxto = 9;
int i;
if ( elem1 >= DMACTABTOTAL || elem2 >= DMACTABTOTAL )
return PARMERROR;
if ( elem2 != NOPARAMETER ) // It is a range
{
iNdxfrom = ( elem1 < elem2 ) ? elem1 : elem2;
iNdxto = ( elem1 >= elem2 ) ? elem1 : elem2;
}
else if ( elem1 != NOPARAMETER )
{
iNdxfrom = iNdxto = elem2 = elem1;
}
iEntryCnt = GetDynaMACEntryCnt ();
printf ( "\nDynamic MAC address table has %d entries\n", iEntryCnt);
iNdxto = iEntryCnt < iNdxto ? iEntryCnt : iNdxto;
#if 0
else if ( iNxdfrom > iEntryCnt )
{
printf ( "\nOut of range, use \"rcnt\" command to get number of entries");
return SUCCESS;
} else if ( iNdxto >= iEntryCnt )
iNdxto = iEntryCnt - 1;
#endif
for ( i = iNdxfrom; i <= iNdxto - 1; i++ )
{
if ( i > 0xff ) // Overflow to Indirect Access Control Register 0
{
WriteData ( INDIRECTCTL1, (BYTE)(INDIRECTREAD | INDIRECTDMACTAB | (BYTE)( i >> 8 ) ));
WriteData ( INDIRECTCTL2, (BYTE) (i % 256) );
}
else
{
WriteData ( INDIRECTCTL1, INDIRECTREAD | INDIRECTDMACTAB );
WriteData ( INDIRECTCTL2, (BYTE)i );
}
bData63.data = ReadDataBegin ( INDIRECTDATABASE + 1 );
// Read until bit 55 is not one
do {
bData55.data = ReadDataEnd ();
} while ( bData55.field.DataReady );
printf ( "\nDynamic MAC Table Entry (%d) => FID = %0x Source Port = %01d Time Stamp= %01d",
i, bData55.field.Fid, bData55.field.SourcePort, bData63.field.TimeStamp );
printf ( "\nDynamic MAC Table Entry (%d) => MAC Address %02x:%02x:%02x:%02x:%02x:%02x",
i,
ReadData ( INDIRECTDATABASE + 3 ),
ReadData ( INDIRECTDATABASE + 4 ),
ReadData ( INDIRECTDATABASE + 5 ),
ReadData ( INDIRECTDATABASE + 6 ),
ReadData ( INDIRECTDATABASE + 7 ),
ReadData ( INDIRECTDATABASE + 8 ) );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -