Exemple #1
0
void Network::on_idle(void *argument)
{
    if (!ethernet->isUp()) return;

    int len= sizeof(uip_buf); // set maximum size
    if (ethernet->_receive_frame(uip_buf, &len)) {
        uip_len = len;
        this->handlePacket();

    } else {

        if (timer_expired(&periodic_timer)) { /* no packet but periodic_timer time out (0.1s)*/
            timer_reset(&periodic_timer);

            for (int 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_CONF_UDP
            for (int 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
        }
/*
        This didn't work actually made it worse,it should have worked though
        else{
            // TODO if the command queue is below a certain amount we should poll any stopped connections
            if(command_q->size() < 4) {
                for (struct uip_conn *connr = &uip_conns[0]; connr <= &uip_conns[UIP_CONNS - 1]; ++connr) {
                    if(uip_stopped(connr)){
                        // Force a poll of this
                        printf("Force poll of connection\n");
                        uip_poll_conn(connr);
                    }
                }
            }
        }
*/
        /* Call the ARP timer function every 10 seconds. */
        if (timer_expired(&arp_timer)) {
            timer_reset(&arp_timer);
            uip_arp_timer();
        }
    }
}
Exemple #2
0
uint8_t
tapdev_output(void)
{
   uip_arp_out();
   tapdev_send();  
   return 0;
}
Exemple #3
0
void Network::handlePacket(void)
{
    if (uip_len > 0) {  /* received packet */
        //printf("handlePacket: %d\n", uip_len);

        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();
                network_device_send();
            }

        } 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 {
            printf("Unknown ethernet packet type %04X\n", htons(BUF->type));
            uip_len = 0;
        }
    }
}
Exemple #4
0
/*---------------------------------------------------------------------------*/
static void
pollhandler(void)
{
  uip_len = tapdev_poll();

  if(uip_len > 0) {
#if UIP_CONF_IPV6
    if(BUF->type == uip_htons(UIP_ETHTYPE_IPV6)) {
      tcpip_input();
    } else
#endif /* UIP_CONF_IPV6 */
    if(BUF->type == uip_htons(UIP_ETHTYPE_IP)) {
      uip_len -= sizeof(struct uip_eth_hdr);
      tcpip_input();
    } else if(BUF->type == uip_htons(UIP_ETHTYPE_ARP)) {
#if !UIP_CONF_IPV6 //math
       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();
       }
#endif              
    } else {
      uip_len = 0;
    }
  }
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(ethernode_uip_process, ev, data)
{
  PROCESS_BEGIN();

  while(1) {
    process_poll(&ethernode_uip_process);
    PROCESS_WAIT_EVENT();
    
    /* Poll Ethernet device to see if there is a frame avaliable. */
    uip_len = ethernode_read(uip_buf, UIP_BUFSIZE);

    if(uip_len > 0) {
      /*      printf("%d: new packet len %d\n", node_id, uip_len);*/

      /*      if((random_rand() % drop) <= drop / 2) {
	printf("Bropp\n");
	} else*/ {

	uip_len = hc_inflate(&uip_buf[UIP_LLH_LEN], uip_len);

#ifdef __CYGWIN__
	wpcap_send();
#else /* __CYGWIN__ */
	tapdev_send();
#endif /* __CYGWIN__ */
	/*    if(uip_fw_forward() == UIP_FW_LOCAL)*/ {
	  /* A frame was avaliable (and is now read into the uip_buf), so
	     we process it. */
	  tcpip_input();
	}
      }
    }
  }
  PROCESS_END();
    
}
Exemple #6
0
void network_device_send()
{
    tapdev_send(uip_buf, uip_len);
}
Exemple #7
0
/*****************************************************************************
 函 数 名  : uip_main
 功能描述  : uip main looplock function
 输入参数  : void  
 输出参数  : 无
 返 回 值  : 
 调用函数  : 
 被调函数  : 
 
 修改历史      :
  1.日    期   : 2017年4月17日
    作    者   : QSWWD
    修改内容   : 新生成函数

*****************************************************************************/
void uip_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);
  
  uip_init();

  uip_ipaddr(ipaddr, 192,168,1,6);
  uip_sethostaddr(ipaddr);
  uip_ipaddr(ipaddr, 192,168,1,1);
  uip_setdraddr(ipaddr);
  uip_ipaddr(ipaddr, 255,255,255,0);
  uip_setnetmask(ipaddr);
  /*MAC*/
  memcpy(&uip_ethaddr,eth_mac_addr(),6);
  /*app initialize*/
  uip_app_init();
  //main loop
  while(1) {
    	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(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();
      }
    }
  }

}
Exemple #8
0
/*-----------------------------------------------------------------------------------*/
int
main(void)
{
  u8_t i, arptimer;
  
  tapdev_init();
  uip_init();
  httpd_init();

  arptimer = 0;
  
  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 = tapdev_read();
    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();
	  tapdev_send();
	}
      }

      /* 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_len -= sizeof(struct uip_eth_hdr);
	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();
	}
      }
    }
    
  }
  return 0;
}
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 #10
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 #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();
	  }
	}
  }
}
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 #13
0
/*************************************************************************
 * Function Name: main
 * Parameters: none
 *
 * Return: none
 *
 * Description: main
 *
 *************************************************************************/
