nic.c

来自「uIP是免费的TCP/IP协议栈,我们将它移植到我们的AVR以太网开发板中」· C语言 代码 · 共 83 行

C
83
字号
/*****************************************************************************
*  Module Name:       NIC Driver Interface for uIP-AVR Port
*  
*  Created By:        Louis Beaudoin (www.embedded-creations.com)
*
*  Original Release:  November 16, 2003 
*
*  Module Description:  
*  Provides three functions to interface with a NIC driver
*  These functions can be called directly from the main uIP control loop
*  to send packets from uip_buf and uip_appbuf, and store incoming packets to
*  uip_buf
*
*
*****************************************************************************/

#include "nic.h"


#define IP_TCP_HEADER_LENGTH 40
#define TOTAL_HEADER_LENGTH (IP_TCP_HEADER_LENGTH+ETHERNET_HEADER_LENGTH)


void nic_init(void)
{
	NICInit();
}


void nic_send(void)
{
	NICBeginPacketSend(uip_len);
	
	// send packet, using data in uip_appdata if over the IP+TCP header size
	if( uip_len <= TOTAL_HEADER_LENGTH )
	{
      NICSendPacketData(uip_buf, uip_len);
	}
	else
	{
      uip_len -= TOTAL_HEADER_LENGTH;
      NICSendPacketData(uip_buf, TOTAL_HEADER_LENGTH);
	  NICSendPacketData((unsigned char *)uip_appdata, uip_len);
	}

	NICEndPacketSend();
}



#if UIP_BUFSIZE > 255
unsigned int nic_poll(void)
#else 
unsigned char nic_poll(void)
#endif /* UIP_BUFSIZE > 255 */
{
	unsigned int packetLength;
	
	packetLength = NICBeginPacketRetreive();

	// if there's no packet or an error - exit without ending the operation
	if( !packetLength )
	  return 0;

	// drop anything too big for the buffer
	if( packetLength > UIP_BUFSIZE )
	{
	  NICEndPacketRetreive();
      return 0;
	}
	
	// copy the packet data into the uIP packet buffer
	NICRetreivePacketData( uip_buf, packetLength );
	NICEndPacketRetreive();
		
#if UIP_BUFSIZE > 255
	return packetLength;
#else 
	return (unsigned char)packetLength;
#endif /* UIP_BUFSIZE > 255 */
		
}

⌨️ 快捷键说明

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