2 * FreeRTOS+UDP V1.0.4
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* Standard includes. */
\r
33 /* FreeRTOS includes. */
\r
34 #include "FreeRTOS.h"
\r
39 /* FreeRTOS+UDP includes. */
\r
40 #include "FreeRTOS_UDP_IP.h"
\r
41 #include "FreeRTOS_IP_Private.h"
\r
42 #include "FreeRTOS_Sockets.h"
\r
43 #include "NetworkBufferManagement.h"
\r
45 /* Demo includes. */
\r
46 #include "NetworkInterface.h"
\r
48 /* If a packet cannot be sent immediately then the task performing the send
\r
49 operation will be held in the Blocked state (so other tasks can execute) for
\r
50 niTX_BUFFER_FREE_WAIT ticks. It will do this a maximum of niMAX_TX_ATTEMPTS
\r
51 before giving up. */
\r
52 #define niTX_BUFFER_FREE_WAIT ( ( TickType_t ) 2UL / portTICK_RATE_MS )
\r
53 #define niMAX_TX_ATTEMPTS ( 5 )
\r
55 /*-----------------------------------------------------------*/
\r
58 * A deferred interrupt handler task that processes received frames.
\r
60 static void prvGMACDeferredInterruptHandlerTask( void *pvParameters );
\r
63 * Called by the ASF GMAC driver when a packet is received.
\r
65 static void prvGMACRxCallback( uint32_t ulStatus );
\r
67 /*-----------------------------------------------------------*/
\r
69 /* The queue used to communicate Ethernet events to the IP task. */
\r
70 extern xQueueHandle xNetworkEventQueue;
\r
72 /* The GMAC driver instance. */
\r
73 static gmac_device_t xGMACStruct;
\r
75 /* Handle of the task used to process MAC events. */
\r
76 static TaskHandle_t xMACEventHandlingTask = NULL;
\r
78 /*-----------------------------------------------------------*/
\r
80 BaseType_t xNetworkInterfaceInitialise( void )
\r
82 gmac_options_t xGMACOptions;
\r
83 extern uint8_t ucMACAddress[ 6 ];
\r
84 const TickType_t xPHYDelay_400ms = 400UL;
\r
85 BaseType_t xReturn = pdFALSE;
\r
87 /* Ensure PHY is ready. */
\r
88 vTaskDelay( xPHYDelay_400ms / portTICK_RATE_MS );
\r
90 /* Enable GMAC clock. */
\r
91 pmc_enable_periph_clk( ID_GMAC );
\r
93 /* Fill in GMAC options */
\r
94 xGMACOptions.uc_copy_all_frame = 0;
\r
95 xGMACOptions.uc_no_boardcast = 0;
\r
96 memcpy( xGMACOptions.uc_mac_addr, ucMACAddress, sizeof( ucMACAddress ) );
\r
98 xGMACStruct.p_hw = GMAC;
\r
100 /* Init GMAC driver structure. */
\r
101 gmac_dev_init( GMAC, &xGMACStruct, &xGMACOptions );
\r
103 /* Init MAC PHY driver. */
\r
104 if( ethernet_phy_init( GMAC, BOARD_GMAC_PHY_ADDR, sysclk_get_cpu_hz() ) == GMAC_OK )
\r
106 /* Auto Negotiate, work in RMII mode. */
\r
107 if( ethernet_phy_auto_negotiate( GMAC, BOARD_GMAC_PHY_ADDR ) == GMAC_OK )
\r
109 /* Establish Ethernet link. */
\r
110 vTaskDelay( xPHYDelay_400ms * 2UL );
\r
111 if( ethernet_phy_set_link( GMAC, BOARD_GMAC_PHY_ADDR, 1 ) == GMAC_OK )
\r
113 /* Register the callbacks. */
\r
114 gmac_dev_set_rx_callback( &xGMACStruct, prvGMACRxCallback );
\r
116 /* The Rx deferred interrupt handler task is created at the
\r
117 highest possible priority to ensure the interrupt handler can
\r
118 return directly to it no matter which task was running when the
\r
119 interrupt occurred. */
\r
120 xTaskCreate( prvGMACDeferredInterruptHandlerTask,/* The function that implements the task. */
\r
122 configMINIMAL_STACK_SIZE, /* Stack allocated to the task (defined in words, not bytes). */
\r
123 NULL, /* The task parameter is not used. */
\r
124 configMAX_PRIORITIES - 1, /* The priority assigned to the task. */
\r
125 &xMACEventHandlingTask ); /* The handle is stored so the ISR knows which task to notify. */
\r
127 /* Enable the interrupt and set its priority as configured.
\r
128 THIS DRIVER REQUIRES configMAC_INTERRUPT_PRIORITY TO BE DEFINED,
\r
129 PREFERABLY IN FreeRTOSConfig.h. */
\r
130 NVIC_SetPriority( GMAC_IRQn, configMAC_INTERRUPT_PRIORITY );
\r
131 NVIC_EnableIRQ( GMAC_IRQn );
\r
139 /*-----------------------------------------------------------*/
\r
141 static void prvGMACRxCallback( uint32_t ulStatus )
\r
143 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
145 configASSERT( xMACEventHandlingTask );
\r
147 /* Unblock the deferred interrupt handler task if the event was an Rx. */
\r
148 if( ( ulStatus & GMAC_RSR_REC ) != 0 )
\r
150 vTaskNotifyGiveFromISR( xMACEventHandlingTask, &xHigherPriorityTaskWoken );
\r
153 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
155 /*-----------------------------------------------------------*/
\r
157 BaseType_t xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
159 BaseType_t xReturn = pdFAIL;
\r
162 /* Attempt to obtain access to a Tx descriptor. */
\r
163 for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )
\r
165 if( gmac_dev_write( &xGMACStruct, pxNetworkBuffer->pucEthernetBuffer, ( uint32_t ) pxNetworkBuffer->xDataLength, NULL ) == GMAC_OK )
\r
167 /* The Tx has been initiated. */
\r
173 iptraceWAITING_FOR_TX_DMA_DESCRIPTOR();
\r
174 vTaskDelay( niTX_BUFFER_FREE_WAIT );
\r
178 /* Finished with the network buffer. */
\r
179 vNetworkBufferRelease( pxNetworkBuffer );
\r
183 /*-----------------------------------------------------------*/
\r
185 void GMAC_Handler( void )
\r
187 gmac_handler( &xGMACStruct );
\r
189 /*-----------------------------------------------------------*/
\r
191 static void prvGMACDeferredInterruptHandlerTask( void *pvParameters )
\r
193 xNetworkBufferDescriptor_t *pxNetworkBuffer = NULL;
\r
194 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };
\r
195 static const TickType_t xBufferWaitDelay = 1500UL / portTICK_RATE_MS;
\r
196 uint32_t ulReturned;
\r
198 /* This is a very simply but also inefficient implementation. */
\r
200 ( void ) pvParameters;
\r
204 /* Wait for the GMAC interrupt to indicate that another packet has been
\r
205 received. A while loop is used to process all received frames each time
\r
206 this task is notified, so it is ok to clear the notification count on the
\r
207 take (hence the first parameter is pdTRUE ). */
\r
208 ulTaskNotifyTake( pdTRUE, xBufferWaitDelay );
\r
210 ulReturned = GMAC_OK;
\r
211 while( ulReturned == GMAC_OK )
\r
213 /* Allocate a buffer to hold the data if one is not already held. */
\r
214 if( pxNetworkBuffer == NULL )
\r
216 pxNetworkBuffer = pxNetworkBufferGet( ipTOTAL_ETHERNET_FRAME_SIZE, xBufferWaitDelay );
\r
219 if( pxNetworkBuffer != NULL )
\r
221 /* Attempt to read data. */
\r
222 ulReturned = gmac_dev_read( &xGMACStruct, pxNetworkBuffer->pucEthernetBuffer, ipTOTAL_ETHERNET_FRAME_SIZE, ( uint32_t * ) &( pxNetworkBuffer->xDataLength ) );
\r
224 if( ulReturned == GMAC_OK )
\r
226 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 1
\r
228 if( pxNetworkBuffer->xDataLength > 0 )
\r
230 /* If the frame would not be processed by the IP
\r
231 stack then don't even bother sending it to the IP
\r
233 if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) != eProcessBuffer )
\r
235 pxNetworkBuffer->xDataLength = 0;
\r
241 if( pxNetworkBuffer->xDataLength > 0 )
\r
243 /* Store a pointer to the network buffer structure in
\r
244 the padding space that was left in front of the Ethernet
\r
245 frame. The pointer is needed to ensure the network
\r
246 buffer structure can be located when it is time for it
\r
247 to be freed if the Ethernet frame gets used as a zero
\r
249 *( ( xNetworkBufferDescriptor_t ** ) ( ( pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING ) ) ) = pxNetworkBuffer;
\r
251 /* Data was received and stored. Send it to the IP task
\r
253 xRxEvent.pvData = ( void * ) pxNetworkBuffer;
\r
254 if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( TickType_t ) 0 ) == pdFALSE )
\r
256 /* The buffer could not be sent to the IP task. The
\r
257 frame will be dropped and the buffer reused. */
\r
258 iptraceETHERNET_RX_EVENT_LOST();
\r
262 iptraceNETWORK_INTERFACE_RECEIVE();
\r
264 /* The buffer is not owned by the IP task - a new
\r
265 buffer is needed the next time around. */
\r
266 pxNetworkBuffer = NULL;
\r
271 /* The buffer does not contain any data so there is no
\r
272 point sending it to the IP task. Re-use the buffer on
\r
274 iptraceETHERNET_RX_EVENT_LOST();
\r
279 /* No data was received, keep the buffer for re-use. The
\r
280 loop will exit as ulReturn is not GMAC_OK. */
\r
285 /* Left a frame in the driver as a buffer was not available.
\r
286 Break out of loop. */
\r
287 ulReturned = GMAC_INVALID;
\r
292 /*-----------------------------------------------------------*/
\r