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