2 * FreeRTOS+UDP V1.0.4
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /******************************************************************************
\r
31 * See the following web page for essential buffer allocation scheme usage and
\r
32 * configuration details:
\r
33 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Buffer_Management.shtml
\r
35 ******************************************************************************/
\r
37 /* THIS FILE SHOULD NOT BE USED IF THE PROJECT INCLUDES A MEMORY ALLOCATOR
\r
38 THAT WILL FRAGMENT THE HEAP MEMORY. For example, heap_2 must not be used,
\r
39 heap_4 can be used. */
\r
42 /* Standard includes. */
\r
45 /* FreeRTOS includes. */
\r
46 #include "FreeRTOS.h"
\r
50 /* FreeRTOS+UDP includes. */
\r
51 #include "FreeRTOS_UDP_IP.h"
\r
52 #include "FreeRTOS_IP_Private.h"
\r
53 #include "NetworkInterface.h"
\r
55 /* For an Ethernet interrupt to be able to obtain a network buffer there must
\r
56 be at least this number of buffers available. */
\r
57 #define ipINTERRUPT_BUFFER_GET_THRESHOLD ( 3 )
\r
59 /* A list of free (available) xNetworkBufferDescriptor_t structures. */
\r
60 static xList xFreeBuffersList;
\r
62 /* Declares the pool of xNetworkBufferDescriptor_t structures that are available to the
\r
63 system. All the network buffers referenced from xFreeBuffersList exist in this
\r
64 array. The array is not accessed directly except during initialisation, when
\r
65 the xFreeBuffersList is filled (as all the buffers are free when the system is
\r
67 static xNetworkBufferDescriptor_t xNetworkBuffers[ ipconfigNUM_NETWORK_BUFFERS ];
\r
69 /* The semaphore used to obtain network buffers. */
\r
70 static xSemaphoreHandle xNetworkBufferSemaphore = NULL;
\r
72 /*-----------------------------------------------------------*/
\r
74 BaseType_t xNetworkBuffersInitialise( void )
\r
76 BaseType_t xReturn, x;
\r
78 /* Only initialise the buffers and their associated kernel objects if they
\r
79 have not been initialised before. */
\r
80 if( xNetworkBufferSemaphore == NULL )
\r
82 xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFERS, ipconfigNUM_NETWORK_BUFFERS );
\r
83 configASSERT( xNetworkBufferSemaphore );
\r
84 vQueueAddToRegistry( xNetworkBufferSemaphore, "NetBufSem" );
\r
86 /* If the trace recorder code is included name the semaphore for viewing
\r
87 in FreeRTOS+Trace. */
\r
88 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1
\r
90 extern xQueueHandle xNetworkEventQueue;
\r
91 vTraceSetQueueName( xNetworkEventQueue, "IPStackEvent" );
\r
92 vTraceSetQueueName( xNetworkBufferSemaphore, "NetworkBufferCount" );
\r
94 #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */
\r
96 if( xNetworkBufferSemaphore != NULL )
\r
98 vListInitialise( &xFreeBuffersList );
\r
100 /* Initialise all the network buffers. No storage is allocated to
\r
101 the buffers yet. */
\r
102 for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )
\r
104 /* Initialise and set the owner of the buffer list items. */
\r
105 xNetworkBuffers[ x ].pucEthernetBuffer = NULL;
\r
106 vListInitialiseItem( &( xNetworkBuffers[ x ].xBufferListItem ) );
\r
107 listSET_LIST_ITEM_OWNER( &( xNetworkBuffers[ x ].xBufferListItem ), &xNetworkBuffers[ x ] );
\r
109 /* Currently, all buffers are available for use. */
\r
110 vListInsert( &xFreeBuffersList, &( xNetworkBuffers[ x ].xBufferListItem ) );
\r
115 if( xNetworkBufferSemaphore == NULL )
\r
126 /*-----------------------------------------------------------*/
\r
128 uint8_t *pucEthernetBufferGet( size_t *pxRequestedSizeBytes )
\r
130 uint8_t *pucEthernetBuffer;
\r
132 if( *pxRequestedSizeBytes < sizeof( xARPPacket_t ) )
\r
134 /* Buffers must be at least large enough to hold ARP packets, otherwise
\r
135 nothing can be done. */
\r
136 *pxRequestedSizeBytes = sizeof( xARPPacket_t );
\r
139 /* Allocate a buffer large enough to store the requested Ethernet frame size
\r
140 and a pointer to a network buffer structure (hence the addition of
\r
141 ipBUFFER_PADDING bytes). */
\r
142 pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( *pxRequestedSizeBytes + ipBUFFER_PADDING );
\r
144 /* Enough space is left at the start of the buffer to place a pointer to
\r
145 the network buffer structure that references this Ethernet buffer. Return
\r
146 a pointer to the start of the Ethernet buffer itself. */
\r
147 pucEthernetBuffer += ipBUFFER_PADDING;
\r
149 return pucEthernetBuffer;
\r
151 /*-----------------------------------------------------------*/
\r
153 void vEthernetBufferRelease( uint8_t *pucEthernetBuffer )
\r
155 /* There is space before the Ethernet buffer in which a pointer to the
\r
156 network buffer that references this Ethernet buffer is stored. Remove the
\r
157 space before freeing the buffer. */
\r
158 if( pucEthernetBuffer != NULL )
\r
160 pucEthernetBuffer -= ipBUFFER_PADDING;
\r
161 vPortFree( ( void * ) pucEthernetBuffer );
\r
164 /*-----------------------------------------------------------*/
\r
166 xNetworkBufferDescriptor_t *pxNetworkBufferGet( size_t xRequestedSizeBytes, TickType_t xBlockTimeTicks )
\r
168 xNetworkBufferDescriptor_t *pxReturn = NULL;
\r
170 if( ( xRequestedSizeBytes != 0 ) && ( xRequestedSizeBytes < sizeof( xARPPacket_t ) ) )
\r
172 /* ARP packets can replace application packets, so the storage must be
\r
173 at least large enough to hold an ARP. */
\r
174 xRequestedSizeBytes = sizeof( xARPPacket_t );
\r
177 /* If there is a semaphore available, there is a network buffer available. */
\r
178 if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )
\r
180 /* Protect the structure as it is accessed from tasks and interrupts. */
\r
181 taskENTER_CRITICAL();
\r
183 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
\r
184 uxListRemove( &( pxReturn->xBufferListItem ) );
\r
186 taskEXIT_CRITICAL();
\r
188 /* Allocate storage of exactly the requested size to the buffer. */
\r
189 configASSERT( pxReturn->pucEthernetBuffer == NULL );
\r
190 if( xRequestedSizeBytes > 0 )
\r
192 /* Extra space is obtained so a pointer to the network buffer can
\r
193 be stored at the beginning of the buffer. */
\r
194 pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytes + ipBUFFER_PADDING );
\r
196 if( pxReturn->pucEthernetBuffer == NULL )
\r
198 /* The attempt to allocate storage for the buffer payload failed,
\r
199 so the network buffer structure cannot be used and must be
\r
201 vNetworkBufferRelease( pxReturn );
\r
206 /* Store a pointer to the network buffer structure in the
\r
207 buffer storage area, then move the buffer pointer on past the
\r
208 stored pointer so the pointer value is not overwritten by the
\r
209 application when the buffer is used. */
\r
210 *( ( xNetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer ) ) = pxReturn;
\r
211 pxReturn->pucEthernetBuffer += ipBUFFER_PADDING;
\r
212 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
\r
214 /* Store the actual size of the allocated buffer, which may be
\r
215 greater than the requested size. */
\r
216 pxReturn->xDataLength = xRequestedSizeBytes;
\r
221 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
\r
225 if( pxReturn == NULL )
\r
227 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();
\r
232 /*-----------------------------------------------------------*/
\r
234 void vNetworkBufferRelease( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
236 BaseType_t xListItemAlreadyInFreeList;
\r
238 /* Ensure the buffer is returned to the list of free buffers before the
\r
239 counting semaphore is 'given' to say a buffer is available. Release the
\r
240 storage allocated to the buffer payload. THIS FILE SHOULD NOT BE USED
\r
241 IF THE PROJECT INCLUDES A MEMORY ALLOCATOR THAT WILL FRAGMENT THE HEAP
\r
242 MEMORY. For example, heap_2 must not be used, heap_4 can be used. */
\r
243 vEthernetBufferRelease( pxNetworkBuffer->pucEthernetBuffer );
\r
244 pxNetworkBuffer->pucEthernetBuffer = NULL;
\r
246 taskENTER_CRITICAL();
\r
248 xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
250 if( xListItemAlreadyInFreeList == pdFALSE )
\r
252 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
255 configASSERT( xListItemAlreadyInFreeList == pdFALSE );
\r
257 taskEXIT_CRITICAL();
\r
259 xSemaphoreGive( xNetworkBufferSemaphore );
\r
260 iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
\r
262 /*-----------------------------------------------------------*/
\r
264 #if( ipconfigINCLUDE_TEST_CODE == 1 )
\r
266 UBaseType_t uxGetNumberOfFreeNetworkBuffers( void )
\r
268 return listCURRENT_LIST_LENGTH( &xFreeBuffersList );
\r
271 #endif /* ipconfigINCLUDE_TEST_CODE */
\r