]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/portable/NetworkInterface/SAM4E/NetworkInterface.c
Release candidate - this will be tagged as FreeRTOS V8.2.0rc1 and a zip file provided.
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-UDP / portable / NetworkInterface / SAM4E / NetworkInterface.c
index 3da074ab2619ea5f4535bea84a55eca660f3cc19..c25c0c31fbd88a14e11c2412677700ff9bef5b8e 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
- * FreeRTOS+UDP V1.0.2 (C) 2013 Real Time Engineers ltd.\r
+ * FreeRTOS+UDP V1.0.4 (C) 2014 Real Time Engineers ltd.\r
  * All rights reserved\r
  *\r
  * This file is part of the FreeRTOS+UDP distribution.  The FreeRTOS+UDP license\r
@@ -19,9 +19,9 @@
  *\r
  * - Commercial licensing -\r
  * Businesses and individuals that for commercial or other reasons cannot comply\r
- * with the terms of the GPL V2 license must obtain a commercial license before \r
- * incorporating FreeRTOS+UDP into proprietary software for distribution in any \r
- * form.  Commercial licenses can be purchased from http://shop.freertos.org/udp \r
+ * with the terms of the GPL V2 license must obtain a commercial license before\r
+ * incorporating FreeRTOS+UDP into proprietary software for distribution in any\r
+ * form.  Commercial licenses can be purchased from http://shop.freertos.org/udp\r
  * and do not require any source files to be changed.\r
  *\r
  * FreeRTOS+UDP is distributed in the hope that it will be useful.  You cannot\r
@@ -40,6 +40,7 @@
 \r
 /* Standard includes. */\r
 #include <stdint.h>\r
+#include <limits.h>\r
 \r
 /* FreeRTOS includes. */\r
 #include "FreeRTOS.h"\r
@@ -60,7 +61,7 @@
 operation will be held in the Blocked state (so other tasks can execute) for\r
 niTX_BUFFER_FREE_WAIT ticks.  It will do this a maximum of niMAX_TX_ATTEMPTS\r
 before giving up. */\r
-#define niTX_BUFFER_FREE_WAIT  ( ( portTickType ) 2UL / portTICK_RATE_MS )\r
+#define niTX_BUFFER_FREE_WAIT  ( ( TickType_t ) 2UL / portTICK_RATE_MS )\r
 #define niMAX_TX_ATTEMPTS              ( 5 )\r
 \r
 /*-----------------------------------------------------------*/\r
@@ -80,24 +81,23 @@ static void prvGMACRxCallback( uint32_t ulStatus );
 /* The queue used to communicate Ethernet events to the IP task. */\r
 extern xQueueHandle xNetworkEventQueue;\r
 \r
-/* The semaphore used to wake the deferred interrupt handler task when an Rx\r
-interrupt is received. */\r
-static xSemaphoreHandle xGMACRxEventSemaphore = NULL;\r
-\r
 /* The GMAC driver instance. */\r
 static gmac_device_t xGMACStruct;\r
 \r
+/* Handle of the task used to process MAC events. */\r
+static TaskHandle_t xMACEventHandlingTask = NULL;\r
+\r
 /*-----------------------------------------------------------*/\r
 \r
