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

📄 8019as.c

📁 dsp下网络通信实验,很有用哦.平台为DSP54
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "8019as.h"
#include "netcom.h"
#include "net_fun.h"
#include "app.h"
#include "sys.h"
long time = 0;

u16_t 	AppDataLen = 0;		//word count
u16_t	SendDataLen = 0;	//word count
u16_t	SendDataBuf[521];
u16_t	RcvDataBuf[600];
u16_t	RcvDataLen = 0;
u16_t 	AppDataBuf[500];
u16_t	AppSendDataID = 0;
u16_t	*pappdata = AppDataBuf;
int 	tt=0;	//temp
int 	IsTxOk=0;
///use for arp 
u16_t	ArpIpAddress[10][4];
u16_t	ArpEthAddress[10][6];
u16_t	ArpAddressRead = 0;
u16_t	ArpAddressWrite = 0;
u16_t	ArpCanAdd = 1;
/////////////////////////////////////////////////////////
u8_t	UdpDestIpAddr[4] = {	DIPADDR0,DIPADDR1,DIPADDR2,DIPADDR3	};

u8_t	UdpDestEthAddr[6] = {	{DETHADDR0},		//default
								{DETHADDR1},
								{DETHADDR2},
								{DETHADDR3},
								{DETHADDR4},
								{DETHADDR5},	
							};
/*				
u8_t	UdpDestEthAddr[6] = {	{ETHADDR0},		//default
								{ETHADDR1},
								{ETHADDR2},
								{ETHADDR3},
								{ETHADDR4},
								{ETHADDR5},	
							};
*/
u16_t	UdpDestPort = DESTUDPPORT ;
u16_t	UdpSrcPort = SRCUDPPORT;
u8_t	MyEthAddr[6] = {		{ETHADDR0},		//default
								{ETHADDR1},
								{ETHADDR2},
								{ETHADDR3},
								{ETHADDR4},
								{ETHADDR5},	
						};
u8_t	MyIpAddr[4] = {	IPADDR0,IPADDR1,IPADDR2,IPADDR3	};
u8_t 	ArpDestEthAddr[6];
u8_t	ArpDestIpAddr[4];

/*
 ***************************************************************
 * arp reply
 **********************************************************
 */
void ArpReply()
{
	if((ArpAddressRead == ArpAddressWrite) && (ArpCanAdd)){
		return;
	}
	while((ArpAddressRead != ArpAddressWrite) ){
		SendDataLen = arp(ARP_REPLY,\
				&ArpEthAddress[ArpAddressRead],\
				&ArpIpAddress[ArpAddressRead],\
				SendDataBuf);
		DISABLE_INT;
		NIC_TxFrame();
		while(IsTxOk == 0){
			;
		}
		IsTxOk = 0;
		ArpAddressRead ++;
		if(ArpAddressRead > 9){
			ArpAddressRead = 0;
		}
		if(ArpAddressRead == ArpAddressWrite){
			ArpCanAdd = 1;
		}
	}
}
/*
 * ****************************************************************************************
 * parse arp
 * ****************************************************************************************
 */
