Exemple #1
0
int main(void) {
    sei();
    uint8_t resetSource = MCUSR;
    MCUSR = 0;
    wdt_reset();
    wdt_disable();

    wdt_enable(WDTO_1S);
    WDTCSR |= (1 << WDIE);  //enable watchdog interrupt
    wdt_reset();
    cli();

    clock_init();
    usart_init(1000000, 9600);
    stdout = &uart_str;
    stderr = &uart_str;
    stdin = &uart_str;

    uip_ipaddr_t ipaddr;
    struct timer periodic_timer, arp_timer;
    uint16_t timer_OW, timer_Simple, timer_Count, timer_EEProm, timer_SendData, timer_IOalarm, timer_network;
    timer_OW = 0;
    timer_Simple = 0;
    timer_Count = 0;
    timer_EEProm = 0;
    timer_SendData = 0;
    timer_IOalarm = 0;
    timer_network = 0;
    
    if(resetSource & (1<<WDRF))
    {
        printf("Mega was reset by watchdog...\r\n");
    }

    if(resetSource & (1<<BORF))
    {
        printf("Mega was reset by brownout...\r\n");
    }

    if(resetSource & (1<<EXTRF))
    {
        printf("Mega was reset by external...\r\n");
    }

    if(resetSource & (1<<PORF))
    {
        printf("Mega was reset by power on...\r\n");
    }

//else jtag (disabled)

    //sensorScan = (uint8_t*) & tempbuf;

    if (eepromReadByte(0) == 255 || eepromReadByte(11) == 255)
    {
            printf_P(PSTR("Setting default values\r\n"));
            //Set defaults
            eepromWriteByte(0, 0); //init

            myip[0] = 192;
            myip[1] = 168;
            myip[2] = 1;
            myip[3] = 67; //47 in final versions

            netmask[0] = 255;
            netmask[1] = 255;
            netmask[2] = 255;
            netmask[3] = 0;

            gwip[0] = 192;
            gwip[1] = 168;
            gwip[2] = 1;
            gwip[3] = 1;

            dnsip[0] = 8;
            dnsip[1] = 8;
            dnsip[2] = 8;
            dnsip[3] = 8;

            eepromWriteByte(29, 80);  //web port
            eepromWriteByte(10, 0);  //dhcp off

            save_ip_addresses();
            wdt_reset();

            eepromWriteStr(200, "SBNG", 4);  //default password
            eepromWriteByte(204, '\0');
            eepromWriteByte(205, '\0');
            eepromWriteByte(206, '\0');
            eepromWriteByte(207, '\0');
            eepromWriteByte(208, '\0');
            eepromWriteByte(209, '\0');

            eepromWriteByte(100, 1); //Analog port 0 = ADC
            eepromWriteByte(101, 1); //Analog port 1 = ADC
            eepromWriteByte(102, 1); //Analog port 2 = ADC
            eepromWriteByte(103, 1); //Analog port 3 = ADC
            eepromWriteByte(104, 1); //Analog port 4 = ADC
            eepromWriteByte(105, 1); //Analog port 5 = ADC
            eepromWriteByte(106, 1); //Analog port 6 = ADC
            eepromWriteByte(107, 1); //Analog port 7 = ADC

            eepromWriteByte(110, 0); //Digital port 0 = OUT
            eepromWriteByte(111, 0); //Digital port 1 = OUT
            eepromWriteByte(112, 0); //Digital port 2 = OUT
            eepromWriteByte(113, 0); //Digital port 3 = OUT

      	    wdt_reset();
            for (uint8_t alarm=1; alarm<=4; alarm++)
            {
                    uint16_t pos = 400 + ((alarm-1)*15); //400 415 430 445

                    eepromWriteByte(pos+0, 0); //enabled
                    eepromWriteByte(pos+1, 0); //sensorid
                    eepromWriteByte(pos+2, 0); //sensorid
                    eepromWriteByte(pos+3, 0); //sensorid
                    eepromWriteByte(pos+4, 0); //sensorid
                    eepromWriteByte(pos+5, 0); //sensorid
                    eepromWriteByte(pos+6, 0); //sensorid
                    eepromWriteByte(pos+7, 0); //sensorid
                    eepromWriteByte(pos+8, 0); //sensorid
                    eepromWriteByte(pos+9, '<'); //type
                    eepromWriteByte(pos+10, 0); //value
                    eepromWriteByte(pos+11, 0); //target
                    eepromWriteByte(pos+12, 0); //state
                    eepromWriteByte(pos+13, 0); //reverse
                    eepromWriteByte(pos+14, 0); //not-used
            }

            eepromWriteByte(1, EEPROM_VERSION);
    }
/*
    findSystemID(systemID);

    if (systemID[0] == 0) {
        printf_P(PSTR("No system id found, add a DS2401 or use old software"));
//        fprintf(&lcd_str, "?f?y0?x00No system id found?nAdd a DS2401 or use old software?n");
        wdt_disable();
        wdt_reset();
        while (true);
    } else {
*/    
        //MAC will be 56 51 99 36 14 00 with example system id
        mymac[1] = systemID[1];
        mymac[2] = systemID[2];
        mymac[3] = systemID[3];
        mymac[4] = systemID[4];
        mymac[5] = systemID[5];
//    }
//    fprintf(&lcd_str, "?y1?x00ID: %02X%02X%02X%02X%02X%02X%02X%02X?n", systemID[0], systemID[1], systemID[2], systemID[3], systemID[4], systemID[5], systemID[6], systemID[7]);

    loadSimpleSensorData();

    //Set digital pins based on selections...
    for (uint8_t i=8; i<=11; i++)
    {
            if (simpleSensorTypes[i] == 0)
            {
                    //output
                    SETBIT(DDRC, (i-6));
            } else {
                    //input
                    CLEARBIT(DDRC, (i-6));
            }
    }


    network_init();

    timer_set(&periodic_timer, CLOCK_SECOND / 2);
    timer_set(&arp_timer, CLOCK_SECOND * 10);

    uip_init();

    //sættes hvert for sig for uip og enc, skal rettes til en samlet setting, så vi kan bruge mymac
    struct uip_eth_addr mac = { {UIP_ETHADDR0, UIP_ETHADDR1, UIP_ETHADDR2, UIP_ETHADDR3, UIP_ETHADDR4, UIP_ETHADDR5} };
//    struct uip_eth_addr mac = {mymac[0], mymac[1], mymac[2], mymac[3], mymac[4], mymac[5]};

    uip_setethaddr(mac);
    httpd_init();
    /*
    #ifdef __DHCPC_H__
            dhcpc_init(&mac, 6);
    #else
     */
    uip_ipaddr(ipaddr, myip[0], myip[1], myip[2], myip[3]);
    uip_sethostaddr(ipaddr);
    uip_ipaddr(ipaddr, gwip[0], gwip[1], gwip[2], gwip[3]);
    uip_setdraddr(ipaddr);
    uip_ipaddr(ipaddr, netmask[0], netmask[1], netmask[2], netmask[3]);
    uip_setnetmask(ipaddr);
    //#endif /*__DHCPC_H__*/

    printf("Setting ip to %u.%u.%u.%u \r\n", myip[0], myip[1], myip[2], myip[3]);

    resolv_init();
    uip_ipaddr(ipaddr, dnsip[0], dnsip[1], dnsip[2], dnsip[3]);
    resolv_conf(ipaddr);
    webclient_init();

    printf("Stokerbot NG R3 ready  -  Firmware %u.%u ...\r\n", SBNG_VERSION_MAJOR, SBNG_VERSION_MINOR);
//    fprintf(&lcd_str, "?y2?x00Firmware %u.%u ready.", SBNG_VERSION_MAJOR, SBNG_VERSION_MINOR);

//    SPILCD_init();

    wdt_reset();

    while (1) {
        //Only one event may fire per loop, listed in order of importance
        //If an event is skipped, it will be run next loop
        if (tickDiff(timer_Count) >= 1) {
            timer_Count = tick;
            wdt_reset(); //sikre at watchdog resetter os hvis timer systemet fejler, vi når her hvert 2ms
            updateCounters(); //bør ske i en interrupt istedet, for at garentere 2ms aflæsning
        } else if (tickDiffS(timer_IOalarm) >= 5) {
            printf("Timer : IO alarm \r\n");
            timer_IOalarm = tickS;
            timedAlarmCheck();
        } else if (tickDiffS(timer_OW) >= 2) {
            printf("Timer : OW \r\n");
            timer_OW = tickS;
            updateOWSensors();
        } else if (tickDiffS(timer_Simple) >= 5) {
            printf("Timer : Simple\r\n");
            timer_Simple = tickS;
            updateSimpleSensors();
        } else if (tickDiffS(timer_SendData) >= 59) {
            printf("Timer : webclient \r\n");
            timer_SendData = tickS;
            webclient_connect();
        } else if (tickDiffS(timer_EEProm) >= 60 * 30) {
            printf("Timer : eeprom \r\n");
            timer_EEProm = tickS;
            timedSaveEeprom();
        }

        //Net handling below



        if (tickDiff(timer_network) >= 1)
        {
            timer_network = tick;
            uip_len = network_read();

            if (uip_len > 0) {
                if (BUF->type == htons(UIP_ETHTYPE_IP)) {
                    uip_arp_ipin();
                    uip_input();
                    if (uip_len > 0) {
                        uip_arp_out();
                        network_send();
                    }
                } else if (BUF->type == htons(UIP_ETHTYPE_ARP)) {
                    uip_arp_arpin();
                    if (uip_len > 0) {
                        network_send();
                    }
                }

            } else if (timer_expired(&periodic_timer)) {
                timer_reset(&periodic_timer);
                //FLIPBIT(PORTC,5);
    //            printf("Timers : %u %u \r\n", tick, tickS);

                for (uint8_t i = 0; i < UIP_CONNS; i++) {
                    uip_periodic(i);
                    if (uip_len > 0) {
                        uip_arp_out();
                        network_send();
                    }
                }

    #if UIP_UDP
                for (uint8_t i = 0; i < UIP_UDP_CONNS; i++) {
                    uip_udp_periodic(i);
                    if (uip_len > 0) {
                        uip_arp_out();
                        network_send();
                    }
                }
    #endif /* UIP_UDP */

                if (timer_expired(&arp_timer)) {
                    timer_reset(&arp_timer);
                    uip_arp_timer();
                }
            }
        }
    }
    return 0;
}
void vuIP_Task( void *pvParameters )
{
portBASE_TYPE i;
unsigned long ulNewEvent = 0UL;
unsigned long ulUIP_Events = 0UL;

	( void ) pvParameters;

	/* Initialise the uIP stack. */
	prvInitialise_uIP();

	/* Initialise the MAC. */
	vInitEmac();

	while( lEMACWaitForLink() != pdPASS )
    {
        vTaskDelay( uipINIT_WAIT );
    }

	for( ;; )
	{
		if( ( ulUIP_Events & uipETHERNET_RX_EVENT ) != 0UL )
		{
			/* Is there received data ready to be processed? */
			uip_len = ( unsigned short ) ulEMACRead();

			if( ( uip_len > 0 ) && ( uip_buf != NULL ) )
			{
				/* Standard uIP loop taken from the uIP manual. */
				if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
				{
					uip_arp_ipin();
					uip_input();

					/* If the above function invocation resulted in data that
					should be sent out on the network, the global variable
					uip_len is set to a value > 0. */
					if( uip_len > 0 )
					{
						uip_arp_out();
						vEMACWrite();
					}
				}
				else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
				{
					uip_arp_arpin();

					/* If the above function invocation resulted in data that
					should be sent out on the network, the global variable
					uip_len is set to a value > 0. */
					if( uip_len > 0 )
					{
						vEMACWrite();
					}
				}
			}
			else
			{
				ulUIP_Events &= ~uipETHERNET_RX_EVENT;
			}
		}

		if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )
		{
			ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;

			for( i = 0; i < UIP_CONNS; i++ )
			{
				uip_periodic( i );

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					uip_arp_out();
					vEMACWrite();
				}
			}
		}

		/* Call the ARP timer function every 10 seconds. */
		if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )
		{
			ulUIP_Events &= ~uipARP_TIMER_EVENT;
			uip_arp_timer();
		}

		if( ulUIP_Events == pdFALSE )
		{
			xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );
			ulUIP_Events |= ulNewEvent;
		}
	}
}
Exemple #3
0
int main(void)
{

	network_init();
	// network.c
	// enc28j60Init();
	// enc28j60PhyWrite(PHLCON,0x476);

	//CLKPR = (1<<CLKPCE);	//Change prescaler
	//CLKPR = (1<<CLKPS0);	//Use prescaler 2
	//clock_prescale_set(clock_div_2);
	enc28j60Write(ECOCON, 1 & 0x7);	
	// enc28j60.c
	//Get a 25MHz signal from enc28j60

	#ifdef MY_DEBUG
	uartInit();
	uartSetBaudRate(9600);
	rprintfInit(uartSendByte);
	#endif

	int i;
	uip_ipaddr_t ipaddr; // uip.h 
	// typedef u16_t uip_ip4addr_t[2]; 
	// typedef uip_ip4addr_t uip_ipaddr_t;
	struct timer periodic_timer, arp_timer;

	clock_init();


	timer_set(&periodic_timer, CLOCK_SECOND / 2);
	timer_set(&arp_timer, CLOCK_SECOND * 10);

	uip_init();
	// uip.c

	//uip_arp_init();
	// uip_arp.c
	// must be done or sometimes arp doesn't work
	
	struct uip_eth_addr mac = {UIP_ETHADDR0, UIP_ETHADDR1, UIP_ETHADDR2, UIP_ETHADDR3, UIP_ETHADDR4, UIP_ETHADDR5};

	uip_setethaddr(mac);

	simple_httpd_init();

#ifdef __DHCPC_H__
	dhcpc_init(&mac, 6);
#else
    uip_ipaddr(ipaddr, 192,168,0,1); // uip.h
	uip_sethostaddr(ipaddr); // uip.h
	// #define uip_sethostaddr(addr) uip_ipaddr_copy(uip_hostaddr, (addr))
    uip_ipaddr(ipaddr, 192,168,0,1);
    uip_setdraddr(ipaddr);
	// #define uip_setdraddr(addr) uip_ipaddr_copy(uip_draddr, (addr))
    uip_ipaddr(ipaddr, 255,255,255,0);
    uip_setnetmask(ipaddr);
	// #define uip_setnetmask(addr) uip_ipaddr_copy(uip_netmask, (addr))

#endif /*__DHCPC_H__*/


	while(1){

		uip_len = network_read(); 
		// uip.c : u16_t uip_len;
		// network.c : return ((unt16_t) enc28j60PacketReceive(UIP_BUFSIZE, (uint8_t *)uip_buf)); 
		// enc28j60.c : enc28j60PacketReceive
		// uip.c : uint8_t uip_buf[UIP_BUFSIZE+2]; 
		// uipconf.h : UIP_BUFSIZE:300
		if(uip_len > 0) {
			if(BUF->type == htons(UIP_ETHTYPE_IP)){  
			#ifdef MY_DEBUG
			//rprintf("eth in : uip_len = %d, proto = %d\n", uip_len, uip_buf[23]);
			//debugPrintHexTable(uip_len, uip_buf);
			#endif
			// struct uip_eth_hdr {
			//	struct uip_eth_addr dest;
			//	struct uip_eth_addr src;
			//	u16_t type;
			//	};
			// struct uip_eth_addr { // Representation of a 48-bit Ethernet address
			// 	u8_t addr[6];
			// };
			//	http://www.netmanias.com/ko/post/blog/5372/ethernet-ip-tcp-ip/packet-header-ethernet-ip-tcp-ip
			//	UIP_ETHTYPE_IP(0x0800) : IPv4 Packet(ICMP, TCP, UDP)
				uip_arp_ipin();
				#ifdef MY_DEBUG
				//rprintf("ip in : uip_len = %d, proto = %d\n", uip_len, uip_buf[23]);
				//debugPrintHexTable(uip_len, uip_buf+14);
				#endif
				// uip_arp.c
				// #define IPBUF ((struct ethip_hdr *)&uip_buf[0])
				// struct ethip_hdr {
				// 	 struct uip_eth_hdr ethhdr;
				// 	 // IP header
				// 	 u8_t vhl,
				// 	 	tos,
				// 	 	len[2],
				// 	 	ipid[2],
				// 	 	ipoffset[2],
				// 	 	ttl,
				// 	 	proto; // ICMP: 1, TCP: 6, UDP: 17
				// 	 u16_t ipchksum;
				// 	 u16_t srcipaddr[2],
				// 	 destipaddr[2];
				// }
				// if ((IBUF->srcipaddr & uip_netmask) != uip_hostaddr & (uip_netmask)) return;
				// uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src));
				uip_input();
				#ifdef MY_DEBUG
				//rprintf("ip out : uip_len = %d, proto = %d\n", uip_len, uip_buf[23]);
				//debugPrintHexTable(uip_len, uip_buf+14);
				#endif
				// ip out packet
				// eg ICMP 
				// uip_len : 84
				// source ip <-> destination ip
				// type : 8 (Echo (ping) request) -> 0 (Echo (ping) reply)
				// uip.h
				// #define uip_input()        uip_process(UIP_DATA) // UIP_DATA(1) : Tells uIP that there is incoming
				// uip_process : The actual uIP function which does all the work.
				if(uip_len > 0) {
					uip_arp_out();
					#ifdef MY_DEBUG
					//rprintf("ip out : uip_len = %d, proto = %d\n", uip_len, uip_buf[23]);
					//debugPrintHexTable(uip_len, uip_buf);
					#endif
					// Destination MAC Address <=> Source MAC Address
					// w/o Ethernet CRC
					// uip_arp.c

					network_send();
					// network.c
					// if(uip_len <= UIP_LLH_LEN + 40){ // UIP_LLH_LEN : 14
					// 	enc28j60PacketSend(uip_len, (uint8_t *)uip_buf, 0, 0);
					// }else{
					// enc28j60PacketSend(54, (uint8_t *)uip_buf , uip_len - UIP_LLH_LEN - 40, (uint8_t*)uip_appdata);
					// }
				}
			}else if(BUF->type == htons(UIP_ETHTYPE_ARP)){
				// UIP_ETHYPE_ARP(0x0806)
				#ifdef MY_DEBUG
				//rprintf("arp in : uip_len = %d\n", uip_len);
				//debugPrintHexTable(uip_len, uip_buf);
				#endif
				// arp in : 64 bytes
				uip_arp_arpin();
				if(uip_len > 0){ // always uip_len > 0
					#ifdef MY_DEBUG
					//rprintf("ip in : uip_len = %d\n", uip_len);
					//debugPrintHexTable(uip_len, uip_buf);
					#endif
					// arp out : 42 bytes
					// 64 - Ethernet_padding(18) - Ethernet_CRC(4)
					// Send MAC address <--> Target MAC address
					// Send IP address <--> Target IP address
					// uip_arp.c
					network_send();
				}
			}

		}else if(timer_expired(&periodic_timer)) {

			timer_reset(&periodic_timer);

			for(i = 0; i < UIP_CONNS; i++) {
				uip_periodic(i);
                // uip.h
                // #define uip_udp_periodic(conn) do { uip_udp_conn = &uip_udp_conns[conn]; \
                // uip_process(UIP_UDP_TIMER); } while (0) // UIP_UDP_TIMER : 5
                // uip.c; uip_process
                // if(flag == UIP_UDP_TIMER) {
                //  if(uip_udp_conn->lport != 0) {
                //      uip_conn = NULL;
                //      uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
                //      uip_len = uip_slen = 0;
                //      uip_flags = UIP_POLL;
                //      UIP_UDP_APPCALL();
                //      goto udp_send;
                //  }
                // } else {
                //  goto drop;
                // }
				if(uip_len > 0) {
					uip_arp_out();
					network_send();
				}
			}

			#if UIP_UDP
			for(i = 0; i < UIP_UDP_CONNS; i++) {
				uip_udp_periodic(i);
				if(uip_len > 0) {
					uip_arp_out();
					network_send();
				}
			}
			#endif /* UIP_UDP */

			if(timer_expired(&arp_timer)) {
				timer_reset(&arp_timer);
				uip_arp_timer();
			}
		}
	}
	return 0;
}
Exemple #4
0
void vuIP_Task( void *pvParameters )
{
portBASE_TYPE i;
unsigned long ulNewEvent = 0UL, ulUIP_Events = 0UL;
long lPacketLength;

	/* Just to prevent compiler warnings about the unused parameter. */
	( void ) pvParameters;

	/* Initialise the uIP stack, configuring for web server usage. */
	prvInitialise_uIP();

	/* Initialise the MAC and PHY. */
	prvInitEmac();

	for( ;; )
	{
		/* Is there received data ready to be processed? */
		lPacketLength = MSS_MAC_rx_packet();

		/* Statements to be executed if data has been received on the Ethernet. */
		if( ( lPacketLength > 0 ) && ( uip_buf != NULL ) )
		{
			uip_len = ( u16_t ) lPacketLength;
			
			/* Standard uIP loop taken from the uIP manual. */
			if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
			{
				uip_arp_ipin();
				uip_input();

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					uip_arp_out();
					vEMACWrite();
				}
			}
			else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
			{
				uip_arp_arpin();

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					vEMACWrite();
				}
			}
		}
		else
		{
			/* Clear the RX event latched in ulUIP_Events - if one was latched. */
			ulUIP_Events &= ~uipETHERNET_RX_EVENT;
		}

		/* Statements to be executed if the TCP/IP period timer has expired. */
		if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )
		{
			ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;

			if( uip_buf != NULL )
			{
				for( i = 0; i < UIP_CONNS; i++ )
				{
					uip_periodic( i );
	
					/* If the above function invocation resulted in data that
					should be sent out on the network, the global variable
					uip_len is set to a value > 0. */
					if( uip_len > 0 )
					{
						uip_arp_out();
						vEMACWrite();
					}
				}
			}
		}

		/* Statements to be executed if the ARP timer has expired. */
		if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )
		{
			ulUIP_Events &= ~uipARP_TIMER_EVENT;
			uip_arp_timer();
		}

		/* If all latched events have been cleared - block until another event
		occurs. */
		if( ulUIP_Events == pdFALSE )
		{
			xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );
			ulUIP_Events |= ulNewEvent;
		}
	}
}
int main(void)
{
	unsigned int i;
	uip_ipaddr_t ipaddr;	/* local IP address */
	struct timer periodic_timer, arp_timer;

	char uart_test[]= "\nWelcome to smart home debugger";

	// system init
	SystemInit();                                      /* setup core clocks */

	// clock init
	clock_init();

	//uart init..
	UARTInit(3, 38400);
	UARTSend(3, (uint8_t *)uart_test, sizeof(uart_test) );	
	

	// two timers for tcp/ip
	timer_set(&periodic_timer, CLOCK_SECOND / 2); /* 0.5s */
	timer_set(&arp_timer, CLOCK_SECOND * 10);	/* 10s */	

	// ethernet init
	tapdev_init();

	// Initialize the uIP TCP/IP stack.
	uip_init();

	uip_ipaddr(ipaddr, 192,168,1,210);
	//uip_ipaddr(ipaddr, 192,168,0,210);
	uip_sethostaddr(ipaddr);	/* host IP address */

	uip_ipaddr(ipaddr, 192,168,1,1);	//this is for the case when the uc is connected to the internet
	//uip_ipaddr(ipaddr, 192,168,0,1); //this is for my local network
	uip_setdraddr(ipaddr);	/* router IP address */

	uip_ipaddr(ipaddr, 255,255,255,0);
	uip_setnetmask(ipaddr);	/* mask */

	// Initialize the HTTP server, listen to port 80.
	//httpd_init();
	scadapp_init();

	

	while(1)
	{
 	/* receive packet and put in uip_buf */
		uip_len = tapdev_read(uip_buf);
    	if(uip_len > 0)		/* received packet */
    	{ 
      		if(BUF->type == htons(UIP_ETHTYPE_IP))	/* IP packet */
      		{
	      		uip_arp_ipin();	
	      		uip_input();
	      		/* If the above function invocation resulted in data that
	         		should be sent out on the network, the global variable
	         		uip_len is set to a value > 0. */
 
	      		if(uip_len > 0)
        		{
	        		uip_arp_out();	
	        		tapdev_send(uip_buf,uip_len);
	      		}
      		}
	      	else if(BUF->type == htons(UIP_ETHTYPE_ARP))	/*ARP packet */
	      	{
	        	uip_arp_arpin();
		      	/* If the above function invocation resulted in data that
		         	should be sent out on the network, the global variable
		         	uip_len is set to a value > 0. */
		      	if(uip_len > 0)
	        	{
		        	tapdev_send(uip_buf,uip_len);	/* ARP ack*/
		      	}
	      	}
    	}
    	else if(timer_expired(&periodic_timer))	/* no packet but periodic_timer time out (0.5s)*/
    	{
      		timer_reset(&periodic_timer);
	  
      		for(i = 0; i < UIP_CONNS; i++)
      		{
      			uip_periodic(i);
		        /* If the above function invocation resulted in data that
		           should be sent out on the network, the global variable
		           uip_len is set to a value > 0. */
		        if(uip_len > 0)
		        {
		          uip_arp_out();
		          tapdev_send(uip_buf,uip_len);
		        }
      		}
#if UIP_UDP
			for(i = 0; i < UIP_UDP_CONNS; i++) {
				uip_udp_periodic(i);
				/* If the above function invocation resulted in data that
				   should be sent out on the network, the global variable
				   uip_len is set to a value > 0. */
				if(uip_len > 0) {
				  uip_arp_out();
				  tapdev_send();
				}
			}
#endif /* UIP_UDP */
	     	/* Call the ARP timer function every 10 seconds. */
			if(timer_expired(&arp_timer))
			{
				timer_reset(&arp_timer);
				uip_arp_timer();
			}
    	}
	}
}
/*---------------------------------------------------------------------------*/
int
main(void)
{
  int i;
  uip_ipaddr_t ipaddr;
  struct uip_timer periodic_timer, arp_timer;
  
  timer_set(&periodic_timer, CLOCK_SECOND / 2);
  timer_set(&arp_timer, CLOCK_SECOND * 10);
  
  network_device_init();
  uip_init();

  uip_ipaddr(ipaddr, 192,168,0,2);
  uip_sethostaddr(ipaddr);

  httpd_init();
  
  while(1) {
    uip_len = network_device_read();
    if(uip_len > 0) {
      if(BUF->type == htons(UIP_ETHTYPE_IP)) {
	uip_arp_ipin();
	uip_input();
	/* If the above function invocation resulted in data that
	   should be sent out on the network, the global variable
	   uip_len is set to a value > 0. */
	if(uip_len > 0) {
	  uip_arp_out();
	  network_device_send();
	}
      } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
	uip_arp_arpin();
	/* If the above function invocation resulted in data that
	   should be sent out on the network, the global variable
	   uip_len is set to a value > 0. */
	if(uip_len > 0) {
	  network_device_send();
	}
      }

    } else if(timer_expired(&periodic_timer)) {
      timer_reset(&periodic_timer);
      for(i = 0; i < UIP_CONNS; i++) {
	uip_periodic(i);
	/* If the above function invocation resulted in data that
	   should be sent out on the network, the global variable
	   uip_len is set to a value > 0. */
	if(uip_len > 0) {
	  uip_arp_out();
	  network_device_send();
	}
      }

#if UIP_UDP
      for(i = 0; i < UIP_UDP_CONNS; i++) {
	uip_udp_periodic(i);
	/* If the above function invocation resulted in data that
	   should be sent out on the network, the global variable
	   uip_len is set to a value > 0. */
	if(uip_len > 0) {
	  uip_arp_out();
	  network_device_send();
	}
      }
#endif /* UIP_UDP */
      
      /* Call the ARP timer function every 10 seconds. */
      if(timer_expired(&arp_timer)) {
	timer_reset(&arp_timer);
	uip_arp_timer();
      }
    }
  }
  return 0;
}
Exemple #7
0
void stack_process(chanend tx_mp3, chanend tcp_chan)
{
	int i;

	uip_len = network_read();

	if (uip_len > 0)
	{
		if (BUF->type == htons(UIP_ETHTYPE_IP))
		{
			// printstrln("ETHTYPE_IP");
			uip_arp_ipin();
			uip_input(tx_mp3, tcp_chan);
			/* If the above function invocation resulted in data that
			   should be sent out on the network, the global variable
			   uip_len is set to a value > 0. */
			if (uip_len > 0)
			{
				uip_arp_out();
				network_send();
			}
		}
		else if (BUF->type == htons(UIP_ETHTYPE_ARP))
		{
			// printstrln("ETHTYPE_ARP");
			uip_arp_arpin();
			if (uip_len > 0)
			{
				network_send();
			}
		}

	}
	else if (timer_expired(&periodic_timer))
	{
		timer_reset(&periodic_timer);

		for (i = 0; i < UIP_CONNS; i++)
		{
			uip_periodic(i, tx_mp3, tcp_chan);
			if(uip_len > 0)
			{
				uip_arp_out();
				network_send();
			}
		}

		for (i = 0; i < UIP_UDP_CONNS; i++)
		{
			uip_udp_periodic(i, tx_mp3, tcp_chan);
			if (uip_len > 0)
			{
				uip_arp_out();
				network_send();
			}
		}

		// if nothing to TX and the self ARP timer expired
		// TX a broadcast ARP reply. This was implemented to
		// cause periodic TX to prevent the AP from disconnecting
		// us from the network
		if (uip_len == 0 && timer_expired(&self_arp_timer)) // 30s 
		{
			timer_reset(&self_arp_timer);
			uip_self_arp_out();
			network_send();
		}

		/* Call the ARP timer function every 10 seconds. */
		if(timer_expired(&arp_timer))
		{
			timer_reset(&arp_timer);
			uip_arp_timer();
		}
	}
}
Exemple #8
0
/** Processes Incoming packets to the server from the connected RNDIS device, creating responses as needed. */
static void uIPManagement_ProcessIncomingPacket(void)
{
	/* Determine which USB mode the system is currently initialized in */
	if (USB_CurrentMode == USB_MODE_Device)
	{
		/* If no packet received, exit processing routine */
		if (!(RNDIS_Device_IsPacketReceived(&Ethernet_RNDIS_Interface_Device)))
		  return;

		LEDs_SetAllLEDs(LEDMASK_USB_BUSY);

		/* Read the Incoming packet straight into the UIP packet buffer */
		RNDIS_Device_ReadPacket(&Ethernet_RNDIS_Interface_Device, uip_buf, &uip_len);
	}
	else
	{
		/* If no packet received, exit processing routine */
		if (!(RNDIS_Host_IsPacketReceived(&Ethernet_RNDIS_Interface_Host)))
		  return;

		LEDs_SetAllLEDs(LEDMASK_USB_BUSY);

		/* Read the Incoming packet straight into the UIP packet buffer */
		RNDIS_Host_ReadPacket(&Ethernet_RNDIS_Interface_Host, uip_buf, &uip_len);
	}

	/* If the packet contains an Ethernet frame, process it */
	if (uip_len > 0)
	{
		switch (((struct uip_eth_hdr*)uip_buf)->type)
		{
			case HTONS(UIP_ETHTYPE_IP):
				/* Filter packet by MAC destination */
				uip_arp_ipin();

				/* Process Incoming packet */
				uip_input();

				/* If a response was generated, send it */
				if (uip_len > 0)
				{
					/* Add destination MAC to outgoing packet */
					uip_arp_out();

					uip_split_output();
				}

				break;
			case HTONS(UIP_ETHTYPE_ARP):
				/* Process ARP packet */
				uip_arp_arpin();

				/* If a response was generated, send it */
				if (uip_len > 0)
				  uip_split_output();

				break;
		}
	}

	LEDs_SetAllLEDs(LEDMASK_USB_READY);
}
void vuIP_Task( void *pvParameters )
{
portBASE_TYPE i;
uip_ipaddr_t xIPAddr;
struct timer periodic_timer, arp_timer;

	/* To prevent compiler warnings. */
	( void ) pvParameters;

	/* Initialise the uIP stack. */
	timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );
	timer_set( &arp_timer, configTICK_RATE_HZ * 10 );
	uip_init();
	uip_ipaddr( xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
	uip_sethostaddr( xIPAddr );

	/* Initialise the WEB server. */
	httpd_init();

	/* Initialise the Ethernet controller peripheral. */
	vFECInit();

	for( ;; )
	{
		/* Is there received data ready to be processed? */
		uip_len = usFECGetRxedData();

		if( uip_len > 0 )
		{
			/* Standard uIP loop taken from the uIP manual. */

			if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
			{
				uip_arp_ipin();
				uip_input();

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					uip_arp_out();
					vFECSendData();
				}
				else
				{
					/* If we are not sending data then let the FEC driver know
					the buffer is no longer required. */
					vFECRxProcessingCompleted();
				}
			}
			else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
			{
				uip_arp_arpin();

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					vFECSendData();
				}
				else
				{
					/* If we are not sending data then let the FEC driver know
					the buffer is no longer required. */
					vFECRxProcessingCompleted();
				}
			}
			else
			{
				/* If we are not sending data then let the FEC driver know
				the buffer is no longer required. */
				vFECRxProcessingCompleted();
			}
		}
		else
		{
			if( timer_expired( &periodic_timer ) )
			{
				timer_reset( &periodic_timer );
				for( i = 0; i < UIP_CONNS; i++ )
				{
					uip_periodic( i );

					/* If the above function invocation resulted in data that
					should be sent out on the network, the global variable
					uip_len is set to a value > 0. */
					if( uip_len > 0 )
					{
						uip_arp_out();
						vFECSendData();
					}
				}

				/* Call the ARP timer function every 10 seconds. */
				if( timer_expired( &arp_timer ) )
				{
					timer_reset( &arp_timer );
					uip_arp_timer();
				}
			}
			else
			{
				/* We did not receive a packet, and there was no periodic
				processing to perform.  Block for a fixed period.  If a packet
				is received during this period we will be woken by the ISR
				giving us the Semaphore. */
				xSemaphoreTake( xFECSemaphore, configTICK_RATE_HZ / 2 );
			}
		}
	}
}
int main(void) {

	// clock init
	clock_init();

	// two timers for tcp/ip
	timer_set(&periodic_timer, CLOCK_SECOND / 2); /* 0.5s */
	timer_set(&arp_timer, CLOCK_SECOND * 10);	/* 10s */

	diag_init();

	// ethernet init
	tapdev_init();

	// Initialize the uIP TCP/IP stack.
	uip_init();

	uip_ipaddr(ipaddr, MYIP1,MYIP2,MYIP3,MYIP4);
	uip_sethostaddr(ipaddr);
	uip_ipaddr(ipaddr, DRTR1,DRTR2,DRTR3,DRTR4);
	uip_setdraddr(ipaddr);
	uip_ipaddr(ipaddr, SMSK1, SMSK2, SMSK3, SMSK4);
    uip_setnetmask(ipaddr);

	bl_init();

	while(1)
	{
		diag_appcall();
		bl_appcall();

		/* receive packet and put in uip_buf */
		uip_len = tapdev_read(uip_buf);
    	if(uip_len > 0)		/* received packet */
    	{
      		if(BUF->type == htons(UIP_ETHTYPE_IP))	/* IP packet */
      		{
	      		uip_arp_ipin();
	      		uip_input();
	      		/* If the above function invocation resulted in data that
	         		should be sent out on the network, the global variable
	         		uip_len is set to a value > 0. */

	      		if(uip_len > 0)
        		{
	      			uip_arp_out();
	        		tapdev_send(uip_buf,uip_len);
	      		}
      		}
	      	else if(BUF->type == htons(UIP_ETHTYPE_ARP))	/*ARP packet */
	      	{
	        	uip_arp_arpin();
		      	/* If the above function invocation resulted in data that
		         	should be sent out on the network, the global variable
		         	uip_len is set to a value > 0. */
		      	if(uip_len > 0)
	        	{
		        	tapdev_send(uip_buf,uip_len);	/* ARP ack*/
		      	}
	      	}
    	}
    	else if(timer_expired(&periodic_timer))	/* no packet but periodic_timer time out (0.5s)*/
    	{
      		timer_reset(&periodic_timer);

      		for(i = 0; i < UIP_CONNS; i++)
      		{
      			uip_periodic(i);
		        /* If the above function invocation resulted in data that
		           should be sent out on the network, the global variable
		           uip_len is set to a value > 0. */
		        if(uip_len > 0)
		        {
		          uip_arp_out();
		          tapdev_send(uip_buf,uip_len);
		        }
      		}
#if UIP_UDP
			for(i = 0; i < UIP_UDP_CONNS; i++) {
				uip_udp_periodic(i);
				/* If the above function invocation resulted in data that
				   should be sent out on the network, the global variable
				   uip_len is set to a value > 0. */
				if(uip_len > 0) {
				  uip_arp_out();
				  tapdev_send(uip_buf,uip_len);
				}
			}
#endif /* UIP_UDP */
	     	/* Call the ARP timer function every 10 seconds. */
			if(timer_expired(&arp_timer))
			{
				timer_reset(&arp_timer);
				uip_arp_timer();
			}
    	}


	}
	return 0 ;
}
Exemple #11
0
int main(void){
  // Init buffer
  for (int i=0; i<BUFFER_SIZE; i++){
	Buffer[i] = 0;
  }
  
  //uIP
  unsigned int i;
  uip_ipaddr_t ipaddr;
  struct timer periodic_timer, arp_timer;
  clock_init(2);
  timer_set(&periodic_timer, CLOCK_SECOND / 2);
  timer_set(&arp_timer, CLOCK_SECOND * 10);
  
  init();
  
  // Touch init
  ToushRes_t XY_Touch;
  Boolean Touch = FALSE;
  TouchScrInit();
  
  // Init font
  GLCD_SetFont(&Terminal_9_12_6,0xFFFFFF,0x000000);
  
  // Init UART
  UART_init(UART_0,4,NORM);
  
  // Init Real Time Clock
  RTC_init();
  
  // Init animations
  Animation_init();
  
  // Init navigationBar
  navigationBar = initNavigationBar();
  
  // Init pages
  mainLayout = initMainLayout();
  learningLayout = initLearningLayout();
  graphLayout = initGraphLayout();
  devicesLayout = initDevicesLayout();
  
  swapToLayout(0);
  
  // Initialize the ethernet device driver
  do{
	GLCD_TextSetPos(0,0);
  }
  while(!tapdev_init());
  GLCD_TextSetPos(0,0);
  
  // uIP web server
  // Initialize the uIP TCP/IP stack.
  uip_init();
  
  uip_ipaddr(ipaddr, 192,168,0,100);
  uip_sethostaddr(ipaddr);
  uip_ipaddr(ipaddr, 192,168,0,1);
  uip_setdraddr(ipaddr);
  uip_ipaddr(ipaddr, 255,255,255,0);
  uip_setnetmask(ipaddr);
  
  // Initialize the HTTP server.
  httpd_init();
  
  while(1){
	if(TouchGet(&XY_Touch))
	{
	  // Check if the current Layout accepts the touch
	  if (!Layout_dispatchTouch(currentLayout, XY_Touch.X, XY_Touch.Y)){
		// Touch not accepted, pass it on to the navigationBar
		Layout_dispatchTouch(navigationBar, XY_Touch.X, XY_Touch.Y);
	  }
	  if (Touch == FALSE){
		Touch = TRUE;
	  }
	}
	else if(Touch)
    {
	  USB_H_LINK_LED_FSET = USB_H_LINK_LED_MASK;
	  Touch = FALSE;
	}
	
	
	// Data from UART0
	UART_Check(Buffer);
	
	if (Buffer[0] != 'E'){
	  
	  Parsing_parse(Buffer, &measurement);
	  
	  // Notify the graph
	  updateGraphLayout(&measurement, currentLayout == graphLayout);
	  checkDevices(&measurement, currentLayout);
	  
	  double vRMS = measurement.voltage;
	  double iRMS = measurement.current;
	  double pACT = measurement.P_power;
	  double pREAC = measurement.Q_power;
	  double pHAR = measurement.H_power;
	  
	  if (currentLayout == mainLayout){
		GLCD_SetWindow(0, 0, 150, 70);  
		GLCD_TextSetPos(0,0);
		GLCD_SetFont(&Terminal_9_12_6,0xFFFFFF,0x000000);
		GLCD_print(" Voltage: %f\r\n Current: %f\r\n Power: \t%f\r\n Reac: \t%f\r\n Har: \t%f", vRMS, iRMS, pACT, pREAC, pHAR);
	  }
	}
	
	// HANDLE uIP
	uip_len = tapdev_read(uip_buf);
	if(uip_len > 0)
	{
	  if(BUF->type == htons(UIP_ETHTYPE_IP)) {
		uip_arp_ipin();
		uip_input();
		/* If the above function invocation resulted in data that
		should be sent out on the network, the global variable
		uip_len is set to a value > 0. */
		if(uip_len > 0) {
		  uip_arp_out();
		  tapdev_send(uip_buf,uip_len);
		}
	  }
	  else if(BUF->type == htons(UIP_ETHTYPE_ARP))
	  {
		uip_arp_arpin();
		/* If the above function invocation resulted in data that
		should be sent out on the network, the global variable
		uip_len is set to a value > 0. */
		if(uip_len > 0) {
		  tapdev_send(uip_buf,uip_len);
		}
	  }
	}
	else if(timer_expired(&periodic_timer)) {
	  timer_reset(&periodic_timer);
	  for(i = 0; i < UIP_CONNS; i++) {
		uip_periodic(i);
		/* If the above function invocation resulted in data that
		should be sent out on the network, the global variable
		uip_len is set to a value > 0. */
		if(uip_len > 0) {
		  uip_arp_out();
		  tapdev_send(uip_buf,uip_len);
		}
	  }
#if UIP_UDP
	  for(i = 0; i < UIP_UDP_CONNS; i++) {
		uip_udp_periodic(i);
		/* If the above function invocation resulted in data that
		should be sent out on the network, the global variable
		uip_len is set to a value > 0. */
		if(uip_len > 0) {
		  uip_arp_out();
		  tapdev_send();
		}
	  }
#endif /* UIP_UDP */
	  /* Call the ARP timer function every 10 seconds. */
	  if(timer_expired(&arp_timer)) {
		timer_reset(&arp_timer);
		uip_arp_timer();
	  }
	}
  }
}
Exemple #12
0
int main(void)
{


	network_init();

	//CLKPR = (1<<CLKPCE);	//Change prescaler
	//CLKPR = (1<<CLKPS0);	//Use prescaler 2
	//clock_prescale_set(clock_div_2);
	enc28j60Write(ECOCON, 1 & 0x7);	//Get a 25MHz signal from enc28j60

	#if MY_DEBUG
	uartInit();
	uartSetBaudRate(9600);
	rprintfInit(uartSendByte);
	#endif

	int i;
	uip_ipaddr_t ipaddr; 
	struct timer periodic_timer, arp_timer;

	clock_init();

	timer_set(&periodic_timer, CLOCK_SECOND / 2);
	timer_set(&arp_timer, CLOCK_SECOND * 10);

	uip_init();
	
	struct uip_eth_addr mac = {UIP_ETHADDR0, UIP_ETHADDR1, UIP_ETHADDR2, UIP_ETHADDR3, UIP_ETHADDR4, UIP_ETHADDR5};

	uip_setethaddr(mac);

    telnetd_init();

#ifdef __DHCPC_H__
	dhcpc_init(&mac, 6);
#else
    uip_ipaddr(ipaddr, 192,168,0,1); 
	uip_sethostaddr(ipaddr);
    uip_ipaddr(ipaddr, 192,168,0,1);
    uip_setdraddr(ipaddr);
    uip_ipaddr(ipaddr, 255,255,255,0);
    uip_setnetmask(ipaddr);
#endif /*__DHCPC_H__*/


	while(1){
		uip_len = network_read(); 
		if(uip_len > 0) {
			if(BUF->type == htons(UIP_ETHTYPE_IP)){  
				uip_arp_ipin();
				uip_input();
				if(uip_len > 0) {
					uip_arp_out();
					network_send();
				}
			}else if(BUF->type == htons(UIP_ETHTYPE_ARP)){
				uip_arp_arpin();
				if(uip_len > 0){
					network_send();
				}
			}

		}else if(timer_expired(&periodic_timer)) {
			timer_reset(&periodic_timer);

			for(i = 0; i < UIP_CONNS; i++) {
				uip_periodic(i);
				if(uip_len > 0) {
					uip_arp_out();
					network_send();
				}
			}

			#if UIP_UDP
			for(i = 0; i < UIP_UDP_CONNS; i++) {
				uip_udp_periodic(i);
				if(uip_len > 0) {
					uip_arp_out();
					network_send();
				}
			}
			#endif /* UIP_UDP */

			if(timer_expired(&arp_timer)) {
				timer_reset(&arp_timer);
				uip_arp_timer();
			}
		}
	}
	return 0;
}
Exemple #13
0
/*----------------------------------------------------------------------------*/ 
int
main(void)
{
  int i;
  // Renesas -- uip_ipaddr_t ipaddr;
  struct timer periodic_timer, arp_timer;
  struct uip_eth_addr my_mac;
  
  uint32_t ch = 0;

  // Renesas ++
  InitialiseLCD();
  DisplayuIPDemo();

  timer_init();
  timer_set(&periodic_timer, CLOCK_SECOND / 2);
  timer_set(&arp_timer, CLOCK_SECOND * 10);

  Exosite_Get_MAC((unsigned char *)&my_mac);
  // Renesas -- network_device_init();
  /* Wait until Ether device initailize succesfully. 
     Make sure Ethernet cable is plugged in. */ 
  while (R_ETHER_ERROR == R_Ether_Open(ch, (uint8_t*)&my_mac));

  // Renesas ++ set Ethernet address
  uip_setethaddr(my_mac);
  uip_init();

  // Renesas -- 
  //uip_ipaddr(ipaddr, 192,168,0,2);
  //uip_sethostaddr(ipaddr);
  dhcpc_init(&my_mac, 6);
  if (!Exosite_Init(APP_NAME, APP_VERSION)) DisplayLCD(LCD_LINE8, "==NEED CIK=="); 

  while (1)
  {
    // Renesas -- uip_len = network_device_read();
    uip_len = R_Ether_Read(ch, (void *)uip_buf);
    if (uip_len > 0)
    {
      if (BUF->type == htons(UIP_ETHTYPE_IP))
      {
        uip_arp_ipin();
        uip_input();
        /* If the above function invocation resulted in data that
           should be sent out on the network, the global variable
           uip_len is set to a value > 0. */
        if (uip_len > 0)
        {
          uip_arp_out();
          // Renesas -- network_device_send();
          R_Ether_Write(ch, (void *)uip_buf, (uint32_t)uip_len);
        }
      }
      else if (BUF->type == htons(UIP_ETHTYPE_ARP))
      {
        uip_arp_arpin();
        /* If the above function invocation resulted in data that
           should be sent out on the network, the global variable
           uip_len is set to a value > 0. */
        if (uip_len > 0)
        {
          // Renesas -- network_device_send();
          R_Ether_Write(ch, (void *)uip_buf, (uint32_t)uip_len);
        }
      }

    }
    else if (timer_expired(&periodic_timer))
    {
      timer_reset(&periodic_timer);
      for (i = 0; i < UIP_CONNS; i++)
      {
        uip_periodic(i);
        /* If the above function invocation resulted in data that
           should be sent out on the network, the global variable
           uip_len is set to a value > 0. */
        if (uip_len > 0)
        {
          uip_arp_out();
          // Renesas -- network_device_send();
          R_Ether_Write(ch, (void *)uip_buf, (uint32_t)uip_len);
        }
      }

#if UIP_UDP
      for (i = 0; i < UIP_UDP_CONNS; i++)
      {
        uip_udp_periodic(i);
        /* If the above function invocation resulted in data that
           should be sent out on the network, the global variable
           uip_len is set to a value > 0. */
        if (uip_len > 0)
        {
          uip_arp_out();
          // Renesas -- network_device_send();
          R_Ether_Write(ch, (void *)uip_buf, (uint32_t)uip_len);
        }
      }
#endif /* UIP_UDP */

      /* Call the ARP timer function every 10 seconds. */
      if (timer_expired(&arp_timer))
      {
        timer_reset(&arp_timer);
        uip_arp_timer();
      }
    }

    // Insert user aplications here.
    // Call WEB application that controls LEDs on the target board.
    user_app();
	
  }
  return 0;
}
/* -----------------------------------------------------------------------------
 * \brief      Deliver an incoming packet to the TCP/IP stack
 *
 *             This function is called by theServer to
 *             deliver an incoming packet to the TCP/IP stack. The
 *             incoming packet must be present in the uip_buf buffer,
 *             and the length of the packet must be in the global
 *             uip_len variable.
 * -------------------------------------------------------------------------- */
