]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC/main.c
e0591dc1a539d2a353269f51b9fac8a7ff3fc506
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC / main.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - 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     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 /* Standard includes. */\r
96 #include <string.h>\r
97 #include <stdio.h>\r
98 \r
99 /* FreeRTOS includes. */\r
100 #include "FreeRTOS.h"\r
101 #include "task.h"\r
102 #include "timers.h"\r
103 #include "queue.h"\r
104 \r
105 /* FreeRTOS+UDP includes. */\r
106 #include "FreeRTOS_UDP_IP.h"\r
107 #include "FreeRTOS_Sockets.h"\r
108 \r
109 /* Example includes. */\r
110 #include "TwoEchoClients.h"\r
111 #include "CDCCommandConsole.h"\r
112 \r
113 /* Library includes. */\r
114 #include "LPC18xx.h"\r
115 \r
116 /* The size of the stack and the priority used by the two echo client tasks. */\r
117 #define mainECHO_CLIENT_TASK_STACK_SIZE         ( configMINIMAL_STACK_SIZE * 2 )\r
118 #define mainECHO_CLIENT_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
119 \r
120 /* The size of the stack and the priority used by the USB CDC command console\r
121 task. */\r
122 #define mainCDC_COMMAND_CONSOLE_STACK_SIZE              ( configMINIMAL_STACK_SIZE * 2 )\r
123 #define mainCDC_COMMAND_CONSOLE_TASK_PRIORITY   ( 4U )\r
124 \r
125 /*\r
126 * Register commands that can be used with FreeRTOS+CLI.  The commands are\r
127 * defined in CLI-commands.c.\r
128 */\r
129 extern void vRegisterCLICommands( void );\r
130 \r
131 /*\r
132  * Initialise the LED ports, and create a timer that periodically toggles an LED\r
133  * just to provide a visual indication that the program is running.\r
134  */\r
135 extern void vLEDsInitialise( void );\r
136 \r
137 /*-----------------------------------------------------------*/\r
138 \r
139 /* The default IP and MAC address used by the demo.  The address configuration\r
140 defined here will be used if ipconfigUSE_DHCP is 0, or if ipconfigUSE_DHCP is\r
141 1 but a DHCP server could not be contacted.  See the online documentation for\r
142 more information. */\r
143 static const uint8_t ucIPAddress[ 4 ] = { configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 };\r
144 static const uint8_t ucNetMask[ 4 ] = { configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 };\r
145 static const uint8_t ucGatewayAddress[ 4 ] = { configGATEWAY_ADDR0, configGATEWAY_ADDR1, configGATEWAY_ADDR2, configGATEWAY_ADDR3 };\r
146 static const uint8_t ucDNSServerAddress[ 4 ] = { configDNS_SERVER_ADDR0, configDNS_SERVER_ADDR1, configDNS_SERVER_ADDR2, configDNS_SERVER_ADDR3 };\r
147 \r
148 /* The MAC address used by the demo.  In production units the MAC address would\r
149 probably be read from flash memory or an EEPROM.  Here it is just hard coded. */\r
150 const uint8_t ucMACAddress[ 6 ] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };\r
151 \r
152 /*-----------------------------------------------------------*/\r
153 \r
154 \r
155 /******************************************************************************\r
156  *\r
157  * See the following web page for information on using this demo.\r
158  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/RTOS_UDP_and_CLI_LPC1830_NGX.shtml\r
159  *\r
160  ******************************************************************************/\r
161 \r
162 \r
163 int main( void )\r
164 {\r
165         /* Prepare the trace recorder library. */\r
166         #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
167                 vTraceInitTraceData();\r
168         #endif\r
169 \r
170         /* The examples assume that all priority bits are assigned as preemption\r
171         priority bits. */\r
172         NVIC_SetPriorityGrouping( 0UL );\r
173 \r
174         /* Start the timer that just toggles an LED to show the demo is running. */\r
175         vLEDsInitialise();\r
176 \r
177         /* Start the tasks that implements the command console on the UART, as\r
178         described above. */\r
179         vCDCCommandConsoleStart( mainCDC_COMMAND_CONSOLE_STACK_SIZE, mainCDC_COMMAND_CONSOLE_TASK_PRIORITY );\r
180 \r
181         /* Register CLI commands. */\r
182         vRegisterCLICommands();\r
183 \r
184         /* Initialise the network interface.  Tasks that use the network are\r
185         created in the network event hook when the network is connected and ready\r
186         for use.  The address values passed in here are used if ipconfigUSE_DHCP is\r
187         set to 0, or if ipconfigUSE_DHCP is set to 1 but a DHCP server cannot be\r
188         contacted. */\r
189         FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );\r
190 \r
191         /* If the trace recorder code is included... */\r
192         #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
193         {\r
194                 extern xQueueHandle xNetworkEventQueue;\r
195 \r
196                 /* Name the queue for viewing in FreeRTOS+Trace. */\r
197                 vTraceSetQueueName( xNetworkEventQueue, "IPStackEvent" );\r
198         }\r
199         #endif /*  configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1 */\r
200 \r
201         /* Start the FreeRTOS scheduler. */\r
202         vTaskStartScheduler();\r
203 \r
204         /* The following line should never execute.  If it does, it means there was\r
205         insufficient FreeRTOS heap memory available to create the Idle and/or timer\r
206         tasks.  See the memory management section on the http://www.FreeRTOS.org web\r
207         site for more information. */\r
208         for( ;; );\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 void vApplicationStackOverflowHook( xTaskHandle pxTask, char *pcTaskName )\r
213 {\r
214         ( void ) pcTaskName;\r
215         ( void ) pxTask;\r
216 \r
217         /* Run time stack overflow checking is performed if\r
218         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
219         function is called if a stack overflow is detected. */\r
220         taskDISABLE_INTERRUPTS();\r
221         for( ;; );\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 void vApplicationMallocFailedHook( void )\r
226 {\r
227         /* vApplicationMallocFailedHook() will only be called if\r
228         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
229         function that will get called if a call to pvPortMalloc() fails.\r
230         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
231         timer or semaphore is created.  It is also called by various parts of the\r
232         demo application.  If heap_1.c, heap_2.c or heap_4.c are used, then the\r
233         size of the heap available to pvPortMalloc() is defined by\r
234         configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()\r
235         API function can be used to query the size of free heap space that remains\r
236         (although it does not provide information on how the remaining heap might\r
237         be fragmented). */\r
238         taskDISABLE_INTERRUPTS();\r
239         for( ;; );\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 /* Called by FreeRTOS+UDP when the network connects. */\r
244 void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )\r
245 {\r
246 static BaseType_t xTaskAlreadyCreated = pdFALSE;\r
247 \r
248         if( eNetworkEvent == eNetworkUp )\r
249         {\r
250                 /* Create the tasks that transmit to and receive from a standard\r
251                 echo server (see the web documentation for this port) in both\r
252                 standard and zero copy mode. */\r
253                 if( xTaskAlreadyCreated == pdFALSE )\r
254                 {\r
255                         vStartEchoClientTasks( mainECHO_CLIENT_TASK_STACK_SIZE, mainECHO_CLIENT_TASK_PRIORITY );\r
256                         xTaskAlreadyCreated = pdTRUE;\r
257                 }\r
258         }\r
259 }\r
260 /*-----------------------------------------------------------*/\r
261 \r
262 /* Called by FreeRTOS+UDP when a reply is received to an outgoing ping request. */\r
263 void vApplicationPingReplyHook( ePingReplyStatus_t eStatus, uint16_t usIdentifier )\r
264 {\r
265 static const char *pcSuccess = "\r\n\r\nPing reply received - ";\r
266 static const char *pcInvalidChecksum = "\r\n\r\nPing reply received with invalid checksum - ";\r
267 static const char *pcInvalidData = "\r\n\r\nPing reply received with invalid data - ";\r
268 static char cMessage[ 50 ];\r
269 void vOutputString( const char * const pcMessage );\r
270 \r
271         switch( eStatus )\r
272         {\r
273                 case eSuccess   :\r
274                         vOutputString( pcSuccess );\r
275                         break;\r
276 \r
277                 case eInvalidChecksum :\r
278                         vOutputString( pcInvalidChecksum );\r
279                         break;\r
280 \r
281                 case eInvalidData :\r
282                         vOutputString( pcInvalidData );\r
283                         break;\r
284 \r
285                 default :\r
286                         /* It is not possible to get here as all enums have their own\r
287                         case. */\r
288                         break;\r
289         }\r
290 \r
291         sprintf( cMessage, "identifier %d\r\n\r\n", ( int ) usIdentifier );\r
292         vOutputString( cMessage );\r
293 }\r
294 \r