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

📄 cp2200.c.bak

📁 CP2201和51单片机实现ARP ICMP IP UDP协议
💻 BAK
📖 第 1 页 / 共 2 页
字号:
/*==========================================================
//CP220X芯片底层驱动,使用89c51芯片,数据地址总线复用

==========================================================*/
#include "reg.h"
#include "ne2000.h"
#include <stdio.h>
#include <absacc.h>
#include "netcomm.h"
extern union NetNode xdata myNode;
volatile INT8U xdata Count1ms;
 unsigned int num_bytes; 
void Delay1ms(unsigned char T)
{
	Count1ms=T;
	while (Count1ms);
}



/*==========================================================
//	function:	write data to add
//	Parameter:	reg			address
//				data		send data
//	return:		void
//	note:		datasheet page 98
==========================================================*/
/*void WriteReg(unsigned char add,unsigned char value)
{
	unsigned char xdata  *xdata regpointer;
	regpointer = (unsigned char xdata *)0x8000;
	regpointer = regpointer + add;
	*regpointer = value;
}*/

/*==========================================================
//	function:	write data to add
//	Parameter:	reg			address
//				data		send data
//	return:		void
//	note:		datasheet page 98
==========================================================*/
/*unsigned char ReadReg(unsigned char reg)
{
	unsigned char  xdata returnvalue;
	
	unsigned char xdata *xdata regpointer;
	
	regpointer =(unsigned char *)0x8000;		//8000这个位置存放寄存器的值
	regpointer = regpointer + reg;
	returnvalue = *regpointer;
	return returnvalue;
}*/

