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 "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
96 if( xNetworkBufferSemaphore != NULL )
\r
98 vListInitialise( &xFreeBuffersList );
\r
100 /* Initialise all the network buffers. The buffer storage comes
\r
101 from the network interface, and different hardware has different
\r
103 vNetworkInterfaceAllocateRAMToBuffers( xNetworkBuffers );
\r
104 for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )
\r
106 /* Initialise and set the owner of the buffer list items. */
\r
107 vListInitialiseItem( &( xNetworkBuffers[ x ].xBufferListItem ) );
\r
108 listSET_LIST_ITEM_OWNER( &( xNetworkBuffers[ x ].xBufferListItem ), &xNetworkBuffers[ x ] );
\r
110 /* Currently, all buffers are available for use. */
\r
111 vListInsert( &xFreeBuffersList, &( xNetworkBuffers[ x ].xBufferListItem ) );
\r
116 if( xNetworkBufferSemaphore == NULL )
\r
127 /*-----------------------------------------------------------*/
\r
129 xNetworkBufferDescriptor_t *pxNetworkBufferGet( size_t xRequestedSizeBytes, portTickType xBlockTimeTicks )
\r
131 xNetworkBufferDescriptor_t *pxReturn = NULL;
\r
133 /*_RB_ The current implementation only has a single size memory block, so
\r
134 the requested size parameter is not used (yet). */
\r
135 ( void ) xRequestedSizeBytes;
\r
137 /* If there is a semaphore available, there is a network buffer available. */
\r
138 if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )
\r
140 /* Protect the structure as it is accessed from tasks and interrupts. */
\r
141 taskENTER_CRITICAL();
\r
143 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
\r
144 uxListRemove( &( pxReturn->xBufferListItem ) );
\r
146 taskEXIT_CRITICAL();
\r
147 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
\r
151 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();
\r
156 /*-----------------------------------------------------------*/
\r
158 xNetworkBufferDescriptor_t *pxNetworkBufferGetFromISR( size_t xRequestedSizeBytes )
\r
160 xNetworkBufferDescriptor_t *pxReturn = NULL;
\r
161 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
163 /*_RB_ The current implementation only has a single size memory block, so
\r
164 the requested size parameter is not used (yet). */
\r
165 ( void ) xRequestedSizeBytes;
\r
167 /* If there is a semaphore available then there is a buffer available, but,
\r
168 as this is called from an interrupt, only take a buffer if there are at
\r
169 least ipINTERRUPT_BUFFER_GET_THRESHOLD buffers remaining. This prevents,
\r
170 to a certain degree at least, a rapidly executing interrupt exhausting
\r
171 buffer and in so doing preventing tasks from continuing. */
\r
172 if( uxQueueMessagesWaitingFromISR( ( xQueueHandle ) xNetworkBufferSemaphore ) > ipINTERRUPT_BUFFER_GET_THRESHOLD )
\r
174 if( xSemaphoreTakeFromISR( xNetworkBufferSemaphore, NULL ) == pdPASS )
\r
176 /* Protect the structure as it is accessed from tasks and interrupts. */
\r
177 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
179 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
\r
180 uxListRemove( &( pxReturn->xBufferListItem ) );
\r
182 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
184 iptraceNETWORK_BUFFER_OBTAINED_FROM_ISR( pxReturn );
\r
188 if( pxReturn == NULL )
\r
190 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER_FROM_ISR();
\r
195 /*-----------------------------------------------------------*/
\r
197 portBASE_TYPE vNetworkBufferReleaseFromISR( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
199 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
200 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
202 /* Ensure the buffer is returned to the list of free buffers before the
\r
203 counting semaphore is 'given' to say a buffer is available. */
\r
204 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
206 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
208 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
210 xSemaphoreGiveFromISR( xNetworkBufferSemaphore, &xHigherPriorityTaskWoken );
\r
211 iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
\r
213 return xHigherPriorityTaskWoken;
\r
215 /*-----------------------------------------------------------*/
\r
217 void vNetworkBufferRelease( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
219 portBASE_TYPE xListItemAlreadyInFreeList;
\r
221 /* Ensure the buffer is returned to the list of free buffers before the
\r
222 counting semaphore is 'given' to say a buffer is available. */
\r
223 taskENTER_CRITICAL();
\r
225 xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
227 if( xListItemAlreadyInFreeList == pdFALSE )
\r
229 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
232 configASSERT( xListItemAlreadyInFreeList == pdFALSE );
\r
234 taskEXIT_CRITICAL();
\r
236 xSemaphoreGive( xNetworkBufferSemaphore );
\r
237 iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
\r
239 /*-----------------------------------------------------------*/
\r
241 #if( ipconfigINCLUDE_TEST_CODE == 1 )
\r
243 unsigned portBASE_TYPE uxGetNumberOfFreeNetworkBuffers( void )
\r
245 return listCURRENT_LIST_LENGTH( &xFreeBuffersList );
\r
248 #endif /* ipconfigINCLUDE_TEST_CODE */
\r