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