/*==========================================================
//	function:	Initializes the PHY using Autonegotiation
//	Parameter:	none
//	return:		unsigned char	0 on success, or the following error code
//	note:		cp220x_core.c, datasheet page 88
==========================================================*/
unsigned char PHY_Init()
{
   unsigned char temp_char,i;
   unsigned char retval = 0;
   printf ("\STATA PHYINT\n");
   temp_char=INT0;
  //printf ("\nINT0RD_Synchronization=%.2x \n",(unsigned  int)INT0RD);
   //--------------------------------------------------------------------------
   // Auto-Negotiation Synchronization (Section 15.2 of CP220x Datasheet)
   //--------------------------------------------------------------------------

   // Step 1: Disable the PHY
      PHYCN = 0x00;  
   // Step 2: Enable the PHY with link integrity test and auto-negotiation 
   // turned off
      
      // A. Disable the Transmitter Power Save Option and Configure Options
      TXPWR = 0x80;
      PHYCF = ( SMSQ | JABBER | ADPAUSE | AUTOPOL );

      // B. Enable the Physical Layer
      PHYCN = PHYEN;
	  
      Delay1ms(20);
      

      // C. Wait for the physical layer to power up
     // wait_ms(10);

      // D. Enable the Transmitter and Receiver
      PHYCN = ( PHYEN | PHYTXEN | PHYRXEN );
      Delay1ms(200);
    
   // Step 3: Poll the Wake-on-Lan Interrupt
     
      // A. Clear Interrupt Flags
     temp_char = INT1;
   //printf ("\nINT1RD=%.2x \n",(unsigned  int)temp_char);
      // B. Start a new timeout for 1.5 seconds
   //      reset_timeout(ONE_SECOND+ONE_SECOND/2);
      // C. Check for a signal
     for(i = 0; i < 150; i++)
	  {
      	Delay1ms(10);
		temp_char =INT1RD;
		if(temp_char & WAKEINT)
		{
			Delay1ms(250);
			break;
		}
	  }
         // If no signal is deteced, wait 1.5s, then continue
//         if(timeout_expired()){
//            break;
 //        }

     
//printf ("\nINT0RD33=%.2x \n",(unsigned  int)INT0RD);
//printf ("\nINT1RD_Negotiation=%.2x \n",(unsigned  int)INT1RD);
   //--------------------------------------------------------------------------
   // Physical Layer Initialization (Section 15.7 of CP220x Datasheet)
   //--------------------------------------------------------------------------
   
   // Step 1: Synchronization procedure implemented above

   // Step 2: Disable the physical layer
   PHYCN = 0x00; 
  
   // Step 3: Configure the desired physical layer options including 
   // auto-negotiation and link integrity
   PHYCF = ( SMSQ | LINKINTG | JABBER | AUTONEG | ADPAUSE | AUTOPOL );
  
   // Step 4: Enable the physcial layer
    
      // A. Enable the Physical Layer
      PHYCN = PHYEN;
	
      Delay1ms(100);
     
      // B. Wait for the physical layer to power up
//      wait_ms(10);
   //printf ("\nINT1RD22=%.2x \n",(unsigned  int)INT1RD);
      // C. Enable the Transmitter and Receiver
      // Auto-negotiation begins now
      PHYCN = ( PHYEN | PHYTXEN | PHYRXEN );	

   // Step 5: Wait for auto-negotiation to complete

      // Clear INT1 Interrupt Flags
      temp_char = INT1;
//      reset_timeout(6*ONE_SECOND);
      // Check for autonegotiation fail or complete flag
  //  printf ("\nINT1RD_autonegotiation_start=%.2x \n",(unsigned  int)INT1RD);
              
             
    for(i = 0; i < 60; i++)
	  {
         Delay1ms(250);
		// temp_char = ReadReg(INT1RD);
		 // If Auto-Negotiation Completes/Fails, break
         if(INT1RD & (ANCINT | ANFINT))
		 {
            break;            
         }
      }


      // Mask out all bits except for auto negotiation bits 
      temp_char = INT1RD;
      temp_char &= (ANCINT | ANFINT);

      // Check if Auto-Negotiation has FAILED 
      if(temp_char & ANFINT)
	  {
      
         // Auto-Negotiation has failed
         retval = LINK_ERROR;

      }
	  else   
      // Check if Auto-Negotiation has PASSED   
      if(temp_char == ANCINT)
	  {
      
         // Auto-Negotiation has passed
         retval = 0;

         // Enable Link LED and Activity LED                         
         IOPWR=0x04;   
         
      }
	  else 
   
      // Timeout Occured.   
      { 
          // Timeout
         retval = LINK_ERROR;
    
      }
   return retval;
     
}

/*==========================================================
//	function:	Writes the value <mac_reg_data> to the indirect MAC register located at 
// 				<mac_reg_offset>.
//	Parameter:	mac_reg_offset	indirect register address
//				mac_reg_data	the data to write to mac_reg_offset
//	return:		void

//	note:		cp220x_core.c, datasheet page 88

==========================================================*/
void MAC_Write(unsigned char mac_reg_offset, unsigned int mac_reg_data)
{

   // Step 1: Write the address of the indirect register to MACADDR.
   MACADDR=mac_reg_offset;              

   // Step 2: Copy the contents of <mac_reg_data> to MACDATAH:MACDATAL
   MACDATAH=(mac_reg_data >> 8);    // Copy High Byte
   MACDATAL=(mac_reg_data & 0xFF);  // Copy Low Byte

   // Step 3: Perform a write on MACRW to transfer the contents of MACDATAH:MACDATAL
   // to the indirect MAC register.
   MACRW=0;
   
   return;
}


/*==========================================================
//	function:	Sets the current MAC address to the MAC address pointed to by <pMAC>.
//	Parameter:	pMAC			pointer to a netnode
//	return:		void
//	note:		cp220x_core.c, datasheet page 80
==========================================================*/
void MAC_SetAddress(union NetNode *pMAC)
{
   INT16U MAC0,MAC1,MAC2;
   MAC0=Swap16U(pMAC->nodebytes.macwords[2]);
   MAC1=Swap16U(pMAC->nodebytes.macwords[1]);
   MAC2=Swap16U(pMAC->nodebytes.macwords[0]);
   MAC_Write(MACAD0, MAC0);
   MAC_Write(MACAD1, MAC1);
   MAC_Write(MACAD2, MAC2);  
   return;
}

