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