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