]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/portable/BufferManagement/BufferAllocation_1.c
Add missing header file to the UDP BufferAllocation_1.c scheme.
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-UDP / portable / BufferManagement / BufferAllocation_1.c
1 /*\r
2  * FreeRTOS+UDP V1.0.1 (C) 2013 Real Time Engineers ltd.\r
3  * All rights reserved\r
4  *\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
7  *\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
14  *\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
21  *\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
28  *\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
35  *\r
36  * 1 tab == 4 spaces!\r
37  *\r
38  * http://www.FreeRTOS.org\r
39  * http://www.FreeRTOS.org/udp\r
40  *\r
41  */\r
42 \r
43 \r
44 /******************************************************************************\r
45  *\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
49  *\r
50  ******************************************************************************/\r
51 \r
52 \r
53 /* Standard includes. */\r
54 #include <stdint.h>\r
55 \r
56 /* FreeRTOS includes. */\r
57 #include "FreeRTOS.h"\r
58 #include "task.h"\r
59 #include "semphr.h"\r
60 \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
65 \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
69 \r
70 /* A list of free (available) xNetworkBufferDescriptor_t structures. */\r
71 static xList xFreeBuffersList;\r
72 \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
77 booted). */\r
78 static xNetworkBufferDescriptor_t xNetworkBuffers[ ipconfigNUM_NETWORK_BUFFERS ];\r
79 \r
80 /* The semaphore used to obtain network buffers. */\r
81 static xSemaphoreHandle xNetworkBufferSemaphore = NULL;\r
82 \r
83 /*-----------------------------------------------------------*/\r
84 \r
85 portBASE_TYPE xNetworkBuffersInitialise( void )\r
86 {\r
87 portBASE_TYPE xReturn, x;\r
88 \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
92         {\r
93                 xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFERS, ipconfigNUM_NETWORK_BUFFERS );\r
94                 configASSERT( xNetworkBufferSemaphore );\r
95 \r
96                 if( xNetworkBufferSemaphore != NULL )\r
97                 {\r
98                         vListInitialise( &xFreeBuffersList );\r
99 \r
100                         /* Initialise all the network buffers.  The buffer storage comes\r
101                         from the network interface, and different hardware has different\r
102                         requirements. */\r
103                         vNetworkInterfaceAllocateRAMToBuffers( xNetworkBuffers );\r
104                         for( x = 0; x < ipconfigNUM_NETWORK_BUFFERS; x++ )\r
105                         {\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
109 \r
110                                 /* Currently, all buffers are available for use. */\r
111                                 vListInsert( &xFreeBuffersList, &( xNetworkBuffers[ x ].xBufferListItem ) );\r
112                         }\r
113                 }\r
114         }\r
115 \r
116         if( xNetworkBufferSemaphore == NULL )\r
117         {\r
118                 xReturn = pdFAIL;\r
119         }\r
120         else\r
121         {\r
122                 xReturn = pdPASS;\r
123         }\r
124 \r
125         return xReturn;\r
126 }\r
127 /*-----------------------------------------------------------*/\r
128 \r
129 xNetworkBufferDescriptor_t *pxNetworkBufferGet( size_t xRequestedSizeBytes, portTickType xBlockTimeTicks )\r
130 {\r
131 xNetworkBufferDescriptor_t *pxReturn = NULL;\r
132 \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
136 \r
137         /* If there is a semaphore available, there is a network buffer available. */\r
138         if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )\r
139         {\r
140                 /* Protect the structure as it is accessed from tasks and interrupts. */\r
141                 taskENTER_CRITICAL();\r
142                 {\r
143                         pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );\r
144                         uxListRemove( &( pxReturn->xBufferListItem ) );\r
145                 }\r
146                 taskEXIT_CRITICAL();\r
147                 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );\r
148         }\r
149         else\r
150         {\r
151                 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();\r
152         }\r
153 \r
154         return pxReturn;\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 xNetworkBufferDescriptor_t *pxNetworkBufferGetFromISR( size_t xRequestedSizeBytes )\r
159 {\r
160 xNetworkBufferDescriptor_t *pxReturn = NULL;\r
161 unsigned portBASE_TYPE uxSavedInterruptStatus;\r
162 \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
166 \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
173         {\r
174                 if( xSemaphoreTakeFromISR( xNetworkBufferSemaphore, NULL ) == pdPASS )\r
175                 {\r
176                         /* Protect the structure as it is accessed from tasks and interrupts. */\r
177                         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
178                         {\r
179                                 pxReturn = ( xNetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );\r
180                                 uxListRemove( &( pxReturn->xBufferListItem ) );\r
181                         }\r
182                         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
183 \r
184                         iptraceNETWORK_BUFFER_OBTAINED_FROM_ISR( pxReturn );\r
185                 }\r
186         }\r
187 \r
188         if( pxReturn == NULL )\r
189         {\r
190                 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER_FROM_ISR();\r
191         }\r
192 \r
193         return pxReturn;\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 portBASE_TYPE vNetworkBufferReleaseFromISR( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
198 {\r
199 unsigned portBASE_TYPE uxSavedInterruptStatus;\r
200 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
201 \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
205         {\r
206                 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
207         }\r
208         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
209 \r
210         xSemaphoreGiveFromISR( xNetworkBufferSemaphore, &xHigherPriorityTaskWoken );\r
211         iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );\r
212 \r
213         return xHigherPriorityTaskWoken;\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 void vNetworkBufferRelease( xNetworkBufferDescriptor_t * const pxNetworkBuffer )\r
218 {\r
219 portBASE_TYPE xListItemAlreadyInFreeList;\r
220 \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
224         {\r
225                 xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
226 \r
227                 if( xListItemAlreadyInFreeList == pdFALSE )\r
228                 {\r
229                         vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );\r
230                 }\r
231 \r
232                 configASSERT( xListItemAlreadyInFreeList == pdFALSE );\r
233         }\r
234         taskEXIT_CRITICAL();\r
235 \r
236         xSemaphoreGive( xNetworkBufferSemaphore );\r
237         iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );\r
238 }\r
239 /*-----------------------------------------------------------*/\r
240 \r
241 #if( ipconfigINCLUDE_TEST_CODE == 1 )\r
242 \r
243 unsigned portBASE_TYPE uxGetNumberOfFreeNetworkBuffers( void )\r
244 {\r
245         return listCURRENT_LIST_LENGTH( &xFreeBuffersList );\r
246 }\r
247 \r
248 #endif /* ipconfigINCLUDE_TEST_CODE */\r