2 FreeRTOS V8.0.0 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /* Standard includes. */
\r
70 /* FreeRTOS includes. */
\r
71 #include <FreeRTOS.h>
\r
76 /* Demo application includes. */
\r
77 #include "FreeRTOS_UDP_IP.h"
\r
78 #include "FreeRTOS_Sockets.h"
\r
79 #include "SimpleClientAndServer.h"
\r
80 #include "TwoEchoClients.h"
\r
81 #include "UDPCommandInterpreter.h"
\r
82 #include "SelectServer.h"
\r
84 /* UDP command server task parameters. */
\r
85 #define mainUDP_CLI_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
86 #define mainUDP_CLI_PORT_NUMBER ( 5001UL )
\r
87 #define mainUDP_CLI_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE )
\r
89 /* Simple UDP client and server task parameters. */
\r
90 #define mainSIMPLE_CLIENT_SERVER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
91 #define mainSIMPLE_CLIENT_SERVER_PORT ( 5005UL )
\r
92 #define mainSIMPLE_CLIENT_SERVER_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE )
\r
94 /* Select UDP server task parameters. */
\r
95 #define mainSELECT_SERVER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
96 #define mainSELECT_SERVER_PORT ( 10001UL )
\r
97 #define mainSELECT_SERVER_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE )
\r
99 /* Echo client task parameters. */
\r
100 #define mainECHO_CLIENT_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE * 2 )
\r
101 #define mainECHO_CLIENT_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
103 /* Set the following constants to 1 or 0 to define which tasks to include and
\r
105 #define mainCREATE_UDP_CLI_TASKS 1
\r
106 #define mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS 0
\r
107 #define mainCREATE_SELECT_UDP_SERVER_TASKS 0
\r
108 #define mainCREATE_UDP_ECHO_TASKS 1
\r
110 /*-----------------------------------------------------------*/
\r
113 * Register commands that can be used with FreeRTOS+CLI through the UDP socket.
\r
114 * The commands are defined in CLI-commands.c.
\r
116 extern void vRegisterCLICommands( void );
\r
118 /* The default IP and MAC address used by the demo. The address configuration
\r
119 defined here will be used if ipconfigUSE_DHCP is 0, or if ipconfigUSE_DHCP is
\r
120 1 but a DHCP server could not be contacted. See the online documentation for
\r
121 more information. */
\r
122 static const uint8_t ucIPAddress[ 4 ] = { configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 };
\r
123 static const uint8_t ucNetMask[ 4 ] = { configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 };
\r
124 static const uint8_t ucGatewayAddress[ 4 ] = { configGATEWAY_ADDR0, configGATEWAY_ADDR1, configGATEWAY_ADDR2, configGATEWAY_ADDR3 };
\r
125 static const uint8_t ucDNSServerAddress[ 4 ] = { configDNS_SERVER_ADDR0, configDNS_SERVER_ADDR1, configDNS_SERVER_ADDR2, configDNS_SERVER_ADDR3 };
\r
127 /* Default MAC address configuration. The demo creates a virtual network
\r
128 connection that uses this MAC address by accessing the raw Ethernet data
\r
129 to and from a real network connection on the host PC. See the
\r
130 configNETWORK_INTERFACE_TO_USE definition for information on how to configure
\r
131 the real network connection to use. */
\r
132 const uint8_t ucMACAddress[ 6 ] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };
\r
134 /* Used to guard prints to the console. */
\r
135 static xSemaphoreHandle xConsoleMutex = NULL;
\r
137 /*-----------------------------------------------------------*/
\r
141 /******************************************************************************
\r
143 * See the following web page for information on using this demo.
\r
144 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/RTOS_UDP_CLI_Windows_Simulator.shtml
\r
146 ******************************************************************************/
\r
151 const uint32_t ulLongTime_ms = 250UL;
\r
153 /* Create a mutex that is used to guard against the console being accessed
\r
154 by more than one task simultaniously. */
\r
155 xConsoleMutex = xSemaphoreCreateMutex();
\r
157 /* Initialise the network interface. Tasks that use the network are
\r
158 created in the network event hook when the network is connected and ready
\r
159 for use. The address values passed in here are used if ipconfigUSE_DHCP is
\r
160 set to 0, or if ipconfigUSE_DHCP is set to 1 but a DHCP server cannot be
\r
162 FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );
\r
164 /* Register commands with the FreeRTOS+CLI command interpreter. */
\r
165 vRegisterCLICommands();
\r
167 /* Start the RTOS scheduler. */
\r
168 vTaskStartScheduler();
\r
170 /* If all is well, the scheduler will now be running, and the following
\r
171 line will never be reached. If the following line does execute, then
\r
172 there was insufficient FreeRTOS heap memory available for the idle and/or
\r
173 timer tasks to be created. See the memory management section on the
\r
174 FreeRTOS web site for more details (this is standard text that is not not
\r
175 really applicable to the Win32 simulator port). */
\r
178 Sleep( ulLongTime_ms );
\r
181 /*-----------------------------------------------------------*/
\r
183 void vApplicationIdleHook( void )
\r
185 const unsigned long ulMSToSleep = 5;
\r
187 /* This function is called on each cycle of the idle task if
\r
188 configUSE_IDLE_HOOK is set to 1 in FreeRTOSConfig.h. Sleep to reduce CPU
\r
190 Sleep( ulMSToSleep );
\r
192 /*-----------------------------------------------------------*/
\r
194 void vAssertCalled( void )
\r
196 const unsigned long ulLongSleep = 1000UL;
\r
197 volatile uint32_t ulBlockVariable = 0UL;
\r
199 /* Setting ulBlockVariable to a non-zero value in the debugger will allow
\r
200 this function to be exited. */
\r
201 taskDISABLE_INTERRUPTS();
\r
203 while( ulBlockVariable == 0UL )
\r
205 Sleep( ulLongSleep );
\r
208 taskENABLE_INTERRUPTS();
\r
210 /*-----------------------------------------------------------*/
\r
212 void vOutputString( char *pcMessage )
\r
214 /* Wrap the standard windows console output (as opposed to the FreeRTOS+CLI
\r
215 console) with a mutex to ensure it can only be accessed by one task at a
\r
217 xSemaphoreTake( xConsoleMutex, portMAX_DELAY );
\r
218 printf( pcMessage );
\r
219 xSemaphoreGive( xConsoleMutex );
\r
221 /*-----------------------------------------------------------*/
\r
223 /* Called by FreeRTOS+UDP when the network connects. */
\r
224 void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )
\r
226 uint32_t ulIPAddress, ulNetMask, ulGatewayAddress, ulDNSServerAddress;
\r
227 int8_t cBuffer[ 16 ];
\r
228 static portBASE_TYPE xTasksAlreadyCreated = pdFALSE;
\r
230 if( eNetworkEvent == eNetworkUp )
\r
232 /* Create the tasks that use the IP stack if they have not already been
\r
234 if( xTasksAlreadyCreated == pdFALSE )
\r
236 #if( mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS == 1 )
\r
238 /* Create tasks that demonstrate sending and receiving in both
\r
239 standard and zero copy mode. */
\r
240 vStartSimpleUDPClientServerTasks( mainSIMPLE_CLIENT_SERVER_TASK_STACK_SIZE, mainSIMPLE_CLIENT_SERVER_PORT, mainSIMPLE_CLIENT_SERVER_TASK_PRIORITY );
\r
242 #endif /* mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS */
\r
244 #if( mainCREATE_SELECT_UDP_SERVER_TASKS == 1 )
\r
246 /* Create tasks that demonstrate sending and receiving in both
\r
247 standard and zero copy mode. */
\r
248 vStartSelectUDPServerTasks( mainSELECT_SERVER_TASK_STACK_SIZE, mainSELECT_SERVER_PORT, mainSELECT_SERVER_TASK_PRIORITY );
\r
250 #endif /* mainCREATE_SIMPLE_UDP_CLIENT_SERVER_TASKS */
\r
253 #if( mainCREATE_UDP_ECHO_TASKS == 1 )
\r
255 /* Create the tasks that transmit to and receive from a standard
\r
256 echo server (see the web documentation for this port) in both
\r
257 standard and zero copy mode. */
\r
258 vStartEchoClientTasks( mainECHO_CLIENT_TASK_STACK_SIZE, mainECHO_CLIENT_TASK_PRIORITY );
\r
260 #endif /* mainCREATE_UDP_ECHO_TASKS */
\r
262 #if( mainCREATE_UDP_CLI_TASKS == 1 )
\r
264 /* Create the task that handles the CLI on a UDP port. The port number
\r
265 is set using the configUDP_CLI_PORT_NUMBER setting in FreeRTOSConfig.h. */
\r
266 vStartUDPCommandInterpreterTask( mainUDP_CLI_TASK_STACK_SIZE, mainUDP_CLI_PORT_NUMBER, mainUDP_CLI_TASK_PRIORITY );
\r
268 #endif /* mainCREATE_UDP_CLI_TASKS */
\r
270 xTasksAlreadyCreated = pdTRUE;
\r
273 /* Print out the network configuration, which may have come from a DHCP
\r
275 FreeRTOS_GetAddressConfiguration( &ulIPAddress, &ulNetMask, &ulGatewayAddress, &ulDNSServerAddress );
\r
276 vOutputString( "IP Address: " );
\r
277 FreeRTOS_inet_ntoa( ulIPAddress, cBuffer );
\r
278 vOutputString( ( char * ) cBuffer );
\r
279 vOutputString( "\r\nSubnet Mask: " );
\r
280 FreeRTOS_inet_ntoa( ulNetMask, cBuffer );
\r
281 vOutputString( ( char * ) cBuffer );
\r
282 vOutputString( "\r\nGateway Address: " );
\r
283 FreeRTOS_inet_ntoa( ulGatewayAddress, cBuffer );
\r
284 vOutputString( ( char * ) cBuffer );
\r
285 vOutputString( "\r\nDNS Server Address: " );
\r
286 FreeRTOS_inet_ntoa( ulDNSServerAddress, cBuffer );
\r
287 vOutputString( ( char * ) cBuffer );
\r
288 vOutputString( "\r\n\r\n" );
\r
291 /*-----------------------------------------------------------*/
\r
293 /* Called automatically when a reply to an outgoing ping is received. */
\r
294 void vApplicationPingReplyHook( ePingReplyStatus_t eStatus, uint16_t usIdentifier )
\r
296 static const uint8_t *pcSuccess = ( uint8_t * ) "Ping reply received - ";
\r
297 static const uint8_t *pcInvalidChecksum = ( uint8_t * ) "Ping reply received with invalid checksum - ";
\r
298 static const uint8_t *pcInvalidData = ( uint8_t * ) "Ping reply received with invalid data - ";
\r
299 static uint8_t cMessage[ 50 ];
\r
305 vOutputString( ( char * ) pcSuccess );
\r
308 case eInvalidChecksum :
\r
309 vOutputString( ( char * ) pcInvalidChecksum );
\r
312 case eInvalidData :
\r
313 vOutputString( ( char * ) pcInvalidData );
\r
317 /* It is not possible to get here as all enums have their own
\r
322 sprintf( ( char * ) cMessage, "identifier %d\r\n", ( int ) usIdentifier );
\r
323 vOutputString( ( char * ) cMessage );
\r
325 /*-----------------------------------------------------------*/
\r
327 void vApplicationMallocFailedHook( void )
\r
329 /* vApplicationMallocFailedHook() will only be called if
\r
330 configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
331 function that will get called if a call to pvPortMalloc() fails.
\r
332 pvPortMalloc() is called internally by the kernel whenever a task, queue,
\r
333 timer or semaphore is created. It is also called by various parts of the
\r
334 demo application. If heap_1.c, heap_2.c or heap_4.c are used, then the
\r
335 size of the heap available to pvPortMalloc() is defined by
\r
336 configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()
\r
337 API function can be used to query the size of free heap space that remains
\r
338 (although it does not provide information on how the remaining heap might
\r
340 taskDISABLE_INTERRUPTS();
\r