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