2 FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
53 /* Standard includes. */
\r
56 /* Library includes. */
\r
57 #include "91x_lib.h"
\r
58 #include "91x_enet.h"
\r
60 /* Scheduler includes. */
\r
61 #include "FreeRTOS.h"
\r
67 #include "uip_arp.h"
\r
70 #include "clock-arch.h"
\r
72 /*-----------------------------------------------------------*/
\r
74 /* MAC address configuration. */
\r
75 #define uipMAC_ADDR0 0x00
\r
76 #define uipMAC_ADDR1 0x12
\r
77 #define uipMAC_ADDR2 0x13
\r
78 #define uipMAC_ADDR3 0x14
\r
79 #define uipMAC_ADDR4 0x15
\r
80 #define uipMAC_ADDR5 0x20
\r
82 /* IP address configuration. */
\r
83 #define uipIP_ADDR0 172
\r
84 #define uipIP_ADDR1 25
\r
85 #define uipIP_ADDR2 218
\r
86 #define uipIP_ADDR3 11
\r
88 /* Netmask configuration. */
\r
89 #define uipNET_MASK0 255
\r
90 #define uipNET_MASK1 255
\r
91 #define uipNET_MASK2 255
\r
92 #define uipNET_MASK3 0
\r
94 /* Gateway address configuration. */
\r
95 #define uipGATEWAY_ADDR0 172
\r
96 #define uipGATEWAY_ADDR1 25
\r
97 #define uipGATEWAY_ADDR2 218
\r
98 #define uipGATEWAY_ADDR3 1
\r
100 /* Shortcut to the header within the Rx buffer. */
\r
101 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
103 /* uIP update frequencies. */
\r
104 #define uipMAX_BLOCK_TIME (configTICK_RATE_HZ / 4)
\r
106 /* Interrupt status bit definition. */
\r
107 #define uipDMI_RX_CURRENT_DONE 0x8000
\r
109 /* If no buffers are available, then wait this long before looking again. */
\r
110 #define uipBUFFER_WAIT_DELAY ( 10 / portTICK_RATE_MS )
\r
111 #define uipBUFFER_WAIT_ATTEMPTS ( 10 )
\r
113 /* Standard constant. */
\r
114 #define uipTOTAL_FRAME_HEADER_SIZE 54
\r
116 /*-----------------------------------------------------------*/
\r
119 * Send the uIP buffer to the MAC.
\r
121 static void prvENET_Send(void);
\r
124 * Setup the MAC address in the MAC itself, and in the uIP stack.
\r
126 static void prvSetMACAddress( void );
\r
129 * Used to return a pointer to the next buffer to be used.
\r
131 extern unsigned char *pcGetNextBuffer( void );
\r
134 * Port functions required by the uIP stack.
\r
136 void clock_init( void );
\r
137 clock_time_t clock_time( void );
\r
139 /*-----------------------------------------------------------*/
\r
141 /* The semaphore used by the ISR to wake the uIP task. */
\r
142 xSemaphoreHandle xSemaphore = NULL;
\r
144 /*-----------------------------------------------------------*/
\r
146 void clock_init(void)
\r
148 /* This is done when the scheduler starts. */
\r
150 /*-----------------------------------------------------------*/
\r
152 clock_time_t clock_time( void )
\r
154 return xTaskGetTickCount();
\r
156 /*-----------------------------------------------------------*/
\r
158 void vuIP_Task( void *pvParameters )
\r
161 uip_ipaddr_t xIPAddr;
\r
162 struct timer periodic_timer, arp_timer;
\r
164 /* Create the semaphore used by the ISR to wake this task. */
\r
165 vSemaphoreCreateBinary( xSemaphore );
\r
167 /* Initialise the uIP stack. */
\r
168 timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );
\r
169 timer_set( &arp_timer, configTICK_RATE_HZ * 10 );
\r
171 uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );
\r
172 uip_sethostaddr( xIPAddr );
\r
173 uip_ipaddr( xIPAddr, uipNET_MASK0, uipNET_MASK1, uipNET_MASK2, uipNET_MASK3 );
\r
174 uip_setnetmask( xIPAddr );
\r
175 uip_ipaddr( xIPAddr, uipGATEWAY_ADDR0, uipGATEWAY_ADDR1, uipGATEWAY_ADDR2, uipGATEWAY_ADDR3 );
\r
176 uip_setdraddr( xIPAddr );
\r
179 /* Initialise the MAC. */
\r
180 ENET_InitClocksGPIO();
\r
182 portENTER_CRITICAL();
\r
185 prvSetMACAddress();
\r
186 VIC_Config( ENET_ITLine, VIC_IRQ, 1 );
\r
187 VIC_ITCmd( ENET_ITLine, ENABLE );
\r
188 ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;
\r
189 ENET_DMA->IER = uipDMI_RX_CURRENT_DONE;
\r
191 portEXIT_CRITICAL();
\r
196 /* Is there received data ready to be processed? */
\r
197 uip_len = ENET_HandleRxPkt( uip_buf );
\r
201 /* Standard uIP loop taken from the uIP manual. */
\r
202 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
\r
207 /* If the above function invocation resulted in data that
\r
208 should be sent out on the network, the global variable
\r
209 uip_len is set to a value > 0. */
\r
216 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
\r
220 /* If the above function invocation resulted in data that
\r
221 should be sent out on the network, the global variable
\r
222 uip_len is set to a value > 0. */
\r
231 if( timer_expired( &periodic_timer ) )
\r
233 timer_reset( &periodic_timer );
\r
234 for( i = 0; i < UIP_CONNS; i++ )
\r
238 /* If the above function invocation resulted in data that
\r
239 should be sent out on the network, the global variable
\r
240 uip_len is set to a value > 0. */
\r
248 /* Call the ARP timer function every 10 seconds. */
\r
249 if( timer_expired( &arp_timer ) )
\r
251 timer_reset( &arp_timer );
\r
257 /* We did not receive a packet, and there was no periodic
\r
258 processing to perform. Block for a fixed period. If a packet
\r
259 is received during this period we will be woken by the ISR
\r
260 giving us the Semaphore. */
\r
261 xSemaphoreTake( xSemaphore, configTICK_RATE_HZ / 2 );
\r
266 /*-----------------------------------------------------------*/
\r
268 static void prvENET_Send(void)
\r
271 static unsigned char *pcTxData;
\r
273 /* Get a DMA buffer into which we can write the data to send. */
\r
274 for( i = 0; i < uipBUFFER_WAIT_ATTEMPTS; i++ )
\r
276 pcTxData = pcGetNextBuffer();
\r
284 vTaskDelay( uipBUFFER_WAIT_DELAY );
\r
290 /* Copy the header into the Tx buffer. */
\r
291 memcpy( ( void * ) pcTxData, ( void * ) uip_buf, uipTOTAL_FRAME_HEADER_SIZE );
\r
292 if( uip_len > uipTOTAL_FRAME_HEADER_SIZE )
\r
294 memcpy( ( void * ) &( pcTxData[ uipTOTAL_FRAME_HEADER_SIZE ] ), ( void * ) uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) );
\r
297 ENET_TxPkt( &pcTxData, uip_len );
\r
300 /*-----------------------------------------------------------*/
\r
302 void ENET_IRQHandler(void)
\r
304 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
306 /* Give the semaphore in case the uIP task needs waking. */
\r
307 xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
\r
309 /* Clear the interrupt. */
\r
310 ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;
\r
312 /* Switch tasks if necessary. */
\r
313 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
315 /*-----------------------------------------------------------*/
\r
317 static void prvSetMACAddress( void )
\r
319 struct uip_eth_addr xAddr;
\r
321 /* Configure the MAC address in the uIP stack. */
\r
322 xAddr.addr[ 0 ] = uipMAC_ADDR0;
\r
323 xAddr.addr[ 1 ] = uipMAC_ADDR1;
\r
324 xAddr.addr[ 2 ] = uipMAC_ADDR2;
\r
325 xAddr.addr[ 3 ] = uipMAC_ADDR3;
\r
326 xAddr.addr[ 4 ] = uipMAC_ADDR4;
\r
327 xAddr.addr[ 5 ] = uipMAC_ADDR5;
\r
328 uip_setethaddr( xAddr );
\r
330 /* Write the MAC address to the MAC. */
\r
331 ENET_MAC->MAL = ( uipMAC_ADDR3 << 24 ) | ( uipMAC_ADDR2 << 16 ) | ( uipMAC_ADDR1 << 8 ) | ( uipMAC_ADDR0 );
\r
332 ENET_MAC->MAH = ( uipMAC_ADDR5 << 8 ) | ( uipMAC_ADDR4 );
\r