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