mpc5748_eth2_driver.c
Go to the documentation of this file.
1 /**
2  * @file mpc5748_eth2_driver.c
3  * @brief NXP MPC5748 Ethernet MAC driver (ENET1 instance)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 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.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include "device_registers.h"
36 #include "interrupt_manager.h"
37 #include "core/net.h"
39 #include "debug.h"
40 
41 //Underlying network interface
42 static NetInterface *nicDriverInterface;
43 
44 //TX buffer
46  __attribute__((aligned(64)));
47 //RX buffer
49  __attribute__((aligned(64)));
50 //TX buffer descriptors
51 static uint32_t txBufferDesc[MPC5748_ETH2_TX_BUFFER_COUNT][8]
52  __attribute__((aligned(64)));
53 //RX buffer descriptors
54 static uint32_t rxBufferDesc[MPC5748_ETH2_RX_BUFFER_COUNT][8]
55  __attribute__((aligned(64)));
56 
57 //TX buffer index
58 static uint_t txBufferIndex;
59 //RX buffer index
60 static uint_t rxBufferIndex;
61 
62 
63 /**
64  * @brief MPC5748 Ethernet MAC driver (ENET1 instance)
65  **/
66 
68 {
70  ETH_MTU,
81  TRUE,
82  TRUE,
83  TRUE,
84  FALSE
85 };
86 
87 
88 /**
89  * @brief MPC5748 Ethernet MAC initialization
90  * @param[in] interface Underlying network interface
91  * @return Error code
92  **/
93 
95 {
96  error_t error;
97  uint32_t value;
98 
99  //Debug message
100  TRACE_INFO("Initializing MPC5748 Ethernet MAC (ENET1)...\r\n");
101 
102  //Save underlying network interface
103  nicDriverInterface = interface;
104 
105  //GPIO configuration
106  mpc5748Eth2InitGpio(interface);
107 
108  //Reset ENET module
109  ENET_1->ECR = ENET_ECR_RESET_MASK;
110  //Wait for the reset to complete
111  while((ENET_1->ECR & ENET_ECR_RESET_MASK) != 0)
112  {
113  }
114 
115  //Receive control register
116  ENET_1->RCR = ENET_RCR_MAX_FL(MPC5748_ETH2_RX_BUFFER_SIZE) |
117  ENET_RCR_RMII_MODE_MASK | ENET_RCR_MII_MODE_MASK;
118 
119  //Transmit control register
120  ENET_1->TCR = 0;
121  //Configure MDC clock frequency
122  ENET_1->MSCR = ENET_MSCR_MII_SPEED(19);
123 
124  //Valid Ethernet PHY or switch driver?
125  if(interface->phyDriver != NULL)
126  {
127  //Ethernet PHY initialization
128  error = interface->phyDriver->init(interface);
129  }
130  else if(interface->switchDriver != NULL)
131  {
132  //Ethernet switch initialization
133  error = interface->switchDriver->init(interface);
134  }
135  else
136  {
137  //The interface is not properly configured
138  error = ERROR_FAILURE;
139  }
140 
141  //Any error to report?
142  if(error)
143  {
144  return error;
145  }
146 
147  //Set the MAC address of the station (upper 16 bits)
148  value = interface->macAddr.b[5];
149  value |= (interface->macAddr.b[4] << 8);
150  ENET_1->PAUR = ENET_PAUR_PADDR2(value) | ENET_PAUR_TYPE(0x8808);
151 
152  //Set the MAC address of the station (lower 32 bits)
153  value = interface->macAddr.b[3];
154  value |= (interface->macAddr.b[2] << 8);
155  value |= (interface->macAddr.b[1] << 16);
156  value |= (interface->macAddr.b[0] << 24);
157  ENET_1->PALR = ENET_PALR_PADDR1(value);
158 
159  //Hash table for unicast address filtering
160  ENET_1->IALR = 0;
161  ENET_1->IAUR = 0;
162  //Hash table for multicast address filtering
163  ENET_1->GALR = 0;
164  ENET_1->GAUR = 0;
165 
166  //Disable transmit accelerator functions
167  ENET_1->TACC = 0;
168  //Disable receive accelerator functions
169  ENET_1->RACC = 0;
170 
171  //Use enhanced buffer descriptors
172  ENET_1->ECR = ENET_ECR_EN1588_MASK;
173 
174  //Reset statistics counters
175  ENET_1->MIBC = ENET_MIBC_MIB_CLEAR_MASK;
176  ENET_1->MIBC = 0;
177 
178  //Initialize buffer descriptors
179  mpc5748Eth2InitBufferDesc(interface);
180 
181  //Clear any pending interrupts
182  ENET_1->EIR = 0xFFFFFFFF;
183  //Enable desired interrupts
184  ENET_1->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
185 
186  //Configure ENET transmit interrupt priority
187  INT_SYS_SetPriority(ENET1_GROUP2_IRQn, MPC5748_ETH2_IRQ_PRIORITY);
188  //Configure ENET receive interrupt priority
189  INT_SYS_SetPriority(ENET1_GROUP1_IRQn, MPC5748_ETH2_IRQ_PRIORITY);
190  //Configure ENET error interrupt priority
191  INT_SYS_SetPriority(ENET1_GROUP0_IRQn, MPC5748_ETH2_IRQ_PRIORITY);
192 
193  //Enable Ethernet MAC
194  ENET_1->ECR |= ENET_ECR_ETHEREN_MASK;
195  //Instruct the DMA to poll the receive descriptor list
196  ENET_1->RDAR = ENET_RDAR_RDAR_MASK;
197 
198  //Accept any packets from the upper layer
199  osSetEvent(&interface->nicTxEvent);
200 
201  //Successful initialization
202  return NO_ERROR;
203 }
204 
205 
206 /**
207  * @brief GPIO configuration
208  * @param[in] interface Underlying network interface
209  **/
210 
211 __weak_func void mpc5748Eth2InitGpio(NetInterface *interface)
212 {
213 }
214 
215 
216 /**
217  * @brief Initialize buffer descriptors
218  * @param[in] interface Underlying network interface
219  **/
220 
222 {
223  uint_t i;
224  uint32_t address;
225 
226  //Clear TX and RX buffer descriptors
227  osMemset(txBufferDesc, 0, sizeof(txBufferDesc));
228  osMemset(rxBufferDesc, 0, sizeof(rxBufferDesc));
229 
230  //Initialize TX buffer descriptors
231  for(i = 0; i < MPC5748_ETH2_TX_BUFFER_COUNT; i++)
232  {
233  //Calculate the address of the current TX buffer
234  address = (uint32_t) txBuffer[i];
235  //Transmit buffer address
236  txBufferDesc[i][1] = address;
237  //Generate interrupts
238  txBufferDesc[i][2] = ENET_TBD2_INT;
239  }
240 
241  //Mark the last descriptor entry with the wrap flag
242  txBufferDesc[i - 1][0] |= ENET_TBD0_W;
243  //Initialize TX buffer index
244  txBufferIndex = 0;
245 
246  //Initialize RX buffer descriptors
247  for(i = 0; i < MPC5748_ETH2_RX_BUFFER_COUNT; i++)
248  {
249  //Calculate the address of the current RX buffer
250  address = (uint32_t) rxBuffer[i];
251  //The descriptor is initially owned by the DMA
252  rxBufferDesc[i][0] = ENET_RBD0_E;
253  //Receive buffer address
254  rxBufferDesc[i][1] = address;
255  //Generate interrupts
256  rxBufferDesc[i][2] = ENET_RBD2_INT;
257  }
258 
259  //Mark the last descriptor entry with the wrap flag
260  rxBufferDesc[i - 1][0] |= ENET_RBD0_W;
261  //Initialize RX buffer index
262  rxBufferIndex = 0;
263 
264  //Start location of the TX descriptor list
265  ENET_1->TDSR = (uint32_t) txBufferDesc;
266  //Start location of the RX descriptor list
267  ENET_1->RDSR = (uint32_t) rxBufferDesc;
268  //Maximum receive buffer size
269  ENET_1->MRBR = MPC5748_ETH2_RX_BUFFER_SIZE;
270 }
271 
272 
273 /**
274  * @brief MPC5748 Ethernet MAC timer handler
275  *
276  * This routine is periodically called by the TCP/IP stack to handle periodic
277  * operations such as polling the link state
278  *
279  * @param[in] interface Underlying network interface
280  **/
281 
283 {
284  //Valid Ethernet PHY or switch driver?
285  if(interface->phyDriver != NULL)
286  {
287  //Handle periodic operations
288  interface->phyDriver->tick(interface);
289  }
290  else if(interface->switchDriver != NULL)
291  {
292  //Handle periodic operations
293  interface->switchDriver->tick(interface);
294  }
295  else
296  {
297  //Just for sanity
298  }
299 }
300 
301 
302 /**
303  * @brief Enable interrupts
304  * @param[in] interface Underlying network interface
305  **/
306 
308 {
309  //Enable Ethernet MAC interrupts
310  INT_SYS_EnableIRQ(ENET1_GROUP2_IRQn);
311  INT_SYS_EnableIRQ(ENET1_GROUP1_IRQn);
312  INT_SYS_EnableIRQ(ENET1_GROUP0_IRQn);
313 
314  //Valid Ethernet PHY or switch driver?
315  if(interface->phyDriver != NULL)
316  {
317  //Enable Ethernet PHY interrupts
318  interface->phyDriver->enableIrq(interface);
319  }
320  else if(interface->switchDriver != NULL)
321  {
322  //Enable Ethernet switch interrupts
323  interface->switchDriver->enableIrq(interface);
324  }
325  else
326  {
327  //Just for sanity
328  }
329 }
330 
331 
332 /**
333  * @brief Disable interrupts
334  * @param[in] interface Underlying network interface
335  **/
336 
338 {
339  //Disable Ethernet MAC interrupts
340  INT_SYS_DisableIRQ(ENET1_GROUP2_IRQn);
341  INT_SYS_DisableIRQ(ENET1_GROUP1_IRQn);
342  INT_SYS_DisableIRQ(ENET1_GROUP0_IRQn);
343 
344  //Valid Ethernet PHY or switch driver?
345  if(interface->phyDriver != NULL)
346  {
347  //Disable Ethernet PHY interrupts
348  interface->phyDriver->disableIrq(interface);
349  }
350  else if(interface->switchDriver != NULL)
351  {
352  //Disable Ethernet switch interrupts
353  interface->switchDriver->disableIrq(interface);
354  }
355  else
356  {
357  //Just for sanity
358  }
359 }
360 
361 
362 /**
363  * @brief Ethernet MAC transmit interrupt
364  **/
365 
367 {
368  bool_t flag;
369 
370  //Interrupt service routine prologue
371  osEnterIsr();
372 
373  //This flag will be set if a higher priority task must be woken
374  flag = FALSE;
375 
376  //Packet transmitted?
377  if((ENET_1->EIR & ENET_EIR_TXF_MASK) != 0)
378  {
379  //Clear TXF interrupt flag
380  ENET_1->EIR = ENET_EIR_TXF_MASK;
381 
382  //Check whether the TX buffer is available for writing
383  if((txBufferDesc[txBufferIndex][0] & ENET_TBD0_R) == 0)
384  {
385  //Notify the TCP/IP stack that the transmitter is ready to send
386  flag = osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
387  }
388 
389  //Instruct the DMA to poll the transmit descriptor list
390  ENET_1->TDAR = ENET_TDAR_TDAR_MASK;
391  }
392 
393  //Interrupt service routine epilogue
394  osExitIsr(flag);
395 }
396 
397 
398 /**
399  * @brief Ethernet MAC receive interrupt
400  **/
401 
403 {
404  bool_t flag;
405 
406  //Interrupt service routine prologue
407  osEnterIsr();
408 
409  //This flag will be set if a higher priority task must be woken
410  flag = FALSE;
411 
412  //Packet received?
413  if((ENET_1->EIR & ENET_EIR_RXF_MASK) != 0)
414  {
415  //Disable RXF interrupt
416  ENET_1->EIMR &= ~ENET_EIMR_RXF_MASK;
417 
418  //Set event flag
419  nicDriverInterface->nicEvent = TRUE;
420  //Notify the TCP/IP stack of the event
421  flag = osSetEventFromIsr(&netEvent);
422  }
423 
424  //Interrupt service routine epilogue
425  osExitIsr(flag);
426 }
427 
428 
429 /**
430  * @brief Ethernet MAC error interrupt
431  **/
432 
434 {
435  bool_t flag;
436 
437  //Interrupt service routine prologue
438  osEnterIsr();
439 
440  //This flag will be set if a higher priority task must be woken
441  flag = FALSE;
442 
443  //System bus error?
444  if((ENET_1->EIR & ENET_EIR_EBERR_MASK) != 0)
445  {
446  //Disable EBERR interrupt
447  ENET_1->EIMR &= ~ENET_EIMR_EBERR_MASK;
448 
449  //Set event flag
450  nicDriverInterface->nicEvent = TRUE;
451  //Notify the TCP/IP stack of the event
452  flag |= osSetEventFromIsr(&netEvent);
453  }
454 
455  //Interrupt service routine epilogue
456  osExitIsr(flag);
457 }
458 
459 
460 /**
461  * @brief MPC5748 Ethernet MAC event handler
462  * @param[in] interface Underlying network interface
463  **/
464 
466 {
467  error_t error;
468  uint32_t status;
469 
470  //Read interrupt event register
471  status = ENET_1->EIR;
472 
473  //Packet received?
474  if((status & ENET_EIR_RXF_MASK) != 0)
475  {
476  //Clear RXF interrupt flag
477  ENET_1->EIR = ENET_EIR_RXF_MASK;
478 
479  //Process all pending packets
480  do
481  {
482  //Read incoming packet
483  error = mpc5748Eth2ReceivePacket(interface);
484 
485  //No more data in the receive buffer?
486  } while(error != ERROR_BUFFER_EMPTY);
487  }
488 
489  //System bus error?
490  if((status & ENET_EIR_EBERR_MASK) != 0)
491  {
492  //Clear EBERR interrupt flag
493  ENET_1->EIR = ENET_EIR_EBERR_MASK;
494 
495  //Disable Ethernet MAC
496  ENET_1->ECR &= ~ENET_ECR_ETHEREN_MASK;
497  //Reset buffer descriptors
498  mpc5748Eth2InitBufferDesc(interface);
499  //Resume normal operation
500  ENET_1->ECR |= ENET_ECR_ETHEREN_MASK;
501  //Instruct the DMA to poll the receive descriptor list
502  ENET_1->RDAR = ENET_RDAR_RDAR_MASK;
503  }
504 
505  //Re-enable Ethernet MAC interrupts
506  ENET_1->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
507 }
508 
509 
510 /**
511  * @brief Send a packet
512  * @param[in] interface Underlying network interface
513  * @param[in] buffer Multi-part buffer containing the data to send
514  * @param[in] offset Offset to the first data byte
515  * @param[in] ancillary Additional options passed to the stack along with
516  * the packet
517  * @return Error code
518  **/
519 
521  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
522 {
523  size_t length;
524 
525  //Retrieve the length of the packet
526  length = netBufferGetLength(buffer) - offset;
527 
528  //Check the frame length
530  {
531  //The transmitter can accept another packet
532  osSetEvent(&interface->nicTxEvent);
533  //Report an error
534  return ERROR_INVALID_LENGTH;
535  }
536 
537  //Make sure the current buffer is available for writing
538  if((txBufferDesc[txBufferIndex][0] & ENET_TBD0_R) != 0)
539  {
540  return ERROR_FAILURE;
541  }
542 
543  //Copy user data to the transmit buffer
544  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
545 
546  //Clear BDU flag
547  txBufferDesc[txBufferIndex][4] = 0;
548 
549  //Check current index
550  if(txBufferIndex < (MPC5748_ETH2_TX_BUFFER_COUNT - 1))
551  {
552  //Give the ownership of the descriptor to the DMA engine
553  txBufferDesc[txBufferIndex][0] = ENET_TBD0_R | ENET_TBD0_L |
555 
556  //Point to the next buffer
557  txBufferIndex++;
558  }
559  else
560  {
561  //Give the ownership of the descriptor to the DMA engine
562  txBufferDesc[txBufferIndex][0] = ENET_TBD0_R | ENET_TBD0_W |
564 
565  //Wrap around
566  txBufferIndex = 0;
567  }
568 
569  //Instruct the DMA to poll the transmit descriptor list
570  ENET_1->TDAR = ENET_TDAR_TDAR_MASK;
571 
572  //Check whether the next buffer is available for writing
573  if((txBufferDesc[txBufferIndex][0] & ENET_TBD0_R) == 0)
574  {
575  //The transmitter can accept another packet
576  osSetEvent(&interface->nicTxEvent);
577  }
578 
579  //Successful processing
580  return NO_ERROR;
581 }
582 
583 
584 /**
585  * @brief Receive a packet
586  * @param[in] interface Underlying network interface
587  * @return Error code
588  **/
589 
591 {
592  error_t error;
593  size_t n;
594  NetRxAncillary ancillary;
595 
596  //Current buffer available for reading?
597  if((rxBufferDesc[rxBufferIndex][0] & ENET_RBD0_E) == 0)
598  {
599  //The frame should not span multiple buffers
600  if((rxBufferDesc[rxBufferIndex][0] & ENET_RBD0_L) != 0)
601  {
602  //Check whether an error occurred
603  if((rxBufferDesc[rxBufferIndex][0] & (ENET_RBD0_LG | ENET_RBD0_NO |
605  {
606  //Retrieve the length of the frame
607  n = rxBufferDesc[rxBufferIndex][0] & ENET_RBD0_DATA_LENGTH;
608  //Limit the number of data to read
610 
611  //Additional options can be passed to the stack along with the packet
612  ancillary = NET_DEFAULT_RX_ANCILLARY;
613 
614  //Pass the packet to the upper layer
615  nicProcessPacket(interface, rxBuffer[rxBufferIndex], n, &ancillary);
616 
617  //Valid packet received
618  error = NO_ERROR;
619  }
620  else
621  {
622  //The received packet contains an error
623  error = ERROR_INVALID_PACKET;
624  }
625  }
626  else
627  {
628  //The packet is not valid
629  error = ERROR_INVALID_PACKET;
630  }
631 
632  //Clear BDU flag
633  rxBufferDesc[rxBufferIndex][4] = 0;
634 
635  //Check current index
636  if(rxBufferIndex < (MPC5748_ETH2_RX_BUFFER_COUNT - 1))
637  {
638  //Give the ownership of the descriptor back to the DMA engine
639  rxBufferDesc[rxBufferIndex][0] = ENET_RBD0_E;
640  //Point to the next buffer
641  rxBufferIndex++;
642  }
643  else
644  {
645  //Give the ownership of the descriptor back to the DMA engine
646  rxBufferDesc[rxBufferIndex][0] = ENET_RBD0_E | ENET_RBD0_W;
647  //Wrap around
648  rxBufferIndex = 0;
649  }
650 
651  //Instruct the DMA to poll the receive descriptor list
652  ENET_1->RDAR = ENET_RDAR_RDAR_MASK;
653  }
654  else
655  {
656  //No more data in the receive buffer
657  error = ERROR_BUFFER_EMPTY;
658  }
659 
660  //Return status code
661  return error;
662 }
663 
664 
665 /**
666  * @brief Configure MAC address filtering
667  * @param[in] interface Underlying network interface
668  * @return Error code
669  **/
670 
672 {
673  uint_t i;
674  uint_t k;
675  uint32_t crc;
676  uint32_t value;
677  uint32_t unicastHashTable[2];
678  uint32_t multicastHashTable[2];
679  MacFilterEntry *entry;
680 
681  //Debug message
682  TRACE_DEBUG("Updating MAC filter...\r\n");
683 
684  //Set the MAC address of the station (upper 16 bits)
685  value = interface->macAddr.b[5];
686  value |= (interface->macAddr.b[4] << 8);
687  ENET_1->PAUR = ENET_PAUR_PADDR2(value) | ENET_PAUR_TYPE(0x8808);
688 
689  //Set the MAC address of the station (lower 32 bits)
690  value = interface->macAddr.b[3];
691  value |= (interface->macAddr.b[2] << 8);
692  value |= (interface->macAddr.b[1] << 16);
693  value |= (interface->macAddr.b[0] << 24);
694  ENET_1->PALR = ENET_PALR_PADDR1(value);
695 
696  //Clear hash table (unicast address filtering)
697  unicastHashTable[0] = 0;
698  unicastHashTable[1] = 0;
699 
700  //Clear hash table (multicast address filtering)
701  multicastHashTable[0] = 0;
702  multicastHashTable[1] = 0;
703 
704  //The MAC address filter contains the list of MAC addresses to accept
705  //when receiving an Ethernet frame
706  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
707  {
708  //Point to the current entry
709  entry = &interface->macAddrFilter[i];
710 
711  //Valid entry?
712  if(entry->refCount > 0)
713  {
714  //Compute CRC over the current MAC address
715  crc = mpc5748Eth2CalcCrc(&entry->addr, sizeof(MacAddr));
716 
717  //The upper 6 bits in the CRC register are used to index the
718  //contents of the hash table
719  k = (crc >> 26) & 0x3F;
720 
721  //Multicast address?
722  if(macIsMulticastAddr(&entry->addr))
723  {
724  //Update the multicast hash table
725  multicastHashTable[k / 32] |= (1 << (k % 32));
726  }
727  else
728  {
729  //Update the unicast hash table
730  unicastHashTable[k / 32] |= (1 << (k % 32));
731  }
732  }
733  }
734 
735  //Write the hash table (unicast address filtering)
736  ENET_1->IALR = unicastHashTable[0];
737  ENET_1->IAUR = unicastHashTable[1];
738 
739  //Write the hash table (multicast address filtering)
740  ENET_1->GALR = multicastHashTable[0];
741  ENET_1->GAUR = multicastHashTable[1];
742 
743  //Debug message
744  TRACE_DEBUG(" IALR = %08" PRIX32 "\r\n", ENET_1->IALR);
745  TRACE_DEBUG(" IAUR = %08" PRIX32 "\r\n", ENET_1->IAUR);
746  TRACE_DEBUG(" GALR = %08" PRIX32 "\r\n", ENET_1->GALR);
747  TRACE_DEBUG(" GAUR = %08" PRIX32 "\r\n", ENET_1->GAUR);
748 
749  //Successful processing
750  return NO_ERROR;
751 }
752 
753 
754 /**
755  * @brief Adjust MAC configuration parameters for proper operation
756  * @param[in] interface Underlying network interface
757  * @return Error code
758  **/
759 
761 {
762  //Disable Ethernet MAC while modifying configuration registers
763  ENET_1->ECR &= ~ENET_ECR_ETHEREN_MASK;
764 
765  //10BASE-T or 100BASE-TX operation mode?
766  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
767  {
768  //100 Mbps operation
769  ENET_1->RCR &= ~ENET_RCR_RMII_10T_MASK;
770  }
771  else
772  {
773  //10 Mbps operation
774  ENET_1->RCR |= ENET_RCR_RMII_10T_MASK;
775  }
776 
777  //Half-duplex or full-duplex mode?
778  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
779  {
780  //Full-duplex mode
781  ENET_1->TCR |= ENET_TCR_FDEN_MASK;
782  //Receive path operates independently of transmit
783  ENET_1->RCR &= ~ENET_RCR_DRT_MASK;
784  }
785  else
786  {
787  //Half-duplex mode
788  ENET_1->TCR &= ~ENET_TCR_FDEN_MASK;
789  //Disable reception of frames while transmitting
790  ENET_1->RCR |= ENET_RCR_DRT_MASK;
791  }
792 
793  //Reset buffer descriptors
794  mpc5748Eth2InitBufferDesc(interface);
795 
796  //Re-enable Ethernet MAC
797  ENET_1->ECR |= ENET_ECR_ETHEREN_MASK;
798  //Instruct the DMA to poll the receive descriptor list
799  ENET_1->RDAR = ENET_RDAR_RDAR_MASK;
800 
801  //Successful processing
802  return NO_ERROR;
803 }
804 
805 
806 /**
807  * @brief Write PHY register
808  * @param[in] opcode Access type (2 bits)
809  * @param[in] phyAddr PHY address (5 bits)
810  * @param[in] regAddr Register address (5 bits)
811  * @param[in] data Register value
812  **/
813 
814 void mpc5748Eth2WritePhyReg(uint8_t opcode, uint8_t phyAddr,
815  uint8_t regAddr, uint16_t data)
816 {
817  uint32_t temp;
818 
819  //Valid opcode?
820  if(opcode == SMI_OPCODE_WRITE)
821  {
822  //Set up a write operation
823  temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(1) | ENET_MMFR_TA(2);
824  //PHY address
825  temp |= ENET_MMFR_PA(phyAddr);
826  //Register address
827  temp |= ENET_MMFR_RA(regAddr);
828  //Register value
829  temp |= ENET_MMFR_DATA(data);
830 
831  //Clear MII interrupt flag
832  ENET_1->EIR = ENET_EIR_MII_MASK;
833  //Start a write operation
834  ENET_1->MMFR = temp;
835 
836  //Wait for the write to complete
837  while((ENET_1->EIR & ENET_EIR_MII_MASK) == 0)
838  {
839  }
840  }
841  else
842  {
843  //The MAC peripheral only supports standard Clause 22 opcodes
844  }
845 }
846 
847 
848 /**
849  * @brief Read PHY register
850  * @param[in] opcode Access type (2 bits)
851  * @param[in] phyAddr PHY address (5 bits)
852  * @param[in] regAddr Register address (5 bits)
853  * @return Register value
854  **/
855 
856 uint16_t mpc5748Eth2ReadPhyReg(uint8_t opcode, uint8_t phyAddr,
857  uint8_t regAddr)
858 {
859  uint16_t data;
860  uint32_t temp;
861 
862  //Valid opcode?
863  if(opcode == SMI_OPCODE_READ)
864  {
865  //Set up a read operation
866  temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(2) | ENET_MMFR_TA(2);
867  //PHY address
868  temp |= ENET_MMFR_PA(phyAddr);
869  //Register address
870  temp |= ENET_MMFR_RA(regAddr);
871 
872  //Clear MII interrupt flag
873  ENET_1->EIR = ENET_EIR_MII_MASK;
874  //Start a read operation
875  ENET_1->MMFR = temp;
876 
877  //Wait for the read to complete
878  while((ENET_1->EIR & ENET_EIR_MII_MASK) == 0)
879  {
880  }
881 
882  //Get register value
883  data = ENET_1->MMFR & ENET_MMFR_DATA_MASK;
884  }
885  else
886  {
887  //The MAC peripheral only supports standard Clause 22 opcodes
888  data = 0;
889  }
890 
891  //Return the value of the PHY register
892  return data;
893 }
894 
895 
896 /**
897  * @brief CRC calculation
898  * @param[in] data Pointer to the data over which to calculate the CRC
899  * @param[in] length Number of bytes to process
900  * @return Resulting CRC value
901  **/
902 
903 uint32_t mpc5748Eth2CalcCrc(const void *data, size_t length)
904 {
905  uint_t i;
906  uint_t j;
907  uint32_t crc;
908  const uint8_t *p;
909 
910  //Point to the data over which to calculate the CRC
911  p = (uint8_t *) data;
912  //CRC preset value
913  crc = 0xFFFFFFFF;
914 
915  //Loop through data
916  for(i = 0; i < length; i++)
917  {
918  //Update CRC value
919  crc ^= p[i];
920 
921  //The message is processed bit by bit
922  for(j = 0; j < 8; j++)
923  {
924  if((crc & 0x01) != 0)
925  {
926  crc = (crc >> 1) ^ 0xEDB88320;
927  }
928  else
929  {
930  crc = crc >> 1;
931  }
932  }
933  }
934 
935  //Return CRC value
936  return crc;
937 }
#define rxBuffer
#define txBuffer
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
unsigned int uint_t
Definition: compiler_port.h:50
int bool_t
Definition: compiler_port.h:53
Debugging facilities.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define TRACE_INFO(...)
Definition: debug.h:95
uint8_t n
uint8_t opcode
Definition: dns_common.h:188
error_t
Error codes.
Definition: error.h:43
@ ERROR_BUFFER_EMPTY
Definition: error.h:141
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_PACKET
Definition: error.h:140
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
#define macIsMulticastAddr(macAddr)
Definition: ethernet.h:133
#define ETH_MTU
Definition: ethernet.h:116
uint8_t data[]
Definition: ethernet.h:222
MacAddr
Definition: ethernet.h:195
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
Ipv6Addr address[]
Definition: ipv6.h:316
#define ENET_RBD0_E
#define ENET_RBD0_DATA_LENGTH
#define ENET_TBD2_INT
#define ENET_TBD0_W
#define ENET_TBD0_R
#define ENET_RBD0_L
#define ENET_RBD0_NO
#define ENET_RBD0_W
#define ENET_RBD0_LG
#define ENET_TBD0_DATA_LENGTH
#define ENET_RBD2_INT
#define ENET_RBD0_OV
#define ENET_TBD0_TC
#define ENET_RBD0_CR
#define ENET_TBD0_L
#define ENET_RBD0_TR
uint16_t regAddr
void ENET1_Tx_IRQHandler(void)
Ethernet MAC transmit interrupt.
void mpc5748Eth2EnableIrq(NetInterface *interface)
Enable interrupts.
void mpc5748Eth2DisableIrq(NetInterface *interface)
Disable interrupts.
void mpc5748Eth2WritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
void ENET1_Err_IRQHandler(void)
Ethernet MAC error interrupt.
error_t mpc5748Eth2UpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
error_t mpc5748Eth2SendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
error_t mpc5748Eth2ReceivePacket(NetInterface *interface)
Receive a packet.
void mpc5748Eth2InitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
uint16_t mpc5748Eth2ReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
__weak_func void mpc5748Eth2InitGpio(NetInterface *interface)
GPIO configuration.
void mpc5748Eth2Tick(NetInterface *interface)
MPC5748 Ethernet MAC timer handler.
error_t mpc5748Eth2UpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
const NicDriver mpc5748Eth2Driver
MPC5748 Ethernet MAC driver (ENET1 instance)
void ENET1_Rx_IRQHandler(void)
Ethernet MAC receive interrupt.
uint32_t mpc5748Eth2CalcCrc(const void *data, size_t length)
CRC calculation.
void mpc5748Eth2EventHandler(NetInterface *interface)
MPC5748 Ethernet MAC event handler.
error_t mpc5748Eth2Init(NetInterface *interface)
MPC5748 Ethernet MAC initialization.
NXP MPC5748 Ethernet MAC driver (ENET1 instance)
#define MPC5748_ETH2_RX_BUFFER_COUNT
#define MPC5748_ETH2_RX_BUFFER_SIZE
#define MPC5748_ETH2_TX_BUFFER_COUNT
#define MPC5748_ETH2_IRQ_PRIORITY
#define MPC5748_ETH2_TX_BUFFER_SIZE
uint8_t p
Definition: ndp.h:300
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define netEvent
Definition: net_legacy.h:196
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
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:674
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:101
#define NetRxAncillary
Definition: net_misc.h:40
#define NetTxAncillary
Definition: net_misc.h:36
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:391
#define SMI_OPCODE_WRITE
Definition: nic.h:66
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83
#define SMI_OPCODE_READ
Definition: nic.h:67
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
#define osMemset(p, value, length)
Definition: os_port.h:135
#define MIN(a, b)
Definition: os_port.h:63
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define osEnterIsr()
#define osExitIsr(flag)
MAC filter table entry.
Definition: ethernet.h:262
MacAddr addr
MAC address.
Definition: ethernet.h:263
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
NIC driver.
Definition: nic.h:283
uint8_t length
Definition: tcp.h:368
uint8_t value[]
Definition: tcp.h:369