2 * FreeRTOS+UDP V1.0.4
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 /******************************************************************************
\r
32 * See the following web page for essential buffer allocation scheme usage and
\r
33 * configuration details:
\r
34 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Buffer_Management.shtml
\r
36 ******************************************************************************/
\r
39 /* Standard includes. */
\r
42 /* FreeRTOS includes. */
\r
43 #include "FreeRTOS.h"
\r
47 /* FreeRTOS+UDP includes. */
\r
48 #include "FreeRTOS_UDP_IP.h"
\r
49 #include "FreeRTOS_IP_Private.h"
\r
50 #include "NetworkInterface.h"
\r
52 /* For an Ethernet interrupt to be able to obtain a network buffer there must
\r
53 be at least this number of buffers available. */
\r
54 #define ipINTERRUPT_BUFFER_GET_THRESHOLD ( 3 )
\r
56 /* A list of free (available) xNetworkBufferDescriptor_t structures. */
\r
57 static xList xFreeBuffersList;
\r
59 /* Declares the pool of xNetworkBufferDescriptor_t structures that are available to the
\r
60 system. All the network buffers referenced from xFreeBuffersList exist in this
\r
61 array. The array is not accessed directly except during initialisation, when
\r
62 the xFreeBuffersList is filled (as all the buffers are free when the system is
\r
64 static xNetworkBufferDescriptor_t xNetworkBuffers[ ipconfigNUM_NETWORK_BUFFERS ];
\r
66 /* The semaphore used to obtain network buffers. */
\r
67 static xSemaphoreHandle xNetworkBufferSemaphore = NULL;
\r
69 /*-----------------------------------------------------------*/
\r
71 BaseType_t xNetworkBuffersInitialise( void )
\r
73 BaseType_t xReturn, x;
\r
75 /* Only initialise the buffers and their associated kernel objects if they
\r
76 have not been initialised before. */
\r
77 if( xNetworkBufferSemaphore == NULL )
\r
79 xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFERS, ipconfigNUM_NETWORK_BUFFERS );
\r
80 configASSERT( xNetworkBufferSemaphore );
\r
82 if( xNetworkBufferSemaphore != NULL )
\r
84 vListInitialise( &xFreeBuffersList );
\r
86 /* Initialise all the network buffers. The buffer storage comes
\r
87 from the network interface, and different hardware has different
\r
89 vNetworkInterfaceAllocateRAMToBuffers( xNetworkBuffers );
\r
90 for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )
\r
92 /* Initialise and set the owner of the buffer list items. */
\r
93 vListInitialiseItem( &( xNetworkBuffers[ x ].xBufferListItem ) );
\r
94 listSET_LIST_ITEM_OWNER( &( xNetworkBuffers[ x ].xBufferListItem ), &xNetworkBuffers[ x ] );
\r
96 /* Currently, all buffers are available for use. */
\r
97 vListInsert( &xFreeBuffersList, &( xNetworkBuffers[ x ].xBufferListItem ) );
\r
102 if( xNetworkBufferSemaphore == NULL )
\r
113 /*-----------------------------------------------------------*/
\r
115 xNetworkBufferDescriptor_t *pxNetworkBufferGet( size_t xRequestedSizeBytes, TickType_t xBlockTimeTicks )
\r
117 xNetworkBufferDescriptor_t *pxReturn = NULL;
\r
119 /*_RB_ The current implementation only has a single size memory block, so
\r
120 the requested size parameter is not used (yet). */
\r
121 ( void ) xRequestedSizeBytes;
\r
123 /* If there is a semaphore available, there is a network buffer available. */
\r
124 if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )
\r
126 /* Protect the structure as it is accessed from tasks and interrupts. */
\r
127 taskENTER_CRITICAL();
\r
129 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
\r
130 uxListRemove( &( pxReturn->xBufferListItem ) );
\r
132 taskEXIT_CRITICAL();
\r
133 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
\r
137 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();
\r
142 /*-----------------------------------------------------------*/
\r
144 xNetworkBufferDescriptor_t *pxNetworkBufferGetFromISR( size_t xRequestedSizeBytes )
\r
146 xNetworkBufferDescriptor_t *pxReturn = NULL;
\r
147 UBaseType_t uxSavedInterruptStatus;
\r
149 /*_RB_ The current implementation only has a single size memory block, so
\r
150 the requested size parameter is not used (yet). */
\r
151 ( void ) xRequestedSizeBytes;
\r
153 /* If there is a semaphore available then there is a buffer available, but,
\r
154 as this is called from an interrupt, only take a buffer if there are at
\r
155 least ipINTERRUPT_BUFFER_GET_THRESHOLD buffers remaining. This prevents,
\r
156 to a certain degree at least, a rapidly executing interrupt exhausting
\r
157 buffer and in so doing preventing tasks from continuing. */
\r
158 if( uxQueueMessagesWaitingFromISR( ( xQueueHandle ) xNetworkBufferSemaphore ) > ipINTERRUPT_BUFFER_GET_THRESHOLD )
\r
160 if( xSemaphoreTakeFromISR( xNetworkBufferSemaphore, NULL ) == pdPASS )
\r
162 /* Protect the structure as it is accessed from tasks and interrupts. */
\r
163 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
165 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
\r
166 uxListRemove( &( pxReturn->xBufferListItem ) );
\r
168 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
170 iptraceNETWORK_BUFFER_OBTAINED_FROM_ISR( pxReturn );
\r
174 if( pxReturn == NULL )
\r
176 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER_FROM_ISR();
\r
181 /*-----------------------------------------------------------*/
\r
183 BaseType_t vNetworkBufferReleaseFromISR( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
185 UBaseType_t uxSavedInterruptStatus;
\r
186 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
188 /* Ensure the buffer is returned to the list of free buffers before the
\r
189 counting semaphore is 'given' to say a buffer is available. */
\r
190 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
192 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
194 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
196 xSemaphoreGiveFromISR( xNetworkBufferSemaphore, &xHigherPriorityTaskWoken );
\r
197 iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
\r
199 return xHigherPriorityTaskWoken;
\r
201 /*-----------------------------------------------------------*/
\r
203 void vNetworkBufferRelease( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
205 BaseType_t xListItemAlreadyInFreeList;
\r
207 /* Ensure the buffer is returned to the list of free buffers before the
\r
208 counting semaphore is 'given' to say a buffer is available. */
\r
209 taskENTER_CRITICAL();
\r
211 xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
213 if( xListItemAlreadyInFreeList == pdFALSE )
\r
215 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
218 configASSERT( xListItemAlreadyInFreeList == pdFALSE );
\r
220 taskEXIT_CRITICAL();
\r
222 xSemaphoreGive( xNetworkBufferSemaphore );
\r
223 iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
\r
225 /*-----------------------------------------------------------*/
\r
227 #if( ipconfigINCLUDE_TEST_CODE == 1 )
\r
229 UBaseType_t uxGetNumberOfFreeNetworkBuffers( void )
\r
231 return listCURRENT_LIST_LENGTH( &xFreeBuffersList );
\r
234 #endif /* ipconfigINCLUDE_TEST_CODE */
\r