portBASE_TYPE xNetworkInterfaceInitialise( void ) { portBASE_TYPE xStatus, xReturn; extern uint8_t ucMACAddress[ 6 ]; /* Initialise the MAC. */ vInitEmac(); while( lEMACWaitForLink() != pdPASS ) { vTaskDelay( 20 ); } vSemaphoreCreateBinary( xEMACRxEventSemaphore ); configASSERT( xEMACRxEventSemaphore ); /* The handler task is created at the highest possible priority to ensure the interrupt handler can return directly to it. */ xTaskCreate( vEMACHandlerTask, ( const signed char * const ) "EMAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL ); xReturn = pdPASS; return xReturn; }
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; } } }
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 ); } } } }