]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3Sxxxx_IAR_Keil/webserver/emac.c
1eeaf3a159fc94545d09486cc98dd9b06b760542
[freertos] / FreeRTOS / Demo / CORTEX_LM3Sxxxx_IAR_Keil / webserver / emac.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* Kernel includes. */\r
29 #include "FreeRTOS.h"\r
30 #include "semphr.h"\r
31 #include "task.h"\r
32 \r
33 /* Demo includes. */\r
34 #include "emac.h"\r
35 \r
36 /* uIP includes. */\r
37 #include "uip.h"\r
38 \r
39 /* Hardware library includes. */\r
40 #include "hw_types.h"\r
41 #include "hw_memmap.h"\r
42 #include "hw_ints.h"\r
43 #include "hw_ethernet.h"\r
44 #include "ethernet.h"\r
45 #include "interrupt.h"\r
46 \r
47 #define emacNUM_RX_BUFFERS              5\r
48 #define emacFRAM_SIZE_BYTES     2\r
49 #define macNEGOTIATE_DELAY              2000\r
50 #define macWAIT_SEND_TIME               ( 10 )\r
51 \r
52 /* The task that handles the MAC peripheral.  This is created at a high\r
53 priority and is effectively a deferred interrupt handler.  The peripheral\r
54 handling is deferred to a task to prevent the entire FIFO having to be read\r
55 from within an ISR. */\r
56 void vMACHandleTask( void *pvParameters );\r
57 \r
58 /*-----------------------------------------------------------*/\r
59 \r
60 /* The semaphore used to wake the uIP task when data arrives. */\r
61 SemaphoreHandle_t xEMACSemaphore = NULL;\r
62 \r
63 /* The semaphore used to wake the interrupt handler task.  The peripheral\r
64 is processed at the task level to prevent the need to read the entire FIFO from\r
65 within the ISR itself. */\r
66 SemaphoreHandle_t xMACInterruptSemaphore = NULL;\r
67 \r
68 /* The buffer used by the uIP stack.  In this case the pointer is used to\r
69 point to one of the Rx buffers. */\r
70 unsigned char *uip_buf;\r
71 \r
72 /* Buffers into which Rx data is placed. */\r
73 static union\r
74 {\r
75         unsigned long ulJustForAlignment;\r
76         unsigned char ucRxBuffers[ emacNUM_RX_BUFFERS ][ UIP_BUFSIZE + ( 4 * emacFRAM_SIZE_BYTES ) ];\r
77 } uxRxBuffers;\r
78 \r
79 /* The length of the data within each of the Rx buffers. */\r
80 static unsigned long ulRxLength[ emacNUM_RX_BUFFERS ];\r
81 \r
82 /* Used to keep a track of the number of bytes to transmit. */\r
83 static unsigned long ulNextTxSpace;\r
84 \r
85 /*-----------------------------------------------------------*/\r
86 \r
87 portBASE_TYPE vInitEMAC( void )\r
88 {\r
89 unsigned long ulTemp;\r
90 portBASE_TYPE xReturn;\r
91 \r
92         /* Ensure all interrupts are disabled. */\r
93         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
94 \r
95         /* Clear any interrupts that were already pending. */\r
96     ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );\r
97     EthernetIntClear( ETH_BASE, ulTemp );\r
98 \r
99         /* Initialise the MAC and connect. */\r
100     EthernetInit( ETH_BASE );\r
101     EthernetConfigSet( ETH_BASE, ( ETH_CFG_TX_DPLXEN | ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN ) );\r
102     EthernetEnable( ETH_BASE );\r
103 \r
104         /* Mark each Rx buffer as empty. */\r
105         for( ulTemp = 0; ulTemp < emacNUM_RX_BUFFERS; ulTemp++ )\r
106         {\r
107                 ulRxLength[ ulTemp ] = 0;\r
108         }\r
109 \r
110         /* Create the queue and task used to defer the MAC processing to the\r
111         task level. */\r
112         vSemaphoreCreateBinary( xMACInterruptSemaphore );\r
113         xSemaphoreTake( xMACInterruptSemaphore, 0 );\r
114         xReturn = xTaskCreate( vMACHandleTask, "MAC", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );\r
115         vTaskDelay( macNEGOTIATE_DELAY );\r
116 \r
117         /* We are only interested in Rx interrupts. */\r
118         IntPrioritySet( INT_ETH, configKERNEL_INTERRUPT_PRIORITY );\r
119     IntEnable( INT_ETH );\r
120     EthernetIntEnable(ETH_BASE, ETH_INT_RX);\r
121 \r
122         return xReturn;\r
123 }\r
124 /*-----------------------------------------------------------*/\r
125 \r
126 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )\r
127 {\r
128 static unsigned long ulNextRxBuffer = 0;\r
129 unsigned int iLen;\r
130 \r
131         iLen = ulRxLength[ ulNextRxBuffer ];\r
132 \r
133         if( iLen != 0 )\r
134         {\r
135                 /* Leave room for the size at the start of the buffer. */\r
136                 uip_buf = &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );\r
137 \r
138                 ulRxLength[ ulNextRxBuffer ] = 0;\r
139 \r
140                 ulNextRxBuffer++;\r
141                 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )\r
142                 {\r
143                         ulNextRxBuffer = 0;\r
144                 }\r
145         }\r
146 \r
147     return iLen;\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 void vInitialiseSend( void )\r
152 {\r
153         /* Set the index to the first byte to send - skipping over the size\r
154         bytes. */\r
155         ulNextTxSpace = 2;\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 void vIncrementTxLength( unsigned long ulLength )\r
160 {\r
161         ulNextTxSpace += ulLength;\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 void vSendBufferToMAC( void )\r
166 {\r
167 unsigned long *pulSource;\r
168 unsigned short * pus;\r
169 unsigned long ulNextWord;\r
170 \r
171         /* Locate the data to be send. */\r
172         pus = ( unsigned short * ) uip_buf;\r
173 \r
174         /* Add in the size of the data. */\r
175         pus--;\r
176         *pus = ulNextTxSpace;\r
177 \r
178         /* Wait for data to be sent if there is no space immediately. */\r
179     while( !EthernetSpaceAvail( ETH_BASE ) )\r
180     {\r
181                 vTaskDelay( macWAIT_SEND_TIME );\r
182     }\r
183 \r
184         pulSource = ( unsigned long * ) pus;\r
185 \r
186         for( ulNextWord = 0; ulNextWord < ulNextTxSpace; ulNextWord += sizeof( unsigned long ) )\r
187         {\r
188         HWREG(ETH_BASE + MAC_O_DATA) = *pulSource;\r
189                 pulSource++;\r
190         }\r
191 \r
192         /* Go. */\r
193     HWREG( ETH_BASE + MAC_O_TR ) = MAC_TR_NEWTX;\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 void vEMAC_ISR( void )\r
198 {\r
199 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
200 unsigned long ulTemp;\r
201 \r
202         /* Clear the interrupt. */\r
203         ulTemp = EthernetIntStatus( ETH_BASE, pdFALSE );\r
204         EthernetIntClear( ETH_BASE, ulTemp );\r
205 \r
206         /* Was it an Rx interrupt? */\r
207         if( ulTemp & ETH_INT_RX )\r
208         {\r
209                 xSemaphoreGiveFromISR( xMACInterruptSemaphore, &xHigherPriorityTaskWoken );\r
210                 EthernetIntDisable( ETH_BASE, ETH_INT_RX );\r
211         }\r
212 \r
213     /* Switch to the uIP task. */\r
214         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 void vMACHandleTask( void *pvParameters )\r
219 {\r
220 unsigned long i, ulInt;\r
221 unsigned long ulLength;\r
222 unsigned long *pulBuffer;\r
223 static unsigned long ulNextRxBuffer = 0;\r
224 \r
225         for( ;; )\r
226         {\r
227                 /* Wait for something to do. */\r
228                 xSemaphoreTake( xMACInterruptSemaphore, portMAX_DELAY );\r
229 \r
230                 while( ( ulInt = ( EthernetIntStatus( ETH_BASE, pdFALSE ) & ETH_INT_RX ) ) != 0 )\r
231                 {\r
232                         ulLength = HWREG( ETH_BASE + MAC_O_DATA );\r
233 \r
234                         /* Leave room at the start of the buffer for the size. */\r
235                         pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 2 ] );\r
236                         *pulBuffer = ( ulLength >> 16 );\r
237 \r
238                         /* Get the size of the data. */\r
239                         pulBuffer = ( unsigned long * ) &( uxRxBuffers.ucRxBuffers[ ulNextRxBuffer ][ 4 ] );\r
240                         ulLength &= 0xFFFF;\r
241 \r
242                         if( ulLength > 4 )\r
243                         {\r
244                                 ulLength -= 4;\r
245 \r
246                                 if( ulLength >= UIP_BUFSIZE )\r
247                                 {\r
248                                         /* The data won't fit in our buffer.  Ensure we don't\r
249                                         try to write into the buffer. */\r
250                                         ulLength = 0;\r
251                                 }\r
252 \r
253                                 /* Read out the data into our buffer. */\r
254                                 for( i = 0; i < ulLength; i += sizeof( unsigned long ) )\r
255                                 {\r
256                                         *pulBuffer = HWREG( ETH_BASE + MAC_O_DATA );\r
257                                         pulBuffer++;\r
258                                 }\r
259 \r
260                                 /* Store the length of the data into the separate array. */\r
261                                 ulRxLength[ ulNextRxBuffer ] = ulLength;\r
262 \r
263                                 /* Use the next buffer the next time through. */\r
264                                 ulNextRxBuffer++;\r
265                                 if( ulNextRxBuffer >= emacNUM_RX_BUFFERS )\r
266                                 {\r
267                                         ulNextRxBuffer = 0;\r
268                                 }\r
269 \r
270                                 /* Ensure the uIP task is not blocked as data has arrived. */\r
271                                 xSemaphoreGive( xEMACSemaphore );\r
272                         }\r
273                 }\r
274 \r
275                 EthernetIntEnable( ETH_BASE, ETH_INT_RX );\r
276 \r
277                 ( void ) ulInt;\r
278         }\r
279 }\r
280 \r