2 FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 * Creates two transmitting tasks and two receiving tasks. The transmitting
\r
77 * tasks send values that are received by the receiving tasks. One set of tasks
\r
78 * uses the standard API. The other set of tasks uses the zero copy API.
\r
81 /* Standard includes. */
\r
85 /* FreeRTOS includes. */
\r
86 #include "FreeRTOS.h"
\r
89 /* FreeRTOS+UDP includes. */
\r
90 #include "FreeRTOS_UDP_IP.h"
\r
91 #include "FreeRTOS_Sockets.h"
\r
93 #define simpTINY_DELAY ( ( portTickType ) 2 )
\r
96 * Uses a socket to send data without using the zero copy option.
\r
97 * prvSimpleServerTask() will receive the data.
\r
99 static void prvSimpleClientTask( void *pvParameters );
\r
102 * Uses a socket to receive the data sent by the prvSimpleClientTask() task.
\r
103 * Does not use the zero copy option.
\r
105 static void prvSimpleServerTask( void *pvParameters );
\r
108 * Uses a socket to send data using the zero copy option.
\r
109 * prvSimpleZeroCopyServerTask() will receive the data.
\r
111 static void prvSimpleZeroCopyUDPClientTask( void *pvParameters );
\r
114 * Uses a socket to receive the data sent by the prvSimpleZeroCopyUDPClientTask()
\r
115 * task. Uses the zero copy option.
\r
117 static void prvSimpleZeroCopyServerTask( void *pvParameters );
\r
119 /*-----------------------------------------------------------*/
\r
121 void vStartSimpleUDPClientServerTasks( uint16_t usStackSize, uint32_t ulPort, unsigned portBASE_TYPE uxPriority )
\r
123 /* Create the client and server tasks that do not use the zero copy
\r
125 xTaskCreate( prvSimpleClientTask, "SimpCpyClnt", usStackSize, ( void * ) ulPort, uxPriority, NULL );
\r
126 xTaskCreate( prvSimpleServerTask, "SimpCpySrv", usStackSize, ( void * ) ulPort, uxPriority + 1, NULL );
\r
128 /* Create the client and server tasks that do use the zero copy interface. */
\r
129 xTaskCreate( prvSimpleZeroCopyUDPClientTask, "SimpZCpyClnt", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority, NULL );
\r
130 xTaskCreate( prvSimpleZeroCopyServerTask, "SimpZCpySrv", usStackSize, ( void * ) ( ulPort + 1 ), uxPriority + 1, NULL );
\r
132 /*-----------------------------------------------------------*/
\r
134 static void prvSimpleClientTask( void *pvParameters )
\r
136 xSocket_t xClientSocket;
\r
137 struct freertos_sockaddr xDestinationAddress;
\r
138 uint8_t cString[ 50 ];
\r
139 portBASE_TYPE lReturned;
\r
140 uint32_t ulCount = 0UL, ulIPAddress;
\r
141 const uint32_t ulLoopsPerSocket = 10UL;
\r
142 const portTickType x150ms = 150UL / portTICK_RATE_MS;
\r
144 /* Remove compiler warning about unused parameters. */
\r
145 ( void ) pvParameters;
\r
147 /* It is assumed that this task is not created until the network is up,
\r
148 so the IP address can be obtained immediately. store the IP address being
\r
149 used in ulIPAddress. This is done so the socket can send to a different
\r
150 port on the same IP address. */
\r
151 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
153 /* This test sends to itself, so data sent from here is received by a server
\r
154 socket on the same IP address. Setup the freertos_sockaddr structure with
\r
155 this nodes IP address, and the port number being sent to. The strange
\r
156 casting is to try and remove compiler warnings on 32 bit machines. */
\r
157 xDestinationAddress.sin_addr = ulIPAddress;
\r
158 xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
159 xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );
\r
163 /* Create the socket. */
\r
164 xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
165 configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );
\r
167 /* The count is used to differentiate between different messages sent to
\r
168 the server, and to break out of the do while loop below. */
\r
173 /* Create the string that is sent to the server. */
\r
174 sprintf( ( char * ) cString, "Server received (not zero copy): Message number %lu\r\n", ulCount );
\r
176 /* Send the string to the socket. ulFlags is set to 0, so the zero
\r
177 copy option is not selected. That means the data from cString[] is
\r
178 copied into a network buffer inside FreeRTOS_sendto(), and cString[]
\r
179 can be reused as soon as FreeRTOS_sendto() has returned. */
\r
180 lReturned = FreeRTOS_sendto( xClientSocket, ( void * ) cString, strlen( ( const char * ) cString ), 0, &xDestinationAddress, sizeof( xDestinationAddress ) );
\r
184 } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );
\r
186 FreeRTOS_closesocket( xClientSocket );
\r
188 /* A short delay to prevent the messages printed by the server task
\r
189 scrolling off the screen too quickly, and to prevent reduce the network
\r
191 vTaskDelay( x150ms );
\r
194 /*-----------------------------------------------------------*/
\r
196 static void prvSimpleServerTask( void *pvParameters )
\r
199 uint8_t cReceivedString[ 60 ];
\r
200 struct freertos_sockaddr xClient, xBindAddress;
\r
201 uint32_t xClientLength = sizeof( xClient );
\r
202 xSocket_t xListeningSocket;
\r
204 /* Just to prevent compiler warnings. */
\r
205 ( void ) pvParameters;
\r
207 /* Attempt to open the socket. */
\r
208 xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
209 configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
\r
211 /* This test receives data sent from a different port on the same IP
\r
212 address. Configure the freertos_sockaddr structure with the address being
\r
213 bound to. The strange casting is to try and remove compiler warnings on 32
\r
214 bit machines. Note that this task is only created after the network is up,
\r
215 so the IP address is valid here. */
\r
216 xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
217 xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
\r
219 /* Bind the socket to the port that the client task will send to. */
\r
220 FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
\r
224 /* Zero out the receive array so there is NULL at the end of the string
\r
225 when it is printed out. */
\r
226 memset( cReceivedString, 0x00, sizeof( cReceivedString ) );
\r
228 /* Receive data on the socket. ulFlags is zero, so the zero copy option
\r
229 is not set and the received data is copied into the buffer pointed to by
\r
230 cReceivedString. By default the block time is portMAX_DELAY.
\r
231 xClientLength is not actually used by FreeRTOS_recvfrom(), but is set
\r
232 appropriately in case future versions do use it. */
\r
233 lBytes = FreeRTOS_recvfrom( xListeningSocket, cReceivedString, sizeof( cReceivedString ), 0, &xClient, &xClientLength );
\r
235 /* Print the received characters. */
\r
238 vOutputString( ( char * ) cReceivedString );
\r
242 configASSERT( lBytes == ( portBASE_TYPE ) strlen( ( const char * ) cReceivedString ) );
\r
245 /*-----------------------------------------------------------*/
\r
247 static void prvSimpleZeroCopyUDPClientTask( void *pvParameters )
\r
249 xSocket_t xClientSocket;
\r
250 uint8_t *pucUDPPayloadBuffer;
\r
251 struct freertos_sockaddr xDestinationAddress;
\r
252 portBASE_TYPE lReturned;
\r
253 uint32_t ulCount = 0UL, ulIPAddress;
\r
254 const uint32_t ulLoopsPerSocket = 10UL;
\r
255 const uint8_t *pucStringToSend = ( const uint8_t * ) "Server received (using zero copy): Message number ";
\r
256 const portTickType x150ms = 150UL / portTICK_RATE_MS;
\r
257 /* 15 is added to ensure the number, \r\n and terminating zero fit. */
\r
258 const size_t xStringLength = strlen( ( char * ) pucStringToSend ) + 15;
\r
260 /* Remove compiler warning about unused parameters. */
\r
261 ( void ) pvParameters;
\r
263 /* It is assumed that this task is not created until the network is up,
\r
264 so the IP address can be obtained immediately. store the IP address being
\r
265 used in ulIPAddress. This is done so the socket can send to a different
\r
266 port on the same IP address. */
\r
267 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
269 /* This test sends to itself, so data sent from here is received by a server
\r
270 socket on the same IP address. Setup the freertos_sockaddr structure with
\r
271 this nodes IP address, and the port number being sent to. The strange
\r
272 casting is to try and remove compiler warnings on 32 bit machines. */
\r
273 xDestinationAddress.sin_addr = ulIPAddress;
\r
274 xDestinationAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
275 xDestinationAddress.sin_port = FreeRTOS_htons( xDestinationAddress.sin_port );
\r
279 /* Create the socket. */
\r
280 xClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
281 configASSERT( xClientSocket != FREERTOS_INVALID_SOCKET );
\r
283 /* The count is used to differentiate between different messages sent to
\r
284 the server, and to break out of the do while loop below. */
\r
289 /* This task is going to send using the zero copy interface. The
\r
290 data being sent is therefore written directly into a buffer that is
\r
291 passed into, rather than copied into, the FreeRTOS_sendto()
\r
294 First obtain a buffer of adequate length from the IP stack into which
\r
295 the string will be written. Although a max delay is used, the actual
\r
296 delay will be capped to ipconfigMAX_SEND_BLOCK_TIME_TICKS, hence
\r
297 the do while loop is used to ensure a buffer is obtained. */
\r
300 } while( ( pucUDPPayloadBuffer = ( uint8_t * ) FreeRTOS_GetUDPPayloadBuffer( xStringLength, portMAX_DELAY ) ) == NULL );
\r
302 /* A buffer was successfully obtained. Create the string that is
\r
303 sent to the server. First the string is filled with zeros as this will
\r
304 effectively be the null terminator when the string is received at the other
\r
305 end. Note that the string is being written directly into the buffer
\r
306 obtained from the IP stack above. */
\r
307 memset( ( void * ) pucUDPPayloadBuffer, 0x00, xStringLength );
\r
308 sprintf( ( char * ) pucUDPPayloadBuffer, "%s%lu\r\n", ( char * ) pucStringToSend, ulCount );
\r
310 /* Pass the buffer into the send function. ulFlags has the
\r
311 FREERTOS_ZERO_COPY bit set so the IP stack will take control of the
\r
312 buffer rather than copy data out of the buffer. */
\r
313 lReturned = FreeRTOS_sendto( xClientSocket, /* The socket being sent to. */
\r
314 ( void * ) pucUDPPayloadBuffer, /* A pointer to the the data being sent. */
\r
315 strlen( ( const char * ) pucUDPPayloadBuffer ) + 1, /* The length of the data being sent - including the string's null terminator. */
\r
316 FREERTOS_ZERO_COPY, /* ulFlags with the FREERTOS_ZERO_COPY bit set. */
\r
317 &xDestinationAddress, /* Where the data is being sent. */
\r
318 sizeof( xDestinationAddress ) );
\r
320 if( lReturned == 0 )
\r
322 /* The send operation failed, so this task is still responsible
\r
323 for the buffer obtained from the IP stack. To ensure the buffer
\r
324 is not lost it must either be used again, or, as in this case,
\r
325 returned to the IP stack using FreeRTOS_ReleaseUDPPayloadBuffer().
\r
326 pucUDPPayloadBuffer can be safely re-used after this call. */
\r
327 FreeRTOS_ReleaseUDPPayloadBuffer( ( void * ) pucUDPPayloadBuffer );
\r
331 /* The send was successful so the IP stack is now managing the
\r
332 buffer pointed to by pucUDPPayloadBuffer, and the IP stack will
\r
333 return the buffer once it has been sent. pucUDPPayloadBuffer can
\r
334 be safely re-used. */
\r
339 } while( ( lReturned != FREERTOS_SOCKET_ERROR ) && ( ulCount < ulLoopsPerSocket ) );
\r
341 FreeRTOS_closesocket( xClientSocket );
\r
343 /* A short delay to prevent the messages scrolling off the screen too
\r
345 vTaskDelay( x150ms );
\r
348 /*-----------------------------------------------------------*/
\r
350 static void prvSimpleZeroCopyServerTask( void *pvParameters )
\r
353 uint8_t *pucUDPPayloadBuffer;
\r
354 struct freertos_sockaddr xClient, xBindAddress;
\r
355 uint32_t xClientLength = sizeof( xClient ), ulIPAddress;
\r
356 xSocket_t xListeningSocket;
\r
358 /* Just to prevent compiler warnings. */
\r
359 ( void ) pvParameters;
\r
361 /* Attempt to open the socket. */
\r
362 xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
\r
363 configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
\r
365 /* This test receives data sent from a different port on the same IP address.
\r
366 Obtain the nodes IP address. Configure the freertos_sockaddr structure with
\r
367 the address being bound to. The strange casting is to try and remove
\r
368 compiler warnings on 32 bit machines. Note that this task is only created
\r
369 after the network is up, so the IP address is valid here. */
\r
370 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
371 xBindAddress.sin_addr = ulIPAddress;
\r
372 xBindAddress.sin_port = ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL;
\r
373 xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
\r
375 /* Bind the socket to the port that the client task will send to. */
\r
376 FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
\r
380 /* Receive data on the socket. ulFlags has the zero copy bit set
\r
381 (FREERTOS_ZERO_COPY) indicating to the stack that a reference to the
\r
382 received data should be passed out to this task using the second
\r
383 parameter to the FreeRTOS_recvfrom() call. When this is done the
\r
384 IP stack is no longer responsible for releasing the buffer, and
\r
385 the task *must* return the buffer to the stack when it is no longer
\r
386 needed. By default the block time is portMAX_DELAY. */
\r
387 lBytes = FreeRTOS_recvfrom( xListeningSocket, ( void * ) &pucUDPPayloadBuffer, 0, FREERTOS_ZERO_COPY, &xClient, &xClientLength );
\r
389 /* It is expected to receive one more byte than the string length as
\r
390 the NULL terminator is also transmitted. */
\r
391 configASSERT( lBytes == ( ( portBASE_TYPE ) strlen( ( const char * ) pucUDPPayloadBuffer ) + 1 ) );
\r
393 /* Print the received characters. */
\r
396 vOutputString( ( char * ) pucUDPPayloadBuffer );
\r
401 /* The buffer *must* be freed once it is no longer needed. */
\r
402 FreeRTOS_ReleaseUDPPayloadBuffer( pucUDPPayloadBuffer );
\r