]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/portable/NetworkInterface/SAM4E/NetworkInterface.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-UDP / portable / NetworkInterface / SAM4E / NetworkInterface.c
1 /*\r
2  * FreeRTOS+UDP V1.0.4\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
15  *\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
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* Standard includes. */\r
30 #include <stdint.h>\r
31 #include <limits.h>\r
32 \r
33 /* FreeRTOS includes. */\r
34 #include "FreeRTOS.h"\r
35 #include "task.h"\r
36 #include "queue.h"\r
37 #include "semphr.h"\r
38 \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
44 \r
45 /* Demo includes. */\r
46 #include "NetworkInterface.h"\r
47 \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
54 \r
55 /*-----------------------------------------------------------*/\r
56 \r
57 /*\r
58  * A deferred interrupt handler task that processes received frames.\r
59  */\r
60 static void prvGMACDeferredInterruptHandlerTask( void *pvParameters );\r
61 \r
62 /*\r
63  * Called by the ASF GMAC driver when a packet is received.\r
64  */\r
65 static void prvGMACRxCallback( uint32_t ulStatus );\r
66 \r
67 /*-----------------------------------------------------------*/\r
68 \r
69 /* The queue used to communicate Ethernet events to the IP task. */\r
70 extern xQueueHandle xNetworkEventQueue;\r
71 \r
72 /* The GMAC driver instance. */\r
73 static gmac_device_t xGMACStruct;\r
74 \r
75 /* Handle of the task used to process MAC events. */\r
76 static TaskHandle_t xMACEventHandlingTask = NULL;\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 BaseType_t xNetworkInterfaceInitialise( void )\r
81 {\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
86 \r
87         /* Ensure PHY is ready. */\r
88         vTaskDelay( xPHYDelay_400ms / portTICK_RATE_MS );\r
89 \r
90         /* Enable GMAC clock. */\r
91         pmc_enable_periph_clk( ID_GMAC );\r
92 \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
97 \r
98         xGMACStruct.p_hw = GMAC;\r
99 \r
100         /* Init GMAC driver structure. */\r
101         gmac_dev_init( GMAC, &xGMACStruct, &xGMACOptions );\r
102 \r
103         /* Init MAC PHY driver. */\r
104         if( ethernet_phy_init( GMAC, BOARD_GMAC_PHY_ADDR, sysclk_get_cpu_hz() ) == GMAC_OK )\r
105         {\r
106                 /* Auto Negotiate, work in RMII mode. */\r
107                 if( ethernet_phy_auto_negotiate( GMAC, BOARD_GMAC_PHY_ADDR ) == GMAC_OK )\r
108                 {\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
112                         {\r
113                                 /* Register the callbacks. */\r
114                                 gmac_dev_set_rx_callback( &xGMACStruct, prvGMACRxCallback );\r
115 \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
121                                                                 "MACTsk",\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
126 \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
132                                 xReturn = pdPASS;\r
133                         }\r
134                 }\r
135         }\r
136 \r
137         return xReturn;\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 static void prvGMACRxCallback( uint32_t ulStatus )\r
142 {\r
143 BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
144 \r
145         configASSERT( xMACEventHandlingTask );\r
146 \r
147         /* Unblock the deferred interrupt handler task if the event was an Rx. */\r
148         if( ( ulStatus & GMAC_RSR_REC ) != 0 )\r
149         {\r
150                 vTaskNotifyGiveFromISR( xMACEventHandlingTask, &xHigherPriorityTaskWoken );\r
151         }\r
152 \r
153         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
154 }\r
155 /*-----------------------------------------------------------*/\r
156 \r
157 BaseType_t xNetworkInterfaceOutput( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
158 {\r
159 BaseType_t xReturn = pdFAIL;\r
160 int32_t x;\r
161 \r
162         /* Attempt to obtain access to a Tx descriptor. */\r
163         for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )\r
164         {\r
165                 if( gmac_dev_write( &xGMACStruct, pxNetworkBuffer->pucEthernetBuffer, ( uint32_t ) pxNetworkBuffer->xDataLength, NULL ) == GMAC_OK )\r
166                 {\r
167                         /* The Tx has been initiated. */\r
168                         xReturn = pdPASS;\r
169                         break;\r
170                 }\r
171                 else\r
172                 {\r
173                         iptraceWAITING_FOR_TX_DMA_DESCRIPTOR();\r
174                         vTaskDelay( niTX_BUFFER_FREE_WAIT );\r
175                 }\r
176         }\r
177 \r
178         /* Finished with the network buffer. */\r
179         vNetworkBufferRelease( pxNetworkBuffer );\r
180 \r
181         return xReturn;\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 void GMAC_Handler( void )\r
186 {\r
187         gmac_handler( &xGMACStruct );\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 static void prvGMACDeferredInterruptHandlerTask( void *pvParameters )\r
192 {\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
197 \r
198         /* This is a very simply but also inefficient implementation. */\r
199 \r
200         ( void ) pvParameters;\r
201 \r
202         for( ;; )\r
203         {\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
209 \r
210                 ulReturned = GMAC_OK;\r
211                 while( ulReturned == GMAC_OK )\r
212                 {\r
213                         /* Allocate a buffer to hold the data if one is not already held. */\r
214                         if( pxNetworkBuffer == NULL )\r
215                         {\r
216                                 pxNetworkBuffer = pxNetworkBufferGet( ipTOTAL_ETHERNET_FRAME_SIZE, xBufferWaitDelay );\r
217                         }\r
218 \r
219                         if( pxNetworkBuffer != NULL )\r
220                         {\r
221                                 /* Attempt to read data. */\r
222                                 ulReturned = gmac_dev_read( &xGMACStruct, pxNetworkBuffer->pucEthernetBuffer, ipTOTAL_ETHERNET_FRAME_SIZE, ( uint32_t * ) &( pxNetworkBuffer->xDataLength ) );\r
223 \r
224                                 if( ulReturned == GMAC_OK )\r
225                                 {\r
226                                         #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 1\r
227                                         {\r
228                                                 if( pxNetworkBuffer->xDataLength > 0 )\r
229                                                 {\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
232                                                         stack. */\r
233                                                         if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) != eProcessBuffer )\r
234                                                         {\r
235                                                                 pxNetworkBuffer->xDataLength = 0;\r
236                                                         }\r
237                                                 }\r
238                                         }\r
239                                         #endif\r
240 \r
241                                         if( pxNetworkBuffer->xDataLength > 0 )\r
242                                         {\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
248                                                 copy buffer. */\r
249                                                 *( ( xNetworkBufferDescriptor_t ** ) ( ( pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING ) ) ) = pxNetworkBuffer;\r
250 \r
251                                                 /* Data was received and stored.  Send it to the IP task\r
252                                                 for processing. */\r
253                                                 xRxEvent.pvData = ( void * ) pxNetworkBuffer;\r
254                                                 if( xQueueSendToBack( xNetworkEventQueue, &xRxEvent, ( TickType_t ) 0 ) == pdFALSE )\r
255                                                 {\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
259                                                 }\r
260                                                 else\r
261                                                 {\r
262                                                         iptraceNETWORK_INTERFACE_RECEIVE();\r
263 \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
267                                                 }\r
268                                         }\r
269                                         else\r
270                                         {\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
273                                                 the next loop. */\r
274                                                 iptraceETHERNET_RX_EVENT_LOST();\r
275                                         }\r
276                                 }\r
277                                 else\r
278                                 {\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
281                                 }\r
282                         }\r
283                         else\r
284                         {\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
288                         }\r
289                 }\r
290         }\r
291 }\r
292 /*-----------------------------------------------------------*/\r
293 \r