2 * FreeRTOS+UDP V1.0.2 (C) 2013 Real Time Engineers ltd.
\r
3 * All rights reserved
\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
8 * FreeRTOS+UDP uses a dual license model that allows the software to be used
\r
9 * under a standard GPL open source license, or a commercial license. The
\r
10 * standard GPL license (unlike the modified GPL license under which FreeRTOS
\r
11 * itself is distributed) requires that all software statically linked with
\r
12 * FreeRTOS+UDP is also distributed under the same GPL V2 license terms.
\r
13 * Details of both license options follow:
\r
15 * - Open source licensing -
\r
16 * FreeRTOS+UDP is a free download and may be used, modified, evaluated and
\r
17 * distributed without charge provided the user adheres to version two of the
\r
18 * GNU General Public License (GPL) and does not remove the copyright notice or
\r
19 * this text. The GPL V2 text is available on the gnu.org web site, and on the
\r
20 * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.
\r
22 * - Commercial licensing -
\r
23 * Businesses and individuals that for commercial or other reasons cannot comply
\r
24 * with the terms of the GPL V2 license must obtain a commercial license before
\r
25 * incorporating FreeRTOS+UDP into proprietary software for distribution in any
\r
26 * form. Commercial licenses can be purchased from http://shop.freertos.org/udp
\r
27 * and do not require any source files to be changed.
\r
29 * FreeRTOS+UDP is distributed in the hope that it will be useful. You cannot
\r
30 * use FreeRTOS+UDP unless you agree that you use the software 'as is'.
\r
31 * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied
\r
32 * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
33 * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
\r
34 * implied, expressed, or statutory.
\r
36 * 1 tab == 4 spaces!
\r
38 * http://www.FreeRTOS.org
\r
39 * http://www.FreeRTOS.org/udp
\r
43 /******************************************************************************
\r
45 * See the following web page for essential buffer allocation scheme usage and
\r
46 * configuration details:
\r
47 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Buffer_Management.shtml
\r
49 ******************************************************************************/
\r
51 /* THIS FILE SHOULD NOT BE USED IF THE PROJECT INCLUDES A MEMORY ALLOCATOR
\r
52 THAT WILL FRAGMENT THE HEAP MEMORY. For example, heap_2 must not be used,
\r
53 heap_4 can be used. */
\r
56 /* Standard includes. */
\r
59 /* FreeRTOS includes. */
\r
60 #include "FreeRTOS.h"
\r
64 /* FreeRTOS+UDP includes. */
\r
65 #include "FreeRTOS_UDP_IP.h"
\r
66 #include "FreeRTOS_IP_Private.h"
\r
67 #include "NetworkInterface.h"
\r
69 /* For an Ethernet interrupt to be able to obtain a network buffer there must
\r
70 be at least this number of buffers available. */
\r
71 #define ipINTERRUPT_BUFFER_GET_THRESHOLD ( 3 )
\r
73 /* A list of free (available) xNetworkBufferDescriptor_t structures. */
\r
74 static xList xFreeBuffersList;
\r
76 /* Declares the pool of xNetworkBufferDescriptor_t structures that are available to the
\r
77 system. All the network buffers referenced from xFreeBuffersList exist in this
\r
78 array. The array is not accessed directly except during initialisation, when
\r
79 the xFreeBuffersList is filled (as all the buffers are free when the system is
\r
81 static xNetworkBufferDescriptor_t xNetworkBuffers[ ipconfigNUM_NETWORK_BUFFERS ];
\r
83 /* The semaphore used to obtain network buffers. */
\r
84 static xSemaphoreHandle xNetworkBufferSemaphore = NULL;
\r
86 /*-----------------------------------------------------------*/
\r
88 portBASE_TYPE xNetworkBuffersInitialise( void )
\r
90 portBASE_TYPE xReturn, x;
\r
92 /* Only initialise the buffers and their associated kernel objects if they
\r
93 have not been initialised before. */
\r
94 if( xNetworkBufferSemaphore == NULL )
\r
96 xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFERS, ipconfigNUM_NETWORK_BUFFERS );
\r
97 configASSERT( xNetworkBufferSemaphore );
\r
98 vQueueAddToRegistry( xNetworkBufferSemaphore, "NetBufSem" );
\r
100 /* If the trace recorder code is included name the semaphore for viewing
\r
101 in FreeRTOS+Trace. */
\r
102 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1
\r
104 extern xQueueHandle xNetworkEventQueue;
\r
105 vTraceSetQueueName( xNetworkEventQueue, "IPStackEvent" );
\r
106 vTraceSetQueueName( xNetworkBufferSemaphore, "NetworkBufferCount" );
\r
108 #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */
\r
110 if( xNetworkBufferSemaphore != NULL )
\r
112 vListInitialise( &xFreeBuffersList );
\r
114 /* Initialise all the network buffers. No storage is allocated to
\r
115 the buffers yet. */
\r
116 for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )
\r
118 /* Initialise and set the owner of the buffer list items. */
\r
119 xNetworkBuffers[ x ].pucEthernetBuffer = NULL;
\r
120 vListInitialiseItem( &( xNetworkBuffers[ x ].xBufferListItem ) );
\r
121 listSET_LIST_ITEM_OWNER( &( xNetworkBuffers[ x ].xBufferListItem ), &xNetworkBuffers[ x ] );
\r
123 /* Currently, all buffers are available for use. */
\r
124 vListInsert( &xFreeBuffersList, &( xNetworkBuffers[ x ].xBufferListItem ) );
\r
129 if( xNetworkBufferSemaphore == NULL )
\r
140 /*-----------------------------------------------------------*/
\r
142 uint8_t *pucEthernetBufferGet( size_t *pxRequestedSizeBytes )
\r
144 uint8_t *pucEthernetBuffer;
\r
146 if( *pxRequestedSizeBytes < sizeof( xARPPacket_t ) )
\r
148 /* Buffers must be at least large enough to hold ARP packets, otherwise
\r
149 nothing can be done. */
\r
150 *pxRequestedSizeBytes = sizeof( xARPPacket_t );
\r
153 /* Allocate a buffer large enough to store the requested Ethernet frame size
\r
154 and a pointer to a network buffer structure (hence the addition of
\r
155 ipBUFFER_PADDING bytes). */
\r
156 pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( *pxRequestedSizeBytes + ipBUFFER_PADDING );
\r
158 /* Enough space is left at the start of the buffer to place a pointer to
\r
159 the network buffer structure that references this Ethernet buffer. Return
\r
160 a pointer to the start of the Ethernet buffer itself. */
\r
161 pucEthernetBuffer += ipBUFFER_PADDING;
\r
163 return pucEthernetBuffer;
\r
165 /*-----------------------------------------------------------*/
\r
167 void vEthernetBufferRelease( uint8_t *pucEthernetBuffer )
\r
169 /* There is space before the Ethernet buffer in which a pointer to the
\r
170 network buffer that references this Ethernet buffer is stored. Remove the
\r
171 space before freeing the buffer. */
\r
172 if( pucEthernetBuffer != NULL )
\r
174 pucEthernetBuffer -= ipBUFFER_PADDING;
\r
175 vPortFree( ( void * ) pucEthernetBuffer );
\r
178 /*-----------------------------------------------------------*/
\r
180 xNetworkBufferDescriptor_t *pxNetworkBufferGet( size_t xRequestedSizeBytes, portTickType xBlockTimeTicks )
\r
182 xNetworkBufferDescriptor_t *pxReturn = NULL;
\r
184 if( ( xRequestedSizeBytes != 0 ) && ( xRequestedSizeBytes < sizeof( xARPPacket_t ) ) )
\r
186 /* ARP packets can replace application packets, so the storage must be
\r
187 at least large enough to hold an ARP. */
\r
188 xRequestedSizeBytes = sizeof( xARPPacket_t );
\r
191 /* If there is a semaphore available, there is a network buffer available. */
\r
192 if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )
\r
194 /* Protect the structure as it is accessed from tasks and interrupts. */
\r
195 taskENTER_CRITICAL();
\r
197 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
\r
198 uxListRemove( &( pxReturn->xBufferListItem ) );
\r
200 taskEXIT_CRITICAL();
\r
202 /* Allocate storage of exactly the requested size to the buffer. */
\r
203 configASSERT( pxReturn->pucEthernetBuffer == NULL );
\r
204 if( xRequestedSizeBytes > 0 )
\r
206 /* Extra space is obtained so a pointer to the network buffer can
\r
207 be stored at the beginning of the buffer. */
\r
208 pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytes + ipBUFFER_PADDING );
\r
210 if( pxReturn->pucEthernetBuffer == NULL )
\r
212 /* The attempt to allocate storage for the buffer payload failed,
\r
213 so the network buffer structure cannot be used and must be
\r
215 vNetworkBufferRelease( pxReturn );
\r
220 /* Store a pointer to the network buffer structure in the
\r
221 buffer storage area, then move the buffer pointer on past the
\r
222 stored pointer so the pointer value is not overwritten by the
\r
223 application when the buffer is used. */
\r
224 *( ( xNetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer ) ) = pxReturn;
\r
225 pxReturn->pucEthernetBuffer += ipBUFFER_PADDING;
\r
226 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
\r
228 /* Store the actual size of the allocated buffer, which may be
\r
229 greater than the requested size. */
\r
230 pxReturn->xDataLength = xRequestedSizeBytes;
\r
235 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
\r
239 if( pxReturn == NULL )
\r
241 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();
\r
246 /*-----------------------------------------------------------*/
\r
248 void vNetworkBufferRelease( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
250 portBASE_TYPE xListItemAlreadyInFreeList;
\r
252 /* Ensure the buffer is returned to the list of free buffers before the
\r
253 counting semaphore is 'given' to say a buffer is available. Release the
\r
254 storage allocated to the buffer payload. THIS FILE SHOULD NOT BE USED
\r
255 IF THE PROJECT INCLUDES A MEMORY ALLOCATOR THAT WILL FRAGMENT THE HEAP
\r
256 MEMORY. For example, heap_2 must not be used, heap_4 can be used. */
\r
257 vEthernetBufferRelease( pxNetworkBuffer->pucEthernetBuffer );
\r
258 pxNetworkBuffer->pucEthernetBuffer = NULL;
\r
260 taskENTER_CRITICAL();
\r
262 xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
264 if( xListItemAlreadyInFreeList == pdFALSE )
\r
266 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
269 configASSERT( xListItemAlreadyInFreeList == pdFALSE );
\r
271 taskEXIT_CRITICAL();
\r
273 xSemaphoreGive( xNetworkBufferSemaphore );
\r
274 iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
\r
276 /*-----------------------------------------------------------*/
\r
278 #if( ipconfigINCLUDE_TEST_CODE == 1 )
\r
280 unsigned portBASE_TYPE uxGetNumberOfFreeNetworkBuffers( void )
\r
282 return listCURRENT_LIST_LENGTH( &xFreeBuffersList );
\r
285 #endif /* ipconfigINCLUDE_TEST_CODE */
\r