]> begriffs open source - cmsis-freertos/blob - Demo/RX600_RX63N-RDK_Renesas/RTOSDemo/uIP_Task.c
Updated pack to FreeRTOS 10.3.1
[cmsis-freertos] / Demo / RX600_RX63N-RDK_Renesas / RTOSDemo / uIP_Task.c
1 /*
2  * FreeRTOS Kernel V10.3.1
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /* Standard includes. */
29 #include <string.h>
30
31 /* Scheduler includes. */
32 #include "FreeRTOS.h"
33 #include "task.h"
34 #include "timers.h"
35 #include "queue.h"
36
37 /* uip includes. */
38 #include "net/uip.h"
39 #include "net/uip_arp.h"
40 #include "apps/httpd/httpd.h"
41 #include "sys/timer.h"
42 #include "net/clock-arch.h"
43 #include "r_ether.h"
44
45 /* Demo includes. */
46 #include "ParTest.h"
47
48 /*-----------------------------------------------------------*/
49
50 /* How long to wait before attempting to connect the MAC again. */
51 #define uipINIT_WAIT    ( 100 / portTICK_PERIOD_MS )
52
53 /* Shortcut to the header within the Rx buffer. */
54 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
55
56 /* Standard constant. */
57 #define uipTOTAL_FRAME_HEADER_SIZE      54
58
59 /* The ARP timer and the periodic timer share a callback function, so the
60 respective timer IDs are used to determine which timer actually expired.  These
61 constants are assigned to the timer IDs. */
62 #define uipARP_TIMER                            0
63 #define uipPERIODIC_TIMER                       1
64
65 /* A block time of zero ticks simply means, "don't block". */
66 #define uipDONT_BLOCK                           0UL
67
68 /*-----------------------------------------------------------*/
69
70 /*
71  * Setup the MAC address in the MAC itself, and in the uIP stack.
72  */
73 static void prvSetMACAddress( void );
74
75 /*
76  * Perform any uIP initialisation necessary.
77  */
78 static void prvInitialise_uIP( void );
79
80 /*
81  * The callback function that is assigned to both the periodic timer and the
82  * ARP timer.
83  */
84 static void prvUIPTimerCallback( TimerHandle_t xTimer );
85
86 /*
87  * Port functions required by the uIP stack.
88  */
89 clock_time_t clock_time( void );
90
91 /*-----------------------------------------------------------*/
92
93 /* The queue used to send TCP/IP events to the uIP stack. */
94 QueueHandle_t xEMACEventQueue = NULL;
95
96 /*-----------------------------------------------------------*/
97
98 clock_time_t clock_time( void )
99 {
100         return xTaskGetTickCount();
101 }
102 /*-----------------------------------------------------------*/
103
104 void vuIP_Task( void *pvParameters )
105 {
106 portBASE_TYPE i;
107 unsigned long ulNewEvent = 0UL;
108 unsigned long ulUIP_Events = 0UL;
109
110         ( void ) pvParameters;
111
112         /* Initialise the uIP stack. */
113         prvInitialise_uIP();
114
115         /* Initialise the MAC. */
116         vInitEmac();
117
118         while( lEMACWaitForLink() != pdPASS )
119     {
120         vTaskDelay( uipINIT_WAIT );
121     }
122
123         for( ;; )
124         {
125                 if( ( ulUIP_Events & uipETHERNET_RX_EVENT ) != 0UL )
126                 {
127                         /* Is there received data ready to be processed? */
128                         uip_len = ( unsigned short ) ulEMACRead();
129
130                         if( ( uip_len > 0 ) && ( uip_buf != NULL ) )
131                         {
132                                 /* Standard uIP loop taken from the uIP manual. */
133                                 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
134                                 {
135                                         uip_arp_ipin();
136                                         uip_input();
137
138                                         /* If the above function invocation resulted in data that
139                                         should be sent out on the network, the global variable
140                                         uip_len is set to a value > 0. */
141                                         if( uip_len > 0 )
142                                         {
143                                                 uip_arp_out();
144                                                 vEMACWrite();
145                                         }
146                                 }
147                                 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
148                                 {
149                                         uip_arp_arpin();
150
151                                         /* If the above function invocation resulted in data that
152                                         should be sent out on the network, the global variable
153                                         uip_len is set to a value > 0. */
154                                         if( uip_len > 0 )
155                                         {
156                                                 vEMACWrite();
157                                         }
158                                 }
159                         }
160                         else
161                         {
162                                 ulUIP_Events &= ~uipETHERNET_RX_EVENT;
163                         }
164                 }
165
166                 if( ( ulUIP_Events & uipPERIODIC_TIMER_EVENT ) != 0UL )
167                 {
168                         ulUIP_Events &= ~uipPERIODIC_TIMER_EVENT;
169
170                         for( i = 0; i < UIP_CONNS; i++ )
171                         {
172                                 uip_periodic( i );
173
174                                 /* If the above function invocation resulted in data that
175                                 should be sent out on the network, the global variable
176                                 uip_len is set to a value > 0. */
177                                 if( uip_len > 0 )
178                                 {
179                                         uip_arp_out();
180                                         vEMACWrite();
181                                 }
182                         }
183                 }
184
185                 /* Call the ARP timer function every 10 seconds. */
186                 if( ( ulUIP_Events & uipARP_TIMER_EVENT ) != 0 )
187                 {
188                         ulUIP_Events &= ~uipARP_TIMER_EVENT;
189                         uip_arp_timer();
190                 }
191
192                 if( ulUIP_Events == pdFALSE )
193                 {
194                         xQueueReceive( xEMACEventQueue, &ulNewEvent, portMAX_DELAY );
195                         ulUIP_Events |= ulNewEvent;
196                 }
197         }
198 }
199 /*-----------------------------------------------------------*/
200
201 static void prvInitialise_uIP( void )
202 {
203 TimerHandle_t xARPTimer, xPeriodicTimer;
204 uip_ipaddr_t xIPAddr;
205 const unsigned long ul_uIPEventQueueLength = 10UL;
206
207         /* Initialise the uIP stack. */
208         uip_init();
209         uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
210         uip_sethostaddr( &xIPAddr );
211         uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
212         uip_setnetmask( &xIPAddr );
213         prvSetMACAddress();
214         httpd_init();
215
216         /* Create the queue used to sent TCP/IP events to the uIP stack. */
217         xEMACEventQueue = xQueueCreate( ul_uIPEventQueueLength, sizeof( unsigned long ) );
218
219         /* Create and start the uIP timers. */
220         xARPTimer = xTimerCreate(       "ARPTimer", /* Just a name that is helpful for debugging, not used by the kernel. */
221                                                                 ( 10000UL / portTICK_PERIOD_MS ), /* Timer period. */
222                                                                 pdTRUE, /* Autor-reload. */
223                                                                 ( void * ) uipARP_TIMER,
224                                                                 prvUIPTimerCallback
225                                                         );
226
227         xPeriodicTimer = xTimerCreate(  "PeriodicTimer",
228                                                                         ( 50 / portTICK_PERIOD_MS ),
229                                                                         pdTRUE, /* Autor-reload. */
230                                                                         ( void * ) uipPERIODIC_TIMER,
231                                                                         prvUIPTimerCallback
232                                                                 );
233
234         configASSERT( xARPTimer );
235         configASSERT( xPeriodicTimer );
236
237         xTimerStart( xARPTimer, portMAX_DELAY );
238         xTimerStart( xPeriodicTimer, portMAX_DELAY );
239 }
240 /*-----------------------------------------------------------*/
241
242 static void prvUIPTimerCallback( TimerHandle_t xTimer )
243 {
244 static const unsigned long ulARPTimerExpired = uipARP_TIMER_EVENT;
245 static const unsigned long ulPeriodicTimerExpired = uipPERIODIC_TIMER_EVENT;
246
247         /* This is a time callback, so calls to xQueueSend() must not attempt to
248         block. */
249         switch( ( int ) pvTimerGetTimerID( xTimer ) )
250         {
251                 case uipARP_TIMER               :       xQueueSend( xEMACEventQueue, &ulARPTimerExpired, uipDONT_BLOCK );
252                                                                         break;
253
254                 case uipPERIODIC_TIMER  :       xQueueSend( xEMACEventQueue, &ulPeriodicTimerExpired, uipDONT_BLOCK );
255                                                                         break;
256
257                 default                                 :       /* Should not get here. */
258                                                                         break;
259         }
260 }
261 /*-----------------------------------------------------------*/
262
263 static void prvSetMACAddress( void )
264 {
265 struct uip_eth_addr xAddr;
266
267         /* Configure the MAC address in the uIP stack. */
268         xAddr.addr[ 0 ] = configMAC_ADDR0;
269         xAddr.addr[ 1 ] = configMAC_ADDR1;
270         xAddr.addr[ 2 ] = configMAC_ADDR2;
271         xAddr.addr[ 3 ] = configMAC_ADDR3;
272         xAddr.addr[ 4 ] = configMAC_ADDR4;
273         xAddr.addr[ 5 ] = configMAC_ADDR5;
274         uip_setethaddr( xAddr );
275 }
276 /*-----------------------------------------------------------*/
277
278 void vApplicationProcessFormInput( char *pcInputString )
279 {
280 char *c;
281
282         /* Only interested in processing form input if this is the IO page. */
283         c = strstr( pcInputString, "io.shtml" );
284
285         if( c )
286         {
287                 /* Is there a command in the string? */
288                 c = strstr( pcInputString, "?" );
289             if( c )
290             {
291                         /* Turn the LED's on or off in accordance with the check box status. */
292                         if( strstr( c, "LED0=1" ) != NULL )
293                         {
294                                 /* Turn the LEDs on. */
295                                 vParTestSetLED( 7, 1 );
296                                 vParTestSetLED( 8, 1 );
297                                 vParTestSetLED( 9, 1 );
298                                 vParTestSetLED( 10, 1 );
299                         }
300                         else
301                         {
302                                 /* Turn the LEDs off. */
303                                 vParTestSetLED( 7, 0 );
304                                 vParTestSetLED( 8, 0 );
305                                 vParTestSetLED( 9, 0 );
306                                 vParTestSetLED( 10, 0 );
307                         }
308             }
309                 else
310                 {
311                         /* Commands to turn LEDs off are not always explicit. */
312                         vParTestSetLED( 7, 0 );
313                         vParTestSetLED( 8, 0 );
314                         vParTestSetLED( 9, 0 );
315                         vParTestSetLED( 10, 0 );
316                 }
317         }
318 }
319