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

📄 8019as.c

📁 dsp下网络通信实验,很有用哦.平台为DSP54
💻 C
📖 第 1 页 / 共 2 页
字号:
	
	/*9. init CURR */
	PG1_CURR = NIC_RCV_MIN_PG;

	/*10. put NIC in START mode and for page 0(=22h) */
	PG0123_CMD = 0x22;

	/*11. init Transmit Cfg Reg */
	PG0_TCR = 0xE0;

	/*12. disable NIC LOOPBACK mode */
	PG0_DCR = 0xCB;
	
	/*13. now the NIC rdy for tx and rcv */

	/* detemine NIC mode */
	PgSelect(3);
	
	if(PG3_CONFIG0 & 0x08){
		tt=tt;
		Msg("Jump mode");
	}else{  /*jumpless mode*/
		if(PG3_CONFIG0 & 0x10 || PG3_CONFIG3 & 0x80){
			Msg("PNP mode");
			tt=tt;
		}else{
			Msg("RT Jumpless mode");
			tt=tt;
		}
	}
	
	/* before writing to the Page3 Config1-3 registers, Bit7,6 of 9346CR must be 11b */
	PG3_9346CR = PG3_9346CR | 0xC0;
	
	/* IRQ enable,int0,base=0x300 */
	PG3_CONFIG1 = 0x80;
	
	/* Medium Type os TP/CX auto-detect(10BaseT link test is enable)*/
	PG3_CONFIG2 = 0x00;
	
	/* BSELB, This bit, when set, forces the BROM disabled */
	PG3_CONFIG2 = 0x20;/* make sure that NIC is not Sleep/PWRDN/ACTIVEB mode */
	PG3_CONFIG3 = 0x00;

	/* prevent writing to Page3 Config1-3 registers */
	PG3_9346CR = PG3_9346CR & 0x0F;
	Delay(0xf);		/* wait for NIC detecting medium type automatically */

	if(PG3_CONFIG3 & 0x40){
		Msg("NIC in full duplex mode");
		tt=tt;
	}else{
		Msg("NIC in half duplex mode");
		tt=tt;
	}
	
	Msg("NIC in auto detect mode");
	tt=tt;
	
	if(PG3_CONFIG0 & 0x04){
   	
		Msg("test failure,set medium type 10Base2 by default");
		tt=tt;
	}else{
		Msg("test success,medium type 10BaseT");
		tt=tt;
	}
	PgSelect(0);
	return 0;
}
/*
 ***********************************************************************************************
 *
 ***************************************************
*/
static void WaitRDMAOperationDone(void)
{
	int i = 0;
	PgSelect(0);

	while(!(PG0_ISR & PG0_ISR_RDC) && i < 300)
		i++;	/* loop until Remote DMA Reading done or force to break the loop */
	if (i <300)
		PG0_ISR = PG0_ISR_RDC;
	else {
		/* RDMA not complete */ 
		Msg("WaitRDMAOperationDone() >> force to brak loop due to i is 300 at least");
	}
}
/*
 ***********************************************************************************************
 * NIC_ISR,the interrupt handler
 ***************************************************
*/
interrupt void NIC_ISR(void){
//	unsigned char isr;
    unsigned int isr;

	PgSelect(0);
	isr = PG0_ISR;
	if(isr & PG0_ISR_OVER){
		tt=tt;
	}
	if(isr & PG0_ISR_TX_ERR){
		tt=tt;
	}
	if(isr & PG0_ISR_RX_ERR){
		tt=tt;
	}	
	if(isr & PG0_ISR_PRX){
	    NIC_RcvFrame();
	    tt=tt;
	}
	if(isr & PG0_ISR_PTX){
		IsTxOk=1;
	}	
	
	/* clear ISR */
	PgSelect(0);
	PG0_ISR = 0xFF;
 
	/* enable all intr */ 
/*	PgSelect(3);
	PG3_9346CR = PG3_9346CR | 0xC0;
	PG3_CONFIG1 = PG3_CONFIG1 | 0x80;
	PG3_9346CR = PG3_9346CR & 0x3F;

    PgSelect(0);
*/

}
/*
 *****************************************************************************************
 * NIC_TxFrame,transfer ethernet frame out
 ******************************************************************************************
*/
void NIC_TxFrame(void){
	int l,i;
	/* if NIC being Tx some frame ,return */
	if(PG0123_CMD & 0x04)
		return;
	/* if no data left to Tx,return */
	if(AppDataLen == 0)
		return;
	/* disable all interrupt */
//	DISABLE_INT;
	
	/* copy data from host mem to NIC local mem */
	PgSelect(0);
	PG0_RSARHI = NIC_TX_MIN_PG;
	PG0_RSARLO = 0x00;

	PG0_RBCRLO = (SendDataLen << 1);
	PG0_RBCRHI = 0x00;
	/* enable remote write */
	PG0123_CMD = (CR_PAGE0|CR_RWRITE|CR_START);
	l = SendDataLen;
	for(i=0;i<l;i++){
		port9310 = SendDataBuf[i];
	}
	
	/* wait for DMA operation done */
	WaitRDMAOperationDone();

	/* ok,now enable the NIC to Tx the data frame */ 
	PG0_TPSR = NIC_TX_MIN_PG;
	PG0_TBCRLO = SendDataLen << 1;
	PG0_TBCRHI = 0x00;
	/* NIC start transmission */
	PG0123_CMD = (CR_PAGE0 | CR_NODMA | CR_TRANS | CR_START);
	
	/* enable all intr */ 
/*	PgSelect(3);
	PG3_9346CR = PG3_9346CR | 0xC0;
	PG3_CONFIG1 = PG3_CONFIG1 | 0x80;
	PG3_9346CR = PG3_9346CR & 0x3B;
    	PgSelect(0);
    	*/

ENABLE_INT;
}
/*
 ****************************************************************************************
 * NIC_RcvFrame,receive frame in
 ****************************************************************************************
 */
