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