sama5d3_eth1_driver.c
Go to the documentation of this file.
1 /**
2  * @file sama5d3_eth1_driver.c
3  * @brief SAMA5D3 Ethernet MAC driver (EMAC instance)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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.5.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include <limits.h>
36 #include "sama5d3x.h"
37 #include "core/net.h"
39 #include "debug.h"
40 
41 //Underlying network interface
42 static NetInterface *nicDriverInterface;
43 
44 //IAR EWARM compiler?
45 #if defined(__ICCARM__)
46 
47 //TX buffer
48 #pragma data_alignment = 8
49 #pragma location = SAMA5D3_ETH1_RAM_SECTION
51 //RX buffer
52 #pragma data_alignment = 8
53 #pragma location = SAMA5D3_ETH1_RAM_SECTION
55 //TX buffer descriptors
56 #pragma data_alignment = 8
57 #pragma location = SAMA5D3_ETH1_RAM_SECTION
59 //RX buffer descriptors
60 #pragma data_alignment = 8
61 #pragma location = SAMA5D3_ETH1_RAM_SECTION
63 
64 //GCC compiler?
65 #else
66 
67 //TX buffer
69  __attribute__((aligned(8), __section__(SAMA5D3_ETH1_RAM_SECTION)));
70 //RX buffer
72  __attribute__((aligned(8), __section__(SAMA5D3_ETH1_RAM_SECTION)));
73 //TX buffer descriptors
75  __attribute__((aligned(8), __section__(SAMA5D3_ETH1_RAM_SECTION)));
76 //RX buffer descriptors
78  __attribute__((aligned(8), __section__(SAMA5D3_ETH1_RAM_SECTION)));
79 
80 #endif
81 
82 //TX buffer index
83 static uint_t txBufferIndex;
84 //RX buffer index
85 static uint_t rxBufferIndex;
86 
87 
88 /**
89  * @brief SAMA5D3 Ethernet MAC driver (EMAC instance)
90  **/
91 
93 {
95  ETH_MTU,
106  TRUE,
107  TRUE,
108  TRUE,
109  FALSE
110 };
111 
112 
113 /**
114  * @brief SAMA5D3 Ethernet MAC initialization
115  * @param[in] interface Underlying network interface
116  * @return Error code
117  **/
118 
120 {
121  error_t error;
122  volatile uint32_t status;
123 
124  //Debug message
125  TRACE_INFO("Initializing SAMA5D3 Ethernet MAC (EMAC)...\r\n");
126 
127  //Save underlying network interface
128  nicDriverInterface = interface;
129 
130  //Enable EMAC peripheral clock
131  PMC->PMC_PCER1 = (1 << (ID_EMAC - 32));
132  //Enable IRQ controller peripheral clock
133  PMC->PMC_PCER1 = (1 << (ID_IRQ - 32));
134 
135  //Disable transmit and receive circuits
136  EMAC->EMAC_NCR = 0;
137 
138  //GPIO configuration
139  sama5d3Eth1InitGpio(interface);
140 
141  //Configure MDC clock speed
142  EMAC->EMAC_NCFGR = EMAC_NCFGR_CLK_MCK_64;
143  //Enable management port (MDC and MDIO)
144  EMAC->EMAC_NCR |= EMAC_NCR_MPE;
145 
146  //Valid Ethernet PHY or switch driver?
147  if(interface->phyDriver != NULL)
148  {
149  //Ethernet PHY initialization
150  error = interface->phyDriver->init(interface);
151  }
152  else if(interface->switchDriver != NULL)
153  {
154  //Ethernet switch initialization
155  error = interface->switchDriver->init(interface);
156  }
157  else
158  {
159  //The interface is not properly configured
160  error = ERROR_FAILURE;
161  }
162 
163  //Any error to report?
164  if(error)
165  {
166  return error;
167  }
168 
169  //Set the MAC address of the station
170  EMAC->EMAC_SA[0].EMAC_SAxB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
171  EMAC->EMAC_SA[0].EMAC_SAxT = interface->macAddr.w[2];
172 
173  //The MAC supports 3 additional addresses for unicast perfect filtering
174  EMAC->EMAC_SA[1].EMAC_SAxB = 0;
175  EMAC->EMAC_SA[2].EMAC_SAxB = 0;
176  EMAC->EMAC_SA[3].EMAC_SAxB = 0;
177 
178  //Initialize hash table
179  EMAC->EMAC_HRB = 0;
180  EMAC->EMAC_HRT = 0;
181 
182  //Configure the receive filter
183  EMAC->EMAC_NCFGR |= EMAC_NCFGR_BIG | EMAC_NCFGR_MTI;
184 
185  //Initialize buffer descriptors
186  sama5d3Eth1InitBufferDesc(interface);
187 
188  //Clear transmit status register
189  EMAC->EMAC_TSR = EMAC_TSR_UND | EMAC_TSR_COMP | EMAC_TSR_BEX |
190  EMAC_TSR_TGO | EMAC_TSR_RLES | EMAC_TSR_COL | EMAC_TSR_UBR;
191 
192  //Clear receive status register
193  EMAC->EMAC_RSR = EMAC_RSR_OVR | EMAC_RSR_REC | EMAC_RSR_BNA;
194 
195  //First disable all EMAC interrupts
196  EMAC->EMAC_IDR = 0xFFFFFFFF;
197 
198  //Only the desired ones are enabled
199  EMAC->EMAC_IER = EMAC_IER_ROVR | EMAC_IER_TCOMP | EMAC_IER_TXERR |
200  EMAC_IER_RLE | EMAC_IER_TUND | EMAC_IER_RXUBR | EMAC_IER_RCOMP;
201 
202  //Read EMAC_ISR register to clear any pending interrupt
203  status = EMAC->EMAC_ISR;
204  (void) status;
205 
206  //Configure interrupt controller
207  AIC->AIC_SSR = ID_EMAC;
208  AIC->AIC_SMR = AIC_SMR_SRCTYPE_INT_LEVEL_SENSITIVE | AIC_SMR_PRIOR(SAMA5D3_ETH1_IRQ_PRIORITY);
209  AIC->AIC_SVR = (uint32_t) sama5d3Eth1IrqHandler;
210 
211  //Enable the EMAC to transmit and receive data
212  EMAC->EMAC_NCR |= EMAC_NCR_TE | EMAC_NCR_RE;
213 
214  //Accept any packets from the upper layer
215  osSetEvent(&interface->nicTxEvent);
216 
217  //Successful initialization
218  return NO_ERROR;
219 }
220 
221 
222 /**
223  * @brief GPIO configuration
224  * @param[in] interface Underlying network interface
225  **/
226 
227 __weak_func void sama5d3Eth1InitGpio(NetInterface *interface)
228 {
229 //SAMA5D3-Xplained or SAMA5D3-EDS evaluation board?
230 #if defined(USE_SAMA5D3_XPLAINED) || defined(USE_SAMA5D3_EDS)
231  uint32_t mask;
232 
233  //Enable PIO peripheral clock
234  PMC->PMC_PCER0 = (1 << ID_PIOC);
235 
236  //Configure RMII pins
237  mask = PIO_PC9A_EMDIO | PIO_PC8A_EMDC | PIO_PC7A_EREFCK | PIO_PC6A_ERXER |
238  PIO_PC5A_ECRSDV | PIO_PC4A_ETXEN | PIO_PC3A_ERX1 | PIO_PC2A_ERX0 |
239  PIO_PC1A_ETX1 | PIO_PC0A_ETX0;
240 
241  //Disable pull-up resistors on RMII pins
242  PIOC->PIO_PUDR = mask;
243  //Disable interrupts-on-change
244  PIOC->PIO_IDR = mask;
245  //Assign RMII pins to peripheral A function
246  PIOC->PIO_ABCDSR[0] &= ~mask;
247  PIOC->PIO_ABCDSR[1] &= ~mask;
248  //Disable the PIO from controlling the corresponding pins
249  PIOC->PIO_PDR = mask;
250 
251  //Select RMII operation mode and enable transceiver clock
252  EMAC->EMAC_USRIO = EMAC_USRIO_CLKEN | EMAC_USRIO_RMII;
253 #endif
254 }
255 
256 
257 /**
258  * @brief Initialize buffer descriptors
259  * @param[in] interface Underlying network interface
260  **/
261 
263 {
264  uint_t i;
265  uint32_t address;
266 
267  //Initialize TX buffer descriptors
268  for(i = 0; i < SAMA5D3_ETH1_TX_BUFFER_COUNT; i++)
269  {
270  //Calculate the address of the current TX buffer
271  address = (uint32_t) txBuffer[i];
272  //Write the address to the descriptor entry
273  txBufferDesc[i].address = address;
274  //Initialize status field
275  txBufferDesc[i].status = EMAC_TX_USED;
276  }
277 
278  //Mark the last descriptor entry with the wrap flag
279  txBufferDesc[i - 1].status |= EMAC_TX_WRAP;
280  //Initialize TX buffer index
281  txBufferIndex = 0;
282 
283  //Initialize RX buffer descriptors
284  for(i = 0; i < SAMA5D3_ETH1_RX_BUFFER_COUNT; i++)
285  {
286  //Calculate the address of the current RX buffer
287  address = (uint32_t) rxBuffer[i];
288  //Write the address to the descriptor entry
289  rxBufferDesc[i].address = address & EMAC_RX_ADDRESS;
290  //Clear status field
291  rxBufferDesc[i].status = 0;
292  }
293 
294  //Mark the last descriptor entry with the wrap flag
295  rxBufferDesc[i - 1].address |= EMAC_RX_WRAP;
296  //Initialize RX buffer index
297  rxBufferIndex = 0;
298 
299  //Start location of the TX descriptor list
300  EMAC->EMAC_TBQP = (uint32_t) txBufferDesc;
301  //Start location of the RX descriptor list
302  EMAC->EMAC_RBQP = (uint32_t) rxBufferDesc;
303 }
304 
305 
306 /**
307  * @brief SAMA5D3 Ethernet MAC timer handler
308  *
309  * This routine is periodically called by the TCP/IP stack to handle periodic
310  * operations such as polling the link state
311  *
312  * @param[in] interface Underlying network interface
313  **/
314 
316 {
317  //Valid Ethernet PHY or switch driver?
318  if(interface->phyDriver != NULL)
319  {
320  //Handle periodic operations
321  interface->phyDriver->tick(interface);
322  }
323  else if(interface->switchDriver != NULL)
324  {
325  //Handle periodic operations
326  interface->switchDriver->tick(interface);
327  }
328  else
329  {
330  //Just for sanity
331  }
332 }
333 
334 
335 /**
336  * @brief Enable interrupts
337  * @param[in] interface Underlying network interface
338  **/
339 
341 {
342  //Enable Ethernet MAC interrupts
343  AIC->AIC_SSR = ID_EMAC;
344  AIC->AIC_IECR = AIC_IECR_INTEN;
345 
346  //Valid Ethernet PHY or switch driver?
347  if(interface->phyDriver != NULL)
348  {
349  //Enable Ethernet PHY interrupts
350  interface->phyDriver->enableIrq(interface);
351  }
352  else if(interface->switchDriver != NULL)
353  {
354  //Enable Ethernet switch interrupts
355  interface->switchDriver->enableIrq(interface);
356  }
357  else
358  {
359  //Just for sanity
360  }
361 }
362 
363 
364 /**
365  * @brief Disable interrupts
366  * @param[in] interface Underlying network interface
367  **/
368 
370 {
371  //Disable Ethernet MAC interrupts
372  AIC->AIC_SSR = ID_EMAC;
373  AIC->AIC_IDCR = AIC_IDCR_INTD;
374 
375  //Valid Ethernet PHY or switch driver?
376  if(interface->phyDriver != NULL)
377  {
378  //Disable Ethernet PHY interrupts
379  interface->phyDriver->disableIrq(interface);
380  }
381  else if(interface->switchDriver != NULL)
382  {
383  //Disable Ethernet switch interrupts
384  interface->switchDriver->disableIrq(interface);
385  }
386  else
387  {
388  //Just for sanity
389  }
390 }
391 
392 
393 /**
394  * @brief SAMA5D3 Ethernet MAC interrupt service routine
395  **/
396 
398 {
399  bool_t flag;
400  volatile uint32_t isr;
401  volatile uint32_t tsr;
402  volatile uint32_t rsr;
403 
404  //Interrupt service routine prologue
405  osEnterIsr();
406 
407  //This flag will be set if a higher priority task must be woken
408  flag = FALSE;
409 
410  //Each time the software reads EMAC_ISR, it has to check the contents
411  //of EMAC_TSR, EMAC_RSR and EMAC_NSR
412  isr = EMAC->EMAC_ISR;
413  tsr = EMAC->EMAC_TSR;
414  rsr = EMAC->EMAC_RSR;
415  (void) isr;
416 
417  //Packet transmitted?
418  if((tsr & (EMAC_TSR_UND | EMAC_TSR_COMP | EMAC_TSR_BEX |
419  EMAC_TSR_TGO | EMAC_TSR_RLES | EMAC_TSR_COL | EMAC_TSR_UBR)) != 0)
420  {
421  //Only clear TSR flags that are currently set
422  EMAC->EMAC_TSR = tsr;
423 
424  //Check whether the TX buffer is available for writing
425  if((txBufferDesc[txBufferIndex].status & EMAC_TX_USED) != 0)
426  {
427  //Notify the TCP/IP stack that the transmitter is ready to send
428  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
429  }
430  }
431 
432  //Packet received?
433  if((rsr & (EMAC_RSR_OVR | EMAC_RSR_REC | EMAC_RSR_BNA)) != 0)
434  {
435  //Set event flag
436  nicDriverInterface->nicEvent = TRUE;
437  //Notify the TCP/IP stack of the event
438  flag |= osSetEventFromIsr(&netEvent);
439  }
440 
441  //Write AIC_EOICR register before exiting
442  AIC->AIC_EOICR = 0;
443 
444  //Interrupt service routine epilogue
445  osExitIsr(flag);
446 }
447 
448 
449 /**
450  * @brief SAMA5D3 Ethernet MAC event handler
451  * @param[in] interface Underlying network interface
452  **/
453 
455 {
456  error_t error;
457  uint32_t rsr;
458 
459  //Read receive status
460  rsr = EMAC->EMAC_RSR;
461 
462  //Packet received?
463  if((rsr & (EMAC_RSR_OVR | EMAC_RSR_REC | EMAC_RSR_BNA)) != 0)
464  {
465  //Only clear RSR flags that are currently set
466  EMAC->EMAC_RSR = rsr;
467 
468  //Process all pending packets
469  do
470  {
471  //Read incoming packet
472  error = sama5d3Eth1ReceivePacket(interface);
473 
474  //No more data in the receive buffer?
475  } while(error != ERROR_BUFFER_EMPTY);
476  }
477 }
478 
479 
480 /**
481  * @brief Send a packet
482  * @param[in] interface Underlying network interface
483  * @param[in] buffer Multi-part buffer containing the data to send
484  * @param[in] offset Offset to the first data byte
485  * @param[in] ancillary Additional options passed to the stack along with
486  * the packet
487  * @return Error code
488  **/
489 
491  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
492 {
493  size_t length;
494 
495  //Retrieve the length of the packet
496  length = netBufferGetLength(buffer) - offset;
497 
498  //Check the frame length
500  {
501  //The transmitter can accept another packet
502  osSetEvent(&interface->nicTxEvent);
503  //Report an error
504  return ERROR_INVALID_LENGTH;
505  }
506 
507  //Make sure the current buffer is available for writing
508  if((txBufferDesc[txBufferIndex].status & EMAC_TX_USED) == 0)
509  {
510  return ERROR_FAILURE;
511  }
512 
513  //Copy user data to the transmit buffer
514  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
515 
516  //Set the necessary flags in the descriptor entry
517  if(txBufferIndex < (SAMA5D3_ETH1_TX_BUFFER_COUNT - 1))
518  {
519  //Write the status word
520  txBufferDesc[txBufferIndex].status = EMAC_TX_LAST |
522 
523  //Point to the next buffer
524  txBufferIndex++;
525  }
526  else
527  {
528  //Write the status word
529  txBufferDesc[txBufferIndex].status = EMAC_TX_WRAP | EMAC_TX_LAST |
531 
532  //Wrap around
533  txBufferIndex = 0;
534  }
535 
536  //Set the TSTART bit to initiate transmission
537  EMAC->EMAC_NCR |= EMAC_NCR_TSTART;
538 
539  //Check whether the next buffer is available for writing
540  if((txBufferDesc[txBufferIndex].status & EMAC_TX_USED) != 0)
541  {
542  //The transmitter can accept another packet
543  osSetEvent(&interface->nicTxEvent);
544  }
545 
546  //Successful processing
547  return NO_ERROR;
548 }
549 
550 
551 /**
552  * @brief Receive a packet
553  * @param[in] interface Underlying network interface
554  * @return Error code
555  **/
556 
558 {
559  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
560  error_t error;
561  uint_t i;
562  uint_t j;
563  uint_t sofIndex;
564  uint_t eofIndex;
565  size_t n;
566  size_t size;
567  size_t length;
568 
569  //Initialize variables
570  size = 0;
571  sofIndex = UINT_MAX;
572  eofIndex = UINT_MAX;
573 
574  //Search for SOF and EOF flags
575  for(i = 0; i < SAMA5D3_ETH1_RX_BUFFER_COUNT; i++)
576  {
577  //Point to the current entry
578  j = rxBufferIndex + i;
579 
580  //Wrap around to the beginning of the buffer if necessary
582  {
584  }
585 
586  //No more entries to process?
587  if((rxBufferDesc[j].address & EMAC_RX_OWNERSHIP) == 0)
588  {
589  //Stop processing
590  break;
591  }
592 
593  //A valid SOF has been found?
594  if((rxBufferDesc[j].status & EMAC_RX_SOF) != 0)
595  {
596  //Save the position of the SOF
597  sofIndex = i;
598  }
599 
600  //A valid EOF has been found?
601  if((rxBufferDesc[j].status & EMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
602  {
603  //Save the position of the EOF
604  eofIndex = i;
605  //Retrieve the length of the frame
606  size = rxBufferDesc[j].status & EMAC_RX_LENGTH;
607  //Limit the number of data to read
608  size = MIN(size, ETH_MAX_FRAME_SIZE);
609  //Stop processing since we have reached the end of the frame
610  break;
611  }
612  }
613 
614  //Determine the number of entries to process
615  if(eofIndex != UINT_MAX)
616  {
617  j = eofIndex + 1;
618  }
619  else if(sofIndex != UINT_MAX)
620  {
621  j = sofIndex;
622  }
623  else
624  {
625  j = i;
626  }
627 
628  //Total number of bytes that have been copied from the receive buffer
629  length = 0;
630 
631  //Process incoming frame
632  for(i = 0; i < j; i++)
633  {
634  //Any data to copy from current buffer?
635  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
636  {
637  //Calculate the number of bytes to read at a time
639  //Copy data from receive buffer
640  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
641  //Update byte counters
642  length += n;
643  size -= n;
644  }
645 
646  //Mark the current buffer as free
647  rxBufferDesc[rxBufferIndex].address &= ~EMAC_RX_OWNERSHIP;
648 
649  //Point to the following entry
650  rxBufferIndex++;
651 
652  //Wrap around to the beginning of the buffer if necessary
653  if(rxBufferIndex >= SAMA5D3_ETH1_RX_BUFFER_COUNT)
654  {
655  rxBufferIndex = 0;
656  }
657  }
658 
659  //Any packet to process?
660  if(length > 0)
661  {
662  NetRxAncillary ancillary;
663 
664  //Additional options can be passed to the stack along with the packet
665  ancillary = NET_DEFAULT_RX_ANCILLARY;
666 
667  //Pass the packet to the upper layer
668  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
669  //Valid packet received
670  error = NO_ERROR;
671  }
672  else
673  {
674  //No more data in the receive buffer
675  error = ERROR_BUFFER_EMPTY;
676  }
677 
678  //Return status code
679  return error;
680 }
681 
682 
683 /**
684  * @brief Configure MAC address filtering
685  * @param[in] interface Underlying network interface
686  * @return Error code
687  **/
688 
690 {
691  uint_t i;
692  uint_t j;
693  uint_t k;
694  uint8_t *p;
695  uint32_t hashTable[2];
696  MacAddr unicastMacAddr[3];
697  MacFilterEntry *entry;
698 
699  //Debug message
700  TRACE_DEBUG("Updating MAC filter...\r\n");
701 
702  //Set the MAC address of the station
703  EMAC->EMAC_SA[0].EMAC_SAxB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
704  EMAC->EMAC_SA[0].EMAC_SAxT = interface->macAddr.w[2];
705 
706  //The MAC supports 3 additional addresses for unicast perfect filtering
707  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
708  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
709  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
710 
711  //The hash table is used for multicast address filtering
712  hashTable[0] = 0;
713  hashTable[1] = 0;
714 
715  //The MAC address filter contains the list of MAC addresses to accept
716  //when receiving an Ethernet frame
717  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
718  {
719  //Point to the current entry
720  entry = &interface->macAddrFilter[i];
721 
722  //Valid entry?
723  if(entry->refCount > 0)
724  {
725  //Multicast address?
726  if(macIsMulticastAddr(&entry->addr))
727  {
728  //Point to the MAC address
729  p = entry->addr.b;
730 
731  //Apply the hash function
732  k = (p[0] >> 6) ^ p[0];
733  k ^= (p[1] >> 4) ^ (p[1] << 2);
734  k ^= (p[2] >> 2) ^ (p[2] << 4);
735  k ^= (p[3] >> 6) ^ p[3];
736  k ^= (p[4] >> 4) ^ (p[4] << 2);
737  k ^= (p[5] >> 2) ^ (p[5] << 4);
738 
739  //The hash value is reduced to a 6-bit index
740  k &= 0x3F;
741 
742  //Update hash table contents
743  hashTable[k / 32] |= (1 << (k % 32));
744  }
745  else
746  {
747  //Up to 3 additional MAC addresses can be specified
748  if(j < 3)
749  {
750  //Save the unicast address
751  unicastMacAddr[j] = entry->addr;
752  }
753  else
754  {
755  //Point to the MAC address
756  p = entry->addr.b;
757 
758  //Apply the hash function
759  k = (p[0] >> 6) ^ p[0];
760  k ^= (p[1] >> 4) ^ (p[1] << 2);
761  k ^= (p[2] >> 2) ^ (p[2] << 4);
762  k ^= (p[3] >> 6) ^ p[3];
763  k ^= (p[4] >> 4) ^ (p[4] << 2);
764  k ^= (p[5] >> 2) ^ (p[5] << 4);
765 
766  //The hash value is reduced to a 6-bit index
767  k &= 0x3F;
768 
769  //Update hash table contents
770  hashTable[k / 32] |= (1 << (k % 32));
771  }
772 
773  //Increment the number of unicast addresses
774  j++;
775  }
776  }
777  }
778 
779  //Configure the first unicast address filter
780  if(j >= 1)
781  {
782  //The address is activated when SAT register is written
783  EMAC->EMAC_SA[1].EMAC_SAxB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
784  EMAC->EMAC_SA[1].EMAC_SAxT = unicastMacAddr[0].w[2];
785  }
786  else
787  {
788  //The address is deactivated when SAB register is written
789  EMAC->EMAC_SA[1].EMAC_SAxB = 0;
790  }
791 
792  //Configure the second unicast address filter
793  if(j >= 2)
794  {
795  //The address is activated when SAT register is written
796  EMAC->EMAC_SA[2].EMAC_SAxB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
797  EMAC->EMAC_SA[2].EMAC_SAxT = unicastMacAddr[1].w[2];
798  }
799  else
800  {
801  //The address is deactivated when SAB register is written
802  EMAC->EMAC_SA[2].EMAC_SAxB = 0;
803  }
804 
805  //Configure the third unicast address filter
806  if(j >= 3)
807  {
808  //The address is activated when SAT register is written
809  EMAC->EMAC_SA[3].EMAC_SAxB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
810  EMAC->EMAC_SA[3].EMAC_SAxT = unicastMacAddr[2].w[2];
811  }
812  else
813  {
814  //The address is deactivated when SAB register is written
815  EMAC->EMAC_SA[3].EMAC_SAxB = 0;
816  }
817 
818  //The perfect MAC filter supports only 3 unicast addresses
819  if(j >= 4)
820  {
821  EMAC->EMAC_NCFGR |= EMAC_NCFGR_UNI;
822  }
823  else
824  {
825  EMAC->EMAC_NCFGR &= ~EMAC_NCFGR_UNI;
826  }
827 
828  //Configure the multicast hash table
829  EMAC->EMAC_HRB = hashTable[0];
830  EMAC->EMAC_HRT = hashTable[1];
831 
832  //Debug message
833  TRACE_DEBUG(" HRB = %08" PRIX32 "\r\n", EMAC->EMAC_HRB);
834  TRACE_DEBUG(" HRT = %08" PRIX32 "\r\n", EMAC->EMAC_HRT);
835 
836  //Successful processing
837  return NO_ERROR;
838 }
839 
840 
841 /**
842  * @brief Adjust MAC configuration parameters for proper operation
843  * @param[in] interface Underlying network interface
844  * @return Error code
845  **/
846 
848 {
849  uint32_t config;
850 
851  //Read network configuration register
852  config = EMAC->EMAC_NCFGR;
853 
854  //10BASE-T or 100BASE-TX operation mode?
855  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
856  {
857  config |= EMAC_NCFGR_SPD;
858  }
859  else
860  {
861  config &= ~EMAC_NCFGR_SPD;
862  }
863 
864  //Half-duplex or full-duplex mode?
865  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
866  {
867  config |= EMAC_NCFGR_FD;
868  }
869  else
870  {
871  config &= ~EMAC_NCFGR_FD;
872  }
873 
874  //Write configuration value back to NCFGR register
875  EMAC->EMAC_NCFGR = config;
876 
877  //Successful processing
878  return NO_ERROR;
879 }
880 
881 
882 /**
883  * @brief Write PHY register
884  * @param[in] opcode Access type (2 bits)
885  * @param[in] phyAddr PHY address (5 bits)
886  * @param[in] regAddr Register address (5 bits)
887  * @param[in] data Register value
888  **/
889 
890 void sama5d3Eth1WritePhyReg(uint8_t opcode, uint8_t phyAddr,
891  uint8_t regAddr, uint16_t data)
892 {
893  uint32_t temp;
894 
895  //Valid opcode?
896  if(opcode == SMI_OPCODE_WRITE)
897  {
898  //Set up a write operation
899  temp = EMAC_MAN_SOF(1) | EMAC_MAN_RW(1) | EMAC_MAN_CODE(2);
900  //PHY address
901  temp |= EMAC_MAN_PHYA(phyAddr);
902  //Register address
903  temp |= EMAC_MAN_REGA(regAddr);
904  //Register value
905  temp |= EMAC_MAN_DATA(data);
906 
907  //Start a write operation
908  EMAC->EMAC_MAN = temp;
909  //Wait for the write to complete
910  while((EMAC->EMAC_NSR & EMAC_NSR_IDLE) == 0)
911  {
912  }
913  }
914  else
915  {
916  //The MAC peripheral only supports standard Clause 22 opcodes
917  }
918 }
919 
920 
921 /**
922  * @brief Read PHY register
923  * @param[in] opcode Access type (2 bits)
924  * @param[in] phyAddr PHY address (5 bits)
925  * @param[in] regAddr Register address (5 bits)
926  * @return Register value
927  **/
928 
929 uint16_t sama5d3Eth1ReadPhyReg(uint8_t opcode, uint8_t phyAddr,
930  uint8_t regAddr)
931 {
932  uint16_t data;
933  uint32_t temp;
934 
935  //Valid opcode?
936  if(opcode == SMI_OPCODE_READ)
937  {
938  //Set up a read operation
939  temp = EMAC_MAN_SOF(1) | EMAC_MAN_RW(2) | EMAC_MAN_CODE(2);
940  //PHY address
941  temp |= EMAC_MAN_PHYA(phyAddr);
942  //Register address
943  temp |= EMAC_MAN_REGA(regAddr);
944 
945  //Start a read operation
946  EMAC->EMAC_MAN = temp;
947  //Wait for the read to complete
948  while((EMAC->EMAC_NSR & EMAC_NSR_IDLE) == 0)
949  {
950  }
951 
952  //Get register value
953  data = EMAC->EMAC_MAN & EMAC_MAN_DATA_Msk;
954  }
955  else
956  {
957  //The MAC peripheral only supports standard Clause 22 opcodes
958  data = 0;
959  }
960 
961  //Return the value of the PHY register
962  return data;
963 }
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
int bool_t
Definition: compiler_port.h:61
#define netEvent
Definition: net_legacy.h:196
error_t sama5d3Eth1UpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
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
error_t sama5d3Eth1SendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
uint8_t p
Definition: ndp.h:300
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define EMAC_RX_WRAP
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:222
#define ETH_MAX_FRAME_SIZE
Definition: ethernet.h:110
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
void sama5d3Eth1IrqHandler(void)
SAMA5D3 Ethernet MAC interrupt service routine.
#define EMAC_RX_EOF
SAMA5D3 Ethernet MAC driver (EMAC instance)
#define EMAC_RX_OWNERSHIP
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:392
#define macIsMulticastAddr(macAddr)
Definition: ethernet.h:133
#define SAMA5D3_ETH1_RAM_SECTION
#define osExitIsr(flag)
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define EMAC_RX_LENGTH
void sama5d3Eth1EnableIrq(NetInterface *interface)
Enable interrupts.
#define FALSE
Definition: os_port.h:46
void sama5d3Eth1EventHandler(NetInterface *interface)
SAMA5D3 Ethernet MAC event handler.
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
#define EMAC_TX_LENGTH
error_t
Error codes.
Definition: error.h:43
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:105
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
void sama5d3Eth1WritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
#define txBuffer
error_t sama5d3Eth1ReceivePacket(NetInterface *interface)
Receive a packet.
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:36
MacAddr addr
MAC address.
Definition: ethernet.h:263
#define SAMA5D3_ETH1_RX_BUFFER_SIZE
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_BUFFER_EMPTY
Definition: error.h:142
#define NetTxAncillary
Definition: net_misc.h:36
uint8_t mask
Definition: web_socket.h:319
#define SMI_OPCODE_READ
Definition: nic.h:67
#define SAMA5D3_ETH1_TX_BUFFER_SIZE
#define TRACE_INFO(...)
Definition: debug.h:105
void sama5d3Eth1InitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
uint8_t length
Definition: tcp.h:375
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:63
void sama5d3Eth1DisableIrq(NetInterface *interface)
Disable interrupts.
#define rxBuffer
MacAddr
Definition: ethernet.h:195
const NicDriver sama5d3Eth1Driver
SAMA5D3 Ethernet MAC driver (EMAC instance)
error_t sama5d3Eth1UpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define SAMA5D3_ETH1_IRQ_PRIORITY
#define TRACE_DEBUG(...)
Definition: debug.h:119
__weak_func void sama5d3Eth1InitGpio(NetInterface *interface)
GPIO configuration.
uint16_t regAddr
#define EMAC_TX_USED
#define ETH_MTU
Definition: ethernet.h:116
uint8_t n
MAC filter table entry.
Definition: ethernet.h:262
Ipv6Addr address[]
Definition: ipv6.h:325
void sama5d3Eth1Tick(NetInterface *interface)
SAMA5D3 Ethernet MAC timer handler.
Transmit buffer descriptor.
#define osEnterIsr()
#define EMAC_RX_ADDRESS
#define EMAC_TX_WRAP
#define EMAC_RX_SOF
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
uint16_t sama5d3Eth1ReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
Receive buffer descriptor.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
NIC driver.
Definition: nic.h:286
#define SAMA5D3_ETH1_TX_BUFFER_COUNT
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
@ NO_ERROR
Success.
Definition: error.h:44
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
Debugging facilities.
#define SAMA5D3_ETH1_RX_BUFFER_COUNT
error_t sama5d3Eth1Init(NetInterface *interface)
SAMA5D3 Ethernet MAC initialization.
#define EMAC_TX_LAST
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83