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

📄 cp2200.c.bak

📁 CP2201和51单片机实现ARP ICMP IP UDP协议
💻 BAK
📖 第 1 页 / 共 2 页
字号:
   // Step 1: Poll TXBUSY until it becomes 0x00
   tmp = TXBUSY;
   while(tmp)
   {
    	tmp = TXBUSY;
	}

   // Step 2: Set the TXSTARTH:TXSTARTL address to 0x0000
   TXSTARTH=0x00;
   TXSTARTL=0x00;


   // Step 3: Load data into transmit buffer
   // When the random access method is used, we do not need to check for
   // aborted packets. This method will be slightly slower than the Autowrite
   // method, however, it reduces code space requirements.
  
      // Setup RAM Address Pointer To 0x0000	
      RAMADDRH=0x00;
      RAMADDRL=0x00;
      ramaddr = 0x0000;
      
         //	printf ("\nsend\n");	
		// Step 3a: Load the source address
      // Step 3d: Load the packet payload
	  for(i = 0; i < buffer_length;i++)
	  {
			  /*if((i%10)==0)
                  printf ("\n");
           		 printf ("%.2x", (unsigned int)*pbuf);
           		 printf (" ");*/
		          RAMTXDATA=*pbuf++;
		          INC_RAMADDR;
	  }
	  
           		printf ("\n");	
      
      // Step 3e: Pad short packets
      while(ramaddr < 64)
	  {
         RAMTXDATA=0;
         INC_RAMADDR;
      }
      
      // Set the TXENDH:TXENDL address to <ramaddr - 1>
      ramaddr--;
      TXENDH=(ramaddr >> 8);
      TXENDL=(ramaddr & 0x00FF);


   // Step 4: Set the TXSTARTH:TXSTARTL address back to 0x0000
   TXSTARTH=0x00;
   TXSTARTL=0x00;
   
   // Step 5: Write '1' to TXGO to begin transmission
   TXCN=0x01;
}

/*==========================================================
//	function:	reads the current packet from the CP220x receive buffer and 
// 				copies it to the passed buffer.
//	Parameter:	rxdnet			received packet buffer, max 314bytes
//	return:		unsigned int	number of bytes added to the buffer
//	note:		cp220x_eth.c, datasheet page 58
//  --------------------------------------------------------------------------
// | Preamble | SFD | Dest | Source | Type/Length |  Data Field | Pad |  FCS  |
// |          |     | Addr |  Addr  |    Field    |             |     | (CRC) |
//  --------------------------------------------------------------------------
//     supplied by  |           supplied by the MCU             | supplied by 
//       CP220x     |                                           |    CP220x  
//-----------------------------------------------------------------------------
==========================================================*/
unsigned int CP220x_Receive( INT8U xdata *inbuf)
{
   unsigned char rx_ok;
   unsigned char skip = 0;
   unsigned int cplen;   
   unsigned int i;
   unsigned char tmp;
   unsigned char interrupt_read;
   unsigned char valid_bits;
   unsigned char num_packets;
   // Clear interrupt flags.
   interrupt_read = INT1;
   interrupt_read = INT0;
   
   // Check for packet received interrupt
   if( interrupt_read & RXINT)
   {   
      // Count the number of packets in the receive buffer         
      // This is equal to the number of bits set to 1 in TLBVALID
      valid_bits = TLBVALID;     
      for(num_packets = 0; valid_bits; num_packets++)      
        {                                 
         valid_bits &= valid_bits - 1; 
        }
   
      // If the receive buffer has 7 packets, then disable reception.
      if( num_packets >= 7) 
      {
         RXCN = RXINH;           // Inhibit New Packet Reception
      }
  }

   // Step 1: Check the RXOK bit to see if packet was received correctly
   rx_ok = ((CPINFOL & RXOK) && (CPINFOH & RXVALID));
   
   // Step 2: If packet received correctly, read the length, otherwise, skip packet.
   if(rx_ok)
   {
   
      // Read the packet length
      cplen = ((unsigned int)(CPLENH << 8));
      cplen += CPLENL;
        
   }
   else
   {
      // Set packet length to zero
      cplen = 0;
      // Skip packet
      skip = 1;      
   }   

   // Step 3: Read the entire packet from the buffer 
	if(cplen <=318)
	{   
		printf ("recevied\n");
		for(i = 0; i < cplen; i++)
		{
		    inbuf[i] = RXAUTORD;
		 /* if((i%10)==0)
                    printf ("\n");
            	    printf ("%.2x", (unsigned int)inbuf[i]); 
           	    printf (" ");*/
		}
		num_bytes = cplen-4;

                  // printf ("\n");
	}
	else
	{
      // Set packet length to zero
      cplen = 0;
      // Skip packet
      skip = 1; 
    }
      
   // Step 4: Skip the packet, or clear the valid bit if the entire packet
   // has been unloaded from the buffer.
   tmp = RXCN;
   if(skip)
   {
      RXCN=(tmp | 0x02);                    // Skip the packet
   } 

   else 
   {
      RXCN=(tmp |= 0x04);                    // Clear the valid bit only
      RXCN&=0x07;
   }

   // If there are no more packets in the receive buffer, enable reception
   tmp = TLBVALID;
   if(tmp == 0x00)
   {
      RXCN=0x00;   
   }
   
   
   // Return the number of bytes added to the buffer
   return cplen-4;
}

