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

📄 main.c

📁 AVR单片机使用U-IP库的例子
💻 C
字号:
/*****************************************************************************
*  Module Name:       uIP-AVR Port - main control loop shell
*  
*  Created By:        Louis Beaudoin (www.embedded-creations.com)
*
*  Original Release:  September 21, 2002 
*
*  Module Description:  
*  This main control loop shell provides everything required for a basic uIP
*  application using the RTL8019AS NIC
*
*  September 30, 2002
*    Added support for Imagecraft Compiler
*****************************************************************************/

#include "uip.h"
#include "ax88796dev.h"
#include "ax88796.h"
#include "uip_arp.h"

#include "compiler.h"

#include "uart.h"
#include "rprintf.h"
#include "vt100.h"

#define BUF ((struct uip_eth_hdr *)&uip_buf[0])

/*****************************************************************************
*  Periodic Timout Functions and variables
*
*  The periodic timeout rate can be changed depeding on your application
*  Modify these functions and variables based on your AVR device and clock
*    rate
*****************************************************************************/
// poll the uIP periodic function every ~0.5 sec
#define TIMERCOUNTER_PERIODIC_TIMEOUT 15

static unsigned char timerCounter;

void initTimer(void)
{
  // timer overflows every 32.8ms (with 8MHz clock)
  outp( 5, TCCR0 ) ;  // timer0 prescale 1/1024 (5)

  // interrupt on overflow
  sbi( TIMSK, TOIE0 ) ;
	
  timerCounter = 0;
}

SIGNAL(SIG_OVERFLOW0)
{
  timerCounter++;
}

/*****************************************************************************
*  Main Control Loop
*****************************************************************************/
int main(void)
{
	unsigned char i;

	uartInit();
	uartSetBaudRate(115200);
	rprintfInit(uartSendByte);
	vt100ClearScreen();
	rprintf("Welcome to UIP-AVR\r\n");

	// init device driver
	ax88796devInit();

	// init uIP
	uip_init();

	// init app
	appInit();

	// init ARP cache
	uip_arp_init();

	// init periodic timer
	initTimer();
  
	sei();

	rprintf("Starting packet receive loop\r\n");
	while(1)
	{
	    // look for a packet
		uip_len = ax88796devPoll();
		if(uip_len == 0)
		{
			// if timed out, call periodic function for each connection
			if(timerCounter > TIMERCOUNTER_PERIODIC_TIMEOUT)
			{
				timerCounter = 0;
        
				vt100SetCursorPos(18,0);
				ax88796RegDump();

				for(i = 0; i < UIP_CONNS; i++)
				{
					uip_periodic(i);
		
					// transmit a packet, if one is ready
					if(uip_len > 0)
					{
						uip_arp_out();
						ax88796devSend();
					}
				}

				/* Call the ARP timer function every 10 seconds. */
				/* not tested yet
				if(++arptimer == 20)
				{	
					uip_arp_timer();
					arptimer = 0;
				}*/

			}
		}
		else  // packet received
		{
			rprintf("Packet Received with length: %d\r\n",uip_len);

			// process an IP packet
			if(BUF->type == htons(UIP_ETHTYPE_IP))
			{
				// add the source to the ARP cache
				// also correctly set the ethernet packet length before processing
				uip_arp_ipin();
				uip_input();

				// transmit a packet, if one is ready
				if(uip_len > 0)
				{
					uip_arp_out();
					ax88796devSend();
				}
			}
			// process an ARP packet
			else if(BUF->type == htons(UIP_ETHTYPE_ARP))
			{
				uip_arp_arpin();

				// transmit a packet, if one is ready
				if(uip_len > 0)
					ax88796devSend();
			}
		}
	}

	return 1;
}

⌨️ 快捷键说明

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