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