]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_LPC1768_IAR/webserver/emac.c
Updated pack to FreeRTOS 10.3.1
[cmsis-freertos] / Demo / CORTEX_LPC1768_IAR / webserver / emac.c
1 /*
2  * FreeRTOS Kernel V10.3.1
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /* Originally adapted from file written by Andreas Dannenberg.  Supplied with permission. */
29
30 /* Kernel includes. */
31 #include "FreeRTOS.h"
32 #include "task.h"
33 #include "semphr.h"
34
35 /* Hardware specific includes. */
36 #include "EthDev_LPC17xx.h"
37
38 /* Time to wait between each inspection of the link status. */
39 #define emacWAIT_FOR_LINK_TO_ESTABLISH ( 500 / portTICK_PERIOD_MS )
40
41 /* Short delay used in several places during the initialisation process. */
42 #define emacSHORT_DELAY                            ( 2 )
43
44 /* Hardware specific bit definitions. */
45 #define emacLINK_ESTABLISHED            ( 0x0020)
46 #define emacFULL_DUPLEX_ENABLED         ( 0x0010 )
47 #define emac10BASE_T_MODE                       ( 0x0004 )
48 #define emacPINSEL2_VALUE                       ( 0x50150105 )
49 #define emacDIV_44                                      ( 0x28 )
50
51 /* If no buffers are available, then wait this long before looking again.... */
52 #define emacBUFFER_WAIT_DELAY   ( 3 / portTICK_PERIOD_MS )
53
54 /* ...and don't look more than this many times. */
55 #define emacBUFFER_WAIT_ATTEMPTS        ( 30 )
56
57 /* Index to the Tx descriptor that is always used first for every Tx.  The second
58 descriptor is then used to re-send in order to speed up the uIP Tx process. */
59 #define emacTX_DESC_INDEX                       ( 0 )
60
61 /*-----------------------------------------------------------*/
62
63 /*
64  * Configure both the Rx and Tx descriptors during the init process.
65  */
66 static void prvInitDescriptors( void );
67
68 /*
69  * Setup the IO and peripherals required for Ethernet communication.
70  */
71 static void prvSetupEMACHardware( void );
72
73 /*
74  * Control the auto negotiate process.
75  */
76 static void prvConfigurePHY( void );
77
78 /*
79  * Wait for a link to be established, then setup the PHY according to the link
80  * parameters.
81  */
82 static long prvSetupLinkStatus( void );
83
84 /*
85  * Search the pool of buffers to find one that is free.  If a buffer is found
86  * mark it as in use before returning its address.
87  */
88 static unsigned char *prvGetNextBuffer( void );
89
90 /*
91  * Return an allocated buffer to the pool of free buffers.
92  */
93 static void prvReturnBuffer( unsigned char *pucBuffer );
94
95 /*
96  * Send lValue to the lPhyReg within the PHY.
97  */
98 static long prvWritePHY( long lPhyReg, long lValue );
99
100 /*
101  * Read a value from ucPhyReg within the PHY.  *plStatus will be set to
102  * pdFALSE if there is an error.
103  */
104 static unsigned short prvReadPHY( unsigned char ucPhyReg, long *plStatus );
105
106 /*-----------------------------------------------------------*/
107
108 /* The semaphore used to wake the uIP task when data arrives. */
109 extern SemaphoreHandle_t xEMACSemaphore;
110
111 /* Each ucBufferInUse index corresponds to a position in the pool of buffers.
112 If the index contains a 1 then the buffer within pool is in use, if it
113 contains a 0 then the buffer is free. */
114 static unsigned char ucBufferInUse[ ETH_NUM_BUFFERS ] = { pdFALSE };
115
116 /* The uip_buffer is not a fixed array, but instead gets pointed to the buffers
117 allocated within this file. */
118 unsigned char * uip_buf;
119
120 /* Store the length of the data being sent so the data can be sent twice.  The
121 value will be set back to 0 once the data has been sent twice. */
122 static unsigned short usSendLen = 0;
123
124 /*-----------------------------------------------------------*/
125
126 long lEMACInit( void )
127 {
128 long lReturn = pdPASS;
129 unsigned long ulID1, ulID2;
130
131         /* Reset peripherals, configure port pins and registers. */
132         prvSetupEMACHardware();
133
134         /* Check the PHY part number is as expected. */
135         ulID1 = prvReadPHY( PHY_REG_IDR1, &lReturn );
136         ulID2 = prvReadPHY( PHY_REG_IDR2, &lReturn );
137         if( ( (ulID1 << 16UL ) | ( ulID2 & 0xFFFFUL ) ) == KS8721_ID )
138         {
139                 /* Set the Ethernet MAC Address registers */
140                 EMAC->SA0 = ( configMAC_ADDR0 << 8 ) | configMAC_ADDR1;
141                 EMAC->SA1 = ( configMAC_ADDR2 << 8 ) | configMAC_ADDR3;
142                 EMAC->SA2 = ( configMAC_ADDR4 << 8 ) | configMAC_ADDR5;
143
144                 /* Initialize Tx and Rx DMA Descriptors */
145                 prvInitDescriptors();
146
147                 /* Receive broadcast and perfect match packets */
148                 EMAC->RxFilterCtrl = RFC_UCAST_EN | RFC_BCAST_EN | RFC_PERFECT_EN;
149
150                 /* Setup the PHY. */
151                 prvConfigurePHY();
152         }
153         else
154         {
155                 lReturn = pdFAIL;
156         }
157
158         /* Check the link status. */
159         if( lReturn == pdPASS )
160         {
161                 lReturn = prvSetupLinkStatus();
162         }
163
164         if( lReturn == pdPASS )
165         {
166                 /* Initialise uip_buf to ensure it points somewhere valid. */
167                 uip_buf = prvGetNextBuffer();
168
169                 /* Reset all interrupts */
170                 EMAC->IntClear = ( INT_RX_OVERRUN | INT_RX_ERR | INT_RX_FIN | INT_RX_DONE | INT_TX_UNDERRUN | INT_TX_ERR | INT_TX_FIN | INT_TX_DONE | INT_SOFT_INT | INT_WAKEUP );
171
172                 /* Enable receive and transmit mode of MAC Ethernet core */
173                 EMAC->Command |= ( CR_RX_EN | CR_TX_EN );
174                 EMAC->MAC1 |= MAC1_REC_EN;
175         }
176
177         return lReturn;
178 }
179 /*-----------------------------------------------------------*/
180
181 static unsigned char *prvGetNextBuffer( void )
182 {
183 long x;
184 unsigned char *pucReturn = NULL;
185 unsigned long ulAttempts = 0;
186
187         while( pucReturn == NULL )
188         {
189                 /* Look through the buffers to find one that is not in use by
190                 anything else. */
191                 for( x = 0; x < ETH_NUM_BUFFERS; x++ )
192                 {
193                         if( ucBufferInUse[ x ] == pdFALSE )
194                         {
195                                 ucBufferInUse[ x ] = pdTRUE;
196                                 pucReturn = ( unsigned char * ) ETH_BUF( x );
197                                 break;
198                         }
199                 }
200
201                 /* Was a buffer found? */
202                 if( pucReturn == NULL )
203                 {
204                         ulAttempts++;
205
206                         if( ulAttempts >= emacBUFFER_WAIT_ATTEMPTS )
207                         {
208                                 break;
209                         }
210
211                         /* Wait then look again. */
212                         vTaskDelay( emacBUFFER_WAIT_DELAY );
213                 }
214         }
215
216         return pucReturn;
217 }
218 /*-----------------------------------------------------------*/
219
220 static void prvInitDescriptors( void )
221 {
222 long x, lNextBuffer = 0;
223
224         for( x = 0; x < NUM_RX_FRAG; x++ )
225         {
226                 /* Allocate the next Ethernet buffer to this descriptor. */
227                 RX_DESC_PACKET( x ) = ETH_BUF( lNextBuffer );
228                 RX_DESC_CTRL( x ) = RCTRL_INT | ( ETH_FRAG_SIZE - 1 );
229                 RX_STAT_INFO( x ) = 0;
230                 RX_STAT_HASHCRC( x ) = 0;
231
232                 /* The Ethernet buffer is now in use. */
233                 ucBufferInUse[ lNextBuffer ] = pdTRUE;
234                 lNextBuffer++;
235         }
236
237         /* Set EMAC Receive Descriptor Registers. */
238         EMAC->RxDescriptor = RX_DESC_BASE;
239         EMAC->RxStatus = RX_STAT_BASE;
240         EMAC->RxDescriptorNumber = NUM_RX_FRAG - 1;
241
242         /* Rx Descriptors Point to 0 */
243         EMAC->RxConsumeIndex = 0;
244
245         /* A buffer is not allocated to the Tx descriptors until they are actually
246         used. */
247         for( x = 0; x < NUM_TX_FRAG; x++ )
248         {
249                 TX_DESC_PACKET( x ) = ( unsigned long ) NULL;
250                 TX_DESC_CTRL( x ) = 0;
251                 TX_STAT_INFO( x ) = 0;
252         }
253
254         /* Set EMAC Transmit Descriptor Registers. */
255         EMAC->TxDescriptor = TX_DESC_BASE;
256         EMAC->TxStatus = TX_STAT_BASE;
257         EMAC->TxDescriptorNumber = NUM_TX_FRAG - 1;
258
259         /* Tx Descriptors Point to 0 */
260         EMAC->TxProduceIndex = 0;
261 }
262 /*-----------------------------------------------------------*/
263
264 static void prvSetupEMACHardware( void )
265 {
266 unsigned short us;
267 long x, lDummy;
268
269         /* Enable P1 Ethernet Pins. */
270         PINCON->PINSEL2 = emacPINSEL2_VALUE;
271         PINCON->PINSEL3 = ( PINCON->PINSEL3 & ~0x0000000F ) | 0x00000005;
272
273         /* Power Up the EMAC controller. */
274         SC->PCONP |= PCONP_PCENET;
275         vTaskDelay( emacSHORT_DELAY );
276
277         /* Reset all EMAC internal modules. */
278         EMAC->MAC1 = MAC1_RES_TX | MAC1_RES_MCS_TX | MAC1_RES_RX | MAC1_RES_MCS_RX | MAC1_SIM_RES | MAC1_SOFT_RES;
279         EMAC->Command = CR_REG_RES | CR_TX_RES | CR_RX_RES | CR_PASS_RUNT_FRM;
280
281         /* A short delay after reset. */
282         vTaskDelay( emacSHORT_DELAY );
283
284         /* Initialize MAC control registers. */
285         EMAC->MAC1 = MAC1_PASS_ALL;
286         EMAC->MAC2 = MAC2_CRC_EN | MAC2_PAD_EN;
287         EMAC->MAXF = ETH_MAX_FLEN;
288         EMAC->CLRT = CLRT_DEF;
289         EMAC->IPGR = IPGR_DEF;
290         EMAC->MCFG = emacDIV_44;
291
292         /* Enable Reduced MII interface. */
293         EMAC->Command = CR_RMII | CR_PASS_RUNT_FRM;
294
295         /* Reset Reduced MII Logic. */
296         EMAC->SUPP = SUPP_RES_RMII;
297         vTaskDelay( emacSHORT_DELAY );
298         EMAC->SUPP = 0;
299
300         /* Put the PHY in reset mode */
301         prvWritePHY( PHY_REG_BMCR, MCFG_RES_MII );
302         prvWritePHY( PHY_REG_BMCR, MCFG_RES_MII );
303
304         /* Wait for hardware reset to end. */
305         for( x = 0; x < 100; x++ )
306         {
307                 vTaskDelay( emacSHORT_DELAY * 5 );
308                 us = prvReadPHY( PHY_REG_BMCR, &lDummy );
309                 if( !( us & MCFG_RES_MII ) )
310                 {
311                         /* Reset complete */
312                         break;
313                 }
314         }
315 }
316 /*-----------------------------------------------------------*/
317
318 static void prvConfigurePHY( void )
319 {
320 unsigned short us;
321 long x, lDummy;
322
323         /* Auto negotiate the configuration. */
324         if( prvWritePHY( PHY_REG_BMCR, PHY_AUTO_NEG ) )
325         {
326                 vTaskDelay( emacSHORT_DELAY * 5 );
327
328                 for( x = 0; x < 10; x++ )
329                 {
330                         us = prvReadPHY( PHY_REG_BMSR, &lDummy );
331
332                         if( us & PHY_AUTO_NEG_COMPLETE )
333                         {
334                                 break;
335                         }
336
337                         vTaskDelay( emacWAIT_FOR_LINK_TO_ESTABLISH );
338                 }
339         }
340 }
341 /*-----------------------------------------------------------*/
342
343 static long prvSetupLinkStatus( void )
344 {
345 long lReturn = pdFAIL, x;
346 unsigned short usLinkStatus;
347
348         /* Wait with timeout for the link to be established. */
349         for( x = 0; x < 10; x++ )
350         {
351                 usLinkStatus = prvReadPHY( PHY_CTRLER, &lReturn );
352                 if( usLinkStatus != 0x00 )
353                 {
354                         /* Link is established. */
355                         lReturn = pdPASS;
356                         break;
357                 }
358
359         vTaskDelay( emacWAIT_FOR_LINK_TO_ESTABLISH );
360         }
361
362         if( lReturn == pdPASS )
363         {
364                 /* Configure Full/Half Duplex mode. */
365                 if( usLinkStatus & emacFULL_DUPLEX_ENABLED )
366                 {
367                         /* Full duplex is enabled. */
368                         EMAC->MAC2 |= MAC2_FULL_DUP;
369                         EMAC->Command |= CR_FULL_DUP;
370                         EMAC->IPGT = IPGT_FULL_DUP;
371                 }
372                 else
373                 {
374                         /* Half duplex mode. */
375                         EMAC->IPGT = IPGT_HALF_DUP;
376                 }
377
378                 /* Configure 100MBit/10MBit mode. */
379                 if( usLinkStatus & emac10BASE_T_MODE )
380                 {
381                         /* 10MBit mode. */
382                         EMAC->SUPP = 0;
383                 }
384                 else
385                 {
386                         /* 100MBit mode. */
387                         EMAC->SUPP = SUPP_SPEED;
388                 }
389         }
390
391         return lReturn;
392 }
393 /*-----------------------------------------------------------*/
394
395 static void prvReturnBuffer( unsigned char *pucBuffer )
396 {
397 unsigned long ul;
398
399         /* Return a buffer to the pool of free buffers. */
400         for( ul = 0; ul < ETH_NUM_BUFFERS; ul++ )
401         {
402                 if( ETH_BUF( ul ) == ( unsigned long ) pucBuffer )
403                 {
404                         ucBufferInUse[ ul ] = pdFALSE;
405                         break;
406                 }
407         }
408 }
409 /*-----------------------------------------------------------*/
410
411 unsigned long ulGetEMACRxData( void )
412 {
413 unsigned long ulLen = 0;
414 long lIndex;
415
416         if( EMAC->RxProduceIndex != EMAC->RxConsumeIndex )
417         {
418                 /* Mark the current buffer as free as uip_buf is going to be set to
419                 the buffer that contains the received data. */
420                 prvReturnBuffer( uip_buf );
421
422                 ulLen = ( RX_STAT_INFO( EMAC->RxConsumeIndex ) & RINFO_SIZE ) - 3;
423                 uip_buf = ( unsigned char * ) RX_DESC_PACKET( EMAC->RxConsumeIndex );
424
425                 /* Allocate a new buffer to the descriptor. */
426         RX_DESC_PACKET( EMAC->RxConsumeIndex ) = ( unsigned long ) prvGetNextBuffer();
427
428                 /* Move the consume index onto the next position, ensuring it wraps to
429                 the beginning at the appropriate place. */
430                 lIndex = EMAC->RxConsumeIndex;
431
432                 lIndex++;
433                 if( lIndex >= NUM_RX_FRAG )
434                 {
435                         lIndex = 0;
436                 }
437
438                 EMAC->RxConsumeIndex = lIndex;
439         }
440
441         return ulLen;
442 }
443 /*-----------------------------------------------------------*/
444
445 void vSendEMACTxData( unsigned short usTxDataLen )
446 {
447 unsigned long ulAttempts = 0UL;
448
449         /* Check to see if the Tx descriptor is free, indicated by its buffer being
450         NULL. */
451         while( TX_DESC_PACKET( emacTX_DESC_INDEX ) != ( unsigned long ) NULL )
452         {
453                 /* Wait for the Tx descriptor to become available. */
454                 vTaskDelay( emacBUFFER_WAIT_DELAY );
455
456                 ulAttempts++;
457                 if( ulAttempts > emacBUFFER_WAIT_ATTEMPTS )
458                 {
459                         /* Something has gone wrong as the Tx descriptor is still in use.
460                         Clear it down manually, the data it was sending will probably be
461                         lost. */
462                         prvReturnBuffer( ( unsigned char * ) TX_DESC_PACKET( emacTX_DESC_INDEX ) );
463                         break;
464                 }
465         }
466
467         /* Setup the Tx descriptor for transmission.  Remember the length of the
468         data being sent so the second descriptor can be used to send it again from
469         within the ISR. */
470         usSendLen = usTxDataLen;
471         TX_DESC_PACKET( emacTX_DESC_INDEX ) = ( unsigned long ) uip_buf;
472         TX_DESC_CTRL( emacTX_DESC_INDEX ) = ( usTxDataLen | TCTRL_LAST | TCTRL_INT );
473         EMAC->TxProduceIndex = ( emacTX_DESC_INDEX + 1 );
474
475         /* uip_buf is being sent by the Tx descriptor.  Allocate a new buffer. */
476         uip_buf = prvGetNextBuffer();
477 }
478 /*-----------------------------------------------------------*/
479
480 static long prvWritePHY( long lPhyReg, long lValue )
481 {
482 const long lMaxTime = 10;
483 long x;
484
485         EMAC->MADR = KS8721_DEF_ADR | lPhyReg;
486         EMAC->MWTD = lValue;
487
488         x = 0;
489         for( x = 0; x < lMaxTime; x++ )
490         {
491                 if( ( EMAC->MIND & MIND_BUSY ) == 0 )
492                 {
493                         /* Operation has finished. */
494                         break;
495                 }
496
497                 vTaskDelay( emacSHORT_DELAY );
498         }
499
500         if( x < lMaxTime )
501         {
502                 return pdPASS;
503         }
504         else
505         {
506                 return pdFAIL;
507         }
508 }
509 /*-----------------------------------------------------------*/
510
511 static unsigned short prvReadPHY( unsigned char ucPhyReg, long *plStatus )
512 {
513 long x;
514 const long lMaxTime = 10;
515
516         EMAC->MADR = KS8721_DEF_ADR | ucPhyReg;
517         EMAC->MCMD = MCMD_READ;
518
519         for( x = 0; x < lMaxTime; x++ )
520         {
521                 /* Operation has finished. */
522                 if( ( EMAC->MIND & MIND_BUSY ) == 0 )
523                 {
524                         break;
525                 }
526
527                 vTaskDelay( emacSHORT_DELAY );
528         }
529
530         EMAC->MCMD = 0;
531
532         if( x >= lMaxTime )
533         {
534                 *plStatus = pdFAIL;
535         }
536
537         return( EMAC->MRDD );
538 }
539 /*-----------------------------------------------------------*/
540
541 void vEMAC_ISR( void )
542 {
543 unsigned long ulStatus;
544 long lHigherPriorityTaskWoken = pdFALSE;
545
546         ulStatus = EMAC->IntStatus;
547
548         /* Clear the interrupt. */
549         EMAC->IntClear = ulStatus;
550
551         if( ulStatus & INT_RX_DONE )
552         {
553                 /* Ensure the uIP task is not blocked as data has arrived. */
554                 xSemaphoreGiveFromISR( xEMACSemaphore, &lHigherPriorityTaskWoken );
555         }
556
557         if( ulStatus & INT_TX_DONE )
558         {
559                 if( usSendLen > 0 )
560                 {
561                         /* Send the data again, using the second descriptor.  As there are
562                         only two descriptors the index is set back to 0. */
563                         TX_DESC_PACKET( ( emacTX_DESC_INDEX + 1 ) ) = TX_DESC_PACKET( emacTX_DESC_INDEX );
564                         TX_DESC_CTRL( ( emacTX_DESC_INDEX + 1 ) ) = ( usSendLen | TCTRL_LAST | TCTRL_INT );
565                         EMAC->TxProduceIndex = ( emacTX_DESC_INDEX );
566
567                         /* This is the second Tx so set usSendLen to 0 to indicate that the
568                         Tx descriptors will be free again. */
569                         usSendLen = 0UL;
570                 }
571                 else
572                 {
573                         /* The Tx buffer is no longer required. */
574                         prvReturnBuffer( ( unsigned char * ) TX_DESC_PACKET( emacTX_DESC_INDEX ) );
575             TX_DESC_PACKET( emacTX_DESC_INDEX ) = ( unsigned long ) NULL;
576                 }
577         }
578
579         portEND_SWITCHING_ISR( lHigherPriorityTaskWoken );
580 }