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