]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC/main.c
Update copyright date ready for tagging V10.1.0.
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC / main.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 /* Standard includes. */\r
28 #include <string.h>\r
29 #include <stdio.h>\r
30 \r
31 /* FreeRTOS includes. */\r
32 #include "FreeRTOS.h"\r
33 #include "task.h"\r
34 #include "timers.h"\r
35 #include "queue.h"\r
36 \r
37 /* FreeRTOS+UDP includes. */\r
38 #include "FreeRTOS_UDP_IP.h"\r
39 #include "FreeRTOS_Sockets.h"\r
40 \r
41 /* Example includes. */\r
42 #include "TwoEchoClients.h"\r
43 #include "CDCCommandConsole.h"\r
44 \r
45 /* Library includes. */\r
46 #include "LPC18xx.h"\r
47 \r
48 /* The size of the stack and the priority used by the two echo client tasks. */\r
49 #define mainECHO_CLIENT_TASK_STACK_SIZE         ( configMINIMAL_STACK_SIZE * 2 )\r
50 #define mainECHO_CLIENT_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
51 \r
52 /* The size of the stack and the priority used by the USB CDC command console\r
53 task. */\r
54 #define mainCDC_COMMAND_CONSOLE_STACK_SIZE              ( configMINIMAL_STACK_SIZE * 2 )\r
55 #define mainCDC_COMMAND_CONSOLE_TASK_PRIORITY   ( 4U )\r
56 \r
57 /*\r
58 * Register commands that can be used with FreeRTOS+CLI.  The commands are\r
59 * defined in CLI-commands.c.\r
60 */\r
61 extern void vRegisterCLICommands( void );\r
62 \r
63 /*\r
64  * Initialise the LED ports, and create a timer that periodically toggles an LED\r
65  * just to provide a visual indication that the program is running.\r
66  */\r
67 extern void vLEDsInitialise( void );\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /* The default IP and MAC address used by the demo.  The address configuration\r
72 defined here will be used if ipconfigUSE_DHCP is 0, or if ipconfigUSE_DHCP is\r
73 1 but a DHCP server could not be contacted.  See the online documentation for\r
74 more information. */\r
75 static const uint8_t ucIPAddress[ 4 ] = { configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 };\r
76 static const uint8_t ucNetMask[ 4 ] = { configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 };\r
77 static const uint8_t ucGatewayAddress[ 4 ] = { configGATEWAY_ADDR0, configGATEWAY_ADDR1, configGATEWAY_ADDR2, configGATEWAY_ADDR3 };\r
78 static const uint8_t ucDNSServerAddress[ 4 ] = { configDNS_SERVER_ADDR0, configDNS_SERVER_ADDR1, configDNS_SERVER_ADDR2, configDNS_SERVER_ADDR3 };\r
79 \r
80 /* The MAC address used by the demo.  In production units the MAC address would\r
81 probably be read from flash memory or an EEPROM.  Here it is just hard coded. */\r
82 const uint8_t ucMACAddress[ 6 ] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };\r
83 \r
84 /*-----------------------------------------------------------*/\r
85 \r
86 \r
87 /******************************************************************************\r
88  *\r
89  * See the following web page for information on using this demo.\r
90  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_UDP/Embedded_Ethernet_Examples/RTOS_UDP_and_CLI_LPC1830_NGX.shtml\r
91  *\r
92  ******************************************************************************/\r
93 \r
94 \r
95 int main( void )\r
96 {\r
97         /* Prepare the trace recorder library. */\r
98         #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
99                 vTraceInitTraceData();\r
100         #endif\r
101 \r
102         /* The examples assume that all priority bits are assigned as preemption\r
103         priority bits. */\r
104         NVIC_SetPriorityGrouping( 0UL );\r
105 \r
106         /* Start the timer that just toggles an LED to show the demo is running. */\r
107         vLEDsInitialise();\r
108 \r
109         /* Start the tasks that implements the command console on the UART, as\r
110         described above. */\r
111         vCDCCommandConsoleStart( mainCDC_COMMAND_CONSOLE_STACK_SIZE, mainCDC_COMMAND_CONSOLE_TASK_PRIORITY );\r
112 \r
113         /* Register CLI commands. */\r
114         vRegisterCLICommands();\r
115 \r
116         /* Initialise the network interface.  Tasks that use the network are\r
117         created in the network event hook when the network is connected and ready\r
118         for use.  The address values passed in here are used if ipconfigUSE_DHCP is\r
119         set to 0, or if ipconfigUSE_DHCP is set to 1 but a DHCP server cannot be\r
120         contacted. */\r
121         FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );\r
122 \r
123         /* If the trace recorder code is included... */\r
124         #if configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1\r
125         {\r
126                 extern xQueueHandle xNetworkEventQueue;\r
127 \r
128                 /* Name the queue for viewing in FreeRTOS+Trace. */\r
129                 vTraceSetQueueName( xNetworkEventQueue, "IPStackEvent" );\r
130         }\r
131         #endif /*  configINCLUDE_TRACE_RELATED_CLI_COMMANDS == 1 */\r
132 \r
133         /* Start the FreeRTOS scheduler. */\r
134         vTaskStartScheduler();\r
135 \r
136         /* The following line should never execute.  If it does, it means there was\r
137         insufficient FreeRTOS heap memory available to create the Idle and/or timer\r
138         tasks.  See the memory management section on the http://www.FreeRTOS.org web\r
139         site for more information. */\r
140         for( ;; );\r
141 }\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 void vApplicationStackOverflowHook( xTaskHandle pxTask, char *pcTaskName )\r
145 {\r
146         ( void ) pcTaskName;\r
147         ( void ) pxTask;\r
148 \r
149         /* Run time stack overflow checking is performed if\r
150         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
151         function is called if a stack overflow is detected. */\r
152         taskDISABLE_INTERRUPTS();\r
153         for( ;; );\r
154 }\r
155 /*-----------------------------------------------------------*/\r
156 \r
157 void vApplicationMallocFailedHook( void )\r
158 {\r
159         /* vApplicationMallocFailedHook() will only be called if\r
160         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
161         function that will get called if a call to pvPortMalloc() fails.\r
162         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
163         timer or semaphore is created.  It is also called by various parts of the\r
164         demo application.  If heap_1.c, heap_2.c or heap_4.c are used, then the\r
165         size of the heap available to pvPortMalloc() is defined by\r
166         configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()\r
167         API function can be used to query the size of free heap space that remains\r
168         (although it does not provide information on how the remaining heap might\r
169         be fragmented). */\r
170         taskDISABLE_INTERRUPTS();\r
171         for( ;; );\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 /* Called by FreeRTOS+UDP when the network connects. */\r
176 void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )\r
177 {\r
178 static BaseType_t xTaskAlreadyCreated = pdFALSE;\r
179 \r
180         if( eNetworkEvent == eNetworkUp )\r
181         {\r
182                 /* Create the tasks that transmit to and receive from a standard\r
183                 echo server (see the web documentation for this port) in both\r
184                 standard and zero copy mode. */\r
185                 if( xTaskAlreadyCreated == pdFALSE )\r
186                 {\r
187                         vStartEchoClientTasks( mainECHO_CLIENT_TASK_STACK_SIZE, mainECHO_CLIENT_TASK_PRIORITY );\r
188                         xTaskAlreadyCreated = pdTRUE;\r
189                 }\r
190         }\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 /* Called by FreeRTOS+UDP when a reply is received to an outgoing ping request. */\r
195 void vApplicationPingReplyHook( ePingReplyStatus_t eStatus, uint16_t usIdentifier )\r
196 {\r
197 static const char *pcSuccess = "\r\n\r\nPing reply received - ";\r
198 static const char *pcInvalidChecksum = "\r\n\r\nPing reply received with invalid checksum - ";\r
199 static const char *pcInvalidData = "\r\n\r\nPing reply received with invalid data - ";\r
200 static char cMessage[ 50 ];\r
201 void vOutputString( const char * const pcMessage );\r
202 \r
203         switch( eStatus )\r
204         {\r
205                 case eSuccess   :\r
206                         vOutputString( pcSuccess );\r
207                         break;\r
208 \r
209                 case eInvalidChecksum :\r
210                         vOutputString( pcInvalidChecksum );\r
211                         break;\r
212 \r
213                 case eInvalidData :\r
214                         vOutputString( pcInvalidData );\r
215                         break;\r
216 \r
217                 default :\r
218                         /* It is not possible to get here as all enums have their own\r
219                         case. */\r
220                         break;\r
221         }\r
222 \r
223         sprintf( cMessage, "identifier %d\r\n\r\n", ( int ) usIdentifier );\r
224         vOutputString( cMessage );\r
225 }\r
226 \r