]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/portable/NetworkInterface/SAM4E/NetworkInterface.c
Add basic SAM4E driver.
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-UDP / portable / NetworkInterface / SAM4E / NetworkInterface.c
1 /*\r
2  * FreeRTOS+UDP V1.0.2 (C) 2013 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 \r
44 /* FreeRTOS includes. */\r
45 #include "FreeRTOS.h"\r
46 #include "task.h"\r
47 #include "queue.h"\r
48 #include "semphr.h"\r
49 \r
50 /* FreeRTOS+UDP includes. */\r
51 #include "FreeRTOS_UDP_IP.h"\r
52 #include "FreeRTOS_IP_Private.h"\r
53 #include "FreeRTOS_Sockets.h"\r
54 #include "NetworkBufferManagement.h"\r
55 \r
56 /* Demo includes. */\r
57 #include "NetworkInterface.h"\r
58 \r
59 /* If a packet cannot be sent immediately then the task performing the send\r
60 operation will be held in the Blocked state (so other tasks can execute) for\r
61 niTX_BUFFER_FREE_WAIT ticks.  It will do this a maximum of niMAX_TX_ATTEMPTS\r
62 before giving up. */\r
63 #define niTX_BUFFER_FREE_WAIT   ( ( portTickType ) 2UL / portTICK_RATE_MS )\r
64 #define niMAX_TX_ATTEMPTS               ( 5 )\r
65 \r
66 /*-----------------------------------------------------------*/\r
67 \r
68 /*\r
69  * A deferred interrupt handler task that processes received frames.\r
70  */\r
71 static void prvGMACDeferredInterruptHandlerTask( void *pvParameters );\r
72 \r
73 /*\r
74  * Called by the ASF GMAC driver when a packet is received.\r
75  */\r
76 static void prvGMACRxCallback( uint32_t ulStatus );\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 /* The queue used to communicate Ethernet events to the IP task. */\r
81 extern xQueueHandle xNetworkEventQueue;\r
82 \r
83 /* The semaphore used to wake the deferred interrupt handler task when an Rx\r
84 interrupt is received. */\r
85 static xSemaphoreHandle xGMACRxEventSemaphore = NULL;\r
86 \r
87 /* The GMAC driver instance. */\r
88 static gmac_device_t xGMACStruct;\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 portBASE_TYPE xNetworkInterfaceInitialise( void )\r
93 {\r
94 gmac_options_t xGMACOptions;\r
95 extern uint8_t ucMACAddress[ 6 ];\r
96 const portTickType xPHYDelay_400ms = 400UL;\r
97 portBASE_TYPE 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                                 /* Create the event semaphore if it has not already been \r
126                                 created. */\r
127                                 if( xGMACRxEventSemaphore == NULL )\r
128                                 {\r
129                                         vSemaphoreCreateBinary( xGMACRxEventSemaphore );\r
130                                         #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1\r
131                                         {\r
132                                                 /* If the trace recorder code is included name the semaphore for\r
133                                                 viewing in FreeRTOS+Trace. */\r
134                                                 vTraceSetQueueName( xGMACRxEventSemaphore, "MAC_RX" );\r
135                                         }\r
136                                         #endif /*  ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */\r
137                                 }\r
138                                 \r
139                                 /* Register the callbacks. */\r
140                                 gmac_dev_set_rx_callback( &xGMACStruct, prvGMACRxCallback );\r
141                                 \r
142                                 /* The Rx deferred interrupt handler task is created at the \r
143                                 highest possible priority to ensure the interrupt handler can \r
144                                 return directly to it no matter which task was running when the \r
145                                 interrupt occurred. */\r
146                                 xTaskCreate(    prvGMACDeferredInterruptHandlerTask,/* The function that implements the task. */\r
147                                                                 ( const signed char * const ) "MACTsk",\r
148                                                                 configMINIMAL_STACK_SIZE,       /* Stack allocated to the task (defined in words, not bytes). */\r
149                                                                 NULL,                                           /* The task parameter is not used. */\r
150                                                                 configMAX_PRIORITIES - 1,       /* The priority assigned to the task. */\r
151                                                                 NULL );                                         /* The handle is not required, so NULL is passed. */\r
152                                 \r
153                                 /* Enable the interrupt and set its priority as configured.  \r
154                                 THIS DRIVER REQUIRES configMAC_INTERRUPT_PRIORITY TO BE DEFINED, \r
155                                 PREFERABLY IN FreeRTOSConfig.h. */\r
156                                 NVIC_SetPriority( GMAC_IRQn, configMAC_INTERRUPT_PRIORITY );\r
157                                 NVIC_EnableIRQ( GMAC_IRQn );\r
158                                 xReturn = pdPASS;\r
159                         }\r
160                 }\r
161         }\r
162 \r
163         return xReturn;\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 static void prvGMACRxCallback( uint32_t ulStatus )\r
168 {\r
169 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
170 \r
171         /* Unblock the deferred interrupt handler task if the event was an Rx. */\r
172         if( ulStatus != 0 )\r
173         {\r
174                 xSemaphoreGiveFromISR( xGMACRxEventSemaphore, &xHigherPriorityTaskWoken );\r
175         }\r
176 \r
177         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 portBASE_TYPE xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
182 {\r
183 portBASE_TYPE xReturn = pdFAIL;\r
184 int32_t x;\r
185 \r
186         /* Attempt to obtain access to a Tx descriptor. */\r
187         for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )\r
188         {\r
189                 if( gmac_dev_write( &xGMACStruct, pxNetworkBuffer->pucEthernetBuffer, ( uint32_t ) pxNetworkBuffer->xDataLength, NULL ) == GMAC_OK )\r
190                 {\r
191                         /* The Tx has been initiated. */\r
192                         xReturn = pdPASS;\r
193                         break;\r
194                 }\r
195                 else\r
196                 {\r
197                         iptraceWAITING_FOR_TX_DMA_DESCRIPTOR();\r
198                         vTaskDelay( niTX_BUFFER_FREE_WAIT );\r
199                 }\r
200         }\r
201 \r
202         /* Finished with the network buffer. */\r
203         vNetworkBufferRelease( pxNetworkBuffer );\r
204 \r
205         return xReturn;\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 void GMAC_Handler( void )\r
210 {\r
211         gmac_handler( &xGMACStruct );\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 static void prvGMACDeferredInterruptHandlerTask( void *pvParameters )\r
216 {\r
217 xNetworkBufferDescriptor_t *pxNetworkBuffer;\r
218 xIPStackEvent_t xRxEvent = { eEthernetRxEvent, NULL };\r
219 \r
220         ( void ) pvParameters;\r
221         configASSERT( xGMACRxEventSemaphore );\r
222 \r
223         for( ;; )\r
224         {\r
225                 /* Wait for the GMAC interrupt to indicate that another packet has been\r
226                 received.  The while() loop is only needed if INCLUDE_vTaskSuspend is\r
227                 set to 0 in FreeRTOSConfig.h.  If INCLUDE_vTaskSuspend is set to 1\r
228                 then portMAX_DELAY would be an indefinite block time and\r
229                 xSemaphoreTake() would only return when the semaphore was actually\r
230                 obtained. */\r
231                 while( xSemaphoreTake( xGMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );\r
232 \r
233                 /* The buffer filled by the DMA is going to be passed into the IP\r
234                 stack.  Allocate another buffer for the DMA descriptor. */\r
235                 pxNetworkBuffer = pxNetworkBufferGet( ipTOTAL_ETHERNET_FRAME_SIZE, portMAX_DELAY );\r
236                 \r
237                 if( pxNetworkBuffer != NULL )\r
238                 {\r
239                         /* At least one packet has been received. */\r
240                         if( gmac_dev_read( &xGMACStruct, pxNetworkBuffer->pucEthernetBuffer, ipTOTAL_ETHERNET_FRAME_SIZE, ( uint32_t * ) &( pxNetworkBuffer->xDataLength ) ) == GMAC_OK )\r
241                         {\r
242                                 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 1\r
243                                 {\r
244                                         if( pxNetworkBuffer->xDataLength > 0 )\r
245                                         {\r
246                                                 /* If the frame would not be processed by the IP stack then\r
247                                                 don't even bother sending it to the IP stack. */\r
248                                                 if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) != eProcessBuffer )\r
249                                                 {\r
250                                                         pxNetworkBuffer->xDataLength = 0;\r
251                                                 }\r
252                                         }\r
253                                 }\r
254                                 #endif\r
255 \r
256                                 if( pxNetworkBuffer->xDataLength > 0 )\r
257                                 {\r
258                                         /* Store a pointer to the network buffer structure in the\r
259                                         padding space that was left in front of the Ethernet frame.\r
260                                         The pointer     is needed to ensure the network buffer structure\r
261                                         can be located when it is time for it to be freed if the\r
262                                         Ethernet frame gets     used as a zero copy buffer. */\r
263                                         *( ( xNetworkBufferDescriptor_t ** ) ( ( pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING ) ) ) = pxNetworkBuffer;\r
264 \r
265                                         /* Data was received and stored.  Send it to the IP task\r
266                                         for processing. */\r
267                                         xRxEvent.pvData = ( void * ) pxNetworkBuffer;\r
268                                         if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( portTickType ) 0 ) == pdFALSE )\r
269                                         {\r
270                                                 /* The buffer could not be sent to the IP task so the\r
271                                                 buffer must be released. */\r
272                                                 vNetworkBufferRelease( pxNetworkBuffer );\r
273                                                 iptraceETHERNET_RX_EVENT_LOST();\r
274                                         }\r
275                                         else\r
276                                         {\r
277                                                 iptraceNETWORK_INTERFACE_RECEIVE();\r
278                                         }\r
279                                 }\r
280                                 else\r
281                                 {\r
282                                         /* The buffer does not contain any data so there is no\r
283                                         point sending it to the IP task.  Just release it. */\r
284                                         vNetworkBufferRelease( pxNetworkBuffer );\r
285                                         iptraceETHERNET_RX_EVENT_LOST();\r
286                                 }\r
287                         }\r
288                         else\r
289                         {\r
290                                 iptraceETHERNET_RX_EVENT_LOST();\r
291                         }\r
292                 }\r
293         }\r
294 }\r
295 /*-----------------------------------------------------------*/\r
296 \r