]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM9_STR91X_IAR/webserver/uIP_Task.c
5ed6dd9f1dddadc2d5bb2221e2ed638a8dd70d23
[freertos] / FreeRTOS / Demo / ARM9_STR91X_IAR / webserver / uIP_Task.c
1 /*\r
2     FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13         ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18         ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40         the FAQ page "My application does not run, what could be wrong?".  Have you\r
41         defined configASSERT()?\r
42 \r
43         http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44         embedded software for free we request you assist our global community by\r
45         participating in the support forum.\r
46 \r
47         http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48         be as productive as possible as early as possible.  Now you can receive\r
49         FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50         Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 /* Standard includes. */\r
70 #include <string.h>\r
71 \r
72 /* Library includes. */\r
73 #include "91x_lib.h"\r
74 #include "91x_enet.h"\r
75 \r
76 /* Scheduler includes. */\r
77 #include "FreeRTOS.h"\r
78 #include "task.h"\r
79 #include "semphr.h"\r
80 \r
81 /* uip includes. */\r
82 #include "uip.h"\r
83 #include "uip_arp.h"\r
84 #include "httpd.h"\r
85 #include "timer.h"\r
86 #include "clock-arch.h"\r
87 \r
88 /*-----------------------------------------------------------*/\r
89 \r
90 /* MAC address configuration. */\r
91 #define uipMAC_ADDR0    0x00\r
92 #define uipMAC_ADDR1    0x12\r
93 #define uipMAC_ADDR2    0x13\r
94 #define uipMAC_ADDR3    0x14\r
95 #define uipMAC_ADDR4    0x15\r
96 #define uipMAC_ADDR5    0x20\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             11      \r
103 \r
104 /* Netmask configuration. */\r
105 #define uipNET_MASK0    255\r
106 #define uipNET_MASK1    255\r
107 #define uipNET_MASK2    255\r
108 #define uipNET_MASK3    0\r
109 \r
110 /* Gateway address configuration. */\r
111 #define uipGATEWAY_ADDR0 172\r
112 #define uipGATEWAY_ADDR1 25\r
113 #define uipGATEWAY_ADDR2 218\r
114 #define uipGATEWAY_ADDR3 1\r
115 \r
116 /* Shortcut to the header within the Rx buffer. */\r
117 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])\r
118 \r
119 /* uIP update frequencies. */\r
120 #define uipMAX_BLOCK_TIME       (configTICK_RATE_HZ / 4)\r
121 \r
122 /* Interrupt status bit definition. */\r
123 #define uipDMI_RX_CURRENT_DONE 0x8000\r
124 \r
125 /* If no buffers are available, then wait this long before looking again. */\r
126 #define uipBUFFER_WAIT_DELAY    ( 10 / portTICK_PERIOD_MS )\r
127 #define uipBUFFER_WAIT_ATTEMPTS ( 10 )\r
128 \r
129 /* Standard constant. */\r
130 #define uipTOTAL_FRAME_HEADER_SIZE      54\r
131 \r
132 /*-----------------------------------------------------------*/\r
133 \r
134 /*\r
135  * Send the uIP buffer to the MAC.\r
136  */\r
137 static void prvENET_Send(void);\r
138 \r
139 /*\r
140  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
141  */\r
142 static void prvSetMACAddress( void );\r
143 \r
144 /*\r
145  * Used to return a pointer to the next buffer to be used.\r
146  */\r
147 extern unsigned char *pcGetNextBuffer( void );\r
148 \r
149 /*\r
150  * Port functions required by the uIP stack.\r
151  */\r
152 void clock_init( void );\r
153 clock_time_t clock_time( void );\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 \r
157 /* The semaphore used by the ISR to wake the uIP task. */\r
158 SemaphoreHandle_t xSemaphore = NULL;\r
159 \r
160 /*-----------------------------------------------------------*/\r
161 \r
162 void clock_init(void)\r
163 {\r
164         /* This is done when the scheduler starts. */\r
165 }\r
166 /*-----------------------------------------------------------*/\r
167 \r
168 clock_time_t clock_time( void )\r
169 {\r
170         return xTaskGetTickCount();\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 void vuIP_Task( void *pvParameters )\r
175 {\r
176 portBASE_TYPE i;\r
177 uip_ipaddr_t xIPAddr;\r
178 struct timer periodic_timer, arp_timer;\r
179 \r
180         /* Create the semaphore used by the ISR to wake this task. */\r
181         vSemaphoreCreateBinary( xSemaphore );\r
182         \r
183         /* Initialise the uIP stack. */\r
184         timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );\r
185         timer_set( &arp_timer, configTICK_RATE_HZ * 10 );\r
186         uip_init();\r
187         uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );\r
188         uip_sethostaddr( xIPAddr );\r
189         uip_ipaddr( xIPAddr, uipNET_MASK0, uipNET_MASK1, uipNET_MASK2, uipNET_MASK3 );\r
190         uip_setnetmask( xIPAddr );\r
191         uip_ipaddr( xIPAddr, uipGATEWAY_ADDR0, uipGATEWAY_ADDR1, uipGATEWAY_ADDR2, uipGATEWAY_ADDR3 );\r
192         uip_setdraddr( xIPAddr );       \r
193         httpd_init();\r
194 \r
195         /* Initialise the MAC. */\r
196         ENET_InitClocksGPIO();\r
197         ENET_Init();\r
198         portENTER_CRITICAL();\r
199         {\r
200                 ENET_Start();\r
201                 prvSetMACAddress();\r
202                 VIC_Config( ENET_ITLine, VIC_IRQ, 1 );\r
203                 VIC_ITCmd( ENET_ITLine, ENABLE );       \r
204                 ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;\r
205                 ENET_DMA->IER = uipDMI_RX_CURRENT_DONE;\r
206         }\r
207         portEXIT_CRITICAL();\r
208         \r
209 \r
210         while(1)\r
211         {\r
212                 /* Is there received data ready to be processed? */\r
213                 uip_len = ENET_HandleRxPkt( uip_buf );\r
214                 \r
215                 if( uip_len > 0 )\r
216                 {\r
217                         /* Standard uIP loop taken from the uIP manual. */\r
218                         if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
219                         {\r
220                                 uip_arp_ipin();\r
221                                 uip_input();\r
222 \r
223                                 /* If the above function invocation resulted in data that\r
224                                 should be sent out on the network, the global variable\r
225                                 uip_len is set to a value > 0. */\r
226                                 if( uip_len > 0 )\r
227                                 {\r
228                                         uip_arp_out();\r
229                                         prvENET_Send();\r
230                                 }\r
231                         }\r
232                         else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
233                         {\r
234                                 uip_arp_arpin();\r
235 \r
236                                 /* If the above function invocation resulted in data that\r
237                                 should be sent out on the network, the global variable\r
238                                 uip_len is set to a value > 0. */\r
239                                 if( uip_len > 0 )\r
240                                 {\r
241                                         prvENET_Send();\r
242                                 }\r
243                         }\r
244                 }\r
245                 else\r
246                 {\r
247                         if( timer_expired( &periodic_timer ) )\r
248                         {\r
249                                 timer_reset( &periodic_timer );\r
250                                 for( i = 0; i < UIP_CONNS; i++ )\r
251                                 {\r
252                                         uip_periodic( i );\r
253         \r
254                                         /* If the above function invocation resulted in data that\r
255                                         should be sent out on the network, the global variable\r
256                                         uip_len is set to a value > 0. */\r
257                                         if( uip_len > 0 )\r
258                                         {\r
259                                                 uip_arp_out();\r
260                                                 prvENET_Send();\r
261                                         }\r
262                                 }       \r
263         \r
264                                 /* Call the ARP timer function every 10 seconds. */\r
265                                 if( timer_expired( &arp_timer ) )\r
266                                 {\r
267                                         timer_reset( &arp_timer );\r
268                                         uip_arp_timer();\r
269                                 }\r
270                         }\r
271                         else\r
272                         {                       \r
273                                 /* We did not receive a packet, and there was no periodic\r
274                                 processing to perform.  Block for a fixed period.  If a packet\r
275                                 is received during this period we will be woken by the ISR\r
276                                 giving us the Semaphore. */\r
277                                 xSemaphoreTake( xSemaphore, configTICK_RATE_HZ / 2 );                   \r
278                         }\r
279                 }\r
280         }\r
281 }\r
282 /*-----------------------------------------------------------*/\r
283 \r
284 static void prvENET_Send(void)\r
285 {\r
286 portBASE_TYPE i;\r
287 static unsigned char *pcTxData;\r
288 \r
289         /* Get a DMA buffer into which we can write the data to send. */\r
290         for( i = 0; i < uipBUFFER_WAIT_ATTEMPTS; i++ )\r
291         {\r
292                 pcTxData = pcGetNextBuffer();\r
293 \r
294                 if( pcTxData )\r
295                 {\r
296                         break;\r
297                 }\r
298                 else\r
299                 {\r
300                         vTaskDelay( uipBUFFER_WAIT_DELAY );\r
301                 }\r
302         }\r
303         \r
304         if( pcTxData )\r
305         {\r
306                 /* Copy the header into the Tx buffer. */\r
307                 memcpy( ( void * ) pcTxData, ( void * ) uip_buf, uipTOTAL_FRAME_HEADER_SIZE );\r
308                 if( uip_len > uipTOTAL_FRAME_HEADER_SIZE )\r
309                 {\r
310                         memcpy( ( void * ) &( pcTxData[ uipTOTAL_FRAME_HEADER_SIZE ] ), ( void * ) uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) );\r
311                 }\r
312 \r
313                 ENET_TxPkt( &pcTxData, uip_len );\r
314         }\r
315 }\r
316 /*-----------------------------------------------------------*/\r
317 \r
318 void ENET_IRQHandler(void)\r
319 {\r
320 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
321 \r
322         /* Give the semaphore in case the uIP task needs waking. */\r
323         xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );\r
324         \r
325         /* Clear the interrupt. */\r
326         ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;\r
327         \r
328         /* Switch tasks if necessary. */        \r
329         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
330 }\r
331 /*-----------------------------------------------------------*/\r
332 \r
333 static void prvSetMACAddress( void )\r
334 {\r
335 struct uip_eth_addr xAddr;\r
336 \r
337         /* Configure the MAC address in the uIP stack. */\r
338         xAddr.addr[ 0 ] = uipMAC_ADDR0;\r
339         xAddr.addr[ 1 ] = uipMAC_ADDR1;\r
340         xAddr.addr[ 2 ] = uipMAC_ADDR2;\r
341         xAddr.addr[ 3 ] = uipMAC_ADDR3;\r
342         xAddr.addr[ 4 ] = uipMAC_ADDR4;\r
343         xAddr.addr[ 5 ] = uipMAC_ADDR5;\r
344         uip_setethaddr( xAddr );\r
345 \r
346         /* Write the MAC address to the MAC. */ \r
347         ENET_MAC->MAL = ( uipMAC_ADDR3 << 24 ) | ( uipMAC_ADDR2 << 16 ) | ( uipMAC_ADDR1 << 8 ) | ( uipMAC_ADDR0 );\r
348         ENET_MAC->MAH = ( uipMAC_ADDR5 << 8 ) | ( uipMAC_ADDR4 );\r
349 }\r
350 \r