int c_entry(void)
{
	UNS_32 i, delay;
	uip_ipaddr_t ipaddr;
	struct timer periodic_timer, arp_timer;

	/* Initialize debug via UART0
	 * – 115200bps
	 * – 8 data bit
	 * – No parity
	 * – 1 stop bit
	 * – No flow control
	 */
	debug_frmwrk_init();

	_DBG_("Hello NXP Semiconductors");
	_DBG_("uIP porting on LPC17xx");


	// Initialize LED for system tick timer
	LED_Init();

	_DBG_("Init Clock");
	// Sys timer init 1/100 sec tick
	clock_init();

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

	_DBG_("Init EMAC");
	// Initialize the ethernet device driver
	while(!tapdev_init()){
		// Delay for a while then continue initializing EMAC module
		_DBG_("Error during initializing EMAC, restart after a while");
		for (delay = 0x100000; delay; delay--);
	}


#if 1

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

	// init MAC address
	uip_ethaddr.addr[0] = EMAC_ADDR0;
	uip_ethaddr.addr[1] = EMAC_ADDR1;
	uip_ethaddr.addr[2] = EMAC_ADDR2;
	uip_ethaddr.addr[3] = EMAC_ADDR3;
	uip_ethaddr.addr[4] = EMAC_ADDR4;
	uip_ethaddr.addr[5] = EMAC_ADDR5;
	uip_setethaddr(uip_ethaddr);


	uip_ipaddr(ipaddr, 192,168,0,100);
	sprintf(_db, "Set own IP address: %d.%d.%d.%d \n\r", \
			((uint8_t *)ipaddr)[0], ((uint8_t *)ipaddr)[1], \
			((uint8_t *)ipaddr)[2], ((uint8_t *)ipaddr)[3]);
	DB;
	uip_sethostaddr(ipaddr);

	uip_ipaddr(ipaddr, 192,168,0,1);
	sprintf(_db, "Set Router IP address: %d.%d.%d.%d \n\r", \
			((uint8_t *)ipaddr)[0], ((uint8_t *)ipaddr)[1], \
			((uint8_t *)ipaddr)[2], ((uint8_t *)ipaddr)[3]);
	DB;
	uip_setdraddr(ipaddr);

	uip_ipaddr(ipaddr, 255,255,255,0);
	sprintf(_db, "Set Subnet mask: %d.%d.%d.%d \n\r", \
			((uint8_t *)ipaddr)[0], ((uint8_t *)ipaddr)[1], \
			((uint8_t *)ipaddr)[2], ((uint8_t *)ipaddr)[3]);
	DB;
	uip_setnetmask(ipaddr);

	// Initialize the HTTP server ----------------------------
	_DBG_("Init HTTP");
	httpd_init();
	_DBG_("Init complete!");

  while(1)
  {
    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();
      }
    }
  }
