2 * FreeRTOS+UDP V1.0.1 (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
44 /******************************************************************************
\r
46 * See the following web page for essential buffer allocation scheme usage and
\r
47 * configuration details:
\r
48 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Buffer_Management.shtml
\r
50 ******************************************************************************/
\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 "NetworkInterface.h"
\r
65 /* For an Ethernet interrupt to be able to obtain a network buffer there must
\r
66 be at least this number of buffers available. */
\r
67 #define ipINTERRUPT_BUFFER_GET_THRESHOLD ( 3 )
\r
69 /* A list of free (available) xNetworkBufferDescriptor_t structures. */
\r
70 static xList xFreeBuffersList;
\r
72 /* Declares the pool of xNetworkBufferDescriptor_t structures that are available to the
\r
73 system. All the network buffers referenced from xFreeBuffersList exist in this
\r
74 array. The array is not accessed directly except during initialisation, when
\r
75 the xFreeBuffersList is filled (as all the buffers are free when the system is
\r
77 static xNetworkBufferDescriptor_t xNetworkBuffers[ ipconfigNUM_NETWORK_BUFFERS ];
\r
79 /* The semaphore used to obtain network buffers. */
\r
80 static xSemaphoreHandle xNetworkBufferSemaphore = NULL;
\r
82 /*-----------------------------------------------------------*/
\r
84 portBASE_TYPE xNetworkBuffersInitialise( void )
\r
86 portBASE_TYPE xReturn, x;
\r
88 /* Only initialise the buffers and their associated kernel objects if they
\r
89 have not been initialised before. */
\r
90 if( xNetworkBufferSemaphore == NULL )
\r
92 xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFERS, ipconfigNUM_NETWORK_BUFFERS );
\r
93 configASSERT( xNetworkBufferSemaphore );
\r
95 if( xNetworkBufferSemaphore != NULL )
\r
97 vListInitialise( &xFreeBuffersList );
\r
99 /* Initialise all the network buffers. The buffer storage comes
\r
100 from the network interface, and different hardware has different
\r
102 vNetworkInterfaceAllocateRAMToBuffers( xNetworkBuffers );
\r
103 for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )
\r
105 /* Initialise and set the owner of the buffer list items. */
\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 xNetworkBufferDescriptor_t *pxNetworkBufferGet( size_t xRequestedSizeBytes, portTickType xBlockTimeTicks )
\r
130 xNetworkBufferDescriptor_t *pxReturn = NULL;
\r
132 /*_RB_ The current implementation only has a single size memory block, so
\r
133 the requested size parameter is not used (yet). */
\r
134 ( void ) xRequestedSizeBytes;
\r
136 /* If there is a semaphore available, there is a network buffer available. */
\r
137 if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )
\r
139 /* Protect the structure as it is accessed from tasks and interrupts. */
\r
140 taskENTER_CRITICAL();
\r
142 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
\r
143 uxListRemove( &( pxReturn->xBufferListItem ) );
\r
145 taskEXIT_CRITICAL();
\r
146 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
\r
150 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();
\r
155 /*-----------------------------------------------------------*/
\r
157 xNetworkBufferDescriptor_t *pxNetworkBufferGetFromISR( size_t xRequestedSizeBytes )
\r
159 xNetworkBufferDescriptor_t *pxReturn = NULL;
\r
160 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
162 /*_RB_ The current implementation only has a single size memory block, so
\r
163 the requested size parameter is not used (yet). */
\r
164 ( void ) xRequestedSizeBytes;
\r
166 /* If there is a semaphore available then there is a buffer available, but,
\r
167 as this is called from an interrupt, only take a buffer if there are at
\r
168 least ipINTERRUPT_BUFFER_GET_THRESHOLD buffers remaining. This prevents,
\r
169 to a certain degree at least, a rapidly executing interrupt exhausting
\r
170 buffer and in so doing preventing tasks from continuing. */
\r
171 if( uxQueueMessagesWaitingFromISR( ( xQueueHandle ) xNetworkBufferSemaphore ) > ipINTERRUPT_BUFFER_GET_THRESHOLD )
\r
173 if( xSemaphoreTakeFromISR( xNetworkBufferSemaphore, NULL ) == pdPASS )
\r
175 /* Protect the structure as it is accessed from tasks and interrupts. */
\r
176 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
178 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
\r
179 uxListRemove( &( pxReturn->xBufferListItem ) );
\r
181 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
183 iptraceNETWORK_BUFFER_OBTAINED_FROM_ISR( pxReturn );
\r
187 if( pxReturn == NULL )
\r
189 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER_FROM_ISR();
\r
194 /*-----------------------------------------------------------*/
\r
196 portBASE_TYPE vNetworkBufferReleaseFromISR( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
198 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
199 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
201 /* Ensure the buffer is returned to the list of free buffers before the
\r
202 counting semaphore is 'given' to say a buffer is available. */
\r
203 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
205 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
207 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
209 xSemaphoreGiveFromISR( xNetworkBufferSemaphore, &xHigherPriorityTaskWoken );
\r
210 iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
\r
212 return xHigherPriorityTaskWoken;
\r
214 /*-----------------------------------------------------------*/
\r
216 void vNetworkBufferRelease( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
218 portBASE_TYPE xListItemAlreadyInFreeList;
\r
220 /* Ensure the buffer is returned to the list of free buffers before the
\r
221 counting semaphore is 'given' to say a buffer is available. */
\r
222 taskENTER_CRITICAL();
\r
224 xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
226 if( xListItemAlreadyInFreeList == pdFALSE )
\r
228 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
231 configASSERT( xListItemAlreadyInFreeList == pdFALSE );
\r
233 taskEXIT_CRITICAL();
\r
235 xSemaphoreGive( xNetworkBufferSemaphore );
\r
236 iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
\r
238 /*-----------------------------------------------------------*/
\r
240 #if( ipconfigINCLUDE_TEST_CODE == 1 )
\r
242 unsigned portBASE_TYPE uxGetNumberOfFreeNetworkBuffers( void )
\r
244 return listCURRENT_LIST_LENGTH( &xFreeBuffersList );
\r
247 #endif /* ipconfigINCLUDE_TEST_CODE */
\r