/*==========================================================
//	function:	Initializes the MAC and programs the MAC address using the MAC address
// 				stored at address 0x1FFA in CP220x Flash.
//	Parameter:	none
//	return:		void
//	note:		cp220x_core.c, datasheet page 78
==========================================================*/
void MAC_Init(void)
{  

   
   // Check the duplex mode and perform duplex-mode specific initializations
   
   if(PHYCN & 0x10)
   {
      
      // The device is in full-duplex mode, configure MAC registers
      // Padding is turned on.
      MAC_Write(MACCF, 0x40B3);
      MAC_Write(IPGT, 0x0015);
      
   } else {

      // The device is in half-duplex mode, configure MAC registers
      // Padding is turned off.
      MAC_Write(MACCF, 0x4012);
      MAC_Write(IPGT, 0x0012);

   }

   // Configure the IPGR register
   MAC_Write(IPGR, 0x0C12);

   // Configure the MAXLEN register to 1518 bytes
   MAC_Write(MAXLEN, 0x05EE);

   // Copy MAC Address Stored in Flash to MYMAC
   FLASHADDRH=0x1F;
   FLASHADDRL=0xFA;

   myNode.node.mac[0] = FLASHAUTORD;
   myNode.node.mac[1] = FLASHAUTORD;
   myNode.node.mac[2] = FLASHAUTORD;
   myNode.node.mac[3] = FLASHAUTORD;
   myNode.node.mac[4] = FLASHAUTORD;
   myNode.node.mac[5] = FLASHAUTORD;    

   // Program the MAC address
   MAC_SetAddress(&myNode); 

   // Enable Reception and configure Loopback mode
   MAC_Write(MACCN, 0x0001);           // Enable Reception without loopback
 	 RXHASHH=0xff;
 	RXHASHL=0xff;
 	 RXFILT=0x0c;
}

/*==========================================================
//	function:	read 8k flash data
//	Parameter:	hAdd			high address, max 0x1F
//				lAdd			low address
//	return:		unsigned char	read data
//	note:		cp220x_core.c, datasheet page 75
//	write:		han
==========================================================*/
 /*unsigned char ReadFlash(unsigned char hAdd,unsigned char lAdd)
{
	unsigned char tmp;
	if(hAdd > 0x1F)
		return 0;
	WriteReg(FLASHADDRH,hAdd);
	WriteReg(FLASHADDRL,lAdd);
	tmp = ReadReg(FLASHAUTORD);
	return tmp;
}*/


/*==========================================================
//	function:	sends an IEEE 802.3 Ethernet packet using the CP220x.
//	Parameter:	pbuf			指向要发送的MAC数据包的指针
//				buffer_length		length of buffer 从目的MAC到数据末尾的长度
//	return:		void
//        (8 bytes)  48-bit  48-bit    16-bit   0-1500 bytes   
//  ----------------------------------------------------------------------
// | Preamble| SFD | Dest |Source| Type/Length |Data Field | Pad |  FCS  |
// |         |     | Addr | Addr |    Field    |           |     | (CRC) |
//  ----------------------------------------------------------------------
//    supplied by  |         supplied by the MCU                 | supplied
//      CP220x     |          (minimum 64 bytes)                 | by CP220x 
==========================================================*/
void CP220x_Send( const unsigned char xdata *pbuf, unsigned int buffer_length)
{

   int i; 
   unsigned int ramaddr;
   unsigned char tmp;

   // Define Macro to increment the RAM address Pointer
   #define INC_RAMADDR  {ramaddr++; \
                        RAMADDRH=(ramaddr >> 8);\
                        RAMADDRL=(ramaddr & 0x00FF);}


⌨️ 快捷键说明

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