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