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