]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3Sxxxx_IAR_Keil/webserver/uIP_Task.c
Add additional critical section to the default tickless implementations.
[freertos] / FreeRTOS / Demo / CORTEX_LM3Sxxxx_IAR_Keil / webserver / uIP_Task.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 /* Standard includes. */\r
65 #include <string.h>\r
66 \r
67 /* Scheduler includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 #include "semphr.h"\r
71 \r
72 /* uip includes. */\r
73 #include "hw_types.h"\r
74 #include "uip.h"\r
75 #include "uip_arp.h"\r
76 #include "httpd.h"\r
77 #include "timer.h"\r
78 #include "clock-arch.h"\r
79 #include "hw_ethernet.h"\r
80 #include "ethernet.h"\r
81 #include "hw_memmap.h"\r
82 #include "lmi_flash.h"\r
83 #include "sysctl.h"\r
84 \r
85 /* Demo includes. */\r
86 #include "emac.h"\r
87 #include "partest.h"\r
88 #include "lcd_message.h"\r
89 \r
90 struct timer {\r
91   clock_time_t start;\r
92   clock_time_t interval;\r
93 };\r
94 \r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 /* IP address configuration. */\r
99 #define uipIP_ADDR0             172\r
100 #define uipIP_ADDR1             25\r
101 #define uipIP_ADDR2             218\r
102 #define uipIP_ADDR3             19      \r
103 \r
104 /* How long to wait before attempting to connect the MAC again. */\r
105 #define uipINIT_WAIT    100\r
106 \r
107 /* Shortcut to the header within the Rx buffer. */\r
108 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
109 \r
110 /* Standard constant. */\r
111 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
112 \r
113 /*-----------------------------------------------------------*/\r
114 \r
115 /*\r
116  * Send the uIP buffer to the MAC.\r
117  */\r
118 static void prvENET_Send(void);\r
119 \r
120 /*\r
121  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
122  */\r
123 static void prvSetMACAddress( void );\r
124 \r
125 /*\r
126  * Port functions required by the uIP stack.\r
127  */\r
128 void clock_init( void );\r
129 clock_time_t clock_time( void );\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /* The semaphore used by the ISR to wake the uIP task. */\r
134 extern xSemaphoreHandle xEMACSemaphore;\r
135 \r
136 /*-----------------------------------------------------------*/\r
137 \r
138 void clock_init(void)\r
139 {\r
140         /* This is done when the scheduler starts. */\r
141 }\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 /* Define clock functions here to avoid header file name clash between uIP\r
145 and the Luminary Micro driver library. */\r
146 clock_time_t clock_time( void )\r
147 {\r
148         return xTaskGetTickCount();\r
149 }\r
150 extern void timer_set(struct timer *t, clock_time_t interval);\r
151 extern int timer_expired(struct timer *t);\r
152 extern void timer_reset(struct timer *t);\r
153 \r
154 \r
155 \r
156 \r
157 void vuIP_Task( void *pvParameters )\r
158 {\r
159 portBASE_TYPE i;\r
160 uip_ipaddr_t xIPAddr;\r
161 struct timer periodic_timer, arp_timer;\r
162 extern void ( vEMAC_ISR )( void );\r
163 \r
164         /* Enable/Reset the Ethernet Controller */\r
165         SysCtlPeripheralEnable( SYSCTL_PERIPH_ETH );\r
166         SysCtlPeripheralReset( SYSCTL_PERIPH_ETH );\r
167 \r
168         /* Create the semaphore used by the ISR to wake this task. */\r
169         vSemaphoreCreateBinary( xEMACSemaphore );\r
170         \r
171         /* Initialise the uIP stack. */\r
172         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
173         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
174         uip_init();\r
175         uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );\r
176         uip_sethostaddr( xIPAddr );\r
177         httpd_init();\r
178 \r
179         while( vInitEMAC() != pdPASS )\r
180     {\r
181         vTaskDelay( uipINIT_WAIT );\r
182     }\r
183         prvSetMACAddress();     \r
184         \r
185 \r
186         for( ;; )\r
187         {\r
188                 /* Is there received data ready to be processed? */\r
189                 uip_len = uiGetEMACRxData( uip_buf );\r
190                 \r
191                 if( uip_len > 0 )\r
192                 {\r
193                         /* Standard uIP loop taken from the uIP manual. */\r
194 \r
195                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
196                         {\r
197                                 uip_arp_ipin();\r
198                                 uip_input();\r
199 \r
200                                 /* If the above function invocation resulted in data that\r
201                                 should be sent out on the network, the global variable\r
202                                 uip_len is set to a value > 0. */\r
203                                 if( uip_len > 0 )\r
204                                 {\r
205                                         uip_arp_out();\r
206                                         prvENET_Send();\r
207                                 }\r
208                         }\r
209                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
210                         {\r
211                                 uip_arp_arpin();\r
212 \r
213                                 /* If the above function invocation resulted in data that\r
214                                 should be sent out on the network, the global variable\r
215                                 uip_len is set to a value > 0. */\r
216                                 if( uip_len > 0 )\r
217                                 {\r
218                                         prvENET_Send();\r
219                                 }\r
220                         }\r
221                 }\r
222                 else\r
223                 {\r
224                         if( timer_expired( &periodic_timer ) )\r
225                         {\r
226                                 timer_reset( &periodic_timer );\r
227                                 for( i = 0; i < UIP_CONNS; i++ )\r
228                                 {\r
229                                         uip_periodic( i );\r
230         \r
231                                         /* If the above function invocation resulted in data that\r
232                                         should be sent out on the network, the global variable\r
233                                         uip_len is set to a value > 0. */\r
234                                         if( uip_len > 0 )\r
235                                         {\r
236                                                 uip_arp_out();\r
237                                                 prvENET_Send();\r
238                                         }\r
239                                 }       \r
240         \r
241                                 /* Call the ARP timer function every 10 seconds. */\r
242                                 if( timer_expired( &arp_timer ) )\r
243                                 {\r
244                                         timer_reset( &arp_timer );\r
245                                         uip_arp_timer();\r
246                                 }\r
247                         }\r
248                         else\r
249                         {                       \r
250                                 /* We did not receive a packet, and there was no periodic\r
251                                 processing to perform.  Block for a fixed period.  If a packet\r
252                                 is received during this period we will be woken by the ISR\r
253                                 giving us the Semaphore. */\r
254                                 xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );                       \r
255                         }\r
256                 }\r
257         }\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 static void prvENET_Send(void)\r
262 {\r
263         vInitialiseSend();\r
264         vIncrementTxLength( uip_len );\r
265         vSendBufferToMAC();\r
266 }\r
267 /*-----------------------------------------------------------*/\r
268 \r
269 static void prvSetMACAddress( void )\r
270 {\r
271 unsigned portLONG ulUser0, ulUser1;\r
272 unsigned char pucMACArray[8];\r
273 struct uip_eth_addr xAddr;\r
274 \r
275         /* Get the device MAC address from flash */\r
276     FlashUserGet(&ulUser0, &ulUser1);\r
277 \r
278         /* Convert the MAC address from flash into sequence of bytes. */\r
279     pucMACArray[0] = ((ulUser0 >>  0) & 0xff);\r
280     pucMACArray[1] = ((ulUser0 >>  8) & 0xff);\r
281     pucMACArray[2] = ((ulUser0 >> 16) & 0xff);\r
282     pucMACArray[3] = ((ulUser1 >>  0) & 0xff);\r
283     pucMACArray[4] = ((ulUser1 >>  8) & 0xff);\r
284     pucMACArray[5] = ((ulUser1 >> 16) & 0xff);\r
285 \r
286         /* Program the MAC address. */\r
287     EthernetMACAddrSet(ETH_BASE, pucMACArray);\r
288 \r
289         xAddr.addr[ 0 ] = pucMACArray[0];\r
290         xAddr.addr[ 1 ] = pucMACArray[1];\r
291         xAddr.addr[ 2 ] = pucMACArray[2];\r
292         xAddr.addr[ 3 ] = pucMACArray[3];\r
293         xAddr.addr[ 4 ] = pucMACArray[4];\r
294         xAddr.addr[ 5 ] = pucMACArray[5];\r
295         uip_setethaddr( xAddr );\r
296 }\r
297 /*-----------------------------------------------------------*/\r
298 \r
299 void vApplicationProcessFormInput( portCHAR *pcInputString, portBASE_TYPE xInputLength )\r
300 {\r
301 char *c, *pcText;\r
302 static portCHAR cMessageForDisplay[ 32 ];\r
303 extern xQueueHandle xOLEDQueue;\r
304 xOLEDMessage xOLEDMessage;\r
305 \r
306         /* Process the form input sent by the IO page of the served HTML. */\r
307 \r
308         c = strstr( pcInputString, "?" );\r
309 \r
310     if( c )\r
311     {\r
312                 /* Turn LED's on or off in accordance with the check box status. */\r
313                 if( strstr( c, "LED0=1" ) != NULL )\r
314                 {\r
315                         vParTestSetLED( 0, 1 );\r
316                 }\r
317                 else\r
318                 {\r
319                         vParTestSetLED( 0, 0 );\r
320                 }               \r
321                 \r
322                 /* Find the start of the text to be displayed on the LCD. */\r
323         pcText = strstr( c, "LCD=" );\r
324         pcText += strlen( "LCD=" );\r
325 \r
326         /* Terminate the file name for further processing within uIP. */\r
327         *c = 0x00;\r
328 \r
329         /* Terminate the LCD string. */\r
330         c = strstr( pcText, " " );\r
331         if( c != NULL )\r
332         {\r
333             *c = 0x00;\r
334         }\r
335 \r
336         /* Add required spaces. */\r
337         while( ( c = strstr( pcText, "+" ) ) != NULL )\r
338         {\r
339             *c = ' ';\r
340         }\r
341 \r
342         /* Write the message to the LCD. */\r
343                 strcpy( cMessageForDisplay, pcText );\r
344                 xOLEDMessage.pcMessage = ( signed portCHAR * ) cMessageForDisplay;\r
345         xQueueSend( xOLEDQueue, &xOLEDMessage, portMAX_DELAY );\r
346     }\r
347 }\r
348 \r