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