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