mk6x_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file mk6x_eth_driver.c
3  * @brief NXP Kinetis K60/K64/K65/K66 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.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 "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 uint16_t txBufferDesc[MK6X_ETH_TX_BUFFER_COUNT][16];
55 //RX buffer descriptors
56 #pragma data_alignment = 16
57 static uint16_t rxBufferDesc[MK6X_ETH_RX_BUFFER_COUNT][16];
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 uint16_t txBufferDesc[MK6X_ETH_TX_BUFFER_COUNT][16]
70  __attribute__((aligned(16)));
71 //RX buffer descriptors
72 static uint16_t rxBufferDesc[MK6X_ETH_RX_BUFFER_COUNT][16]
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 K6x Ethernet MAC driver
85  **/
86 
88 {
90  ETH_MTU,
101  TRUE,
102  TRUE,
103  TRUE,
104  FALSE
105 };
106 
107 
108 /**
109  * @brief Kinetis K6x 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 K6x 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  OSC->CR |= OSC_CR_ERCLKEN_MASK;
130  //Enable ENET peripheral clock
131  SIM->SCGC2 |= SIM_SCGC2_ENET_MASK;
132 
133  //GPIO configuration
134  mk6xEthInitGpio(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(MK6X_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_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  mk6xEthInitBufferDesc(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(MK6X_ETH_IRQ_PRIORITY_GROUPING);
216 
217  //Configure ENET transmit interrupt priority
218  NVIC_SetPriority(ENET_Transmit_IRQn, NVIC_EncodePriority(MK6X_ETH_IRQ_PRIORITY_GROUPING,
220 
221  //Configure ENET receive interrupt priority
222  NVIC_SetPriority(ENET_Receive_IRQn, NVIC_EncodePriority(MK6X_ETH_IRQ_PRIORITY_GROUPING,
224 
225  //Configure ENET error interrupt priority
226  NVIC_SetPriority(ENET_Error_IRQn, NVIC_EncodePriority(MK6X_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 mk6xEthInitGpio(NetInterface *interface)
248 {
249 //TWR-K60N512, TWR-K60D100M or TWR-K60F120M evaluation board?
250 #if defined(USE_TWR_K60N512) || defined(USE_TWR_K60D100M) || \
251  defined(USE_TWR_K60F120M)
252  //Enable PORTA and PORTB peripheral clocks
253  SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK;
254 
255  //Configure RMII0_RXER (PTA5)
256  PORTA->PCR[5] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK;
257  //Configure RMII0_RXD1 (PTA12)
258  PORTA->PCR[12] = PORT_PCR_MUX(4);
259  //Configure RMII0_RXD0 (PTA13)
260  PORTA->PCR[13] = PORT_PCR_MUX(4);
261  //Configure RMII0_CRS_DV (PTA14)
262  PORTA->PCR[14] = PORT_PCR_MUX(4);
263  //Configure RMII0_TXEN (PTA15)
264  PORTA->PCR[15] = PORT_PCR_MUX(4);
265  //Configure RMII0_TXD0 (PTA16)
266  PORTA->PCR[16] = PORT_PCR_MUX(4);
267  //Configure RMII0_TXD1 (PTA17)
268  PORTA->PCR[17] = PORT_PCR_MUX(4);
269 
270  //Configure RMII0_MDIO (PTB0)
271  PORTB->PCR[0] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
272  //Configure RMII0_MDC (PTB1)
273  PORTB->PCR[1] = PORT_PCR_MUX(4);
274 
275 //FRDM-K64F or TWR-K64F120M evaluation board?
276 #elif defined(USE_FRDM_K64F) || defined(USE_TWR_K64F120M)
277  //Enable PORTA and PORTB peripheral clocks
278  SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK;
279 
280  //Configure RMII0_RXER (PTA5)
281  PORTA->PCR[5] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK;
282  //Configure RMII0_RXD1 (PTA12)
283  PORTA->PCR[12] = PORT_PCR_MUX(4);
284  //Configure RMII0_RXD0 (PTA13)
285  PORTA->PCR[13] = PORT_PCR_MUX(4);
286  //Configure RMII0_CRS_DV (PTA14)
287  PORTA->PCR[14] = PORT_PCR_MUX(4);
288  //Configure RMII0_TXEN (PTA15)
289  PORTA->PCR[15] = PORT_PCR_MUX(4);
290  //Configure RMII0_TXD0 (PTA16)
291  PORTA->PCR[16] = PORT_PCR_MUX(4);
292  //Configure RMII0_TXD1 (PTA17)
293  PORTA->PCR[17] = PORT_PCR_MUX(4);
294 
295  //Configure RMII0_MDIO (PTB0)
296  PORTB->PCR[0] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
297  //Configure RMII0_MDC (PTB1)
298  PORTB->PCR[1] = PORT_PCR_MUX(4);
299 
300  //Select RMII clock source (EXTAL)
301  SIM->SOPT2 &= ~SIM_SOPT2_RMIISRC_MASK;
302 
303 //TWR-K65F180M evaluation board?
304 #elif defined(USE_TWR_K65F180M)
305  //Enable PORTA and PORTE peripheral clocks
306  SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTE_MASK;
307 
308  //Configure RMII0_RXER (PTA5)
309  PORTA->PCR[5] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK;
310  //Configure RMII0_MDIO (PTA7)
311  PORTA->PCR[7] = PORT_PCR_MUX(5) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
312  //Configure RMII0_MDC (PTA8)
313  PORTA->PCR[8] = PORT_PCR_MUX(5);
314  //Configure RMII0_RXD1 (PTA12)
315  PORTA->PCR[12] = PORT_PCR_MUX(4);
316  //Configure RMII0_RXD0 (PTA13)
317  PORTA->PCR[13] = PORT_PCR_MUX(4);
318  //Configure RMII0_CRS_DV (PTA14)
319  PORTA->PCR[14] = PORT_PCR_MUX(4);
320  //Configure RMII0_TXEN (PTA15)
321  PORTA->PCR[15] = PORT_PCR_MUX(4);
322  //Configure RMII0_TXD0 (PTA16)
323  PORTA->PCR[16] = PORT_PCR_MUX(4);
324  //Configure RMII0_TXD1 (PTA17)
325  PORTA->PCR[17] = PORT_PCR_MUX(4);
326 
327  //Configure ENET_1588_CLKIN (PTE26)
328  PORTE->PCR[26] = PORT_PCR_MUX(2);
329 
330  //Select RMII clock source (ENET_1588_CLKIN)
331  SIM->SOPT2 |= SIM_SOPT2_RMIISRC_MASK;
332 
333 //FRDM-K66F evaluation board?
334 #elif defined(USE_FRDM_K66F)
335 //Enable PORTA, PORTB and PORTE peripheral clocks
336  SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK |
337  SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTE_MASK;
338 
339  //Configure RMII0_RXER (PTA5)
340  PORTA->PCR[5] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK;
341  //Configure RMII0_RXD1 (PTA12)
342  PORTA->PCR[12] = PORT_PCR_MUX(4);
343  //Configure RMII0_RXD0 (PTA13)
344  PORTA->PCR[13] = PORT_PCR_MUX(4);
345  //Configure RMII0_CRS_DV (PTA14)
346  PORTA->PCR[14] = PORT_PCR_MUX(4);
347  //Configure RMII0_TXEN (PTA15)
348  PORTA->PCR[15] = PORT_PCR_MUX(4);
349  //Configure RMII0_TXD0 (PTA16)
350  PORTA->PCR[16] = PORT_PCR_MUX(4);
351  //Configure RMII0_TXD1 (PTA17)
352  PORTA->PCR[17] = PORT_PCR_MUX(4);
353 
354  //Configure RMII0_MDIO (PTB0)
355  PORTB->PCR[0] = PORT_PCR_MUX(4) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;
356  //Configure RMII0_MDC (PTB1)
357  PORTB->PCR[1] = PORT_PCR_MUX(4);
358 
359  //Configure ENET_1588_CLKIN (PTE26)
360  PORTE->PCR[26] = PORT_PCR_MUX(2);
361 
362  //Select RMII clock source (ENET_1588_CLKIN)
363  SIM->SOPT2 |= SIM_SOPT2_RMIISRC_MASK;
364 
365 //embOS/IP Switch Board?
366 #elif defined(USE_EMBOS_IP_SWITCH_BOARD)
367  //Enable PORTA and PORTE peripheral clocks
368  SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTE_MASK;
369 
370  //Configure RMII0_RXD1 (PTA12)
371  PORTA->PCR[12] = PORT_PCR_MUX(4);
372  //Configure RMII0_RXD0 (PTA13)
373  PORTA->PCR[13] = PORT_PCR_MUX(4);
374  //Configure RMII0_CRS_DV (PTA14)
375  PORTA->PCR[14] = PORT_PCR_MUX(4);
376  //Configure RMII0_TXEN (PTA15)
377  PORTA->PCR[15] = PORT_PCR_MUX(4);
378  //Configure RMII0_TXD0 (PTA16)
379  PORTA->PCR[16] = PORT_PCR_MUX(4);
380  //Configure RMII0_TXD1 (PTA17)
381  PORTA->PCR[17] = PORT_PCR_MUX(4);
382 
383  //Configure ENET_1588_CLKIN (PTE26)
384  PORTE->PCR[26] = PORT_PCR_MUX(2);
385 
386  //Select RMII clock source (ENET_1588_CLKIN)
387  SIM->SOPT2 |= SIM_SOPT2_RMIISRC_MASK;
388 #endif
389 }
390 
391 
392 /**
393  * @brief Initialize buffer descriptors
394  * @param[in] interface Underlying network interface
395  **/
396 
398 {
399  uint_t i;
400  uint32_t address;
401 
402  //Clear TX and RX buffer descriptors
403  osMemset(txBufferDesc, 0, sizeof(txBufferDesc));
404  osMemset(rxBufferDesc, 0, sizeof(rxBufferDesc));
405 
406  //Initialize TX buffer descriptors
407  for(i = 0; i < MK6X_ETH_TX_BUFFER_COUNT; i++)
408  {
409  //Calculate the address of the current TX buffer
410  address = (uint32_t) txBuffer[i];
411  //Transmit buffer address
412  txBufferDesc[i][2] = htobe16(address >> 16);
413  txBufferDesc[i][3] = htobe16(address & 0xFFFF);
414  //Generate interrupts
415  txBufferDesc[i][4] = HTOBE16(ENET_TBD4_INT);
416  }
417 
418  //Mark the last descriptor entry with the wrap flag
419  txBufferDesc[i - 1][0] |= HTOBE16(ENET_TBD0_W);
420  //Initialize TX buffer index
421  txBufferIndex = 0;
422 
423  //Initialize RX buffer descriptors
424  for(i = 0; i < MK6X_ETH_RX_BUFFER_COUNT; i++)
425  {
426  //Calculate the address of the current RX buffer
427  address = (uint32_t) rxBuffer[i];
428  //The descriptor is initially owned by the DMA
429  rxBufferDesc[i][0] = HTOBE16(ENET_RBD0_E);
430  //Receive buffer address
431  rxBufferDesc[i][2] = htobe16(address >> 16);
432  rxBufferDesc[i][3] = htobe16(address & 0xFFFF);
433  //Generate interrupts
434  rxBufferDesc[i][4] = HTOBE16(ENET_RBD4_INT);
435  }
436 
437  //Mark the last descriptor entry with the wrap flag
438  rxBufferDesc[i - 1][0] |= HTOBE16(ENET_RBD0_W);
439  //Initialize RX buffer index
440  rxBufferIndex = 0;
441 
442  //Start location of the TX descriptor list
443  ENET->TDSR = (uint32_t) txBufferDesc;
444  //Start location of the RX descriptor list
445  ENET->RDSR = (uint32_t) rxBufferDesc;
446  //Maximum receive buffer size
447  ENET->MRBR = MK6X_ETH_RX_BUFFER_SIZE;
448 }
449 
450 
451 /**
452  * @brief Kinetis K6x Ethernet MAC timer handler
453  *
454  * This routine is periodically called by the TCP/IP stack to handle periodic
455  * operations such as polling the link state
456  *
457  * @param[in] interface Underlying network interface
458  **/
459 
460 void mk6xEthTick(NetInterface *interface)
461 {
462  //Valid Ethernet PHY or switch driver?
463  if(interface->phyDriver != NULL)
464  {
465  //Handle periodic operations
466  interface->phyDriver->tick(interface);
467  }
468  else if(interface->switchDriver != NULL)
469  {
470  //Handle periodic operations
471  interface->switchDriver->tick(interface);
472  }
473  else
474  {
475  //Just for sanity
476  }
477 }
478 
479 
480 /**
481  * @brief Enable interrupts
482  * @param[in] interface Underlying network interface
483  **/
484 
486 {
487  //Enable Ethernet MAC interrupts
488  NVIC_EnableIRQ(ENET_Transmit_IRQn);
489  NVIC_EnableIRQ(ENET_Receive_IRQn);
490  NVIC_EnableIRQ(ENET_Error_IRQn);
491 
492  //Valid Ethernet PHY or switch driver?
493  if(interface->phyDriver != NULL)
494  {
495  //Enable Ethernet PHY interrupts
496  interface->phyDriver->enableIrq(interface);
497  }
498  else if(interface->switchDriver != NULL)
499  {
500  //Enable Ethernet switch interrupts
501  interface->switchDriver->enableIrq(interface);
502  }
503  else
504  {
505  //Just for sanity
506  }
507 }
508 
509 
510 /**
511  * @brief Disable interrupts
512  * @param[in] interface Underlying network interface
513  **/
514 
516 {
517  //Disable Ethernet MAC interrupts
518  NVIC_DisableIRQ(ENET_Transmit_IRQn);
519  NVIC_DisableIRQ(ENET_Receive_IRQn);
520  NVIC_DisableIRQ(ENET_Error_IRQn);
521 
522  //Valid Ethernet PHY or switch driver?
523  if(interface->phyDriver != NULL)
524  {
525  //Disable Ethernet PHY interrupts
526  interface->phyDriver->disableIrq(interface);
527  }
528  else if(interface->switchDriver != NULL)
529  {
530  //Disable Ethernet switch interrupts
531  interface->switchDriver->disableIrq(interface);
532  }
533  else
534  {
535  //Just for sanity
536  }
537 }
538 
539 
540 /**
541  * @brief Ethernet MAC transmit interrupt
542  **/
543 
545 {
546  bool_t flag;
547 
548  //Interrupt service routine prologue
549  osEnterIsr();
550 
551  //This flag will be set if a higher priority task must be woken
552  flag = FALSE;
553 
554  //Packet transmitted?
555  if((ENET->EIR & ENET_EIR_TXF_MASK) != 0)
556  {
557  //Clear TXF interrupt flag
558  ENET->EIR = ENET_EIR_TXF_MASK;
559 
560  //Check whether the TX buffer is available for writing
561  if((txBufferDesc[txBufferIndex][0] & HTOBE16(ENET_TBD0_R)) == 0)
562  {
563  //Notify the TCP/IP stack that the transmitter is ready to send
564  flag = osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
565  }
566 
567  //Instruct the DMA to poll the transmit descriptor list
568  ENET->TDAR = ENET_TDAR_TDAR_MASK;
569  }
570 
571  //Interrupt service routine epilogue
572  osExitIsr(flag);
573 }
574 
575 
576 /**
577  * @brief Ethernet MAC receive interrupt
578  **/
579 
581 {
582  bool_t flag;
583 
584  //Interrupt service routine prologue
585  osEnterIsr();
586 
587  //This flag will be set if a higher priority task must be woken
588  flag = FALSE;
589 
590  //Packet received?
591  if((ENET->EIR & ENET_EIR_RXF_MASK) != 0)
592  {
593  //Disable RXF interrupt
594  ENET->EIMR &= ~ENET_EIMR_RXF_MASK;
595 
596  //Set event flag
597  nicDriverInterface->nicEvent = TRUE;
598  //Notify the TCP/IP stack of the event
599  flag = osSetEventFromIsr(&netEvent);
600  }
601 
602  //Interrupt service routine epilogue
603  osExitIsr(flag);
604 }
605 
606 
607 /**
608  * @brief Ethernet MAC error interrupt
609  **/
610 
612 {
613  bool_t flag;
614 
615  //Interrupt service routine prologue
616  osEnterIsr();
617 
618  //This flag will be set if a higher priority task must be woken
619  flag = FALSE;
620 
621  //System bus error?
622  if((ENET->EIR & ENET_EIR_EBERR_MASK) != 0)
623  {
624  //Disable EBERR interrupt
625  ENET->EIMR &= ~ENET_EIMR_EBERR_MASK;
626 
627  //Set event flag
628  nicDriverInterface->nicEvent = TRUE;
629  //Notify the TCP/IP stack of the event
630  flag |= osSetEventFromIsr(&netEvent);
631  }
632 
633  //Interrupt service routine epilogue
634  osExitIsr(flag);
635 }
636 
637 
638 /**
639  * @brief Kinetis K6x Ethernet MAC event handler
640  * @param[in] interface Underlying network interface
641  **/
642 
644 {
645  error_t error;
646  uint32_t status;
647 
648  //Read interrupt event register
649  status = ENET->EIR;
650 
651  //Packet received?
652  if((status & ENET_EIR_RXF_MASK) != 0)
653  {
654  //Clear RXF interrupt flag
655  ENET->EIR = ENET_EIR_RXF_MASK;
656 
657  //Process all pending packets
658  do
659  {
660  //Read incoming packet
661  error = mk6xEthReceivePacket(interface);
662 
663  //No more data in the receive buffer?
664  } while(error != ERROR_BUFFER_EMPTY);
665  }
666 
667  //System bus error?
668  if((status & ENET_EIR_EBERR_MASK) != 0)
669  {
670  //Clear EBERR interrupt flag
671  ENET->EIR = ENET_EIR_EBERR_MASK;
672 
673  //Disable Ethernet MAC
674  ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
675  //Reset buffer descriptors
676  mk6xEthInitBufferDesc(interface);
677  //Resume normal operation
678  ENET->ECR |= ENET_ECR_ETHEREN_MASK;
679  //Instruct the DMA to poll the receive descriptor list
680  ENET->RDAR = ENET_RDAR_RDAR_MASK;
681  }
682 
683  //Re-enable Ethernet MAC interrupts
684  ENET->EIMR = ENET_EIMR_TXF_MASK | ENET_EIMR_RXF_MASK | ENET_EIMR_EBERR_MASK;
685 }
686 
687 
688 /**
689  * @brief Send a packet
690  * @param[in] interface Underlying network interface
691  * @param[in] buffer Multi-part buffer containing the data to send
692  * @param[in] offset Offset to the first data byte
693  * @param[in] ancillary Additional options passed to the stack along with
694  * the packet
695  * @return Error code
696  **/
697 
699  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
700 {
701  size_t length;
702 
703  //Retrieve the length of the packet
704  length = netBufferGetLength(buffer) - offset;
705 
706  //Check the frame length
708  {
709  //The transmitter can accept another packet
710  osSetEvent(&interface->nicTxEvent);
711  //Report an error
712  return ERROR_INVALID_LENGTH;
713  }
714 
715  //Make sure the current buffer is available for writing
716  if(txBufferDesc[txBufferIndex][0] & HTOBE16(ENET_TBD0_R))
717  {
718  return ERROR_FAILURE;
719  }
720 
721  //Copy user data to the transmit buffer
722  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
723 
724  //Set frame length
725  txBufferDesc[txBufferIndex][1] = HTOBE16(length);
726  //Clear BDU flag
727  txBufferDesc[txBufferIndex][8] = 0;
728 
729  //Check current index
730  if(txBufferIndex < (MK6X_ETH_TX_BUFFER_COUNT - 1))
731  {
732  //Give the ownership of the descriptor to the DMA engine
733  txBufferDesc[txBufferIndex][0] = HTOBE16(ENET_TBD0_R |
735 
736  //Point to the next buffer
737  txBufferIndex++;
738  }
739  else
740  {
741  //Give the ownership of the descriptor to the DMA engine
742  txBufferDesc[txBufferIndex][0] = HTOBE16(ENET_TBD0_R |
744 
745  //Wrap around
746  txBufferIndex = 0;
747  }
748 
749  //Instruct the DMA to poll the transmit descriptor list
750  ENET->TDAR = ENET_TDAR_TDAR_MASK;
751 
752  //Check whether the next buffer is available for writing
753  if((txBufferDesc[txBufferIndex][0] & HTOBE16(ENET_TBD0_R)) == 0)
754  {
755  //The transmitter can accept another packet
756  osSetEvent(&interface->nicTxEvent);
757  }
758 
759  //Successful processing
760  return NO_ERROR;
761 }
762 
763 
764 /**
765  * @brief Receive a packet
766  * @param[in] interface Underlying network interface
767  * @return Error code
768  **/
769 
771 {
772  error_t error;
773  size_t n;
774  NetRxAncillary ancillary;
775 
776  //Current buffer available for reading?
777  if((rxBufferDesc[rxBufferIndex][0] & HTOBE16(ENET_RBD0_E)) == 0)
778  {
779  //The frame should not span multiple buffers
780  if((rxBufferDesc[rxBufferIndex][0] & HTOBE16(ENET_RBD0_L)) != 0)
781  {
782  //Check whether an error occurred
783  if((rxBufferDesc[rxBufferIndex][0] & HTOBE16(ENET_RBD0_LG |
785  {
786  //Retrieve the length of the frame
787  n = betoh16(rxBufferDesc[rxBufferIndex][1]);
788  //Limit the number of data to read
790 
791  //Additional options can be passed to the stack along with the packet
792  ancillary = NET_DEFAULT_RX_ANCILLARY;
793 
794  //Pass the packet to the upper layer
795  nicProcessPacket(interface, rxBuffer[rxBufferIndex], n, &ancillary);
796 
797  //Valid packet received
798  error = NO_ERROR;
799  }
800  else
801  {
802  //The received packet contains an error
803  error = ERROR_INVALID_PACKET;
804  }
805  }
806  else
807  {
808  //The packet is not valid
809  error = ERROR_INVALID_PACKET;
810  }
811 
812  //Clear BDU flag
813  rxBufferDesc[rxBufferIndex][8] = 0;
814 
815  //Check current index
816  if(rxBufferIndex < (MK6X_ETH_RX_BUFFER_COUNT - 1))
817  {
818  //Give the ownership of the descriptor back to the DMA engine
819  rxBufferDesc[rxBufferIndex][0] = HTOBE16(ENET_RBD0_E);
820  //Point to the next buffer
821  rxBufferIndex++;
822  }
823  else
824  {
825  //Give the ownership of the descriptor back to the DMA engine
826  rxBufferDesc[rxBufferIndex][0] = HTOBE16(ENET_RBD0_E | ENET_RBD0_W);
827  //Wrap around
828  rxBufferIndex = 0;
829  }
830 
831  //Instruct the DMA to poll the receive descriptor list
832  ENET->RDAR = ENET_RDAR_RDAR_MASK;
833  }
834  else
835  {
836  //No more data in the receive buffer
837  error = ERROR_BUFFER_EMPTY;
838  }
839 
840  //Return status code
841  return error;
842 }
843 
844 
845 /**
846  * @brief Configure MAC address filtering
847  * @param[in] interface Underlying network interface
848  * @return Error code
849  **/
850 
852 {
853  uint_t i;
854  uint_t k;
855  uint32_t crc;
856  uint32_t value;
857  uint32_t unicastHashTable[2];
858  uint32_t multicastHashTable[2];
859  MacFilterEntry *entry;
860 
861  //Debug message
862  TRACE_DEBUG("Updating MAC filter...\r\n");
863 
864  //Set the MAC address of the station (upper 16 bits)
865  value = interface->macAddr.b[5];
866  value |= (interface->macAddr.b[4] << 8);
867  ENET->PAUR = ENET_PAUR_PADDR2(value) | ENET_PAUR_TYPE(0x8808);
868 
869  //Set the MAC address of the station (lower 32 bits)
870  value = interface->macAddr.b[3];
871  value |= (interface->macAddr.b[2] << 8);
872  value |= (interface->macAddr.b[1] << 16);
873  value |= (interface->macAddr.b[0] << 24);
874  ENET->PALR = ENET_PALR_PADDR1(value);
875 
876  //Clear hash table (unicast address filtering)
877  unicastHashTable[0] = 0;
878  unicastHashTable[1] = 0;
879 
880  //Clear hash table (multicast address filtering)
881  multicastHashTable[0] = 0;
882  multicastHashTable[1] = 0;
883 
884  //The MAC address filter contains the list of MAC addresses to accept
885  //when receiving an Ethernet frame
886  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
887  {
888  //Point to the current entry
889  entry = &interface->macAddrFilter[i];
890 
891  //Valid entry?
892  if(entry->refCount > 0)
893  {
894  //Compute CRC over the current MAC address
895  crc = mk6xEthCalcCrc(&entry->addr, sizeof(MacAddr));
896 
897  //The upper 6 bits in the CRC register are used to index the
898  //contents of the hash table
899  k = (crc >> 26) & 0x3F;
900 
901  //Multicast address?
902  if(macIsMulticastAddr(&entry->addr))
903  {
904  //Update the multicast hash table
905  multicastHashTable[k / 32] |= (1 << (k % 32));
906  }
907  else
908  {
909  //Update the unicast hash table
910  unicastHashTable[k / 32] |= (1 << (k % 32));
911  }
912  }
913  }
914 
915  //Write the hash table (unicast address filtering)
916  ENET->IALR = unicastHashTable[0];
917  ENET->IAUR = unicastHashTable[1];
918 
919  //Write the hash table (multicast address filtering)
920  ENET->GALR = multicastHashTable[0];
921  ENET->GAUR = multicastHashTable[1];
922 
923  //Debug message
924  TRACE_DEBUG(" IALR = %08" PRIX32 "\r\n", ENET->IALR);
925  TRACE_DEBUG(" IAUR = %08" PRIX32 "\r\n", ENET->IAUR);
926  TRACE_DEBUG(" GALR = %08" PRIX32 "\r\n", ENET->GALR);
927  TRACE_DEBUG(" GAUR = %08" PRIX32 "\r\n", ENET->GAUR);
928 
929  //Successful processing
930  return NO_ERROR;
931 }
932 
933 
934 /**
935  * @brief Adjust MAC configuration parameters for proper operation
936  * @param[in] interface Underlying network interface
937  * @return Error code
938  **/
939 
941 {
942  //Disable Ethernet MAC while modifying configuration registers
943  ENET->ECR &= ~ENET_ECR_ETHEREN_MASK;
944 
945  //10BASE-T or 100BASE-TX operation mode?
946  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
947  {
948  //100 Mbps operation
949  ENET->RCR &= ~ENET_RCR_RMII_10T_MASK;
950  }
951  else
952  {
953  //10 Mbps operation
954  ENET->RCR |= ENET_RCR_RMII_10T_MASK;
955  }
956 
957  //Half-duplex or full-duplex mode?
958  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
959  {
960  //Full-duplex mode
961  ENET->TCR |= ENET_TCR_FDEN_MASK;
962  //Receive path operates independently of transmit
963  ENET->RCR &= ~ENET_RCR_DRT_MASK;
964  }
965  else
966  {
967  //Half-duplex mode
968  ENET->TCR &= ~ENET_TCR_FDEN_MASK;
969  //Disable reception of frames while transmitting
970  ENET->RCR |= ENET_RCR_DRT_MASK;
971  }
972 
973  //Reset buffer descriptors
974  mk6xEthInitBufferDesc(interface);
975 
976  //Re-enable Ethernet MAC
977  ENET->ECR |= ENET_ECR_ETHEREN_MASK;
978  //Instruct the DMA to poll the receive descriptor list
979  ENET->RDAR = ENET_RDAR_RDAR_MASK;
980 
981  //Successful processing
982  return NO_ERROR;
983 }
984 
985 
986 /**
987  * @brief Write PHY register
988  * @param[in] opcode Access type (2 bits)
989  * @param[in] phyAddr PHY address (5 bits)
990  * @param[in] regAddr Register address (5 bits)
991  * @param[in] data Register value
992  **/
993 
994 void mk6xEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
995  uint8_t regAddr, uint16_t data)
996 {
997  uint32_t temp;
998 
999  //Valid opcode?
1000  if(opcode == SMI_OPCODE_WRITE)
1001  {
1002  //Set up a write operation
1003  temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(1) | ENET_MMFR_TA(2);
1004  //PHY address
1005  temp |= ENET_MMFR_PA(phyAddr);
1006  //Register address
1007  temp |= ENET_MMFR_RA(regAddr);
1008  //Register value
1009  temp |= ENET_MMFR_DATA(data);
1010 
1011  //Clear MII interrupt flag
1012  ENET->EIR = ENET_EIR_MII_MASK;
1013  //Start a write operation
1014  ENET->MMFR = temp;
1015 
1016  //Wait for the write to complete
1017  while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
1018  {
1019  }
1020  }
1021  else
1022  {
1023  //The MAC peripheral only supports standard Clause 22 opcodes
1024  }
1025 }
1026 
1027 
1028 /**
1029  * @brief Read PHY register
1030  * @param[in] opcode Access type (2 bits)
1031  * @param[in] phyAddr PHY address (5 bits)
1032  * @param[in] regAddr Register address (5 bits)
1033  * @return Register value
1034  **/
1035 
1036 uint16_t mk6xEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
1037  uint8_t regAddr)
1038 {
1039  uint16_t data;
1040  uint32_t temp;
1041 
1042  //Valid opcode?
1043  if(opcode == SMI_OPCODE_READ)
1044  {
1045  //Set up a read operation
1046  temp = ENET_MMFR_ST(1) | ENET_MMFR_OP(2) | ENET_MMFR_TA(2);
1047  //PHY address
1048  temp |= ENET_MMFR_PA(phyAddr);
1049  //Register address
1050  temp |= ENET_MMFR_RA(regAddr);
1051 
1052  //Clear MII interrupt flag
1053  ENET->EIR = ENET_EIR_MII_MASK;
1054  //Start a read operation
1055  ENET->MMFR = temp;
1056 
1057  //Wait for the read to complete
1058  while((ENET->EIR & ENET_EIR_MII_MASK) == 0)
1059  {
1060  }
1061 
1062  //Get register value
1063  data = ENET->MMFR & ENET_MMFR_DATA_MASK;
1064  }
1065  else
1066  {
1067  //The MAC peripheral only supports standard Clause 22 opcodes
1068  data = 0;
1069  }
1070 
1071  //Return the value of the PHY register
1072  return data;
1073 }
1074 
1075 
1076 /**
1077  * @brief CRC calculation
1078  * @param[in] data Pointer to the data over which to calculate the CRC
1079  * @param[in] length Number of bytes to process
1080  * @return Resulting CRC value
1081  **/
1082 
1083 uint32_t mk6xEthCalcCrc(const void *data, size_t length)
1084 {
1085  uint_t i;
1086  uint_t j;
1087  uint32_t crc;
1088  const uint8_t *p;
1089 
1090  //Point to the data over which to calculate the CRC
1091  p = (uint8_t *) data;
1092  //CRC preset value
1093  crc = 0xFFFFFFFF;
1094 
1095  //Loop through data
1096  for(i = 0; i < length; i++)
1097  {
1098  //Update CRC value
1099  crc ^= p[i];
1100 
1101  //The message is processed bit by bit
1102  for(j = 0; j < 8; j++)
1103  {
1104  if((crc & 0x01) != 0)
1105  {
1106  crc = (crc >> 1) ^ 0xEDB88320;
1107  }
1108  else
1109  {
1110  crc = crc >> 1;
1111  }
1112  }
1113  }
1114 
1115  //Return CRC value
1116  return crc;
1117 }
#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
#define betoh16(value)
Definition: cpu_endian.h:453
#define HTOBE16(value)
Definition: cpu_endian.h:442
#define htobe16(value)
Definition: cpu_endian.h:445
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_TBD0_W
#define ENET_TBD0_R
#define ENET_RBD0_L
#define ENET_RBD0_NO
#define ENET_RBD0_W
#define ENET_RBD0_LG
#define ENET_RBD0_OV
#define ENET_TBD0_TC
#define ENET_RBD0_CR
#define ENET_TBD0_L
#define ENET_RBD0_TR
error_t mk6xEthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
__weak_func void mk6xEthInitGpio(NetInterface *interface)
GPIO configuration.
void mk6xEthTick(NetInterface *interface)
Kinetis K6x Ethernet MAC timer handler.
void ENET_Receive_IRQHandler(void)
Ethernet MAC receive interrupt.
void mk6xEthEnableIrq(NetInterface *interface)
Enable interrupts.
error_t mk6xEthInit(NetInterface *interface)
Kinetis K6x Ethernet MAC initialization.
void mk6xEthDisableIrq(NetInterface *interface)
Disable interrupts.
uint32_t mk6xEthCalcCrc(const void *data, size_t length)
CRC calculation.
void mk6xEthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
void ENET_Error_IRQHandler(void)
Ethernet MAC error interrupt.
error_t mk6xEthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
error_t mk6xEthReceivePacket(NetInterface *interface)
Receive a packet.
void mk6xEthInitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
void ENET_Transmit_IRQHandler(void)
Ethernet MAC transmit interrupt.
uint16_t mk6xEthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
const NicDriver mk6xEthDriver
Kinetis K6x Ethernet MAC driver.
void mk6xEthEventHandler(NetInterface *interface)
Kinetis K6x Ethernet MAC event handler.
error_t mk6xEthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
NXP Kinetis K60/K64/K65/K66 Ethernet MAC driver.
#define MK6X_ETH_IRQ_GROUP_PRIORITY
#define ENET_TBD4_INT
#define ENET_RBD4_INT
#define MK6X_ETH_TX_BUFFER_SIZE
#define MPU_CESR_VLD_MASK
#define MK6X_ETH_RX_BUFFER_SIZE
#define MPU
#define MK6X_ETH_IRQ_PRIORITY_GROUPING
#define MK6X_ETH_IRQ_SUB_PRIORITY
#define MK6X_ETH_RX_BUFFER_COUNT
#define MK6X_ETH_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 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