void parse_arp()
{
	arphdr	*parp = (arphdr *)&RcvDataBuf[ETH_HLEN >> 1];
	ArpDestEthAddr[0] = parp->send_hd_addr0;
	ArpDestEthAddr[1] = parp->send_hd_addr1;
	ArpDestEthAddr[2] = parp->send_hd_addr2;
	ArpDestEthAddr[3] = parp->send_hd_addr3;
	ArpDestEthAddr[4] = parp->send_hd_addr4;
	ArpDestEthAddr[5] = parp->send_hd_addr5;

	ArpDestIpAddr[0] = parp->send_ip_addr0;
	ArpDestIpAddr[1] = parp->send_ip_addr1;
	ArpDestIpAddr[2] = parp->send_ip_addr2;
	ArpDestIpAddr[3] = parp->send_ip_addr3;
	
	if(parp->target_ip_addr0 == MyIpAddr[0] && \
				parp->target_ip_addr1 == MyIpAddr[1] && \
				parp->target_ip_addr2 == MyIpAddr[2] && \
				parp->target_ip_addr3 == MyIpAddr[3]){
		//yes it's me
		switch(parp->opcode){
			case ARP_REQUEST://request my ethaddress,so I will reply my ethnet address
				if(!ArpCanAdd){
					break;
				}
				ArpIpAddress[ArpAddressWrite][0] = ArpDestIpAddr[0];
				ArpIpAddress[ArpAddressWrite][1] = ArpDestIpAddr[1];
				ArpIpAddress[ArpAddressWrite][2] = ArpDestIpAddr[2];
				ArpIpAddress[ArpAddressWrite][3] = ArpDestIpAddr[3];
				
				ArpEthAddress[ArpAddressWrite][0] = ArpDestEthAddr[0];
				ArpEthAddress[ArpAddressWrite][1] = ArpDestEthAddr[1];
				ArpEthAddress[ArpAddressWrite][2] = ArpDestEthAddr[2];
				ArpEthAddress[ArpAddressWrite][3] = ArpDestEthAddr[3];
				ArpEthAddress[ArpAddressWrite][4] = ArpDestEthAddr[4];
				ArpEthAddress[ArpAddressWrite][5] = ArpDestEthAddr[5];
				
				ArpAddressWrite ++;
				if(ArpAddressWrite > 9){
					ArpAddressWrite = 0;				
				if(ArpAddressWrite == ArpAddressRead){
					ArpCanAdd = 0;
				}
				break;
			case ARP_REPLY:
				//are the address I want?
				if(parp->target_ip_addr0 == MyIpAddr[0] && \
							parp->target_ip_addr1 == MyIpAddr[1] && \
							parp->target_ip_addr2 == MyIpAddr[2] && \
							parp->target_ip_addr3 == MyIpAddr[3]){
					//yes,store it
					UdpDestEthAddr[0] = parp->target_hd_addr0;
					UdpDestEthAddr[1] = parp->target_hd_addr1;
					UdpDestEthAddr[2] = parp->target_hd_addr2;
					UdpDestEthAddr[0] = parp->target_hd_addr0;
					UdpDestEthAddr[1] = parp->target_hd_addr1;
					UdpDestEthAddr[2] = parp->target_hd_addr2;
				}
				break;
			default:
				Msg("wrong");
				break;
		}
	}
}
}
u16_t	AppRcvDataBuf[600];
u16_t	AppRcvDataLen = 0;
u16_t	RcvOk = 0;
u16_t	CurrentState = 0;
u16_t	TotalWillRcvDataLen = 0;
u16_t	CurrentAppID = 0;
u16_t	CurrentDataID = 0;
u16_t	SendOk = 0;
u16_t	bSendTokenStatus = 0;
u16_t	SendTokenStatusAppID = 0;

void	SendTokenStatus()
{
	u16_t	token[3];
	token[0] = TOKEN_STATUS;
	token[1] = SendTokenStatusAppID;
	SendDataLen = totalmake(&SendDataBuf[0],&token[0],3,NULL,0);
DISABLE_INT;
	NIC_TxFrame();
	while(IsTxOk == 0){
		;
	}
	IsTxOk = 0;
}

void RcvUdpData()
{
	u16_t	i,j;
	u16_t	*pRcvData = &RcvDataBuf[(ETHHDR_LEN + IPHDR_LEN + UDPHDR_LEN) >> 1];

	switch(pRcvData[0]){
		case TOKEN_SEND_DATA:
			CurrentState = TOKEN_DATA;
			CurrentAppID = pRcvData[1];
			CurrentDataID = 0;
			TotalWillRcvDataLen = pRcvData[2];
			AppRcvDataLen = 0;
			RcvOk = 0;
			break;
		case TOKEN_DATA:
			if(RcvOk){
				break;
			}
			if(pRcvData[1] == CurrentDataID){
				for(i = 0,j = (21+3);i < pRcvData[2];i++,j++){
					AppRcvDataBuf[AppRcvDataLen + i] = RcvDataBuf[j];
				}
				AppRcvDataLen += pRcvData[2];
				CurrentDataID ++;
				if(TotalWillRcvDataLen == AppRcvDataLen){
					//ok,complete rcv data
					RcvOk = 1;
					CurrentState = TOKEN_STATUS;
					//send status;
					bSendTokenStatus = 1;
					SendTokenStatusAppID = CurrentAppID;
					// the call SendTokenStatus();
				}
			}
			break;
		case TOKEN_STATUS:
			if(pRcvData[1] == AppSendDataID){
				SendOk = 1;
			}
			break;
		default:
			tt = tt;
			break;
	}
}

