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

📄 rtl8019.c

📁 Provides functions to initialize the Realtek 8019AS, and send and retreive packets
💻 C
📖 第 1 页 / 共 2 页
字号:

	for(i=0;i<length;i++)
		rtl8019Write(RDMAPORT, localBuffer[i]);
}



void rtl8019EndPacketSend(void)
{
	//send the contents of the transmit buffer onto the network
	rtl8019Write(CR,0x24);

	// clear the remote DMA interrupt
	rtl8019Write(ISR, (1<<ISR_RDC));
}




// pointers to locations in the RTL8019 receive buffer
static unsigned char nextPage;
static unsigned int currentRetreiveAddress;

// location of items in the RTL8019's page header
#define  enetpacketstatus     0x00
#define  nextblock_ptr        0x01
#define	 enetpacketLenL		  0x02
#define	 enetpacketLenH		  0x03

unsigned int rtl8019BeginPacketRetreive(void)
{
	unsigned char i;
	unsigned char bnry;

	unsigned char pageheader[4];
	unsigned int rxlen;

	// check for and handle an overflow
	rtl8019ProcessInterrupt();

	// read CURR from page 1
	rtl8019Write(CR,0x62);
	i = rtl8019Read(CURR);

	// return to page 0
	rtl8019Write(CR,0x22);

	// read the boundary register - pointing to the beginning of the packet
	bnry = rtl8019Read(BNRY) ;

	// return if there is no packet in the buffer
	if( bnry == i )
		return 0;


	// clear the packet received interrupt flag
	rtl8019Write(ISR, (1<<ISR_PRX));

	// the boundary pointer is invalid, reset the contents of the buffer and exit
	if( (bnry >= RXSTOP_INIT) || (bnry < RXSTART_INIT) )
	{
		rtl8019Write(BNRY, RXSTART_INIT);
		rtl8019Write(CR, 0x62);
		rtl8019Write(CURR, RXSTART_INIT);
		rtl8019Write(CR, 0x22);

		return 0;
	}

	// initiate DMA to transfer the RTL8019 packet header
    rtl8019Write(RBCR0, 4);
    rtl8019Write(RBCR1, 0);
    rtl8019Write(RSAR0, 0);
    rtl8019Write(RSAR1, bnry);
    rtl8019Write(CR, 0x0A);
	for(i=0;i<4;i++)
		pageheader[i] = rtl8019Read(RDMAPORT);

	// end the DMA operation
    rtl8019Write(CR, 0x22);
    for(i = 0; i <= 20; i++)
        if(rtl8019Read(ISR) & 1<<6)
            break;
    rtl8019Write(ISR, 1<<6);


	rxlen = (pageheader[enetpacketLenH]<<8) + pageheader[enetpacketLenL];
	nextPage = pageheader[nextblock_ptr] ;

	currentRetreiveAddress = (bnry<<8) + 4;

	// if the nextPage pointer is invalid, the packet is not ready yet - exit
	if( (nextPage >= RXSTOP_INIT) || (nextPage < RXSTART_INIT) )
		return 0;

    return rxlen-4;
}


void rtl8019RetreivePacketData(unsigned char * localBuffer, unsigned int length)
{
	unsigned int i;

	// initiate DMA to transfer the data
    rtl8019Write(RBCR0, (unsigned char)length);
    rtl8019Write(RBCR1, (unsigned char)(length>>8));
    rtl8019Write(RSAR0, (unsigned char)currentRetreiveAddress);
    rtl8019Write(RSAR1, (unsigned char)(currentRetreiveAddress>>8));
    rtl8019Write(CR, 0x0A);
	for(i=0;i<length;i++)
		localBuffer[i] = rtl8019Read(RDMAPORT);

	// end the DMA operation
    rtl8019Write(CR, 0x22);
    for(i = 0; i <= 20; i++)
        if(rtl8019Read(ISR) & 1<<6)
            break;
    rtl8019Write(ISR, 1<<6);

    currentRetreiveAddress += length;
    if( currentRetreiveAddress >= 0x6000 )
    	currentRetreiveAddress = currentRetreiveAddress - (0x6000-0x4600) ;
}



