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