-portBASE_TYPE xNetworkInterfaceInitialise( void )\r
+BaseType_t xNetworkInterfaceInitialise( void )\r
 {\r
 gmac_options_t xGMACOptions;\r
 extern uint8_t ucMACAddress[ 6 ];\r
-const portTickType xPHYDelay_400ms = 400UL;\r
-portBASE_TYPE xReturn = pdFALSE;\r
+const TickType_t xPHYDelay_400ms = 400UL;\r
+BaseType_t xReturn = pdFALSE;\r
 \r
        /* Ensure PHY is ready. */\r
-       vTaskDelay( xPHYDelay_400ms / portTICK_RATE_MS );       \r
+       vTaskDelay( xPHYDelay_400ms / portTICK_RATE_MS );\r
 \r
        /* Enable GMAC clock. */\r
        pmc_enable_periph_clk( ID_GMAC );\r
@@ -116,42 +116,28 @@ portBASE_TYPE xReturn = pdFALSE;
        if( ethernet_phy_init( GMAC, BOARD_GMAC_PHY_ADDR, sysclk_get_cpu_hz() ) == GMAC_OK )\r
        {\r
                /* Auto Negotiate, work in RMII mode. */\r
-               if( ethernet_phy_auto_negotiate( GMAC, BOARD_GMAC_PHY_ADDR ) == GMAC_OK ) \r
+               if( ethernet_phy_auto_negotiate( GMAC, BOARD_GMAC_PHY_ADDR ) == GMAC_OK )\r
                {\r
                        /* Establish Ethernet link. */\r
                        vTaskDelay( xPHYDelay_400ms * 2UL );\r
                        if( ethernet_phy_set_link( GMAC, BOARD_GMAC_PHY_ADDR, 1 ) == GMAC_OK )\r
                        {\r
-                               /* Create the event semaphore if it has not already been \r
-                               created. */\r
-                               if( xGMACRxEventSemaphore == NULL )\r
-                               {\r
-                                       vSemaphoreCreateBinary( xGMACRxEventSemaphore );\r
-                                       #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1\r
-                                       {\r
-                                               /* If the trace recorder code is included name the semaphore for\r
-                                               viewing in FreeRTOS+Trace. */\r
-                                               vTraceSetQueueName( xGMACRxEventSemaphore, "MAC_RX" );\r
-                                       }\r
-                                       #endif /*  ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */\r
-                               }\r
-                               \r
                                /* Register the callbacks. */\r
                                gmac_dev_set_rx_callback( &xGMACStruct, prvGMACRxCallback );\r
-                               \r
-                               /* The Rx deferred interrupt handler task is created at the \r
-                               highest possible priority to ensure the interrupt handler can \r
-                               return directly to it no matter which task was running when the \r
+\r
+                               /* The Rx deferred interrupt handler task is created at the\r
+                               highest possible priority to ensure the interrupt handler can\r
+                               return directly to it no matter which task was running when the\r
                                interrupt occurred. */\r
                                xTaskCreate(    prvGMACDeferredInterruptHandlerTask,/* The function that implements the task. */\r
-                                                               ( const signed char * const ) "MACTsk",\r
+                                                               "MACTsk",\r
                                                                configMINIMAL_STACK_SIZE,       /* Stack allocated to the task (defined in words, not bytes). */\r
                                                                NULL,                                           /* The task parameter is not used. */\r
                                                                configMAX_PRIORITIES - 1,       /* The priority assigned to the task. */\r
-                                                               NULL );                                         /* The handle is not required, so NULL is passed. */\r
-                               \r
-                               /* Enable the interrupt and set its priority as configured.  \r
-                               THIS DRIVER REQUIRES configMAC_INTERRUPT_PRIORITY TO BE DEFINED, \r
+                                                               &xMACEventHandlingTask );       /* The handle is stored so the ISR knows which task to notify. */\r
+\r
+                               /* Enable the interrupt and set its priority as configured.\r
+                               THIS DRIVER REQUIRES configMAC_INTERRUPT_PRIORITY TO BE DEFINED,\r
                                PREFERABLY IN FreeRTOSConfig.h. */\r
                                NVIC_SetPriority( GMAC_IRQn, configMAC_INTERRUPT_PRIORITY );\r
                                NVIC_EnableIRQ( GMAC_IRQn );\r
@@ -166,21 +152,23 @@ portBASE_TYPE xReturn = pdFALSE;
 \r
 static void prvGMACRxCallback( uint32_t ulStatus )\r
 {\r
-portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
+BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
+\r
+       configASSERT( xMACEventHandlingTask );\r
 \r
        /* Unblock the deferred interrupt handler task if the event was an Rx. */\r
-       if( ulStatus != 0 )\r
+       if( ( ulStatus & GMAC_RSR_REC ) != 0 )\r
        {\r
-               xSemaphoreGiveFromISR( xGMACRxEventSemaphore, &xHigherPriorityTaskWoken );\r
+               vTaskNotifyGiveFromISR( xMACEventHandlingTask, &xHigherPriorityTaskWoken );\r
        }\r
 \r
        portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-portBASE_TYPE xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
+BaseType_t xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
 {\r
-portBASE_TYPE xReturn = pdFAIL;\r
+BaseType_t xReturn = pdFAIL;\r
 int32_t x;\r
 \r
        /* Attempt to obtain access to a Tx descriptor. */\r
@@ -214,80 +202,101 @@ void GMAC_Handler( void )
 \r
 static void prvGMACDeferredInterruptHandlerTask( void *pvParameters )\r
 {\r
-xNetworkBufferDescriptor_t *pxNetworkBuffer;\r
+xNetworkBufferDescriptor_t *pxNetworkBuffer = NULL;\r
 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };\r
+static const TickType_t xBufferWaitDelay = 1500UL / portTICK_RATE_MS;\r
+uint32_t ulReturned;\r
+\r
+       /* This is a very simply but also inefficient implementation. */\r
 \r
        ( void ) pvParameters;\r
-       configASSERT( xGMACRxEventSemaphore );\r
 \r
        for( ;; )\r
        {\r
                /* Wait for the GMAC interrupt to indicate that another packet has been\r
-               received.  The while() loop is only needed if INCLUDE_vTaskSuspend is\r
-               set to 0 in FreeRTOSConfig.h.  If INCLUDE_vTaskSuspend is set to 1\r
-               then portMAX_DELAY would be an indefinite block time and\r
-               xSemaphoreTake() would only return when the semaphore was actually\r
-               obtained. */\r
-               while( xSemaphoreTake( xGMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );\r
-\r
-               /* The buffer filled by the DMA is going to be passed into the IP\r
-               stack.  Allocate another buffer for the DMA descriptor. */\r
-               pxNetworkBuffer = pxNetworkBufferGet( ipTOTAL_ETHERNET_FRAME_SIZE, portMAX_DELAY );\r
-               \r
-               if( pxNetworkBuffer != NULL )\r
+               received.  A while loop is used to process all received frames each time\r
+               this task is notified, so it is ok to clear the notification count on the\r
+               take (hence the first parameter is pdTRUE ). */\r
+               ulTaskNotifyTake( pdTRUE, xBufferWaitDelay );\r
+\r
+               ulReturned = GMAC_OK;\r
+               while( ulReturned == GMAC_OK )\r
                {\r
-                       /* At least one packet has been received. */\r
-                       if( gmac_dev_read( &xGMACStruct, pxNetworkBuffer->pucEthernetBuffer, ipTOTAL_ETHERNET_FRAME_SIZE, ( uint32_t * ) &( pxNetworkBuffer->xDataLength ) ) == GMAC_OK )\r
+                       /* Allocate a buffer to hold the data if one is not already held. */\r
+                       if( pxNetworkBuffer == NULL )\r
+                       {\r
+                               pxNetworkBuffer = pxNetworkBufferGet( ipTOTAL_ETHERNET_FRAME_SIZE, xBufferWaitDelay );\r
+                       }\r
+\r
+                       if( pxNetworkBuffer != NULL )\r
                        {\r
-                               #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 1\r
+                               /* Attempt to read data. */\r
+                               ulReturned = gmac_dev_read( &xGMACStruct, pxNetworkBuffer->pucEthernetBuffer, ipTOTAL_ETHERNET_FRAME_SIZE, ( uint32_t * ) &( pxNetworkBuffer->xDataLength ) );\r
+\r
+                               if( ulReturned == GMAC_OK )\r
                                {\r
-                                       if( pxNetworkBuffer->xDataLength > 0 )\r
+                                       #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 1\r
                                        {\r
-                                               /* If the frame would not be processed by the IP stack then\r
-                                               don't even bother sending it to the IP stack. */\r
-                                               if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) != eProcessBuffer )\r
+                                               if( pxNetworkBuffer->xDataLength > 0 )\r
                                                {\r
-                                                       pxNetworkBuffer->xDataLength = 0;\r
+                                                       /* If the frame would not be processed by the IP\r
+                                                       stack then don't even bother sending it to the IP\r
+                                                       stack. */\r
+                                                       if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) != eProcessBuffer )\r
+                                                       {\r
+                                                               pxNetworkBuffer->xDataLength = 0;\r
+                                                       }\r
                                                }\r
                                        }\r
-                               }\r
-                               #endif\r
+                                       #endif\r
 \r
-                               if( pxNetworkBuffer->xDataLength > 0 )\r
-                               {\r
-                                       /* Store a pointer to the network buffer structure in the\r
-                                       padding space that was left in front of the Ethernet frame.\r
-                                       The pointer     is needed to ensure the network buffer structure\r
-                                       can be located when it is time for it to be freed if the\r
-                                       Ethernet frame gets     used as a zero copy buffer. */\r
-                                       *( ( xNetworkBufferDescriptor_t ** ) ( ( pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING ) ) ) = pxNetworkBuffer;\r
-\r
-                                       /* Data was received and stored.  Send it to the IP task\r
-                                       for processing. */\r
-                                       xRxEvent.pvData = ( void * ) pxNetworkBuffer;\r
-                                       if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( portTickType ) 0 ) == pdFALSE )\r
+                                       if( pxNetworkBuffer->xDataLength > 0 )\r
                                        {\r
-                                               /* The buffer could not be sent to the IP task so the\r
-                                               buffer must be released. */\r
-                                               vNetworkBufferRelease( pxNetworkBuffer );\r
-                                               iptraceETHERNET_RX_EVENT_LOST();\r
+                                               /* Store a pointer to the network buffer structure in\r
+                                               the     padding space that was left in front of the Ethernet\r
+                                               frame.  The pointer is needed to ensure the network\r
+                                               buffer structure can be located when it is time for it\r
+                                               to be freed if the Ethernet frame gets used as a zero\r
+                                               copy buffer. */\r
+                                               *( ( xNetworkBufferDescriptor_t ** ) ( ( pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING ) ) ) = pxNetworkBuffer;\r
+\r
+                                               /* Data was received and stored.  Send it to the IP task\r
+                                               for processing. */\r
+                                               xRxEvent.pvData = ( void * ) pxNetworkBuffer;\r
+                                               if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( TickType_t ) 0 ) == pdFALSE )\r
+                                               {\r
+                                                       /* The buffer could not be sent to the IP task. The\r
+                                                       frame will be dropped and the buffer reused. */\r
+                                                       iptraceETHERNET_RX_EVENT_LOST();\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       iptraceNETWORK_INTERFACE_RECEIVE();\r
+\r
+                                                       /* The buffer is not owned by the IP task - a new\r
+                                                       buffer is needed the next time around. */\r
+                                                       pxNetworkBuffer = NULL;\r
+                                               }\r
                                        }\r
                                        else\r
                                        {\r
-                                               iptraceNETWORK_INTERFACE_RECEIVE();\r
+                                               /* The buffer does not contain any data so there is no\r
+                                               point sending it to the IP task.  Re-use the buffer on\r
+                                               the next loop. */\r
+                                               iptraceETHERNET_RX_EVENT_LOST();\r
                                        }\r
                                }\r
                                else\r
                                {\r
-                                       /* The buffer does not contain any data so there is no\r
-                                       point sending it to the IP task.  Just release it. */\r
-                                       vNetworkBufferRelease( pxNetworkBuffer );\r
-                                       iptraceETHERNET_RX_EVENT_LOST();\r
+                                       /* No data was received, keep the buffer for re-use.  The\r
+                                       loop will exit as ulReturn is not GMAC_OK. */\r
                                }\r
                        }\r
                        else\r
                        {\r
-                               iptraceETHERNET_RX_EVENT_LOST();\r
+                               /* Left a frame in the driver as a buffer was not available.\r
+                               Break out of loop. */\r
+                               ulReturned = GMAC_INVALID;\r
                        }\r
                }\r
        }\r