2 FreeRTOS V7.1.1 - Copyright (C) 2012 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
66 /* Standard includes. */
\r
69 /* Library includes. */
\r
70 #include "91x_lib.h"
\r
71 #include "91x_enet.h"
\r
73 /* Scheduler includes. */
\r
74 #include "FreeRTOS.h"
\r
80 #include "uip_arp.h"
\r
83 #include "clock-arch.h"
\r
85 /*-----------------------------------------------------------*/
\r
87 /* MAC address configuration. */
\r
88 #define uipMAC_ADDR0 0x00
\r
89 #define uipMAC_ADDR1 0x12
\r
90 #define uipMAC_ADDR2 0x13
\r
91 #define uipMAC_ADDR3 0x14
\r
92 #define uipMAC_ADDR4 0x15
\r
93 #define uipMAC_ADDR5 0x20
\r
95 /* IP address configuration. */
\r
96 #define uipIP_ADDR0 172
\r
97 #define uipIP_ADDR1 25
\r
98 #define uipIP_ADDR2 218
\r
99 #define uipIP_ADDR3 11
\r
101 /* Netmask configuration. */
\r
102 #define uipNET_MASK0 255
\r
103 #define uipNET_MASK1 255
\r
104 #define uipNET_MASK2 255
\r
105 #define uipNET_MASK3 0
\r
107 /* Gateway address configuration. */
\r
108 #define uipGATEWAY_ADDR0 172
\r
109 #define uipGATEWAY_ADDR1 25
\r
110 #define uipGATEWAY_ADDR2 218
\r
111 #define uipGATEWAY_ADDR3 1
\r
113 /* Shortcut to the header within the Rx buffer. */
\r
114 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
116 /* uIP update frequencies. */
\r
117 #define uipMAX_BLOCK_TIME (configTICK_RATE_HZ / 4)
\r
119 /* Interrupt status bit definition. */
\r
120 #define uipDMI_RX_CURRENT_DONE 0x8000
\r
122 /* If no buffers are available, then wait this long before looking again. */
\r
123 #define uipBUFFER_WAIT_DELAY ( 10 / portTICK_RATE_MS )
\r
124 #define uipBUFFER_WAIT_ATTEMPTS ( 10 )
\r
126 /* Standard constant. */
\r
127 #define uipTOTAL_FRAME_HEADER_SIZE 54
\r
129 /*-----------------------------------------------------------*/
\r
132 * Send the uIP buffer to the MAC.
\r
134 static void prvENET_Send(void);
\r
137 * Setup the MAC address in the MAC itself, and in the uIP stack.
\r
139 static void prvSetMACAddress( void );
\r
142 * Used to return a pointer to the next buffer to be used.
\r
144 extern unsigned char *pcGetNextBuffer( void );
\r
147 * Port functions required by the uIP stack.
\r
149 void clock_init( void );
\r
150 clock_time_t clock_time( void );
\r
152 /*-----------------------------------------------------------*/
\r
154 /* The semaphore used by the ISR to wake the uIP task. */
\r
155 xSemaphoreHandle xSemaphore = NULL;
\r
157 /*-----------------------------------------------------------*/
\r
159 void clock_init(void)
\r
161 /* This is done when the scheduler starts. */
\r
163 /*-----------------------------------------------------------*/
\r
165 clock_time_t clock_time( void )
\r
167 return xTaskGetTickCount();
\r
169 /*-----------------------------------------------------------*/
\r
171 void vuIP_Task( void *pvParameters )
\r
174 uip_ipaddr_t xIPAddr;
\r
175 struct timer periodic_timer, arp_timer;
\r
177 /* Create the semaphore used by the ISR to wake this task. */
\r
178 vSemaphoreCreateBinary( xSemaphore );
\r
180 /* Initialise the uIP stack. */
\r
181 timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );
\r
182 timer_set( &arp_timer, configTICK_RATE_HZ * 10 );
\r
184 uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );
\r
185 uip_sethostaddr( xIPAddr );
\r
186 uip_ipaddr( xIPAddr, uipNET_MASK0, uipNET_MASK1, uipNET_MASK2, uipNET_MASK3 );
\r
187 uip_setnetmask( xIPAddr );
\r
188 uip_ipaddr( xIPAddr, uipGATEWAY_ADDR0, uipGATEWAY_ADDR1, uipGATEWAY_ADDR2, uipGATEWAY_ADDR3 );
\r
189 uip_setdraddr( xIPAddr );
\r
192 /* Initialise the MAC. */
\r
193 ENET_InitClocksGPIO();
\r
195 portENTER_CRITICAL();
\r
198 prvSetMACAddress();
\r
199 VIC_Config( ENET_ITLine, VIC_IRQ, 1 );
\r
200 VIC_ITCmd( ENET_ITLine, ENABLE );
\r
201 ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;
\r
202 ENET_DMA->IER = uipDMI_RX_CURRENT_DONE;
\r
204 portEXIT_CRITICAL();
\r
209 /* Is there received data ready to be processed? */
\r
210 uip_len = ENET_HandleRxPkt( uip_buf );
\r
214 /* Standard uIP loop taken from the uIP manual. */
\r
215 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
\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
229 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
\r
233 /* If the above function invocation resulted in data that
\r
234 should be sent out on the network, the global variable
\r
235 uip_len is set to a value > 0. */
\r
244 if( timer_expired( &periodic_timer ) )
\r
246 timer_reset( &periodic_timer );
\r
247 for( i = 0; i < UIP_CONNS; i++ )
\r
251 /* If the above function invocation resulted in data that
\r
252 should be sent out on the network, the global variable
\r
253 uip_len is set to a value > 0. */
\r
261 /* Call the ARP timer function every 10 seconds. */
\r
262 if( timer_expired( &arp_timer ) )
\r
264 timer_reset( &arp_timer );
\r
270 /* We did not receive a packet, and there was no periodic
\r
271 processing to perform. Block for a fixed period. If a packet
\r
272 is received during this period we will be woken by the ISR
\r
273 giving us the Semaphore. */
\r
274 xSemaphoreTake( xSemaphore, configTICK_RATE_HZ / 2 );
\r
279 /*-----------------------------------------------------------*/
\r
281 static void prvENET_Send(void)
\r
284 static unsigned char *pcTxData;
\r
286 /* Get a DMA buffer into which we can write the data to send. */
\r
287 for( i = 0; i < uipBUFFER_WAIT_ATTEMPTS; i++ )
\r
289 pcTxData = pcGetNextBuffer();
\r
297 vTaskDelay( uipBUFFER_WAIT_DELAY );
\r
303 /* Copy the header into the Tx buffer. */
\r
304 memcpy( ( void * ) pcTxData, ( void * ) uip_buf, uipTOTAL_FRAME_HEADER_SIZE );
\r
305 if( uip_len > uipTOTAL_FRAME_HEADER_SIZE )
\r
307 memcpy( ( void * ) &( pcTxData[ uipTOTAL_FRAME_HEADER_SIZE ] ), ( void * ) uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) );
\r
310 ENET_TxPkt( &pcTxData, uip_len );
\r
313 /*-----------------------------------------------------------*/
\r
315 void ENET_IRQHandler(void)
\r
317 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
319 /* Give the semaphore in case the uIP task needs waking. */
\r
320 xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
\r
322 /* Clear the interrupt. */
\r
323 ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;
\r
325 /* Switch tasks if necessary. */
\r
326 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
328 /*-----------------------------------------------------------*/
\r
330 static void prvSetMACAddress( void )
\r
332 struct uip_eth_addr xAddr;
\r
334 /* Configure the MAC address in the uIP stack. */
\r
335 xAddr.addr[ 0 ] = uipMAC_ADDR0;
\r
336 xAddr.addr[ 1 ] = uipMAC_ADDR1;
\r
337 xAddr.addr[ 2 ] = uipMAC_ADDR2;
\r
338 xAddr.addr[ 3 ] = uipMAC_ADDR3;
\r
339 xAddr.addr[ 4 ] = uipMAC_ADDR4;
\r
340 xAddr.addr[ 5 ] = uipMAC_ADDR5;
\r
341 uip_setethaddr( xAddr );
\r
343 /* Write the MAC address to the MAC. */
\r
344 ENET_MAC->MAL = ( uipMAC_ADDR3 << 24 ) | ( uipMAC_ADDR2 << 16 ) | ( uipMAC_ADDR1 << 8 ) | ( uipMAC_ADDR0 );
\r
345 ENET_MAC->MAH = ( uipMAC_ADDR5 << 8 ) | ( uipMAC_ADDR4 );
\r