void parse_udp()
{
	eth_ip_udp_hdr *phdr = ((eth_ip_udp_hdr *)RcvDataBuf);
	u16_t	destport = phdr->uhdr.destport;
	destport = destport;
	switch(phdr->uhdr.destport){
		case DESTUDPPORT:
			RcvUdpData();
			break;
		default:
			break;
	}	
}
void parseit()
{
	//first parse the ethnet head
	ethhdr *peth = (ethhdr *)RcvDataBuf;
	eth_ip_udp_hdr *phdr = ((eth_ip_udp_hdr *)RcvDataBuf);
	u16_t	proto = peth->proto;
	switch(proto){
		case ETH_P_ARP:
			parse_arp();
			break;
		case ETH_P_IP:
			switch(phdr->ihdr.proto){
				case PROTO_UDP:
					parse_udp();
					break;
				default:
					break;
			}
			break;
		default:
			tt = tt;
			break;
	}		
	tt = tt;
}
/*
 *******************************************************************
 * NIC_reset,reset the NIC
 ********************************************************************
*/
static void NIC_reset(void)
{
	int temp;
	port9307=0xffff;//PG0_ISR_RESET;	
	
	temp=port9318;
	port9318=temp;
	PgSelect(0);
	
	/* check if reset */
	while((PG0_ISR & PG0_ISR_RESET) == 0){
		Delay(1);
		Msg("8019as_reset not complete");
	}
	/* Ack the reset intr */
	PG0_ISR = PG0_ISR_RESET;
}
/*
 ************************************************************************
 * NIC_init,intitialize the NIC
 ************************************************************************
*/
int NIC_init(void){
	int i,par[3];
	/* refer to Rs232/ */
	PG0123_CMD = 0x21;
	Delay(0x2);

	/*1. check RTL8019AS */
	if(PG0123_CMD != 0x21)
		return 1;

	/*2. init Data Cfg Reg */
	PG0_DCR = 0x43;	//loopback mode,word wide dma
    
	
	/*3. clear Remote Byte Count Reg */
	PG0_RBCRHI = 0;
	PG0_RBCRLO = 0;

	/*4. init Rcv Cfg Reg */
	PG0_RCR = 0x1A;


	/*5. Place the NIC in LOOPBACK mode 1 or 2 (Transmit Cfg Reg eque 02h or 04h) */
	PG0_TCR = 0x02;
    

	/*6. init Rcv Buf Ring:Boundary Point(BNDRY),Page Start,Page Stop */
	PG0_PSTART = NIC_RCV_MIN_PG;
	PG0_PSTOP = NIC_RCV_MAX_PG;
	PG0_BNDRY = NIC_RCV_MIN_PG;

	/*7. clear Interrupt Status Reg,and set Interrupt Mask Reg */
	PG0_ISR = 0xFF;
	PG0_IMR = 0x03;

	/*8. init PAR & MAR */
	PgSelect(1);
	//Mar set
	PG1_MAR0 = 0xff;
	PG1_MAR1 = 0xff;
	PG1_MAR2 = 0xff;
	PG1_MAR3 = 0xff;
	PG1_MAR4 = 0xff;
	PG1_MAR5 = 0xff;
	PG1_MAR6 = 0xff;
	PG1_MAR7 = 0xff;
	
	//PAR set
	PgSelect(0);
	PG0_RSARHI = 0x00;
	PG0_RSARLO = 0x00;
	PG0_RBCRLO = 0x06;
	PG0_RBCRHI = 0x00;
	/* enable remote read */
	PG0123_CMD = (CR_PAGE0|CR_RREAD|CR_START);
	/* begin copy data */
	for(i=0;i<3;i++){
		par[i]=port9310;//int par[0],like 0xff00,the PAR0 is 00,and the PAR1 is ff 
	}
	//par init value is: 00ff00ff00ff
	/* wait for DMA operation done */
	WaitRDMAOperationDone();
	PgSelect(1);
	PG1_PAR0 = par[0] & 0x00ff;
	PG1_PAR1 = par[0] >> 8;
	PG1_PAR2 = par[1] & 0x00ff;
	PG1_PAR3 = par[1] >> 8;
	PG1_PAR4 = par[2] & 0x00ff;
	PG1_PAR5 = par[2] >>8;
	//but we should self fill the PAR
	PG1_PAR0 = MyEthAddr[0];
	PG1_PAR1 = MyEthAddr[1];
	PG1_PAR2 = MyEthAddr[2];
	PG1_PAR3 = MyEthAddr[3];
	PG1_PAR4 = MyEthAddr[4];
	PG1_PAR5 = MyEthAddr[5];

⌨️ 快捷键说明

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