sam4e_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file sam4e_eth_driver.c
3  * @brief SAM4E Ethernet MAC driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include <limits.h>
36 #include "sam4e.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
50 //RX buffer
51 #pragma data_alignment = 8
53 //TX buffer descriptors
54 #pragma data_alignment = 4
55 static Sam4eTxBufferDesc txBufferDesc[SAM4E_ETH_TX_BUFFER_COUNT];
56 //RX buffer descriptors
57 #pragma data_alignment = 4
58 static Sam4eRxBufferDesc rxBufferDesc[SAM4E_ETH_RX_BUFFER_COUNT];
59 
60 //Keil MDK-ARM or GCC compiler?
61 #else
62 
63 //TX buffer
65  __attribute__((aligned(8)));
66 //RX buffer
68  __attribute__((aligned(8)));
69 //TX buffer descriptors
71  __attribute__((aligned(4)));
72 //RX buffer descriptors
74  __attribute__((aligned(4)));
75 
76 #endif
77 
78 //TX buffer index
79 static uint_t txBufferIndex;
80 //RX buffer index
81 static uint_t rxBufferIndex;
82 
83 
84 /**
85  * @brief SAM4E Ethernet MAC driver
86  **/
87 
89 {
91  ETH_MTU,
102  TRUE,
103  TRUE,
104  TRUE,
105  FALSE
106 };
107 
108 
109 /**
110  * @brief SAM4E Ethernet MAC initialization
111  * @param[in] interface Underlying network interface
112  * @return Error code
113  **/
114 
116 {
117  error_t error;
118  volatile uint32_t status;
119 
120  //Debug message
121  TRACE_INFO("Initializing SAM4E Ethernet MAC...\r\n");
122 
123  //Save underlying network interface
124  nicDriverInterface = interface;
125 
126  //Enable GMAC peripheral clock
127  PMC->PMC_PCER1 = (1 << (ID_GMAC - 32));
128 
129  //Disable transmit and receive circuits
130  GMAC->GMAC_NCR = 0;
131 
132  //GPIO configuration
133  sam4eEthInitGpio(interface);
134 
135  //Configure MDC clock speed
136  GMAC->GMAC_NCFGR = GMAC_NCFGR_CLK_MCK_96;
137  //Enable management port (MDC and MDIO)
138  GMAC->GMAC_NCR |= GMAC_NCR_MPE;
139 
140  //Valid Ethernet PHY or switch driver?
141  if(interface->phyDriver != NULL)
142  {
143  //Ethernet PHY initialization
144  error = interface->phyDriver->init(interface);
145  }
146  else if(interface->switchDriver != NULL)
147  {
148  //Ethernet switch initialization
149  error = interface->switchDriver->init(interface);
150  }
151  else
152  {
153  //The interface is not properly configured
154  error = ERROR_FAILURE;
155  }
156 
157  //Any error to report?
158  if(error)
159  {
160  return error;
161  }
162 
163  //Set the MAC address of the station
164  GMAC->GMAC_SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
165  GMAC->GMAC_SA[0].GMAC_SAT = interface->macAddr.w[2];
166 
167  //The MAC supports 3 additional addresses for unicast perfect filtering
168  GMAC->GMAC_SA[1].GMAC_SAB = 0;
169  GMAC->GMAC_SA[2].GMAC_SAB = 0;
170  GMAC->GMAC_SA[3].GMAC_SAB = 0;
171 
172  //Initialize hash table
173  GMAC->GMAC_HRB = 0;
174  GMAC->GMAC_HRT = 0;
175 
176  //Configure the receive filter
177  GMAC->GMAC_NCFGR |= GMAC_NCFGR_MAXFS | GMAC_NCFGR_MTIHEN;
178 
179  //Initialize buffer descriptors
180  sam4eEthInitBufferDesc(interface);
181 
182  //Clear transmit status register
183  GMAC->GMAC_TSR = GMAC_TSR_HRESP | GMAC_TSR_UND | GMAC_TSR_TXCOMP |
184  GMAC_TSR_TFC | GMAC_TSR_TXGO | GMAC_TSR_RLE | GMAC_TSR_COL |
185  GMAC_TSR_UBR;
186 
187  //Clear receive status register
188  GMAC->GMAC_RSR = GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC |
189  GMAC_RSR_BNA;
190 
191  //First disable all GMAC interrupts
192  GMAC->GMAC_IDR = 0xFFFFFFFF;
193 
194  //Only the desired ones are enabled
195  GMAC->GMAC_IER = GMAC_IER_HRESP | GMAC_IER_ROVR | GMAC_IER_TCOMP |
196  GMAC_IER_TFC | GMAC_IER_RLEX | GMAC_IER_TUR | GMAC_IER_RXUBR |
197  GMAC_IER_RCOMP;
198 
199  //Read GMAC_ISR register to clear any pending interrupt
200  status = GMAC->GMAC_ISR;
201  (void) status;
202 
203  //Set priority grouping (4 bits for pre-emption priority, no bits for subpriority)
204  NVIC_SetPriorityGrouping(SAM4E_ETH_IRQ_PRIORITY_GROUPING);
205 
206  //Configure GMAC interrupt priority
207  NVIC_SetPriority(GMAC_IRQn, NVIC_EncodePriority(SAM4E_ETH_IRQ_PRIORITY_GROUPING,
209 
210  //Enable the GMAC to transmit and receive data
211  GMAC->GMAC_NCR |= GMAC_NCR_TXEN | GMAC_NCR_RXEN;
212 
213  //Accept any packets from the upper layer
214  osSetEvent(&interface->nicTxEvent);
215 
216  //Successful initialization
217  return NO_ERROR;
218 }
219 
220 
221 /**
222  * @brief GPIO configuration
223  * @param[in] interface Underlying network interface
224  **/
225 
226 __weak_func void sam4eEthInitGpio(NetInterface *interface)
227 {
228 //SAM4E-EK or SAM4E-Xplained-Pro evaluation board?
229 #if defined(USE_SAM4E_EK) || defined(USE_SAM4E_XPLAINED_PRO)
230  uint32_t mask;
231 
232  //Enable PIO peripheral clock
233  PMC->PMC_PCER0 = (1 << ID_PIOD);
234 
235  //Configure MII pins
236  mask = PIO_PD16A_GTX3 | PIO_PD15A_GTX2 | PIO_PD14A_GRXCK | PIO_PD13A_GCOL |
237  PIO_PD12A_GRX3 | PIO_PD11A_GRX2 | PIO_PD10A_GCRS | PIO_PD9A_GMDIO |
238  PIO_PD8A_GMDC | PIO_PD7A_GRXER | PIO_PD6A_GRX1 | PIO_PD5A_GRX0 |
239  PIO_PD4A_GRXDV | PIO_PD3A_GTX1 | PIO_PD2A_GTX0 | PIO_PD1A_GTXEN |
240  PIO_PD0A_GTXCK;
241 
242  //Disable pull-up resistors on MII pins
243  PIOD->PIO_PUDR = mask;
244  //Disable interrupts-on-change
245  PIOD->PIO_IDR = mask;
246  //Assign MII pins to peripheral A function
247  PIOD->PIO_ABCDSR[0] &= ~mask;
248  PIOD->PIO_ABCDSR[1] &= ~mask;
249  //Disable the PIO from controlling the corresponding pins
250  PIOD->PIO_PDR = mask;
251 
252  //Select MII operation mode
253  GMAC->GMAC_UR = GMAC_UR_RMIIMII;
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 < SAM4E_ETH_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 = GMAC_TX_USED;
277  }
278 
279  //Mark the last descriptor entry with the wrap flag
280  txBufferDesc[i - 1].status |= GMAC_TX_WRAP;
281  //Initialize TX buffer index
282  txBufferIndex = 0;
283 
284  //Initialize RX buffer descriptors
285  for(i = 0; i < SAM4E_ETH_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 & GMAC_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 |= GMAC_RX_WRAP;
297  //Initialize RX buffer index
298  rxBufferIndex = 0;
299 
300  //Start location of the TX descriptor list
301  GMAC->GMAC_TBQB = (uint32_t) txBufferDesc;
302  //Start location of the RX descriptor list
303  GMAC->GMAC_RBQB = (uint32_t) rxBufferDesc;
304 }
305 
306 
307 /**
308  * @brief SAM4E 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 
316 void sam4eEthTick(NetInterface *interface)
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  NVIC_EnableIRQ(GMAC_IRQn);
345 
346  //Valid Ethernet PHY or switch driver?
347  if(interface->phyDriver != NULL)
348  {
349  //Enable Ethernet PHY interrupts
350  interface->phyDriver->enableIrq(interface);
351  }
352  else if(interface->switchDriver != NULL)
353  {
354  //Enable Ethernet switch interrupts
355  interface->switchDriver->enableIrq(interface);
356  }
357  else
358  {
359  //Just for sanity
360  }
361 }
362 
363 
364 /**
365  * @brief Disable interrupts
366  * @param[in] interface Underlying network interface
367  **/
368 
370 {
371  //Disable Ethernet MAC interrupts
372  NVIC_DisableIRQ(GMAC_IRQn);
373 
374  //Valid Ethernet PHY or switch driver?
375  if(interface->phyDriver != NULL)
376  {
377  //Disable Ethernet PHY interrupts
378  interface->phyDriver->disableIrq(interface);
379  }
380  else if(interface->switchDriver != NULL)
381  {
382  //Disable Ethernet switch interrupts
383  interface->switchDriver->disableIrq(interface);
384  }
385  else
386  {
387  //Just for sanity
388  }
389 }
390 
391 
392 /**
393  * @brief SAM4E Ethernet MAC interrupt service routine
394  **/
395 
396 void GMAC_Handler(void)
397 {
398  bool_t flag;
399  volatile uint32_t isr;
400  volatile uint32_t tsr;
401  volatile uint32_t rsr;
402 
403  //Interrupt service routine prologue
404  osEnterIsr();
405 
406  //This flag will be set if a higher priority task must be woken
407  flag = FALSE;
408 
409  //Each time the software reads GMAC_ISR, it has to check the contents
410  //of GMAC_TSR, GMAC_RSR and GMAC_NSR
411  isr = GMAC->GMAC_ISR;
412  tsr = GMAC->GMAC_TSR;
413  rsr = GMAC->GMAC_RSR;
414  (void) isr;
415 
416  //Packet transmitted?
417  if((tsr & (GMAC_TSR_HRESP | GMAC_TSR_UND | GMAC_TSR_TXCOMP | GMAC_TSR_TFC |
418  GMAC_TSR_TXGO | GMAC_TSR_RLE | GMAC_TSR_COL | GMAC_TSR_UBR)) != 0)
419  {
420  //Only clear TSR flags that are currently set
421  GMAC->GMAC_TSR = tsr;
422 
423  //Check whether the TX buffer is available for writing
424  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
425  {
426  //Notify the TCP/IP stack that the transmitter is ready to send
427  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
428  }
429  }
430 
431  //Packet received?
432  if((rsr & (GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC | GMAC_RSR_BNA)) != 0)
433  {
434  //Set event flag
435  nicDriverInterface->nicEvent = TRUE;
436  //Notify the TCP/IP stack of the event
437  flag |= osSetEventFromIsr(&netEvent);
438  }
439 
440  //Interrupt service routine epilogue
441  osExitIsr(flag);
442 }
443 
444 
445 /**
446  * @brief SAM4E Ethernet MAC event handler
447  * @param[in] interface Underlying network interface
448  **/
449 
451 {
452  error_t error;
453  uint32_t rsr;
454 
455  //Read receive status
456  rsr = GMAC->GMAC_RSR;
457 
458  //Packet received?
459  if((rsr & (GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC | GMAC_RSR_BNA)) != 0)
460  {
461  //Only clear RSR flags that are currently set
462  GMAC->GMAC_RSR = rsr;
463 
464  //Process all pending packets
465  do
466  {
467  //Read incoming packet
468  error = sam4eEthReceivePacket(interface);
469 
470  //No more data in the receive buffer?
471  } while(error != ERROR_BUFFER_EMPTY);
472  }
473 }
474 
475 
476 /**
477  * @brief Send a packet
478  * @param[in] interface Underlying network interface
479  * @param[in] buffer Multi-part buffer containing the data to send
480  * @param[in] offset Offset to the first data byte
481  * @param[in] ancillary Additional options passed to the stack along with
482  * the packet
483  * @return Error code
484  **/
485 
487  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
488 {
489  size_t length;
490 
491  //Retrieve the length of the packet
492  length = netBufferGetLength(buffer) - offset;
493 
494  //Check the frame length
496  {
497  //The transmitter can accept another packet
498  osSetEvent(&interface->nicTxEvent);
499  //Report an error
500  return ERROR_INVALID_LENGTH;
501  }
502 
503  //Make sure the current buffer is available for writing
504  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) == 0)
505  {
506  return ERROR_FAILURE;
507  }
508 
509  //Copy user data to the transmit buffer
510  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
511 
512  //Set the necessary flags in the descriptor entry
513  if(txBufferIndex < (SAM4E_ETH_TX_BUFFER_COUNT - 1))
514  {
515  //Write the status word
516  txBufferDesc[txBufferIndex].status = GMAC_TX_LAST |
518 
519  //Point to the next buffer
520  txBufferIndex++;
521  }
522  else
523  {
524  //Write the status word
525  txBufferDesc[txBufferIndex].status = GMAC_TX_WRAP | GMAC_TX_LAST |
527 
528  //Wrap around
529  txBufferIndex = 0;
530  }
531 
532  //Set the TSTART bit to initiate transmission
533  GMAC->GMAC_NCR |= GMAC_NCR_TSTART;
534 
535  //Check whether the next buffer is available for writing
536  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
537  {
538  //The transmitter can accept another packet
539  osSetEvent(&interface->nicTxEvent);
540  }
541 
542  //Successful processing
543  return NO_ERROR;
544 }
545 
546 
547 /**
548  * @brief Receive a packet
549  * @param[in] interface Underlying network interface
550  * @return Error code
551  **/
552 
554 {
555  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
556  error_t error;
557  uint_t i;
558  uint_t j;
559  uint_t sofIndex;
560  uint_t eofIndex;
561  size_t n;
562  size_t size;
563  size_t length;
564 
565  //Initialize variables
566  size = 0;
567  sofIndex = UINT_MAX;
568  eofIndex = UINT_MAX;
569 
570  //Search for SOF and EOF flags
571  for(i = 0; i < SAM4E_ETH_RX_BUFFER_COUNT; i++)
572  {
573  //Point to the current entry
574  j = rxBufferIndex + i;
575 
576  //Wrap around to the beginning of the buffer if necessary
578  {
580  }
581 
582  //No more entries to process?
583  if((rxBufferDesc[j].address & GMAC_RX_OWNERSHIP) == 0)
584  {
585  //Stop processing
586  break;
587  }
588 
589  //A valid SOF has been found?
590  if((rxBufferDesc[j].status & GMAC_RX_SOF) != 0)
591  {
592  //Save the position of the SOF
593  sofIndex = i;
594  }
595 
596  //A valid EOF has been found?
597  if((rxBufferDesc[j].status & GMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
598  {
599  //Save the position of the EOF
600  eofIndex = i;
601  //Retrieve the length of the frame
602  size = rxBufferDesc[j].status & GMAC_RX_LENGTH;
603  //Limit the number of data to read
604  size = MIN(size, ETH_MAX_FRAME_SIZE);
605  //Stop processing since we have reached the end of the frame
606  break;
607  }
608  }
609 
610  //Determine the number of entries to process
611  if(eofIndex != UINT_MAX)
612  {
613  j = eofIndex + 1;
614  }
615  else if(sofIndex != UINT_MAX)
616  {
617  j = sofIndex;
618  }
619  else
620  {
621  j = i;
622  }
623 
624  //Total number of bytes that have been copied from the receive buffer
625  length = 0;
626 
627  //Process incoming frame
628  for(i = 0; i < j; i++)
629  {
630  //Any data to copy from current buffer?
631  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
632  {
633  //Calculate the number of bytes to read at a time
634  n = MIN(size, SAM4E_ETH_RX_BUFFER_SIZE);
635  //Copy data from receive buffer
636  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
637  //Update byte counters
638  length += n;
639  size -= n;
640  }
641 
642  //Mark the current buffer as free
643  rxBufferDesc[rxBufferIndex].address &= ~GMAC_RX_OWNERSHIP;
644 
645  //Point to the following entry
646  rxBufferIndex++;
647 
648  //Wrap around to the beginning of the buffer if necessary
649  if(rxBufferIndex >= SAM4E_ETH_RX_BUFFER_COUNT)
650  {
651  rxBufferIndex = 0;
652  }
653  }
654 
655  //Any packet to process?
656  if(length > 0)
657  {
658  NetRxAncillary ancillary;
659 
660  //Additional options can be passed to the stack along with the packet
661  ancillary = NET_DEFAULT_RX_ANCILLARY;
662 
663  //Pass the packet to the upper layer
664  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
665  //Valid packet received
666  error = NO_ERROR;
667  }
668  else
669  {
670  //No more data in the receive buffer
671  error = ERROR_BUFFER_EMPTY;
672  }
673 
674  //Return status code
675  return error;
676 }
677 
678 
679 /**
680  * @brief Configure MAC address filtering
681  * @param[in] interface Underlying network interface
682  * @return Error code
683  **/
684 
686 {
687  uint_t i;
688  uint_t j;
689  uint_t k;
690  uint8_t *p;
691  uint32_t hashTable[2];
692  MacAddr unicastMacAddr[3];
693  MacFilterEntry *entry;
694 
695  //Debug message
696  TRACE_DEBUG("Updating MAC filter...\r\n");
697 
698  //Set the MAC address of the station
699  GMAC->GMAC_SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
700  GMAC->GMAC_SA[0].GMAC_SAT = interface->macAddr.w[2];
701 
702  //The MAC supports 3 additional addresses for unicast perfect filtering
703  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
704  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
705  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
706 
707  //The hash table is used for multicast address filtering
708  hashTable[0] = 0;
709  hashTable[1] = 0;
710 
711  //The MAC address filter contains the list of MAC addresses to accept
712  //when receiving an Ethernet frame
713  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
714  {
715  //Point to the current entry
716  entry = &interface->macAddrFilter[i];
717 
718  //Valid entry?
719  if(entry->refCount > 0)
720  {
721  //Multicast address?
722  if(macIsMulticastAddr(&entry->addr))
723  {
724  //Point to the MAC address
725  p = entry->addr.b;
726 
727  //Apply the hash function
728  k = (p[0] >> 6) ^ p[0];
729  k ^= (p[1] >> 4) ^ (p[1] << 2);
730  k ^= (p[2] >> 2) ^ (p[2] << 4);
731  k ^= (p[3] >> 6) ^ p[3];
732  k ^= (p[4] >> 4) ^ (p[4] << 2);
733  k ^= (p[5] >> 2) ^ (p[5] << 4);
734 
735  //The hash value is reduced to a 6-bit index
736  k &= 0x3F;
737 
738  //Update hash table contents
739  hashTable[k / 32] |= (1 << (k % 32));
740  }
741  else
742  {
743  //Up to 3 additional MAC addresses can be specified
744  if(j < 3)
745  {
746  //Save the unicast address
747  unicastMacAddr[j++] = entry->addr;
748  }
749  }
750  }
751  }
752 
753  //Configure the first unicast address filter
754  if(j >= 1)
755  {
756  //The address is activated when SAT register is written
757  GMAC->GMAC_SA[1].GMAC_SAB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
758  GMAC->GMAC_SA[1].GMAC_SAT = unicastMacAddr[0].w[2];
759  }
760  else
761  {
762  //The address is deactivated when SAB register is written
763  GMAC->GMAC_SA[1].GMAC_SAB = 0;
764  }
765 
766  //Configure the second unicast address filter
767  if(j >= 2)
768  {
769  //The address is activated when SAT register is written
770  GMAC->GMAC_SA[2].GMAC_SAB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
771  GMAC->GMAC_SA[2].GMAC_SAT = unicastMacAddr[1].w[2];
772  }
773  else
774  {
775  //The address is deactivated when SAB register is written
776  GMAC->GMAC_SA[2].GMAC_SAB = 0;
777  }
778 
779  //Configure the third unicast address filter
780  if(j >= 3)
781  {
782  //The address is activated when SAT register is written
783  GMAC->GMAC_SA[3].GMAC_SAB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
784  GMAC->GMAC_SA[3].GMAC_SAT = unicastMacAddr[2].w[2];
785  }
786  else
787  {
788  //The address is deactivated when SAB register is written
789  GMAC->GMAC_SA[3].GMAC_SAB = 0;
790  }
791 
792  //Configure the multicast hash table
793  GMAC->GMAC_HRB = hashTable[0];
794  GMAC->GMAC_HRT = hashTable[1];
795 
796  //Debug message
797  TRACE_DEBUG(" HRB = %08" PRIX32 "\r\n", GMAC->GMAC_HRB);
798  TRACE_DEBUG(" HRT = %08" PRIX32 "\r\n", GMAC->GMAC_HRT);
799 
800  //Successful processing
801  return NO_ERROR;
802 }
803 
804 
805 /**
806  * @brief Adjust MAC configuration parameters for proper operation
807  * @param[in] interface Underlying network interface
808  * @return Error code
809  **/
810 
812 {
813  uint32_t config;
814 
815  //Read network configuration register
816  config = GMAC->GMAC_NCFGR;
817 
818  //10BASE-T or 100BASE-TX operation mode?
819  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
820  {
821  config |= GMAC_NCFGR_SPD;
822  }
823  else
824  {
825  config &= ~GMAC_NCFGR_SPD;
826  }
827 
828  //Half-duplex or full-duplex mode?
829  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
830  {
831  config |= GMAC_NCFGR_FD;
832  }
833  else
834  {
835  config &= ~GMAC_NCFGR_FD;
836  }
837 
838  //Write configuration value back to NCFGR register
839  GMAC->GMAC_NCFGR = config;
840 
841  //Successful processing
842  return NO_ERROR;
843 }
844 
845 
846 /**
847  * @brief Write PHY register
848  * @param[in] opcode Access type (2 bits)
849  * @param[in] phyAddr PHY address (5 bits)
850  * @param[in] regAddr Register address (5 bits)
851  * @param[in] data Register value
852  **/
853 
854 void sam4eEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
855  uint8_t regAddr, uint16_t data)
856 {
857  uint32_t temp;
858 
859  //Valid opcode?
860  if(opcode == SMI_OPCODE_WRITE)
861  {
862  //Set up a write operation
863  temp = GMAC_MAN_CLTTO | GMAC_MAN_OP(1) | GMAC_MAN_WTN(2);
864  //PHY address
865  temp |= GMAC_MAN_PHYA(phyAddr);
866  //Register address
867  temp |= GMAC_MAN_REGA(regAddr);
868  //Register value
869  temp |= GMAC_MAN_DATA(data);
870 
871  //Start a write operation
872  GMAC->GMAC_MAN = temp;
873  //Wait for the write to complete
874  while((GMAC->GMAC_NSR & GMAC_NSR_IDLE) == 0)
875  {
876  }
877  }
878  else
879  {
880  //The MAC peripheral only supports standard Clause 22 opcodes
881  }
882 }
883 
884 
885 /**
886  * @brief Read PHY register
887  * @param[in] opcode Access type (2 bits)
888  * @param[in] phyAddr PHY address (5 bits)
889  * @param[in] regAddr Register address (5 bits)
890  * @return Register value
891  **/
892 
893 uint16_t sam4eEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
894  uint8_t regAddr)
895 {
896  uint16_t data;
897  uint32_t temp;
898 
899  //Valid opcode?
900  if(opcode == SMI_OPCODE_READ)
901  {
902  //Set up a read operation
903  temp = GMAC_MAN_CLTTO | GMAC_MAN_OP(2) | GMAC_MAN_WTN(2);
904  //PHY address
905  temp |= GMAC_MAN_PHYA(phyAddr);
906  //Register address
907  temp |= GMAC_MAN_REGA(regAddr);
908 
909  //Start a read operation
910  GMAC->GMAC_MAN = temp;
911  //Wait for the read to complete
912  while((GMAC->GMAC_NSR & GMAC_NSR_IDLE) == 0)
913  {
914  }
915 
916  //Get register value
917  data = GMAC->GMAC_MAN & GMAC_MAN_DATA_Msk;
918  }
919  else
920  {
921  //The MAC peripheral only supports standard Clause 22 opcodes
922  data = 0;
923  }
924 
925  //Return the value of the PHY register
926  return data;
927 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
error_t sam4eEthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
error_t sam4eEthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
SAM4E Ethernet MAC driver.
uint8_t opcode
Definition: dns_common.h:188
int bool_t
Definition: compiler_port.h:61
#define GMAC_TX_LENGTH
uint16_t sam4eEthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define netEvent
Definition: net_legacy.h:196
@ 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
#define PIO_PD6A_GRX1
uint8_t p
Definition: ndp.h:300
#define SAM4E_ETH_RX_BUFFER_SIZE
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:222
#define ETH_MAX_FRAME_SIZE
Definition: ethernet.h:110
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
__weak_func void sam4eEthInitGpio(NetInterface *interface)
GPIO configuration.
#define GMAC_RX_WRAP
Transmit buffer descriptor.
#define GMAC_MAN_PHYA
error_t sam4eEthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:392
#define macIsMulticastAddr(macAddr)
Definition: ethernet.h:133
#define osExitIsr(flag)
#define GMAC_RX_EOF
#define GMAC_MAN_DATA
#define SMI_OPCODE_WRITE
Definition: nic.h:66
void sam4eEthTick(NetInterface *interface)
SAM4E Ethernet MAC timer handler.
Receive buffer descriptor.
#define GMAC_TX_USED
#define GMAC_MAN_OP
#define FALSE
Definition: os_port.h:46
#define GMAC_IRQn
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
error_t
Error codes.
Definition: error.h:43
#define GMAC_RX_ADDRESS
#define SAM4E_ETH_IRQ_PRIORITY_GROUPING
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:105
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
#define SAM4E_ETH_IRQ_SUB_PRIORITY
#define txBuffer
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:36
MacAddr addr
MAC address.
Definition: ethernet.h:263
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_BUFFER_EMPTY
Definition: error.h:142
#define SAM4E_ETH_RX_BUFFER_COUNT
#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
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
error_t sam4eEthInit(NetInterface *interface)
SAM4E Ethernet MAC initialization.
#define MIN(a, b)
Definition: os_port.h:63
#define rxBuffer
#define GMAC_RX_SOF
MacAddr
Definition: ethernet.h:195
#define TRACE_DEBUG(...)
Definition: debug.h:119
void sam4eEthInitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
void sam4eEthEventHandler(NetInterface *interface)
SAM4E Ethernet MAC event handler.
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:262
Ipv6Addr address[]
Definition: ipv6.h:325
#define SAM4E_ETH_TX_BUFFER_SIZE
#define osEnterIsr()
error_t sam4eEthReceivePacket(NetInterface *interface)
Receive a packet.
const NicDriver sam4eEthDriver
SAM4E Ethernet MAC driver.
#define GMAC_MAN_WTN
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void sam4eEthDisableIrq(NetInterface *interface)
Disable interrupts.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
void sam4eEthEnableIrq(NetInterface *interface)
Enable interrupts.
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
#define SAM4E_ETH_TX_BUFFER_COUNT
#define SAM4E_ETH_IRQ_GROUP_PRIORITY
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
#define GMAC_MAN_DATA_Msk
@ NO_ERROR
Success.
Definition: error.h:44
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
Debugging facilities.
void sam4eEthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
#define GMAC_TX_WRAP
void GMAC_Handler(void)
SAM4E Ethernet MAC interrupt service routine.
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83