#endif

  while (1);
}
Exemple #14
0
/*******************************************************************************
*	函数名:UipPro
*	输  入:
*	输  出:
*	功能说明:uip协议栈的实现入口,必须被轮询处理。未用中断模式
*/
void UipPro(void)
{
	uint8_t i;
	static struct timer periodic_timer, arp_timer;
	static char timer_ok = 0;	/* armfly */

	/* 创建2个定时器,只用执行1次 */
	if (timer_ok == 0)
	{
		timer_ok = 1;
		timer_set(&periodic_timer, CLOCK_SECOND / 2);  /* 创建1个0.5秒的定时器 */
		timer_set(&arp_timer, CLOCK_SECOND * 10);	   /* 创建1个10秒的定时器 */
	}

	/*
		从网络设备读取一个IP包,返回数据长度 (非阻塞)
		这个地方没有使用DM9000AEP的中断功能,采用的是查询方式
	*/
	uip_len = tapdev_read();	/* uip_len 是在uip中定义的全局变量 */
	if(uip_len > 0)
	{
		/* 处理IP数据包(只有校验通过的IP包才会被接收) */
		if(BUF->type == htons(UIP_ETHTYPE_IP))
		{
			uip_arp_ipin();
			uip_input();
			/*
				当上面的函数执行后,如果需要发送数据,则全局变量 uip_len > 0
				需要发送的数据在uip_buf, 长度是uip_len  (这是2个全局变量)
			*/
			if (uip_len > 0)
			{
				uip_arp_out();
				tapdev_send();
			}
		}
		/* 处理arp报文 */
		else if (BUF->type == htons(UIP_ETHTYPE_ARP))
		{
			uip_arp_arpin();
			/*
				当上面的函数执行后,如果需要发送数据,则全局变量 uip_len > 0
				需要发送的数据在uip_buf, 长度是uip_len  (这是2个全局变量)
			*/
			if (uip_len > 0)
			{
				tapdev_send();
			}
		}
	}
	else if(timer_expired(&periodic_timer))	/* 0.5秒定时器超时 */
	{
		timer_reset(&periodic_timer);	/* 复位0.5秒定时器 */

		/* 轮流处理每个TCP连接, UIP_CONNS缺省是10个 */
		for(i = 0; i < UIP_CONNS; i++)
		{
			uip_periodic(i);	/* 处理TCP通信事件 */
			/*
				当上面的函数执行后,如果需要发送数据,则全局变量 uip_len > 0
				需要发送的数据在uip_buf, 长度是uip_len  (这是2个全局变量)
			*/
			if(uip_len > 0)
			{
				uip_arp_out();
				tapdev_send();
			}
		}

	#if UIP_UDP
		/* 轮流处理每个UDP连接, UIP_UDP_CONNS缺省是10个 */
		for(i = 0; i < UIP_UDP_CONNS; i++)
		{
			uip_udp_periodic(i);	/*处理UDP通信事件 */
			/* 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 */

		/* 每隔10秒调用1次ARP定时器函数 */
		if (timer_expired(&arp_timer))
		{
			timer_reset(&arp_timer);
			uip_arp_timer();
		}
	}
}
int main(void)
{
    pInt8U pBuffer;
    Int32U Size,TranSize;
    int i=0,j;
    int k=0;
    int cnt=0;
    int tempcnt=0;
    int concurCnt = 0;
    Flo64 curP, curQ;
    int meanCalc=1,displayIntro=1;
    AlgoPowers_t PowerLines[3];
    AlgoPowers_t prevPLines;
    AlgoPowers_t curPLines;
    int LearnNewMean = 1;
    int learning =1;
    int recognized=0;
    int plugOut = 0;
    Flo64 cInterval = 5000;
    
    //web
    unsigned int e;
    uip_ipaddr_t ipaddr;
    struct timer periodic_timer, arp_timer;
    
    // int deviceCnt[3] = {0};
    //AlgoLine_t TestLine;
    // pAlgoLine_t pTestLine=&TestLine;
    AlgoDevice_t tmpDev;
//    AlgoDevice_t devProfiles[3]={0};
    AlgoDevice_t devProfiles[3];
    int devNum=0;
    
    
    bool addDevice = 1;
    
    Int32S devLamps[3];
    
    
    #if CDC_DEVICE_SUPPORT_LINE_CODING > 0
        CDC_LineCoding_t CDC_LineCoding;
    UartLineCoding_t UartLineCoding;
    #endif // CDC_DEVICE_SUPPORT_LINE_CODING > 0
        
    
    
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //  GUI init  START
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // initialize touch parametres
    Int32U cursor_x = (C_GLCD_H_SIZE - CURSOR_H_SIZE)/2, cursor_y = (C_GLCD_V_SIZE - CURSOR_V_SIZE)/2;
    ToushRes_t XY_Touch;
    Boolean Touch = FALSE;
    
    
    GLCD_Ctrl (FALSE);
    
    // Init GPIO
    GpioInit();
    
    #ifndef SDRAM_DEBUG
    // MAM init
    MAMCR_bit.MODECTRL = 0;
    MAMTIM_bit.CYCLES  = 3;   // FCLK > 40 MHz
    MAMCR_bit.MODECTRL = 2;   // MAM functions fully enabled
    // Init clock
    InitClock();
    // SDRAM Init
    SDRAM_Init();
    #endif // SDRAM_DEBUG
        // Init VIC   ---interrupt
    VIC_Init();
    
    
    // GLCD init
    GLCD_Init (NULL, NULL);
    
    GLCD_Cursor_Dis(0);
    
    GLCD_Copy_Cursor ((Int32U *)Cursor, 0, sizeof(Cursor)/sizeof(Int32U));
    
    GLCD_Cursor_Cfg(CRSR_FRAME_SYNC | CRSR_PIX_32);
    
    GLCD_Move_Cursor(cursor_x, cursor_y);
    
    GLCD_Cursor_En(0);
    
    // Init touch screen
    TouchScrInit();
    
    // Touched indication LED
    USB_H_LINK_LED_SEL = 0; // GPIO
    USB_H_LINK_LED_FSET = USB_H_LINK_LED_MASK;
    USB_H_LINK_LED_FDIR |= USB_H_LINK_LED_MASK;
    
    // Init UART 0
    UartInit(UART_0,0,NORM);
    
    
    __enable_interrupt();
    
    GLCD_Ctrl (TRUE);
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //  GUI init  END
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    
    
    /* devProfiles[0].dP= 101366.22;   //blow dryer
    devProfiles[0].dQ =51765.90;
    
    devProfiles[1].dP= 35957.14;    //light bulb
    devProfiles[1].dQ = 9045.64;
    */
    
    
    //  GLCD_print("Device char %f %f\r\n",devProfiles[devNum].dP, devProfiles[devNum].dQ );
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    // Update the baud rate
    UartLineCoding.dwDTERate = 115200;
    // Update the stop bits number
    UartLineCoding.bStopBitsFormat = UART_ONE_STOP_BIT;
    // Update the parity type
    UartLineCoding.bParityType = UART_NO_PARITY;
    // Update the word width
    UartLineCoding.bDataBits = (UartWordWidth_t)(3);
    //Description: Init UART Baud rate, Word width, Stop bits, Parity type
    UartSetLineCoding(UART_0,UartLineCoding);
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    //calculate the no load powerlines
    //calcMeanRange(&prevPLines);
    // GLCD_print("P %f %f\n\r",prevPLines.P.CiHigh, prevPLines.P.CiLow);
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    for(i=0; i<3; i++){
      devProfiles[i].dP=0;
      devProfiles[i].dQ=0;
      devLamps[i]=0;
    }
    
    // initialize gui
    gui_monitoringScreen(devProfiles,devLamps);
    
    GLCD_SetFont(&Terminal_6_8_6,0x0000FF,0x000cd4ff);
    GLCD_SetWindow(0,0,319,239);
    GLCD_TextSetPos(0,0);
    //calculate the no load powerlines
    calcMeanRange(&prevPLines);
    
    while(1)
    {
        
        ///////////////////////////////////////////////////////////////////////////////
        GLCD_SetFont(&Terminal_6_8_6,0x0000FF,0x000cd4ff);
        GLCD_SetWindow(0,0,319,239);
        GLCD_TextSetPos(0,0);
        
        /////////////////////////////////////////////////////////////////////////////////
        
        /////////////////////////////////////////////////////////////////////////////////
        //algo    START
        /////////////////////////////////////////////////////////////////////////////////
        if(dataReady){
            dataReady=0;
            GLCD_print("%s\r\n",dataArray);
            //  GLCD_print("reach reach!!!\r\n");
                        
            //convert the incoming data to floats
            dataConversion(dataArray, dataP, dataQ, dataT, cnt);           
            
            //keep track of devices
            GLCD_print("Device zero:%d   one:%d   two:%d\r",devLamps[0],
            devLamps[1],devLamps[2]);
            
            concurCnt=0;
            //detect a step change.
            if(detectStepChange(&prevPLines, dataP[cnt], dataQ[cnt] )){
                tempcnt=(int) fmod(cnt+4,45);
                while(cnt != tempcnt){
                    if(dataReady){
                        dataConversion(dataArray, dataP, dataQ, dataT, cnt);
                        if(detectStepChange(&prevPLines, dataP[cnt], dataQ[cnt] )){
                            concurCnt++;
                        }
                        cnt++;
                        dataReady=0;
                        if(cnt>=45){
                            cnt=0;
                        }
                    }
                }
                
                if(concurCnt>=3){
                    //calculate new powerlines
                    calcMeanRange(&curPLines);
                    
                    tmpDev.dP = curPLines.P.mean - prevPLines.P.mean;
                    tmpDev.dQ = curPLines.Q.mean - prevPLines.Q.mean;
                    GLCD_print("test dev dP is %f\r\n",tmpDev.dP);
                    GLCD_print("test dev dQ is %f\r\n",tmpDev.dQ);
                    
                    
                    //in learning phase
                    if(screen==1){
                        
                        // GLCD_print("we can now add DEVICES!!!\r\n");
                        
                        if(devNum==3){
                            GLCD_print("Can't learn anymore devices\r\n");
                            learning=0;
                        }
                        else{
                            if(tmpDev.dP>0 && tmpDev.dQ>0){
                                
                                GLCD_print("add device or not?\r\n");
                                
                                if(addDevice){
                                    
                                    devProfiles[devNum].dP = tmpDev.dP;
                                    devProfiles[devNum].dQ = tmpDev.dQ;
                                    GLCD_print("new profile %d p:%f q:%f\r\n",devNum,
                                    tmpDev.dP,tmpDev.dQ);
                                    devNum++;
                                }
                                
                            }
                            else{
                                GLCD_print("device is unplugged\r\n");
                                //DO THE CHECK
                                
                                //   determineDevice(devProfiles,tmpDev,&devNum,devLamps);
                                //gui_monitoringScreen(devProfiles,devLamps);
                            }
                        }
                    }
                    //in user phase
                    else if (screen==0){
                        if(devNum==0){
                            GLCD_print("No devices on file. Please enter learning mode");
                        }
                        
                        else{
                            //check aganst known devices
                            for(k=0;k<3;k++){
                                GLCD_print("devProfile %d p:%f  q:%f\r\n",k, devProfiles[k].dP,
                                devProfiles[k].dQ);
                                //if plugging out, the deltas will be negative
                                if(tmpDev.dP<0 && tmpDev.dQ<0){
                                    plugOut=1;
                                    tmpDev.dP = fabs(tmpDev.dP);
                                    tmpDev.dQ = fabs(tmpDev.dQ);
                                }
                                
                                if(withinRange(tmpDev,devProfiles[k],cInterval)){
                                    if(plugOut){
                                        GLCD_print("device %d unplugged!\r\n",k);
                                        plugOut=0;
                                        devLamps[k]=0;
                                    }
                                    else{
                                        GLCD_print("device %d plugged in!\r\n",k);
                                        devLamps[k]=1;
                                    }
                                    gui_monitoringScreen(devProfiles,devLamps);
                                    break;
                                }
                            }
                        } // end of if(devNum>0)
                    }   // end user phase
                    prevPLines = curPLines;
                    GLCD_print("prevPlines %f, %f\r",prevPLines.P,prevPLines.Q);
                }       // end of (concurCnt>=3)
            }           // end of detectStepChange()
            cnt++;
            //dataReady=0;
            if(cnt >= 45){
                cnt=0;
            }
        }               // end of dataReady
        /////////////////////////////////////////////////////////////////////////////////
        //                      End of Algo
        /////////////////////////////////////////////////////////////////////////////////
        
        
        /////////////////////////////////////////////////////////////////////////////////
        //      GUI start
        /////////////////////////////////////////////////////////////////////////////////
        
        if(TouchGet(&XY_Touch))
        {
            cursor_x = XY_Touch.X;
            cursor_y = XY_Touch.Y;
            GLCD_Move_Cursor(cursor_x, cursor_y);
            
            if (FALSE == Touch)
            {
                Touch = TRUE;
                USB_H_LINK_LED_FCLR = USB_H_LINK_LED_MASK;
            }
        }
        
        // check the need to swtich screen
        else if(Touch)
        {
            switch(screen)
            {
                
                case 0: // 0 = Monitoring screen
                
                // Touch logic
                if(modeButtonState)
                {
                    if (cursor_x <= 80 && cursor_y >= 190)
                    {
                        gui_toggleMode(devProfiles,devLamps);
                        break;
                    }
                }
                if(settingsButtonState)
                {
                    if (cursor_x >= 239 && cursor_y >= 190)
                    {
                        gui_settingsScreen();
                        break;
                    }
                }
                break;
                case 1: // 1 = Learning screen
                // Touch logic
                if(modeButtonState)
                {
                    if (cursor_x <= 80 && cursor_y >= 190) //  mode
                    {
                        gui_toggleMode(devProfiles,devLamps);
                        break;
                    }
                }
                if(settingsButtonState)
                {
                    if (cursor_x >= 239 && cursor_y >= 190) //  set
                    {
                        gui_settingsScreen();
                        break;
                    }
                }
                if(addDeviceButtonState)
                {
                    // add device button placement (80,70,240,120)
                    if (cursor_x >= 80 && cursor_y >= 70 && cursor_x <= 240 && cursor_y <= 120)
                    {
                        addDevice = 1;
                        
                        //gui_addDeviceScreen();
                        break;
                    }
                }
                break;
                case 2: // 2 = Add device screen
                // Back button
                if(backButtonState)
                {
                    if (cursor_x <= 80 && cursor_y >= 190)
                    {
                        gui_learningScreen();
                        break;
                    }
                }
                break;
                case 3: // 3: Settings screen
                // Back button
                if(backButtonState)
                {
                    if (cursor_x <= 80 && cursor_y >= 190)
                    {
                        if(gui_getMode())
                        {
                            gui_learningScreen();
                            break;
                        }
                        else
                        {
                            gui_monitoringScreen(devProfiles,devLamps);
                            break;
                        }
                    }
                }
                if(settingsButtonState)
                {
                    if (cursor_x >= 239 && cursor_y >= 190)
                    {
                        gui_settingsScreen();
                        break;
                    }
                }
                break;
                case 4: // 4: Delete Devices screen
                // Back button
                if(backButtonState)
                {
                    if (cursor_x <= 80 && cursor_y >= 190)
                    {
                        gui_learningScreen();
                        break;
                    }
                }
                
                break;
            }
            USB_H_LINK_LED_FSET = USB_H_LINK_LED_MASK;
            Touch = FALSE;
            
        }
        /////////////////////////////////////////////////////////////////////////////////
        
        /////////////////////////////////////////////////////////////////////////////////
    }
    
    
  // 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)
  {
    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(e = 0; e < UIP_CONNS; e++)
      {
      	uip_periodic(e);
        /* 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(e = 0; e < UIP_UDP_CONNS; e++) {
        uip_udp_periodic(e);
        /* 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)
{
	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 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 #18
0
/*************************************************************************
 * Function Name: main
 * Parameters: none
 *
 * Return: none
 *
 * Description: main
 *
 *************************************************************************/
int main(void)
{
typedef Int32U ram_unit;
// int cursor_x = (C_GLCD_H_SIZE - CURSOR_H_SIZE)/2, cursor_y = (C_GLCD_V_SIZE - CURSOR_V_SIZE)/2;
// unsigned long int deltaT;
static float freq_aveg;
int LCD_updatecount;
LCD_updatecount = 0;


//From uip start
unsigned int i;
uip_ipaddr_t ipaddr;
struct timer periodic_timer, arp_timer;
//From uip end
  /*** COMPARE FIX POINT 523235 ***/
  /*** COMPARE FIX POINT 523235 ***/
  GLCD_Ctrl (FALSE);
  // Init GPIO
  GpioInit();
#ifndef SDRAM_DEBUG
  // MAM init
  MAMCR_bit.MODECTRL = 0;
  MAMTIM_bit.CYCLES  = 3;   // FCLK > 40 MHz
  MAMCR_bit.MODECTRL = 2;   // MAM functions fully enabled
  // Init clock
  InitClock();
  // SDRAM Init
  SDRAM_Init();
#endif // SDRAM_DEBUG
  // Init VIC
  VIC_Init();
  // GLCD init
  // GLCD_Init (IarLogoPic.pPicStream, NULL); // Can be removed, remember to remove the h and c file as well
  // GLCD_Init (LogoPic.pPicStream, NULL);  // Can be removed, remember to remove the h and c file as well
  GLCD_Init (what_is_a_blissPic.pPicStream, NULL);
  GLCD_Cursor_Dis(0); //From uip
  // GLCD_Cursor_Dis(0);

  // GLCD_Copy_Cursor ((Int32U *)Cursor, 0, sizeof(Cursor)/sizeof(Int32U));
  /*** COMPARE FIX POINT 534252 ***/
  /*** COMPARE FIX POINT 534252 ***/
  GLCD_Cursor_Cfg(CRSR_FRAME_SYNC | CRSR_PIX_32); //From uip
  // GLCD_Cursor_Cfg(CRSR_FRAME_SYNC | CRSR_PIX_64);
  // GLCD_Move_Cursor(cursor_x, cursor_y);
  // GLCD_Cursor_En(0);

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

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


  // Init USB Link  LED
  USB_D_LINK_LED_SEL = 0; // GPIO
  USB_D_LINK_LED_FSET = USB_D_LINK_LED_MASK;
  USB_D_LINK_LED_FDIR |= USB_D_LINK_LED_MASK;
  
  USB_H_LINK_LED_SEL = 0; // GPIO
  USB_H_LINK_LED_FSET = USB_H_LINK_LED_MASK;
  USB_H_LINK_LED_FDIR |= USB_H_LINK_LED_MASK;

  
  /*-----------------------------------------------------------------*/
  
  // Init AD0[3] for current meassurement
  PINSEL1_bit.P0_26 = 1; // Assign P26 to AD0[3], page 180
  PINMODE1_bit.P0_26 = 2; // //Neither pull-up or pull-down
  // PCONP_bit.PCAD = 1;     // Note: Clear the PDN bit in the AD0CR before clearing this bit and set this before PDN
  // Other initial parameters are already set
  // AD0CR_bit.SEL  = 8;     // select Ch3 [1111]
  current_amp = 0;
  
  
  /*-----------------------------------------------------------------*/
  // Init the DAC converter
  //Clock: In the PCLK_SEL0 register (Table 4�), select PCLK_DAC
    //PCLKSEL0_bit.PCLK_DAC = 3UL;// **HAS Desided for values yet!** // '11' at bit 23 and 22  (which is CCLK/8)  //or use 0x3 for 3UL instead  
  //Pins: Select the DAC pin and pin mode in registers PINSEL1 and PINMODE1 (see Section 9�.
  //PINSEL1 |= (2UL<<20); //  PINSEL1_bit.P0_26 = 1; //??        
    //PINSEL1_bit.P0_26 = 2UL; 
  //"PINMODE registers control the on-chip pull-up/pull-down resistor feature for all GPIO ports." - page 178
  //PINMODE1 |= ________; // See table 128 for values. Write to bit 21:20
    //PINMODE1_bit.P0_26 = 2UL;  //P0.26MODE = 2UL; //Neither pull-up or pull-down


 /* ------------------------------------------------------------------*/
  // Init ADC converter
  // Power the ADC converter
  PINSEL1_bit.P0_25 = 1; // Assign Pin 25 to ADO[2]
  PINMODE1_bit.P0_25 = 1; // Neither pull-up or pull-dow
  PCONP_bit.PCAD = 1;     // Note: Clear the PDN bit in the AD0CR before clearing this bit and set this before PDN
  AD0CR_bit.PDN = 1;      // A/D converter is operational
  AD0CR_bit.START = 0;    // Conversion not started
  AD0CR_bit.BURST  = 0;   // disable burst
  // AD0CR_bit.SEL  = 4;     // select Ch2 [11]
  
  // Select number of clocks for each conversion
  AD0CR_bit.CLKS = 0; // [000] 11 clocks / 10 bits
  AD0CR_bit.CLKDIV = SYS_GetFpclk(ADC_PCLK_OFFSET)/ 10000; // 4500000;  // Should be equal to 10K samplingrate
  ADINTEN_bit.ADGINTEN = 1; // Global A/D channels enabled by ADINTEN 7:0
  // Since only on channel is used at the moment the global flag is enabled
  
  VIC_SetVectoredIRQ(AD0IntrHandler,1,VIC_AD0); // Set the interrupt call
  VICINTENABLE |= 1UL << VIC_AD0;
  


  // Setting parameters for the low-pass filter
  DACR_previous = 0; // Initialize DACR_temp which is y(i-1)
  deltaT = 1.0/TIMER0_TICK_PER_SEC; // Set the sample rate
  // Calculate the R*C for cut-off frequency of the low pass filter
  alpha = deltaT/(1./(2.*3.1416*100.) + deltaT); // Cut-off = 100 Hz
  done = 0; // Channel stage
/* ------------------------------------------------------------------*/
  
  // Setting the port to P0[11] and P0[19]
  PINSEL1_bit.P0_19 = 0; // GPIO to P0[19]
  PINSEL0_bit.P0_11 = 0; // GPIO to P0[11]
  PINMODE1_bit.P0_19 = 2; // Pin has neither pull up or down
  PINMODE0_bit.P0_11 = 2; // Pin has neither pull up or down
  FIO0DIR_bit.P0_19  = 1; 
  FIO0CLR  = (1UL<<19);
  FIO0DIR_bit.P0_11 = 1;
  FIO0CLR  = (1UL<<11);
  
  FIO0PIN_bit.P0_19 = 1;
  FIO0PIN_bit.P0_11 = 1;
/* ------------------------------------------------------------------*/  
  // Enable TIM0 clocks
  PCONP_bit.PCTIM0 = 1; // enable clock

  // Init Time0
  T0TCR_bit.CE = 0;     // counting  disable
  T0TCR_bit.CR = 1;     // set reset
  T0TCR_bit.CR = 0;     // release reset
  T0CTCR_bit.CTM = 0;   // Timer Mode: every rising PCLK edge
  T0MCR_bit.MR0I = 1;   // Enable Interrupt on MR0
  T0MCR_bit.MR0R = 1;   // Enable reset on MR0
  T0MCR_bit.MR0S = 0;   // Disable stop on MR0
  // set timer 0 period
  T0PR = 0;
  T0MR0 = SYS_GetFpclk(TIMER0_PCLK_OFFSET)/(TIMER0_TICK_PER_SEC);
  // init timer 0 interrupt
  T0IR_bit.MR0INT = 1;  // clear pending interrupt
  VIC_SetVectoredIRQ(Timer0IntrHandler,0,VIC_TIMER0);
  VICINTENABLE |= 1UL << VIC_TIMER0;
  T0TCR_bit.CE = 1;     // counting Enable
  __enable_interrupt();
  GLCD_Ctrl (TRUE);

#if 0
  SDRAM_Test();
#endif

  /*
  //
  SMB380_Init();

  SMB380_GetID(&Smb380Id, &Smb380Ver);

  SMB380_Data_t XYZT;
  */

  /*** COMPARE FIX POINT 856364 ***/
  /*** COMPARE FIX POINT 856364 ***/
  /*** COMPARE FIX POINT 856364 ***/
  /*** COMPARE FIX POINT 856364 ***/

  //From uip start
  GLCD_SetFont(&Terminal_18_24_12,0x000000,0x000cd4ff);
  GLCD_SetWindow(85,10,255,33);
  GLCD_TextSetPos(0,0);
  GLCD_print("\f Room Station");
  
  
  //From uip start

  /*** COMPARE FIX POINT 458923 ***/
  /*** COMPARE FIX POINT 458923 ***/

  // GLCD_SetWindow(5,200,319,239);
  // GLCD_SetFont(&Terminal_6_8_6,0x0000FF,0x000cd4ff);

  // 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();

  /*** COMPARE FIX POINT 4572742 ***/
  /*** COMPARE FIX POINT 4572742 ***/
  /*** COMPARE FIX POINT 4572742 ***/
  /*** COMPARE FIX POINT 4572742 ***/


/*** WHILE LOOP START ***/
  while(1)
  {

  /*** COMPARE FIX POINT 938194 ***/
  /*** COMPARE FIX POINT 938194 ***/
  /*** COMPARE FIX POINT 938194 ***/
  /*** COMPARE FIX POINT 938194 ***/
  /*** COMPARE FIX POINT 938194 ***/

    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();
      }
    }

    #define AVERAGECOUNT 100000
    if(LCD_updatecount <= AVERAGECOUNT) {
      ++LCD_updatecount;
      freq_aveg += freq;
    }
    else { 
      freq_aveg = freq_aveg/AVERAGECOUNT;

      updateFreqHistory(freq_aveg); //Must be kept together with freq calculation!

      GLCD_SetWindow(20,55,150,80);
      GLCD_SetFont(&Terminal_18_24_12,0x000000,0x009fee00);
      GLCD_TextSetPos(0,5);
      GLCD_print("\f Hz %3.3f", freq_aveg);
      freq_aveg = 0;
      
      GLCD_SetWindow(20,90,150,115);
      GLCD_SetFont(&Terminal_18_24_12,0x000000,0x009fee00);
      GLCD_TextSetPos(0,5);
      GLCD_print("\f  V  %3.3f", sqrtf(vol_rms_result));

      updateVoltageHistory(sqrtf(vol_rms_result));
      
      GLCD_SetWindow(20,125,150,150);
      GLCD_SetFont(&Terminal_18_24_12,0x000000,0x009fee00);
      GLCD_TextSetPos(0,5);
      GLCD_print("\f uA  %3.3f", sqrtf(current_amp));
      
      GLCD_SetWindow(20,160,150,185);
      GLCD_SetFont(&Terminal_18_24_12,0x000000,0x009fee00);
      GLCD_TextSetPos(0,5);
      GLCD_print("\f uP  %3.3f", sqrtf(vol_rms_result)*sqrtf(current_amp));
      LCD_updatecount = 0;
    }
    
    
  }//while(1) loop

}//main function