]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LPC1768_GCC_RedSuite/src/webserver/uIP_Task.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_LPC1768_GCC_RedSuite / src / webserver / uIP_Task.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 \r
29 /* Standard includes. */\r
30 #include <string.h>\r
31 \r
32 /* Scheduler includes. */\r
33 #include "FreeRTOS.h"\r
34 #include "task.h"\r
35 #include "semphr.h"\r
36 \r
37 /* uip includes. */\r
38 #include "uip.h"\r
39 #include "uip_arp.h"\r
40 #include "httpd.h"\r
41 #include "timer.h"\r
42 #include "clock-arch.h"\r
43 \r
44 /* Demo includes. */\r
45 #include "EthDev_LPC17xx.h"\r
46 #include "EthDev.h"\r
47 #include "ParTest.h"\r
48 \r
49 /*-----------------------------------------------------------*/\r
50 \r
51 /* How long to wait before attempting to connect the MAC again. */\r
52 #define uipINIT_WAIT    ( 100 / portTICK_PERIOD_MS )\r
53 \r
54 /* Shortcut to the header within the Rx buffer. */\r
55 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
56 \r
57 /* Standard constant. */\r
58 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
59 \r
60 /*-----------------------------------------------------------*/\r
61 \r
62 /*\r
63  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
64  */\r
65 static void prvSetMACAddress( void );\r
66 \r
67 /*\r
68  * Port functions required by the uIP stack.\r
69  */\r
70 void clock_init( void );\r
71 clock_time_t clock_time( void );\r
72 \r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /* The semaphore used by the ISR to wake the uIP task. */\r
76 SemaphoreHandle_t xEMACSemaphore = NULL;\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 void clock_init(void)\r
81 {\r
82         /* This is done when the scheduler starts. */\r
83 }\r
84 /*-----------------------------------------------------------*/\r
85 \r
86 clock_time_t clock_time( void )\r
87 {\r
88         return xTaskGetTickCount();\r
89 }\r
90 /*-----------------------------------------------------------*/\r
91 \r
92 void vuIP_Task( void *pvParameters )\r
93 {\r
94 portBASE_TYPE i;\r
95 uip_ipaddr_t xIPAddr;\r
96 struct timer periodic_timer, arp_timer;\r
97 extern void ( vEMAC_ISR_Wrapper )( void );\r
98 \r
99         ( void ) pvParameters;\r
100 \r
101         /* Initialise the uIP stack. */\r
102         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
103         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
104         uip_init();\r
105         uip_ipaddr( xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
106         uip_sethostaddr( xIPAddr );\r
107         uip_ipaddr( xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
108         uip_setnetmask( xIPAddr );\r
109         httpd_init();\r
110 \r
111         /* Create the semaphore used to wake the uIP task. */\r
112         vSemaphoreCreateBinary( xEMACSemaphore );\r
113 \r
114         /* Initialise the MAC. */\r
115         while( lEMACInit() != pdPASS )\r
116     {\r
117         vTaskDelay( uipINIT_WAIT );\r
118     }\r
119 \r
120         portENTER_CRITICAL();\r
121         {\r
122                 LPC_EMAC->IntEnable = ( INT_RX_DONE | INT_TX_DONE );\r
123 \r
124                 /* Set the interrupt priority to the max permissible to cause some\r
125                 interrupt nesting. */\r
126                 NVIC_SetPriority( ENET_IRQn, configEMAC_INTERRUPT_PRIORITY );\r
127 \r
128                 /* Enable the interrupt. */\r
129                 NVIC_EnableIRQ( ENET_IRQn );\r
130                 prvSetMACAddress();\r
131         }\r
132         portEXIT_CRITICAL();\r
133 \r
134 \r
135         for( ;; )\r
136         {\r
137                 /* Is there received data ready to be processed? */\r
138                 uip_len = ulGetEMACRxData();\r
139 \r
140                 if( ( uip_len > 0 ) && ( uip_buf != NULL ) )\r
141                 {\r
142                         /* Standard uIP loop taken from the uIP manual. */\r
143                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
144                         {\r
145                                 uip_arp_ipin();\r
146                                 uip_input();\r
147 \r
148                                 /* If the above function invocation resulted in data that\r
149                                 should be sent out on the network, the global variable\r
150                                 uip_len is set to a value > 0. */\r
151                                 if( uip_len > 0 )\r
152                                 {\r
153                                         uip_arp_out();\r
154                                         vSendEMACTxData( uip_len );\r
155                                 }\r
156                         }\r
157                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
158                         {\r
159                                 uip_arp_arpin();\r
160 \r
161                                 /* If the above function invocation resulted in data that\r
162                                 should be sent out on the network, the global variable\r
163                                 uip_len is set to a value > 0. */\r
164                                 if( uip_len > 0 )\r
165                                 {\r
166                                         vSendEMACTxData( uip_len );\r
167                                 }\r
168                         }\r
169                 }\r
170                 else\r
171                 {\r
172                         if( timer_expired( &periodic_timer ) && ( uip_buf != NULL ) )\r
173                         {\r
174                                 timer_reset( &periodic_timer );\r
175                                 for( i = 0; i < UIP_CONNS; i++ )\r
176                                 {\r
177                                         uip_periodic( i );\r
178 \r
179                                         /* If the above function invocation resulted in data that\r
180                                         should be sent out on the network, the global variable\r
181                                         uip_len is set to a value > 0. */\r
182                                         if( uip_len > 0 )\r
183                                         {\r
184                                                 uip_arp_out();\r
185                                                 vSendEMACTxData( uip_len );\r
186                                         }\r
187                                 }\r
188 \r
189                                 /* Call the ARP timer function every 10 seconds. */\r
190                                 if( timer_expired( &arp_timer ) )\r
191                                 {\r
192                                         timer_reset( &arp_timer );\r
193                                         uip_arp_timer();\r
194                                 }\r
195                         }\r
196                         else\r
197                         {\r
198                                 /* We did not receive a packet, and there was no periodic\r
199                                 processing to perform.  Block for a fixed period.  If a packet\r
200                                 is received during this period we will be woken by the ISR\r
201                                 giving us the Semaphore. */\r
202                                 xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );\r
203                         }\r
204                 }\r
205         }\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 static void prvSetMACAddress( void )\r
210 {\r
211 struct uip_eth_addr xAddr;\r
212 \r
213         /* Configure the MAC address in the uIP stack. */\r
214         xAddr.addr[ 0 ] = configMAC_ADDR0;\r
215         xAddr.addr[ 1 ] = configMAC_ADDR1;\r
216         xAddr.addr[ 2 ] = configMAC_ADDR2;\r
217         xAddr.addr[ 3 ] = configMAC_ADDR3;\r
218         xAddr.addr[ 4 ] = configMAC_ADDR4;\r
219         xAddr.addr[ 5 ] = configMAC_ADDR5;\r
220         uip_setethaddr( xAddr );\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 void vApplicationProcessFormInput( char *pcInputString )\r
225 {\r
226 char *c;\r
227 const unsigned long ulLEDNo = 3;\r
228 \r
229         /* Process the form input sent by the IO page of the served HTML. */\r
230 \r
231         c = strstr( pcInputString, "?" );\r
232     if( c )\r
233     {\r
234                 /* Turn LED's on or off in accordance with the check box status. */\r
235                 if( strstr( c, "LED0=1" ) != NULL )\r
236                 {\r
237                         /* Set LED7. */\r
238                         vParTestSetLED( ulLEDNo, pdFALSE );\r
239                 }\r
240                 else\r
241                 {\r
242                         /* Clear LED7. */\r
243                         vParTestSetLED( ulLEDNo, pdTRUE );\r
244                 }\r
245     }\r
246 }\r
247 \r