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