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

📄 loop.c

📁 基于RTL8019XX芯片实现loopback mode 1 ,2 和 3,纯C代码.
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
#include <conio.h>

#define BASE	0x300

void stepEight( int TCRValue );
void HardReset( void );
int pktTransmit( void );
void PCtoNIC( char *, int , int  );

void main( void ){
	int x;
	char Packet[128], DummyPacket[128];
	printf("\r\nPrograma p/ leitura dos registros do 8019XX.");
	printf("\r\nPagina 0:");
	outportb( BASE+0X00, 0X22 );
	printf("\r\nCR     = %02X", inportb( BASE+0X00));
	printf("\r\nCLDA0  = %02X", inportb( BASE+0X01));
	printf("\r\nCLDA1  = %02X", inportb( BASE+0X02));
	printf("\r\nISR    = %02X", inportb( BASE+0X07));
	printf("\r\n8019ID0= %02X", inportb( BASE+0X0A));
	printf("\r\n8019ID1= %02X", inportb( BASE+0X0B));
	while( !kbhit() );
	getch();
	printf("\r\nPagina 2:");
	outportb( BASE+0X00, 0XA2 );
	printf("\r\nCR     = %02X", inportb( BASE+0X00));
	printf("\r\nPSTART = %02X", inportb( BASE+0X01));
	printf("\r\nPSTOP  = %02X", inportb( BASE+0X02));
	printf("\r\nTPSR   = %02X", inportb( BASE+0X04));
	printf("\r\nRCR    = %02X", inportb( BASE+0X0C));
	printf("\r\nTCR    = %02X", inportb( BASE+0X0D));
	printf("\r\nDCR    = %02X", inportb( BASE+0X0E));
	printf("\r\nIMR    = %02X", inportb( BASE+0X0F));
	while( !kbhit() );            //Wait a key to be pressed
	getch();
	printf("\r\nPagina 3:");      //Read page 3 registers
	outportb( BASE+0X00, 0XE2 );
	printf("\r\nCR     = %02X", inportb( BASE+0X00));
	printf("\r\n9346CR = %02X", inportb( BASE+0x01));
	printf("\r\nBPAGE  = %02X", inportb( BASE+0x02));
	printf("\r\nCONFIG0= %02X", inportb( BASE+0x03));
	printf("\r\nCONFIG1= %02X", inportb( BASE+0x04));
	printf("\r\nCONFIG2= %02X", inportb( BASE+0x05));
	printf("\r\nCONFIG3= %02X", inportb( BASE+0x06));
	printf("\r\nCSNSAV = %02X", inportb( BASE+0x08));
	printf("\r\nINTR   = %02X", inportb( BASE+0x0B));
	printf("\r\nCONFIG4= %02X", inportb( BASE+0x0D));
	while( !kbhit() );					//Wait a key to be pressed
	getch();
	printf("\r\nExecutando loopback mode 1");
	HardReset();
	for( x=0; x<128; x++ ) Packet[x]=0;
	for( x=0; x<128; x+=2)              //Assemble loopback packet
		Packet[x+1] = x;
	for( x=0; x<128; x+=2)              //Assemble dummypacket
		DummyPacket[x+1] = 2*x;
	outportb( BASE+0x00, 0X21 );
	delay(3);
	outportb( BASE+0x0E, 0X41 );           //16bits 0x43?????
	outportb( BASE+0x0C, 0X1F );
	outportb( BASE+0x05, 120  );     //Why did they use 120 here (I believe is 128)????
	outportb( BASE+0x06, 0    );
	outportb( BASE+0x04, 0X40 );
	outportb( BASE+0x00, 0X22 );
	PCtoNIC( &Packet[0], 120, 0x4000 );
	outportb( BASE+0x0D, 0    );
	outportb( BASE+0x07, 0xFF );
	outportb( BASE+0x00, 0x26 );
	if( !pktTransmit() ){
		if( inportb( BASE+0x07 ) == 0x08 )
			printf("\r\nTransmit error bit set. Cable may not be terminated.");
		else
			printf("\r\nTransmit timed out. Cable may not be connected.");
		exit( 1 );
	}
	printf("\r\nGroup I Loopbacktests: CRC Generation.");
	printf("\r\nMode 1 Loopback test (TCR = 02).");
	stepEight( 2 );
	while( !kbhit() );
	getch();
	printf("\r\nMode 2 Loopback test (TCR = 04).");
	stepEight( 4 );
	while( !kbhit() );
	getch();
	PCtoNIC( &DummyPacket[0], 120, 0x4000 );
	printf("\r\nMode 3 Loopback test (TCR = 06).");
	stepEight( 6 );
}

void stepEight( int TCRValue ){
	int x;
	outportb( BASE+0x0D, 0X00 );
	outportb( BASE+0x00, 0x21 );
	delay( 3 );
	outportb( BASE+0x0D, TCRValue );
	outportb( BASE+0x07, 0xFF );
	outportb( BASE+0x00, 0x22 );
	outportb( BASE+0x00, 0x26 );
	while( inportb( BASE+0x00 ) != 0x22 );
	if( (char)inportb( BASE+0x07 ) != 0x06 ){
		printf("\n\rBad transmission. interrupt status register");
		printf("\n\rdoesn't equal 0x06. %02X", inportb( BASE+0x07 ));
	}
	for( x=0; x<8; x++ ){
		printf("\n\rFIFO %d: %02X.", x, inportb( BASE+0x06 ) );
		delay(1);
	}
	outportb( BASE+0X00, 0xA2 );
	printf("\r\nTCR: %02X;", inportb( BASE+0x0D ) & 0x1F );
	printf("\r\nRCR: %02X;", inportb( BASE+0x0C ) & 0x3F );
	outportb( BASE+0X00, 0x22 );
	printf("\r\nTSR: %02X;", inportb( BASE+0x04 ) & 0xFD );
	printf("\r\nRSR: %02X;", inportb( BASE+0x0C ) );
	printf("\r\nISR: %02X;", inportb( BASE+0x07 ) );
}

void PCtoNIC( char *tr, int nbytes, int page ){
	int x,aux;
	nbytes++;
	nbytes &= 0xFFFE;			//make even
	outportb( BASE+0x0A, (nbytes) );
	outportb( BASE+0x0B, (nbytes >> 8) );
	outportb( BASE+0x08, (page) );
	outportb( BASE+0x09, (page >> 8) );
	outportb( BASE+0x00, 0x12 );
	aux = nbytes ;
	for( x=0; x< aux; x+=2 ){ outport( BASE+0x10, tr[x] ); delay(1);} //IO port
	while( inportb( BASE+0x07 ) == 0x40 );
	outportb( BASE+0x07, 0x40 );
}

int pktTransmit( void ){
	clock_t Start, Current;
	Start = clock( );
	do{
		if( (inportb( BASE+0x07 ) & 0x02) != 0 ) return 1;
		Current = clock( );
	}while( ((Current - Start) < 5000) );
	return 0;
}

void HardReset( void ){
	inportb( BASE+0x1F );
	outportb( BASE+0x1F, 0x01 );
	delay( 3 );
}

⌨️ 快捷键说明

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