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

📄 ethernet.c

📁 开发工具用EMBEST IDE进行开发,该程序是一个S3C44B0直接驱动网卡的程序,包括8019初始化,物理层收包发包,UDP的接收和发送,IP的发送和接收.已及ARM的地址解板,整个程序是自顶而下
💻 C
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************************************
* File:	EtherNet.c
* Author:	embest	
* Desc:	ethernet MAC protocol and control code
* History:	
*********************************************************************************************/

/*--- include files ---*/

#include "44b.h"
#include "44blib.h"
#include "def.h"
#include "option.h"
#include "ethernet.h"
#include "def.h"
#include "string.h"
//#include "stdlib.h"
#include "stdio.h"
#include "lib.h"
/*--- global variables ---*/

unsigned char txd_buffer_select=0;

//unsigned char  rcve_buf_allocated;
extern unsigned char  rcve_buf_allocated;
/* local MAC Address */
char mac_addr[6] = { 0x00,0x06,0x98,0x01,0x7E,0x8F };
unsigned char tempmac[6];
extern unsigned char 	my_hwaddr[6];
extern unsigned short volatile event_word;
/* data buffer and operate signal */
char  net_out_data[1536];
char  net_in_data[1536];
int	  iReceiveData;
int   iSendData;
unsigned short emtp;
/* Ethernet control address */
volatile unsigned char *ether_base = (unsigned char *)0x02180000;
//int EtherOutput(unsigned char *dmac,char* pdata, USHORT len);
int EtherOutput(char* pdata, USHORT len);
int EtherInput(USHORT tms);
int iLoopBackMode;

/*--- function decleare ---*/

int  NicReset(void);
void NicInit(void);
void NicClose(void);
void query_8019(void);
void Readrt801nodeid(void);
void Writert8019nodeid(void);
void send_frame(unsigned char * outbuf, unsigned short len);
unsigned char * rcve_frame(void);
void eth_send(unsigned char * outbuf, unsigned char * hwaddr, unsigned short ptype, unsigned short len);
void eth_rcve(unsigned  char  * inbuf);
void page(unsigned char pagenumber)
{
	unsigned char  temp;
	temp=nic_read(NIC_CR);		
	temp=temp&0x3B ;
	pagenumber=pagenumber <<6;
	temp=temp | pagenumber;
//	reg00=temp;
	nic_write(NIC_CR, temp);
}
/*--- function code ---*/

/*********************************************************************************************
* name:		Readrt801nodeid
* func:		read 8019 hwaddr
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void Readrt801nodeid(void)
{
unsigned char i;

 nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2);

 /* set byte count */
    nic_write(NIC_PG0_RBCR0, 0x0c);
    /* set byte count */
    nic_write(NIC_PG0_RBCR1, 0x00);
    /* Remote Start Address*/
    nic_write(NIC_PG0_RSAR0, 0x00);
    /* Remote Start Address */
    nic_write(NIC_PG0_RSAR1, 0x00);
    
    nic_write(NIC_CR,0X0A);
	for(i=0;i<6;i++)
	{
//	my_hwaddr[i]=nic_read(NIC_IOPORT);
//	my_hwaddr[i]=nic_read(NIC_IOPORT);
	tempmac[i]=nic_read(NIC_IOPORT);
	tempmac[i]=nic_read(NIC_IOPORT);
	}

}

/*--- function code ---*/

/*********************************************************************************************
* name:		writert8019nodeid
* func:		initialize 8019 hwaddr
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void Writert8019nodeid(void)
{
   unsigned char i;
 /* Page 1 configure */
    nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0);
    /* set MAC address */
    for( i = 0; i < 6; i++ )
    	{
        nic_write(NIC_PG1_PAR0 + i, my_hwaddr[i]);
 //  		 Uart_SendByte(mac_addr[i]);
        }
    /* broadcast address is 0x0 */
    for( i = 0; i < 8; i++ )
        nic_write(NIC_PG1_MAR0 + i, 0);

}



