]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LM3Sxxxx_Eclipse/RTOSDemo/webserver/emac.c
dde8aa392082477bfebfe97a1ade8111636f4425
[freertos] / Demo / CORTEX_LM3Sxxxx_Eclipse / RTOSDemo / webserver / emac.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\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 exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     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 unsigned portCHAR ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ];\r
100 \r
101 /* The length of the data within each of the Rx buffers. */\r
102 static unsigned portLONG ulRxLength[ emacNUM_RX_BUFFERS ];\r
103 \r
104 /* Used to keep a track of the number of bytes to transmit. */\r
105 static unsigned portLONG ulNextTxSpace;\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 portBASE_TYPE vInitEMAC( void )\r
110 {\r
111 unsigned long ulTemp;\r
112 portBASE_TYPE xReturn;\r
113 \r
114         /* Ensure all interrupts are disabled. */\r
115         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
116 \r
117         /* Clear any interrupts that were already pending. */\r
118     ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );\r
119     EthernetIntClear( ETH_BASE, ulTemp );\r
120 \r
121         /* Initialise the MAC and connect. */\r
122     EthernetInit( ETH_BASE );\r
123     EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );\r
124     EthernetEnable( ETH_BASE );\r
125 \r
126         /* Mark each Rx buffer as empty. */\r
127         for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )\r
128         {\r
129                 ulRxLength[ ulTemp ] = 0;\r
130         }\r
131         \r
132         /* Create the queue and task used to defer the MAC processing to the\r
133         task level. */\r
134         vSemaphoreCreateBinary( xMACInterruptSemaphore );\r
135         xSemaphoreTake( xMACInterruptSemaphore, 0 );\r
136         xReturn = xTaskCreate( vMACHandleTask, ( signed portCHAR * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
137         vTaskDelay( macNEGOTIATE_DELAY );\r
138         \r
139         /* We are only interested in Rx interrupts. */\r
140         IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );\r
141     IntEnable( INT_ETH );\r
142     EthernetIntEnable(ETH_BASE, ETH_INT_RX);\r
143 \r
144         return xReturn;\r
145 }\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )\r
149 {\r
150 static unsigned long ulNextRxBuffer = 0;\r
151 unsigned int iLen;\r
152 \r
153         iLen = ulRxLength[ ulNextRxBuffer ];\r
154 \r
155         if( iLen != 0 )\r
156         {\r
157                 /* Leave room for the size at the start of the buffer. */\r
158                 uip_buf = &( ucRxBuffers[ ulNextRxBuffer ][ 2 ] );\r
159                 \r
160                 ulRxLength[ ulNextRxBuffer ] = 0;\r
161                 \r
162                 ulNextRxBuffer++;\r
163                 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )\r
164                 {\r
165                         ulNextRxBuffer = 0;\r
166                 }\r
167         }\r
168 \r
169     return iLen;\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 void vInitialiseSend( void )\r
174 {\r
175         /* Set the index to the first byte to send - skipping over the size\r
176         bytes. */\r
177         ulNextTxSpace = 2;\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 void vIncrementTxLength( unsigned portLONG ulLength )\r
182 {\r
183         ulNextTxSpace += ulLength;\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 void vSendBufferToMAC( void )\r
188 {\r
189 unsigned long *pulSource;\r
190 unsigned portSHORT * pus;\r
191 unsigned portLONG ulNextWord;\r
192 \r
193         /* Locate the data to be send. */\r
194         pus = ( unsigned portSHORT * ) uip_buf;\r
195 \r
196         /* Add in the size of the data. */\r
197         pus--;\r
198         *pus = ulNextTxSpace;\r
199 \r
200         /* Wait for data to be sent if there is no space immediately. */\r
201     while( !EthernetSpaceAvail( ETH_BASE ) )\r
202     {\r
203                 vTaskDelay( macWAIT_SEND_TIME );\r
204     }\r
205         \r
206         pulSource = ( unsigned portLONG * ) pus;        \r
207         \r
208         for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned portLONG ) )\r
209         {\r
210         HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;\r
211                 pulSource++;\r
212         }\r
213 \r
214         /* Go. */\r
215     HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 void vEMAC_ISR( void )\r
220 {\r
221 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
222 unsigned portLONG ulTemp;\r
223 \r
224         /* Clear the interrupt. */\r
225         ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );\r
226         EthernetIntClear( ETH_BASE, ulTemp );\r
227                 \r
228         /* Was it an Rx interrupt? */\r
229         if( ulTemp & ETH_INT_RX )\r
230         {\r
231                 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );\r
232                 EthernetIntDisable( ETH_BASE, ETH_INT_RX );\r
233         }\r
234                 \r
235     /* Switch to the uIP task. */\r
236         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 void vMACHandleTask( void *pvParameters )\r
241 {\r
242 unsigned long ulLen = 0, i;\r
243 unsigned portLONG ulLength, ulInt;\r
244 unsigned long *pulBuffer;\r
245 static unsigned portLONG ulNextRxBuffer = 0;\r
246 portBASE_TYPE xSwitchRequired = pdFALSE;\r
247 \r
248         for( ;; )\r
249         {\r
250                 /* Wait for something to do. */\r
251                 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );\r
252                 \r
253                 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )\r
254                 {               \r
255                         ulLength = HWREG( ETH_BASE + MAC_O_DATA );\r
256                         \r
257                         /* Leave room at the start of the buffer for the size. */\r
258                         pulBuffer = ( unsigned long * ) &( ucRxBuffers[ ulNextRxBuffer ][ 2 ] );                        \r
259                         *pulBuffer = ( ulLength >> 16 );\r
260 \r
261                         /* Get the size of the data. */                 \r
262                         pulBuffer = ( unsigned long * ) &( ucRxBuffers[ ulNextRxBuffer ][ 4 ] );                        \r
263                         ulLength &= 0xFFFF;\r
264                         \r
265                         if( ulLength > 4 )\r
266                         {\r
267                                 ulLength -= 4;\r
268                                 \r
269                                 if( ulLength >= UIP_BUFSIZE )\r
270                                 {\r
271                                         /* The data won't fit in our buffer.  Ensure we don't\r
272                                         try to write into the buffer. */\r
273                                         ulLength = 0;\r
274                                 }\r
275 \r
276                                 /* Read out the data into our buffer. */\r
277                                 for( i = 0; i < ulLength; i += sizeof( unsigned portLONG ) )\r
278                                 {\r
279                                         *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );\r
280                                         pulBuffer++;\r
281                                 }\r
282                                 \r
283                                 /* Store the length of the data into the separate array. */\r
284                                 ulRxLength[ ulNextRxBuffer ] = ulLength;\r
285                                 \r
286                                 /* Use the next buffer the next time through. */\r
287                                 ulNextRxBuffer++;\r
288                                 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )\r
289                                 {\r
290                                         ulNextRxBuffer = 0;\r
291                                 }\r
292                 \r
293                                 /* Ensure the uIP task is not blocked as data has arrived. */\r
294                                 xSemaphoreGive( xEMACSemaphore );\r
295                         }\r
296                 }\r
297                 \r
298                 EthernetIntEnable( ETH_BASE, ETH_INT_RX );\r
299         }\r
300 }\r
301 \r