]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3Sxxxx_Rowley/webserver/uIP_Task.c
Fix some build issues in older kernel demo projects.
[freertos] / FreeRTOS / Demo / CORTEX_LM3Sxxxx_Rowley / webserver / uIP_Task.c
1 /*\r
2  * FreeRTOS Kernel V10.1.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.\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 \r
30 /* Scheduler includes. */\r
31 #include "FreeRTOS.h"\r
32 #include "task.h"\r
33 #include "semphr.h"\r
34 \r
35 #include "lcd_message.h"\r
36 \r
37 /* uip includes. */\r
38 #include "hw_types.h"\r
39 \r
40 #include "uip.h"\r
41 #include "uip_arp.h"\r
42 #include "httpd.h"\r
43 #include "timer.h"\r
44 #include "clock-arch.h"\r
45 #include "hw_ethernet.h"\r
46 #include "ethernet.h"\r
47 #include "hw_memmap.h"\r
48 #include "lmi_flash.h"\r
49 #include "sysctl.h"\r
50 \r
51 /* Demo includes. */\r
52 #include "emac.h"\r
53 #include "partest.h"\r
54 \r
55 /*-----------------------------------------------------------*/\r
56 \r
57 /* IP address configuration. */\r
58 #define uipIP_ADDR0             192\r
59 #define uipIP_ADDR1             168\r
60 #define uipIP_ADDR2             0\r
61 #define uipIP_ADDR3             201\r
62 \r
63 /* Netmask configuration. */\r
64 #define uipNETMASK_0    255\r
65 #define uipNETMASK_1    255\r
66 #define uipNETMASK_2    255\r
67 #define uipNETMASK_3    0\r
68 \r
69 /* How long to wait before attempting to connect the MAC again. */\r
70 #define uipINIT_WAIT    100\r
71 \r
72 /* Shortcut to the header within the Rx buffer. */\r
73 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
74 \r
75 /* Standard constant. */\r
76 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 /*\r
81  * Send the uIP buffer to the MAC.\r
82  */\r
83 static void prvENET_Send(void);\r
84 \r
85 /*\r
86  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
87  */\r
88 static void prvSetMACAddress( void );\r
89 \r
90 /*\r
91  * Port functions required by the uIP stack.\r
92  */\r
93 void clock_init( void );\r
94 clock_time_t clock_time( void );\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 /* The semaphore used by the ISR to wake the uIP task. */\r
99 extern SemaphoreHandle_t xEMACSemaphore;\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 void clock_init(void)\r
104 {\r
105         /* This is done when the scheduler starts. */\r
106 }\r
107 /*-----------------------------------------------------------*/\r
108 \r
109 clock_time_t clock_time( void )\r
110 {\r
111         return xTaskGetTickCount();\r
112 }\r
113 \r
114 \r
115 void vuIP_Task( void *pvParameters )\r
116 {\r
117 portBASE_TYPE i;\r
118 uip_ipaddr_t xIPAddr;\r
119 struct timer periodic_timer, arp_timer;\r
120 extern void ( vEMAC_ISR )( void );\r
121 \r
122         /* Enable/Reset the Ethernet Controller */\r
123         SysCtlPeripheralEnable( SYSCTL_PERIPH_ETH );\r
124         SysCtlPeripheralReset( SYSCTL_PERIPH_ETH );\r
125 \r
126         /* Create the semaphore used by the ISR to wake this task. */\r
127         vSemaphoreCreateBinary( xEMACSemaphore );\r
128 \r
129         /* Initialise the uIP stack. */\r
130         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
131         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
132         uip_init();\r
133         uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );\r
134         uip_sethostaddr( xIPAddr );\r
135         uip_ipaddr( xIPAddr, uipNETMASK_0, uipNETMASK_1, uipNETMASK_2, uipNETMASK_3 );\r
136     uip_setnetmask( xIPAddr );\r
137         httpd_init();\r
138 \r
139         while( vInitEMAC() != pdPASS )\r
140     {\r
141         vTaskDelay( uipINIT_WAIT );\r
142     }\r
143         prvSetMACAddress();\r
144 \r
145 \r
146         for( ;; )\r
147         {\r
148                 /* Is there received data ready to be processed? */\r
149                 uip_len = uiGetEMACRxData( uip_buf );\r
150 \r
151                 if( uip_len > 0 )\r
152                 {\r
153                         /* Standard uIP loop taken from the uIP manual. */\r
154 \r
155                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
156                         {\r
157                                 uip_arp_ipin();\r
158                                 uip_input();\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                                         uip_arp_out();\r
166                                         prvENET_Send();\r
167                                 }\r
168                         }\r
169                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
170                         {\r
171                                 uip_arp_arpin();\r
172 \r
173                                 /* If the above function invocation resulted in data that\r
174                                 should be sent out on the network, the global variable\r
175                                 uip_len is set to a value > 0. */\r
176                                 if( uip_len > 0 )\r
177                                 {\r
178                                         prvENET_Send();\r
179                                 }\r
180                         }\r
181                 }\r
182                 else\r
183                 {\r
184                         if( timer_expired( &periodic_timer ) )\r
185                         {\r
186                                 timer_reset( &periodic_timer );\r
187                                 for( i = 0; i < UIP_CONNS; i++ )\r
188                                 {\r
189                                         uip_periodic( i );\r
190 \r
191                                         /* If the above function invocation resulted in data that\r
192                                         should be sent out on the network, the global variable\r
193                                         uip_len is set to a value > 0. */\r
194                                         if( uip_len > 0 )\r
195                                         {\r
196                                                 uip_arp_out();\r
197                                                 prvENET_Send();\r
198                                         }\r
199                                 }\r
200 \r
201                                 /* Call the ARP timer function every 10 seconds. */\r
202                                 if( timer_expired( &arp_timer ) )\r
203                                 {\r
204                                         timer_reset( &arp_timer );\r
205                                         uip_arp_timer();\r
206                                 }\r
207                         }\r
208                         else\r
209                         {\r
210                                 /* We did not receive a packet, and there was no periodic\r
211                                 processing to perform.  Block for a fixed period.  If a packet\r
212                                 is received during this period we will be woken by the ISR\r
213                                 giving us the Semaphore. */\r
214                                 xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );\r
215                         }\r
216                 }\r
217         }\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 static void prvENET_Send(void)\r
222 {\r
223     vInitialiseSend();\r
224     vIncrementTxLength( uip_len );\r
225     vSendBufferToMAC();\r
226     vInitialiseSend();\r
227     vIncrementTxLength( uip_len );\r
228     vSendBufferToMAC();\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 static void prvSetMACAddress( void )\r
233 {\r
234 unsigned long ulUser0, ulUser1;\r
235 unsigned char pucMACArray[8];\r
236 struct uip_eth_addr xAddr;\r
237 \r
238         /* Get the device MAC address from flash */\r
239     FlashUserGet(&ulUser0, &ulUser1);\r
240 \r
241         /* Convert the MAC address from flash into sequence of bytes. */\r
242     pucMACArray[0] = ((ulUser0 >>  0) & 0xff);\r
243     pucMACArray[1] = ((ulUser0 >>  8) & 0xff);\r
244     pucMACArray[2] = ((ulUser0 >> 16) & 0xff);\r
245     pucMACArray[3] = ((ulUser1 >>  0) & 0xff);\r
246     pucMACArray[4] = ((ulUser1 >>  8) & 0xff);\r
247     pucMACArray[5] = ((ulUser1 >> 16) & 0xff);\r
248 \r
249         /* Program the MAC address. */\r
250     EthernetMACAddrSet(ETH_BASE, pucMACArray);\r
251 \r
252         xAddr.addr[ 0 ] = pucMACArray[0];\r
253         xAddr.addr[ 1 ] = pucMACArray[1];\r
254         xAddr.addr[ 2 ] = pucMACArray[2];\r
255         xAddr.addr[ 3 ] = pucMACArray[3];\r
256         xAddr.addr[ 4 ] = pucMACArray[4];\r
257         xAddr.addr[ 5 ] = pucMACArray[5];\r
258         uip_setethaddr( xAddr );\r
259 }\r
260 /*-----------------------------------------------------------*/\r
261 \r
262 void vApplicationProcessFormInput( char *pcInputString, portBASE_TYPE xInputLength )\r
263 {\r
264 char *c, *pcText;\r
265 static char cMessageForDisplay[ 32 ];\r
266 extern QueueHandle_t xOLEDQueue;\r
267 xOLEDMessage xOLEDMessage;\r
268 \r
269         /* Process the form input sent by the IO page of the served HTML. */\r
270 \r
271         c = strstr( pcInputString, "?" );\r
272 \r
273     if( c )\r
274     {\r
275                 /* Turn LED's on or off in accordance with the check box status. */\r
276                 if( strstr( c, "LED0=1" ) != NULL )\r
277                 {\r
278                         vParTestSetLED( 0, 1 );\r
279                 }\r
280                 else\r
281                 {\r
282                         vParTestSetLED( 0, 0 );\r
283                 }\r
284 \r
285                 /* Find the start of the text to be displayed on the LCD. */\r
286         pcText = strstr( c, "LCD=" );\r
287         pcText += strlen( "LCD=" );\r
288 \r
289         /* Terminate the file name for further processing within uIP. */\r
290         *c = 0x00;\r
291 \r
292         /* Terminate the LCD string. */\r
293         c = strstr( pcText, " " );\r
294         if( c != NULL )\r
295         {\r
296             *c = 0x00;\r
297         }\r
298 \r
299         /* Add required spaces. */\r
300         while( ( c = strstr( pcText, "+" ) ) != NULL )\r
301         {\r
302             *c = ' ';\r
303         }\r
304 \r
305         /* Write the message to the LCD. */\r
306                 strcpy( cMessageForDisplay, pcText );\r
307                 xOLEDMessage.pcMessage = cMessageForDisplay;\r
308         xQueueSend( xOLEDQueue, &xOLEDMessage, portMAX_DELAY );\r
309     }\r
310 }\r
311 \r