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 standard GPL open source license, or a commercial license. The
\r
9 * standard GPL license (unlike the modified GPL license under which FreeRTOS
\r
10 * itself is distributed) requires that all software statically linked with
\r
11 * FreeRTOS+UDP is also distributed under the same GPL V2 license terms.
\r
12 * Details of both license options follow:
\r
14 * - Open source licensing -
\r
15 * FreeRTOS+UDP is a free download and may be used, modified, evaluated and
\r
16 * distributed without charge provided the user adheres to version two of the
\r
17 * GNU General Public License (GPL) and does not remove the copyright notice or
\r
18 * this text. The GPL V2 text is available on the gnu.org web site, and on the
\r
19 * following URL: http://www.FreeRTOS.org/gpl-2.0.txt.
\r
21 * - Commercial licensing -
\r
22 * Businesses and individuals that for commercial or other reasons cannot comply
\r
23 * with the terms of the GPL V2 license must obtain a commercial license before
\r
24 * incorporating FreeRTOS+UDP into proprietary software for distribution in any
\r
25 * form. Commercial licenses can be purchased from http://shop.freertos.org/udp
\r
26 * and do not require any source files to be changed.
\r
28 * FreeRTOS+UDP is distributed in the hope that it will be useful. You cannot
\r
29 * use FreeRTOS+UDP unless you agree that you use the software 'as is'.
\r
30 * FreeRTOS+UDP is provided WITHOUT ANY WARRANTY; without even the implied
\r
31 * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
32 * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
\r
33 * implied, expressed, or statutory.
\r
35 * 1 tab == 4 spaces!
\r
37 * http://www.FreeRTOS.org
\r
38 * http://www.FreeRTOS.org/udp
\r
42 /******************************************************************************
\r
44 * See the following web page for essential buffer allocation scheme usage and
\r
45 * configuration details:
\r
46 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Buffer_Management.shtml
\r
48 ******************************************************************************/
\r
50 /* THIS FILE SHOULD NOT BE USED IF THE PROJECT INCLUDES A MEMORY ALLOCATOR
\r
51 THAT WILL FRAGMENT THE HEAP MEMORY. For example, heap_2 must not be used,
\r
52 heap_4 can be used. */
\r
55 /* Standard includes. */
\r
58 /* FreeRTOS includes. */
\r
59 #include "FreeRTOS.h"
\r
63 /* FreeRTOS+UDP includes. */
\r
64 #include "FreeRTOS_UDP_IP.h"
\r
65 #include "FreeRTOS_IP_Private.h"
\r
66 #include "NetworkInterface.h"
\r
68 /* For an Ethernet interrupt to be able to obtain a network buffer there must
\r
69 be at least this number of buffers available. */
\r
70 #define ipINTERRUPT_BUFFER_GET_THRESHOLD ( 3 )
\r
72 /* A list of free (available) xNetworkBufferDescriptor_t structures. */
\r
73 static xList xFreeBuffersList;
\r
75 /* Declares the pool of xNetworkBufferDescriptor_t structures that are available to the
\r
76 system. All the network buffers referenced from xFreeBuffersList exist in this
\r
77 array. The array is not accessed directly except during initialisation, when
\r
78 the xFreeBuffersList is filled (as all the buffers are free when the system is
\r
80 static xNetworkBufferDescriptor_t xNetworkBuffers[ ipconfigNUM_NETWORK_BUFFERS ];
\r
82 /* The semaphore used to obtain network buffers. */
\r
83 static xSemaphoreHandle xNetworkBufferSemaphore = NULL;
\r
85 /*-----------------------------------------------------------*/
\r
87 portBASE_TYPE xNetworkBuffersInitialise( void )
\r
89 portBASE_TYPE xReturn, x;
\r
91 /* Only initialise the buffers and their associated kernel objects if they
\r
92 have not been initialised before. */
\r
93 if( xNetworkBufferSemaphore == NULL )
\r
95 xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFERS, ipconfigNUM_NETWORK_BUFFERS );
\r
96 configASSERT( xNetworkBufferSemaphore );
\r
97 vQueueAddToRegistry( xNetworkBufferSemaphore, ( signed char * ) "NetBufSem" );
\r
99 /* If the trace recorder code is included name the semaphore for viewing
\r
100 in FreeRTOS+Trace. */
\r
101 #if ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1
\r
103 extern xQueueHandle xNetworkEventQueue;
\r
104 vTraceSetQueueName( xNetworkEventQueue, "IPStackEvent" );
\r
105 vTraceSetQueueName( xNetworkBufferSemaphore, "NetworkBufferCount" );
\r
107 #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */
\r
109 if( xNetworkBufferSemaphore != NULL )
\r
111 vListInitialise( &xFreeBuffersList );
\r
113 /* Initialise all the network buffers. No storage is allocated to
\r
114 the buffers yet. */
\r
115 for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )
\r
117 /* Initialise and set the owner of the buffer list items. */
\r
118 xNetworkBuffers[ x ].pucEthernetBuffer = NULL;
\r
119 vListInitialiseItem( &( xNetworkBuffers[ x ].xBufferListItem ) );
\r
120 listSET_LIST_ITEM_OWNER( &( xNetworkBuffers[ x ].xBufferListItem ), &xNetworkBuffers[ x ] );
\r
122 /* Currently, all buffers are available for use. */
\r
123 vListInsert( &xFreeBuffersList, &( xNetworkBuffers[ x ].xBufferListItem ) );
\r
128 if( xNetworkBufferSemaphore == NULL )
\r
139 /*-----------------------------------------------------------*/
\r
141 uint8_t *pucEthernetBufferGet( size_t *pxRequestedSizeBytes )
\r
143 uint8_t *pucEthernetBuffer;
\r
145 if( *pxRequestedSizeBytes < sizeof( xARPPacket_t ) )
\r
147 /* Buffers must be at least large enough to hold ARP packets, otherwise
\r
148 nothing can be done. */
\r
149 *pxRequestedSizeBytes = sizeof( xARPPacket_t );
\r
152 /* Allocate a buffer large enough to store the requested Ethernet frame size
\r
153 and a pointer to a network buffer structure (hence the addition of
\r
154 ipBUFFER_PADDING bytes). */
\r
155 pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( *pxRequestedSizeBytes + ipBUFFER_PADDING );
\r
157 /* Enough space is left at the start of the buffer to place a pointer to
\r
158 the network buffer structure that references this Ethernet buffer. Return
\r
159 a pointer to the start of the Ethernet buffer itself. */
\r
160 pucEthernetBuffer += ipBUFFER_PADDING;
\r
162 return pucEthernetBuffer;
\r
164 /*-----------------------------------------------------------*/
\r
166 void vEthernetBufferRelease( uint8_t *pucEthernetBuffer )
\r
168 /* There is space before the Ethernet buffer in which a pointer to the
\r
169 network buffer that references this Ethernet buffer is stored. Remove the
\r
170 space before freeing the buffer. */
\r
171 if( pucEthernetBuffer != NULL )
\r
173 pucEthernetBuffer -= ipBUFFER_PADDING;
\r
174 vPortFree( ( void * ) pucEthernetBuffer );
\r
177 /*-----------------------------------------------------------*/
\r
179 xNetworkBufferDescriptor_t *pxNetworkBufferGet( size_t xRequestedSizeBytes, portTickType xBlockTimeTicks )
\r
181 xNetworkBufferDescriptor_t *pxReturn = NULL;
\r
183 if( ( xRequestedSizeBytes != 0 ) && ( xRequestedSizeBytes < sizeof( xARPPacket_t ) ) )
\r
185 /* ARP packets can replace application packets, so the storage must be
\r
186 at least large enough to hold an ARP. */
\r
187 xRequestedSizeBytes = sizeof( xARPPacket_t );
\r
190 /* If there is a semaphore available, there is a network buffer available. */
\r
191 if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )
\r
193 /* Protect the structure as it is accessed from tasks and interrupts. */
\r
194 taskENTER_CRITICAL();
\r
196 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
\r
197 uxListRemove( &( pxReturn->xBufferListItem ) );
\r
199 taskEXIT_CRITICAL();
\r
201 /* Allocate storage of exactly the requested size to the buffer. */
\r
202 configASSERT( pxReturn->pucEthernetBuffer == NULL );
\r
203 if( xRequestedSizeBytes > 0 )
\r
205 /* Extra space is obtained so a pointer to the network buffer can
\r
206 be stored at the beginning of the buffer. */
\r
207 pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytes + ipBUFFER_PADDING );
\r
209 if( pxReturn->pucEthernetBuffer == NULL )
\r
211 /* The attempt to allocate storage for the buffer payload failed,
\r
212 so the network buffer structure cannot be used and must be
\r
214 vNetworkBufferRelease( pxReturn );
\r
219 /* Store a pointer to the network buffer structure in the
\r
220 buffer storage area, then move the buffer pointer on past the
\r
221 stored pointer so the pointer value is not overwritten by the
\r
222 application when the buffer is used. */
\r
223 *( ( xNetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer ) ) = pxReturn;
\r
224 pxReturn->pucEthernetBuffer += ipBUFFER_PADDING;
\r
225 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
\r
227 /* Store the actual size of the allocated buffer, which may be
\r
228 greater than the requested size. */
\r
229 pxReturn->xDataLength = xRequestedSizeBytes;
\r
234 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
\r
238 if( pxReturn == NULL )
\r
240 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();
\r
245 /*-----------------------------------------------------------*/
\r
247 void vNetworkBufferRelease( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
249 portBASE_TYPE xListItemAlreadyInFreeList;
\r
251 /* Ensure the buffer is returned to the list of free buffers before the
\r
252 counting semaphore is 'given' to say a buffer is available. Release the
\r
253 storage allocated to the buffer payload. THIS FILE SHOULD NOT BE USED
\r
254 IF THE PROJECT INCLUDES A MEMORY ALLOCATOR THAT WILL FRAGMENT THE HEAP
\r
255 MEMORY. For example, heap_2 must not be used, heap_4 can be used. */
\r
256 vEthernetBufferRelease( pxNetworkBuffer->pucEthernetBuffer );
\r
257 pxNetworkBuffer->pucEthernetBuffer = NULL;
\r
259 taskENTER_CRITICAL();
\r
261 xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
263 if( xListItemAlreadyInFreeList == pdFALSE )
\r
265 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
268 configASSERT( xListItemAlreadyInFreeList == pdFALSE );
\r
270 taskEXIT_CRITICAL();
\r
272 xSemaphoreGive( xNetworkBufferSemaphore );
\r
273 iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
\r
275 /*-----------------------------------------------------------*/
\r
277 #if( ipconfigINCLUDE_TEST_CODE == 1 )
\r
279 unsigned portBASE_TYPE uxGetNumberOfFreeNetworkBuffers( void )
\r
281 return listCURRENT_LIST_LENGTH( &xFreeBuffersList );
\r
284 #endif /* ipconfigINCLUDE_TEST_CODE */
\r