void NIC_RcvFrame(void)
{
	int l,i;
	int bCanReadNICData=1;
	u16_t Pg1CurrValue,Pg0BoundaryValue,framelen;
	typedef	struct _ethh{
		u16_t	nextpage_status;
		u16_t	lenhighbyte_lenlowbyte;
	}ethh;
	ethh ethhh;
	/* get NIC write and read Ptr */ 
	PgSelect(1);
	Pg1CurrValue = PG1_CURR;
	PgSelect(0);
	Pg0BoundaryValue = PG0_BNDRY;
	
	/* now,check if there have data come in */
	while(1){
		/* Rcv buf is empty ? */
		if(Pg0BoundaryValue == Pg1CurrValue){
		 	break;
		}
		
		/* ok,have data in,then get the NIC Rcv buf index */
		PG0_RSARLO = 0x00;
		PG0_RSARHI = Pg0BoundaryValue;
		PG0_RBCRHI = 0x00;
		/* 4 byte ,|Status(first)|NextPage(second)|LenLowByte(third)|LenHighByte(...)| */
		PG0_RBCRLO = 0x04;
		PG0123_CMD = CR_PAGE0|CR_RREAD|CR_START;

		ethhh.nextpage_status=port9310;
		ethhh.lenhighbyte_lenlowbyte=port9310;
		framelen=ethhh.lenhighbyte_lenlowbyte-4;//discard the first 4 byte;

		/* check NextPage */
		if((ethhh.nextpage_status>>8) > NIC_RCV_MAX_PG || (ethhh.nextpage_status>>8) <NIC_RCV_MIN_PG){
			/* error */
			if(bCanReadNICData){
				/* try to read rcv data again */
				bCanReadNICData=0;
				continue;
			}else{
				/* ignor all frame */
				PG0_BNDRY = Pg1CurrValue;
				return;
			}
		}
		
		/* wait for DMA operaiton done */
		
		/* check data len */
		if(framelen> ETH_FRAME_LEN){
			/* len error,then discard the current rcved frame */
			PG0_BNDRY = ethhh.nextpage_status >> 8;
			Pg0BoundaryValue=(ethhh.nextpage_status>>8);
			continue;
		}
		
		/* ok,copy rcved Ethernet frame to host mem */ 
		PG0_RSARLO = 0x00 + 0x04;	//discard the 4 byte above
		PG0_RSARHI = Pg0BoundaryValue;
		PG0_RBCRHI = framelen >> 8;
		PG0_RBCRLO = framelen & 0x00FF;

		PG0123_CMD = CR_PAGE0|CR_RREAD|CR_START;
		l=framelen;
		l=l+1;
		l=l >> 1;l=l;l=l;
		l=l; 
		for(i=0;i<l;i++){
			RcvDataBuf[i]=port9310;
		}
		RcvDataLen = framelen;
		for(i = 0;i<l;i++){
			RcvDataBuf[i] = ntohs(RcvDataBuf[i]);
		}
		/* wait for DMA operation done */
	    WaitRDMAOperationDone();
	    /* now the Ethernet frame have put in the host mem,then careful analysis it */
	    parseit();	
	    /* next */
	    Pg0BoundaryValue=(ethhh.nextpage_status>>8);
	    PG0_BNDRY = (ethhh.nextpage_status>>8);
	    bCanReadNICData=1;
	}	/* end while(1) */
}
	