/*------------ininialize netcard 8019 10m/100m ent chip register--------------------*/
void init_8019(void)
{
	if(NicReset())
	{
		Uart_Printf("\nReset 8019AS fail!\n");
        return;
	}
	//Rtl8019AS_Reset();		//复位8019
//	R8019_CHIP_SELECT;
	nic_write(NIC_CR,0x21);
//	Uart_Printf("\n in init_8019() fuction NIC_CR== %x\n",nic_read(NIC_CR));
	
	//reg00=0x21;				//使芯片处于停止模式,这时进行寄存器设置 停止模式下,将不会发送和接收数据包
 	DelayTime(1000);				//延时10毫秒,确保芯片进入停止模式
	page(0);
	nic_write(0x0a,0x00);nic_write(0x0b,0x00);nic_write(0x0c,0xe0);
	//reg0a=0x00;	reg0b=0x00;	reg0c= 0xe0;			//monitor mode (no packet receive)
	nic_write(0x0d,0xe2);
	//reg0d= 0xe2;			//loop back mode 使芯片处于mon和loopback模式,跟外部网络断开  
	
	nic_write(0x01,0x4c);nic_write(0x02,0x80);nic_write(0x03,0x4c);nic_write(0x04,0x40);
	//reg01=0x4c;	reg02=0x80;	reg03=0x4c;	reg04=0x40;
	nic_write(0x07,0xff);
	//reg07=0xff;				//清除所有中断标志位
	nic_write(0x0f,0x00);
	//reg0f=0x00;				//disable all interrupt
	nic_write(0x0e,0xc8);
	//reg0e=0xc8;				//byte dma 8位dma方式
	page(1);
	nic_write(0x07,0x4d);nic_write(0x08,0x00);nic_write(0x09,0x00);nic_write(0x0a,0x00);nic_write(0x0b,0x00);
	nic_write(0x0c,0x00);nic_write(0x0d,0x00);nic_write(0x0e,0x00);nic_write(0x0f,0x00);
//	reg07=0x4d;	reg08=0x00;	reg09=0x00;	reg0a=0x00;	reg0b=0x00;	
//	reg0c=0x00;	reg0d=0x00;	reg0e=0x00;	reg0f=0x00;
	nic_write(0x00,0x22);
	//reg00=0x22;				//这时让芯片开始工作
	Readrt801nodeid();
    Writert8019nodeid();
	//ReadRtl8019NodeID();	//读出网卡的物理地址48位  
	//WriteRtl8019NodeID();	//将网卡地址写入到mar寄存器  
	page(0);
	nic_write(0x0c,0xcc);
	//reg0c=0xcc;				//将网卡设置成正常的模式,跟外部网络连接
	nic_write(0x0d,0xe0);
	//reg0d=0xe0;
	nic_write(0x00,0x22);
	//reg00=0x22;				//这时让芯片开始工作
	nic_write(0x07,0xff);
	//reg07=0xff;				//清除所有中断标志位
		Uart_Printf("\n\r init 8019 Ethernet init flish\n");
}



/*********************************************************************************************
* name:		NicReset
* func:		Reset 8019AS chip
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
int NicReset(void)
{
    unsigned char i;
    unsigned char j;

    for( j = 0; j < 20; j++ )
    {
        i = nic_read(NIC_RESET);
        DelayTime(50);
        nic_write(NIC_RESET, i);
        for( i = 0; i < 20; i++ )
        {
            DelayTime(50);
            if(nic_read(NIC_PG0_ISR) & NIC_ISR_RST)
                return 0;
        }
    }
    return -1;
}


int EtherOutput(char* pdata, USHORT len)
{
    USHORT i;
    USHORT send_len;
    UCHAR *cp;
	struct ETH_HEADER *eh;
	
	/* set MAC address */
//	eh = (struct ETHERHDR *)pdata;
//    for(i = 0; i < 6; i++)
//        eh->ether_shost[i] = mac_addr[i];
//    for(i = 0; i < 6; i++)
//        eh->ether_dhost[i] = dmac[i];
    
//    send_len = len + sizeof(struct ETHERHDR);
	send_len=len;
    if( send_len > 1518 )
    {
//    	Uart_Printf("\nCann't send data more than 1518 bytes!\n");
    	return -1;
    }
    /* at less send 60 byte */
    if( send_len < 60 )
        send_len = 60;
	//page(0);
	/* Register Page 0 operate */
    nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2);
    //reg00=0x22;
    /* set byte count */
    nic_write(NIC_PG0_RBCR0, (UCHAR)(send_len));
    //reg0a=send_len;
    /* set byte count */
    nic_write(NIC_PG0_RBCR1, (UCHAR)(send_len >> 8));
    //reg0b=(send_len>>8)
    /* Remote Start Address*/
    nic_write(NIC_PG0_RSAR0, 0);
    /* Remote Start Address */
    nic_write(NIC_PG0_RSAR1, NIC_FIRST_TX_PAGE);
    /* remote write */
    nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD1);
    
    /* Transfer data. */
    cp = pdata;
    for( i = 0; i < send_len; i++, cp++ )
        nic_write(NIC_IOPORT, *cp);
    for( i = send_len; i < 60; i++ )
        nic_write(NIC_IOPORT, 0);
    
    /** Complete remote dma. */
    nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2);
    /* if completed */
    for( i = 0; i <= 20; i++ )
        if(nic_read(NIC_PG0_ISR) & NIC_ISR_RDC)
            break;
    /* clear status */
    nic_write(NIC_PG0_ISR, NIC_ISR_RDC);
        
    /* Number of bytes to be transmitted. */
    nic_write(NIC_PG0_TBCR0, ((send_len) & 0xff));
    nic_write(NIC_PG0_TBCR1, ((send_len >> 8) & 0xff));
        
    /* First page of packet to be transmitted. */
    nic_write(NIC_PG0_TPSR, NIC_FIRST_TX_PAGE);
        
    /* Start transmission. */
    nic_write(NIC_CR, NIC_CR_STA | NIC_CR_TXP | NIC_CR_RD1 | NIC_CR_RD0);

    /* Wait until transmission is completed or aborted. */
    while(nic_read(NIC_CR) & NIC_CR_TXP);

	/* Complete DMA. */
    nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2 );

	iSendData = 0;
	
    return 0;
}


/*********************************************************************************************
* name:		query8019
* func:		quer8019
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void query_8019(void)
{   

⌨️ 快捷键说明

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