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

📄 ip51_mac.c

📁 完整的TCP/IP源代码,绝对好用
💻 C
📖 第 1 页 / 共 2 页
字号:
  BTCTRL	|=	0x80;

  //update tout_ptr pointer
  tout_ptr.p.low =	BTSCPL;
  tout_ptr.p.high=	BTSCPH;
   //update txbuf_lft
  txbuf_lft	+=	2;
  txbuf_lft	+=	len.i;

  COUTEN	&=	0xcf;//select MDMA
  MTLENH	=	len.u8.u8_1;
  MTLENL	=	len.u8.u8_2;
  len.i		=	(UINT8 xdata *)&mtx_cache[2];//use len as temp varab
  MTBAL		=	len.u8.u8_2;
  MTBAH		=	len.u8.u8_1;
//  printf("tx%x %x %x %x\n",(UINT16)mtx_cache[34],(UINT16)mtx_cache[35],(UINT16)mtx_cache[36],(UINT16)mtx_cache[37]);  
  MTPG		|=	0x80;
  tx_pkt_nums	--;



  COUTEN	= couten_bk;
  EA	=	1;
  return;
}

err_t
 Wr_pkt(UINT8  *eth_addr,UINT16 len,UINT8  *tptr,UINT16 flag)
{
  unsigned char	couten_bk;
  int_2_c	length;
  ptr_2_c	tmp_ptr;
  if(txbuf_lft > (len + 10))//have enough space for the next pkt
  {
	//put the data to buffer
	EA	=	0;

	length.i	=	len	+	8;
	couten_bk	=	COUTEN;
	COUTEN	|=	0x30; //select BOOT DMA
	tmp_ptr.ptr  =   &length;
	BTSPAG	=	tmp_ptr.p.type;//Attention
	BTSCPL	=	tmp_ptr.p.low;
	BTSCPH	=	tmp_ptr.p.high;
	//结束指针
    BTSEDL	=	0xff;
    BTSEDH	=	0xff;
    //目的当前指针
	BTTPAG	=	tin_ptr.p.type;//
	BTTCPL	=	tin_ptr.p.low;
	BTTCPH	=	tin_ptr.p.high;
	//目的启始指针
	BTTHDL	=	mtbuf_header.p.low;
	BTTHDH	=	mtbuf_header.p.high;
	//目的结束指针
	BTTEDL	=	mtbuf_tailer.p.low;
	BTTEDH	=	mtbuf_tailer.p.high;
	//数据长度
	BTLTHL	=	2;
	BTLTHH	=	0;
	//启动
	BTCTRL	|=	0x80;

    tmp_ptr.ptr = eth_addr;
	BTSPAG	=	tmp_ptr.p.type;//Attention
	BTSCPL	=	tmp_ptr.p.low;
	BTSCPH	=	tmp_ptr.p.high;
	BTLTHL	=	6;
	BTLTHH	=	0;
	BTCTRL	|=	0x80;//write eth addr
		
	tmp_ptr.ptr     =   &flag;
	BTSPAG	=	tmp_ptr.p.type;//Attention
	BTSCPL	=	tmp_ptr.p.low;
	BTSCPH	=	tmp_ptr.p.high;
	BTLTHL	=	2;
	BTLTHH	=	0;
	BTCTRL	|=	0x80;

	length.i	=	len;
	BTLTHL		=	length.u8.u8_2;
	BTLTHH		=	length.u8.u8_1;
	tmp_ptr.ptr  = tptr;
	BTSPAG	=	tmp_ptr.p.type;//Attention
	BTSCPL	=	tmp_ptr.p.low;
	BTSCPH	=	tmp_ptr.p.high;
	BTCTRL		|=	0x80;
		
	//update tin_ptr
	tin_ptr.p.low	=	BTTCPL;	//get the addr
	tin_ptr.p.high	=	BTTCPH;	//get the addr
	//update txbuf_lft
	txbuf_lft	-=	10;
	txbuf_lft	-=	len;
	tx_pkt_nums	++;

	//when TXDMA is stop and all last pkt has been sent out
	COUTEN	&=	0xcf;//select MAC DMA
	if((tx_pkt_nums > 0) && 
		((MTPG & 0x80)==0))
	{
	  restart	=	1;
	  EXIF	|=	0x80;//generate MAC interrupt
		}
		
	COUTEN	=	couten_bk;
	EA	=	1;
	return ERR_OK;
	}
	else
	{
		return ERR_MTXBFF;
	}
}

