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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 /******************************************************************************
\r
31 * See the following web page for essential buffer allocation scheme usage and
\r
32 * configuration details:
\r
33 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Buffer_Management.shtml
\r
35 ******************************************************************************/
\r
38 /* Standard includes. */
\r
41 /* FreeRTOS includes. */
\r
42 #include "FreeRTOS.h"
\r
46 /* FreeRTOS+UDP includes. */
\r
47 #include "FreeRTOS_UDP_IP.h"
\r
48 #include "FreeRTOS_IP_Private.h"
\r
49 #include "NetworkInterface.h"
\r
51 /* For an Ethernet interrupt to be able to obtain a network buffer there must
\r
52 be at least this number of buffers available. */
\r
53 #define ipINTERRUPT_BUFFER_GET_THRESHOLD ( 3 )
\r
55 /* A list of free (available) xNetworkBufferDescriptor_t structures. */
\r
56 static xList xFreeBuffersList;
\r
58 /* Declares the pool of xNetworkBufferDescriptor_t structures that are available to the
\r
59 system. All the network buffers referenced from xFreeBuffersList exist in this
\r
60 array. The array is not accessed directly except during initialisation, when
\r
61 the xFreeBuffersList is filled (as all the buffers are free when the system is
\r
63 static xNetworkBufferDescriptor_t xNetworkBuffers[ ipconfigNUM_NETWORK_BUFFERS ];
\r
65 /* The semaphore used to obtain network buffers. */
\r
66 static xSemaphoreHandle xNetworkBufferSemaphore = NULL;
\r
68 /*-----------------------------------------------------------*/
\r
70 BaseType_t xNetworkBuffersInitialise( void )
\r
72 BaseType_t xReturn, x;
\r
74 /* Only initialise the buffers and their associated kernel objects if they
\r
75 have not been initialised before. */
\r
76 if( xNetworkBufferSemaphore == NULL )
\r
78 xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFERS, ipconfigNUM_NETWORK_BUFFERS );
\r
79 configASSERT( xNetworkBufferSemaphore );
\r
81 if( xNetworkBufferSemaphore != NULL )
\r
83 vListInitialise( &xFreeBuffersList );
\r
85 /* Initialise all the network buffers. The buffer storage comes
\r
86 from the network interface, and different hardware has different
\r
88 vNetworkInterfaceAllocateRAMToBuffers( xNetworkBuffers );
\r
89 for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )
\r
91 /* Initialise and set the owner of the buffer list items. */
\r
92 vListInitialiseItem( &( xNetworkBuffers[ x ].xBufferListItem ) );
\r
93 listSET_LIST_ITEM_OWNER( &( xNetworkBuffers[ x ].xBufferListItem ), &xNetworkBuffers[ x ] );
\r
95 /* Currently, all buffers are available for use. */
\r
96 vListInsert( &xFreeBuffersList, &( xNetworkBuffers[ x ].xBufferListItem ) );
\r
101 if( xNetworkBufferSemaphore == NULL )
\r
112 /*-----------------------------------------------------------*/
\r
114 xNetworkBufferDescriptor_t *pxNetworkBufferGet( size_t xRequestedSizeBytes, TickType_t xBlockTimeTicks )
\r
116 xNetworkBufferDescriptor_t *pxReturn = NULL;
\r
118 /*_RB_ The current implementation only has a single size memory block, so
\r
119 the requested size parameter is not used (yet). */
\r
120 ( void ) xRequestedSizeBytes;
\r
122 /* If there is a semaphore available, there is a network buffer available. */
\r
123 if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )
\r
125 /* Protect the structure as it is accessed from tasks and interrupts. */
\r
126 taskENTER_CRITICAL();
\r
128 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
\r
129 uxListRemove( &( pxReturn->xBufferListItem ) );
\r
131 taskEXIT_CRITICAL();
\r
132 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
\r
136 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();
\r
141 /*-----------------------------------------------------------*/
\r
143 xNetworkBufferDescriptor_t *pxNetworkBufferGetFromISR( size_t xRequestedSizeBytes )
\r
145 xNetworkBufferDescriptor_t *pxReturn = NULL;
\r
146 UBaseType_t uxSavedInterruptStatus;
\r
148 /*_RB_ The current implementation only has a single size memory block, so
\r
149 the requested size parameter is not used (yet). */
\r
150 ( void ) xRequestedSizeBytes;
\r
152 /* If there is a semaphore available then there is a buffer available, but,
\r
153 as this is called from an interrupt, only take a buffer if there are at
\r
154 least ipINTERRUPT_BUFFER_GET_THRESHOLD buffers remaining. This prevents,
\r
155 to a certain degree at least, a rapidly executing interrupt exhausting
\r
156 buffer and in so doing preventing tasks from continuing. */
\r
157 if( uxQueueMessagesWaitingFromISR( ( xQueueHandle ) xNetworkBufferSemaphore ) > ipINTERRUPT_BUFFER_GET_THRESHOLD )
\r
159 if( xSemaphoreTakeFromISR( xNetworkBufferSemaphore, NULL ) == pdPASS )
\r
161 /* Protect the structure as it is accessed from tasks and interrupts. */
\r
162 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
164 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
\r
165 uxListRemove( &( pxReturn->xBufferListItem ) );
\r
167 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
169 iptraceNETWORK_BUFFER_OBTAINED_FROM_ISR( pxReturn );
\r
173 if( pxReturn == NULL )
\r
175 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER_FROM_ISR();
\r
180 /*-----------------------------------------------------------*/
\r
182 BaseType_t vNetworkBufferReleaseFromISR( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
184 UBaseType_t uxSavedInterruptStatus;
\r
185 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
187 /* Ensure the buffer is returned to the list of free buffers before the
\r
188 counting semaphore is 'given' to say a buffer is available. */
\r
189 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
191 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
193 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
195 xSemaphoreGiveFromISR( xNetworkBufferSemaphore, &xHigherPriorityTaskWoken );
\r
196 iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
\r
198 return xHigherPriorityTaskWoken;
\r
200 /*-----------------------------------------------------------*/
\r
202 void vNetworkBufferRelease( xNetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
204 BaseType_t xListItemAlreadyInFreeList;
\r
206 /* Ensure the buffer is returned to the list of free buffers before the
\r
207 counting semaphore is 'given' to say a buffer is available. */
\r
208 taskENTER_CRITICAL();
\r
210 xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
212 if( xListItemAlreadyInFreeList == pdFALSE )
\r
214 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
217 configASSERT( xListItemAlreadyInFreeList == pdFALSE );
\r
219 taskEXIT_CRITICAL();
\r
221 xSemaphoreGive( xNetworkBufferSemaphore );
\r
222 iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
\r
224 /*-----------------------------------------------------------*/
\r
226 #if( ipconfigINCLUDE_TEST_CODE == 1 )
\r
228 UBaseType_t uxGetNumberOfFreeNetworkBuffers( void )
\r
230 return listCURRENT_LIST_LENGTH( &xFreeBuffersList );
\r
233 #endif /* ipconfigINCLUDE_TEST_CODE */
\r