xmc4700_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file xmc4700_eth_driver.c
3  * @brief Infineon XMC4700 Ethernet MAC driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include "xmc4700.h"
36 #include "core/net.h"
38 #include "debug.h"
39 
40 //Underlying network interface
41 static NetInterface *nicDriverInterface;
42 
43 //IAR EWARM compiler?
44 #if defined(__ICCARM__)
45 
46 //Transmit buffer
47 #pragma data_alignment = 4
48 #pragma location = XMC4700_ETH_RAM_SECTION
50 //Receive buffer
51 #pragma data_alignment = 4
52 #pragma location = XMC4700_ETH_RAM_SECTION
54 //Transmit DMA descriptors
55 #pragma data_alignment = 4
56 #pragma location = XMC4700_ETH_RAM_SECTION
58 //Receive DMA descriptors
59 #pragma data_alignment = 4
60 #pragma location = XMC4700_ETH_RAM_SECTION
62 
63 //Keil MDK-ARM or GCC compiler?
64 #else
65 
66 //Transmit buffer
68  __attribute__((aligned(4), __section__(XMC4700_ETH_RAM_SECTION)));
69 //Receive buffer
71  __attribute__((aligned(4), __section__(XMC4700_ETH_RAM_SECTION)));
72 //Transmit DMA descriptors
74  __attribute__((aligned(4), __section__(XMC4700_ETH_RAM_SECTION)));
75 //Receive DMA descriptors
77  __attribute__((aligned(4), __section__(XMC4700_ETH_RAM_SECTION)));
78 
79 #endif
80 
81 //Pointer to the current TX DMA descriptor
82 static Xmc4700TxDmaDesc *txCurDmaDesc;
83 //Pointer to the current RX DMA descriptor
84 static Xmc4700RxDmaDesc *rxCurDmaDesc;
85 
86 
87 /**
88  * @brief XMC4700 Ethernet MAC driver
89  **/
90 
92 {
94  ETH_MTU,
105  TRUE,
106  TRUE,
107  TRUE,
108  FALSE
109 };
110 
111 
112 /**
113  * @brief XMC4700 Ethernet MAC initialization
114  * @param[in] interface Underlying network interface
115  * @return Error code
116  **/
117 
119 {
120  error_t error;
121 
122  //Debug message
123  TRACE_INFO("Initializing XMC4700 Ethernet MAC...\r\n");
124 
125  //Save underlying network interface
126  nicDriverInterface = interface;
127 
128  //Disable parity error trap
129  SCU_PARITY->PETE = 0;
130  //Disable unaligned access trap
131  PPB->CCR &= ~PPB_CCR_UNALIGN_TRP_Msk;
132 
133  //Enable ETH0 peripheral clock
134  SCU_CLK->CLKSET = SCU_CLK_CLKSET_ETH0CEN_Msk;
135 
136  //GPIO configuration
137  xmc4700EthInitGpio(interface);
138 
139  //Reset ETH0 peripheral
140  SCU_RESET->PRSET2 = SCU_RESET_PRSET2_ETH0RS_Msk;
141  SCU_RESET->PRCLR2 = SCU_RESET_PRCLR2_ETH0RS_Msk;
142 
143  //Reset DMA controller
144  ETH0->BUS_MODE |= ETH_BUS_MODE_SWR_Msk;
145  //Wait for the reset to complete
146  while((ETH0->BUS_MODE & ETH_BUS_MODE_SWR_Msk) != 0)
147  {
148  }
149 
150  //Adjust MDC clock range depending on ETH clock frequency
151  ETH0->GMII_ADDRESS = ETH_GMII_ADDRESS_CR_DIV62;
152 
153  //Valid Ethernet PHY or switch driver?
154  if(interface->phyDriver != NULL)
155  {
156  //Ethernet PHY initialization
157  error = interface->phyDriver->init(interface);
158  }
159  else if(interface->switchDriver != NULL)
160  {
161  //Ethernet switch initialization
162  error = interface->switchDriver->init(interface);
163  }
164  else
165  {
166  //The interface is not properly configured
167  error = ERROR_FAILURE;
168  }
169 
170  //Any error to report?
171  if(error)
172  {
173  return error;
174  }
175 
176  //Use default MAC configuration
177  ETH0->MAC_CONFIGURATION = ETH_MAC_CONFIGURATION_RESERVED15_Msk |
178  ETH_MAC_CONFIGURATION_DO_Msk;
179 
180  //Configure MAC address filtering
182 
183  //Disable flow control
184  ETH0->FLOW_CONTROL = 0;
185  //Enable store and forward mode
186  ETH0->OPERATION_MODE = ETH_OPERATION_MODE_RSF_Msk | ETH_OPERATION_MODE_TSF_Msk;
187 
188  //Configure DMA bus mode
189  ETH0->BUS_MODE = ETH_BUS_MODE_AAL_Msk | ETH_BUS_MODE_USP_Msk |
191 
192  //Initialize DMA descriptor lists
193  xmc4700EthInitDmaDesc(interface);
194 
195  //Prevent interrupts from being generated when statistic counters reach
196  //half their maximum value
197  ETH0->MMC_TRANSMIT_INTERRUPT_MASK = 0xFFFFFFFF;
198  ETH0->MMC_RECEIVE_INTERRUPT_MASK = 0xFFFFFFFF;
199  ETH0->MMC_IPC_RECEIVE_INTERRUPT_MASK = 0xFFFFFFFF;
200 
201  //Disable MAC interrupts
202  ETH0->INTERRUPT_MASK = ETH_INTERRUPT_MASK_TSIM_Msk | ETH_INTERRUPT_MASK_PMTIM_Msk;
203 
204  //Enable the desired DMA interrupts
205  ETH0->INTERRUPT_ENABLE = ETH_INTERRUPT_ENABLE_NIE_Msk |
206  ETH_INTERRUPT_ENABLE_RIE_Msk | ETH_INTERRUPT_ENABLE_TIE_Msk;
207 
208  //Set priority grouping (6 bits for pre-emption priority, no bits for subpriority)
209  NVIC_SetPriorityGrouping(XMC4700_ETH_IRQ_PRIORITY_GROUPING);
210 
211  //Configure Ethernet interrupt priority
212  NVIC_SetPriority(ETH0_0_IRQn, NVIC_EncodePriority(XMC4700_ETH_IRQ_PRIORITY_GROUPING,
214 
215  //Enable MAC transmission and reception
216  ETH0->MAC_CONFIGURATION |= ETH_MAC_CONFIGURATION_TE_Msk | ETH_MAC_CONFIGURATION_RE_Msk;
217  //Enable DMA transmission and reception
218  ETH0->OPERATION_MODE |= ETH_OPERATION_MODE_ST_Msk | ETH_OPERATION_MODE_SR_Msk;
219 
220  //Accept any packets from the upper layer
221  osSetEvent(&interface->nicTxEvent);
222 
223  //Successful initialization
224  return NO_ERROR;
225 }
226 
227 
228 /**
229  * @brief GPIO configuration
230  * @param[in] interface Underlying network interface
231  **/
232 
233 __weak_func void xmc4700EthInitGpio(NetInterface *interface)
234 {
235 //XMC4700 Relax Kit?
236 #if defined(USE_KIT_XMC4700_RELAX)
237  uint32_t temp;
238 
239  //Configure ETH0.MDIO (P2.0), ETH0.RXD0A (P2.2) and ETH0.RXD1A (P2.3)
240  temp = PORT2->IOCR0;
241  temp &= ~(PORT2_IOCR0_PC0_Msk | PORT2_IOCR0_PC2_Msk | PORT2_IOCR0_PC3_Msk);
242  temp |= (0UL << PORT2_IOCR0_PC0_Pos) | (0UL << PORT2_IOCR0_PC2_Pos) | (0UL << PORT2_IOCR0_PC3_Pos);
243  PORT2->IOCR0 = temp;
244 
245  //Configure ETH0.RXERA (P2.4), ETH0.TX_EN (P2.5) and ETH0.MDC (P2.7)
246  temp = PORT2->IOCR4;
247  temp &= ~(PORT2_IOCR4_PC4_Msk | PORT2_IOCR4_PC5_Msk | PORT2_IOCR4_PC7_Msk);
248  temp |= (0UL << PORT2_IOCR4_PC4_Pos) | (17UL << PORT2_IOCR4_PC5_Pos) | (17UL << PORT2_IOCR4_PC7_Pos);
249  PORT2->IOCR4 = temp;
250 
251  //Configure ETH0.TXD0 (P2.8) and ETH0.TXD1 (P2.9)
252  temp = PORT2->IOCR8;
253  temp &= ~(PORT2_IOCR8_PC8_Msk | PORT2_IOCR8_PC9_Msk);
254  temp |= (17UL << PORT2_IOCR8_PC8_Pos) | (17UL << PORT2_IOCR8_PC9_Pos);
255  PORT2->IOCR8 = temp;
256 
257  //Configure ETH0.CLK_RMIIC (P15.8) and ETH0.CRS_DVC (P15.9)
258  temp = PORT15->IOCR8;
259  temp &= ~(PORT15_IOCR8_PC8_Msk | PORT15_IOCR8_PC9_Msk);
260  temp |= (0UL << PORT15_IOCR8_PC8_Pos) | (0UL << PORT15_IOCR8_PC9_Pos);
261  PORT15->IOCR8 = temp;
262 
263  //Assign ETH_MDIO (P2.0) to HW0
264  temp = PORT2->HWSEL & ~PORT2_HWSEL_HW0_Msk;
265  PORT2->HWSEL = temp | (1UL << PORT2_HWSEL_HW0_Pos);
266 
267  //Select output driver strength for ETH0.TX_EN (P2.5)
268  temp = PORT2->PDR0;
269  temp &= ~PORT2_PDR0_PD5_Msk;
270  temp |= (0UL << PORT2_PDR0_PD5_Pos);
271  PORT2->PDR0 = temp;
272 
273  //Select output driver strength for ETH0.TXD0 (P2.8) and ETH0.TXD1 (P2.9)
274  temp = PORT2->PDR1;
275  temp &= ~(PORT2_PDR1_PD8_Msk | PORT2_PDR1_PD9_Msk);
276  temp |= (0UL << PORT2_PDR1_PD8_Pos) | (0UL << PORT2_PDR1_PD9_Pos);
277  PORT2->PDR1 = temp;
278 
279  //Use ETH0.CLK_RMIIC (P15.8) and ETH0.CRS_DVC (P15.9) as digital inputs
280  PORT15->PDISC &= ~(PORT15_PDISC_PDIS8_Msk | PORT15_PDISC_PDIS9_Msk);
281 
282  //Select RMII operation mode
283  ETH0_CON->CON = ETH_CON_INFSEL_Msk | ETH_CON_MDIO_B | ETH_CON_RXER_A |
285 #endif
286 }
287 
288 
289 /**
290  * @brief Initialize DMA descriptor lists
291  * @param[in] interface Underlying network interface
292  **/
293 
295 {
296  uint_t i;
297 
298  //Initialize TX DMA descriptor list
299  for(i = 0; i < XMC4700_ETH_TX_BUFFER_COUNT; i++)
300  {
301  //Use chain structure rather than ring structure
302  txDmaDesc[i].tdes0 = ETH_TDES0_IC | ETH_TDES0_TCH;
303  //Initialize transmit buffer size
304  txDmaDesc[i].tdes1 = 0;
305  //Transmit buffer address
306  txDmaDesc[i].tdes2 = (uint32_t) txBuffer[i];
307  //Next descriptor address
308  txDmaDesc[i].tdes3 = (uint32_t) &txDmaDesc[i + 1];
309  }
310 
311  //The last descriptor is chained to the first entry
312  txDmaDesc[i - 1].tdes3 = (uint32_t) &txDmaDesc[0];
313  //Point to the very first descriptor
314  txCurDmaDesc = &txDmaDesc[0];
315 
316  //Initialize RX DMA descriptor list
317  for(i = 0; i < XMC4700_ETH_RX_BUFFER_COUNT; i++)
318  {
319  //The descriptor is initially owned by the DMA
320  rxDmaDesc[i].rdes0 = ETH_RDES0_OWN;
321  //Use chain structure rather than ring structure
323  //Receive buffer address
324  rxDmaDesc[i].rdes2 = (uint32_t) rxBuffer[i];
325  //Next descriptor address
326  rxDmaDesc[i].rdes3 = (uint32_t) &rxDmaDesc[i + 1];
327  }
328 
329  //The last descriptor is chained to the first entry
330  rxDmaDesc[i - 1].rdes3 = (uint32_t) &rxDmaDesc[0];
331  //Point to the very first descriptor
332  rxCurDmaDesc = &rxDmaDesc[0];
333 
334  //Start location of the TX descriptor list
335  ETH0->TRANSMIT_DESCRIPTOR_LIST_ADDRESS = (uint32_t) txDmaDesc;
336  //Start location of the RX descriptor list
337  ETH0->RECEIVE_DESCRIPTOR_LIST_ADDRESS = (uint32_t) rxDmaDesc;
338 }
339 
340 
341 /**
342  * @brief XMC4700 Ethernet MAC timer handler
343  *
344  * This routine is periodically called by the TCP/IP stack to handle periodic
345  * operations such as polling the link state
346  *
347  * @param[in] interface Underlying network interface
348  **/
349 
350 void xmc4700EthTick(NetInterface *interface)
351 {
352  //Valid Ethernet PHY or switch driver?
353  if(interface->phyDriver != NULL)
354  {
355  //Handle periodic operations
356  interface->phyDriver->tick(interface);
357  }
358  else if(interface->switchDriver != NULL)
359  {
360  //Handle periodic operations
361  interface->switchDriver->tick(interface);
362  }
363  else
364  {
365  //Just for sanity
366  }
367 }
368 
369 
370 /**
371  * @brief Enable interrupts
372  * @param[in] interface Underlying network interface
373  **/
374 
376 {
377  //Enable Ethernet MAC interrupts
378  NVIC_EnableIRQ(ETH0_0_IRQn);
379 
380  //Valid Ethernet PHY or switch driver?
381  if(interface->phyDriver != NULL)
382  {
383  //Enable Ethernet PHY interrupts
384  interface->phyDriver->enableIrq(interface);
385  }
386  else if(interface->switchDriver != NULL)
387  {
388  //Enable Ethernet switch interrupts
389  interface->switchDriver->enableIrq(interface);
390  }
391  else
392  {
393  //Just for sanity
394  }
395 }
396 
397 
398 /**
399  * @brief Disable interrupts
400  * @param[in] interface Underlying network interface
401  **/
402 
404 {
405  //Disable Ethernet MAC interrupts
406  NVIC_DisableIRQ(ETH0_0_IRQn);
407 
408  //Valid Ethernet PHY or switch driver?
409  if(interface->phyDriver != NULL)
410  {
411  //Disable Ethernet PHY interrupts
412  interface->phyDriver->disableIrq(interface);
413  }
414  else if(interface->switchDriver != NULL)
415  {
416  //Disable Ethernet switch interrupts
417  interface->switchDriver->disableIrq(interface);
418  }
419  else
420  {
421  //Just for sanity
422  }
423 }
424 
425 
426 /**
427  * @brief XMC4700 Ethernet MAC interrupt service routine
428  **/
429 
431 {
432  bool_t flag;
433  uint32_t status;
434 
435  //Interrupt service routine prologue
436  osEnterIsr();
437 
438  //This flag will be set if a higher priority task must be woken
439  flag = FALSE;
440 
441  //Read DMA status register
442  status = ETH0->STATUS;
443 
444  //Packet transmitted?
445  if((status & ETH_STATUS_TI_Msk) != 0)
446  {
447  //Clear TI interrupt flag
448  ETH0->STATUS = ETH_STATUS_TI_Msk;
449 
450  //Check whether the TX buffer is available for writing
451  if((txCurDmaDesc->tdes0 & ETH_TDES0_OWN) == 0)
452  {
453  //Notify the TCP/IP stack that the transmitter is ready to send
454  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
455  }
456  }
457 
458  //Packet received?
459  if((status & ETH_STATUS_RI_Msk) != 0)
460  {
461  //Clear RI interrupt flag
462  ETH0->STATUS = ETH_STATUS_RI_Msk;
463 
464  //Set event flag
465  nicDriverInterface->nicEvent = TRUE;
466  //Notify the TCP/IP stack of the event
467  flag |= osSetEventFromIsr(&nicDriverInterface->netContext->event);
468  }
469 
470  //Clear NIS interrupt flag
471  ETH0->STATUS = ETH_STATUS_NIS_Msk;
472 
473  //Interrupt service routine epilogue
474  osExitIsr(flag);
475 }
476 
477 
478 /**
479  * @brief XMC4700 Ethernet MAC event handler
480  * @param[in] interface Underlying network interface
481  **/
482 
484 {
485  error_t error;
486 
487  //Process all pending packets
488  do
489  {
490  //Read incoming packet
491  error = xmc4700EthReceivePacket(interface);
492 
493  //No more data in the receive buffer?
494  } while(error != ERROR_BUFFER_EMPTY);
495 }
496 
497 
498 /**
499  * @brief Send a packet
500  * @param[in] interface Underlying network interface
501  * @param[in] buffer Multi-part buffer containing the data to send
502  * @param[in] offset Offset to the first data byte
503  * @param[in] ancillary Additional options passed to the stack along with
504  * the packet
505  * @return Error code
506  **/
507 
509  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
510 {
511  size_t length;
512 
513  //Retrieve the length of the packet
514  length = netBufferGetLength(buffer) - offset;
515 
516  //Check the frame length
518  {
519  //The transmitter can accept another packet
520  osSetEvent(&interface->nicTxEvent);
521  //Report an error
522  return ERROR_INVALID_LENGTH;
523  }
524 
525  //Make sure the current buffer is available for writing
526  if((txCurDmaDesc->tdes0 & ETH_TDES0_OWN) != 0)
527  {
528  return ERROR_FAILURE;
529  }
530 
531  //Copy user data to the transmit buffer
532  netBufferRead((uint8_t *) txCurDmaDesc->tdes2, buffer, offset, length);
533 
534  //Write the number of bytes to send
535  txCurDmaDesc->tdes1 = length & ETH_TDES1_TBS1;
536  //Set LS and FS flags as the data fits in a single buffer
537  txCurDmaDesc->tdes0 |= ETH_TDES0_LS | ETH_TDES0_FS;
538  //Give the ownership of the descriptor to the DMA
539  txCurDmaDesc->tdes0 |= ETH_TDES0_OWN;
540 
541  //Clear TU flag to resume processing
542  ETH0->STATUS = ETH_STATUS_TU_Msk;
543  //Instruct the DMA to poll the transmit descriptor list
544  ETH0->TRANSMIT_POLL_DEMAND = 0;
545 
546  //Point to the next descriptor in the list
547  txCurDmaDesc = (Xmc4700TxDmaDesc *) txCurDmaDesc->tdes3;
548 
549  //Check whether the next buffer is available for writing
550  if((txCurDmaDesc->tdes0 & ETH_TDES0_OWN) == 0)
551  {
552  //The transmitter can accept another packet
553  osSetEvent(&interface->nicTxEvent);
554  }
555 
556  //Data successfully written
557  return NO_ERROR;
558 }
559 
560 
561 /**
562  * @brief Receive a packet
563  * @param[in] interface Underlying network interface
564  * @return Error code
565  **/
566 
568 {
569  error_t error;
570  size_t n;
571  NetRxAncillary ancillary;
572 
573  //Current buffer available for reading?
574  if((rxCurDmaDesc->rdes0 & ETH_RDES0_OWN) == 0)
575  {
576  //FS and LS flags should be set
577  if((rxCurDmaDesc->rdes0 & ETH_RDES0_FS) != 0 &&
578  (rxCurDmaDesc->rdes0 & ETH_RDES0_LS) != 0)
579  {
580  //Make sure no error occurred
581  if((rxCurDmaDesc->rdes0 & ETH_RDES0_ES) == 0)
582  {
583  //Retrieve the length of the frame
584  n = (rxCurDmaDesc->rdes0 & ETH_RDES0_FL) >> 16;
585  //Limit the number of data to read
587 
588  //Additional options can be passed to the stack along with the packet
589  ancillary = NET_DEFAULT_RX_ANCILLARY;
590 
591  //Pass the packet to the upper layer
592  nicProcessPacket(interface, (uint8_t *) rxCurDmaDesc->rdes2, n,
593  &ancillary);
594 
595  //Valid packet received
596  error = NO_ERROR;
597  }
598  else
599  {
600  //The received packet contains an error
601  error = ERROR_INVALID_PACKET;
602  }
603  }
604  else
605  {
606  //The packet is not valid
607  error = ERROR_INVALID_PACKET;
608  }
609 
610  //Give the ownership of the descriptor back to the DMA
611  rxCurDmaDesc->rdes0 = ETH_RDES0_OWN;
612  //Point to the next descriptor in the list
613  rxCurDmaDesc = (Xmc4700RxDmaDesc *) rxCurDmaDesc->rdes3;
614  }
615  else
616  {
617  //No more data in the receive buffer
618  error = ERROR_BUFFER_EMPTY;
619  }
620 
621  //Clear RU flag to resume processing
622  ETH0->STATUS = ETH_STATUS_RU_Msk;
623  //Instruct the DMA to poll the receive descriptor list
624  ETH0->RECEIVE_POLL_DEMAND = 0;
625 
626  //Return status code
627  return error;
628 }
629 
630 
631 /**
632  * @brief Configure MAC address filtering
633  * @param[in] interface Underlying network interface
634  * @return Error code
635  **/
636 
638 {
639  uint_t i;
640  uint_t j;
641  uint_t k;
642  uint32_t crc;
643  uint32_t hashTable[2];
644  MacAddr unicastMacAddr[3];
645  MacFilterEntry *entry;
646 
647  //Debug message
648  TRACE_DEBUG("Updating MAC filter...\r\n");
649 
650  //Promiscuous mode?
651  if(interface->promiscuous)
652  {
653  //Pass all incoming frames regardless of their destination address
654  ETH0->MAC_FRAME_FILTER = ETH_MAC_FRAME_FILTER_PR_Msk;
655  }
656  else
657  {
658  //Set the MAC address of the station
659  ETH0->MAC_ADDRESS0_LOW = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
660  ETH0->MAC_ADDRESS0_HIGH = interface->macAddr.w[2];
661 
662  //The MAC supports 3 additional addresses for unicast perfect filtering
663  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
664  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
665  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
666 
667  //The hash table is used for multicast address filtering
668  hashTable[0] = 0;
669  hashTable[1] = 0;
670 
671  //The MAC address filter contains the list of MAC addresses to accept
672  //when receiving an Ethernet frame
673  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
674  {
675  //Point to the current entry
676  entry = &interface->macAddrFilter[i];
677 
678  //Valid entry?
679  if(entry->refCount > 0)
680  {
681  //Multicast address?
682  if(macIsMulticastAddr(&entry->addr))
683  {
684  //Compute CRC over the current MAC address
685  crc = xmc4700EthCalcCrc(&entry->addr, sizeof(MacAddr));
686 
687  //The upper 6 bits in the CRC register are used to index the
688  //contents of the hash table
689  k = (crc >> 26) & 0x3F;
690 
691  //Update hash table contents
692  hashTable[k / 32] |= (1 << (k % 32));
693  }
694  else
695  {
696  //Up to 3 additional MAC addresses can be specified
697  if(j < 3)
698  {
699  //Save the unicast address
700  unicastMacAddr[j++] = entry->addr;
701  }
702  }
703  }
704  }
705 
706  //Configure the first unicast address filter
707  if(j >= 1)
708  {
709  //When the AE bit is set, the entry is used for perfect filtering
710  ETH0->MAC_ADDRESS1_LOW = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
711  ETH0->MAC_ADDRESS1_HIGH = unicastMacAddr[0].w[2] | ETH_MAC_ADDRESS1_HIGH_AE_Msk;
712  }
713  else
714  {
715  //When the AE bit is cleared, the entry is ignored
716  ETH0->MAC_ADDRESS1_LOW = 0;
717  ETH0->MAC_ADDRESS1_HIGH = 0;
718  }
719 
720  //Configure the second unicast address filter
721  if(j >= 2)
722  {
723  //When the AE bit is set, the entry is used for perfect filtering
724  ETH0->MAC_ADDRESS2_LOW = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
725  ETH0->MAC_ADDRESS2_HIGH = unicastMacAddr[1].w[2] | ETH_MAC_ADDRESS2_HIGH_AE_Msk;
726  }
727  else
728  {
729  //When the AE bit is cleared, the entry is ignored
730  ETH0->MAC_ADDRESS2_LOW = 0;
731  ETH0->MAC_ADDRESS2_HIGH = 0;
732  }
733 
734  //Configure the third unicast address filter
735  if(j >= 3)
736  {
737  //When the AE bit is set, the entry is used for perfect filtering
738  ETH0->MAC_ADDRESS3_LOW = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
739  ETH0->MAC_ADDRESS3_HIGH = unicastMacAddr[2].w[2] | ETH_MAC_ADDRESS3_HIGH_AE_Msk;
740  }
741  else
742  {
743  //When the AE bit is cleared, the entry is ignored
744  ETH0->MAC_ADDRESS3_LOW = 0;
745  ETH0->MAC_ADDRESS3_HIGH = 0;
746  }
747 
748  //Check whether frames with a multicast destination address should be
749  //accepted
750  if(interface->acceptAllMulticast)
751  {
752  //Configure the receive filter
753  ETH0->MAC_FRAME_FILTER = ETH_MAC_FRAME_FILTER_HPF_Msk |
754  ETH_MAC_FRAME_FILTER_PM_Msk;
755  }
756  else
757  {
758  //Configure the receive filter
759  ETH0->MAC_FRAME_FILTER = ETH_MAC_FRAME_FILTER_HPF_Msk |
760  ETH_MAC_FRAME_FILTER_HMC_Msk;
761 
762  //Configure the multicast hash table
763  ETH0->HASH_TABLE_LOW = hashTable[0];
764  ETH0->HASH_TABLE_HIGH = hashTable[1];
765 
766  //Debug message
767  TRACE_DEBUG(" HASH_TABLE_LOW = 0x%08" PRIX32 "\r\n", ETH0->HASH_TABLE_LOW);
768  TRACE_DEBUG(" HASH_TABLE_HIGH = 0x%08" PRIX32 "\r\n", ETH0->HASH_TABLE_HIGH);
769  }
770  }
771 
772  //Successful processing
773  return NO_ERROR;
774 }
775 
776 
777 /**
778  * @brief Adjust MAC configuration parameters for proper operation
779  * @param[in] interface Underlying network interface
780  * @return Error code
781  **/
782 
784 {
785  uint32_t config;
786 
787  //Read current MAC configuration
788  config = ETH0->MAC_CONFIGURATION;
789 
790  //10BASE-T or 100BASE-TX operation mode?
791  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
792  {
793  config |= ETH_MAC_CONFIGURATION_FES_Msk;
794  }
795  else
796  {
797  config &= ~ETH_MAC_CONFIGURATION_FES_Msk;
798  }
799 
800  //Half-duplex or full-duplex mode?
801  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
802  {
803  config |= ETH_MAC_CONFIGURATION_DM_Msk;
804  }
805  else
806  {
807  config &= ~ETH_MAC_CONFIGURATION_DM_Msk;
808  }
809 
810  //Update MAC configuration register
811  ETH0->MAC_CONFIGURATION = config;
812 
813  //Successful processing
814  return NO_ERROR;
815 }
816 
817 
818 /**
819  * @brief Write PHY register
820  * @param[in] opcode Access type (2 bits)
821  * @param[in] phyAddr PHY address (5 bits)
822  * @param[in] regAddr Register address (5 bits)
823  * @param[in] data Register value
824  **/
825 
826 void xmc4700EthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
827  uint8_t regAddr, uint16_t data)
828 {
829  uint32_t temp;
830 
831  //Valid opcode?
832  if(opcode == SMI_OPCODE_WRITE)
833  {
834  //Take care not to alter MDC clock configuration
835  temp = ETH0->GMII_ADDRESS & ETH_GMII_ADDRESS_CR_Msk;
836  //Set up a write operation
837  temp |= ETH_GMII_ADDRESS_MW_Msk | ETH_GMII_ADDRESS_MB_Msk;
838  //PHY address
839  temp |= (phyAddr << ETH_GMII_ADDRESS_PA_Pos) & ETH_GMII_ADDRESS_PA_Msk;
840  //Register address
841  temp |= (regAddr << ETH_GMII_ADDRESS_MR_Pos) & ETH_GMII_ADDRESS_MR_Msk;
842 
843  //Data to be written in the PHY register
844  ETH0->GMII_DATA = data & ETH_GMII_DATA_MD_Msk;
845 
846  //Start a write operation
847  ETH0->GMII_ADDRESS = temp;
848  //Wait for the write to complete
849  while((ETH0->GMII_ADDRESS & ETH_GMII_ADDRESS_MB_Msk) != 0)
850  {
851  }
852  }
853  else
854  {
855  //The MAC peripheral only supports standard Clause 22 opcodes
856  }
857 }
858 
859 
860 /**
861  * @brief Read PHY register
862  * @param[in] opcode Access type (2 bits)
863  * @param[in] phyAddr PHY address (5 bits)
864  * @param[in] regAddr Register address (5 bits)
865  * @return Register value
866  **/
867 
868 uint16_t xmc4700EthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
869  uint8_t regAddr)
870 {
871  uint16_t data;
872  uint32_t temp;
873 
874  //Valid opcode?
875  if(opcode == SMI_OPCODE_READ)
876  {
877  //Take care not to alter MDC clock configuration
878  temp = ETH0->GMII_ADDRESS & ETH_GMII_ADDRESS_CR_Msk;
879  //Set up a read operation
880  temp |= ETH_GMII_ADDRESS_MB_Msk;
881  //PHY address
882  temp |= (phyAddr << ETH_GMII_ADDRESS_PA_Pos) & ETH_GMII_ADDRESS_PA_Msk;
883  //Register address
884  temp |= (regAddr << ETH_GMII_ADDRESS_MR_Pos) & ETH_GMII_ADDRESS_MR_Msk;
885 
886  //Start a read operation
887  ETH0->GMII_ADDRESS = temp;
888  //Wait for the read to complete
889  while((ETH0->GMII_ADDRESS & ETH_GMII_ADDRESS_MB_Msk) != 0)
890  {
891  }
892 
893  //Get register value
894  data = ETH0->GMII_DATA & ETH_GMII_DATA_MD_Msk;
895  }
896  else
897  {
898  //The MAC peripheral only supports standard Clause 22 opcodes
899  data = 0;
900  }
901 
902  //Return the value of the PHY register
903  return data;
904 }
905 
906 
907 /**
908  * @brief CRC calculation
909  * @param[in] data Pointer to the data over which to calculate the CRC
910  * @param[in] length Number of bytes to process
911  * @return Resulting CRC value
912  **/
913 
914 uint32_t xmc4700EthCalcCrc(const void *data, size_t length)
915 {
916  uint_t i;
917  uint_t j;
918  uint32_t crc;
919  const uint8_t *p;
920 
921  //Point to the data over which to calculate the CRC
922  p = (uint8_t *) data;
923  //CRC preset value
924  crc = 0xFFFFFFFF;
925 
926  //Loop through data
927  for(i = 0; i < length; i++)
928  {
929  //The message is processed bit by bit
930  for(j = 0; j < 8; j++)
931  {
932  //Update CRC value
933  if((((crc >> 31) ^ (p[i] >> j)) & 0x01) != 0)
934  {
935  crc = (crc << 1) ^ 0x04C11DB7;
936  }
937  else
938  {
939  crc = crc << 1;
940  }
941  }
942  }
943 
944  //Return CRC value
945  return ~crc;
946 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
#define ETH_BUS_MODE_PR_1_1
uint8_t opcode
Definition: dns_common.h:191
int bool_t
Definition: compiler_port.h:63
#define ETH_CON_MDIO_B
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
size_t netBufferRead(void *dest, const NetBuffer *src, size_t srcOffset, size_t length)
Read data from a multi-part buffer.
Definition: net_mem.c:690
#define XMC4700_ETH_IRQ_SUB_PRIORITY
uint8_t p
Definition: ndp.h:300
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
#define ETH_TDES1_TBS1
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:224
uint16_t xmc4700EthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define ETH_CON_RXER_A
#define XMC4700_ETH_RAM_SECTION
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:266
#define ETH_RDES0_LS
void xmc4700EthEnableIrq(NetInterface *interface)
Enable interrupts.
error_t xmc4700EthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
#define ETH_BUS_MODE_RPBL_32
void xmc4700EthEventHandler(NetInterface *interface)
XMC4700 Ethernet MAC event handler.
#define ETH_CON_CLK_RMII_C
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:418
#define macIsMulticastAddr(macAddr)
Definition: ethernet.h:133
#define osExitIsr(flag)
error_t xmc4700EthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define ETH_BUS_MODE_PBL_32
#define SMI_OPCODE_WRITE
Definition: nic.h:66
error_t xmc4700EthReceivePacket(NetInterface *interface)
Receive a packet.
#define XMC4700_ETH_TX_BUFFER_SIZE
#define ETH_CON_RXD0_A
#define FALSE
Definition: os_port.h:46
#define XMC4700_ETH_TX_BUFFER_COUNT
#define ETH_RDES0_FL
error_t
Error codes.
Definition: error.h:43
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:103
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
#define XMC4700_ETH_IRQ_PRIORITY_GROUPING
#define txBuffer
#define ETH_RDES0_OWN
#define NetRxAncillary
Definition: net_misc.h:40
@ ERROR_INVALID_PACKET
Definition: error.h:141
#define NetInterface
Definition: net.h:40
MacAddr addr
MAC address.
Definition: ethernet.h:265
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_BUFFER_EMPTY
Definition: error.h:142
#define ETH_RDES0_FS
#define XMC4700_ETH_IRQ_GROUP_PRIORITY
const NicDriver xmc4700EthDriver
XMC4700 Ethernet MAC driver.
#define NetTxAncillary
Definition: net_misc.h:36
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:105
#define ETH_TDES0_OWN
uint8_t length
Definition: tcp.h:375
#define ETH_TDES0_TCH
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
#define XMC4700_ETH_RX_BUFFER_COUNT
#define XMC4700_ETH_RX_BUFFER_SIZE
#define MIN(a, b)
Definition: os_port.h:63
#define ETH_RDES0_ES
#define rxBuffer
MacAddr
Definition: ethernet.h:197
Receive DMA descriptor.
#define ETH_TDES0_IC
#define TRACE_DEBUG(...)
Definition: debug.h:119
uint16_t regAddr
#define ETH_GMII_ADDRESS_CR_DIV62
void ETH0_0_IRQHandler(void)
XMC4700 Ethernet MAC interrupt service routine.
#define ETH_MTU
Definition: ethernet.h:116
void xmc4700EthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
uint8_t n
#define ETH_TDES0_FS
MAC filter table entry.
Definition: ethernet.h:264
void xmc4700EthInitDmaDesc(NetInterface *interface)
Initialize DMA descriptor lists.
#define ETH_RDES1_RCH
Infineon XMC4700 Ethernet MAC driver.
#define osEnterIsr()
__weak_func void xmc4700EthInitGpio(NetInterface *interface)
GPIO configuration.
error_t xmc4700EthInit(NetInterface *interface)
XMC4700 Ethernet MAC initialization.
Transmit DMA descriptor.
#define rxDmaDesc
#define ETH_CON_CRS_DV_C
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define ETH_TDES0_LS
void xmc4700EthDisableIrq(NetInterface *interface)
Disable interrupts.
#define ETH_MAC_CONFIGURATION_RESERVED15_Msk
#define txDmaDesc
#define ETH_RDES1_RBS1
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
void xmc4700EthTick(NetInterface *interface)
XMC4700 Ethernet MAC timer handler.
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
NIC driver.
Definition: nic.h:286
#define ETH_CON_RXD1_A
uint32_t xmc4700EthCalcCrc(const void *data, size_t length)
CRC calculation.
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:51
@ NO_ERROR
Success.
Definition: error.h:44
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
Debugging facilities.
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83
error_t xmc4700EthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.