]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX600_RX62N-RDK_IAR/webserver/EMAC.c
38232162852c2134d5d3c5a8ecd20e1a6d2d1fe5
[freertos] / FreeRTOS / Demo / RX600_RX62N-RDK_IAR / 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 /* Hardware specific includes. */\r
29 #include <iorx62n.h>\r
30 #include "typedefine.h"\r
31 #include "r_ether.h"\r
32 #include "phy.h"\r
33 \r
34 /* FreeRTOS includes. */\r
35 #include "FreeRTOS.h"\r
36 #include "task.h"\r
37 #include "semphr.h"\r
38 \r
39 /* uIP includes. */\r
40 #include "net/uip.h"\r
41 \r
42 /* The time to wait between attempts to obtain a free buffer. */\r
43 #define emacBUFFER_WAIT_DELAY_ms                ( 3 / portTICK_PERIOD_MS )\r
44 \r
45 /* The number of times emacBUFFER_WAIT_DELAY_ms should be waited before giving\r
46 up on attempting to obtain a free buffer all together. */\r
47 #define emacBUFFER_WAIT_ATTEMPTS        ( 30 )\r
48 \r
49 /* The number of Rx descriptors. */\r
50 #define emacNUM_RX_DESCRIPTORS  8\r
51 \r
52 /* The number of Tx descriptors.  When using uIP there is not point in having\r
53 more than two. */\r
54 #define emacNUM_TX_BUFFERS      2\r
55 \r
56 /* The total number of EMAC buffers to allocate. */\r
57 #define emacNUM_BUFFERS         ( emacNUM_RX_DESCRIPTORS + emacNUM_TX_BUFFERS )\r
58 \r
59 /* The time to wait for the Tx descriptor to become free. */\r
60 #define emacTX_WAIT_DELAY_ms ( 10 / portTICK_PERIOD_MS )\r
61 \r
62 /* The total number of times to wait emacTX_WAIT_DELAY_ms for the Tx descriptor to\r
63 become free. */\r
64 #define emacTX_WAIT_ATTEMPTS ( 50 )\r
65 \r
66 /* Only Rx end and Tx end interrupts are used by this driver. */\r
67 #define emacTX_END_INTERRUPT    ( 1UL << 21UL )\r
68 #define emacRX_END_INTERRUPT    ( 1UL << 18UL )\r
69 \r
70 /*-----------------------------------------------------------*/\r
71 \r
72 /* The buffers and descriptors themselves.  */\r
73 #pragma data_alignment=32\r
74 volatile ethfifo xRxDescriptors[ emacNUM_RX_DESCRIPTORS ];\r
75 \r
76 #pragma data_alignment=32\r
77 volatile ethfifo xTxDescriptors[ emacNUM_TX_BUFFERS ];\r
78 \r
79 #pragma data_alignment=32\r
80 char xEthernetBuffers[ emacNUM_BUFFERS ][ UIP_BUFSIZE ];\r
81 \r
82 \r
83 /* Used to indicate which buffers are free and which are in use.  If an index\r
84 contains 0 then the corresponding buffer in xEthernetBuffers is free, otherwise\r
85 the buffer is in use or about to be used. */\r
86 static unsigned char ucBufferInUse[ emacNUM_BUFFERS ];\r
87 \r
88 /*-----------------------------------------------------------*/\r
89 \r
90 /*\r
91  * Initialise both the Rx and Tx descriptors.\r
92  */\r
93 static void prvInitialiseDescriptors( void );\r
94 \r
95 /*\r
96  * Return a pointer to a free buffer within xEthernetBuffers.\r
97  */\r
98 static unsigned char *prvGetNextBuffer( void );\r
99 \r
100 /*\r
101  * Return a buffer to the list of free buffers.\r
102  */\r
103 static void prvReturnBuffer( unsigned char *pucBuffer );\r
104 \r
105 /*\r
106  * Examine the status of the next Rx FIFO to see if it contains new data.\r
107  */\r
108 static unsigned long prvCheckRxFifoStatus( void );\r
109 \r
110 /*\r
111  * Setup the microcontroller for communication with the PHY.\r
112  */\r
113 static void prvResetMAC( void );\r
114 \r
115 /*\r
116  * Configure the Ethernet interface peripherals.\r
117  */\r
118 static void prvConfigureEtherCAndEDMAC( void );\r
119 \r
120 /*\r
121  * Something has gone wrong with the descriptor usage.  Reset all the buffers\r
122  * and descriptors.\r
123  */\r
124 static void prvResetEverything( void );\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128 /* Points to the Rx descriptor currently in use. */\r
129 static volatile ethfifo *pxCurrentRxDesc = NULL;\r
130 \r
131 /* The buffer used by the uIP stack to both receive and send.  This points to\r
132 one of the Ethernet buffers when its actually in use. */\r
133 unsigned char *uip_buf = NULL;\r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 void vInitEmac( void )\r
138 {\r
139         /* Software reset. */\r
140         prvResetMAC();\r
141         \r
142         /* Set the Rx and Tx descriptors into their initial state. */\r
143         prvInitialiseDescriptors();\r
144 \r
145         /* Set the MAC address into the ETHERC */\r
146         ETHERC.MAHR =   ( ( unsigned long ) configMAC_ADDR0 << 24UL ) |\r
147                                         ( ( unsigned long ) configMAC_ADDR1 << 16UL ) |\r
148                                         ( ( unsigned long ) configMAC_ADDR2 << 8UL ) |\r
149                                         ( unsigned long ) configMAC_ADDR3;\r
150                                         \r
151         ETHERC.MALR.BIT.MA = ( ( unsigned long ) configMAC_ADDR4 << 8UL ) |\r
152                                                  ( unsigned long ) configMAC_ADDR5;\r
153 \r
154         /* Perform rest of interface hardware configuration. */\r
155         prvConfigureEtherCAndEDMAC();\r
156         \r
157         /* Nothing received yet, so uip_buf points nowhere. */\r
158         uip_buf = NULL;\r
159 \r
160         /* Initialize the PHY */\r
161         phy_init();\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 void vEMACWrite( void )\r
166 {\r
167 long x;\r
168 \r
169         /* Wait until the second transmission of the last packet has completed. */\r
170         for( x = 0; x < emacTX_WAIT_ATTEMPTS; x++ )\r
171         {\r
172                 if( ( xTxDescriptors[ 1 ].status & ACT ) != 0 )\r
173                 {\r
174                         /* Descriptor is still active. */\r
175                         vTaskDelay( emacTX_WAIT_DELAY_ms );\r
176                 }\r
177                 else\r
178                 {\r
179                         break;\r
180                 }\r
181         }\r
182         \r
183         /* Is the descriptor free after waiting for it? */\r
184         if( ( xTxDescriptors[ 1 ].status & ACT ) != 0 )\r
185         {\r
186                 /* Something has gone wrong. */\r
187                 prvResetEverything();\r
188         }\r
189         \r
190         /* Setup both descriptors to transmit the frame. */\r
191         xTxDescriptors[ 0 ].buf_p = ( char * ) uip_buf;\r
192         xTxDescriptors[ 0 ].bufsize = uip_len;  \r
193         xTxDescriptors[ 1 ].buf_p = ( char * ) uip_buf;\r
194         xTxDescriptors[ 1 ].bufsize = uip_len;\r
195 \r
196         /* uip_buf is being sent by the Tx descriptor.  Allocate a new buffer\r
197         for use by the stack. */\r
198         uip_buf = prvGetNextBuffer();\r
199 \r
200         /* Clear previous settings and go. */\r
201         xTxDescriptors[0].status &= ~( FP1 | FP0 );\r
202         xTxDescriptors[0].status |= ( FP1 | FP0 | ACT );\r
203         xTxDescriptors[1].status &= ~( FP1 | FP0 );\r
204         xTxDescriptors[1].status |= ( FP1 | FP0 | ACT );\r
205 \r
206         EDMAC.EDTRR.LONG = 0x00000001;\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 unsigned long ulEMACRead( void )\r
211 {\r
212 unsigned long ulBytesReceived;\r
213 \r
214         ulBytesReceived = prvCheckRxFifoStatus();\r
215 \r
216         if( ulBytesReceived > 0 )\r
217         {\r
218                 /* Mark the pxDescriptor buffer as free as uip_buf is going to be set to\r
219                 the buffer that contains the received data. */\r
220                 prvReturnBuffer( uip_buf );\r
221 \r
222                 /* Point uip_buf to the data about ot be processed. */\r
223                 uip_buf = ( void * ) pxCurrentRxDesc->buf_p;\r
224                 \r
225                 /* Allocate a new buffer to the descriptor, as uip_buf is now using it's\r
226                 old descriptor. */\r
227                 pxCurrentRxDesc->buf_p = ( char * ) prvGetNextBuffer();\r
228 \r
229                 /* Prepare the descriptor to go again. */\r
230                 pxCurrentRxDesc->status &= ~( FP1 | FP0 );\r
231                 pxCurrentRxDesc->status |= ACT;\r
232 \r
233                 /* Move onto the next buffer in the ring. */\r
234                 pxCurrentRxDesc = pxCurrentRxDesc->next;\r
235                 \r
236                 if( EDMAC.EDRRR.LONG == 0x00000000L )\r
237                 {\r
238                         /* Restart Ethernet if it has stopped */\r
239                         EDMAC.EDRRR.LONG = 0x00000001L;\r
240                 }\r
241         }\r
242 \r
243         return ulBytesReceived;\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 long lEMACWaitForLink( void )\r
248 {\r
249 long lReturn;\r
250 \r
251         /* Set the link status. */\r
252         switch( phy_set_autonegotiate() )\r
253         {\r
254                 /* Half duplex link */\r
255                 case PHY_LINK_100H:\r
256                                                                 ETHERC.ECMR.BIT.DM = 0;\r
257                                                                 ETHERC.ECMR.BIT.RTM = 1;\r
258                                                                 lReturn = pdPASS;\r
259                                                                 break;\r
260 \r
261                 case PHY_LINK_10H:\r
262                                                                 ETHERC.ECMR.BIT.DM = 0;\r
263                                                                 ETHERC.ECMR.BIT.RTM = 0;\r
264                                                                 lReturn = pdPASS;\r
265                                                                 break;\r
266 \r
267 \r
268                 /* Full duplex link */\r
269                 case PHY_LINK_100F:\r
270                                                                 ETHERC.ECMR.BIT.DM = 1;\r
271                                                                 ETHERC.ECMR.BIT.RTM = 1;\r
272                                                                 lReturn = pdPASS;\r
273                                                                 break;\r
274                 \r
275                 case PHY_LINK_10F:\r
276                                                                 ETHERC.ECMR.BIT.DM = 1;\r
277                                                                 ETHERC.ECMR.BIT.RTM = 0;\r
278                                                                 lReturn = pdPASS;\r
279                                                                 break;\r
280 \r
281                 default:\r
282                                                                 lReturn = pdFAIL;\r
283                                                                 break;\r
284         }\r
285 \r
286         if( lReturn == pdPASS )\r
287         {\r
288                 /* Enable receive and transmit. */\r
289                 ETHERC.ECMR.BIT.RE = 1;\r
290                 ETHERC.ECMR.BIT.TE = 1;\r
291 \r
292                 /* Enable EDMAC receive */\r
293                 EDMAC.EDRRR.LONG = 0x1;\r
294         }\r
295         \r
296         return lReturn;\r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 static void prvInitialiseDescriptors( void )\r
301 {\r
302 volatile ethfifo *pxDescriptor;\r
303 long x;\r
304 \r
305         for( x = 0; x < emacNUM_BUFFERS; x++ )\r
306         {\r
307                 /* Ensure none of the buffers are shown as in use at the start. */\r
308                 ucBufferInUse[ x ] = pdFALSE;\r
309         }\r
310 \r
311         /* Initialise the Rx descriptors. */\r
312         for( x = 0; x < emacNUM_RX_DESCRIPTORS; x++ )\r
313         {\r
314                 pxDescriptor = &( xRxDescriptors[ x ] );\r
315                 pxDescriptor->buf_p = &( xEthernetBuffers[ x ][ 0 ] );\r
316 \r
317                 pxDescriptor->bufsize = UIP_BUFSIZE;\r
318                 pxDescriptor->size = 0;\r
319                 pxDescriptor->status = ACT;\r
320                 pxDescriptor->next = ( ethfifo * ) &xRxDescriptors[ x + 1 ];    \r
321                 \r
322                 /* Mark this buffer as in use. */\r
323                 ucBufferInUse[ x ] = pdTRUE;\r
324         }\r
325 \r
326         /* The last descriptor points back to the start. */\r
327         pxDescriptor->status |= DL;\r
328         pxDescriptor->next = ( ethfifo * ) &xRxDescriptors[ 0 ];\r
329         \r
330         /* Initialise the Tx descriptors. */\r
331         for( x = 0; x < emacNUM_TX_BUFFERS; x++ )\r
332         {\r
333                 pxDescriptor = &( xTxDescriptors[ x ] );\r
334                 \r
335                 /* A buffer is not allocated to the Tx descriptor until a send is\r
336                 actually required. */\r
337                 pxDescriptor->buf_p = NULL;\r
338 \r
339                 pxDescriptor->bufsize = UIP_BUFSIZE;\r
340                 pxDescriptor->size = 0;\r
341                 pxDescriptor->status = 0;\r
342                 pxDescriptor->next = ( ethfifo * ) &xTxDescriptors[ x + 1 ];    \r
343         }\r
344 \r
345         /* The last descriptor points back to the start. */\r
346         pxDescriptor->status |= DL;\r
347         pxDescriptor->next = ( ethfifo * ) &( xTxDescriptors[ 0 ] );\r
348         \r
349         /* Use the first Rx descriptor to start with. */\r
350         pxCurrentRxDesc = &( xRxDescriptors[ 0 ] );\r
351 }\r
352 /*-----------------------------------------------------------*/\r
353 \r
354 static unsigned char *prvGetNextBuffer( void )\r
355 {\r
356 long x;\r
357 unsigned char *pucReturn = NULL;\r
358 unsigned long ulAttempts = 0;\r
359 \r
360         while( pucReturn == NULL )\r
361         {\r
362                 /* Look through the buffers to find one that is not in use by\r
363                 anything else. */\r
364                 for( x = 0; x < emacNUM_BUFFERS; x++ )\r
365                 {\r
366                         if( ucBufferInUse[ x ] == pdFALSE )\r
367                         {\r
368                                 ucBufferInUse[ x ] = pdTRUE;\r
369                                 pucReturn = ( unsigned char * ) &( xEthernetBuffers[ x ][ 0 ] );\r
370                                 break;\r
371                         }\r
372                 }\r
373 \r
374                 /* Was a buffer found? */\r
375                 if( pucReturn == NULL )\r
376                 {\r
377                         ulAttempts++;\r
378 \r
379                         if( ulAttempts >= emacBUFFER_WAIT_ATTEMPTS )\r
380                         {\r
381                                 break;\r
382                         }\r
383 \r
384                         /* Wait then look again. */\r
385                         vTaskDelay( emacBUFFER_WAIT_DELAY_ms );\r
386                 }\r
387         }\r
388 \r
389         return pucReturn;\r
390 }\r
391 /*-----------------------------------------------------------*/\r
392 \r
393 static void prvReturnBuffer( unsigned char *pucBuffer )\r
394 {\r
395 unsigned long ul;\r
396 \r
397         /* Return a buffer to the pool of free buffers. */\r
398         for( ul = 0; ul < emacNUM_BUFFERS; ul++ )\r
399         {\r
400                 if( &( xEthernetBuffers[ ul ][ 0 ] ) == ( void * ) pucBuffer )\r
401                 {\r
402                         ucBufferInUse[ ul ] = pdFALSE;\r
403                         break;\r
404                 }\r
405         }\r
406 }\r
407 /*-----------------------------------------------------------*/\r
408 \r
409 static void prvResetEverything( void )\r
410 {\r
411         /* Temporary code just to see if this gets called.  This function has not\r
412         been implemented. */\r
413         portDISABLE_INTERRUPTS();\r
414         for( ;; );\r
415 }\r
416 /*-----------------------------------------------------------*/\r
417 \r
418 static unsigned long prvCheckRxFifoStatus( void )\r
419 {\r
420 unsigned long ulReturn = 0;\r
421 \r
422         if( ( pxCurrentRxDesc->status & ACT ) != 0 )\r
423         {\r
424                 /* Current descriptor is still active. */\r
425         }\r
426         else if( ( pxCurrentRxDesc->status & FE ) != 0 )\r
427         {\r
428                 /* Frame error.  Clear the error. */\r
429                 pxCurrentRxDesc->status &= ~( FP1 | FP0 | FE );\r
430                 pxCurrentRxDesc->status &= ~( RMAF | RRF | RTLF | RTSF | PRE | CERF );\r
431                 pxCurrentRxDesc->status |= ACT;\r
432                 pxCurrentRxDesc = pxCurrentRxDesc->next;\r
433 \r
434                 if( EDMAC.EDRRR.LONG == 0x00000000UL )\r
435                 {\r
436                         /* Restart Ethernet if it has stopped. */\r
437                         EDMAC.EDRRR.LONG = 0x00000001UL;\r
438                 }       \r
439         }\r
440         else\r
441         {\r
442                 /* The descriptor contains a frame.  Because of the size of the buffers\r
443                 the frame should always be complete. */\r
444                 if( ( pxCurrentRxDesc->status & FP0 ) == FP0 )\r
445                 {\r
446                         ulReturn = pxCurrentRxDesc->size;\r
447                 }\r
448                 else\r
449                 {\r
450                         /* Do not expect to get here. */\r
451                         prvResetEverything();\r
452                 }\r
453         }\r
454         \r
455         return ulReturn;\r
456 }\r
457 /*-----------------------------------------------------------*/\r
458 \r
459 static void prvResetMAC( void )\r
460 {\r
461         /* Ensure the EtherC and EDMAC are enabled. */\r
462         SYSTEM.MSTPCRB.BIT.MSTPB15 = 0;\r
463         vTaskDelay( 100 / portTICK_PERIOD_MS );\r
464         \r
465         EDMAC.EDMR.BIT.SWR = 1; \r
466         \r
467         /* Crude wait for reset to complete. */\r
468         vTaskDelay( 500 / portTICK_PERIOD_MS ); \r
469 }\r
470 /*-----------------------------------------------------------*/\r
471 \r
472 static void prvConfigureEtherCAndEDMAC( void )\r
473 {\r
474         /* Initialisation code taken from Renesas example project. */\r
475         \r
476         /* TODO:    Check   bit 5   */\r
477         ETHERC.ECSR.LONG = 0x00000037;                          /* Clear all ETHERC statuS BFR, PSRTO, LCHNG, MPD, ICD */\r
478 \r
479         /* Set the EDMAC interrupt priority. */\r
480         _IPR( _ETHER_EINT ) = configKERNEL_INTERRUPT_PRIORITY;\r
481 \r
482         /* TODO:    Check   bit 5   */\r
483         /* Enable interrupts of interest only. */\r
484         EDMAC.EESIPR.LONG = emacTX_END_INTERRUPT | emacRX_END_INTERRUPT;\r
485         ETHERC.RFLR.LONG = 1518;                                        /* Ether payload is 1500+ CRC */\r
486         ETHERC.IPGR.LONG = 0x00000014;                          /* Intergap is 96-bit time */\r
487 \r
488         /* EDMAC */\r
489         EDMAC.EESR.LONG = 0x47FF0F9F;                           /* Clear all ETHERC and EDMAC status bits */\r
490         #if __LITTLE_ENDIAN__ == 1\r
491                 EDMAC.EDMR.BIT.DE = 1;\r
492         #endif\r
493         EDMAC.RDLAR = ( void * ) pxCurrentRxDesc;       /* Initialaize Rx Descriptor List Address */\r
494         EDMAC.TDLAR = ( void * ) &( xTxDescriptors[ 0 ] );/* Initialaize Tx Descriptor List Address */\r
495         EDMAC.TRSCER.LONG = 0x00000000;                         /* Copy-back status is RFE & TFE only   */\r
496         EDMAC.TFTR.LONG = 0x00000000;                           /* Threshold of Tx_FIFO */\r
497         EDMAC.FDR.LONG = 0x00000000;                            /* Transmit fifo & receive fifo is 256 bytes */\r
498         EDMAC.RMCR.LONG = 0x00000003;                           /* Receive function is normal mode(continued) */\r
499         ETHERC.ECMR.BIT.PRM = 0;                                        /* Ensure promiscuous mode is off. */\r
500         \r
501         /* Enable the interrupt... */\r
502         _IEN( _ETHER_EINT ) = 1;        \r
503 }\r
504 /*-----------------------------------------------------------*/\r
505 \r
506 #pragma vector = VECT_ETHER_EINT\r
507 __interrupt void vEMAC_ISR_Handler( void )\r
508 {\r
509 unsigned long ul = EDMAC.EESR.LONG;\r
510 long lHigherPriorityTaskWoken = pdFALSE;\r
511 extern QueueHandle_t xEMACEventQueue;\r
512 const unsigned long ulRxEvent = uipETHERNET_RX_EVENT;\r
513 \r
514         __enable_interrupt();\r
515 \r
516         /* Has a Tx end occurred? */\r
517         if( ul & emacTX_END_INTERRUPT )\r
518         {\r
519                 /* Only return the buffer to the pool once both Txes have completed. */\r
520                 prvReturnBuffer( ( void * ) xTxDescriptors[ 0 ].buf_p );\r
521                 EDMAC.EESR.LONG = emacTX_END_INTERRUPT;\r
522         }\r
523 \r
524         /* Has an Rx end occurred? */\r
525         if( ul & emacRX_END_INTERRUPT )\r
526         {\r
527                 /* Make sure the Ethernet task is not blocked waiting for a packet. */\r
528                 xQueueSendFromISR( xEMACEventQueue, &ulRxEvent, &lHigherPriorityTaskWoken );\r
529                 portYIELD_FROM_ISR( lHigherPriorityTaskWoken );\r
530                 EDMAC.EESR.LONG = emacRX_END_INTERRUPT;\r
531         }\r
532 }\r
533 \r