/* * Ethernet frame received. */ static void FrameReceivedHandler(eventid_t id) { (void)id; while ((uip_len = network_device_read()) > 0) { if (BUF->type == HTONS(UIP_ETHTYPE_IP)) { uip_arp_ipin(); uip_input(); if (uip_len > 0) { uip_arp_out(); network_device_send(); } } else if (BUF->type == HTONS(UIP_ETHTYPE_ARP)) { uip_arp_arpin(); if (uip_len > 0) network_device_send(); } } }
int main(void) { struct timer periodic_timer, arp_timer; // Flash the status LED two times to indicate that the AVR controller is living STATUS_LED_on(); _delay_ms(20); STATUS_LED_off(); _delay_ms(100); STATUS_LED_on(); _delay_ms(20); STATUS_LED_off(); // Initialize direction and level of I/O pins init_io_pins(); #if ADC_CHANNELS > 0 // initialize ADC converter initADC(); #endif // enable io functions isBusy=0; // Initialize system clock timers. clock_init(); timer_set(&periodic_timer, CLOCK_SECOND); timer_set(&arp_timer, CLOCK_SECOND*10); // check if the config reset jumper is connected. check_for_reset_jumper(); #ifdef SERIAL // start the serial port server seriald_init(); #endif // SERIAL // initialize ethernet controller // loop until link comes up while (init_CP2200()==0) { _delay_ms(200); } // replace config by hardcoded defaults if the flash is unprogrammed (all bits 1) if (uip_ipaddr_cmp(config.ipaddr, all_ones_addr) && uip_ipaddr_cmp(config.netmask, all_ones_addr) && uip_ipaddr_cmp(config.gateway, all_ones_addr)) { set_defaults(); } // initialize uIP protocol uip_arp_init(); uip_init(); // Configure the IP-Adrdess IP_config(); #ifdef EMAIL_APP // Initialize the email application email_app_init(); #endif // EMAIL_APP // Initialize the inetd inetd_init(); // main loop, dispatches network and timer events 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 (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(); network_device_send(); } } #if UIP_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(); 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(); } } #ifdef EMAIL_APP email_app_main_loop(); #endif //EMAIL_APP } return 0; }
///////////////////////////////////////////////////////////////////////////// // The uIP Task is executed each mS ///////////////////////////////////////////////////////////////////////////// static void UIP_TASK_Handler(void *pvParameters) { int i; struct timer periodic_timer, arp_timer; // Initialise the xLastExecutionTime variable on task entry portTickType xLastExecutionTime = xTaskGetTickCount(); // take over exclusive access to UIP functions MUTEX_UIP_TAKE; // init uIP timers timer_set(&periodic_timer, CLOCK_SECOND / 2); timer_set(&arp_timer, CLOCK_SECOND * 10); // init the network driver network_device_init(); // init uIP uip_init(); uip_arp_init(); // set my ethernet address unsigned char *mac_addr = network_device_mac_addr(); { int i; for(i=0; i<6; ++i) uip_ethaddr.addr[i] = mac_addr[i]; } #ifndef DONT_USE_DHCP dhcpc_init(uip_ethaddr.addr, sizeof(uip_ethaddr.addr)); MIOS32_MIDI_SendDebugMessage("[UIP_TASK] DHCP Client requests the IP settings...\n"); #else uip_ipaddr_t ipaddr; // set my IP address uip_ipaddr(ipaddr, ((MY_IP_ADDRESS)>>24) & 0xff, ((MY_IP_ADDRESS)>>16) & 0xff, ((MY_IP_ADDRESS)>> 8) & 0xff, ((MY_IP_ADDRESS)>> 0) & 0xff); uip_sethostaddr(ipaddr); // set my netmask uip_ipaddr(ipaddr, ((MY_NETMASK)>>24) & 0xff, ((MY_NETMASK)>>16) & 0xff, ((MY_NETMASK)>> 8) & 0xff, ((MY_NETMASK)>> 0) & 0xff); uip_setnetmask(ipaddr); // default router uip_ipaddr(ipaddr, ((MY_GATEWAY)>>24) & 0xff, ((MY_GATEWAY)>>16) & 0xff, ((MY_GATEWAY)>> 8) & 0xff, ((MY_GATEWAY)>> 0) & 0xff); uip_setdraddr(ipaddr); MIOS32_MIDI_SendDebugMessage("[UIP_TASK] IP Address statically set:\n"); // start services immediately UIP_TASK_StartServices(); #endif // release exclusive access to UIP functions MUTEX_UIP_GIVE; // endless loop while( 1 ) { vTaskDelayUntil(&xLastExecutionTime, 1 / portTICK_RATE_MS); // take over exclusive access to UIP functions MUTEX_UIP_TAKE; if( !(clock_time_tick() % 100) ) { // each 100 mS: check availablility of network device network_device_check(); } if( network_device_available() ) { 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(); } } } // release exclusive access to UIP functions MUTEX_UIP_GIVE; } }
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; }
/*---------------------------------------------------------------------------*/ 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; }
///////////////////////////////////////////////////////////////////////////// // The uIP Task is executed each mS ///////////////////////////////////////////////////////////////////////////// static void UIP_TASK_Handler(void *pvParameters) { int i; struct timer periodic_timer, arp_timer; // take over exclusive access to UIP functions MUTEX_UIP_TAKE; // init uIP timers timer_set(&periodic_timer, CLOCK_SECOND / 2); timer_set(&arp_timer, CLOCK_SECOND * 10); // init the network driver network_device_init(); // init uIP uip_init(); uip_arp_init(); // set my ethernet address unsigned char *mac_addr = network_device_mac_addr(); { int i; for(i=0; i<6; ++i) uip_ethaddr.addr[i] = mac_addr[i]; } // enable dhcp mode (can be changed during runtime) UIP_TASK_DHCP_EnableSet(dhcp_enabled); // release exclusive access to UIP functions MUTEX_UIP_GIVE; #if 0 // wait until HW config has been loaded do { vTaskDelay(1 / portTICK_RATE_MS); } while( !SEQ_FILE_HW_ConfigLocked() ); #endif // Initialise the xLastExecutionTime variable on task entry portTickType xLastExecutionTime = xTaskGetTickCount(); // endless loop while( 1 ) { #if 0 do { vTaskDelayUntil(&xLastExecutionTime, 1 / portTICK_RATE_MS); } while( TASK_MSD_EnableGet() ); // don't service ethernet if MSD mode enabled for faster transfer speed #else vTaskDelayUntil(&xLastExecutionTime, 1 / portTICK_RATE_MS); #endif // take over exclusive access to UIP functions MUTEX_UIP_TAKE; if( !(clock_time_tick() % 100) ) { // each 100 mS: check availablility of network device #if defined(MIOS32_BOARD_MBHP_CORE_LPC17) || defined(MIOS32_BOARD_LPCXPRESSO) network_device_check(); // TK: on STM32 no auto-detection for MBSEQ for best performance if no MBHP_ETH module connected // the user has to reboot MBSEQ to restart module detection #endif } if( network_device_available() ) { 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(); } } } // release exclusive access to UIP functions MUTEX_UIP_GIVE; } }