]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3Sxxxx_Rowley/webserver/emac.c
Remove compiler warnings.
[freertos] / FreeRTOS / Demo / CORTEX_LM3Sxxxx_Rowley / webserver / emac.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
67     Integrity Systems, who sell the code with commercial support,\r
68     indemnification and middleware, under the OpenRTOS brand.\r
69 \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
71     engineered and independently SIL3 certified version for use in safety and\r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /* Kernel includes. */\r
76 #include "FreeRTOS.h"\r
77 #include "semphr.h"\r
78 #include "task.h"\r
79 \r
80 /* Demo includes. */\r
81 #include "emac.h"\r
82 \r
83 /* uIP includes. */\r
84 #include "uip.h"\r
85 \r
86 /* Hardware library includes. */\r
87 #include "hw_types.h"\r
88 #include "hw_memmap.h"\r
89 #include "hw_ints.h"\r
90 #include "hw_ethernet.h"\r
91 #include "ethernet.h"\r
92 #include "interrupt.h"\r
93 \r
94 #define emacNUM_RX_BUFFERS              5\r
95 #define emacFRAM_SIZE_BYTES     2\r
96 #define macNEGOTIATE_DELAY              2000\r
97 #define macWAIT_SEND_TIME               ( 10 )\r
98 \r
99 /* The task that handles the MAC peripheral.  This is created at a high\r
100 priority and is effectively a deferred interrupt handler.  The peripheral\r
101 handling is deferred to a task to prevent the entire FIFO having to be read\r
102 from within an ISR. */\r
103 void vMACHandleTask( void *pvParameters );\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /* The semaphore used to wake the uIP task when data arrives. */\r
108 xSemaphoreHandle xEMACSemaphore = NULL;\r
109 \r
110 /* The semaphore used to wake the interrupt handler task.  The peripheral\r
111 is processed at the task level to prevent the need to read the entire FIFO from\r
112 within the ISR itself. */\r
113 xSemaphoreHandle xMACInterruptSemaphore = NULL;\r
114 \r
115 /* The buffer used by the uIP stack.  In this case the pointer is used to\r
116 point to one of the Rx buffers. */\r
117 unsigned char *uip_buf;\r
118 \r
119 /* Buffers into which Rx data is placed. */\r
120 static unsigned char ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ] __attribute__((aligned(4)));\r
121 \r
122 /* The length of the data within each of the Rx buffers. */\r
123 static unsigned long ulRxLength[ emacNUM_RX_BUFFERS ];\r
124 \r
125 /* Used to keep a track of the number of bytes to transmit. */\r
126 static unsigned long ulNextTxSpace;\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 portBASE_TYPE vInitEMAC( void )\r
131 {\r
132 unsigned long ulTemp;\r
133 portBASE_TYPE xReturn;\r
134 \r
135         /* Ensure all interrupts are disabled. */\r
136         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
137 \r
138         /* Clear any interrupts that were already pending. */\r
139     ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );\r
140     EthernetIntClear( ETH_BASE, ulTemp );\r
141 \r
142         /* Initialise the MAC and connect. */\r
143     EthernetInit( ETH_BASE );\r
144     EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );\r
145     EthernetEnable( ETH_BASE );\r
146 \r
147         /* Mark each Rx buffer as empty. */\r
148         for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )\r
149         {\r
150                 ulRxLength[ ulTemp ] = 0;\r
151         }\r
152 \r
153         /* Create the queue and task used to defer the MAC processing to the\r
154         task level. */\r
155         vSemaphoreCreateBinary( xMACInterruptSemaphore );\r
156         xSemaphoreTake( xMACInterruptSemaphore, 0 );\r
157         xReturn = xTaskCreate( vMACHandleTask, ( signed char * ) "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
158         vTaskDelay( macNEGOTIATE_DELAY );\r
159 \r
160         /* We are only interested in Rx interrupts. */\r
161         IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );\r
162     IntEnable( INT_ETH );\r
163     EthernetIntEnable(ETH_BASE, ETH_INT_RX);\r
164 \r
165         return xReturn;\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )\r
170 {\r
171 static unsigned long ulNextRxBuffer = 0;\r
172 unsigned int iLen;\r
173 \r
174         iLen = ulRxLength[ ulNextRxBuffer ];\r
175 \r
176         if( iLen != 0 )\r
177         {\r
178                 /* Leave room for the size at the start of the buffer. */\r
179                 uip_buf = &( ucRxBuffers[ ulNextRxBuffer ][ 2 ] );\r
180 \r
181                 ulRxLength[ ulNextRxBuffer ] = 0;\r
182 \r
183                 ulNextRxBuffer++;\r
184                 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )\r
185                 {\r
186                         ulNextRxBuffer = 0;\r
187                 }\r
188         }\r
189 \r
190     return iLen;\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 void vInitialiseSend( void )\r
195 {\r
196         /* Set the index to the first byte to send - skipping over the size\r
197         bytes. */\r
198         ulNextTxSpace = 2;\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 void vIncrementTxLength( unsigned long ulLength )\r
203 {\r
204         ulNextTxSpace += ulLength;\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 void vSendBufferToMAC( void )\r
209 {\r
210 unsigned long *pulSource;\r
211 unsigned short * pus;\r
212 unsigned long ulNextWord;\r
213 \r
214         /* Locate the data to be send. */\r
215         pus = ( unsigned short * ) uip_buf;\r
216 \r
217         /* Add in the size of the data. */\r
218         pus--;\r
219         *pus = ulNextTxSpace;\r
220 \r
221         /* Wait for data to be sent if there is no space immediately. */\r
222     while( !EthernetSpaceAvail( ETH_BASE ) )\r
223     {\r
224                 vTaskDelay( macWAIT_SEND_TIME );\r
225     }\r
226 \r
227         pulSource = ( unsigned long * ) pus;\r
228 \r
229         for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned long ) )\r
230         {\r
231         HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;\r
232                 pulSource++;\r
233         }\r
234 \r
235         /* Go. */\r
236     HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 void vEMAC_ISR( void )\r
241 {\r
242 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
243 unsigned long ulTemp;\r
244 \r
245         /* Clear the interrupt. */\r
246         ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );\r
247         EthernetIntClear( ETH_BASE, ulTemp );\r
248 \r
249         /* Was it an Rx interrupt? */\r
250         if( ulTemp & ETH_INT_RX )\r
251         {\r
252                 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );\r
253                 EthernetIntDisable( ETH_BASE, ETH_INT_RX );\r
254         }\r
255 \r
256     /* Switch to the uIP task. */\r
257         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 void vMACHandleTask( void *pvParameters )\r
262 {\r
263 unsigned long i;\r
264 unsigned long ulLength, ulInt;\r
265 unsigned long *pulBuffer;\r
266 static unsigned long ulNextRxBuffer = 0;\r
267 \r
268         for( ;; )\r
269         {\r
270                 /* Wait for something to do. */\r
271                 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );\r
272 \r
273                 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )\r
274                 {\r
275                         ulLength = HWREG( ETH_BASE + MAC_O_DATA );\r
276 \r
277                         /* Leave room at the start of the buffer for the size. */\r
278                         pulBuffer = ( unsigned long * ) &( ucRxBuffers[ ulNextRxBuffer ][ 2 ] );\r
279                         *pulBuffer = ( ulLength >> 16 );\r
280 \r
281                         /* Get the size of the data. */\r
282                         pulBuffer = ( unsigned long * ) &( ucRxBuffers[ ulNextRxBuffer ][ 4 ] );\r
283                         ulLength &= 0xFFFF;\r
284 \r
285                         if( ulLength > 4 )\r
286                         {\r
287                                 ulLength -= 4;\r
288 \r
289                                 if( ulLength >= UIP_BUFSIZE )\r
290                                 {\r
291                                         /* The data won't fit in our buffer.  Ensure we don't\r
292                                         try to write into the buffer. */\r
293                                         ulLength = 0;\r
294                                 }\r
295 \r
296                                 /* Read out the data into our buffer. */\r
297                                 for( i = 0; i < ulLength; i += sizeof( unsigned long ) )\r
298                                 {\r
299                                         *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );\r
300                                         pulBuffer++;\r
301                                 }\r
302 \r
303                                 /* Store the length of the data into the separate array. */\r
304                                 ulRxLength[ ulNextRxBuffer ] = ulLength;\r
305 \r
306                                 /* Use the next buffer the next time through. */\r
307                                 ulNextRxBuffer++;\r
308                                 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )\r
309                                 {\r
310                                         ulNextRxBuffer = 0;\r
311                                 }\r
312 \r
313                                 /* Ensure the uIP task is not blocked as data has arrived. */\r
314                                 xSemaphoreGive( xEMACSemaphore );\r
315                         }\r
316                 }\r
317 \r
318                 EthernetIntEnable( ETH_BASE, ETH_INT_RX );\r
319         }\r
320 }\r
321 \r