void xtcpip_input(chanend mac_tx)
{
/*_______________*/
#if UIP_CONF_IPV4 /* ORIGINAL_XMOS */
	if (BUF->type == htons(UIP_ETHTYPE_IP)) {
		uip_arp_ipin();
		uip_input();
		if (uip_len > 0) {
			if (uip_udpconnection()
				&& (TCPBUF->proto != UIP_PROTO_ICMP)
				&& (TCPBUF->proto != UIP_PROTO_IGMP))
				uip_arp_out( uip_udp_conn);
			else
				uip_arp_out( NULL);
			xtcp_tx_buffer(mac_tx);
		}
	} else if (BUF->type == htons(UIP_ETHTYPE_ARP)) {
		uip_arp_arpin();

		if (uip_len > 0) {
			xtcp_tx_buffer(mac_tx);
		}
		for (int i = 0; i < UIP_UDP_CONNS; i++) {
			uip_udp_arp_event(i);
			if (uip_len > 0) {
				uip_arp_out(&uip_udp_conns[i]);
				xtcp_tx_buffer(mac_tx);
			}
		}
	}
#endif /* UIP_CONF_IPV4 ORIGINAL_XMOS */
/*_______________*/
	/* contiki tcpip.c */
#if UIP_CONF_IP_FORWARD
  if(uip_len > 0) {
    tcpip_is_forwarding = 1;
    if(uip_fw_forward() == UIP_FW_LOCAL) {
      tcpip_is_forwarding = 0;
      check_for_tcp_syn();
      uip_input();
      if(uip_len > 0) {
#if UIP_CONF_TCP_SPLIT
        uip_split_output(mac_tx);
#else /* UIP_CONF_TCP_SPLIT */
#if UIP_CONF_IPV6
        xtcpip_ipv6_output(mac_tx);
#else
	PRINTF("tcpip packet_input forward output len %d\n", uip_len);
        xtcpip_output(mac_tx);
#endif
#endif /* UIP_CONF_TCP_SPLIT */
      }
    }
    tcpip_is_forwarding = 0;
  }
#else /* UIP_CONF_IP_FORWARD */
  if(uip_len > 0) {
    uip_input();
    if(uip_len > 0) {
#if UIP_CONF_TCP_SPLIT
      uip_split_output(mac_tx);
#else /* UIP_CONF_TCP_SPLIT */
#if UIP_CONF_IPV6
      xtcpip_ipv6_output(mac_tx);
#else
      PRINTF("tcpip packet_input output len %d\n", uip_len);
      tcpip_output();
#endif
#endif /* UIP_CONF_TCP_SPLIT */
    }
  }
#endif /* UIP_CONF_IP_FORWARD */
}
Exemple #15
0
// This gets called on both Ethernet RX interrupts and timer requests,
// but it's called only from the Ethernet interrupt handler
void elua_uip_mainloop()
{
  u32 temp, packet_len;

  // Increment uIP timers
  temp = platform_eth_get_elapsed_time();
  periodic_timer += temp;
  arp_timer += temp;  

  // Check for an RX packet and read it
  if( ( packet_len = platform_eth_get_packet_nb( uip_buf, sizeof( uip_buf ) ) ) > 0 )
  {
    // Set uip_len for uIP stack usage.
    uip_len = ( unsigned short )packet_len;

    // Process incoming IP packets here.
    if( BUF->type == htons( UIP_ETHTYPE_IP ) )
    {
      uip_arp_ipin();
      uip_input();

      // If the above function invocation resulted in data that
      // should be sent out on the network, the global variable
      // uip_len is set to a value > 0.
      if( uip_len > 0 )
      {
        uip_arp_out();
        device_driver_send();
      }
    }

    // Process incoming ARP packets here.
    else if( BUF->type == htons( UIP_ETHTYPE_ARP ) )
    {
      uip_arp_arpin();

      // If the above function invocation resulted in data that
      // should be sent out on the network, the global variable
      // uip_len is set to a value > 0.
      if( uip_len > 0 )
        device_driver_send();
    }
  }
  
  // Process TCP/IP Periodic Timer here.
  // Also process the "force interrupt" events (platform_eth_force_interrupt)
  if( periodic_timer >= UIP_PERIODIC_TIMER_MS )
  {
    periodic_timer = 0;
    uip_set_forced_poll( 0 );
  }
  else
    uip_set_forced_poll( 1 );
  for( temp = 0; temp < UIP_CONNS; temp ++ )
  {
    uip_periodic( temp );

    // If the above function invocation resulted in data that
    // should be sent out on the network, the global variable
    // uip_len is set to a value > 0.
    if( uip_len > 0 )
    {
      uip_arp_out();
      device_driver_send();
    }
  }

#if UIP_UDP
    for( temp = 0; temp < UIP_UDP_CONNS; temp ++ )
    {
      uip_udp_periodic( temp );

      // If the above function invocation resulted in data that
      // should be sent out on the network, the global variable
      // uip_len is set to a value > 0.
      if( uip_len > 0 )
      {
        uip_arp_out();
        device_driver_send();
      }
    }
#endif // UIP_UDP
  
  // Process ARP Timer here.
  if( arp_timer >= UIP_ARP_TIMER_MS )
  {
    arp_timer = 0;
    uip_arp_timer();
  }  
}
Exemple #16
0
void vuIP_TASK( void *pvParameters )
{
/* The semaphore used by the EMAC ISR to indicate that an Rx frame is ready
for processing. */
xSemaphoreHandle xSemaphore = NULL;
portBASE_TYPE xARPTimer;
unsigned portBASE_TYPE uxPriority;
static volatile portTickType xStartTime, xCurrentTime;

	/* Initialize the uIP TCP/IP stack. */
	uip_init();
	uip_arp_init();
	
	/* Initialize the HTTP server. */
	httpd_init();

	/* Initialise the local timers. */
	xStartTime = xTaskGetTickCount();
	xARPTimer = 0;

	/* Initialise the EMAC.  A semaphore will be returned when this is
	successful. This routine contains code that polls status bits.  If the
	Ethernet cable is not plugged in then this can take a considerable time.
	To prevent this starving lower priority tasks of processing time we
	lower our priority prior to the call, then raise it back again once the
	initialisation is complete. */
	uxPriority = uxTaskPriorityGet( NULL );
	vTaskPrioritySet( NULL, tskIDLE_PRIORITY );
	while( xSemaphore == NULL )
	{
		xSemaphore = xEMACInit();
	}
	vTaskPrioritySet( NULL, uxPriority );

	for( ;; )
	{
		/* Let the network device driver read an entire IP packet
		into the uip_buf. If it returns > 0, there is a packet in the
		uip_buf buffer. */
		uip_len = ulEMACPoll();

		/* Was a packet placed in the uIP buffer? */
		if( uip_len > 0 )
		{
			/* A packet is present in the uIP buffer. We call the
			appropriate ARP functions depending on what kind of packet we
			have received. If the packet is an IP packet, we should call
			uip_input() as well. */
			if( pucUIP_Buffer->type == htons( UIP_ETHTYPE_IP ) )
			{
				uip_arp_ipin();
				uip_input();

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					uip_arp_out();
					lEMACSend();
				}
			}
			else if( pucUIP_Buffer->type == htons( UIP_ETHTYPE_ARP ) )
			{
				uip_arp_arpin();

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */	
				if( uip_len > 0 )
				{	
					lEMACSend();
				}
			}
		}
		else
		{
			/* The poll function returned 0, so no packet was
			received. Instead we check if it is time that we do the
			periodic processing. */
			xCurrentTime = xTaskGetTickCount();

			if( ( xCurrentTime - xStartTime ) >= RT_CLOCK_SECOND )
			{
				portBASE_TYPE i;

				/* Reset the timer. */
				xStartTime = xCurrentTime;

				/* Periodic check of all connections. */
				for( i = 0; i < UIP_CONNS; i++ )
				{
					uip_periodic( i );

					/* If the above function invocation resulted in data that
					should be sent out on the network, the global variable
					uip_len is set to a value > 0. */					
					if( uip_len > 0 )
					{
						uip_arp_out();
						lEMACSend();
					}
				}

				#if UIP_UDP
					for( i = 0; i < UIP_UDP_CONNS; i++ )
					{
						uip_udp_periodic( i );

						/* If the above function invocation resulted in data that
						should be sent out on the network, the global variable
						uip_len is set to a value > 0. */
						if( uip_len > 0 )
						{
							uip_arp_out();
							tapdev_send();
						}
					}
				#endif /* UIP_UDP */

				/* Periodically call the ARP timer function. */
				if( ++xARPTimer == uipARP_FREQUENCY )
				{	
					uip_arp_timer();
					xARPTimer = 0;
				}
			}
			else
			{				
				/* We did not receive a packet, and there was no periodic
				processing to perform.  Block for a fixed period.  If a packet
				is received during this period we will be woken by the ISR
				giving us the Semaphore. */
				xSemaphoreTake( xSemaphore, uipMAX_BLOCK_TIME );
			}
		}
	}
}
Exemple #17
0
void vuIP_Task( void *pvParameters )
{
portBASE_TYPE i;
uip_ipaddr_t xIPAddr;
struct timer periodic_timer, arp_timer;

	/* Create the semaphore used by the ISR to wake this task. */
	vSemaphoreCreateBinary( xSemaphore );
	
	/* Initialise the uIP stack. */
	timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );
	timer_set( &arp_timer, configTICK_RATE_HZ * 10 );
	uip_init();
	uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );
	uip_sethostaddr( xIPAddr );
	uip_ipaddr( xIPAddr, uipNET_MASK0, uipNET_MASK1, uipNET_MASK2, uipNET_MASK3 );
	uip_setnetmask( xIPAddr );
	uip_ipaddr( xIPAddr, uipGATEWAY_ADDR0, uipGATEWAY_ADDR1, uipGATEWAY_ADDR2, uipGATEWAY_ADDR3 );
	uip_setdraddr( xIPAddr );	
	httpd_init();

	/* Initialise the MAC. */
	ENET_InitClocksGPIO();
	ENET_Init();
	portENTER_CRITICAL();
	{
		ENET_Start();
		prvSetMACAddress();
		VIC_Config( ENET_ITLine, VIC_IRQ, 1 );
		VIC_ITCmd( ENET_ITLine, ENABLE );	
		ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;
 		ENET_DMA->IER = uipDMI_RX_CURRENT_DONE;
	}
	portEXIT_CRITICAL();
	

	while(1)
	{
		/* Is there received data ready to be processed? */
		uip_len = ENET_HandleRxPkt( uip_buf );
		
		if( uip_len > 0 )
		{
			/* Standard uIP loop taken from the uIP manual. */
			if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
			{
				uip_arp_ipin();
				uip_input();

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					uip_arp_out();
					prvENET_Send();
				}
			}
			else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
			{
				uip_arp_arpin();

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					prvENET_Send();
				}
			}
		}
		else
		{
			if( timer_expired( &periodic_timer ) )
			{
				timer_reset( &periodic_timer );
				for( i = 0; i < UIP_CONNS; i++ )
				{
					uip_periodic( i );
	
					/* If the above function invocation resulted in data that
					should be sent out on the network, the global variable
					uip_len is set to a value > 0. */
					if( uip_len > 0 )
					{
						uip_arp_out();
						prvENET_Send();
					}
				}	
	
				/* Call the ARP timer function every 10 seconds. */
				if( timer_expired( &arp_timer ) )
				{
					timer_reset( &arp_timer );
					uip_arp_timer();
				}
			}
			else
			{			
				/* We did not receive a packet, and there was no periodic
				processing to perform.  Block for a fixed period.  If a packet
				is received during this period we will be woken by the ISR
				giving us the Semaphore. */
				xSemaphoreTake( xSemaphore, configTICK_RATE_HZ / 2 );			
			}
		}
	}
}
Exemple #18
0
void vuIP_Task( void *pvParameters )
{
portBASE_TYPE i;
uip_ipaddr_t xIPAddr;
struct timer periodic_timer, arp_timer;
extern void ( vEMAC_ISR_Wrapper )( void );

	( void ) pvParameters;

	/* Initialise the uIP stack. */
	timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );
	timer_set( &arp_timer, configTICK_RATE_HZ * 10 );
	uip_init();
	uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
	uip_sethostaddr( &xIPAddr );
	uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
	uip_setnetmask( &xIPAddr );
	prvSetMACAddress();
	httpd_init();

	/* Create the semaphore used to wake the uIP task. */
	vSemaphoreCreateBinary( xEMACSemaphore );

	/* Initialise the MAC. */
	vInitEmac();

	while( lEMACWaitForLink() != pdPASS )
    {
        vTaskDelay( uipINIT_WAIT );
    }

	for( ;; )
	{
		/* Is there received data ready to be processed? */
		uip_len = ( unsigned short ) ulEMACRead();
		
		if( ( uip_len > 0 ) && ( uip_buf != NULL ) )
		{
			/* Standard uIP loop taken from the uIP manual. */
			if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
			{
				uip_arp_ipin();
				uip_input();

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					uip_arp_out();
					vEMACWrite();
				}
			}
			else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
			{
				uip_arp_arpin();

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					vEMACWrite();
				}
			}
		}
		else
		{
			if( timer_expired( &periodic_timer ) && ( uip_buf != NULL ) )
			{
				timer_reset( &periodic_timer );
				for( i = 0; i < UIP_CONNS; i++ )
				{
					uip_periodic( i );

					/* If the above function invocation resulted in data that
					should be sent out on the network, the global variable
					uip_len is set to a value > 0. */
					if( uip_len > 0 )
					{
						uip_arp_out();
						vEMACWrite();
					}
				}

				/* Call the ARP timer function every 10 seconds. */
				if( timer_expired( &arp_timer ) )
				{
					timer_reset( &arp_timer );
					uip_arp_timer();
				}
			}
			else
			{
				/* We did not receive a packet, and there was no periodic
				processing to perform.  Block for a fixed period.  If a packet
				is received during this period we will be woken by the ISR
				giving us the Semaphore. */
				xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );
			}
		}
	}
}
Exemple #19
0
int main(int argc, char *argv[])
{
  uint16_t eth_hdr_type;
  int i;

  SCS      = BIT0 | BIT1;   /* use fast I/O registers    */

  VPBDIV = 0x00000001;  /* PCLK = CCLK */
  PLLCFG  = 0x00000024; /* Fosc=12MHz, CCLK=60MHz */
  PLLCON  = 0x00000001; /* enable the PLL  */
  PLLFEED = 0x000000AA; /* feed sequence   */
  PLLFEED = 0x00000055;
  while (!(PLLSTAT & 0x00000400));
  PLLCON = 3;     // enable and connect
  PLLFEED = 0xAA;
  PLLFEED = 0x55;

  //busywaitInit();
  //systickInit();
  //lcdInit();

  vicInit();
  uart0Init();

  DBG("Initializing USB Stack");
  usbInit();

  rndisInit();
  DBG("init 2");
  network_device_init();

  DBG("init 3");
  uip_init();

  /*
  ipaddr_set = 1;
  uip_ipaddr(ipaddr, 192, 168, 12, 1);
  uip_ipaddr(dhcpd_client_ipaddr, 192, 168, 12, 2);
  uip_sethostaddr(ipaddr);
*/
  uip_setethaddr(ethaddr);

  //uip_setethaddr(eaddr);

  //telnetd_init();

  //net_timers_init();

#ifdef USE_DHCPD
  dhcpdInit();
#endif


  DBG("Starting USB Stack");
  interruptsEnable();
  usbConnect();

  //lcdOn();
  //lcdClear();

  //int xyz = 0;
  //printf("Frame number %u ",usbFrameNumber());

  while(1) {
    //if (xyz) DBG("is there a packet? bulk ep status = %x",_usbGetEPStatus(RNDIS_BULK_EP | USB_EP_OUT));
    uip_len = network_device_read();
    //if (xyz) DBG("len=%d",uip_len);
    if (uip_len > 0) {
      eth_hdr_type = (uip_buf[0xc] << 8) | uip_buf[0xd];
      if(eth_hdr_type == UIP_ETHTYPE_IP) {
        //DBG("Hey, got an IP packet!!!");
        uip_arp_ipin();
        uip_input();
        if(uip_len > 0) {
          uip_arp_out();
          //DBG("IP Reply! (%d bytes)",uip_len);
          //int j;
          //for (j=0; j<uip_len; j++) {
          //  DBG(">%d %02x ",j,uip_buf[j]);
          //}
          //DBG("");
          network_device_send();
        }
      }
      else if (eth_hdr_type == UIP_ETHTYPE_ARP) {
        //DBG("Hey, got an ARP packet!!!");
        uip_arp_arpin();
        //xyz = 1;
        if(uip_len > 0) {
          //DBG("ARP Reply!");
          network_device_send();
        }
      } else {
        DBG("Hey, got some weird packet, not IP, not ARP, type=%x",eth_hdr_type);
      }

    }

    /*
     * we reply to DHCP requests on another connection, and the connection
     * is faster if we don't wait for expiration of the timer.
     * I tried to call uip_udp_periodic_conn(dhcpd_reply_conn) instead of
     * the loop, but it did not work.
     */
    //if (dhcpd_state != DHCPD_IDLE) {
    if (udp_poll_request) {
      udp_poll_request = 0;
      for(i = 0; i < UIP_UDP_CONNS; i++) {
        uip_udp_periodic(i);
        if(uip_len > 0) {
          uip_arp_out();
          network_device_send();
        }
      }
      //uip_udp_periodic_conn(dhcpd_reply_conn);
    }

    if (net_timers_ip_expired()) {
      net_timers_ip_reset();

      for(i = 0; i < UIP_CONNS; i++) {
        uip_periodic(i);
        if(uip_len > 0) {
          uip_arp_out();
          network_device_send();
        }
      }
#define USE_UDP_PERIODIC
#ifdef USE_UDP_PERIODIC
      for(i = 0; i < UIP_UDP_CONNS; i++) {
        uip_udp_periodic(i);
        if(uip_len > 0) {
          uip_arp_out();
          network_device_send();
        }
      }
#endif
      if(net_timers_arp_expired()) {
        net_timers_arp_reset();
        uip_arp_timer();
      }
    }
  }
  return 0;
}
Exemple #20
0
/*****************************************************************************
*  Main Control Loop
*
*  
*****************************************************************************/
int main(void)
{
  unsigned char i;
  unsigned char arptimer=0;

	// PORTB PB5 als Ausgang (in use LED)
	DDRB=(1<<PB5);
	PORTB=(1<<PB5);

	init_sensors();

  // init NIC device driver
  nic_init();

  // init uIP
  uip_init();

  // init app
  example1_init();

  // init ARP cache
  uip_arp_init();

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

	// initialisierendes lesen der Temperatur(en)
	read_temp_sensors();

  while(1)
  {

	if(minInt==1){
		minInt=0;
		read_temp_sensors();
	}
    // look for a packet
    uip_len = nic_poll();
    if(uip_len == 0)
    {
      // if timed out, call periodic function for each connection
      //if(timerCounter > TIMERCOUNTER_PERIODIC_TIMEOUT)
	  if(tInt)
      {
        
		tInt = 0;
		//timerCounter = 0;
        
        for(i = 0; i < UIP_CONNS; i++)
        {
          uip_periodic(i);
		
          // transmit a packet, if one is ready
          if(uip_len > 0)
          {
            uip_arp_out();
            nic_send();
          }
        }

        /* Call the ARP timer function every 10 seconds. */
        if(++arptimer == 20)
        {	
          uip_arp_timer();
          arptimer = 0;
        }
      }
    }
    else  // packet received
    {
      // 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();
          nic_send();
        }
      }
      // 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)
          nic_send();
      }
    }
  }

  return 1;
}
uint32_t uIPMain(void)
{
    uint32_t i;
    uip_ipaddr_t ipaddr;
    struct timer periodic_timer, arp_timer, can_sync_timer;

    LED_On(2);

    // Sys timer init 1/100 sec tick
    clock_init(2);

    timer_set(&periodic_timer, CLOCK_SECOND / 10);
    timer_set(&arp_timer, CLOCK_SECOND * 10);
    timer_set(&can_sync_timer, CLOCK_SECOND / 8); //ca. 1x pro sec wird gesynced

    // Initialize the ethernet device driver
    // Init MAC
    // Phy network negotiation
    tapdev_init(); //code in: ENET_TxDscrInit (ethernet.c) & ENET_RxDscrInit (ethernet.c) &  ETH_Start (stm32_eth.c)

    // Initialize the uIP TCP/IP stack.
    uip_init(); //code in uip.c

    // Init Server
    #ifdef TEST_GATEWAY
        uip_ipaddr(ipaddr, 10,0,241,2);
    #else
        uip_ipaddr(ipaddr, 10,0,241,1);
    #endif
    uip_sethostaddr(ipaddr); //ip
    uip_ipaddr(ipaddr, 10,0,240,0);
    uip_setdraddr(ipaddr);  //gw
    uip_ipaddr(ipaddr, 255,255,252,0);
    uip_setnetmask(ipaddr); //nm

    // Initialize the server listen on port 23
    uip_listen(HTONS(23));

    //turn led off, we are now ready for inncoming conenctions
    LED_Off(1);

    CanRxMsg RxMessage;
    int can_last_msg_id = 0;
    uint32_t nCount;

    while(1)
    {
        if(timer_expired(&can_sync_timer))
        {
            nCount++;
            timer_reset(&can_sync_timer);
            #ifndef TEST_GATEWAY
            send_sync( nCount % 2 );
            if( nCount % 2 == 1 )
                LED_On(2);
            else
                LED_Off(2);
            #endif
        }

        // ethernet stuff
        uip_len = tapdev_read(uip_buf);
        if(uip_len > 0) //read input
        {
            if(BUF->type == htons(UIP_ETHTYPE_IP)) //Layer3?
            {
                uip_arp_ipin();
                uip_input();
                /* If the above function invocation resulted in data that
                 should be sent out on the network, the global variable
                 uip_len is set to a value > 0. */
                if(uip_len > 0)
                {
                    uip_arp_out(); //get ARP of destination - or default gw (uip_arp.c)
                    tapdev_send(uip_buf,uip_len);
                }
            }
            else if(BUF->type == htons(UIP_ETHTYPE_ARP)) //Layer2?
            {
                uip_arp_arpin();
                /* If the above function invocation resulted in data that
                should be sent out on the network, the global variable
                uip_len is set to a value > 0. */
                if(uip_len > 0)
                {
                    tapdev_send(uip_buf,uip_len);
                }
            }
        }
        else if(timer_expired(&periodic_timer)) //check if there is data in the send queue of each connection and send it
        {
            timer_reset(&periodic_timer);
            for(i = 0; i < UIP_CONNS; i++)
            {
                uip_periodic(i); //sets uip_conn to the current connections (macro uip.h)
                /* If the above function invocation resulted in data that
                should be sent out on the network, the global variable
                uip_len is set to a value > 0. */
                if(uip_len > 0)
                {
                    uip_arp_out(); //get ARP of destination - or default gw (uip_arp.c)
                    tapdev_send(uip_buf,uip_len);
                }
            }
            #if UIP_UDP
            for(i = 0; i < UIP_UDP_CONNS; i++)
            {
                uip_udp_periodic(i);
                /* If the above function invocation resulted in data that
                should be sent out on the network, the global variable
                uip_len is set to a value > 0. */
                if(uip_len > 0)
                {
                    uip_arp_out(); //get ARP of destination - or default gw (uip_arp.c)
                    tapdev_send();
                }
            }
            #endif /* UIP_UDP */

            /* Call the ARP timer function every 10 seconds. */
            /* It updates the arp-table (removes old entries) */
            if(timer_expired(&arp_timer))
            {
                timer_reset(&arp_timer);
                uip_arp_timer();
            }
        }
    }
    return(TRUE);
}
Exemple #22
0
/*****************************************************************************
*  Main Control Loop
*
*  
*****************************************************************************/
int main(void)
{
 	//led PB4
	// Pins als Ausgänge definieren:
	DDRB  |= (1 << 4);

    PORTB |= (0 << 4);

//DDRD |= (1 << 7);
//PORTD |= (1 << 7);   
  
  unsigned char i;
  unsigned char arptimer=0;

   // init NIC device driver
  nic_init();

  // init uIP
  uip_init();

  // init app
  services_init();

  // init ARP cache
  uip_arp_init();

  // init periodic timer
  initTimer();

  SET_USART_9600();
  //PORTB |= (1 << 4); //LED PB4 ein
  sei();

//  if((mca25_stat.init=mca25_init())==0) mca25_stat.init=mca25_configure();
//  if(mca25_stat.init) {

    // print error message?;
//  }
#if USE_MCA25_CAM
//DEBUG:
	MCA25_ERROR_LED_OFF();
	MCA25_CLOCK_LED_OFF();
	//DDRB = 0xFF;
	
	MCA25_STATUS_LED_ON();
	mca25_init();
	mca25_configure();	
	MCA25_STATUS_LED_OFF();
	MCA25_ERROR_LED_ON();
	MCA25_CLOCK_LED_ON();
#endif	
//	#if USE_CLOCK
//		Start_Clock();
	//#endif  
#if USE_SERVO
	servo_init();
#endif	
//PORTD |= (1 << 7);
  while(1)
  {
    // look for a packet
    uip_len = nic_poll();
    if(uip_len == 0)
    {
      // if timed out, call periodic function for each connection
      if(timerCounter > TIMERCOUNTER_PERIODIC_TIMEOUT)
      {
        timerCounter = 0;
        
        for(i = 0; i < UIP_CONNS; i++)
        {
          uip_periodic(i);
		
          // transmit a packet, if one is ready
          if(uip_len > 0)
          {
            uip_arp_out();
            nic_send();
          }
        }

        /* Call the ARP timer function every 10 seconds. */
        if(++arptimer == 20)
        {	
          uip_arp_timer();
          arptimer = 0;
        }
      }
    }
    else  // packet received
    {
      // 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();
          nic_send();
        }
      }
      // 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)
          nic_send();
      }
    }
  }

  return 1;
}
Exemple #23
0
void vuIP_Task( void *pvParameters )
{
portBASE_TYPE i;
uip_ipaddr_t xIPAddr;
struct timer periodic_timer, arp_timer;
extern void ( vEMAC_ISR_Wrapper )( void );

	( void ) pvParameters;

	/* Initialise the uIP stack. */
	timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );
	timer_set( &arp_timer, configTICK_RATE_HZ * 10 );
	uip_init();
	uip_ipaddr( xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
	uip_sethostaddr( xIPAddr );
	uip_ipaddr( xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
	uip_setnetmask( xIPAddr );
	httpd_init();

	/* Create the semaphore used to wake the uIP task. */
	vSemaphoreCreateBinary( xEMACSemaphore );

	/* Initialise the MAC. */
	while( lEMACInit() != pdPASS )
    {
        vTaskDelay( uipINIT_WAIT );
    }

	portENTER_CRITICAL();
	{
		LPC_EMAC->IntEnable = ( INT_RX_DONE | INT_TX_DONE );

		/* Set the interrupt priority to the max permissible to cause some
		interrupt nesting. */
		NVIC_SetPriority( ENET_IRQn, configEMAC_INTERRUPT_PRIORITY );

		/* Enable the interrupt. */
		NVIC_EnableIRQ( ENET_IRQn );
		prvSetMACAddress();
	}
	portEXIT_CRITICAL();


	for( ;; )
	{
		/* Is there received data ready to be processed? */
		uip_len = ulGetEMACRxData();

		if( ( uip_len > 0 ) && ( uip_buf != NULL ) )
		{
			/* Standard uIP loop taken from the uIP manual. */
			if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
			{
				uip_arp_ipin();
				uip_input();

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					uip_arp_out();
					vSendEMACTxData( uip_len );
				}
			}
			else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
			{
				uip_arp_arpin();

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					vSendEMACTxData( uip_len );
				}
			}
		}
		else
		{
			if( timer_expired( &periodic_timer ) && ( uip_buf != NULL ) )
			{
				timer_reset( &periodic_timer );
				for( i = 0; i < UIP_CONNS; i++ )
				{
					uip_periodic( i );

					/* If the above function invocation resulted in data that
					should be sent out on the network, the global variable
					uip_len is set to a value > 0. */
					if( uip_len > 0 )
					{
						uip_arp_out();
						vSendEMACTxData( uip_len );
					}
				}

				/* Call the ARP timer function every 10 seconds. */
				if( timer_expired( &arp_timer ) )
				{
					timer_reset( &arp_timer );
					uip_arp_timer();
				}
			}
			else
			{
				/* We did not receive a packet, and there was no periodic
				processing to perform.  Block for a fixed period.  If a packet
				is received during this period we will be woken by the ISR
				giving us the Semaphore. */
				xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );
			}
		}
	}
}
Exemple #24
0
/*---------------------------------------------------------------------------*/
int
main(void)
{
  EEPROM_main();

  int i;
  uip_ipaddr_t ipaddr;
  struct timer periodic_timer, arp_timer;

  memcpy (&uip_ethaddr.addr[0], &eeprom.MAC[0], 6);

  AVR_init();
  egpio_init();

  clock_init();
  mbuf_init();
  adlc_init();
  GICR = (1 << INT0);

  timer_set(&periodic_timer, CLOCK_SECOND / 2);
  timer_set(&arp_timer, CLOCK_SECOND * 10);

  nic_init();


  uip_ipaddr(ipaddr, eeprom.IPAddr[0],eeprom.IPAddr[1],eeprom.IPAddr[2],eeprom.IPAddr[3]);
  uip_sethostaddr(ipaddr);
  uip_ipaddr(ipaddr, eeprom.Gateway[0],eeprom.Gateway[1],eeprom.Gateway[2],eeprom.Gateway[3]);
  uip_setdraddr(ipaddr);
  uip_ipaddr(ipaddr, eeprom.Subnet[0],eeprom.Subnet[1],eeprom.Subnet[2],eeprom.Subnet[3]);
  uip_setnetmask(ipaddr);

  telnetd_init();
  aun_init();
  internet_init();

  egpio_write (EGPIO_STATUS_GREEN);

  while(1) {

// check the econet for complete packets
    adlc_poller();
    aun_poller ();

    uip_len = nic_poll();

    if(uip_len > 0) {

      if(BUF->type == htons(UIP_ETHTYPE_IP)) {
	uip_arp_ipin();
	uip_input();
	/* If the above function invocation resulted in data that
	   should be sent out on the network, the global variable
	   uip_len is set to a value > 0. */
	maybe_send();
      } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
	uip_arp_arpin();
	/* If the above function invocation resulted in data that
	   should be sent out on the network, the global variable
	   uip_len is set to a value > 0. */
	if(uip_len > 0) {
	  nic_send(NULL);
	}
      }

    } else if(timer_expired(&periodic_timer)) {
      timer_reset(&periodic_timer);
      for(i = 0; i < UIP_CONNS; i++) {
	uip_periodic(i);
	maybe_send();
      }

#if UIP_UDP
      for(i = 0; i < UIP_UDP_CONNS; i++) {
	uip_udp_periodic(i);
	maybe_send();
      }
#endif /* UIP_UDP */

      /* Call the ARP timer function every 10 seconds. */
      if(timer_expired(&arp_timer)) {
	timer_reset(&arp_timer);
	uip_arp_timer();
      }
    }
  }
}
Exemple #25
0
int main(void)
{


	network_init();

	#if MY_DEBUG
	uartInit();
	uartSetBaudRate(9600);
	rprintfInit(uartSendByte);
	#endif

	int i;
	uip_ipaddr_t ipaddr; 
	struct timer periodic_timer, arp_timer;

	clock_init();

	timer_set(&periodic_timer, CLOCK_SECOND / 2);
	timer_set(&arp_timer, CLOCK_SECOND * 10);

	uip_init();
	
	struct uip_eth_addr mac = {UIP_ETHADDR0, UIP_ETHADDR1, UIP_ETHADDR2, UIP_ETHADDR3, UIP_ETHADDR4, UIP_ETHADDR5};

	uip_setethaddr(mac);

    hello_world_init();

#ifdef __DHCPC_H__
	dhcpc_init(&mac, 6);
#else
    uip_ipaddr(ipaddr, 192,168,0,1); 
	uip_sethostaddr(ipaddr);
    uip_ipaddr(ipaddr, 192,168,0,1);
    uip_setdraddr(ipaddr);
    uip_ipaddr(ipaddr, 255,255,255,0);
    uip_setnetmask(ipaddr);
#endif /*__DHCPC_H__*/


	while(1){
		uip_len = network_read(); 
		if(uip_len > 0) {
			if(BUF->type == htons(UIP_ETHTYPE_IP)){  
				uip_arp_ipin();
				uip_input();
				if(uip_len > 0) {
					uip_arp_out();
					network_send();
				}
			}else if(BUF->type == htons(UIP_ETHTYPE_ARP)){
				uip_arp_arpin();
				if(uip_len > 0){
					network_send();
				}
			}

		}else if(timer_expired(&periodic_timer)) {
			timer_reset(&periodic_timer);

			for(i = 0; i < UIP_CONNS; i++) {
				uip_periodic(i);
				if(uip_len > 0) {
					uip_arp_out();
					network_send();
				}
			}

			#if UIP_UDP
			for(i = 0; i < UIP_UDP_CONNS; i++) {
				uip_udp_periodic(i);
				if(uip_len > 0) {
					uip_arp_out();
					network_send();
				}
			}
			#endif /* UIP_UDP */

			if(timer_expired(&arp_timer)) {
				timer_reset(&arp_timer);
				uip_arp_timer();
			}
		}
	}
	return 0;
}
Exemple #26
0
/*---------------------------------------------------------------------------*/
int
main(void)
{
  int i;
  uip_ipaddr_t ipaddr;
  struct timer periodic_timer, arp_timer;

  timer_set(&periodic_timer, CLOCK_SECOND / 2);
  timer_set(&arp_timer, CLOCK_SECOND * 10);
  
  tapdev_init();
  uip_init();

  uip_ipaddr(ipaddr, 192,168,0,2);
  uip_sethostaddr(ipaddr);
  uip_ipaddr(ipaddr, 192,168,0,1);
  uip_setdraddr(ipaddr);
  uip_ipaddr(ipaddr, 255,255,255,0);
  uip_setnetmask(ipaddr);

  httpd_init();
  
  /*  telnetd_init();*/
  
  /*  hello_world_init();*/

  /*  {
      u8_t mac[6] = {1,2,3,4,5,6};
      dhcpc_init(&mac, 6);
      }*/
  
  /*uip_ipaddr(ipaddr, 127,0,0,1);
  smtp_configure("localhost", ipaddr);
  SMTP_SEND("*****@*****.**", NULL, "*****@*****.**",
	    "Testing SMTP from uIP",
	    "Test message sent by uIP\r\n");*/

  /*
    webclient_init();
    resolv_init();
    uip_ipaddr(ipaddr, 195,54,122,204);
    resolv_conf(ipaddr);
    resolv_query("www.sics.se");*/


  
  while(1) 
  {
    uip_len = tapdev_read();
    if(uip_len > 0) {
      if(BUF->type == htons(UIP_ETHTYPE_IP)) 
      {
	uip_arp_ipin();
	uip_input();
	/* If the above function invocation resulted in data that
	   should be sent out on the network, the global variable
	   uip_len is set to a value > 0. */
	if(uip_len > 0) 
        {
	  uip_arp_out();
	  tapdev_send();
	}
      } 
      else if(BUF->type == htons(UIP_ETHTYPE_ARP)) 
      {
	uip_arp_arpin();
	/* If the above function invocation resulted in data that
	   should be sent out on the network, the global variable
	   uip_len is set to a value > 0. */
	if(uip_len > 0) 
        {
	  tapdev_send();
	}
      }
    } 
    else if(timer_expired(&periodic_timer)) 
    {
      timer_reset(&periodic_timer);
      for(i = 0; i < UIP_CONNS; i++) 
      {
	uip_periodic(i);
	/* If the above function invocation resulted in data that
	   should be sent out on the network, the global variable
	   uip_len is set to a value > 0. */
	if(uip_len > 0) 
        {
	  uip_arp_out();
	  tapdev_send();
	}
      }

#if UIP_UDP
      for(i = 0; i < UIP_UDP_CONNS; i++) 
      {
	uip_udp_periodic(i);
	/* If the above function invocation resulted in data that
	   should be sent out on the network, the global variable
	   uip_len is set to a value > 0. */
	if(uip_len > 0) 
        {
	  uip_arp_out();
	  tapdev_send();
	}
      }
#endif /* UIP_UDP */
      
      /* Call the ARP timer function every 10 seconds. */
      if(timer_expired(&arp_timer)) {
	timer_reset(&arp_timer);
	uip_arp_timer();
      }
    }
  }
  return 0;
}
Exemple #27
0
void vuIP_Task( void *pvParameters )
{
portBASE_TYPE i;
uip_ipaddr_t xIPAddr;
struct timer periodic_timer, arp_timer;
extern void ( vEMAC_ISR_Wrapper )( void );

	/* Create the semaphore used by the ISR to wake this task. */
	vSemaphoreCreateBinary( xEMACSemaphore );
	
	/* Initialise the uIP stack. */
	timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );
	timer_set( &arp_timer, configTICK_RATE_HZ * 10 );
	uip_init();
	uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );
	uip_sethostaddr( xIPAddr );
	httpd_init();

	/* Initialise the MAC. */
	while( Init_EMAC() != pdPASS )
    {
        vTaskDelay( uipINIT_WAIT );
    }

	portENTER_CRITICAL();
	{
		MAC_INTENABLE = INT_RX_DONE;
        VICIntEnable |= 0x00200000;
        VICVectAddr21 = ( portLONG ) vEMAC_ISR_Wrapper;
		prvSetMACAddress();
	}
	portEXIT_CRITICAL();
	

	for( ;; )
	{
		/* Is there received data ready to be processed? */
		uip_len = uiGetEMACRxData( uip_buf );
		
		if( uip_len > 0 )
		{
			/* Standard uIP loop taken from the uIP manual. */
			if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
			{
				uip_arp_ipin();
				uip_input();

				/* If the above function invocation resulted in data that 
				should be sent out on the network, the global variable 
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					uip_arp_out();
					prvENET_Send();
				}
			}
			else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
			{
				uip_arp_arpin();

				/* If the above function invocation resulted in data that 
				should be sent out on the network, the global variable 
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					prvENET_Send();
				}
			}
		}
		else
		{
			if( timer_expired( &periodic_timer ) )
			{
				timer_reset( &periodic_timer );
				for( i = 0; i < UIP_CONNS; i++ )
				{
					uip_periodic( i );
	
					/* If the above function invocation resulted in data that 
					should be sent out on the network, the global variable 
					uip_len is set to a value > 0. */
					if( uip_len > 0 )
					{
						uip_arp_out();
						prvENET_Send();
					}
				}	
	
				/* Call the ARP timer function every 10 seconds. */
				if( timer_expired( &arp_timer ) )
				{
					timer_reset( &arp_timer );
					uip_arp_timer();
				}
			}
			else
			{			
				/* We did not receive a packet, and there was no periodic
				processing to perform.  Block for a fixed period.  If a packet
				is received during this period we will be woken by the ISR
				giving us the Semaphore. */
				xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );			
			}
		}
	}
}
Exemple #28
0
/*-----------------------------------------------------------------------------------*/
int
main(void)
{
	idata u8_t i, arptimer;
	idata u16_t j;
//	idata int i;
	InitGraphic();//putchar(0,62,0);
	//while(1)
	for(i=0;i<100;i++)
	{
		putstring(6,0, "Welcome to http://shop34480016.taobao.com  www.dianshijin.cn");
	}

	init_uart();
	printu("starting......\r\n");
	/* Initialize the device driver. */ 
	//  rtl8019as_init();
	dev_init();
	uip_arp_init();
	/* Initialize the uIP TCP/IP stack. */
	uip_init();
	printu("11111111111111111111111\r\n");
	/* Initialize the HTTP server. */
//	httpd_init();
	tcp_server_init();
	
	arptimer = 0;
	printu("222222222222222222222222222\r\n");
  while(1) {
    /* Let the tapdev network device driver read an entire IP packet
       into the uip_buf. If it must wait for more than 0.5 seconds, it
       will return with the return value 0. If so, we know that it is
       time to call upon the uip_periodic(). Otherwise, the tapdev has
       received an IP packet that is to be processed by uIP. */
    uip_len = dev_poll();
	for(j=0;j<500;j++);
/*
	if(uip_len > 0)
	{
		printuf("--------------- uip_len = 0x%x", uip_len);
		printuf("%x ----------\r\n", uip_len);
		for(i=0;i<uip_len;i++)
		{
			printuf("%x ", uip_buf[i]);
			if((i+1)%16==0) printu("\r\n");			
		}
		printu("\r\n");			
	}
*/
    if(uip_len == 0) {
      for(i = 0; i < UIP_CONNS; i++) {
	uip_periodic(i);
	/* If the above function invocation resulted in data that
	   should be sent out on the network, the global variable
	   uip_len is set to a value > 0. */
	if(uip_len > 0) {
	  uip_arp_out();
	  dev_send();
	}
      }

#if UIP_UDP
      for(i = 0; i < UIP_UDP_CONNS; i++) {
	uip_udp_periodic(i);
	/* If the above function invocation resulted in data that
	   should be sent out on the network, the global variable
	   uip_len is set to a value > 0. */
	if(uip_len > 0) {
	  uip_arp_out();
	  dev_send();
	}
      }
#endif /* UIP_UDP */
      
      /* Call the ARP timer function every 10 seconds. */
      if(++arptimer == 20) {	
	uip_arp_timer();
	arptimer = 0;
      }
      
    } else {
      if(BUF->type == htons(UIP_ETHTYPE_IP)) {
	uip_arp_ipin();
	uip_input();
	/* If the above function invocation resulted in data that
	   should be sent out on the network, the global variable
	   uip_len is set to a value > 0. */
	if(uip_len > 0) {
	  uip_arp_out();
	  dev_send();
	}
      } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) {
	uip_arp_arpin();
	/* If the above function invocation resulted in data that
	   should be sent out on the network, the global variable
	   uip_len is set to a value > 0. */	
	if(uip_len > 0) {	
	  dev_send();
	}
      }
    }
    
  }
  return 0;
}
Exemple #29
0
void vuIP_Task( void *pvParameters )
{
portBASE_TYPE i;
uip_ipaddr_t xIPAddr;
struct timer periodic_timer, arp_timer;
extern void ( vEMAC_ISR )( void );

	/* Just to get rid of the compiler warning. */
	( void ) pvParameters;

	/* Enable/Reset the Ethernet Controller */

	/* Create the semaphore used by the ISR to wake this task. */
	vSemaphoreCreateBinary( xFECSemaphore );
	
	/* Initialise the uIP stack. */
	timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );
	timer_set( &arp_timer, configTICK_RATE_HZ * 10 );
	uip_init();
	uip_ipaddr( xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
	uip_sethostaddr( xIPAddr );
	uip_ipaddr( xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
	uip_setnetmask( xIPAddr );		
	httpd_init();

	vInitFEC();

	for( ;; )
	{
		/* Is there received data ready to be processed? */
		uip_len = ( unsigned short ) ulFECRx();
		
		if( ( uip_len > 0 ) && ( uip_buf != NULL ) )
		{
			/* Standard uIP loop taken from the uIP manual. */

			if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
			{
				uip_arp_ipin();
				uip_input();

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					uip_arp_out();
					vFECTx();
				}
			}
			else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
			{
				uip_arp_arpin();

				/* If the above function invocation resulted in data that
				should be sent out on the network, the global variable
				uip_len is set to a value > 0. */
				if( uip_len > 0 )
				{
					vFECTx();
				}
			}			
		}
		else
		{
			if( ( timer_expired( &periodic_timer ) ) && ( uip_buf != NULL ) )
			{
				timer_reset( &periodic_timer );
				for( i = 0; i < UIP_CONNS; i++ )
				{
					uip_periodic( i );
	
					/* If the above function invocation resulted in data that
					should be sent out on the network, the global variable
					uip_len is set to a value > 0. */
					if( uip_len > 0 )
					{
						uip_arp_out();
						vFECTx();
					}
				}	
	
				/* Call the ARP timer function every 10 seconds. */
				if( timer_expired( &arp_timer ) )
				{
					timer_reset( &arp_timer );
					uip_arp_timer();
				}
			}
			else
			{			
				/* We did not receive a packet, and there was no periodic
				processing to perform.  Block for a fixed period.  If a packet
				is received during this period we will be woken by the ISR
				giving us the Semaphore. */
				xSemaphoreTake( xFECSemaphore, configTICK_RATE_HZ / 2 );			
			}
		}
	}
}
/*
* This is a co-operative task. This function must NEVER return
*/
void uip_task_main(void)
{
    //initialise all the connections to point to the invalid handle
    for( i = 0 ; i  < UIP_CONNS; i++)
    {
        uip_conns[i].appstate[0] = FILE_INVALID_HANDLE;
    }

    while(1) {

        task_yield();

        if( UIP_DMA_RX_PACKET_READY )
        {
            UIP_DMA_RX_PACKET_READY = 0;
            uip_len = uip_dma_rx_last_packet_length;
            // packet received - buffer is already acquired

            // 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();
                    nic_send();
                }
                else nic_rx_maybe();
            }
            else if(BUF->type == HTONS(UIP_ETHTYPE_ARP)) {

                // process an ARP packet
                uip_arp_arpin();

                // transmit a packet, if one is ready
                if(uip_len > 0) {

                    nic_send();
                }
                else {

                    nic_rx_maybe();
                }
            }
            else //don't know how to process this packet. Discard
            {
                puts("Unknown packet discarded");
                nic_rx_maybe();
            }
        }
        else if(UIP_DMA_PERIODIC_TIMEOUT && uip_acquireBuffer()) {
            if( next_conn_id != UIP_CONNS )
            {
                uip_len = 0;
                while( uip_len == 0 && next_conn_id < UIP_CONNS )
                {
                    uip_periodic( next_conn_id );
                    // transmit a packet, if one is ready
                    if(uip_len > 0) {
                        uip_arp_out();
                        nic_send();
                    }
                    next_conn_id++;
                }
            }
            if( next_conn_id == UIP_CONNS )
            {

                UIP_DMA_PERIODIC_TIMEOUT=0;

                //blink LED to show we're alive
                LD1_O = !LD1_O;
                next_conn_id = 0;

                /* Call the ARP timer function every 10 seconds. */
                if(++arptimer == 20) {
                    uip_arp_timer();
                    arptimer = 0;
                }
                uip_releaseBuffer();
//possibly change test below to if( uip_len == 0)??
                if( ENC_DMACONbits.CHEN == 0 ) nic_rx_maybe();
            }
        }
        else
        {
            //anyone need to send something or close?
            unsigned char loop_end = next_send_id;

            do
            {
                struct uip_conn *connptr = &uip_conns[next_send_id];
                file_handle_t fh = connptr->appstate[0];
                struct file_t *f = file_get_by_handle(fh);
                if(f != NULL &&
                        ( f->state == ClosePending ||
                          (f->write_buffer != NULL && buffer_available( f->write_buffer) > 0 &&
                           connptr->len == 0
                          )
                        )
                  )
                {
#ifdef __DEBUG
                    printf("\n TCP conn id: %hhd state: %hhd. File State: %hhx", next_send_id, connptr->tcpstateflags, f->state);
#endif
                    if(  connptr->tcpstateflags == CLOSED )
                    {
#ifdef __DEBUG
                        puts("\r\nFreed tcp conn and file ");
#endif
                        file_free( fh );
                        connptr->appstate[0] = FILE_INVALID_HANDLE;
                    }
                    if( uip_acquireBuffer() )
                    {
                        uip_periodic_conn(connptr);
                        if(uip_len > 0) {
#ifdef __DEBUG
                            printf("\n id %hhd sending", next_send_id);
#endif
                            uip_arp_out();
                            nic_send();
                            break;
                        }
                        else uip_releaseBuffer();
                    }
                    break; //this connection wants to write. try again later
                }

                next_send_id = ++next_send_id < UIP_CONNS ? next_send_id : 0;

            } while( next_send_id != loop_end );
        }


    } //end while

}