]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LM3Sxxxx_IAR_Keil/webserver/emac.c
1b49129ca970f6bba15404c4aca72c2638bc7b1b
[freertos] / Demo / CORTEX_LM3Sxxxx_IAR_Keil / webserver / emac.c
1 /*\r
2         FreeRTOS.org V5.0.4 - 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 unsigned portCHAR ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ];\r
79 \r
80 /* The length of the data within each of the Rx buffers. */\r
81 static unsigned portLONG ulRxLength[ emacNUM_RX_BUFFERS ];\r
82 \r
83 /* Used to keep a track of the number of bytes to transmit. */\r
84 static unsigned portLONG ulNextTxSpace;\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 portBASE_TYPE vInitEMAC( void )\r
89 {\r
90 unsigned long ulTemp;\r
91 portBASE_TYPE xReturn;\r
92 \r
93         /* Ensure all interrupts are disabled. */\r
94         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
95 \r
96         /* Clear any interrupts that were already pending. */\r
97     ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );\r
98     EthernetIntClear( ETH_BASE, ulTemp );\r
99 \r
100         /* Initialise the MAC and connect. */\r
101     EthernetInit( ETH_BASE );\r
102     EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );\r
103     EthernetEnable( ETH_BASE );\r
104 \r
105         /* Mark each Rx buffer as empty. */\r
106         for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )\r
107         {\r
108                 ulRxLength[ ulTemp ] = 0;\r
109         }\r
110         \r
111         /* Create the queue and task used to defer the MAC processing to the\r
112         task level. */\r
113         vSemaphoreCreateBinary( xMACInterruptSemaphore );\r
114         xSemaphoreTake( xMACInterruptSemaphore, 0 );\r
115         xReturn = xTaskCreate( vMACHandleTask, ( signed portCHAR * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
116         vTaskDelay( macNEGOTIATE_DELAY );\r
117         \r
118         /* We are only interested in Rx interrupts. */\r
119         IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );\r
120     IntEnable( INT_ETH );\r
121     EthernetIntEnable(ETH_BASE, ETH_INT_RX);\r
122 \r
123         return xReturn;\r
124 }\r
125 /*-----------------------------------------------------------*/\r
126 \r
127 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )\r
128 {\r
129 static unsigned long ulNextRxBuffer = 0;\r
130 unsigned int iLen;\r
131 \r
132         iLen = ulRxLength[ ulNextRxBuffer ];\r
133 \r
134         if( iLen != 0 )\r
135         {\r
136                 /* Leave room for the size at the start of the buffer. */\r
137                 uip_buf = &( ucRxBuffers[ ulNextRxBuffer ][ 2 ] );\r
138                 \r
139                 ulRxLength[ ulNextRxBuffer ] = 0;\r
140                 \r
141                 ulNextRxBuffer++;\r
142                 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )\r
143                 {\r
144                         ulNextRxBuffer = 0;\r
145                 }\r
146         }\r
147 \r
148     return iLen;\r
149 }\r
150 /*-----------------------------------------------------------*/\r
151 \r
152 void vInitialiseSend( void )\r
153 {\r
154         /* Set the index to the first byte to send - skipping over the size\r
155         bytes. */\r
156         ulNextTxSpace = 2;\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 void vIncrementTxLength( unsigned portLONG ulLength )\r
161 {\r
162         ulNextTxSpace += ulLength;\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 void vSendBufferToMAC( void )\r
167 {\r
168 unsigned long *pulSource;\r
169 unsigned portSHORT * pus;\r
170 unsigned portLONG ulNextWord;\r
171 \r
172         /* Locate the data to be send. */\r
173         pus = ( unsigned portSHORT * ) uip_buf;\r
174 \r
175         /* Add in the size of the data. */\r
176         pus--;\r
177         *pus = ulNextTxSpace;\r
178 \r
179         /* Wait for data to be sent if there is no space immediately. */\r
180     while( !EthernetSpaceAvail( ETH_BASE ) )\r
181     {\r
182                 vTaskDelay( macWAIT_SEND_TIME );\r
183     }\r
184         \r
185         pulSource = ( unsigned portLONG * ) pus;        \r
186         \r
187         for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned portLONG ) )\r
188         {\r
189         HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;\r
190                 pulSource++;\r
191         }\r
192 \r
193         /* Go. */\r
194     HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 void vEMAC_ISR( void )\r
199 {\r
200 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
201 unsigned portLONG ulTemp;\r
202 \r
203         /* Clear the interrupt. */\r
204         ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );\r
205         EthernetIntClear( ETH_BASE, ulTemp );\r
206                 \r
207         /* Was it an Rx interrupt? */\r
208         if( ulTemp & ETH_INT_RX )\r
209         {\r
210                 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );\r
211                 EthernetIntDisable( ETH_BASE, ETH_INT_RX );\r
212         }\r
213                 \r
214     /* Switch to the uIP task. */\r
215         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 void vMACHandleTask( void *pvParameters )\r
220 {\r
221 unsigned long ulLen = 0, i;\r
222 unsigned portLONG ulLength, ulInt;\r
223 unsigned long *pulBuffer;\r
224 static unsigned portLONG ulNextRxBuffer = 0;\r
225 portBASE_TYPE xSwitchRequired = pdFALSE;\r
226 \r
227         for( ;; )\r
228         {\r
229                 /* Wait for something to do. */\r
230                 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );\r
231                 \r
232                 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )\r
233                 {               \r
234                         ulLength = HWREG( ETH_BASE + MAC_O_DATA );\r
235                         \r
236                         /* Leave room at the start of the buffer for the size. */\r
237                         pulBuffer = ( unsigned long * ) &( ucRxBuffers[ ulNextRxBuffer ][ 2 ] );                        \r
238                         *pulBuffer = ( ulLength >> 16 );\r
239 \r
240                         /* Get the size of the data. */                 \r
241                         pulBuffer = ( unsigned long * ) &( ucRxBuffers[ ulNextRxBuffer ][ 4 ] );                        \r
242                         ulLength &= 0xFFFF;\r
243                         \r
244                         if( ulLength > 4 )\r
245                         {\r
246                                 ulLength -= 4;\r
247                                 \r
248                                 if( ulLength >= UIP_BUFSIZE )\r
249                                 {\r
250                                         /* The data won't fit in our buffer.  Ensure we don't\r
251                                         try to write into the buffer. */\r
252                                         ulLength = 0;\r
253                                 }\r
254 \r
255                                 /* Read out the data into our buffer. */\r
256                                 for( i = 0; i < ulLength; i += sizeof( unsigned portLONG ) )\r
257                                 {\r
258                                         *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );\r
259                                         pulBuffer++;\r
260                                 }\r
261                                 \r
262                                 /* Store the length of the data into the separate array. */\r
263                                 ulRxLength[ ulNextRxBuffer ] = ulLength;\r
264                                 \r
265                                 /* Use the next buffer the next time through. */\r
266                                 ulNextRxBuffer++;\r
267                                 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )\r
268                                 {\r
269                                         ulNextRxBuffer = 0;\r
270                                 }\r
271                 \r
272                                 /* Ensure the uIP task is not blocked as data has arrived. */\r
273                                 xSemaphoreGive( xEMACSemaphore );\r
274                         }\r
275                 }\r
276                 \r
277                 EthernetIntEnable( ETH_BASE, ETH_INT_RX );\r
278         }\r
279 }\r
280 \r