]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LM3Sxxxx_IAR_Keil/webserver/emac.c
Update to V5.4.1
[freertos] / Demo / CORTEX_LM3Sxxxx_IAR_Keil / webserver / emac.c
1 /*\r
2         FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /* Kernel includes. */\r
49 #include "FreeRTOS.h"\r
50 #include "semphr.h"\r
51 #include "task.h"\r
52 \r
53 /* Demo includes. */\r
54 #include "emac.h"\r
55 \r
56 /* uIP includes. */\r
57 #include "uip.h"\r
58 \r
59 /* Hardware library includes. */\r
60 #include "hw_types.h"\r
61 #include "hw_memmap.h"\r
62 #include "hw_ints.h"\r
63 #include "hw_ethernet.h"\r
64 #include "ethernet.h"\r
65 #include "interrupt.h"\r
66 \r
67 #define emacNUM_RX_BUFFERS              5\r
68 #define emacFRAM_SIZE_BYTES     2\r
69 #define macNEGOTIATE_DELAY              2000\r
70 #define macWAIT_SEND_TIME               ( 10 )\r
71 \r
72 /* The task that handles the MAC peripheral.  This is created at a high\r
73 priority and is effectively a deferred interrupt handler.  The peripheral\r
74 handling is deferred to a task to prevent the entire FIFO having to be read\r
75 from within an ISR. */\r
76 void vMACHandleTask( void *pvParameters );\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 /* The semaphore used to wake the uIP task when data arrives. */\r
81 xSemaphoreHandle xEMACSemaphore = NULL;\r
82 \r
83 /* The semaphore used to wake the interrupt handler task.  The peripheral\r
84 is processed at the task level to prevent the need to read the entire FIFO from\r
85 within the ISR itself. */\r
86 xSemaphoreHandle xMACInterruptSemaphore = NULL;\r
87 \r
88 /* The buffer used by the uIP stack.  In this case the pointer is used to\r
89 point to one of the Rx buffers. */\r
90 unsigned portCHAR *uip_buf;\r
91 \r
92 /* Buffers into which Rx data is placed. */\r
93 static union\r
94 {\r
95         unsigned portLONG ulJustForAlignment;\r
96         unsigned portCHAR ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ];\r
97 } uxRxBuffers;\r
98 \r
99 /* The length of the data within each of the Rx buffers. */\r
100 static unsigned portLONG ulRxLength[ emacNUM_RX_BUFFERS ];\r
101 \r
102 /* Used to keep a track of the number of bytes to transmit. */\r
103 static unsigned portLONG ulNextTxSpace;\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 portBASE_TYPE vInitEMAC( void )\r
108 {\r
109 unsigned long ulTemp;\r
110 portBASE_TYPE xReturn;\r
111 \r
112         /* Ensure all interrupts are disabled. */\r
113         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
114 \r
115         /* Clear any interrupts that were already pending. */\r
116     ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );\r
117     EthernetIntClear( ETH_BASE, ulTemp );\r
118 \r
119         /* Initialise the MAC and connect. */\r
120     EthernetInit( ETH_BASE );\r
121     EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );\r
122     EthernetEnable( ETH_BASE );\r
123 \r
124         /* Mark each Rx buffer as empty. */\r
125         for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )\r
126         {\r
127                 ulRxLength[ ulTemp ] = 0;\r
128         }\r
129         \r
130         /* Create the queue and task used to defer the MAC processing to the\r
131         task level. */\r
132         vSemaphoreCreateBinary( xMACInterruptSemaphore );\r
133         xSemaphoreTake( xMACInterruptSemaphore, 0 );\r
134         xReturn = xTaskCreate( vMACHandleTask, ( signed portCHAR * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
135         vTaskDelay( macNEGOTIATE_DELAY );\r
136         \r
137         /* We are only interested in Rx interrupts. */\r
138         IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );\r
139     IntEnable( INT_ETH );\r
140     EthernetIntEnable(ETH_BASE, ETH_INT_RX);\r
141 \r
142         return xReturn;\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )\r
147 {\r
148 static unsigned long ulNextRxBuffer = 0;\r
149 unsigned int iLen;\r
150 \r
151         iLen = ulRxLength[ ulNextRxBuffer ];\r
152 \r
153         if( iLen != 0 )\r
154         {\r
155                 /* Leave room for the size at the start of the buffer. */\r
156                 uip_buf = &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );\r
157                 \r
158                 ulRxLength[ ulNextRxBuffer ] = 0;\r
159                 \r
160                 ulNextRxBuffer++;\r
161                 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )\r
162                 {\r
163                         ulNextRxBuffer = 0;\r
164                 }\r
165         }\r
166 \r
167     return iLen;\r
168 }\r
169 /*-----------------------------------------------------------*/\r
170 \r
171 void vInitialiseSend( void )\r
172 {\r
173         /* Set the index to the first byte to send - skipping over the size\r
174         bytes. */\r
175         ulNextTxSpace = 2;\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 void vIncrementTxLength( unsigned portLONG ulLength )\r
180 {\r
181         ulNextTxSpace += ulLength;\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 void vSendBufferToMAC( void )\r
186 {\r
187 unsigned long *pulSource;\r
188 unsigned portSHORT * pus;\r
189 unsigned portLONG ulNextWord;\r
190 \r
191         /* Locate the data to be send. */\r
192         pus = ( unsigned portSHORT * ) uip_buf;\r
193 \r
194         /* Add in the size of the data. */\r
195         pus--;\r
196         *pus = ulNextTxSpace;\r
197 \r
198         /* Wait for data to be sent if there is no space immediately. */\r
199     while( !EthernetSpaceAvail( ETH_BASE ) )\r
200     {\r
201                 vTaskDelay( macWAIT_SEND_TIME );\r
202     }\r
203         \r
204         pulSource = ( unsigned portLONG * ) pus;        \r
205         \r
206         for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned portLONG ) )\r
207         {\r
208         HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;\r
209                 pulSource++;\r
210         }\r
211 \r
212         /* Go. */\r
213     HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 void vEMAC_ISR( void )\r
218 {\r
219 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
220 unsigned portLONG ulTemp;\r
221 \r
222         /* Clear the interrupt. */\r
223         ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );\r
224         EthernetIntClear( ETH_BASE, ulTemp );\r
225                 \r
226         /* Was it an Rx interrupt? */\r
227         if( ulTemp & ETH_INT_RX )\r
228         {\r
229                 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );\r
230                 EthernetIntDisable( ETH_BASE, ETH_INT_RX );\r
231         }\r
232                 \r
233     /* Switch to the uIP task. */\r
234         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 void vMACHandleTask( void *pvParameters )\r
239 {\r
240 unsigned long i, ulInt;\r
241 unsigned portLONG ulLength;\r
242 unsigned long *pulBuffer;\r
243 static unsigned portLONG ulNextRxBuffer = 0;\r
244 \r
245         for( ;; )\r
246         {\r
247                 /* Wait for something to do. */\r
248                 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );\r
249                 \r
250                 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )\r
251                 {               \r
252                         ulLength = HWREG( ETH_BASE + MAC_O_DATA );\r
253                         \r
254                         /* Leave room at the start of the buffer for the size. */\r
255                         pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );                    \r
256                         *pulBuffer = ( ulLength >> 16 );\r
257 \r
258                         /* Get the size of the data. */                 \r
259                         pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 4 ] );                    \r
260                         ulLength &= 0xFFFF;\r
261                         \r
262                         if( ulLength > 4 )\r
263                         {\r
264                                 ulLength -= 4;\r
265                                 \r
266                                 if( ulLength >= UIP_BUFSIZE )\r
267                                 {\r
268                                         /* The data won't fit in our buffer.  Ensure we don't\r
269                                         try to write into the buffer. */\r
270                                         ulLength = 0;\r
271                                 }\r
272 \r
273                                 /* Read out the data into our buffer. */\r
274                                 for( i = 0; i < ulLength; i += sizeof( unsigned portLONG ) )\r
275                                 {\r
276                                         *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );\r
277                                         pulBuffer++;\r
278                                 }\r
279                                 \r
280                                 /* Store the length of the data into the separate array. */\r
281                                 ulRxLength[ ulNextRxBuffer ] = ulLength;\r
282                                 \r
283                                 /* Use the next buffer the next time through. */\r
284                                 ulNextRxBuffer++;\r
285                                 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )\r
286                                 {\r
287                                         ulNextRxBuffer = 0;\r
288                                 }\r
289                 \r
290                                 /* Ensure the uIP task is not blocked as data has arrived. */\r
291                                 xSemaphoreGive( xEMACSemaphore );\r
292                         }\r
293                 }\r
294                 \r
295                 EthernetIntEnable( ETH_BASE, ETH_INT_RX );\r
296                 \r
297                 ( void ) ulInt;\r
298         }\r
299 }\r
300 \r