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