2 FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
69 /* Standard includes. */
\r
72 /* Library includes. */
\r
73 #include "91x_lib.h"
\r
74 #include "91x_enet.h"
\r
76 /* Scheduler includes. */
\r
77 #include "FreeRTOS.h"
\r
83 #include "uip_arp.h"
\r
86 #include "clock-arch.h"
\r
88 /*-----------------------------------------------------------*/
\r
90 /* MAC address configuration. */
\r
91 #define uipMAC_ADDR0 0x00
\r
92 #define uipMAC_ADDR1 0x12
\r
93 #define uipMAC_ADDR2 0x13
\r
94 #define uipMAC_ADDR3 0x14
\r
95 #define uipMAC_ADDR4 0x15
\r
96 #define uipMAC_ADDR5 0x20
\r
98 /* IP address configuration. */
\r
99 #define uipIP_ADDR0 172
\r
100 #define uipIP_ADDR1 25
\r
101 #define uipIP_ADDR2 218
\r
102 #define uipIP_ADDR3 11
\r
104 /* Netmask configuration. */
\r
105 #define uipNET_MASK0 255
\r
106 #define uipNET_MASK1 255
\r
107 #define uipNET_MASK2 255
\r
108 #define uipNET_MASK3 0
\r
110 /* Gateway address configuration. */
\r
111 #define uipGATEWAY_ADDR0 172
\r
112 #define uipGATEWAY_ADDR1 25
\r
113 #define uipGATEWAY_ADDR2 218
\r
114 #define uipGATEWAY_ADDR3 1
\r
116 /* Shortcut to the header within the Rx buffer. */
\r
117 #define xHeader ((struct uip_eth_hdr *) &uip_buf[ 0 ])
\r
119 /* uIP update frequencies. */
\r
120 #define uipMAX_BLOCK_TIME (configTICK_RATE_HZ / 4)
\r
122 /* Interrupt status bit definition. */
\r
123 #define uipDMI_RX_CURRENT_DONE 0x8000
\r
125 /* If no buffers are available, then wait this long before looking again. */
\r
126 #define uipBUFFER_WAIT_DELAY ( 10 / portTICK_PERIOD_MS )
\r
127 #define uipBUFFER_WAIT_ATTEMPTS ( 10 )
\r
129 /* Standard constant. */
\r
130 #define uipTOTAL_FRAME_HEADER_SIZE 54
\r
132 /*-----------------------------------------------------------*/
\r
135 * Send the uIP buffer to the MAC.
\r
137 static void prvENET_Send(void);
\r
140 * Setup the MAC address in the MAC itself, and in the uIP stack.
\r
142 static void prvSetMACAddress( void );
\r
145 * Used to return a pointer to the next buffer to be used.
\r
147 extern unsigned char *pcGetNextBuffer( void );
\r
150 * Port functions required by the uIP stack.
\r
152 void clock_init( void );
\r
153 clock_time_t clock_time( void );
\r
155 /*-----------------------------------------------------------*/
\r
157 /* The semaphore used by the ISR to wake the uIP task. */
\r
158 SemaphoreHandle_t xSemaphore = NULL;
\r
160 /*-----------------------------------------------------------*/
\r
162 void clock_init(void)
\r
164 /* This is done when the scheduler starts. */
\r
166 /*-----------------------------------------------------------*/
\r
168 clock_time_t clock_time( void )
\r
170 return xTaskGetTickCount();
\r
172 /*-----------------------------------------------------------*/
\r
174 void vuIP_Task( void *pvParameters )
\r
177 uip_ipaddr_t xIPAddr;
\r
178 struct timer periodic_timer, arp_timer;
\r
180 /* Create the semaphore used by the ISR to wake this task. */
\r
181 vSemaphoreCreateBinary( xSemaphore );
\r
183 /* Initialise the uIP stack. */
\r
184 timer_set( &periodic_timer, configTICK_RATE_HZ / 2 );
\r
185 timer_set( &arp_timer, configTICK_RATE_HZ * 10 );
\r
187 uip_ipaddr( xIPAddr, uipIP_ADDR0, uipIP_ADDR1, uipIP_ADDR2, uipIP_ADDR3 );
\r
188 uip_sethostaddr( xIPAddr );
\r
189 uip_ipaddr( xIPAddr, uipNET_MASK0, uipNET_MASK1, uipNET_MASK2, uipNET_MASK3 );
\r
190 uip_setnetmask( xIPAddr );
\r
191 uip_ipaddr( xIPAddr, uipGATEWAY_ADDR0, uipGATEWAY_ADDR1, uipGATEWAY_ADDR2, uipGATEWAY_ADDR3 );
\r
192 uip_setdraddr( xIPAddr );
\r
195 /* Initialise the MAC. */
\r
196 ENET_InitClocksGPIO();
\r
198 portENTER_CRITICAL();
\r
201 prvSetMACAddress();
\r
202 VIC_Config( ENET_ITLine, VIC_IRQ, 1 );
\r
203 VIC_ITCmd( ENET_ITLine, ENABLE );
\r
204 ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;
\r
205 ENET_DMA->IER = uipDMI_RX_CURRENT_DONE;
\r
207 portEXIT_CRITICAL();
\r
212 /* Is there received data ready to be processed? */
\r
213 uip_len = ENET_HandleRxPkt( uip_buf );
\r
217 /* Standard uIP loop taken from the uIP manual. */
\r
218 if( xHeader->type == htons( UIP_ETHTYPE_IP ) )
\r
223 /* If the above function invocation resulted in data that
\r
224 should be sent out on the network, the global variable
\r
225 uip_len is set to a value > 0. */
\r
232 else if( xHeader->type == htons( UIP_ETHTYPE_ARP ) )
\r
236 /* If the above function invocation resulted in data that
\r
237 should be sent out on the network, the global variable
\r
238 uip_len is set to a value > 0. */
\r
247 if( timer_expired( &periodic_timer ) )
\r
249 timer_reset( &periodic_timer );
\r
250 for( i = 0; i < UIP_CONNS; i++ )
\r
254 /* If the above function invocation resulted in data that
\r
255 should be sent out on the network, the global variable
\r
256 uip_len is set to a value > 0. */
\r
264 /* Call the ARP timer function every 10 seconds. */
\r
265 if( timer_expired( &arp_timer ) )
\r
267 timer_reset( &arp_timer );
\r
273 /* We did not receive a packet, and there was no periodic
\r
274 processing to perform. Block for a fixed period. If a packet
\r
275 is received during this period we will be woken by the ISR
\r
276 giving us the Semaphore. */
\r
277 xSemaphoreTake( xSemaphore, configTICK_RATE_HZ / 2 );
\r
282 /*-----------------------------------------------------------*/
\r
284 static void prvENET_Send(void)
\r
287 static unsigned char *pcTxData;
\r
289 /* Get a DMA buffer into which we can write the data to send. */
\r
290 for( i = 0; i < uipBUFFER_WAIT_ATTEMPTS; i++ )
\r
292 pcTxData = pcGetNextBuffer();
\r
300 vTaskDelay( uipBUFFER_WAIT_DELAY );
\r
306 /* Copy the header into the Tx buffer. */
\r
307 memcpy( ( void * ) pcTxData, ( void * ) uip_buf, uipTOTAL_FRAME_HEADER_SIZE );
\r
308 if( uip_len > uipTOTAL_FRAME_HEADER_SIZE )
\r
310 memcpy( ( void * ) &( pcTxData[ uipTOTAL_FRAME_HEADER_SIZE ] ), ( void * ) uip_appdata, ( uip_len - uipTOTAL_FRAME_HEADER_SIZE ) );
\r
313 ENET_TxPkt( &pcTxData, uip_len );
\r
316 /*-----------------------------------------------------------*/
\r
318 void ENET_IRQHandler(void)
\r
320 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
322 /* Give the semaphore in case the uIP task needs waking. */
\r
323 xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
\r
325 /* Clear the interrupt. */
\r
326 ENET_DMA->ISR = uipDMI_RX_CURRENT_DONE;
\r
328 /* Switch tasks if necessary. */
\r
329 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
331 /*-----------------------------------------------------------*/
\r
333 static void prvSetMACAddress( void )
\r
335 struct uip_eth_addr xAddr;
\r
337 /* Configure the MAC address in the uIP stack. */
\r
338 xAddr.addr[ 0 ] = uipMAC_ADDR0;
\r
339 xAddr.addr[ 1 ] = uipMAC_ADDR1;
\r
340 xAddr.addr[ 2 ] = uipMAC_ADDR2;
\r
341 xAddr.addr[ 3 ] = uipMAC_ADDR3;
\r
342 xAddr.addr[ 4 ] = uipMAC_ADDR4;
\r
343 xAddr.addr[ 5 ] = uipMAC_ADDR5;
\r
344 uip_setethaddr( xAddr );
\r
346 /* Write the MAC address to the MAC. */
\r
347 ENET_MAC->MAL = ( uipMAC_ADDR3 << 24 ) | ( uipMAC_ADDR2 << 16 ) | ( uipMAC_ADDR1 << 8 ) | ( uipMAC_ADDR0 );
\r
348 ENET_MAC->MAH = ( uipMAC_ADDR5 << 8 ) | ( uipMAC_ADDR4 );
\r