]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/portable/NetworkInterface/LPC17xx/NetworkInterface.c
7cee711cd3dc89132c114b5554820808640bd1f4
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / portable / NetworkInterface / LPC17xx / NetworkInterface.c
1 /*
2 FreeRTOS+TCP V2.0.11
3 Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy of
6 this software and associated documentation files (the "Software"), to deal in
7 the Software without restriction, including without limitation the rights to
8 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 the Software, and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in all
13 copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22  http://aws.amazon.com/freertos
23  http://www.FreeRTOS.org
24 */
25
26 /* Standard includes. */
27 #include <stdint.h>
28
29 /* FreeRTOS includes. */
30 #include "FreeRTOS.h"
31 #include "task.h"
32 #include "queue.h"
33 #include "semphr.h"
34
35 /* Hardware abstraction. */
36 #include "FreeRTOS_IO.h"
37
38 /* FreeRTOS+TCP includes. */
39 #include "FreeRTOS_UDP_IP.h"
40 #include "FreeRTOS_Sockets.h"
41 #include "NetworkBufferManagement.h"
42
43 /* Driver includes. */
44 #include "lpc17xx_emac.h"
45 #include "lpc17xx_pinsel.h"
46
47 /* Demo includes. */
48 #include "NetworkInterface.h"
49
50 #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES != 1
51         #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer
52 #else
53         #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )
54 #endif
55
56 /* When a packet is ready to be sent, if it cannot be sent immediately then the
57 task performing the transmit will block for niTX_BUFFER_FREE_WAIT
58 milliseconds.  It will do this a maximum of niMAX_TX_ATTEMPTS before giving
59 up. */
60 #define niTX_BUFFER_FREE_WAIT   ( pdMS_TO_TICKS( 2UL ) )
61 #define niMAX_TX_ATTEMPTS               ( 5 )
62
63 /* The length of the queue used to send interrupt status words from the
64 interrupt handler to the deferred handler task. */
65 #define niINTERRUPT_QUEUE_LENGTH        ( 10 )
66
67 /*-----------------------------------------------------------*/
68
69 /*
70  * A deferred interrupt handler task that processes
71  */
72 static void prvEMACHandlerTask( void *pvParameters );
73
74 /*-----------------------------------------------------------*/
75
76 /* The queue used to communicate Ethernet events with the IP task. */
77 extern QueueHandle_t xNetworkEventQueue;
78
79 /* The semaphore used to wake the deferred interrupt handler task when an Rx
80 interrupt is received. */
81 static SemaphoreHandle_t xEMACRxEventSemaphore = NULL;
82 /*-----------------------------------------------------------*/
83
84 BaseType_t xNetworkInterfaceInitialise( void )
85 {
86 EMAC_CFG_Type Emac_Config;
87 PINSEL_CFG_Type xPinConfig;
88 BaseType_t xStatus, xReturn;
89 extern uint8_t ucMACAddress[ 6 ];
90
91         /* Enable Ethernet Pins */
92         boardCONFIGURE_ENET_PINS( xPinConfig );
93
94         Emac_Config.Mode = EMAC_MODE_AUTO;
95         Emac_Config.pbEMAC_Addr = ucMACAddress;
96         xStatus = EMAC_Init( &Emac_Config );
97
98         LPC_EMAC->IntEnable &= ~( EMAC_INT_TX_DONE );
99
100         if( xStatus != ERROR )
101         {
102                 vSemaphoreCreateBinary( xEMACRxEventSemaphore );
103                 configASSERT( xEMACRxEventSemaphore );
104
105                 /* The handler task is created at the highest possible priority to
106                 ensure the interrupt handler can return directly to it. */
107                 xTaskCreate( prvEMACHandlerTask, "EMAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
108
109                 /* Enable the interrupt and set its priority to the minimum
110                 interrupt priority.  */
111                 NVIC_SetPriority( ENET_IRQn, configMAC_INTERRUPT_PRIORITY );
112                 NVIC_EnableIRQ( ENET_IRQn );
113
114                 xReturn = pdPASS;
115         }
116         else
117         {
118                 xReturn = pdFAIL;
119         }
120
121         configASSERT( xStatus != ERROR );
122
123         return xReturn;
124 }
125 /*-----------------------------------------------------------*/
126
127 BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxNetworkBuffer )
128 {
129 BaseType_t xReturn = pdFAIL;
130 int32_t x;
131 extern void EMAC_StartTransmitNextBuffer( uint32_t ulLength );
132 extern void EMAC_SetNextPacketToSend( uint8_t * pucBuffer );
133
134
135         /* Attempt to obtain access to a Tx buffer. */
136         for( x = 0; x < niMAX_TX_ATTEMPTS; x++ )
137         {
138                 if( EMAC_CheckTransmitIndex() == TRUE )
139                 {
140                         /* Will the data fit in the Tx buffer? */
141                         if( pxNetworkBuffer->xDataLength < EMAC_ETH_MAX_FLEN ) /*_RB_ The size needs to come from FreeRTOSIPConfig.h. */
142                         {
143                                 /* Assign the buffer to the Tx descriptor that is now known to
144                                 be free. */
145                                 EMAC_SetNextPacketToSend( pxNetworkBuffer->pucBuffer );
146
147                                 /* The EMAC now owns the buffer. */
148                                 pxNetworkBuffer->pucBuffer = NULL;
149
150                                 /* Initiate the Tx. */
151                                 EMAC_StartTransmitNextBuffer( pxNetworkBuffer->xDataLength );
152                                 iptraceNETWORK_INTERFACE_TRANSMIT();
153
154                                 /* The Tx has been initiated. */
155                                 xReturn = pdPASS;
156                         }
157                         break;
158                 }
159                 else
160                 {
161                         vTaskDelay( niTX_BUFFER_FREE_WAIT );
162                 }
163         }
164
165         /* Finished with the network buffer. */
166         vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
167
168         return xReturn;
169 }
170 /*-----------------------------------------------------------*/
171
172 void ENET_IRQHandler( void )
173 {
174 uint32_t ulInterruptCause;
175
176         while( ( ulInterruptCause = LPC_EMAC->IntStatus ) != 0 )
177         {
178                 /* Clear the interrupt. */
179                 LPC_EMAC->IntClear = ulInterruptCause;
180
181                 /* Clear fatal error conditions.  NOTE:  The driver does not clear all
182                 errors, only those actually experienced.  For future reference, range
183                 errors are not actually errors so can be ignored. */
184                 if( ( ulInterruptCause & EMAC_INT_TX_UNDERRUN ) != 0U )
185                 {
186                         LPC_EMAC->Command |= EMAC_CR_TX_RES;
187                 }
188
189                 /* Unblock the deferred interrupt handler task if the event was an
190                 Rx. */
191                 if( ( ulInterruptCause & EMAC_INT_RX_DONE ) != 0UL )
192                 {
193                         xSemaphoreGiveFromISR( xEMACRxEventSemaphore, NULL );
194                 }
195         }
196
197         /* ulInterruptCause is used for convenience here.  A context switch is
198         wanted, but coding portEND_SWITCHING_ISR( 1 ) would likely result in a
199         compiler warning. */
200         portEND_SWITCHING_ISR( ulInterruptCause );
201 }
202 /*-----------------------------------------------------------*/
203
204 static void prvEMACHandlerTask( void *pvParameters )
205 {
206 size_t xDataLength;
207 const uint16_t usCRCLength = 4;
208 NetworkBufferDescriptor_t *pxNetworkBuffer;
209 IPStackEvent_t xRxEvent = { eNetworkRxEvent, NULL };
210
211 /* This is not included in the header file for some reason. */
212 extern uint8_t *EMAC_NextPacketToRead( void );
213
214         ( void ) pvParameters;
215         configASSERT( xEMACRxEventSemaphore );
216
217         for( ;; )
218         {
219                 /* Wait for the EMAC interrupt to indicate that another packet has been
220                 received.  The while() loop is only needed if INCLUDE_vTaskSuspend is
221                 set to 0 in FreeRTOSConfig.h. */
222                 while( xSemaphoreTake( xEMACRxEventSemaphore, portMAX_DELAY ) == pdFALSE );
223
224                 /* At least one packet has been received. */
225                 while( EMAC_CheckReceiveIndex() != FALSE )
226                 {
227                         /* Obtain the length, minus the CRC.  The CRC is four bytes
228                         but the length is already minus 1. */
229                         xDataLength = ( size_t ) EMAC_GetReceiveDataSize() - ( usCRCLength - 1U );
230
231                         if( xDataLength > 0U )
232                         {
233                                 /* Obtain a network buffer to pass this data into the
234                                 stack.  No storage is required as the network buffer
235                                 will point directly to the buffer that already holds
236                                 the     received data. */
237                                 pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( 0, ( TickType_t ) 0 );
238
239                                 if( pxNetworkBuffer != NULL )
240                                 {
241                                         pxNetworkBuffer->pucBuffer = EMAC_NextPacketToRead();
242                                         pxNetworkBuffer->xDataLength = xDataLength;
243                                         xRxEvent.pvData = ( void * ) pxNetworkBuffer;
244
245                                         /* Data was received and stored.  Send a message to the IP
246                                         task to let it know. */
247                                         if( xSendEventStructToIPTask( &xRxEvent, ( TickType_t ) 0 ) == pdFAIL )
248                                         {
249                                                 vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
250                                                 iptraceETHERNET_RX_EVENT_LOST();
251                                         }
252                                 }
253                                 else
254                                 {
255                                         iptraceETHERNET_RX_EVENT_LOST();
256                                 }
257
258                                 iptraceNETWORK_INTERFACE_RECEIVE();
259                         }
260
261                         /* Release the frame. */
262                         EMAC_UpdateRxConsumeIndex();
263                 }
264         }
265 }
266 /*-----------------------------------------------------------*/
267