| 1 |
577 |
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 |
|
|
/* Standard includes. */
|
| 54 |
|
|
#include <string.h>
|
| 55 |
|
|
|
| 56 |
|
|
/* Library includes. */
|
| 57 |
|
|
#include "91x_lib.h"
|
| 58 |
|
|
#include "91x_enet.h"
|
| 59 |
|
|
|
| 60 |
|
|
/* Scheduler includes. */
|
| 61 |
|
|
#include "FreeRTOS.h"
|
| 62 |
|
|
#include "task.h"
|
| 63 |
|
|
#include "semphr.h"
|
| 64 |
|
|
|
| 65 |
|
|
/* uip includes. */
|
| 66 |
|
|
#include "uip.h"
|
| 67 |
|
|
#include "uip_arp.h"
|
| 68 |
|
|
#include "httpd.h"
|
| 69 |
|
|
#include "timer.h"
|
| 70 |
|
|
#include "clock-arch.h"
|
| 71 |
|
|
|
| 72 |
|
|
/*-----------------------------------------------------------*/
|
| 73 |
|
|
|
| 74 |
|
|
/* MAC address configuration. */
|
| 75 |
|
|
#define uipMAC_ADDR0 0x00
|
| 76 |
|
|
#define uipMAC_ADDR1 0x12
|
| 77 |
|
|
#define uipMAC_ADDR2 0x13
|
| 78 |
|
|
#define uipMAC_ADDR3 0x14
|
| 79 |
|
|
#define uipMAC_ADDR4 0x15
|
| 80 |
|
|
#define uipMAC_ADDR5 0x20
|
| 81 |
|
|
|
| 82 |
|
|
/* IP address configuration. */
|
| 83 |
|
|
#define uipIP_ADDR0 172
|
| 84 |
|
|
#define uipIP_ADDR1 25
|
| 85 |
|
|
#define uipIP_ADDR2 218
|
| 86 |
|
|
#define uipIP_ADDR3 11
|
| 87 |
|
|
|
| 88 |
|
|
/* Netmask configuration. */
|
| 89 |
|
|
#define uipNET_MASK0 255
|
| 90 |
|
|
#define uipNET_MASK1 255
|
| 91 |
|
|
#define uipNET_MASK2 255
|
| 92 |
|
|
#define uipNET_MASK3 0
|
| 93 |
|
|
|
| 94 |
|
|
/* Gateway address configuration. */
|
| 95 |
|
|
#define uipGATEWAY_ADDR0 172
|
| 96 |
|
|
#define uipGATEWAY_ADDR1 25
|
| 97 |
|
|
#define uipGATEWAY_ADDR2 218
|
| 98 |
|
|
#define uipGATEWAY_ADDR3 1
|
| 99 |
|
|
|
| 100 |
|
|
/* Shortcut to the header within the Rx buffer. */
|
| 101 |
|
|
#define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
|
| 102 |
|
|
|
| 103 |
|
|
/* uIP update frequencies. */
|
| 104 |
|
|
#define uipMAX_BLOCK_TIME (configTICK_RATE_HZ / 4)
|
| 105 |
|
|
|
| 106 |
|
|
/* Interrupt status bit definition. */
|
| 107 |
|
|
#define uipDMI_RX_CURRENT_DONE 0x8000
|
| 108 |
|
|
|
| 109 |
|
|
/* If no buffers are available, then wait this long before looking again. */
|
| 110 |
|
|
#define uipBUFFER_WAIT_DELAY ( 10 / portTICK_RATE_MS )
|
| 111 |
|
|
#define uipBUFFER_WAIT_ATTEMPTS ( 10 )
|
| 112 |
|
|
|
| 113 |
|
|
/* Standard constant. */
|
| 114 |
|
|
#define uipTOTAL_FRAME_HEADER_SIZE 54
|
| 115 |
|
|
|
| 116 |
|
|
/*-----------------------------------------------------------*/
|
| 117 |
|
|
|
| 118 |
|
|
/*
|
| 119 |
|
|
* Send the uIP buffer to the MAC.
|
| 120 |
|
|
*/
|
| 121 |
|
|
static void prvENET_Send(void);
|
| 122 |
|
|
|
| 123 |
|
|
/*
|
| 124 |
|
|
* Setup the MAC address in the MAC itself, and in the uIP stack.
|
| 125 |
|
|
*/
|
| 126 |
|
|
static void prvSetMACAddress( void );
|
| 127 |
|
|
|
| 128 |
|
|
/*
|
| 129 |
|
|
* Used to return a pointer to the next buffer to be used.
|
| 130 |
|
|
*/
|
| 131 |
|
|
extern unsigned char *pcGetNextBuffer( void );
|
| 132 |
|
|
|
| 133 |
|
|
/*
|
| 134 |
|
|
* Port functions required by the uIP stack.
|
| 135 |
|
|
*/
|
| 136 |
|
|
void clock_init( void );
|
| 137 |
|
|
clock_time_t clock_time( void );
|
| 138 |
|
|
|
| 139 |
|
|
/*-----------------------------------------------------------*/
|
| 140 |
|
|
|
| 141 |
|
|
/* The semaphore used by the ISR to wake the uIP task. */
|
| 142 |
|
|
xSemaphoreHandle xSemaphore = NULL;
|
| 143 |
|
|
|
| 144 |
|
|
/*-----------------------------------------------------------*/
|
| 145 |
|
|
|
| 146 |
|
|
void clock_init(void)
|
| 147 |
|
|
{
|
| 148 |
|
|
/* This is done when the scheduler starts. */
|
| 149 |
|
|
}
|
| 150 |
|
|
/*-----------------------------------------------------------*/
|
| 151 |
|
|
|
| 152 |
|
|
clock_time_t clock_time( void )
|
| 153 |
|
|
{
|
| 154 |
|
|
return xTaskGetTickCount();
|
| 155 |
|
|
}
|
| 156 |
|
|
/*-----------------------------------------------------------*/
|
| 157 |
|
|
|
| 158 |
|
|
void vuIP_Task( void *pvParameters )
|
| 159 |
|
|
{
|
| 160 |
|
|
portBASE_TYPE i;
|
| 161 |
|
|
uip_ipaddr_t xIPAddr;
|
| 162 |
|
|
struct timer periodic_timer, arp_timer;
|
| 163 |
|
|
|
| 164 |
|
|
/* Create the semaphore used by the ISR to wake this task. */
|
| 165 |
|
|
vSemaphoreCreateBinary( xSemaphore );
|
| 166 |
|
|
|
| 167 |
|
|
/* Initialise the uIP stack. */
|
| 168 |
|
|
timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );
|
| 169 |
|
|
timer_set( &arp_timer, configTICK_RATE_HZ * 10 );
|
| 170 |
|
|
uip_init();
|
| 171 |
|
|
uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );
|
| 172 |
|
|
uip_sethostaddr( xIPAddr );
|
| 173 |
|
|
uip_ipaddr( xIPAddr, uipNET_MASK0, uipNET_MASK1, uipNET_MASK2, uipNET_MASK3 );
|
| 174 |
|
|
uip_setnetmask( xIPAddr );
|
| 175 |
|
|
uip_ipaddr( xIPAddr, uipGATEWAY_ADDR0, uipGATEWAY_ADDR1, uipGATEWAY_ADDR2, uipGATEWAY_ADDR3 );
|
| 176 |
|
|
uip_setdraddr( xIPAddr );
|
| 177 |
|
|
httpd_init();
|
| 178 |
|
|
|
| 179 |
|
|
/* Initialise the MAC. */
|
| 180 |
|
|
ENET_InitClocksGPIO();
|
| 181 |
|
|
ENET_Init();
|
| 182 |
|
|
portENTER_CRITICAL();
|
| 183 |
|
|
{
|
| 184 |
|
|
ENET_Start();
|
| 185 |
|
|
prvSetMACAddress();
|
| 186 |
|
|
VIC_Config( ENET_ITLine, VIC_IRQ, 1 );
|
| 187 |
|
|
VIC_ITCmd( ENET_ITLine, ENABLE );
|
| 188 |
|
|
ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;
|
| 189 |
|
|
ENET_DMA->IER = uipDMI_RX_CURRENT_DONE;
|
| 190 |
|
|
}
|
| 191 |
|
|
portEXIT_CRITICAL();
|
| 192 |
|
|
|
| 193 |
|
|
|
| 194 |
|
|
while(1)
|
| 195 |
|
|
{
|
| 196 |
|
|
/* Is there received data ready to be processed? */
|
| 197 |
|
|
uip_len = ENET_HandleRxPkt( uip_buf );
|
| 198 |
|
|
|
| 199 |
|
|
if( uip_len > 0 )
|
| 200 |
|
|
{
|
| 201 |
|
|
/* Standard uIP loop taken from the uIP manual. */
|
| 202 |
|
|
if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
|
| 203 |
|
|
{
|
| 204 |
|
|
uip_arp_ipin();
|
| 205 |
|
|
uip_input();
|
| 206 |
|
|
|
| 207 |
|
|
/* If the above function invocation resulted in data that
|
| 208 |
|
|
should be sent out on the network, the global variable
|
| 209 |
|
|
uip_len is set to a value > 0. */
|
| 210 |
|
|
if( uip_len > 0 )
|
| 211 |
|
|
{
|
| 212 |
|
|
uip_arp_out();
|
| 213 |
|
|
prvENET_Send();
|
| 214 |
|
|
}
|
| 215 |
|
|
}
|
| 216 |
|
|
else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
|
| 217 |
|
|
{
|
| 218 |
|
|
uip_arp_arpin();
|
| 219 |
|
|
|
| 220 |
|
|
/* If the above function invocation resulted in data that
|
| 221 |
|
|
should be sent out on the network, the global variable
|
| 222 |
|
|
uip_len is set to a value > 0. */
|
| 223 |
|
|
if( uip_len > 0 )
|
| 224 |
|
|
{
|
| 225 |
|
|
prvENET_Send();
|
| 226 |
|
|
}
|
| 227 |
|
|
}
|
| 228 |
|
|
}
|
| 229 |
|
|
else
|
| 230 |
|
|
{
|
| 231 |
|
|
if( timer_expired( &periodic_timer ) )
|
| 232 |
|
|
{
|
| 233 |
|
|
timer_reset( &periodic_timer );
|
| 234 |
|
|
for( i = 0; i < UIP_CONNS; i++ )
|
| 235 |
|
|
{
|
| 236 |
|
|
uip_periodic( i );
|
| 237 |
|
|
|
| 238 |
|
|
/* If the above function invocation resulted in data that
|
| 239 |
|
|
should be sent out on the network, the global variable
|
| 240 |
|
|
uip_len is set to a value > 0. */
|
| 241 |
|
|
if( uip_len > 0 )
|
| 242 |
|
|
{
|
| 243 |
|
|
uip_arp_out();
|
| 244 |
|
|
prvENET_Send();
|
| 245 |
|
|
}
|
| 246 |
|
|
}
|
| 247 |
|
|
|
| 248 |
|
|
/* Call the ARP timer function every 10 seconds. */
|
| 249 |
|
|
if( timer_expired( &arp_timer ) )
|
| 250 |
|
|
{
|
| 251 |
|
|
timer_reset( &arp_timer );
|
| 252 |
|
|
uip_arp_timer();
|
| 253 |
|
|
}
|
| 254 |
|
|
}
|
| 255 |
|
|
else
|
| 256 |
|
|
{
|
| 257 |
|
|
/* We did not receive a packet, and there was no periodic
|
| 258 |
|
|
processing to perform. Block for a fixed period. If a packet
|
| 259 |
|
|
is received during this period we will be woken by the ISR
|
| 260 |
|
|
giving us the Semaphore. */
|
| 261 |
|
|
xSemaphoreTake( xSemaphore, configTICK_RATE_HZ / 2 );
|
| 262 |
|
|
}
|
| 263 |
|
|
}
|
| 264 |
|
|
}
|
| 265 |
|
|
}
|
| 266 |
|
|
/*-----------------------------------------------------------*/
|
| 267 |
|
|
|
| 268 |
|
|
static void prvENET_Send(void)
|
| 269 |
|
|
{
|
| 270 |
|
|
portBASE_TYPE i;
|
| 271 |
|
|
static unsigned char *pcTxData;
|
| 272 |
|
|
|
| 273 |
|
|
/* Get a DMA buffer into which we can write the data to send. */
|
| 274 |
|
|
for( i = 0; i < uipBUFFER_WAIT_ATTEMPTS; i++ )
|
| 275 |
|
|
{
|
| 276 |
|
|
pcTxData = pcGetNextBuffer();
|
| 277 |
|
|
|
| 278 |
|
|
if( pcTxData )
|
| 279 |
|
|
{
|
| 280 |
|
|
break;
|
| 281 |
|
|
}
|
| 282 |
|
|
else
|
| 283 |
|
|
{
|
| 284 |
|
|
vTaskDelay( uipBUFFER_WAIT_DELAY );
|
| 285 |
|
|
}
|
| 286 |
|
|
}
|
| 287 |
|
|
|
| 288 |
|
|
if( pcTxData )
|
| 289 |
|
|
{
|
| 290 |
|
|
/* Copy the header into the Tx buffer. */
|
| 291 |
|
|
memcpy( ( void * ) pcTxData, ( void * ) uip_buf, uipTOTAL_FRAME_HEADER_SIZE );
|
| 292 |
|
|
if( uip_len > uipTOTAL_FRAME_HEADER_SIZE )
|
| 293 |
|
|
{
|
| 294 |
|
|
memcpy( ( void * ) &( pcTxData[ uipTOTAL_FRAME_HEADER_SIZE ] ), ( void * ) uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) );
|
| 295 |
|
|
}
|
| 296 |
|
|
|
| 297 |
|
|
ENET_TxPkt( &pcTxData, uip_len );
|
| 298 |
|
|
}
|
| 299 |
|
|
}
|
| 300 |
|
|
/*-----------------------------------------------------------*/
|
| 301 |
|
|
|
| 302 |
|
|
void ENET_IRQHandler(void)
|
| 303 |
|
|
{
|
| 304 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
| 305 |
|
|
|
| 306 |
|
|
/* Give the semaphore in case the uIP task needs waking. */
|
| 307 |
|
|
xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
|
| 308 |
|
|
|
| 309 |
|
|
/* Clear the interrupt. */
|
| 310 |
|
|
ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;
|
| 311 |
|
|
|
| 312 |
|
|
/* Switch tasks if necessary. */
|
| 313 |
|
|
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
|
| 314 |
|
|
}
|
| 315 |
|
|
/*-----------------------------------------------------------*/
|
| 316 |
|
|
|
| 317 |
|
|
static void prvSetMACAddress( void )
|
| 318 |
|
|
{
|
| 319 |
|
|
struct uip_eth_addr xAddr;
|
| 320 |
|
|
|
| 321 |
|
|
/* Configure the MAC address in the uIP stack. */
|
| 322 |
|
|
xAddr.addr[ 0 ] = uipMAC_ADDR0;
|
| 323 |
|
|
xAddr.addr[ 1 ] = uipMAC_ADDR1;
|
| 324 |
|
|
xAddr.addr[ 2 ] = uipMAC_ADDR2;
|
| 325 |
|
|
xAddr.addr[ 3 ] = uipMAC_ADDR3;
|
| 326 |
|
|
xAddr.addr[ 4 ] = uipMAC_ADDR4;
|
| 327 |
|
|
xAddr.addr[ 5 ] = uipMAC_ADDR5;
|
| 328 |
|
|
uip_setethaddr( xAddr );
|
| 329 |
|
|
|
| 330 |
|
|
/* Write the MAC address to the MAC. */
|
| 331 |
|
|
ENET_MAC->MAL = ( uipMAC_ADDR3 << 24 ) | ( uipMAC_ADDR2 << 16 ) | ( uipMAC_ADDR1 << 8 ) | ( uipMAC_ADDR0 );
|
| 332 |
|
|
ENET_MAC->MAH = ( uipMAC_ADDR5 << 8 ) | ( uipMAC_ADDR4 );
|
| 333 |
|
|
}
|
| 334 |
|
|
|