unsigned char CPRcv(void)   //判断接受的包是否正确
{
	unsigned char tmp;

	if(CPINFOH & RXVALID)
		tmp = 1;
	else
		tmp = 0;
	return tmp;
}

//-----------------------------------------------------------------------------
// CP220x Flash Routines
//-----------------------------------------------------------------------------
// Not used. Commented to save code space.
//-----------------------------------------------------------------------------
// CPFLASH_ByteRead
//-----------------------------------------------------------------------------
//
// Return Value :
//   unsigned char - the value of the Flash byte.
//
// Parameters   : 
//   1)  unsigned int addr - the address in CP220x Flash.
//
// Reads a Flash byte and returns its value.
//-----------------------------------------------------------------------------
/*
unsigned char CPFLASH_ByteRead (unsigned int addr)
{  

   // Set the Flash Address Pointer to <addr>
   FLASHADDRH = (addr >> 8);           // Copy High Byte
   FLASHADDRL = (addr & 0xFF);         // Copy Low Byte
   
   // Read and Return the value in the Flash Data Register 
   return FLASHDATA;
}
//-----------------------------------------------------------------------------
// poll_flash_busy
//-----------------------------------------------------------------------------
//
// Return Value : 
//    unsigned char - Returns '0' on success or FLASH_ERROR if a problem
//    is encountered.
//
// Parameters   : None
//
// Waits for a Flash operation to start and complete
//
// Return Values:
//
//----------------------------------------------------------------------------- 
unsigned char poll_flash_busy (void)
{
 
   // Start Millisecond Timer and set timeout
   reset_timeout(DEFAULT_TIMEOUT);

   // Wait for operation to end
   while((FLASHSTA & 0x08)){       
      
      if(!AB4_RST_State()){
         #if(UART_ENABLED)
         puts("Reset Pin Driven Low. Could indicate power failure.");
         #endif
         return FLASH_ERROR;
      }
    
     if(timeout_expired()){
         #if(UART_ENABLED)
         puts("Timeout: Flash operation has not ended.");
         #endif
         return FLASH_ERROR;
      }

   }
   
   return 0;
}*/

//-----------------------------------------------------------------------------
// CPFLASH_ByteWrite
//-----------------------------------------------------------------------------
//
// Return Value : 
//    unsigned char - Returns '0' on success or FLASH_ERROR if a problem
//    is encountered.
//
// Parameters   : 
//   1)  unsigned int addr - the address of the Flash byte.
//   2)  unsigned char byte - the data to write to Flash.
//
// Writes the value <byte> to the Flash address <addr>.
//
// Note: The addresses 0x1FFA through 0x1FFF cannot be written using this
//       function because they contain the MAC address.
//
// Note: Software calling this function must ensure that the target Flash
//       byte has been erased (value = 0xFF).
//
// Note: The Flash must be unlocked prior to calling this function.
//----------------------------------------------------------------------------- 
/*
unsigned char CPFLASH_ByteWrite (unsigned int addr, char byte)
{
   

   // Check if address is in-range
   if(addr < 0x1FFA)
   {
      // Set the Flash Address Pointer to <addr>
      FLASHADDRH = (addr >> 8);           // Copy High Byte
      FLASHADDRL = (addr & 0xFF);         // Copy Low Byte
      
      // Write the Flash unlock sequence 0xA5, 0xF1
      FLASHKEY = 0xA5;
      FLASHKEY = 0xF1;

      // Initiate the Flash write
      FLASHDATA = byte;
      
     
      // Wait for the Flash operation to start and complete
      return poll_flash_busy();     
     
   }

   return FLASH_ERROR;
}


//-----------------------------------------------------------------------------
// CPFLASH_PageErase
//-----------------------------------------------------------------------------
//
// Return Value : 
//    unsigned char - Returns '0' on success or FLASH_ERROR if a problem
//    is encountered.
//
// Parameters   : 
//   1)  unsigned int addr - the address of the Flash Page.
//
// Erases the Flash page containing address <addr>.
//
// Note: The last Flash page (0x1E00 - 0x1FFF) cannot be erased using this
//       function because it contains the MAC address.
//
// Note: All data stored on a Flash page will be lost once the page is erased.
//
// Note: The Flash must be unlocked prior to calling this function.
//----------------------------------------------------------------------------- 
unsigned char CPFLASH_PageErase (unsigned int addr)
{
   // Check if address is in-range
   if(addr < 0x1E00)
   {
      // Set the Flash Address Pointer to <addr>
      FLASHADDRH = (addr >> 8);           // Copy High Byte
      FLASHADDRL = (addr & 0xFF);         // Copy Low Byte
      
      // Write the Flash unlock sequence 0xA5, 0xF1
      FLASHKEY = 0xA5;
      FLASHKEY = 0xF1;

      // Initiate the Flash erase
      FLASHERASE = 0x01;
      
      // Wait for the Flash operation to start and complete
      return poll_flash_busy();  
      
   }

   return FLASH_ERROR;
}*/

⌨️ 快捷键说明

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