]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC/main.c
c669e8574bc13ab9c7ab63781b2a480a9ca6df39
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC / main.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\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
12      *                                                                       *\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
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \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
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \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
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \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
54 \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
58 \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
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 /* Standard includes. */\r
66 #include <string.h>\r
67 #include <stdio.h>\r
68 \r
69 /* FreeRTOS includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 #include "timers.h"\r
73 #include "queue.h"\r
74 \r
75 /* FreeRTOS+UDP includes. */\r
76 #include "FreeRTOS_UDP_IP.h"\r
77 #include "FreeRTOS_Sockets.h"\r
78 \r
79 /* Example includes. */\r
80 #include "TwoEchoClients.h"\r
81 #include "CDCCommandConsole.h"\r
82 \r
83 /* Library includes. */\r
84 #include "LPC18xx.h"\r
85 \r
86 /* The size of the stack and the priority used by the two echo client tasks. */\r
87 #define mainECHO_CLIENT_TASK_STACK_SIZE         ( configMINIMAL_STACK_SIZE * 2 )\r
88 #define mainECHO_CLIENT_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
89 \r
90 /* The size of the stack and the priority used by the USB CDC command console\r
91 task. */\r
92 #define mainCDC_COMMAND_CONSOLE_STACK_SIZE              ( configMINIMAL_STACK_SIZE * 2 )\r
93 #define mainCDC_COMMAND_CONSOLE_TASK_PRIORITY   ( 4U )\r
94 \r
95 /*\r
96 * Register commands that can be used with FreeRTOS+CLI.  The commands are\r
97 * defined in CLI-commands.c.\r
98 */\r
99 extern void vRegisterCLICommands( void );\r
100 \r
101 /*\r
102  * Initialise the LED ports, and create a timer that periodically toggles an LED\r
103  * just to provide a visual indication that the program is running.\r
104  */\r
105 extern void vLEDsInitialise( void );\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 /* The default IP and MAC address used by the demo.  The address configuration\r
110 defined here will be used if ipconfigUSE_DHCP is 0, or if ipconfigUSE_DHCP is\r
111 1 but a DHCP server could not be contacted.  See the online documentation for\r
112 more information. */\r
113 static const uint8_t ucIPAddress[ 4 ] = { configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 };\r
114 static const uint8_t ucNetMask[ 4 ] = { configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 };\r
115 static const uint8_t ucGatewayAddress[ 4 ] = { configGATEWAY_ADDR0, configGATEWAY_ADDR1, configGATEWAY_ADDR2, configGATEWAY_ADDR3 };\r
116 static const uint8_t ucDNSServerAddress[ 4 ] = { configDNS_SERVER_ADDR0, configDNS_SERVER_ADDR1, configDNS_SERVER_ADDR2, configDNS_SERVER_ADDR3 };\r
117 \r
118 /* The MAC address used by the demo.  In production units the MAC address would\r
119 probably be read from flash memory or an EEPROM.  Here it is just hard coded. */\r
120 const uint8_t ucMACAddress[ 6 ] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 \r
125 /******************************************************************************\r
126  *\r
127  * See the following web page for information on using this demo.\r
128  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/RTOS_UDP_and_CLI_LPC1830_NGX.shtml\r
129  *\r
130  ******************************************************************************/\r
131 \r
132 \r
133 int main( void )\r
134 {\r
135         /* Prepare the trace recorder library. */\r
136         #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
137                 vTraceInitTraceData();\r
138         #endif\r
139 \r
140         /* The examples assume that all priority bits are assigned as preemption\r
141         priority bits. */\r
142         NVIC_SetPriorityGrouping( 0UL );\r
143 \r
144         /* Start the timer that just toggles an LED to show the demo is running. */\r
145         vLEDsInitialise();\r
146 \r
147         /* Start the tasks that implements the command console on the UART, as\r
148         described above. */\r
149         vCDCCommandConsoleStart( mainCDC_COMMAND_CONSOLE_STACK_SIZE, mainCDC_COMMAND_CONSOLE_TASK_PRIORITY );\r
150 \r
151         /* Register CLI commands. */\r
152         vRegisterCLICommands();\r
153 \r
154         /* Initialise the network interface.  Tasks that use the network are\r
155         created in the network event hook when the network is connected and ready\r
156         for use.  The address values passed in here are used if ipconfigUSE_DHCP is\r
157         set to 0, or if ipconfigUSE_DHCP is set to 1 but a DHCP server cannot be\r
158         contacted. */\r
159         FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );\r
160 \r
161         /* If the trace recorder code is included... */\r
162         #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
163         {\r
164                 extern xQueueHandle xNetworkEventQueue;\r
165 \r
166                 /* Name the queue for viewing in FreeRTOS+Trace. */\r
167                 vTraceSetQueueName( xNetworkEventQueue, "IPStackEvent" );\r
168         }\r
169         #endif /*  configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1 */\r
170 \r
171         /* Start the FreeRTOS scheduler. */\r
172         vTaskStartScheduler();\r
173 \r
174         /* The following line should never execute.  If it does, it means there was\r
175         insufficient FreeRTOS heap memory available to create the Idle and/or timer\r
176         tasks.  See the memory management section on the http://www.FreeRTOS.org web\r
177         site for more information. */\r
178         for( ;; );\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 void vApplicationStackOverflowHook( xTaskHandle pxTask, char *pcTaskName )\r
183 {\r
184         ( void ) pcTaskName;\r
185         ( void ) pxTask;\r
186 \r
187         /* Run time stack overflow checking is performed if\r
188         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
189         function is called if a stack overflow is detected. */\r
190         taskDISABLE_INTERRUPTS();\r
191         for( ;; );\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void vApplicationMallocFailedHook( void )\r
196 {\r
197         /* vApplicationMallocFailedHook() will only be called if\r
198         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
199         function that will get called if a call to pvPortMalloc() fails.\r
200         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
201         timer or semaphore is created.  It is also called by various parts of the\r
202         demo application.  If heap_1.c, heap_2.c or heap_4.c are used, then the\r
203         size of the heap available to pvPortMalloc() is defined by\r
204         configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()\r
205         API function can be used to query the size of free heap space that remains\r
206         (although it does not provide information on how the remaining heap might\r
207         be fragmented). */\r
208         taskDISABLE_INTERRUPTS();\r
209         for( ;; );\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 /* Called by FreeRTOS+UDP when the network connects. */\r
214 void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )\r
215 {\r
216 static BaseType_t xTaskAlreadyCreated = pdFALSE;\r
217 \r
218         if( eNetworkEvent == eNetworkUp )\r
219         {\r
220                 /* Create the tasks that transmit to and receive from a standard\r
221                 echo server (see the web documentation for this port) in both\r
222                 standard and zero copy mode. */\r
223                 if( xTaskAlreadyCreated == pdFALSE )\r
224                 {\r
225                         vStartEchoClientTasks( mainECHO_CLIENT_TASK_STACK_SIZE, mainECHO_CLIENT_TASK_PRIORITY );\r
226                         xTaskAlreadyCreated = pdTRUE;\r
227                 }\r
228         }\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 /* Called by FreeRTOS+UDP when a reply is received to an outgoing ping request. */\r
233 void vApplicationPingReplyHook( ePingReplyStatus_t eStatus, uint16_t usIdentifier )\r
234 {\r
235 static const char *pcSuccess = "\r\n\r\nPing reply received - ";\r
236 static const char *pcInvalidChecksum = "\r\n\r\nPing reply received with invalid checksum - ";\r
237 static const char *pcInvalidData = "\r\n\r\nPing reply received with invalid data - ";\r
238 static char cMessage[ 50 ];\r
239 void vOutputString( const char * const pcMessage );\r
240 \r
241         switch( eStatus )\r
242         {\r
243                 case eSuccess   :\r
244                         vOutputString( pcSuccess );\r
245                         break;\r
246 \r
247                 case eInvalidChecksum :\r
248                         vOutputString( pcInvalidChecksum );\r
249                         break;\r
250 \r
251                 case eInvalidData :\r
252                         vOutputString( pcInvalidData );\r
253                         break;\r
254 \r
255                 default :\r
256                         /* It is not possible to get here as all enums have their own\r
257                         case. */\r
258                         break;\r
259         }\r
260 \r
261         sprintf( cMessage, "identifier %d\r\n\r\n", ( int ) usIdentifier );\r
262         vOutputString( cMessage );\r
263 }\r
264 \r