BaseType_t xNetworkInterfaceInitialise( void ) { BaseType_t xReturn; extern uint8_t ucMACAddress[ 6 ]; xReturn = xEMACInit( ucMACAddress ); if( xReturn == pdPASS ) { /* Create the event semaphore if it has not already been created. */ if( xEMACRxEventSemaphore == NULL ) { vSemaphoreCreateBinary( xEMACRxEventSemaphore ); #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 { /* If the trace recorder code is included name the semaphore for viewing in FreeRTOS+Trace. */ vTraceSetQueueName( xEMACRxEventSemaphore, "MAC_RX" ); } #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */ configASSERT( xEMACRxEventSemaphore ); /* The Rx deferred interrupt handler task is created at the highest possible priority to ensure the interrupt handler can return directly to it no matter which task was running when the interrupt occurred. */ xTaskCreate( prvEMACDeferredInterruptHandlerTask,/* The function that implements the task. */ "MACTsk", configMINIMAL_STACK_SIZE, /* Stack allocated to the task (defined in words, not bytes). */ NULL, /* The task parameter is not used. */ configMAX_PRIORITIES - 1, /* The priority assigned to the task. */ NULL ); /* The handle is not required, so NULL is passed. */ } } return xReturn; }
static void low_level_init(struct netif *netif) { //struct ethernetif *ethernetif = netif->state; /* MAKINGTHINGS: Addition for FREERTOS */ unsigned portBASE_TYPE uxPriority; /* set MAC hardware address length */ netif->hwaddr_len = 6; /* set MAC hardware address */ /* MAKINGTHINGS: Added */ /* MAKINGTHINGS: Tweaked */ netif->hwaddr[0] = emacETHADDR0; netif->hwaddr[1] = emacETHADDR1; netif->hwaddr[2] = emacETHADDR2; netif->hwaddr[3] = emacETHADDR3; netif->hwaddr[4] = emacETHADDR4; netif->hwaddr[5] = emacETHADDR5; /* MAKINGTHINGS: Removed */ /* netif->hwaddr[0] = ; ... netif->hwaddr[5] = ; */ /* maximum transfer unit */ /* MAKINGTHINGS: Added */ netif->mtu = netifMTU; /* MAKINGTHINGS: Removed */ //netif->mtu = 1500; /* broadcast capability */ netif->flags = NETIF_FLAG_BROADCAST; /* Do whatever else is needed to initialize interface. */ xNetIf = netif; /* Initialise the EMAC. 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( xEMACInit() == NULL ) { __asm( "NOP" ); } vTaskPrioritySet( NULL, uxPriority ); /* Create the task that handles the EMAC. */ xTaskCreate( ethernetif_input, ( signed portCHAR * ) "ETH_INT", netifINTERFACE_TASK_STACK_SIZE, NULL, netifINTERFACE_TASK_PRIORITY, NULL ); }
void vuIP_Task( void *pvParameters ) { portBASE_TYPE i; uip_ipaddr_t xIPAddr; struct timer periodic_timer, arp_timer; /* 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. */ do { vTaskDelay( uipINIT_WAIT ); xEMACSemaphore = xEMACInit(); } while( xEMACSemaphore == NULL ); for( ;; ) { /* Is there received data ready to be processed? */ uip_len = ulEMACPoll(); 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(); lEMACSend(); } } 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 ) { lEMACSend(); } } } 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(); lEMACSend(); } } /* 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 ); } } } }
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 ); } } } }