| 1 |
585 |
jeremybenn |
/*
|
| 2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
| 3 |
|
|
|
| 4 |
|
|
***************************************************************************
|
| 5 |
|
|
* *
|
| 6 |
|
|
* If you are: *
|
| 7 |
|
|
* *
|
| 8 |
|
|
* + New to FreeRTOS, *
|
| 9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
| 10 |
|
|
* + Looking for basic training, *
|
| 11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
| 12 |
|
|
* *
|
| 13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
| 14 |
|
|
* *
|
| 15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
| 16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
| 17 |
|
|
* *
|
| 18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
| 19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
| 20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
| 21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
| 22 |
|
|
* *
|
| 23 |
|
|
***************************************************************************
|
| 24 |
|
|
|
| 25 |
|
|
This file is part of the FreeRTOS distribution.
|
| 26 |
|
|
|
| 27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
| 28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
| 29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
| 30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
| 31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
| 32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
| 33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
| 34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
| 35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
| 36 |
|
|
more details. You should have received a copy of the GNU General Public
|
| 37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
| 38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
| 39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
| 40 |
|
|
FreeRTOS WEB site.
|
| 41 |
|
|
|
| 42 |
|
|
1 tab == 4 spaces!
|
| 43 |
|
|
|
| 44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
| 45 |
|
|
contact details.
|
| 46 |
|
|
|
| 47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
| 48 |
|
|
critical systems.
|
| 49 |
|
|
|
| 50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
| 51 |
|
|
licensing and training services.
|
| 52 |
|
|
*/
|
| 53 |
|
|
|
| 54 |
|
|
/* Standard includes. */
|
| 55 |
|
|
#include <string.h>
|
| 56 |
|
|
|
| 57 |
|
|
/* Scheduler includes. */
|
| 58 |
|
|
#include "FreeRTOS.h"
|
| 59 |
|
|
#include "task.h"
|
| 60 |
|
|
#include "semphr.h"
|
| 61 |
|
|
|
| 62 |
|
|
/* uip includes. */
|
| 63 |
|
|
#include "net/uip.h"
|
| 64 |
|
|
#include "net/uip_arp.h"
|
| 65 |
|
|
#include "apps/httpd/httpd.h"
|
| 66 |
|
|
#include "sys/timer.h"
|
| 67 |
|
|
#include "net/clock-arch.h"
|
| 68 |
|
|
#include "r_ether.h"
|
| 69 |
|
|
|
| 70 |
|
|
/* Demo includes. */
|
| 71 |
|
|
#include "ParTest.h"
|
| 72 |
|
|
|
| 73 |
|
|
/*-----------------------------------------------------------*/
|
| 74 |
|
|
|
| 75 |
|
|
/* How long to wait before attempting to connect the MAC again. */
|
| 76 |
|
|
#define uipINIT_WAIT ( 100 / portTICK_RATE_MS )
|
| 77 |
|
|
|
| 78 |
|
|
/* Shortcut to the header within the Rx buffer. */
|
| 79 |
|
|
#define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
|
| 80 |
|
|
|
| 81 |
|
|
/* Standard constant. */
|
| 82 |
|
|
#define uipTOTAL_FRAME_HEADER_SIZE 54
|
| 83 |
|
|
|
| 84 |
|
|
/*-----------------------------------------------------------*/
|
| 85 |
|
|
|
| 86 |
|
|
/*
|
| 87 |
|
|
* Setup the MAC address in the MAC itself, and in the uIP stack.
|
| 88 |
|
|
*/
|
| 89 |
|
|
static void prvSetMACAddress( void );
|
| 90 |
|
|
|
| 91 |
|
|
/*
|
| 92 |
|
|
* Port functions required by the uIP stack.
|
| 93 |
|
|
*/
|
| 94 |
|
|
void clock_init( void );
|
| 95 |
|
|
clock_time_t clock_time( void );
|
| 96 |
|
|
|
| 97 |
|
|
/*-----------------------------------------------------------*/
|
| 98 |
|
|
|
| 99 |
|
|
/* The semaphore used by the ISR to wake the uIP task. */
|
| 100 |
|
|
xSemaphoreHandle xEMACSemaphore = NULL;
|
| 101 |
|
|
|
| 102 |
|
|
/*-----------------------------------------------------------*/
|
| 103 |
|
|
|
| 104 |
|
|
void clock_init(void)
|
| 105 |
|
|
{
|
| 106 |
|
|
/* This is done when the scheduler starts. */
|
| 107 |
|
|
}
|
| 108 |
|
|
/*-----------------------------------------------------------*/
|
| 109 |
|
|
|
| 110 |
|
|
clock_time_t clock_time( void )
|
| 111 |
|
|
{
|
| 112 |
|
|
return xTaskGetTickCount();
|
| 113 |
|
|
}
|
| 114 |
|
|
/*-----------------------------------------------------------*/
|
| 115 |
|
|
|
| 116 |
|
|
void vuIP_Task( void *pvParameters )
|
| 117 |
|
|
{
|
| 118 |
|
|
portBASE_TYPE i, xDoneSomething;
|
| 119 |
|
|
uip_ipaddr_t xIPAddr;
|
| 120 |
|
|
struct timer periodic_timer, arp_timer;
|
| 121 |
|
|
|
| 122 |
|
|
( void ) pvParameters;
|
| 123 |
|
|
|
| 124 |
|
|
/* Initialise the uIP stack. */
|
| 125 |
|
|
timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );
|
| 126 |
|
|
timer_set( &arp_timer, configTICK_RATE_HZ * 10 );
|
| 127 |
|
|
uip_init();
|
| 128 |
|
|
uip_ipaddr( &xIPAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
|
| 129 |
|
|
uip_sethostaddr( &xIPAddr );
|
| 130 |
|
|
uip_ipaddr( &xIPAddr, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 );
|
| 131 |
|
|
uip_setnetmask( &xIPAddr );
|
| 132 |
|
|
prvSetMACAddress();
|
| 133 |
|
|
httpd_init();
|
| 134 |
|
|
|
| 135 |
|
|
/* Create the semaphore used to wake the uIP task. */
|
| 136 |
|
|
vSemaphoreCreateBinary( xEMACSemaphore );
|
| 137 |
|
|
|
| 138 |
|
|
/* Initialise the MAC. */
|
| 139 |
|
|
vInitEmac();
|
| 140 |
|
|
|
| 141 |
|
|
while( lEMACWaitForLink() != pdPASS )
|
| 142 |
|
|
{
|
| 143 |
|
|
vTaskDelay( uipINIT_WAIT );
|
| 144 |
|
|
}
|
| 145 |
|
|
|
| 146 |
|
|
for( ;; )
|
| 147 |
|
|
{
|
| 148 |
|
|
xDoneSomething = pdFALSE;
|
| 149 |
|
|
|
| 150 |
|
|
/* Is there received data ready to be processed? */
|
| 151 |
|
|
uip_len = ( unsigned short ) ulEMACRead();
|
| 152 |
|
|
|
| 153 |
|
|
if( ( uip_len > 0 ) && ( uip_buf != NULL ) )
|
| 154 |
|
|
{
|
| 155 |
|
|
/* Standard uIP loop taken from the uIP manual. */
|
| 156 |
|
|
if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
|
| 157 |
|
|
{
|
| 158 |
|
|
uip_arp_ipin();
|
| 159 |
|
|
uip_input();
|
| 160 |
|
|
|
| 161 |
|
|
/* If the above function invocation resulted in data that
|
| 162 |
|
|
should be sent out on the network, the global variable
|
| 163 |
|
|
uip_len is set to a value > 0. */
|
| 164 |
|
|
if( uip_len > 0 )
|
| 165 |
|
|
{
|
| 166 |
|
|
uip_arp_out();
|
| 167 |
|
|
vEMACWrite();
|
| 168 |
|
|
}
|
| 169 |
|
|
|
| 170 |
|
|
xDoneSomething = pdTRUE;
|
| 171 |
|
|
}
|
| 172 |
|
|
else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
|
| 173 |
|
|
{
|
| 174 |
|
|
uip_arp_arpin();
|
| 175 |
|
|
|
| 176 |
|
|
/* If the above function invocation resulted in data that
|
| 177 |
|
|
should be sent out on the network, the global variable
|
| 178 |
|
|
uip_len is set to a value > 0. */
|
| 179 |
|
|
if( uip_len > 0 )
|
| 180 |
|
|
{
|
| 181 |
|
|
vEMACWrite();
|
| 182 |
|
|
}
|
| 183 |
|
|
|
| 184 |
|
|
xDoneSomething = pdTRUE;
|
| 185 |
|
|
}
|
| 186 |
|
|
}
|
| 187 |
|
|
|
| 188 |
|
|
if( timer_expired( &periodic_timer ) && ( uip_buf != NULL ) )
|
| 189 |
|
|
{
|
| 190 |
|
|
timer_reset( &periodic_timer );
|
| 191 |
|
|
for( i = 0; i < UIP_CONNS; i++ )
|
| 192 |
|
|
{
|
| 193 |
|
|
uip_periodic( i );
|
| 194 |
|
|
|
| 195 |
|
|
/* If the above function invocation resulted in data that
|
| 196 |
|
|
should be sent out on the network, the global variable
|
| 197 |
|
|
uip_len is set to a value > 0. */
|
| 198 |
|
|
if( uip_len > 0 )
|
| 199 |
|
|
{
|
| 200 |
|
|
uip_arp_out();
|
| 201 |
|
|
vEMACWrite();
|
| 202 |
|
|
}
|
| 203 |
|
|
}
|
| 204 |
|
|
|
| 205 |
|
|
/* Call the ARP timer function every 10 seconds. */
|
| 206 |
|
|
if( timer_expired( &arp_timer ) )
|
| 207 |
|
|
{
|
| 208 |
|
|
timer_reset( &arp_timer );
|
| 209 |
|
|
uip_arp_timer();
|
| 210 |
|
|
}
|
| 211 |
|
|
|
| 212 |
|
|
xDoneSomething = pdTRUE;
|
| 213 |
|
|
}
|
| 214 |
|
|
|
| 215 |
|
|
if( xDoneSomething == pdFALSE )
|
| 216 |
|
|
{
|
| 217 |
|
|
/* We did not receive a packet, and there was no periodic
|
| 218 |
|
|
processing to perform. Block for a fixed period. If a packet
|
| 219 |
|
|
is received during this period we will be woken by the ISR
|
| 220 |
|
|
giving us the Semaphore. */
|
| 221 |
|
|
xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 20 );
|
| 222 |
|
|
}
|
| 223 |
|
|
}
|
| 224 |
|
|
}
|
| 225 |
|
|
/*-----------------------------------------------------------*/
|
| 226 |
|
|
|
| 227 |
|
|
static void prvSetMACAddress( void )
|
| 228 |
|
|
{
|
| 229 |
|
|
struct uip_eth_addr xAddr;
|
| 230 |
|
|
|
| 231 |
|
|
/* Configure the MAC address in the uIP stack. */
|
| 232 |
|
|
xAddr.addr[ 0 ] = configMAC_ADDR0;
|
| 233 |
|
|
xAddr.addr[ 1 ] = configMAC_ADDR1;
|
| 234 |
|
|
xAddr.addr[ 2 ] = configMAC_ADDR2;
|
| 235 |
|
|
xAddr.addr[ 3 ] = configMAC_ADDR3;
|
| 236 |
|
|
xAddr.addr[ 4 ] = configMAC_ADDR4;
|
| 237 |
|
|
xAddr.addr[ 5 ] = configMAC_ADDR5;
|
| 238 |
|
|
uip_setethaddr( xAddr );
|
| 239 |
|
|
}
|
| 240 |
|
|
/*-----------------------------------------------------------*/
|
| 241 |
|
|
|
| 242 |
|
|
void vApplicationProcessFormInput( char *pcInputString )
|
| 243 |
|
|
{
|
| 244 |
|
|
char *c;
|
| 245 |
|
|
|
| 246 |
|
|
/* Only interested in processing form input if this is the IO page. */
|
| 247 |
|
|
c = strstr( pcInputString, "io.shtml" );
|
| 248 |
|
|
|
| 249 |
|
|
if( c )
|
| 250 |
|
|
{
|
| 251 |
|
|
/* Is there a command in the string? */
|
| 252 |
|
|
c = strstr( pcInputString, "?" );
|
| 253 |
|
|
if( c )
|
| 254 |
|
|
{
|
| 255 |
|
|
/* Turn the LED's on or off in accordance with the check box status. */
|
| 256 |
|
|
if( strstr( c, "LED0=1" ) != NULL )
|
| 257 |
|
|
{
|
| 258 |
|
|
/* Turn LEDs on. */
|
| 259 |
|
|
vParTestSetLED( 3, 1 );
|
| 260 |
|
|
vParTestSetLED( 4, 1 );
|
| 261 |
|
|
}
|
| 262 |
|
|
else
|
| 263 |
|
|
{
|
| 264 |
|
|
/* Turn LED 4 off. */
|
| 265 |
|
|
vParTestSetLED( 3, 0 );
|
| 266 |
|
|
vParTestSetLED( 4, 0 );
|
| 267 |
|
|
}
|
| 268 |
|
|
}
|
| 269 |
|
|
else
|
| 270 |
|
|
{
|
| 271 |
|
|
/* Commands to turn LEDs off are not always explicit, turn LED 4
|
| 272 |
|
|
off. */
|
| 273 |
|
|
vParTestSetLED( 3, 0 );
|
| 274 |
|
|
vParTestSetLED( 4, 0 );
|
| 275 |
|
|
}
|
| 276 |
|
|
}
|
| 277 |
|
|
}
|