/*
 ***************************************************************************************
 * main,the program entry point
 ***************************************************************************************
*/
void main(){

    int nettemp;
    
	cpu_ini();
//	StartTimer();
	//write then read i/o port 8009h to reset NIC
	  
//--------- 
	port800d = 0xffff;
	Delay(0x2);
	nettemp = port800d;
	Delay(0x2);
	NIC_reset();
	
	NIC_init();
	while(1){
		application();
	}
}
/*
 * ********************************************************************
 * SendData,user application call it to send data
 * pappdata --->the application data buf
 * paadatalen ---->application data len
 * return send ok?
 * depend on AppSendDataID,SendDabaBuf,SendDataLen,IsTxOk,
 * ********************************************************************
 */
#define	UDP_MAX_LEN	10


u16_t SendData(u16_t *pappdata,u16_t appdatalen){
	long	timeout = 0;
	u16_t	appdata_len_left;
	u16_t	cnt = 0;
	u16_t	token[3];
	appdata_len_left = appdatalen;

	//1, send the TOKEN_SEND_DATA

	token[0] = TOKEN_SEND_DATA;
	token[1] = appdatalen << 1;
	token[2] = AppSendDataID;	//indicate whick app data
	SendDataLen = totalmake(&SendDataBuf[0],&token[0],3,NULL,0);
DISABLE_INT;
	NIC_TxFrame();
//	while(IsTxOk == 0){
//		;
//	}
	IsTxOk = 0;
	AppSendDataID++;
	
	//2, send data
	while(appdata_len_left > 0){
		if(appdata_len_left >= UDP_MAX_LEN){
			appdatalen = UDP_MAX_LEN;
		}else
			appdatalen = appdata_len_left;
		//construct protocoled data
		token[0] = TOKEN_DATA;
		token[1] = cnt;
		token[2] = appdatalen << 1;//the true application data length
		SendDataLen = totalmake(&SendDataBuf[0],pappdata,appdatalen,&token[0],3);
		//send it
DISABLE_INT;
		NIC_TxFrame();
//		while(IsTxOk == 0){
//			;
//		}
		IsTxOk = 0;
		cnt++;
		if(appdata_len_left >= UDP_MAX_LEN){
			appdata_len_left -= UDP_MAX_LEN;
			pappdata += UDP_MAX_LEN;
		}else
			break;
	}

	//3,wait for status return,are it send ok,are the app data id ok?
//	timeout = time;
	while(!SendOk){
		if(timeout++ > 0xff)
			return 1;
	}
	return 1;	
}

⌨️ 快捷键说明

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