sama5d4_eth2_driver.c
Go to the documentation of this file.
1 /**
2  * @file sama5d4_eth2_driver.c
3  * @brief SAMA5D4 Ethernet MAC driver (GMAC1 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 = SAMA5D4_ETH2_RAM_SECTION
52 //RX buffer
53 #pragma data_alignment = 8
54 #pragma location = SAMA5D4_ETH2_RAM_SECTION
56 //TX buffer descriptors
57 #pragma data_alignment = 8
58 #pragma location = SAMA5D4_ETH2_RAM_SECTION
60 //RX buffer descriptors
61 #pragma data_alignment = 8
62 #pragma location = SAMA5D4_ETH2_RAM_SECTION
64 
65 //GCC compiler?
66 #else
67 
68 //TX buffer
70  __attribute__((aligned(8), __section__(SAMA5D4_ETH2_RAM_SECTION)));
71 //RX buffer
73  __attribute__((aligned(8), __section__(SAMA5D4_ETH2_RAM_SECTION)));
74 //TX buffer descriptors
76  __attribute__((aligned(8), __section__(SAMA5D4_ETH2_RAM_SECTION)));
77 //RX buffer descriptors
79  __attribute__((aligned(8), __section__(SAMA5D4_ETH2_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 SAMA5D4 Ethernet MAC driver (GMAC1 instance)
91  **/
92 
94 {
96  ETH_MTU,
107  TRUE,
108  TRUE,
109  TRUE,
110  FALSE
111 };
112 
113 
114 /**
115  * @brief SAMA5D4 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 SAMA5D4 Ethernet MAC (GMAC1)...\r\n");
127 
128  //Save underlying network interface
129  nicDriverInterface = interface;
130 
131  //Enable GMAC peripheral clock
132  PMC->PMC_PCER1 = (1 << (ID_GMAC1 - 32));
133 
134  //Disable transmit and receive circuits
135  GMAC1->GMAC_NCR = 0;
136 
137  //GPIO configuration
138  sama5d4Eth2InitGpio(interface);
139 
140  //Configure MDC clock speed
141  GMAC1->GMAC_NCFGR = GMAC_NCFGR_CLK_MCK_96;
142  //Enable management port (MDC and MDIO)
143  GMAC1->GMAC_NCR |= GMAC_NCR_MPE;
144 
145  //Valid Ethernet PHY or switch driver?
146  if(interface->phyDriver != NULL)
147  {
148  //Ethernet PHY initialization
149  error = interface->phyDriver->init(interface);
150  }
151  else if(interface->switchDriver != NULL)
152  {
153  //Ethernet switch initialization
154  error = interface->switchDriver->init(interface);
155  }
156  else
157  {
158  //The interface is not properly configured
159  error = ERROR_FAILURE;
160  }
161 
162  //Any error to report?
163  if(error)
164  {
165  return error;
166  }
167 
168  //Set the MAC address of the station
169  GMAC1->GMAC_SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
170  GMAC1->GMAC_SA[0].GMAC_SAT = interface->macAddr.w[2];
171 
172  //The MAC supports 3 additional addresses for unicast perfect filtering
173  GMAC1->GMAC_SA[1].GMAC_SAB = 0;
174  GMAC1->GMAC_SA[2].GMAC_SAB = 0;
175  GMAC1->GMAC_SA[3].GMAC_SAB = 0;
176 
177  //Initialize hash table
178  GMAC1->GMAC_HRB = 0;
179  GMAC1->GMAC_HRT = 0;
180 
181  //Configure the receive filter
182  GMAC1->GMAC_NCFGR |= GMAC_NCFGR_MAXFS | GMAC_NCFGR_MTIHEN;
183 
184  //Initialize buffer descriptors
185  sama5d4Eth2InitBufferDesc(interface);
186 
187  //Clear transmit status register
188  GMAC1->GMAC_TSR = GMAC_TSR_HRESP | GMAC_TSR_UND | GMAC_TSR_TXCOMP |
189  GMAC_TSR_TFC | GMAC_TSR_TXGO | GMAC_TSR_RLE | GMAC_TSR_COL |
190  GMAC_TSR_UBR;
191 
192  //Clear receive status register
193  GMAC1->GMAC_RSR = GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC |
194  GMAC_RSR_BNA;
195 
196  //First disable all GMAC interrupts
197  GMAC1->GMAC_IDR = 0xFFFFFFFF;
198 
199  //Only the desired ones are enabled
200  GMAC1->GMAC_IER = GMAC_IER_HRESP | GMAC_IER_ROVR | GMAC_IER_TCOMP |
201  GMAC_IER_TFC | GMAC_IER_RLEX | GMAC_IER_TUR | GMAC_IER_RXUBR |
202  GMAC_IER_RCOMP;
203 
204  //Read GMAC_ISR register to clear any pending interrupt
205  status = GMAC1->GMAC_ISR;
206  (void) status;
207 
208  //Configure interrupt controller
209  AIC->AIC_SSR = ID_GMAC1;
210  AIC->AIC_SMR = AIC_SMR_SRCTYPE_INT_LEVEL_SENSITIVE | AIC_SMR_PRIOR(SAMA5D4_ETH2_IRQ_PRIORITY);
211  AIC->AIC_SVR = (uint32_t) sama5d4Eth2IrqHandler;
212 
213  //Enable the GMAC to transmit and receive data
214  GMAC1->GMAC_NCR |= GMAC_NCR_TXEN | GMAC_NCR_RXEN;
215 
216  //Accept any packets from the upper layer
217  osSetEvent(&interface->nicTxEvent);
218 
219  //Successful initialization
220  return NO_ERROR;
221 }
222 
223 
224 /**
225  * @brief GPIO configuration
226  * @param[in] interface Underlying network interface
227  **/
228 
229 __weak_func void sama5d4Eth2InitGpio(NetInterface *interface)
230 {
231 }
232 
233 
234 /**
235  * @brief Initialize buffer descriptors
236  * @param[in] interface Underlying network interface
237  **/
238 
240 {
241  uint_t i;
242  uint32_t address;
243 
244  //Initialize TX buffer descriptors
245  for(i = 0; i < SAMA5D4_ETH2_TX_BUFFER_COUNT; i++)
246  {
247  //Calculate the address of the current TX buffer
248  address = (uint32_t) txBuffer[i];
249  //Write the address to the descriptor entry
250  txBufferDesc[i].address = address;
251  //Initialize status field
252  txBufferDesc[i].status = GMAC_TX_USED;
253  }
254 
255  //Mark the last descriptor entry with the wrap flag
256  txBufferDesc[i - 1].status |= GMAC_TX_WRAP;
257  //Initialize TX buffer index
258  txBufferIndex = 0;
259 
260  //Initialize RX buffer descriptors
261  for(i = 0; i < SAMA5D4_ETH2_RX_BUFFER_COUNT; i++)
262  {
263  //Calculate the address of the current RX buffer
264  address = (uint32_t) rxBuffer[i];
265  //Write the address to the descriptor entry
266  rxBufferDesc[i].address = address & GMAC_RX_ADDRESS;
267  //Clear status field
268  rxBufferDesc[i].status = 0;
269  }
270 
271  //Mark the last descriptor entry with the wrap flag
272  rxBufferDesc[i - 1].address |= GMAC_RX_WRAP;
273  //Initialize RX buffer index
274  rxBufferIndex = 0;
275 
276  //Start location of the TX descriptor list
277  GMAC1->GMAC_TBQB = (uint32_t) txBufferDesc;
278  //Start location of the RX descriptor list
279  GMAC1->GMAC_RBQB = (uint32_t) rxBufferDesc;
280 }
281 
282 
283 /**
284  * @brief SAMA5D4 Ethernet MAC timer handler
285  *
286  * This routine is periodically called by the TCP/IP stack to handle periodic
287  * operations such as polling the link state
288  *
289  * @param[in] interface Underlying network interface
290  **/
291 
293 {
294  //Valid Ethernet PHY or switch driver?
295  if(interface->phyDriver != NULL)
296  {
297  //Handle periodic operations
298  interface->phyDriver->tick(interface);
299  }
300  else if(interface->switchDriver != NULL)
301  {
302  //Handle periodic operations
303  interface->switchDriver->tick(interface);
304  }
305  else
306  {
307  //Just for sanity
308  }
309 }
310 
311 
312 /**
313  * @brief Enable interrupts
314  * @param[in] interface Underlying network interface
315  **/
316 
318 {
319  //Enable Ethernet MAC interrupts
320  AIC->AIC_SSR = ID_GMAC1;
321  AIC->AIC_IECR = AIC_IECR_INTEN;
322 
323  //Valid Ethernet PHY or switch driver?
324  if(interface->phyDriver != NULL)
325  {
326  //Enable Ethernet PHY interrupts
327  interface->phyDriver->enableIrq(interface);
328  }
329  else if(interface->switchDriver != NULL)
330  {
331  //Enable Ethernet switch interrupts
332  interface->switchDriver->enableIrq(interface);
333  }
334  else
335  {
336  //Just for sanity
337  }
338 }
339 
340 
341 /**
342  * @brief Disable interrupts
343  * @param[in] interface Underlying network interface
344  **/
345 
347 {
348  //Disable Ethernet MAC interrupts
349  AIC->AIC_SSR = ID_GMAC1;
350  AIC->AIC_IDCR = AIC_IDCR_INTD;
351 
352  //Valid Ethernet PHY or switch driver?
353  if(interface->phyDriver != NULL)
354  {
355  //Disable Ethernet PHY interrupts
356  interface->phyDriver->disableIrq(interface);
357  }
358  else if(interface->switchDriver != NULL)
359  {
360  //Disable Ethernet switch interrupts
361  interface->switchDriver->disableIrq(interface);
362  }
363  else
364  {
365  //Just for sanity
366  }
367 }
368 
369 
370 /**
371  * @brief SAMA5D4 Ethernet MAC interrupt service routine
372  **/
373 
375 {
376  bool_t flag;
377  volatile uint32_t isr;
378  volatile uint32_t tsr;
379  volatile uint32_t rsr;
380 
381  //Interrupt service routine prologue
382  osEnterIsr();
383 
384  //This flag will be set if a higher priority task must be woken
385  flag = FALSE;
386 
387  //Each time the software reads GMAC_ISR, it has to check the contents
388  //of GMAC_TSR, GMAC_RSR and GMAC_NSR
389  isr = GMAC1->GMAC_ISR;
390  tsr = GMAC1->GMAC_TSR;
391  rsr = GMAC1->GMAC_RSR;
392  (void) isr;
393 
394  //Packet transmitted?
395  if((tsr & (GMAC_TSR_HRESP | GMAC_TSR_UND | GMAC_TSR_TXCOMP | GMAC_TSR_TFC |
396  GMAC_TSR_TXGO | GMAC_TSR_RLE | GMAC_TSR_COL | GMAC_TSR_UBR)) != 0)
397  {
398  //Only clear TSR flags that are currently set
399  GMAC1->GMAC_TSR = tsr;
400 
401  //Check whether the TX buffer is available for writing
402  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
403  {
404  //Notify the TCP/IP stack that the transmitter is ready to send
405  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
406  }
407  }
408 
409  //Packet received?
410  if((rsr & (GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC | GMAC_RSR_BNA)) != 0)
411  {
412  //Set event flag
413  nicDriverInterface->nicEvent = TRUE;
414  //Notify the TCP/IP stack of the event
415  flag |= osSetEventFromIsr(&nicDriverInterface->netContext->event);
416  }
417 
418  //Write AIC_EOICR register before exiting
419  AIC->AIC_EOICR = 0;
420 
421  //Interrupt service routine epilogue
422  osExitIsr(flag);
423 }
424 
425 
426 /**
427  * @brief SAMA5D4 Ethernet MAC event handler
428  * @param[in] interface Underlying network interface
429  **/
430 
432 {
433  error_t error;
434  uint32_t rsr;
435 
436  //Read receive status
437  rsr = GMAC1->GMAC_RSR;
438 
439  //Packet received?
440  if((rsr & (GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC | GMAC_RSR_BNA)) != 0)
441  {
442  //Only clear RSR flags that are currently set
443  GMAC1->GMAC_RSR = rsr;
444 
445  //Process all pending packets
446  do
447  {
448  //Read incoming packet
449  error = sama5d4Eth2ReceivePacket(interface);
450 
451  //No more data in the receive buffer?
452  } while(error != ERROR_BUFFER_EMPTY);
453  }
454 }
455 
456 
457 /**
458  * @brief Send a packet
459  * @param[in] interface Underlying network interface
460  * @param[in] buffer Multi-part buffer containing the data to send
461  * @param[in] offset Offset to the first data byte
462  * @param[in] ancillary Additional options passed to the stack along with
463  * the packet
464  * @return Error code
465  **/
466 
468  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
469 {
470  size_t length;
471 
472  //Retrieve the length of the packet
473  length = netBufferGetLength(buffer) - offset;
474 
475  //Check the frame length
477  {
478  //The transmitter can accept another packet
479  osSetEvent(&interface->nicTxEvent);
480  //Report an error
481  return ERROR_INVALID_LENGTH;
482  }
483 
484  //Make sure the current buffer is available for writing
485  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) == 0)
486  {
487  return ERROR_FAILURE;
488  }
489 
490  //Copy user data to the transmit buffer
491  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
492 
493  //Set the necessary flags in the descriptor entry
494  if(txBufferIndex < (SAMA5D4_ETH2_TX_BUFFER_COUNT - 1))
495  {
496  //Write the status word
497  txBufferDesc[txBufferIndex].status = GMAC_TX_LAST |
499 
500  //Point to the next buffer
501  txBufferIndex++;
502  }
503  else
504  {
505  //Write the status word
506  txBufferDesc[txBufferIndex].status = GMAC_TX_WRAP | GMAC_TX_LAST |
508 
509  //Wrap around
510  txBufferIndex = 0;
511  }
512 
513  //Data synchronization barrier
514  dsb();
515 
516  //Set the TSTART bit to initiate transmission
517  GMAC1->GMAC_NCR |= GMAC_NCR_TSTART;
518 
519  //Check whether the next buffer is available for writing
520  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
521  {
522  //The transmitter can accept another packet
523  osSetEvent(&interface->nicTxEvent);
524  }
525 
526  //Successful processing
527  return NO_ERROR;
528 }
529 
530 
531 /**
532  * @brief Receive a packet
533  * @param[in] interface Underlying network interface
534  * @return Error code
535  **/
536 
538 {
539  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
540  error_t error;
541  uint_t i;
542  uint_t j;
543  uint_t sofIndex;
544  uint_t eofIndex;
545  size_t n;
546  size_t size;
547  size_t length;
548 
549  //Initialize variables
550  size = 0;
551  sofIndex = UINT_MAX;
552  eofIndex = UINT_MAX;
553 
554  //Search for SOF and EOF flags
555  for(i = 0; i < SAMA5D4_ETH2_RX_BUFFER_COUNT; i++)
556  {
557  //Point to the current entry
558  j = rxBufferIndex + i;
559 
560  //Wrap around to the beginning of the buffer if necessary
562  {
564  }
565 
566  //No more entries to process?
567  if((rxBufferDesc[j].address & GMAC_RX_OWNERSHIP) == 0)
568  {
569  //Stop processing
570  break;
571  }
572 
573  //A valid SOF has been found?
574  if((rxBufferDesc[j].status & GMAC_RX_SOF) != 0)
575  {
576  //Save the position of the SOF
577  sofIndex = i;
578  }
579 
580  //A valid EOF has been found?
581  if((rxBufferDesc[j].status & GMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
582  {
583  //Save the position of the EOF
584  eofIndex = i;
585  //Retrieve the length of the frame
586  size = rxBufferDesc[j].status & GMAC_RX_LENGTH;
587  //Limit the number of data to read
588  size = MIN(size, ETH_MAX_FRAME_SIZE);
589  //Stop processing since we have reached the end of the frame
590  break;
591  }
592  }
593 
594  //Determine the number of entries to process
595  if(eofIndex != UINT_MAX)
596  {
597  j = eofIndex + 1;
598  }
599  else if(sofIndex != UINT_MAX)
600  {
601  j = sofIndex;
602  }
603  else
604  {
605  j = i;
606  }
607 
608  //Total number of bytes that have been copied from the receive buffer
609  length = 0;
610 
611  //Process incoming frame
612  for(i = 0; i < j; i++)
613  {
614  //Any data to copy from current buffer?
615  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
616  {
617  //Calculate the number of bytes to read at a time
619  //Copy data from receive buffer
620  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
621  //Update byte counters
622  length += n;
623  size -= n;
624  }
625 
626  //Mark the current buffer as free
627  rxBufferDesc[rxBufferIndex].address &= ~GMAC_RX_OWNERSHIP;
628 
629  //Point to the following entry
630  rxBufferIndex++;
631 
632  //Wrap around to the beginning of the buffer if necessary
633  if(rxBufferIndex >= SAMA5D4_ETH2_RX_BUFFER_COUNT)
634  {
635  rxBufferIndex = 0;
636  }
637  }
638 
639  //Any packet to process?
640  if(length > 0)
641  {
642  NetRxAncillary ancillary;
643 
644  //Additional options can be passed to the stack along with the packet
645  ancillary = NET_DEFAULT_RX_ANCILLARY;
646 
647  //Pass the packet to the upper layer
648  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
649  //Valid packet received
650  error = NO_ERROR;
651  }
652  else
653  {
654  //No more data in the receive buffer
655  error = ERROR_BUFFER_EMPTY;
656  }
657 
658  //Return status code
659  return error;
660 }
661 
662 
663 /**
664  * @brief Configure MAC address filtering
665  * @param[in] interface Underlying network interface
666  * @return Error code
667  **/
668 
670 {
671  uint_t i;
672  uint_t j;
673  uint_t k;
674  uint8_t *p;
675  uint32_t hashTable[2];
676  MacAddr unicastMacAddr[3];
677  MacFilterEntry *entry;
678 
679  //Debug message
680  TRACE_DEBUG("Updating MAC filter...\r\n");
681 
682  //Set the MAC address of the station
683  GMAC1->GMAC_SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
684  GMAC1->GMAC_SA[0].GMAC_SAT = interface->macAddr.w[2];
685 
686  //The MAC supports 3 additional addresses for unicast perfect filtering
687  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
688  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
689  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
690 
691  //The hash table is used for multicast address filtering
692  hashTable[0] = 0;
693  hashTable[1] = 0;
694 
695  //The MAC address filter contains the list of MAC addresses to accept
696  //when receiving an Ethernet frame
697  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
698  {
699  //Point to the current entry
700  entry = &interface->macAddrFilter[i];
701 
702  //Valid entry?
703  if(entry->refCount > 0)
704  {
705  //Multicast address?
706  if(macIsMulticastAddr(&entry->addr))
707  {
708  //Point to the MAC address
709  p = entry->addr.b;
710 
711  //Apply the hash function
712  k = (p[0] >> 6) ^ p[0];
713  k ^= (p[1] >> 4) ^ (p[1] << 2);
714  k ^= (p[2] >> 2) ^ (p[2] << 4);
715  k ^= (p[3] >> 6) ^ p[3];
716  k ^= (p[4] >> 4) ^ (p[4] << 2);
717  k ^= (p[5] >> 2) ^ (p[5] << 4);
718 
719  //The hash value is reduced to a 6-bit index
720  k &= 0x3F;
721 
722  //Update hash table contents
723  hashTable[k / 32] |= (1 << (k % 32));
724  }
725  else
726  {
727  //Up to 3 additional MAC addresses can be specified
728  if(j < 3)
729  {
730  //Save the unicast address
731  unicastMacAddr[j] = entry->addr;
732  }
733  else
734  {
735  //Point to the MAC address
736  p = entry->addr.b;
737 
738  //Apply the hash function
739  k = (p[0] >> 6) ^ p[0];
740  k ^= (p[1] >> 4) ^ (p[1] << 2);
741  k ^= (p[2] >> 2) ^ (p[2] << 4);
742  k ^= (p[3] >> 6) ^ p[3];
743  k ^= (p[4] >> 4) ^ (p[4] << 2);
744  k ^= (p[5] >> 2) ^ (p[5] << 4);
745 
746  //The hash value is reduced to a 6-bit index
747  k &= 0x3F;
748 
749  //Update hash table contents
750  hashTable[k / 32] |= (1 << (k % 32));
751  }
752 
753  //Increment the number of unicast addresses
754  j++;
755  }
756  }
757  }
758 
759  //Configure the first unicast address filter
760  if(j >= 1)
761  {
762  //The address is activated when SAT register is written
763  GMAC1->GMAC_SA[1].GMAC_SAB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
764  GMAC1->GMAC_SA[1].GMAC_SAT = unicastMacAddr[0].w[2];
765  }
766  else
767  {
768  //The address is deactivated when SAB register is written
769  GMAC1->GMAC_SA[1].GMAC_SAB = 0;
770  }
771 
772  //Configure the second unicast address filter
773  if(j >= 2)
774  {
775  //The address is activated when SAT register is written
776  GMAC1->GMAC_SA[2].GMAC_SAB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
777  GMAC1->GMAC_SA[2].GMAC_SAT = unicastMacAddr[1].w[2];
778  }
779  else
780  {
781  //The address is deactivated when SAB register is written
782  GMAC1->GMAC_SA[2].GMAC_SAB = 0;
783  }
784 
785  //Configure the third unicast address filter
786  if(j >= 3)
787  {
788  //The address is activated when SAT register is written
789  GMAC1->GMAC_SA[3].GMAC_SAB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
790  GMAC1->GMAC_SA[3].GMAC_SAT = unicastMacAddr[2].w[2];
791  }
792  else
793  {
794  //The address is deactivated when SAB register is written
795  GMAC1->GMAC_SA[3].GMAC_SAB = 0;
796  }
797 
798  //The perfect MAC filter supports only 3 unicast addresses
799  if(j >= 4)
800  {
801  GMAC1->GMAC_NCFGR |= GMAC_NCFGR_UNIHEN;
802  }
803  else
804  {
805  GMAC1->GMAC_NCFGR &= ~GMAC_NCFGR_UNIHEN;
806  }
807 
808  //Configure the multicast hash table
809  GMAC1->GMAC_HRB = hashTable[0];
810  GMAC1->GMAC_HRT = hashTable[1];
811 
812  //Debug message
813  TRACE_DEBUG(" HRB = 0x%08" PRIX32 "\r\n", GMAC1->GMAC_HRB);
814  TRACE_DEBUG(" HRT = 0x%08" PRIX32 "\r\n", GMAC1->GMAC_HRT);
815 
816  //Successful processing
817  return NO_ERROR;
818 }
819 
820 
821 /**
822  * @brief Adjust MAC configuration parameters for proper operation
823  * @param[in] interface Underlying network interface
824  * @return Error code
825  **/
826 
828 {
829  uint32_t config;
830 
831  //Read network configuration register
832  config = GMAC1->GMAC_NCFGR;
833 
834  //10BASE-T or 100BASE-TX operation mode?
835  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
836  {
837  config |= GMAC_NCFGR_SPD;
838  }
839  else
840  {
841  config &= ~GMAC_NCFGR_SPD;
842  }
843 
844  //Half-duplex or full-duplex mode?
845  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
846  {
847  config |= GMAC_NCFGR_FD;
848  }
849  else
850  {
851  config &= ~GMAC_NCFGR_FD;
852  }
853 
854  //Write configuration value back to NCFGR register
855  GMAC1->GMAC_NCFGR = config;
856 
857  //Successful processing
858  return NO_ERROR;
859 }
860 
861 
862 /**
863  * @brief Write PHY register
864  * @param[in] opcode Access type (2 bits)
865  * @param[in] phyAddr PHY address (5 bits)
866  * @param[in] regAddr Register address (5 bits)
867  * @param[in] data Register value
868  **/
869 
870 void sama5d4Eth2WritePhyReg(uint8_t opcode, uint8_t phyAddr,
871  uint8_t regAddr, uint16_t data)
872 {
873  uint32_t temp;
874 
875  //Valid opcode?
876  if(opcode == SMI_OPCODE_WRITE)
877  {
878  //Set up a write operation
879  temp = GMAC_MAN_CLTTO | GMAC_MAN_OP(1) | GMAC_MAN_WTN(2);
880  //PHY address
881  temp |= GMAC_MAN_PHYA(phyAddr);
882  //Register address
883  temp |= GMAC_MAN_REGA(regAddr);
884  //Register value
885  temp |= GMAC_MAN_DATA(data);
886 
887  //Start a write operation
888  GMAC1->GMAC_MAN = temp;
889  //Wait for the write to complete
890  while((GMAC1->GMAC_NSR & GMAC_NSR_IDLE) == 0)
891  {
892  }
893  }
894  else
895  {
896  //The MAC peripheral only supports standard Clause 22 opcodes
897  }
898 }
899 
900 
901 /**
902  * @brief Read PHY register
903  * @param[in] opcode Access type (2 bits)
904  * @param[in] phyAddr PHY address (5 bits)
905  * @param[in] regAddr Register address (5 bits)
906  * @return Register value
907  **/
908 
909 uint16_t sama5d4Eth2ReadPhyReg(uint8_t opcode, uint8_t phyAddr,
910  uint8_t regAddr)
911 {
912  uint16_t data;
913  uint32_t temp;
914 
915  //Valid opcode?
916  if(opcode == SMI_OPCODE_READ)
917  {
918  //Set up a read operation
919  temp = GMAC_MAN_CLTTO | GMAC_MAN_OP(2) | GMAC_MAN_WTN(2);
920  //PHY address
921  temp |= GMAC_MAN_PHYA(phyAddr);
922  //Register address
923  temp |= GMAC_MAN_REGA(regAddr);
924 
925  //Start a read operation
926  GMAC1->GMAC_MAN = temp;
927  //Wait for the read to complete
928  while((GMAC1->GMAC_NSR & GMAC_NSR_IDLE) == 0)
929  {
930  }
931 
932  //Get register value
933  data = GMAC1->GMAC_MAN & GMAC_MAN_DATA_Msk;
934  }
935  else
936  {
937  //The MAC peripheral only supports standard Clause 22 opcodes
938  data = 0;
939  }
940 
941  //Return the value of the PHY register
942  return data;
943 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
error_t sama5d4Eth2UpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
uint8_t opcode
Definition: dns_common.h:191
int bool_t
Definition: compiler_port.h:63
#define GMAC_TX_LENGTH
#define SAMA5D4_ETH2_TX_BUFFER_SIZE
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
error_t sama5d4Eth2ReceivePacket(NetInterface *interface)
Receive a packet.
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
uint16_t sama5d4Eth2ReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
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 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
#define SAMA5D4_ETH2_RX_BUFFER_SIZE
#define GMAC_RX_WRAP
#define GMAC_MAN_PHYA
#define SAMA5D4_ETH2_RAM_SECTION
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 osExitIsr(flag)
void sama5d4Eth2EnableIrq(NetInterface *interface)
Enable interrupts.
#define GMAC_RX_EOF
#define GMAC_MAN_DATA
#define SMI_OPCODE_WRITE
Definition: nic.h:66
SAMA5D4 Ethernet MAC driver (GMAC1 instance)
#define SAMA5D4_ETH2_IRQ_PRIORITY
#define GMAC_TX_USED
#define GMAC_MAN_OP
#define FALSE
Definition: os_port.h:46
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
__weak_func void sama5d4Eth2InitGpio(NetInterface *interface)
GPIO configuration.
error_t
Error codes.
Definition: error.h:43
#define GMAC_RX_ADDRESS
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:103
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
#define txBuffer
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:40
MacAddr addr
MAC address.
Definition: ethernet.h:265
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_BUFFER_EMPTY
Definition: error.h:142
#define NetTxAncillary
Definition: net_misc.h:36
#define SMI_OPCODE_READ
Definition: nic.h:67
error_t sama5d4Eth2Init(NetInterface *interface)
SAMA5D4 Ethernet MAC initialization.
#define TRACE_INFO(...)
Definition: debug.h:105
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 sama5d4Eth2IrqHandler(void)
SAMA5D4 Ethernet MAC interrupt service routine.
#define rxBuffer
#define GMAC_RX_SOF
MacAddr
Definition: ethernet.h:197
#define SAMA5D4_ETH2_TX_BUFFER_COUNT
#define SAMA5D4_ETH2_RX_BUFFER_COUNT
const NicDriver sama5d4Eth2Driver
SAMA5D4 Ethernet MAC driver (GMAC1 instance)
#define TRACE_DEBUG(...)
Definition: debug.h:119
uint16_t regAddr
#define GMAC_RX_LENGTH
#define ETH_MTU
Definition: ethernet.h:116
#define GMAC_TX_LAST
uint8_t n
MAC filter table entry.
Definition: ethernet.h:264
Ipv6Addr address[]
Definition: ipv6.h:345
void sama5d4Eth2EventHandler(NetInterface *interface)
SAMA5D4 Ethernet MAC event handler.
#define osEnterIsr()
void sama5d4Eth2InitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
Transmit buffer descriptor.
error_t sama5d4Eth2UpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define GMAC_MAN_WTN
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
Receive buffer descriptor.
void sama5d4Eth2WritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
error_t sama5d4Eth2SendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
@ 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 GMAC_RX_OWNERSHIP
#define GMAC_MAN_REGA
void sama5d4Eth2DisableIrq(NetInterface *interface)
Disable interrupts.
void sama5d4Eth2Tick(NetInterface *interface)
SAMA5D4 Ethernet MAC timer handler.
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:51
#define GMAC_MAN_DATA_Msk
@ NO_ERROR
Success.
Definition: error.h:44
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
Debugging facilities.
#define GMAC_TX_WRAP
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83