]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX600_RX62N-RSK_IAR/uIP_Task.c
546d049bfd2606239ef5420ff83f99374ed13cbb
[freertos] / FreeRTOS / Demo / RX600_RX62N-RSK_IAR / 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 "timers.h"\r
35 #include "queue.h"\r
36 \r
37 /* uip includes. */\r
38 #include "net/uip.h"\r
39 #include "net/uip_arp.h"\r
40 #include "apps/httpd/httpd.h"\r
41 #include "sys/timer.h"\r
42 #include "net/clock-arch.h"\r
43 #include "r_ether.h"\r
44 \r
45 /* Demo includes. */\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 /* The ARP timer and the periodic timer share a callback function, so the\r
60 respective timer IDs are used to determine which timer actually expired.  These\r
61 constants are assigned to the timer IDs. */\r
62 #define uipARP_TIMER                            0\r
63 #define uipPERIODIC_TIMER                       1\r
64 \r
65 /* A block time of zero ticks simply means, "don't block". */\r
66 #define uipDONT_BLOCK                           0UL\r
67 \r
68 /*-----------------------------------------------------------*/\r
69 \r
70 /*\r
71  * Setup the MAC address in the MAC itself, and in the uIP stack.\r
72  */\r
73 static void prvSetMACAddress( void );\r
74 \r
75 /*\r
76  * Perform any uIP initialisation necessary.\r
77  */\r
78 static void prvInitialise_uIP( void );\r
79 \r
80 /*\r
81  * The callback function that is assigned to both the periodic timer and the\r
82  * ARP timer.\r
83  */\r
84 static void prvUIPTimerCallback( TimerHandle_t xTimer );\r
85 \r
86 /*\r
87  * Port functions required by the uIP stack.\r
88  */\r
89 clock_time_t clock_time( void );\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /* The queue used to send TCP/IP events to the uIP stack. */\r
94 QueueHandle_t xEMACEventQueue = NULL;\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 clock_time_t clock_time( void )\r
99 {\r
100         return xTaskGetTickCount();\r
101 }\r
102 /*-----------------------------------------------------------*/\r
103 \r
104 void vuIP_Task( void *pvParameters )\r
105 {\r
106 portBASE_TYPE i;\r
107 unsigned long ulNewEvent = 0UL;\r
108 unsigned long ulUIP_Events = 0UL;\r
109 \r
110         ( void ) pvParameters;\r
111         \r
112         /* Initialise the uIP stack. */\r
113         prvInitialise_uIP();\r
114 \r
115         /* Initialise the MAC. */\r
116         vInitEmac();\r
117 \r
118         while( lEMACWaitForLink() != pdPASS )\r
119     {\r
120         vTaskDelay( uipINIT_WAIT );\r
121     }\r
122 \r
123         for( ;; )\r
124         {\r
125                 if( ( ulUIP_Events & uipETHERNET_RX_EVENT ) != 0UL )\r
126                 {               \r
127                         /* Is there received data ready to be processed? */\r
128                         uip_len = ( unsigned short ) ulEMACRead();\r
129                         \r
130                         if( ( uip_len > 0 ) && ( uip_buf != NULL ) )\r
131                         {\r
132                                 /* Standard uIP loop taken from the uIP manual. */\r
133                                 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )\r
134                                 {\r
135                                         uip_arp_ipin();\r
136                                         uip_input();\r
137 \r
138                                         /* If the above function invocation resulted in data that\r
139                                         should be sent out on the network, the global variable\r
140                                         uip_len is set to a value > 0. */\r
141                                         if( uip_len > 0 )\r
142                                         {\r
143                                                 uip_arp_out();\r
144                                                 vEMACWrite();\r
145                                         }\r
146                                 }\r
147                                 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )\r
148                                 {\r
149                                         uip_arp_arpin();\r
150 \r
151                                         /* If the above function invocation resulted in data that\r
152                                         should be sent out on the network, the global variable\r
153                                         uip_len is set to a value > 0. */\r
154                                         if( uip_len > 0 )\r
155                                         {\r
156                                                 vEMACWrite();\r
157                                         }\r
158                                 }\r
159                         }\r
160                         else\r
161                         {\r
162                                 ulUIP_Events &= ~uipETHERNET_RX_EVENT;\r
163                         }\r
164                 }\r
165                 \r
166                 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )\r
167                 {\r
168                         ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;\r
169                                         \r
170                         for( i = 0; i < UIP_CONNS; i++ )\r
171                         {\r
172                                 uip_periodic( i );\r
173 \r
174                                 /* If the above function invocation resulted in data that\r
175                                 should be sent out on the network, the global variable\r
176                                 uip_len is set to a value > 0. */\r
177                                 if( uip_len > 0 )\r
178                                 {\r
179                                         uip_arp_out();\r
180                                         vEMACWrite();\r
181                                 }\r
182                         }\r
183                 }\r
184                 \r
185                 /* Call the ARP timer function every 10 seconds. */\r
186                 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )\r
187                 {\r
188                         ulUIP_Events &= ~uipARP_TIMER_EVENT;\r
189                         uip_arp_timer();\r
190                 }\r
191                         \r
192                 if( ulUIP_Events == pdFALSE )\r
193                 {\r
194                         xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );\r
195                         ulUIP_Events |= ulNewEvent;\r
196                 }\r
197         }\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 static void prvInitialise_uIP( void )\r
202 {\r
203 TimerHandle_t xARPTimer, xPeriodicTimer;\r
204 uip_ipaddr_t xIPAddr;\r
205 const unsigned long ul_uIPEventQueueLength = 10UL;\r
206 \r
207         /* Initialise the uIP stack. */\r
208         uip_init();\r
209         uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
210         uip_sethostaddr( &xIPAddr );\r
211         uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );\r
212         uip_setnetmask( &xIPAddr );\r
213         prvSetMACAddress();\r
214         httpd_init();\r
215 \r
216         /* Create the queue used to sent TCP/IP events to the uIP stack. */\r
217         xEMACEventQueue = xQueueCreate( ul_uIPEventQueueLength, sizeof( unsigned long ) );\r
218 \r
219         /* Create and start the uIP timers. */\r
220         xARPTimer = xTimerCreate(       "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */\r
221                                                                 ( 10000UL / portTICK_PERIOD_MS ), /* Timer period. */\r
222                                                                 pdTRUE, /* Autor-reload. */\r
223                                                                 ( void * ) uipARP_TIMER,\r
224                                                                 prvUIPTimerCallback\r
225                                                         );\r
226 \r
227         xPeriodicTimer = xTimerCreate(  "PeriodicTimer",\r
228                                                                         ( 50 / portTICK_PERIOD_MS ),\r
229                                                                         pdTRUE, /* Autor-reload. */\r
230                                                                         ( void * ) uipPERIODIC_TIMER,\r
231                                                                         prvUIPTimerCallback\r
232                                                                 );\r
233 \r
234         configASSERT( xARPTimer );\r
235         configASSERT( xPeriodicTimer );\r
236 \r
237         xTimerStart( xARPTimer, portMAX_DELAY );\r
238         xTimerStart( xPeriodicTimer, portMAX_DELAY );\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 static void prvUIPTimerCallback( TimerHandle_t xTimer )\r
243 {\r
244 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;\r
245 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;\r
246 \r
247         /* This is a time callback, so calls to xQueueSend() must not attempt to\r
248         block. */\r
249         switch( ( int ) pvTimerGetTimerID( xTimer ) )\r
250         {\r
251                 case uipARP_TIMER               :       xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );\r
252                                                                         break;\r
253 \r
254                 case uipPERIODIC_TIMER  :       xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );\r
255                                                                         break;\r
256 \r
257                 default                                 :       /* Should not get here. */\r
258                                                                         break;\r
259         }\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 static void prvSetMACAddress( void )\r
264 {\r
265 struct uip_eth_addr xAddr;\r
266 \r
267         /* Configure the MAC address in the uIP stack. */\r
268         xAddr.addr[ 0 ] = configMAC_ADDR0;\r
269         xAddr.addr[ 1 ] = configMAC_ADDR1;\r
270         xAddr.addr[ 2 ] = configMAC_ADDR2;\r
271         xAddr.addr[ 3 ] = configMAC_ADDR3;\r
272         xAddr.addr[ 4 ] = configMAC_ADDR4;\r
273         xAddr.addr[ 5 ] = configMAC_ADDR5;\r
274         uip_setethaddr( xAddr );\r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 void vApplicationProcessFormInput( char *pcInputString )\r
279 {\r
280 char *c;\r
281 \r
282         /* Only interested in processing form input if this is the IO page. */\r
283         c = strstr( pcInputString, "io.shtml" );\r
284         \r
285         if( c )\r
286         {\r
287                 /* Is there a command in the string? */\r
288                 c = strstr( pcInputString, "?" );\r
289             if( c )\r
290             {\r
291                         /* Turn the LED's on or off in accordance with the check box status. */\r
292                         if( strstr( c, "LED0=1" ) != NULL )\r
293                         {\r
294                                 /* Turn the LEDs on. */\r
295                                 vParTestSetLED( 7, 1 );\r
296                                 vParTestSetLED( 8, 1 );\r
297                                 vParTestSetLED( 9, 1 );\r
298                                 vParTestSetLED( 10, 1 );\r
299                         }\r
300                         else\r
301                         {\r
302                                 /* Turn the LEDs off. */\r
303                                 vParTestSetLED( 7, 0 );\r
304                                 vParTestSetLED( 8, 0 );\r
305                                 vParTestSetLED( 9, 0 );\r
306                                 vParTestSetLED( 10, 0 );\r
307                         }\r
308             }\r
309                 else\r
310                 {\r
311                         /* Commands to turn LEDs off are not always explicit. */\r
312                         vParTestSetLED( 7, 0 );\r
313                         vParTestSetLED( 8, 0 );\r
314                         vParTestSetLED( 9, 0 );\r
315                         vParTestSetLED( 10, 0 );\r
316                 }\r
317         }\r
318 }\r
319 \r