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