]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3Sxxxx_IAR_Keil/webserver/emac.c
Prepare for V7.4.0 release.
[freertos] / FreeRTOS / Demo / CORTEX_LM3Sxxxx_IAR_Keil / webserver / emac.c
1 /*\r
2     FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not itcan be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /* Kernel includes. */\r
76 #include "FreeRTOS.h"\r
77 #include "semphr.h"\r
78 #include "task.h"\r
79 \r
80 /* Demo includes. */\r
81 #include "emac.h"\r
82 \r
83 /* uIP includes. */\r
84 #include "uip.h"\r
85 \r
86 /* Hardware library includes. */\r
87 #include "hw_types.h"\r
88 #include "hw_memmap.h"\r
89 #include "hw_ints.h"\r
90 #include "hw_ethernet.h"\r
91 #include "ethernet.h"\r
92 #include "interrupt.h"\r
93 \r
94 #define emacNUM_RX_BUFFERS              5\r
95 #define emacFRAM_SIZE_BYTES     2\r
96 #define macNEGOTIATE_DELAY              2000\r
97 #define macWAIT_SEND_TIME               ( 10 )\r
98 \r
99 /* The task that handles the MAC peripheral.  This is created at a high\r
100 priority and is effectively a deferred interrupt handler.  The peripheral\r
101 handling is deferred to a task to prevent the entire FIFO having to be read\r
102 from within an ISR. */\r
103 void vMACHandleTask( void *pvParameters );\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /* The semaphore used to wake the uIP task when data arrives. */\r
108 xSemaphoreHandle xEMACSemaphore = NULL;\r
109 \r
110 /* The semaphore used to wake the interrupt handler task.  The peripheral\r
111 is processed at the task level to prevent the need to read the entire FIFO from\r
112 within the ISR itself. */\r
113 xSemaphoreHandle xMACInterruptSemaphore = NULL;\r
114 \r
115 /* The buffer used by the uIP stack.  In this case the pointer is used to\r
116 point to one of the Rx buffers. */\r
117 unsigned portCHAR *uip_buf;\r
118 \r
119 /* Buffers into which Rx data is placed. */\r
120 static union\r
121 {\r
122         unsigned portLONG ulJustForAlignment;\r
123         unsigned portCHAR ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ];\r
124 } uxRxBuffers;\r
125 \r
126 /* The length of the data within each of the Rx buffers. */\r
127 static unsigned portLONG ulRxLength[ emacNUM_RX_BUFFERS ];\r
128 \r
129 /* Used to keep a track of the number of bytes to transmit. */\r
130 static unsigned portLONG ulNextTxSpace;\r
131 \r
132 /*-----------------------------------------------------------*/\r
133 \r
134 portBASE_TYPE vInitEMAC( void )\r
135 {\r
136 unsigned long ulTemp;\r
137 portBASE_TYPE xReturn;\r
138 \r
139         /* Ensure all interrupts are disabled. */\r
140         EthernetIntDisable( ETH_BASE, ( ETH_INT_PHY | ETH_INT_MDIO | ETH_INT_RXER | ETH_INT_RXOF | ETH_INT_TX | ETH_INT_TXER | ETH_INT_RX));\r
141 \r
142         /* Clear any interrupts that were already pending. */\r
143     ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );\r
144     EthernetIntClear( ETH_BASE, ulTemp );\r
145 \r
146         /* Initialise the MAC and connect. */\r
147     EthernetInit( ETH_BASE );\r
148     EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );\r
149     EthernetEnable( ETH_BASE );\r
150 \r
151         /* Mark each Rx buffer as empty. */\r
152         for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )\r
153         {\r
154                 ulRxLength[ ulTemp ] = 0;\r
155         }\r
156         \r
157         /* Create the queue and task used to defer the MAC processing to the\r
158         task level. */\r
159         vSemaphoreCreateBinary( xMACInterruptSemaphore );\r
160         xSemaphoreTake( xMACInterruptSemaphore, 0 );\r
161         xReturn = xTaskCreate( vMACHandleTask, ( signed portCHAR * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
162         vTaskDelay( macNEGOTIATE_DELAY );\r
163         \r
164         /* We are only interested in Rx interrupts. */\r
165         IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );\r
166     IntEnable( INT_ETH );\r
167     EthernetIntEnable(ETH_BASE, ETH_INT_RX);\r
168 \r
169         return xReturn;\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )\r
174 {\r
175 static unsigned long ulNextRxBuffer = 0;\r
176 unsigned int iLen;\r
177 \r
178         iLen = ulRxLength[ ulNextRxBuffer ];\r
179 \r
180         if( iLen != 0 )\r
181         {\r
182                 /* Leave room for the size at the start of the buffer. */\r
183                 uip_buf = &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );\r
184                 \r
185                 ulRxLength[ ulNextRxBuffer ] = 0;\r
186                 \r
187                 ulNextRxBuffer++;\r
188                 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )\r
189                 {\r
190                         ulNextRxBuffer = 0;\r
191                 }\r
192         }\r
193 \r
194     return iLen;\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 void vInitialiseSend( void )\r
199 {\r
200         /* Set the index to the first byte to send - skipping over the size\r
201         bytes. */\r
202         ulNextTxSpace = 2;\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 void vIncrementTxLength( unsigned portLONG ulLength )\r
207 {\r
208         ulNextTxSpace += ulLength;\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 void vSendBufferToMAC( void )\r
213 {\r
214 unsigned long *pulSource;\r
215 unsigned portSHORT * pus;\r
216 unsigned portLONG ulNextWord;\r
217 \r
218         /* Locate the data to be send. */\r
219         pus = ( unsigned portSHORT * ) uip_buf;\r
220 \r
221         /* Add in the size of the data. */\r
222         pus--;\r
223         *pus = ulNextTxSpace;\r
224 \r
225         /* Wait for data to be sent if there is no space immediately. */\r
226     while( !EthernetSpaceAvail( ETH_BASE ) )\r
227     {\r
228                 vTaskDelay( macWAIT_SEND_TIME );\r
229     }\r
230         \r
231         pulSource = ( unsigned portLONG * ) pus;        \r
232         \r
233         for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned portLONG ) )\r
234         {\r
235         HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;\r
236                 pulSource++;\r
237         }\r
238 \r
239         /* Go. */\r
240     HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 void vEMAC_ISR( void )\r
245 {\r
246 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
247 unsigned portLONG ulTemp;\r
248 \r
249         /* Clear the interrupt. */\r
250         ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );\r
251         EthernetIntClear( ETH_BASE, ulTemp );\r
252                 \r
253         /* Was it an Rx interrupt? */\r
254         if( ulTemp & ETH_INT_RX )\r
255         {\r
256                 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );\r
257                 EthernetIntDisable( ETH_BASE, ETH_INT_RX );\r
258         }\r
259                 \r
260     /* Switch to the uIP task. */\r
261         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 void vMACHandleTask( void *pvParameters )\r
266 {\r
267 unsigned long i, ulInt;\r
268 unsigned portLONG ulLength;\r
269 unsigned long *pulBuffer;\r
270 static unsigned portLONG ulNextRxBuffer = 0;\r
271 \r
272         for( ;; )\r
273         {\r
274                 /* Wait for something to do. */\r
275                 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );\r
276                 \r
277                 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )\r
278                 {               \r
279                         ulLength = HWREG( ETH_BASE + MAC_O_DATA );\r
280                         \r
281                         /* Leave room at the start of the buffer for the size. */\r
282                         pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );                    \r
283                         *pulBuffer = ( ulLength >> 16 );\r
284 \r
285                         /* Get the size of the data. */                 \r
286                         pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 4 ] );                    \r
287                         ulLength &= 0xFFFF;\r
288                         \r
289                         if( ulLength > 4 )\r
290                         {\r
291                                 ulLength -= 4;\r
292                                 \r
293                                 if( ulLength >= UIP_BUFSIZE )\r
294                                 {\r
295                                         /* The data won't fit in our buffer.  Ensure we don't\r
296                                         try to write into the buffer. */\r
297                                         ulLength = 0;\r
298                                 }\r
299 \r
300                                 /* Read out the data into our buffer. */\r
301                                 for( i = 0; i < ulLength; i += sizeof( unsigned portLONG ) )\r
302                                 {\r
303                                         *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );\r
304                                         pulBuffer++;\r
305                                 }\r
306                                 \r
307                                 /* Store the length of the data into the separate array. */\r
308                                 ulRxLength[ ulNextRxBuffer ] = ulLength;\r
309                                 \r
310                                 /* Use the next buffer the next time through. */\r
311                                 ulNextRxBuffer++;\r
312                                 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )\r
313                                 {\r
314                                         ulNextRxBuffer = 0;\r
315                                 }\r
316                 \r
317                                 /* Ensure the uIP task is not blocked as data has arrived. */\r
318                                 xSemaphoreGive( xEMACSemaphore );\r
319                         }\r
320                 }\r
321                 \r
322                 EthernetIntEnable( ETH_BASE, ETH_INT_RX );\r
323                 \r
324                 ( void ) ulInt;\r
325         }\r
326 }\r
327 \r