void rtl8019EndPacketRetreive(void)
{
	unsigned char i;

	// end the DMA operation
    rtl8019Write(CR, 0x22);
    for(i = 0; i <= 20; i++)
        if(rtl8019Read(ISR) & 1<<6)
            break;
    rtl8019Write(ISR, 1<<6);

	// set the boundary register to point to the start of the next packet
    rtl8019Write(BNRY, nextPage);
}


void rtl8019Overrun(void)
{
	unsigned char data_L, resend;

	data_L = rtl8019Read(CR);
	rtl8019Write(CR, 0x21);
	delay_ms(2);
	rtl8019Write(RBCR0, 0x00);
	rtl8019Write(RBCR1, 0x00);
	if(!(data_L & 0x04))
		resend = 0;
	else if(data_L & 0x04)
	{
		data_L = rtl8019Read(ISR);
		if((data_L & 0x02) || (data_L & 0x08))
	    	resend = 0;
	    else
	    	resend = 1;
	}

	rtl8019Write(TCR, 0x02);
	rtl8019Write(CR, 0x22);
	rtl8019Write(BNRY, RXSTART_INIT);
	rtl8019Write(CR, 0x62);
	rtl8019Write(CURR, RXSTART_INIT);
	rtl8019Write(CR, 0x22);
	rtl8019Write(ISR, 0x10);
	rtl8019Write(TCR, TCR_INIT);

    if(resend)
        rtl8019Write(CR, 0x26);

    rtl8019Write(ISR, 0xFF);
}

/*****************************************************************************
*  rtl8019_read_eprom_word(u08 adr);
*
*  Created By:  Volker Troyke www.troyke.de
*  Date:        September 28, 2004
*  Description: Read the 93LC46 EEPROM containing Ethernet MAC (NE2000 Standard)
*               connected to the rtl8019
*****************************************************************************/
#ifdef MAC_FROM_EEPROM
u16 rtl8019_read_eeprom_word(u08 adr)
{
	u08 i, temp;
	u16 result;

	temp = ((adr & 0x1F) | 0x80);

	rtl8019Write(CR, 0xE1);			// Page 3
	rtl8019Write(RTL_EECR, 0x80); // 9346 programming
	rtl8019Write(RTL_EECR, 0x84); // 9346 programming CLK on
	rtl8019Write(RTL_EECR, 0x80); // 9346 programming CKL off

	rtl8019Write(RTL_EECR, 0x8A); // 9346 programming CS on Data on
	rtl8019Write(RTL_EECR, 0x8E); // 9346 programming CLK on
	rtl8019Write(RTL_EECR, 0x8A); // 9346 programming CKL off

	for(i=0;i<8;i++)  // Write "read" Command and Word-Adress
	{
		if( (temp >> (7-i)) & 0x01 )
		{
			rtl8019Write(RTL_EECR, 0x8A); // 9346 programming DI on
			rtl8019Write(RTL_EECR, 0x8E); // 9346 programming CLK on
			rtl8019Write(RTL_EECR, 0x8A); // 9346 programming CKL off
			}
		else
		{
			rtl8019Write(RTL_EECR, 0x88); // 9346 programming DI off
			rtl8019Write(RTL_EECR, 0x8C); // 9346 programming CLK on
			rtl8019Write(RTL_EECR, 0x88); // 9346 programming CKL off
		}
	}
	rtl8019Write(RTL_EECR, 0x88); // 9346 programming DI off

	result = 0;

	for(i=0;i<16;i++)	// Read 16 Bits of Data
	{
		rtl8019Write(RTL_EECR, 0x8C); // 9346 programming CLK on
		rtl8019Write(RTL_EECR, 0x88); // 9346 programming CKL off
		if( (rtl8019Read(RTL_EECR)) & 0x01)
		{
			result |= (1 << (15-i));
		}
	}
	rtl8019Write(RTL_EECR, 0x80); // 9346 programming CS off

	return result;
}
#endif


