2 * FreeRTOS+UDP V1.0.0 (C) 2013 Real Time Engineers ltd.
\r
4 * This file is part of the FreeRTOS+UDP distribution. The FreeRTOS+UDP license
\r
5 * terms are different to the FreeRTOS license terms.
\r
7 * FreeRTOS+UDP uses a dual license model that allows the software to be used
\r
8 * under a pure GPL open source license (as opposed to the modified GPL license
\r
9 * under which FreeRTOS is distributed) or a commercial license. Details of
\r
10 * both license options follow:
\r
12 * - Open source licensing -
\r
13 * FreeRTOS+UDP is a free download and may be used, modified, evaluated and
\r
14 * distributed without charge provided the user adheres to version two of the
\r
15 * GNU General Public License (GPL) and does not remove the copyright notice or
\r
16 * this text. The GPL V2 text is available on the gnu.org web site, and on the
\r
17 * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.
\r
19 * - Commercial licensing -
\r
20 * Businesses and individuals that for commercial or other reasons cannot comply
\r
21 * with the terms of the GPL V2 license must obtain a commercial license before
\r
22 * incorporating FreeRTOS+UDP into proprietary software for distribution in any
\r
23 * form. Commercial licenses can be purchased from http://shop.freertos.org/udp
\r
24 * and do not require any source files to be changed.
\r
26 * FreeRTOS+UDP is distributed in the hope that it will be useful. You cannot
\r
27 * use FreeRTOS+UDP unless you agree that you use the software 'as is'.
\r
28 * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied
\r
29 * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
30 * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
\r
31 * implied, expressed, or statutory.
\r
33 * 1 tab == 4 spaces!
\r
35 * http://www.FreeRTOS.org
\r
36 * http://www.FreeRTOS.org/udp
\r
40 /******************************************************************************
\r
42 * See the following web page for essential buffer allocation scheme usage and
\r
43 * configuration details:
\r
44 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Buffer_Management.shtml
\r
46 ******************************************************************************/
\r
48 /* THIS FILE SHOULD NOT BE USED IF THE PROJECT INCLUDES A MEMORY ALLOCATOR
\r
49 THAT WILL FRAGMENT THE HEAP MEMORY. For example, heap_2 must not be used,
\r
50 heap_4 can be used. */
\r
53 /* Standard includes. */
\r
56 /* FreeRTOS includes. */
\r
57 #include "FreeRTOS.h"
\r
61 /* FreeRTOS+UDP includes. */
\r
62 #include "FreeRTOS_UDP_IP.h"
\r
63 #include "FreeRTOS_IP_Private.h"
\r
64 #include "NetworkInterface.h"
\r
66 /* For an Ethernet interrupt to be able to obtain a network buffer there must
\r
67 be at least this number of buffers available. */
\r
68 #define ipINTERRUPT_BUFFER_GET_THRESHOLD ( 3 )
\r
70 /* A list of free (available) xNetworkBufferDescriptor_t structures. */
\r
71 static xList xFreeBuffersList;
\r
73 /* Declares the pool of xNetworkBufferDescriptor_t structures that are available to the
\r
74 system. All the network buffers referenced from xFreeBuffersList exist in this
\r
75 array. The array is not accessed directly except during initialisation, when
\r
76 the xFreeBuffersList is filled (as all the buffers are free when the system is
\r
78 static xNetworkBufferDescriptor_t xNetworkBuffers[ ipconfigNUM_NETWORK_BUFFERS ];
\r
80 /* The semaphore used to obtain network buffers. */
\r
81 static xSemaphoreHandle xNetworkBufferSemaphore = NULL;
\r
83 /*-----------------------------------------------------------*/
\r
85 portBASE_TYPE xNetworkBuffersInitialise( void )
\r
87 portBASE_TYPE xReturn, x;
\r
89 /* Only initialise the buffers and their associated kernel objects if they
\r
90 have not been initialised before. */
\r
91 if( xNetworkBufferSemaphore == NULL )
\r
93 xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFERS, ipconfigNUM_NETWORK_BUFFERS );
\r
94 configASSERT( xNetworkBufferSemaphore );
\r
95 vQueueAddToRegistry( xNetworkBufferSemaphore, ( signed char * ) "NetBufSem" );
\r
97 /* If the trace recorder code is included name the semaphore for viewing
\r
98 in FreeRTOS+Trace. */
\r
99 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1
\r
101 extern xQueueHandle xNetworkEventQueue;
\r
102 vTraceSetQueueName( xNetworkEventQueue, "IPStackEvent" );
\r
103 vTraceSetQueueName( xNetworkBufferSemaphore, "NetworkBufferCount" );
\r
105 #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */
\r
107 if( xNetworkBufferSemaphore != NULL )
\r
109 vListInitialise( &xFreeBuffersList );
\r
111 /* Initialise all the network buffers. No storage is allocated to
\r
112 the buffers yet. */
\r
113 for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )
\r
115 /* Initialise and set the owner of the buffer list items. */
\r
116 xNetworkBuffers[ x ].pucEthernetBuffer = NULL;
\r
117 vListInitialiseItem( &( xNetworkBuffers[ x ].xBufferListItem ) );
\r
118 listSET_LIST_ITEM_OWNER( &( xNetworkBuffers[ x ].xBufferListItem ), &xNetworkBuffers[ x ] );
\r
120 /* Currently, all buffers are available for use. */
\r
121 vListInsert( &xFreeBuffersList, &( xNetworkBuffers[ x ].xBufferListItem ) );
\r
126 if( xNetworkBufferSemaphore == NULL )
\r
137 /*-----------------------------------------------------------*/
\r
139 uint8_t *pucEthernetBufferGet( size_t *pxRequestedSizeBytes )
\r
141 uint8_t *pucEthernetBuffer;
\r
143 if( *pxRequestedSizeBytes < sizeof( xARPPacket_t ) )
\r
145 /* Buffers must be at least large enough to hold ARP packets, otherwise
\r
146 nothing can be done. */
\r
147 *pxRequestedSizeBytes = sizeof( xARPPacket_t );
\r
150 /* Allocate a buffer large enough to store the requested Ethernet frame size
\r
151 and a pointer to a network buffer structure (hence the addition of
\r
152 ipBUFFER_PADDING bytes). */
\r
153 pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( *pxRequestedSizeBytes + ipBUFFER_PADDING );
\r
155 /* Enough space is left at the start of the buffer to place a pointer to
\r
156 the network buffer structure that references this Ethernet buffer. Return
\r
157 a pointer to the start of the Ethernet buffer itself. */
\r
158 pucEthernetBuffer += ipBUFFER_PADDING;
\r
160 return pucEthernetBuffer;
\r
162 /*-----------------------------------------------------------*/
\r
164 void vEthernetBufferRelease( uint8_t *pucEthernetBuffer )
\r
166 /* There is space before the Ethernet buffer in which a pointer to the
\r
167 network buffer that references this Ethernet buffer is stored. Remove the
\r
168 space before freeing the buffer. */
\r
169 if( pucEthernetBuffer != NULL )
\r
171 pucEthernetBuffer -= ipBUFFER_PADDING;
\r
172 vPortFree( ( void * ) pucEthernetBuffer );
\r
175 /*-----------------------------------------------------------*/
\r
177 xNetworkBufferDescriptor_t *pxNetworkBufferGet( size_t xRequestedSizeBytes, portTickType xBlockTimeTicks )
\r
179 xNetworkBufferDescriptor_t *pxReturn = NULL;
\r
181 if( ( xRequestedSizeBytes != 0 ) && ( xRequestedSizeBytes < sizeof( xARPPacket_t ) ) )
\r
183 /* ARP packets can replace application packets, so the storage must be
\r
184 at least large enough to hold an ARP. */
\r
185 xRequestedSizeBytes = sizeof( xARPPacket_t );
\r
188 /* If there is a semaphore available, there is a network buffer available. */
\r
189 if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )
\r
191 /* Protect the structure as it is accessed from tasks and interrupts. */
\r
192 taskENTER_CRITICAL();
\r
194 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
\r
195 uxListRemove( &( pxReturn->xBufferListItem ) );
\r
197 taskEXIT_CRITICAL();
\r
199 /* Allocate storage of exactly the requested size to the buffer. */
\r
200 configASSERT( pxReturn->pucEthernetBuffer == NULL );
\r
201 if( xRequestedSizeBytes > 0 )
\r
203 /* Extra space is obtained so a pointer to the network buffer can
\r
204 be stored at the beginning of the buffer. */
\r
205 pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytes + ipBUFFER_PADDING );
\r
207 if( pxReturn->pucEthernetBuffer == NULL )
\r
209 /* The attempt to allocate storage for the buffer payload failed,
\r
210 so the network buffer structure cannot be used and must be
\r
212 vNetworkBufferRelease( pxReturn );
\r
217 /* Store a pointer to the network buffer structure in the
\r
218 buffer storage area, then move the buffer pointer on past the
\r
219 stored pointer so the pointer value is not overwritten by the
\r
220 application when the buffer is used. */
\r
221 *( ( xNetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer ) ) = pxReturn;
\r
222 pxReturn->pucEthernetBuffer += ipBUFFER_PADDING;
\r
223 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
\r
225 /* Store the actual size of the allocated buffer, which may be
\r
226 greater than the requested size. */
\r
227 pxReturn->xDataLength = xRequestedSizeBytes;
\r
232 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
\r
236 if( pxReturn == NULL )
\r
238 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();
\r
243 /*-----------------------------------------------------------*/
\r
245 void vNetworkBufferRelease( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
247 portBASE_TYPE xListItemAlreadyInFreeList;
\r
249 /* Ensure the buffer is returned to the list of free buffers before the
\r
250 counting semaphore is 'given' to say a buffer is available. Release the
\r
251 storage allocated to the buffer payload. THIS FILE SHOULD NOT BE USED
\r
252 IF THE PROJECT INCLUDES A MEMORY ALLOCATOR THAT WILL FRAGMENT THE HEAP
\r
253 MEMORY. For example, heap_2 must not be used, heap_4 can be used. */
\r
254 vEthernetBufferRelease( pxNetworkBuffer->pucEthernetBuffer );
\r
255 pxNetworkBuffer->pucEthernetBuffer = NULL;
\r
257 taskENTER_CRITICAL();
\r
259 xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
261 if( xListItemAlreadyInFreeList == pdFALSE )
\r
263 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
266 configASSERT( xListItemAlreadyInFreeList == pdFALSE );
\r
268 taskEXIT_CRITICAL();
\r
270 xSemaphoreGive( xNetworkBufferSemaphore );
\r
271 iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
\r
273 /*-----------------------------------------------------------*/
\r
275 #if( ipconfigINCLUDE_TEST_CODE == 1 )
\r
277 unsigned portBASE_TYPE uxGetNumberOfFreeNetworkBuffers( void )
\r
279 return listCURRENT_LIST_LENGTH( &xFreeBuffersList );
\r
282 #endif /* ipconfigINCLUDE_TEST_CODE */
\r