]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3Sxxxx_IAR_Keil/webserver/emac.c
815c6cb63cd1df6e80dabdfbde5e02584b2e8518
[freertos] / FreeRTOS / Demo / CORTEX_LM3Sxxxx_IAR_Keil / webserver / emac.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\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
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \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
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \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
65 */\r
66 \r
67 /* Kernel includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "semphr.h"\r
70 #include "task.h"\r
71 \r
72 /* Demo includes. */\r
73 #include "emac.h"\r
74 \r
75 /* uIP includes. */\r
76 #include "uip.h"\r
77 \r
78 /* Hardware library includes. */\r
79 #include "hw_types.h"\r
80 #include "hw_memmap.h"\r
81 #include "hw_ints.h"\r
82 #include "hw_ethernet.h"\r
83 #include "ethernet.h"\r
84 #include "interrupt.h"\r
85 \r
86 #define emacNUM_RX_BUFFERS              5\r
87 #define emacFRAM_SIZE_BYTES     2\r
88 #define macNEGOTIATE_DELAY              2000\r
89 #define macWAIT_SEND_TIME               ( 10 )\r
90 \r
91 /* The task that handles the MAC peripheral.  This is created at a high\r
92 priority and is effectively a deferred interrupt handler.  The peripheral\r
93 handling is deferred to a task to prevent the entire FIFO having to be read\r
94 from within an ISR. */\r
95 void vMACHandleTask( void *pvParameters );\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /* The semaphore used to wake the uIP task when data arrives. */\r
100 xSemaphoreHandle xEMACSemaphore = NULL;\r
101 \r
102 /* The semaphore used to wake the interrupt handler task.  The peripheral\r
103 is processed at the task level to prevent the need to read the entire FIFO from\r
104 within the ISR itself. */\r
105 xSemaphoreHandle xMACInterruptSemaphore = NULL;\r
106 \r
107 /* The buffer used by the uIP stack.  In this case the pointer is used to\r
108 point to one of the Rx buffers. */\r
109 unsigned portCHAR *uip_buf;\r
110 \r
111 /* Buffers into which Rx data is placed. */\r
112 static union\r
113 {\r
114         unsigned portLONG ulJustForAlignment;\r
115         unsigned portCHAR ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ];\r
116 } uxRxBuffers;\r
117 \r
118 /* The length of the data within each of the Rx buffers. */\r
119 static unsigned portLONG ulRxLength[ emacNUM_RX_BUFFERS ];\r
120 \r
121 /* Used to keep a track of the number of bytes to transmit. */\r
122 static unsigned portLONG ulNextTxSpace;\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 portBASE_TYPE vInitEMAC( void )\r
127 {\r
128 unsigned long ulTemp;\r
129 portBASE_TYPE xReturn;\r
130 \r
131         /* Ensure all interrupts are disabled. */\r
132         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
133 \r
134         /* Clear any interrupts that were already pending. */\r
135     ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );\r
136     EthernetIntClear( ETH_BASE, ulTemp );\r
137 \r
138         /* Initialise the MAC and connect. */\r
139     EthernetInit( ETH_BASE );\r
140     EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );\r
141     EthernetEnable( ETH_BASE );\r
142 \r
143         /* Mark each Rx buffer as empty. */\r
144         for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )\r
145         {\r
146                 ulRxLength[ ulTemp ] = 0;\r
147         }\r
148         \r
149         /* Create the queue and task used to defer the MAC processing to the\r
150         task level. */\r
151         vSemaphoreCreateBinary( xMACInterruptSemaphore );\r
152         xSemaphoreTake( xMACInterruptSemaphore, 0 );\r
153         xReturn = xTaskCreate( vMACHandleTask, ( signed portCHAR * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
154         vTaskDelay( macNEGOTIATE_DELAY );\r
155         \r
156         /* We are only interested in Rx interrupts. */\r
157         IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );\r
158     IntEnable( INT_ETH );\r
159     EthernetIntEnable(ETH_BASE, ETH_INT_RX);\r
160 \r
161         return xReturn;\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )\r
166 {\r
167 static unsigned long ulNextRxBuffer = 0;\r
168 unsigned int iLen;\r
169 \r
170         iLen = ulRxLength[ ulNextRxBuffer ];\r
171 \r
172         if( iLen != 0 )\r
173         {\r
174                 /* Leave room for the size at the start of the buffer. */\r
175                 uip_buf = &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );\r
176                 \r
177                 ulRxLength[ ulNextRxBuffer ] = 0;\r
178                 \r
179                 ulNextRxBuffer++;\r
180                 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )\r
181                 {\r
182                         ulNextRxBuffer = 0;\r
183                 }\r
184         }\r
185 \r
186     return iLen;\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 void vInitialiseSend( void )\r
191 {\r
192         /* Set the index to the first byte to send - skipping over the size\r
193         bytes. */\r
194         ulNextTxSpace = 2;\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 void vIncrementTxLength( unsigned portLONG ulLength )\r
199 {\r
200         ulNextTxSpace += ulLength;\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 void vSendBufferToMAC( void )\r
205 {\r
206 unsigned long *pulSource;\r
207 unsigned portSHORT * pus;\r
208 unsigned portLONG ulNextWord;\r
209 \r
210         /* Locate the data to be send. */\r
211         pus = ( unsigned portSHORT * ) uip_buf;\r
212 \r
213         /* Add in the size of the data. */\r
214         pus--;\r
215         *pus = ulNextTxSpace;\r
216 \r
217         /* Wait for data to be sent if there is no space immediately. */\r
218     while( !EthernetSpaceAvail( ETH_BASE ) )\r
219     {\r
220                 vTaskDelay( macWAIT_SEND_TIME );\r
221     }\r
222         \r
223         pulSource = ( unsigned portLONG * ) pus;        \r
224         \r
225         for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned portLONG ) )\r
226         {\r
227         HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;\r
228                 pulSource++;\r
229         }\r
230 \r
231         /* Go. */\r
232     HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 void vEMAC_ISR( void )\r
237 {\r
238 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
239 unsigned portLONG ulTemp;\r
240 \r
241         /* Clear the interrupt. */\r
242         ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );\r
243         EthernetIntClear( ETH_BASE, ulTemp );\r
244                 \r
245         /* Was it an Rx interrupt? */\r
246         if( ulTemp & ETH_INT_RX )\r
247         {\r
248                 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );\r
249                 EthernetIntDisable( ETH_BASE, ETH_INT_RX );\r
250         }\r
251                 \r
252     /* Switch to the uIP task. */\r
253         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 void vMACHandleTask( void *pvParameters )\r
258 {\r
259 unsigned long i, ulInt;\r
260 unsigned portLONG ulLength;\r
261 unsigned long *pulBuffer;\r
262 static unsigned portLONG ulNextRxBuffer = 0;\r
263 \r
264         for( ;; )\r
265         {\r
266                 /* Wait for something to do. */\r
267                 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );\r
268                 \r
269                 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )\r
270                 {               \r
271                         ulLength = HWREG( ETH_BASE + MAC_O_DATA );\r
272                         \r
273                         /* Leave room at the start of the buffer for the size. */\r
274                         pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );                    \r
275                         *pulBuffer = ( ulLength >> 16 );\r
276 \r
277                         /* Get the size of the data. */                 \r
278                         pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 4 ] );                    \r
279                         ulLength &= 0xFFFF;\r
280                         \r
281                         if( ulLength > 4 )\r
282                         {\r
283                                 ulLength -= 4;\r
284                                 \r
285                                 if( ulLength >= UIP_BUFSIZE )\r
286                                 {\r
287                                         /* The data won't fit in our buffer.  Ensure we don't\r
288                                         try to write into the buffer. */\r
289                                         ulLength = 0;\r
290                                 }\r
291 \r
292                                 /* Read out the data into our buffer. */\r
293                                 for( i = 0; i < ulLength; i += sizeof( unsigned portLONG ) )\r
294                                 {\r
295                                         *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );\r
296                                         pulBuffer++;\r
297                                 }\r
298                                 \r
299                                 /* Store the length of the data into the separate array. */\r
300                                 ulRxLength[ ulNextRxBuffer ] = ulLength;\r
301                                 \r
302                                 /* Use the next buffer the next time through. */\r
303                                 ulNextRxBuffer++;\r
304                                 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )\r
305                                 {\r
306                                         ulNextRxBuffer = 0;\r
307                                 }\r
308                 \r
309                                 /* Ensure the uIP task is not blocked as data has arrived. */\r
310                                 xSemaphoreGive( xEMACSemaphore );\r
311                         }\r
312                 }\r
313                 \r
314                 EthernetIntEnable( ETH_BASE, ETH_INT_RX );\r
315                 \r
316                 ( void ) ulInt;\r
317         }\r
318 }\r
319 \r