void rtl8019Init(void)
{


#ifdef MAC_FROM_EEPROM
#ifdef DISPLAY_MAC
	char data[10];
#endif
	struct uip_eth_addr eth_adr;
	u08 i;
	u16 temp;
#endif

	rtl8019SetupPorts();

	HARD_RESET_RTL8019();

	// do soft reset
	rtl8019Write( ISR, rtl8019Read(ISR) ) ;
	delay_ms(20);

    // switch to page 3 to load config registers
    rtl8019Write(CR, 0xE1);

    // disable EEPROM write protect of config registers
    rtl8019Write(RTL_EECR, 0xC0);

    // set network type to 10 Base-T link test
  	 rtl8019Write(CONFIG2, 0x20);

    // disable powerdown and sleep
  	  rtl8019Write(CONFIG3, 0x00);
    delay_ms(20);

#ifdef MAC_FROM_EEPROM
	// read Ethernet MAC from EEPROM
	for(i=0;i<3;i++)
	{
		temp = rtl8019_read_eeprom_word(0x02+i);
		eth_adr.addr[(2*i)+1] = temp >> 8;
		eth_adr.addr[(2*i)] = temp & 0x00FF;
	}
#endif

    // reenable EEPROM write protect
    rtl8019Write(RTL_EECR, 0x00);
    // go back to page 0

   rtl8019Write(CR,0x21);       // stop the NIC, abort DMA, page 0
	delay_ms(2);               // make sure nothing is coming in or going out
	rtl8019Write(DCR, DCR_INIT);    // 0x58
	rtl8019Write(RBCR0,0x00);
	rtl8019Write(RBCR1,0x00);
	rtl8019Write(RCR,0x04);
	rtl8019Write(TPSR, TXSTART_INIT);
	rtl8019Write(TCR,0x02);
	rtl8019Write(PSTART, RXSTART_INIT);
	rtl8019Write(BNRY, RXSTART_INIT);
	rtl8019Write(PSTOP, RXSTOP_INIT);
	rtl8019Write(CR, 0x61);
	delay_ms(2);
	rtl8019Write(CURR, RXSTART_INIT);

	rtl8019Write(CR, 0x61); //Page 1

#ifdef MAC_FROM_EEPROM
	for(i=0;i<6;i++)			// write MAC from EEPROM to rtl8019 Registers
	{
		rtl8019Write(PAR0+i,eth_adr.addr[i]);
#ifdef DISPLAY_MAC
		lcd_gotoxy(1 + (i*3),2);
		char2strh(eth_adr.addr[i], data);
		lcd_puts(data);
		lcd_puts_P(PSTR(" "));
#endif
}
#else
	rtl8019Write(PAR0+0, MYMAC_0);
	rtl8019Write(PAR0+1, MYMAC_1);
	rtl8019Write(PAR0+2, MYMAC_2);
	rtl8019Write(PAR0+3, MYMAC_3);
	rtl8019Write(PAR0+4, MYMAC_4);
	rtl8019Write(PAR0+5, MYMAC_5);
#endif

#ifdef DISPLAY_MAC
	lcd_puts_P(PSTR("   "));
#endif

#ifdef MAC_FROM_EEPROM	// write MAC to the ARP-Module
	uip_setethaddr(eth_adr);
#endif

	rtl8019Write(CR,0x21);
	rtl8019Write(DCR, DCR_INIT);
	rtl8019Write(CR,0x22);
	rtl8019Write(ISR,0xFF);
	rtl8019Write(IMR, IMR_INIT);
	rtl8019Write(TCR, TCR_INIT);

	rtl8019Write(CR, 0x22);	// start the NIC
}


void rtl8019ProcessInterrupt(void)
{
	unsigned char byte = rtl8019Read(ISR);

	if( byte & (1<<ISR_OVW) )
		rtl8019Overrun();
}


⌨️ 快捷键说明

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