]> git.sur5r.net Git - freertos/blob - 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
1 /*\r
2  * FreeRTOS+UDP V1.0.4 (C) 2014 Real Time Engineers ltd.\r
3  * All rights reserved\r
4  *\r
5  * This file is part of the FreeRTOS+UDP distribution.  The FreeRTOS+UDP license\r
6  * terms are different to the FreeRTOS license terms.\r
7  *\r
8  * FreeRTOS+UDP uses a dual license model that allows the software to be used\r
9  * under a pure GPL open source license (as opposed to the modified GPL license\r
10  * under which FreeRTOS is distributed) or a commercial license.  Details of\r
11  * both license options follow:\r
12  *\r
13  * - Open source licensing -\r
14  * FreeRTOS+UDP is a free download and may be used, modified, evaluated and\r
15  * distributed without charge provided the user adheres to version two of the\r
16  * GNU General Public License (GPL) and does not remove the copyright notice or\r
17  * this text.  The GPL V2 text is available on the gnu.org web site, and on the\r
18  * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.\r
19  *\r
20  * - Commercial licensing -\r
21  * Businesses and individuals that for commercial or other reasons cannot comply\r
22  * with the terms of the GPL V2 license must obtain a commercial license before\r
23  * incorporating FreeRTOS+UDP into proprietary software for distribution in any\r
24  * form.  Commercial licenses can be purchased from http://shop.freertos.org/udp\r
25  * and do not require any source files to be changed.\r
26  *\r
27  * FreeRTOS+UDP is distributed in the hope that it will be useful.  You cannot\r
28  * use FreeRTOS+UDP unless you agree that you use the software 'as is'.\r
29  * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied\r
30  * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR\r
31  * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they\r
32  * implied, expressed, or statutory.\r
33  *\r
34  * 1 tab == 4 spaces!\r
35  *\r
36  * http://www.FreeRTOS.org\r
37  * http://www.FreeRTOS.org/udp\r
38  *\r
39  */\r
40 \r
41 /* Standard includes. */\r
42 #include <stdint.h>\r
43 #include <limits.h>\r
44 \r
45 /* FreeRTOS includes. */\r
46 #include "FreeRTOS.h"\r
47 #include "task.h"\r
48 #include "queue.h"\r
49 #include "semphr.h"\r
50 \r
51 /* FreeRTOS+UDP includes. */\r
52 #include "FreeRTOS_UDP_IP.h"\r
53 #include "FreeRTOS_IP_Private.h"\r
54 #include "FreeRTOS_Sockets.h"\r
55 #include "NetworkBufferManagement.h"\r
56 \r
57 /* Demo includes. */\r
58 #include "NetworkInterface.h"\r
59 \r
60 /* If a packet cannot be sent immediately then the task performing the send\r
61 operation will be held in the Blocked state (so other tasks can execute) for\r
62 niTX_BUFFER_FREE_WAIT ticks.  It will do this a maximum of niMAX_TX_ATTEMPTS\r
63 before giving up. */\r
64 #define niTX_BUFFER_FREE_WAIT   ( ( TickType_t ) 2UL / portTICK_RATE_MS )\r
65 #define niMAX_TX_ATTEMPTS               ( 5 )\r
66 \r
67 /*-----------------------------------------------------------*/\r
68 \r
69 /*\r
70  * A deferred interrupt handler task that processes received frames.\r
71  */\r
72 static void prvGMACDeferredInterruptHandlerTask( void *pvParameters );\r
73 \r
74 /*\r
75  * Called by the ASF GMAC driver when a packet is received.\r
76  */\r
77 static void prvGMACRxCallback( uint32_t ulStatus );\r
78 \r
79 /*-----------------------------------------------------------*/\r
80 \r
81 /* The queue used to communicate Ethernet events to the IP task. */\r
82 extern xQueueHandle xNetworkEventQueue;\r
83 \r
84 /* The GMAC driver instance. */\r
85 static gmac_device_t xGMACStruct;\r
86 \r
87 /* Handle of the task used to process MAC events. */\r
88 static TaskHandle_t xMACEventHandlingTask = NULL;\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 BaseType_t xNetworkInterfaceInitialise( void )\r
93 {\r
94 gmac_options_t xGMACOptions;\r
95 extern uint8_t ucMACAddress[ 6 ];\r
96 const TickType_t xPHYDelay_400ms = 400UL;\r
97 BaseType_t xReturn = pdFALSE;\r
98 \r
99         /* Ensure PHY is ready. */\r
100         vTaskDelay( xPHYDelay_400ms / portTICK_RATE_MS );\r
101 \r
102         /* Enable GMAC clock. */\r
103         pmc_enable_periph_clk( ID_GMAC );\r
104 \r
105         /* Fill in GMAC options */\r
106         xGMACOptions.uc_copy_all_frame = 0;\r
107         xGMACOptions.uc_no_boardcast = 0;\r
108         memcpy( xGMACOptions.uc_mac_addr, ucMACAddress, sizeof( ucMACAddress ) );\r
109 \r
110         xGMACStruct.p_hw = GMAC;\r
111 \r
112         /* Init GMAC driver structure. */\r
113         gmac_dev_init( GMAC, &xGMACStruct, &xGMACOptions );\r
114 \r
115         /* Init MAC PHY driver. */\r
116         if( ethernet_phy_init( GMAC, BOARD_GMAC_PHY_ADDR, sysclk_get_cpu_hz() ) == GMAC_OK )\r
117         {\r
118                 /* Auto Negotiate, work in RMII mode. */\r
119                 if( ethernet_phy_auto_negotiate( GMAC, BOARD_GMAC_PHY_ADDR ) == GMAC_OK )\r
120                 {\r
121                         /* Establish Ethernet link. */\r
122                         vTaskDelay( xPHYDelay_400ms * 2UL );\r
123                         if( ethernet_phy_set_link( GMAC, BOARD_GMAC_PHY_ADDR, 1 ) == GMAC_OK )\r
124                         {\r
125                                 /* Register the callbacks. */\r
126                                 gmac_dev_set_rx_callback( &xGMACStruct, prvGMACRxCallback );\r
127 \r
128                                 /* The Rx deferred interrupt handler task is created at the\r
129                                 highest possible priority to ensure the interrupt handler can\r
130                                 return directly to it no matter which task was running when the\r
131                                 interrupt occurred. */\r
132                                 xTaskCreate(    prvGMACDeferredInterruptHandlerTask,/* The function that implements the task. */\r
133                                                                 "MACTsk",\r
134                                                                 configMINIMAL_STACK_SIZE,       /* Stack allocated to the task (defined in words, not bytes). */\r
135                                                                 NULL,                                           /* The task parameter is not used. */\r
136                                                                 configMAX_PRIORITIES - 1,       /* The priority assigned to the task. */\r
137                                                                 &xMACEventHandlingTask );       /* The handle is stored so the ISR knows which task to notify. */\r
138 \r
139                                 /* Enable the interrupt and set its priority as configured.\r
140                                 THIS DRIVER REQUIRES configMAC_INTERRUPT_PRIORITY TO BE DEFINED,\r
141                                 PREFERABLY IN FreeRTOSConfig.h. */\r
142                                 NVIC_SetPriority( GMAC_IRQn, configMAC_INTERRUPT_PRIORITY );\r
143                                 NVIC_EnableIRQ( GMAC_IRQn );\r
144                                 xReturn = pdPASS;\r
145                         }\r
146                 }\r
147         }\r
148 \r
149         return xReturn;\r
150 }\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 static void prvGMACRxCallback( uint32_t ulStatus )\r
154 {\r
155 BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
156 \r
157         configASSERT( xMACEventHandlingTask );\r
158 \r
159         /* Unblock the deferred interrupt handler task if the event was an Rx. */\r
160         if( ( ulStatus & GMAC_RSR_REC ) != 0 )\r
161         {\r
162                 vTaskNotifyGiveFromISR( xMACEventHandlingTask, &xHigherPriorityTaskWoken );\r
163         }\r
164 \r
165         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 BaseType_t xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
170 {\r
171 BaseType_t xReturn = pdFAIL;\r
172 int32_t x;\r
173 \r
174         /* Attempt to obtain access to a Tx descriptor. */\r
175         for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )\r
176         {\r
177                 if( gmac_dev_write( &xGMACStruct, pxNetworkBuffer->pucEthernetBuffer, ( uint32_t ) pxNetworkBuffer->xDataLength, NULL ) == GMAC_OK )\r
178                 {\r
179                         /* The Tx has been initiated. */\r
180                         xReturn = pdPASS;\r
181                         break;\r
182                 }\r
183                 else\r
184                 {\r
185                         iptraceWAITING_FOR_TX_DMA_DESCRIPTOR();\r
186                         vTaskDelay( niTX_BUFFER_FREE_WAIT );\r
187                 }\r
188         }\r
189 \r
190         /* Finished with the network buffer. */\r
191         vNetworkBufferRelease( pxNetworkBuffer );\r
192 \r
193         return xReturn;\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 void GMAC_Handler( void )\r
198 {\r
199         gmac_handler( &xGMACStruct );\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 static void prvGMACDeferredInterruptHandlerTask( void *pvParameters )\r
204 {\r
205 xNetworkBufferDescriptor_t *pxNetworkBuffer = NULL;\r
206 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };\r
207 static const TickType_t xBufferWaitDelay = 1500UL / portTICK_RATE_MS;\r
208 uint32_t ulReturned;\r
209 \r
210         /* This is a very simply but also inefficient implementation. */\r
211 \r
212         ( void ) pvParameters;\r
213 \r
214         for( ;; )\r
215         {\r
216                 /* Wait for the GMAC interrupt to indicate that another packet has been\r
217                 received.  A while loop is used to process all received frames each time\r
218                 this task is notified, so it is ok to clear the notification count on the\r
219                 take (hence the first parameter is pdTRUE ). */\r
220                 ulTaskNotifyTake( pdTRUE, xBufferWaitDelay );\r
221 \r
222                 ulReturned = GMAC_OK;\r
223                 while( ulReturned == GMAC_OK )\r
224                 {\r
225                         /* Allocate a buffer to hold the data if one is not already held. */\r
226                         if( pxNetworkBuffer == NULL )\r
227                         {\r
228                                 pxNetworkBuffer = pxNetworkBufferGet( ipTOTAL_ETHERNET_FRAME_SIZE, xBufferWaitDelay );\r
229                         }\r
230 \r
231                         if( pxNetworkBuffer != NULL )\r
232                         {\r
233                                 /* Attempt to read data. */\r
234                                 ulReturned = gmac_dev_read( &xGMACStruct, pxNetworkBuffer->pucEthernetBuffer, ipTOTAL_ETHERNET_FRAME_SIZE, ( uint32_t * ) &( pxNetworkBuffer->xDataLength ) );\r
235 \r
236                                 if( ulReturned == GMAC_OK )\r
237                                 {\r
238                                         #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 1\r
239                                         {\r
240                                                 if( pxNetworkBuffer->xDataLength > 0 )\r
241                                                 {\r
242                                                         /* If the frame would not be processed by the IP\r
243                                                         stack then don't even bother sending it to the IP\r
244                                                         stack. */\r
245                                                         if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) != eProcessBuffer )\r
246                                                         {\r
247                                                                 pxNetworkBuffer->xDataLength = 0;\r
248                                                         }\r
249                                                 }\r
250                                         }\r
251                                         #endif\r
252 \r
253                                         if( pxNetworkBuffer->xDataLength > 0 )\r
254                                         {\r
255                                                 /* Store a pointer to the network buffer structure in\r
256                                                 the     padding space that was left in front of the Ethernet\r
257                                                 frame.  The pointer is needed to ensure the network\r
258                                                 buffer structure can be located when it is time for it\r
259                                                 to be freed if the Ethernet frame gets used as a zero\r
260                                                 copy buffer. */\r
261                                                 *( ( xNetworkBufferDescriptor_t ** ) ( ( pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING ) ) ) = pxNetworkBuffer;\r
262 \r
263                                                 /* Data was received and stored.  Send it to the IP task\r
264                                                 for processing. */\r
265                                                 xRxEvent.pvData = ( void * ) pxNetworkBuffer;\r
266                                                 if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( TickType_t ) 0 ) == pdFALSE )\r
267                                                 {\r
268                                                         /* The buffer could not be sent to the IP task. The\r
269                                                         frame will be dropped and the buffer reused. */\r
270                                                         iptraceETHERNET_RX_EVENT_LOST();\r
271                                                 }\r
272                                                 else\r
273                                                 {\r
274                                                         iptraceNETWORK_INTERFACE_RECEIVE();\r
275 \r
276                                                         /* The buffer is not owned by the IP task - a new\r
277                                                         buffer is needed the next time around. */\r
278                                                         pxNetworkBuffer = NULL;\r
279                                                 }\r
280                                         }\r
281                                         else\r
282                                         {\r
283                                                 /* The buffer does not contain any data so there is no\r
284                                                 point sending it to the IP task.  Re-use the buffer on\r
285                                                 the next loop. */\r
286                                                 iptraceETHERNET_RX_EVENT_LOST();\r
287                                         }\r
288                                 }\r
289                                 else\r
290                                 {\r
291                                         /* No data was received, keep the buffer for re-use.  The\r
292                                         loop will exit as ulReturn is not GMAC_OK. */\r
293                                 }\r
294                         }\r
295                         else\r
296                         {\r
297                                 /* Left a frame in the driver as a buffer was not available.\r
298                                 Break out of loop. */\r
299                                 ulReturned = GMAC_INVALID;\r
300                         }\r
301                 }\r
302         }\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r