void MAC_interupt(void)  interrupt 11 //using 3
{
  UINT8	int_flag;
  UINT8	couten_bk;

  EA = 0;
  EXIF	&=	0x7F;//soft clear interrupt
 //read to clear INTST
  int_flag	=	INTST;
  couten_bk	=	COUTEN;
  COUTEN	&=	0xCF;

/*	SFR		
*	INTST:	| bit7	| bit6	| bit5	| bit4	| bit3	| bit2	| bit1	| bit0
*			   --	   --	  TxDN	 HOSTAB	  RxDN	  RxOF	  --	  BOV
*/

/*****************************************************************/
/*********************        RX INT          ********************/
/*****************************************************************/
  //when RXDN || RXOF || BOV int
  if(int_flag & 0x0d)
  {
//  	rin_stk[2]	=	rin_stk[1];
//	rin_stk[1]	=	rin_stk[0];
//	rin_stk[0]	=	rin_ptr;
	if(int_flag &INT_RX_DN)
	{
//	  ((mdma_hdr *)rin_ptr.ptr)->RxSTA	=	RxSTA;
	  if(((RxSTA & 0x1C) == 0) &&
	    (((mdma_hdr *)(rin_ptr.ptr))->sta & 0x80) == 0)
	  {
	  	rxbuf_lft		-=	(((mdma_hdr *)rin_ptr.ptr)->len).i + 4;
	    rin_ptr.p.low	=	MRBAL;
	    rin_ptr.p.high=	MRBAH;
	    rx_pkt_nums	++;
	  }
	  else
	  {
	  	MRBAL	=	rin_ptr.p.low;
		MRBAH	=	rin_ptr.p.high;
	//printf("E%x\n",(UINT16)RxSTA);
		P6	^=	0x06;
	  }
	}
	else if(int_flag & INT_RX_OB)//received buffer overflow interrupt
	{
	  //When receive buffer overflow,this must mean 
	  //*that the write pointer(MRBAH,MRBAL)
	  //have reached the end of the buffer
	  if((rin_ptr.p.low == MRBAL) && 
	    (rin_ptr.p.high == MRBAH))//
		//This must mean the alloated buffer is less than
		//5 bytes,and at the end of the receive buffer
		//write 0xAA at this byte,
	  {
	    ((mdma_hdr *)rin_ptr.ptr)->sta = 0xaa;
	    rxbuf_lft	-=	mrbuf_tailer.ptr - rin_ptr.ptr;
	    rin_ptr	=	mrbuf_header;
	  }
	  MRBAL	=	mrbuf_header.p.low;//adjust the  MRBAL,MRBAH 
	  MRBAH	=	mrbuf_header.p.high;//to rx buffer start if have space
	}
	else  //do nothing when  INT_RX_OF
	{
	  MRBAL	=	rin_ptr.p.low;
	  MRBAH	=	rin_ptr.p.high;
 printf("EF\n");
	}
//	if(rxbuf_lft < 0x3C00)
//	if(in_indx > 66)
//	while(1);
	if((rxbuf_lft > ETH_MTU) && (rx_pkt_nums < 255))
	//have enough space to receive the next packet
	{
 	  MRPG	|=	0x80;//enable MDMA
	}
	else
	  printf("STOP!!!\n");

	if(rxbuf_lft < MAC_RXBUF_THRE)
	{
		PAUSE_ON	=	1;
#ifdef FULL_DX
    	Rx_pause(1);
#else
		Rx_bkp(0x0f);
#endif//FULL_DX*/
	}

//	printf("%x	  %x\n",rxbuf_lft,(UINT16)int_flag);
//	printf("%d    %x\n",(UINT16)rx_pkt_nums,MRPG);

  }

/*****************************************************************/
/*********************        TX INT          ********************/
/*****************************************************************/
  //when TxDN || HOSTAB int 
  if(int_flag & 0x30)
  {
	if(int_flag & INT_TX_DN) //one frame is sent out
	  if(tx_pkt_nums)
	  {
  	    Tx_pkt();
		restart	=	0;	
	  }
  }
  else//one frame is not sent out due to abort
  {
	//restart TXDMA
	MTPG	|=	0x80;
  }

  if(restart == 1)
  {
	restart	=	0;
	Tx_pkt();
  }

	COUTEN	=	couten_bk;
	EA	=	1;
}


void	MII_init(UINT8 init)
{
	MADDR	=	0x08;
	MDATL	=	init;
}

void	MII_config(UINT8 addr,UINT16 config)
{
  MADDR	=	0x0a;//MII Mgmt Address
  MDATH	=	0;//PHY address =0;
  MDATL	=	addr;//reg address

  MADDR	=	0x0b;//MII mgmt write data
  MDATH	=	(UINT8)(config >> 8);

  MADDR	=	0x0d;//MII mgmt indicator
  while(MDATL & 0x01);//wait untill write is not busy

  MADDR	=	0x0b;//MII mgmt write data
  MDATL	=	(UINT8)config;

  MADDR	=	0x0d;//MII mgmt indicator
  while(MDATL & 0x01);//wait untill write is not busy

  return;
}


UINT16	MII_status(UINT8 addr)
{
  UINT16	res;

  MADDR	=	0x0a;//MII mgmt address
  MDATH	=	0x00;
  MDATL	=	addr;//reg address

  MADDR	=	0x09;//MII mgmt command 
  MDATL	=	0x01;//read command

  MADDR	=	0x0d;//MII mgmt indicator
  while(MDATL & 0x04);

  MADDR	=	0x0d;//MII mgmt indicator
  while(MDATL & 0x01);//wait untill read cycle is end

  MADDR	=	0x0c;//MII mgmt read data
  res	=	((MDATH << 8) | MDATL);

  MADDR	=	0x09;//MII mgmt command 
  MDATL	=	0x0;//read command

  return res;
}



void MII_mcoinit(UINT8* hash)
{
  MADDR	=	0x14;
  MDATH	=	*hash;
  hash	++;
  MDATL	=	*hash;
  hash	++;

  MADDR	=	0x15;
  MDATH	=	*hash;
  hash	++;
  MDATL	=	*hash;
  hash	++;

  MADDR	=	0x16;
  MDATH	=	*hash;
  hash	++;
  MDATL	=	*hash;
  hash	++;

  MADDR	=	0x17;
  MDATH	=	*hash;
  hash	++;
  MDATL	=	*hash;
}



void Ext_int0(void) interrupt 1
{
	static	i;
	i	++;
}

⌨️ 快捷键说明

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