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-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 <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  //Enable PIO peripheral clock
232  PMC->PMC_PCER0 = (1 << ID_PIOC);
233 
234  //Disable pull-up resistors on RMII pins
235  PIOC->PIO_PUDR = EMAC_RMII_MASK;
236  //Disable interrupts-on-change
237  PIOC->PIO_IDR = EMAC_RMII_MASK;
238  //Assign RMII pins to peripheral A function
239  PIOC->PIO_ABCDSR[0] &= ~EMAC_RMII_MASK;
240  PIOC->PIO_ABCDSR[1] &= ~EMAC_RMII_MASK;
241  //Disable the PIO from controlling the corresponding pins
242  PIOC->PIO_PDR = EMAC_RMII_MASK;
243 
244  //Select RMII operation mode and enable transceiver clock
245  EMAC->EMAC_USRIO = EMAC_USRIO_CLKEN | EMAC_USRIO_RMII;
246 #endif
247 }
248 
249 
250 /**
251  * @brief Initialize buffer descriptors
252  * @param[in] interface Underlying network interface
253  **/
254 
256 {
257  uint_t i;
258  uint32_t address;
259 
260  //Initialize TX buffer descriptors
261  for(i = 0; i < SAMA5D3_ETH1_TX_BUFFER_COUNT; i++)
262  {
263  //Calculate the address of the current TX buffer
264  address = (uint32_t) txBuffer[i];
265  //Write the address to the descriptor entry
266  txBufferDesc[i].address = address;
267  //Initialize status field
268  txBufferDesc[i].status = EMAC_TX_USED;
269  }
270 
271  //Mark the last descriptor entry with the wrap flag
272  txBufferDesc[i - 1].status |= EMAC_TX_WRAP;
273  //Initialize TX buffer index
274  txBufferIndex = 0;
275 
276  //Initialize RX buffer descriptors
277  for(i = 0; i < SAMA5D3_ETH1_RX_BUFFER_COUNT; i++)
278  {
279  //Calculate the address of the current RX buffer
280  address = (uint32_t) rxBuffer[i];
281  //Write the address to the descriptor entry
282  rxBufferDesc[i].address = address & EMAC_RX_ADDRESS;
283  //Clear status field
284  rxBufferDesc[i].status = 0;
285  }
286 
287  //Mark the last descriptor entry with the wrap flag
288  rxBufferDesc[i - 1].address |= EMAC_RX_WRAP;
289  //Initialize RX buffer index
290  rxBufferIndex = 0;
291 
292  //Start location of the TX descriptor list
293  EMAC->EMAC_TBQP = (uint32_t) txBufferDesc;
294  //Start location of the RX descriptor list
295  EMAC->EMAC_RBQP = (uint32_t) rxBufferDesc;
296 }
297 
298 
299 /**
300  * @brief SAMA5D3 Ethernet MAC timer handler
301  *
302  * This routine is periodically called by the TCP/IP stack to handle periodic
303  * operations such as polling the link state
304  *
305  * @param[in] interface Underlying network interface
306  **/
307 
309 {
310  //Valid Ethernet PHY or switch driver?
311  if(interface->phyDriver != NULL)
312  {
313  //Handle periodic operations
314  interface->phyDriver->tick(interface);
315  }
316  else if(interface->switchDriver != NULL)
317  {
318  //Handle periodic operations
319  interface->switchDriver->tick(interface);
320  }
321  else
322  {
323  //Just for sanity
324  }
325 }
326 
327 
328 /**
329  * @brief Enable interrupts
330  * @param[in] interface Underlying network interface
331  **/
332 
334 {
335  //Enable Ethernet MAC interrupts
336  AIC->AIC_SSR = ID_EMAC;
337  AIC->AIC_IECR = AIC_IECR_INTEN;
338 
339  //Valid Ethernet PHY or switch driver?
340  if(interface->phyDriver != NULL)
341  {
342  //Enable Ethernet PHY interrupts
343  interface->phyDriver->enableIrq(interface);
344  }
345  else if(interface->switchDriver != NULL)
346  {
347  //Enable Ethernet switch interrupts
348  interface->switchDriver->enableIrq(interface);
349  }
350  else
351  {
352  //Just for sanity
353  }
354 }
355 
356 
357 /**
358  * @brief Disable interrupts
359  * @param[in] interface Underlying network interface
360  **/
361 
363 {
364  //Disable Ethernet MAC interrupts
365  AIC->AIC_SSR = ID_EMAC;
366  AIC->AIC_IDCR = AIC_IDCR_INTD;
367 
368  //Valid Ethernet PHY or switch driver?
369  if(interface->phyDriver != NULL)
370  {
371  //Disable Ethernet PHY interrupts
372  interface->phyDriver->disableIrq(interface);
373  }
374  else if(interface->switchDriver != NULL)
375  {
376  //Disable Ethernet switch interrupts
377  interface->switchDriver->disableIrq(interface);
378  }
379  else
380  {
381  //Just for sanity
382  }
383 }
384 
385 
386 /**
387  * @brief SAMA5D3 Ethernet MAC interrupt service routine
388  **/
389 
391 {
392  bool_t flag;
393  volatile uint32_t isr;
394  volatile uint32_t tsr;
395  volatile uint32_t rsr;
396 
397  //Interrupt service routine prologue
398  osEnterIsr();
399 
400  //This flag will be set if a higher priority task must be woken
401  flag = FALSE;
402 
403  //Each time the software reads EMAC_ISR, it has to check the contents
404  //of EMAC_TSR, EMAC_RSR and EMAC_NSR
405  isr = EMAC->EMAC_ISR;
406  tsr = EMAC->EMAC_TSR;
407  rsr = EMAC->EMAC_RSR;
408  (void) isr;
409 
410  //Packet transmitted?
411  if((tsr & (EMAC_TSR_UND | EMAC_TSR_COMP | EMAC_TSR_BEX |
412  EMAC_TSR_TGO | EMAC_TSR_RLES | EMAC_TSR_COL | EMAC_TSR_UBR)) != 0)
413  {
414  //Only clear TSR flags that are currently set
415  EMAC->EMAC_TSR = tsr;
416 
417  //Check whether the TX buffer is available for writing
418  if((txBufferDesc[txBufferIndex].status & EMAC_TX_USED) != 0)
419  {
420  //Notify the TCP/IP stack that the transmitter is ready to send
421  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
422  }
423  }
424 
425  //Packet received?
426  if((rsr & (EMAC_RSR_OVR | EMAC_RSR_REC | EMAC_RSR_BNA)) != 0)
427  {
428  //Set event flag
429  nicDriverInterface->nicEvent = TRUE;
430  //Notify the TCP/IP stack of the event
431  flag |= osSetEventFromIsr(&netEvent);
432  }
433 
434  //Write AIC_EOICR register before exiting
435  AIC->AIC_EOICR = 0;
436 
437  //Interrupt service routine epilogue
438  osExitIsr(flag);
439 }
440 
441 
442 /**
443  * @brief SAMA5D3 Ethernet MAC event handler
444  * @param[in] interface Underlying network interface
445  **/
446 
448 {
449  error_t error;
450  uint32_t rsr;
451 
452  //Read receive status
453  rsr = EMAC->EMAC_RSR;
454 
455  //Packet received?
456  if((rsr & (EMAC_RSR_OVR | EMAC_RSR_REC | EMAC_RSR_BNA)) != 0)
457  {
458  //Only clear RSR flags that are currently set
459  EMAC->EMAC_RSR = rsr;
460 
461  //Process all pending packets
462  do
463  {
464  //Read incoming packet
465  error = sama5d3Eth1ReceivePacket(interface);
466 
467  //No more data in the receive buffer?
468  } while(error != ERROR_BUFFER_EMPTY);
469  }
470 }
471 
472 
473 /**
474  * @brief Send a packet
475  * @param[in] interface Underlying network interface
476  * @param[in] buffer Multi-part buffer containing the data to send
477  * @param[in] offset Offset to the first data byte
478  * @param[in] ancillary Additional options passed to the stack along with
479  * the packet
480  * @return Error code
481  **/
482 
484  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
485 {
486  size_t length;
487 
488  //Retrieve the length of the packet
489  length = netBufferGetLength(buffer) - offset;
490 
491  //Check the frame length
493  {
494  //The transmitter can accept another packet
495  osSetEvent(&interface->nicTxEvent);
496  //Report an error
497  return ERROR_INVALID_LENGTH;
498  }
499 
500  //Make sure the current buffer is available for writing
501  if((txBufferDesc[txBufferIndex].status & EMAC_TX_USED) == 0)
502  {
503  return ERROR_FAILURE;
504  }
505 
506  //Copy user data to the transmit buffer
507  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
508 
509  //Set the necessary flags in the descriptor entry
510  if(txBufferIndex < (SAMA5D3_ETH1_TX_BUFFER_COUNT - 1))
511  {
512  //Write the status word
513  txBufferDesc[txBufferIndex].status = EMAC_TX_LAST |
515 
516  //Point to the next buffer
517  txBufferIndex++;
518  }
519  else
520  {
521  //Write the status word
522  txBufferDesc[txBufferIndex].status = EMAC_TX_WRAP | EMAC_TX_LAST |
524 
525  //Wrap around
526  txBufferIndex = 0;
527  }
528 
529  //Set the TSTART bit to initiate transmission
530  EMAC->EMAC_NCR |= EMAC_NCR_TSTART;
531 
532  //Check whether the next buffer is available for writing
533  if((txBufferDesc[txBufferIndex].status & EMAC_TX_USED) != 0)
534  {
535  //The transmitter can accept another packet
536  osSetEvent(&interface->nicTxEvent);
537  }
538 
539  //Successful processing
540  return NO_ERROR;
541 }
542 
543 
544 /**
545  * @brief Receive a packet
546  * @param[in] interface Underlying network interface
547  * @return Error code
548  **/
549 
551 {
552  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
553  error_t error;
554  uint_t i;
555  uint_t j;
556  uint_t sofIndex;
557  uint_t eofIndex;
558  size_t n;
559  size_t size;
560  size_t length;
561 
562  //Initialize variables
563  size = 0;
564  sofIndex = UINT_MAX;
565  eofIndex = UINT_MAX;
566 
567  //Search for SOF and EOF flags
568  for(i = 0; i < SAMA5D3_ETH1_RX_BUFFER_COUNT; i++)
569  {
570  //Point to the current entry
571  j = rxBufferIndex + i;
572 
573  //Wrap around to the beginning of the buffer if necessary
575  {
577  }
578 
579  //No more entries to process?
580  if((rxBufferDesc[j].address & EMAC_RX_OWNERSHIP) == 0)
581  {
582  //Stop processing
583  break;
584  }
585 
586  //A valid SOF has been found?
587  if((rxBufferDesc[j].status & EMAC_RX_SOF) != 0)
588  {
589  //Save the position of the SOF
590  sofIndex = i;
591  }
592 
593  //A valid EOF has been found?
594  if((rxBufferDesc[j].status & EMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
595  {
596  //Save the position of the EOF
597  eofIndex = i;
598  //Retrieve the length of the frame
599  size = rxBufferDesc[j].status & EMAC_RX_LENGTH;
600  //Limit the number of data to read
601  size = MIN(size, ETH_MAX_FRAME_SIZE);
602  //Stop processing since we have reached the end of the frame
603  break;
604  }
605  }
606 
607  //Determine the number of entries to process
608  if(eofIndex != UINT_MAX)
609  {
610  j = eofIndex + 1;
611  }
612  else if(sofIndex != UINT_MAX)
613  {
614  j = sofIndex;
615  }
616  else
617  {
618  j = i;
619  }
620 
621  //Total number of bytes that have been copied from the receive buffer
622  length = 0;
623 
624  //Process incoming frame
625  for(i = 0; i < j; i++)
626  {
627  //Any data to copy from current buffer?
628  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
629  {
630  //Calculate the number of bytes to read at a time
632  //Copy data from receive buffer
633  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
634  //Update byte counters
635  length += n;
636  size -= n;
637  }
638 
639  //Mark the current buffer as free
640  rxBufferDesc[rxBufferIndex].address &= ~EMAC_RX_OWNERSHIP;
641 
642  //Point to the following entry
643  rxBufferIndex++;
644 
645  //Wrap around to the beginning of the buffer if necessary
646  if(rxBufferIndex >= SAMA5D3_ETH1_RX_BUFFER_COUNT)
647  {
648  rxBufferIndex = 0;
649  }
650  }
651 
652  //Any packet to process?
653  if(length > 0)
654  {
655  NetRxAncillary ancillary;
656 
657  //Additional options can be passed to the stack along with the packet
658  ancillary = NET_DEFAULT_RX_ANCILLARY;
659 
660  //Pass the packet to the upper layer
661  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
662  //Valid packet received
663  error = NO_ERROR;
664  }
665  else
666  {
667  //No more data in the receive buffer
668  error = ERROR_BUFFER_EMPTY;
669  }
670 
671  //Return status code
672  return error;
673 }
674 
675 
676 /**
677  * @brief Configure MAC address filtering
678  * @param[in] interface Underlying network interface
679  * @return Error code
680  **/
681 
683 {
684  uint_t i;
685  uint_t j;
686  uint_t k;
687  uint8_t *p;
688  uint32_t hashTable[2];
689  MacAddr unicastMacAddr[3];
690  MacFilterEntry *entry;
691 
692  //Debug message
693  TRACE_DEBUG("Updating MAC filter...\r\n");
694 
695  //Set the MAC address of the station
696  EMAC->EMAC_SA[0].EMAC_SAxB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
697  EMAC->EMAC_SA[0].EMAC_SAxT = interface->macAddr.w[2];
698 
699  //The MAC supports 3 additional addresses for unicast perfect filtering
700  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
701  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
702  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
703 
704  //The hash table is used for multicast address filtering
705  hashTable[0] = 0;
706  hashTable[1] = 0;
707 
708  //The MAC address filter contains the list of MAC addresses to accept
709  //when receiving an Ethernet frame
710  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
711  {
712  //Point to the current entry
713  entry = &interface->macAddrFilter[i];
714 
715  //Valid entry?
716  if(entry->refCount > 0)
717  {
718  //Multicast address?
719  if(macIsMulticastAddr(&entry->addr))
720  {
721  //Point to the MAC address
722  p = entry->addr.b;
723 
724  //Apply the hash function
725  k = (p[0] >> 6) ^ p[0];
726  k ^= (p[1] >> 4) ^ (p[1] << 2);
727  k ^= (p[2] >> 2) ^ (p[2] << 4);
728  k ^= (p[3] >> 6) ^ p[3];
729  k ^= (p[4] >> 4) ^ (p[4] << 2);
730  k ^= (p[5] >> 2) ^ (p[5] << 4);
731 
732  //The hash value is reduced to a 6-bit index
733  k &= 0x3F;
734 
735  //Update hash table contents
736  hashTable[k / 32] |= (1 << (k % 32));
737  }
738  else
739  {
740  //Up to 3 additional MAC addresses can be specified
741  if(j < 3)
742  {
743  //Save the unicast address
744  unicastMacAddr[j] = entry->addr;
745  }
746  else
747  {
748  //Point to the MAC address
749  p = entry->addr.b;
750 
751  //Apply the hash function
752  k = (p[0] >> 6) ^ p[0];
753  k ^= (p[1] >> 4) ^ (p[1] << 2);
754  k ^= (p[2] >> 2) ^ (p[2] << 4);
755  k ^= (p[3] >> 6) ^ p[3];
756  k ^= (p[4] >> 4) ^ (p[4] << 2);
757  k ^= (p[5] >> 2) ^ (p[5] << 4);
758 
759  //The hash value is reduced to a 6-bit index
760  k &= 0x3F;
761 
762  //Update hash table contents
763  hashTable[k / 32] |= (1 << (k % 32));
764  }
765 
766  //Increment the number of unicast addresses
767  j++;
768  }
769  }
770  }
771 
772  //Configure the first unicast address filter
773  if(j >= 1)
774  {
775  //The address is activated when SAT register is written
776  EMAC->EMAC_SA[1].EMAC_SAxB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
777  EMAC->EMAC_SA[1].EMAC_SAxT = unicastMacAddr[0].w[2];
778  }
779  else
780  {
781  //The address is deactivated when SAB register is written
782  EMAC->EMAC_SA[1].EMAC_SAxB = 0;
783  }
784 
785  //Configure the second unicast address filter
786  if(j >= 2)
787  {
788  //The address is activated when SAT register is written
789  EMAC->EMAC_SA[2].EMAC_SAxB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
790  EMAC->EMAC_SA[2].EMAC_SAxT = unicastMacAddr[1].w[2];
791  }
792  else
793  {
794  //The address is deactivated when SAB register is written
795  EMAC->EMAC_SA[2].EMAC_SAxB = 0;
796  }
797 
798  //Configure the third unicast address filter
799  if(j >= 3)
800  {
801  //The address is activated when SAT register is written
802  EMAC->EMAC_SA[3].EMAC_SAxB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
803  EMAC->EMAC_SA[3].EMAC_SAxT = unicastMacAddr[2].w[2];
804  }
805  else
806  {
807  //The address is deactivated when SAB register is written
808  EMAC->EMAC_SA[3].EMAC_SAxB = 0;
809  }
810 
811  //The perfect MAC filter supports only 3 unicast addresses
812  if(j >= 4)
813  {
814  EMAC->EMAC_NCFGR |= EMAC_NCFGR_UNI;
815  }
816  else
817  {
818  EMAC->EMAC_NCFGR &= ~EMAC_NCFGR_UNI;
819  }
820 
821  //Configure the multicast hash table
822  EMAC->EMAC_HRB = hashTable[0];
823  EMAC->EMAC_HRT = hashTable[1];
824 
825  //Debug message
826  TRACE_DEBUG(" HRB = %08" PRIX32 "\r\n", EMAC->EMAC_HRB);
827  TRACE_DEBUG(" HRT = %08" PRIX32 "\r\n", EMAC->EMAC_HRT);
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  uint32_t config;
843 
844  //Read network configuration register
845  config = EMAC->EMAC_NCFGR;
846 
847  //10BASE-T or 100BASE-TX operation mode?
848  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
849  {
850  config |= EMAC_NCFGR_SPD;
851  }
852  else
853  {
854  config &= ~EMAC_NCFGR_SPD;
855  }
856 
857  //Half-duplex or full-duplex mode?
858  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
859  {
860  config |= EMAC_NCFGR_FD;
861  }
862  else
863  {
864  config &= ~EMAC_NCFGR_FD;
865  }
866 
867  //Write configuration value back to NCFGR register
868  EMAC->EMAC_NCFGR = config;
869 
870  //Successful processing
871  return NO_ERROR;
872 }
873 
874 
875 /**
876  * @brief Write PHY register
877  * @param[in] opcode Access type (2 bits)
878  * @param[in] phyAddr PHY address (5 bits)
879  * @param[in] regAddr Register address (5 bits)
880  * @param[in] data Register value
881  **/
882 
883 void sama5d3Eth1WritePhyReg(uint8_t opcode, uint8_t phyAddr,
884  uint8_t regAddr, uint16_t data)
885 {
886  uint32_t temp;
887 
888  //Valid opcode?
889  if(opcode == SMI_OPCODE_WRITE)
890  {
891  //Set up a write operation
892  temp = EMAC_MAN_SOF(1) | EMAC_MAN_RW(1) | EMAC_MAN_CODE(2);
893  //PHY address
894  temp |= EMAC_MAN_PHYA(phyAddr);
895  //Register address
896  temp |= EMAC_MAN_REGA(regAddr);
897  //Register value
898  temp |= EMAC_MAN_DATA(data);
899 
900  //Start a write operation
901  EMAC->EMAC_MAN = temp;
902  //Wait for the write to complete
903  while((EMAC->EMAC_NSR & EMAC_NSR_IDLE) == 0)
904  {
905  }
906  }
907  else
908  {
909  //The MAC peripheral only supports standard Clause 22 opcodes
910  }
911 }
912 
913 
914 /**
915  * @brief Read PHY register
916  * @param[in] opcode Access type (2 bits)
917  * @param[in] phyAddr PHY address (5 bits)
918  * @param[in] regAddr Register address (5 bits)
919  * @return Register value
920  **/
921 
922 uint16_t sama5d3Eth1ReadPhyReg(uint8_t opcode, uint8_t phyAddr,
923  uint8_t regAddr)
924 {
925  uint16_t data;
926  uint32_t temp;
927 
928  //Valid opcode?
929  if(opcode == SMI_OPCODE_READ)
930  {
931  //Set up a read operation
932  temp = EMAC_MAN_SOF(1) | EMAC_MAN_RW(2) | EMAC_MAN_CODE(2);
933  //PHY address
934  temp |= EMAC_MAN_PHYA(phyAddr);
935  //Register address
936  temp |= EMAC_MAN_REGA(regAddr);
937 
938  //Start a read operation
939  EMAC->EMAC_MAN = temp;
940  //Wait for the read to complete
941  while((EMAC->EMAC_NSR & EMAC_NSR_IDLE) == 0)
942  {
943  }
944 
945  //Get register value
946  data = EMAC->EMAC_MAN & EMAC_MAN_DATA_Msk;
947  }
948  else
949  {
950  //The MAC peripheral only supports standard Clause 22 opcodes
951  data = 0;
952  }
953 
954  //Return the value of the PHY register
955  return data;
956 }
#define rxBuffer
#define txBuffer
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
unsigned int uint_t
Definition: compiler_port.h:50
int bool_t
Definition: compiler_port.h:53
Debugging facilities.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define TRACE_INFO(...)
Definition: debug.h:95
uint8_t n
uint8_t opcode
Definition: dns_common.h:188
error_t
Error codes.
Definition: error.h:43
@ ERROR_BUFFER_EMPTY
Definition: error.h:141
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
#define macIsMulticastAddr(macAddr)
Definition: ethernet.h:133
#define ETH_MTU
Definition: ethernet.h:116
uint8_t data[]
Definition: ethernet.h:222
#define ETH_MAX_FRAME_SIZE
Definition: ethernet.h:110
MacAddr
Definition: ethernet.h:195
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
Ipv6Addr address[]
Definition: ipv6.h:316
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 osMemcpy(dest, src, length)
Definition: os_port.h:141
#define MIN(a, b)
Definition: os_port.h:63
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define osEnterIsr()
#define osExitIsr(flag)
#define EMAC_RX_WRAP
#define EMAC_RX_LENGTH
#define EMAC_RX_ADDRESS
#define EMAC_RX_OWNERSHIP
#define EMAC_TX_LAST
#define EMAC_RX_EOF
#define EMAC_TX_USED
#define EMAC_RMII_MASK
#define EMAC_RX_SOF
#define EMAC_TX_LENGTH
#define EMAC_TX_WRAP
error_t sama5d3Eth1UpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
error_t sama5d3Eth1Init(NetInterface *interface)
SAMA5D3 Ethernet MAC initialization.
const NicDriver sama5d3Eth1Driver
SAMA5D3 Ethernet MAC driver (EMAC instance)
void sama5d3Eth1EnableIrq(NetInterface *interface)
Enable interrupts.
__weak_func void sama5d3Eth1InitGpio(NetInterface *interface)
GPIO configuration.
void sama5d3Eth1EventHandler(NetInterface *interface)
SAMA5D3 Ethernet MAC event handler.
void sama5d3Eth1InitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
uint16_t sama5d3Eth1ReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
void sama5d3Eth1IrqHandler(void)
SAMA5D3 Ethernet MAC interrupt service routine.
void sama5d3Eth1Tick(NetInterface *interface)
SAMA5D3 Ethernet MAC timer handler.
void sama5d3Eth1DisableIrq(NetInterface *interface)
Disable interrupts.
error_t sama5d3Eth1SendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
error_t sama5d3Eth1UpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
void sama5d3Eth1WritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
error_t sama5d3Eth1ReceivePacket(NetInterface *interface)
Receive a packet.
SAMA5D3 Ethernet MAC driver (EMAC instance)
#define SAMA5D3_ETH1_RAM_SECTION
#define SAMA5D3_ETH1_RX_BUFFER_SIZE
#define SAMA5D3_ETH1_TX_BUFFER_SIZE
#define SAMA5D3_ETH1_IRQ_PRIORITY
#define SAMA5D3_ETH1_RX_BUFFER_COUNT
#define SAMA5D3_ETH1_TX_BUFFER_COUNT
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
Receive buffer descriptor.
Transmit buffer descriptor.
uint8_t length
Definition: tcp.h:368