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