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