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