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-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
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  //Enable PIO peripheral clock
231  PMC->PMC_PCER0 = (1 << ID_PIOD);
232 
233  //Disable pull-up resistors on MII pins
234  PIOD->PIO_PUDR = GMAC_MII_MASK;
235  //Disable interrupts-on-change
236  PIOD->PIO_IDR = GMAC_MII_MASK;
237  //Assign MII pins to peripheral A function
238  PIOD->PIO_ABCDSR[0] &= ~GMAC_MII_MASK;
239  PIOD->PIO_ABCDSR[1] &= ~GMAC_MII_MASK;
240  //Disable the PIO from controlling the corresponding pins
241  PIOD->PIO_PDR = GMAC_MII_MASK;
242 
243  //Select MII operation mode
244  GMAC->GMAC_UR = GMAC_UR_RMIIMII;
245 #endif
246 }
247 
248 
249 /**
250  * @brief Initialize buffer descriptors
251  * @param[in] interface Underlying network interface
252  **/
253 
255 {
256  uint_t i;
257  uint32_t address;
258 
259  //Initialize TX buffer descriptors
260  for(i = 0; i < SAM4E_ETH_TX_BUFFER_COUNT; i++)
261  {
262  //Calculate the address of the current TX buffer
263  address = (uint32_t) txBuffer[i];
264  //Write the address to the descriptor entry
265  txBufferDesc[i].address = address;
266  //Initialize status field
267  txBufferDesc[i].status = GMAC_TX_USED;
268  }
269 
270  //Mark the last descriptor entry with the wrap flag
271  txBufferDesc[i - 1].status |= GMAC_TX_WRAP;
272  //Initialize TX buffer index
273  txBufferIndex = 0;
274 
275  //Initialize RX buffer descriptors
276  for(i = 0; i < SAM4E_ETH_RX_BUFFER_COUNT; i++)
277  {
278  //Calculate the address of the current RX buffer
279  address = (uint32_t) rxBuffer[i];
280  //Write the address to the descriptor entry
281  rxBufferDesc[i].address = address & GMAC_RX_ADDRESS;
282  //Clear status field
283  rxBufferDesc[i].status = 0;
284  }
285 
286  //Mark the last descriptor entry with the wrap flag
287  rxBufferDesc[i - 1].address |= GMAC_RX_WRAP;
288  //Initialize RX buffer index
289  rxBufferIndex = 0;
290 
291  //Start location of the TX descriptor list
292  GMAC->GMAC_TBQB = (uint32_t) txBufferDesc;
293  //Start location of the RX descriptor list
294  GMAC->GMAC_RBQB = (uint32_t) rxBufferDesc;
295 }
296 
297 
298 /**
299  * @brief SAM4E Ethernet MAC timer handler
300  *
301  * This routine is periodically called by the TCP/IP stack to handle periodic
302  * operations such as polling the link state
303  *
304  * @param[in] interface Underlying network interface
305  **/
306 
307 void sam4eEthTick(NetInterface *interface)
308 {
309  //Valid Ethernet PHY or switch driver?
310  if(interface->phyDriver != NULL)
311  {
312  //Handle periodic operations
313  interface->phyDriver->tick(interface);
314  }
315  else if(interface->switchDriver != NULL)
316  {
317  //Handle periodic operations
318  interface->switchDriver->tick(interface);
319  }
320  else
321  {
322  //Just for sanity
323  }
324 }
325 
326 
327 /**
328  * @brief Enable interrupts
329  * @param[in] interface Underlying network interface
330  **/
331 
333 {
334  //Enable Ethernet MAC interrupts
335  NVIC_EnableIRQ(GMAC_IRQn);
336 
337  //Valid Ethernet PHY or switch driver?
338  if(interface->phyDriver != NULL)
339  {
340  //Enable Ethernet PHY interrupts
341  interface->phyDriver->enableIrq(interface);
342  }
343  else if(interface->switchDriver != NULL)
344  {
345  //Enable Ethernet switch interrupts
346  interface->switchDriver->enableIrq(interface);
347  }
348  else
349  {
350  //Just for sanity
351  }
352 }
353 
354 
355 /**
356  * @brief Disable interrupts
357  * @param[in] interface Underlying network interface
358  **/
359 
361 {
362  //Disable Ethernet MAC interrupts
363  NVIC_DisableIRQ(GMAC_IRQn);
364 
365  //Valid Ethernet PHY or switch driver?
366  if(interface->phyDriver != NULL)
367  {
368  //Disable Ethernet PHY interrupts
369  interface->phyDriver->disableIrq(interface);
370  }
371  else if(interface->switchDriver != NULL)
372  {
373  //Disable Ethernet switch interrupts
374  interface->switchDriver->disableIrq(interface);
375  }
376  else
377  {
378  //Just for sanity
379  }
380 }
381 
382 
383 /**
384  * @brief SAM4E Ethernet MAC interrupt service routine
385  **/
386 
387 void GMAC_Handler(void)
388 {
389  bool_t flag;
390  volatile uint32_t isr;
391  volatile uint32_t tsr;
392  volatile uint32_t rsr;
393 
394  //Interrupt service routine prologue
395  osEnterIsr();
396 
397  //This flag will be set if a higher priority task must be woken
398  flag = FALSE;
399 
400  //Each time the software reads GMAC_ISR, it has to check the contents
401  //of GMAC_TSR, GMAC_RSR and GMAC_NSR
402  isr = GMAC->GMAC_ISR;
403  tsr = GMAC->GMAC_TSR;
404  rsr = GMAC->GMAC_RSR;
405  (void) isr;
406 
407  //Packet transmitted?
408  if((tsr & (GMAC_TSR_HRESP | GMAC_TSR_UND | GMAC_TSR_TXCOMP | GMAC_TSR_TFC |
409  GMAC_TSR_TXGO | GMAC_TSR_RLE | GMAC_TSR_COL | GMAC_TSR_UBR)) != 0)
410  {
411  //Only clear TSR flags that are currently set
412  GMAC->GMAC_TSR = tsr;
413 
414  //Check whether the TX buffer is available for writing
415  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
416  {
417  //Notify the TCP/IP stack that the transmitter is ready to send
418  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
419  }
420  }
421 
422  //Packet received?
423  if((rsr & (GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC | GMAC_RSR_BNA)) != 0)
424  {
425  //Set event flag
426  nicDriverInterface->nicEvent = TRUE;
427  //Notify the TCP/IP stack of the event
428  flag |= osSetEventFromIsr(&netEvent);
429  }
430 
431  //Interrupt service routine epilogue
432  osExitIsr(flag);
433 }
434 
435 
436 /**
437  * @brief SAM4E Ethernet MAC event handler
438  * @param[in] interface Underlying network interface
439  **/
440 
442 {
443  error_t error;
444  uint32_t rsr;
445 
446  //Read receive status
447  rsr = GMAC->GMAC_RSR;
448 
449  //Packet received?
450  if((rsr & (GMAC_RSR_HNO | GMAC_RSR_RXOVR | GMAC_RSR_REC | GMAC_RSR_BNA)) != 0)
451  {
452  //Only clear RSR flags that are currently set
453  GMAC->GMAC_RSR = rsr;
454 
455  //Process all pending packets
456  do
457  {
458  //Read incoming packet
459  error = sam4eEthReceivePacket(interface);
460 
461  //No more data in the receive buffer?
462  } while(error != ERROR_BUFFER_EMPTY);
463  }
464 }
465 
466 
467 /**
468  * @brief Send a packet
469  * @param[in] interface Underlying network interface
470  * @param[in] buffer Multi-part buffer containing the data to send
471  * @param[in] offset Offset to the first data byte
472  * @param[in] ancillary Additional options passed to the stack along with
473  * the packet
474  * @return Error code
475  **/
476 
478  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
479 {
480  size_t length;
481 
482  //Retrieve the length of the packet
483  length = netBufferGetLength(buffer) - offset;
484 
485  //Check the frame length
487  {
488  //The transmitter can accept another packet
489  osSetEvent(&interface->nicTxEvent);
490  //Report an error
491  return ERROR_INVALID_LENGTH;
492  }
493 
494  //Make sure the current buffer is available for writing
495  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) == 0)
496  {
497  return ERROR_FAILURE;
498  }
499 
500  //Copy user data to the transmit buffer
501  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
502 
503  //Set the necessary flags in the descriptor entry
504  if(txBufferIndex < (SAM4E_ETH_TX_BUFFER_COUNT - 1))
505  {
506  //Write the status word
507  txBufferDesc[txBufferIndex].status = GMAC_TX_LAST |
509 
510  //Point to the next buffer
511  txBufferIndex++;
512  }
513  else
514  {
515  //Write the status word
516  txBufferDesc[txBufferIndex].status = GMAC_TX_WRAP | GMAC_TX_LAST |
518 
519  //Wrap around
520  txBufferIndex = 0;
521  }
522 
523  //Set the TSTART bit to initiate transmission
524  GMAC->GMAC_NCR |= GMAC_NCR_TSTART;
525 
526  //Check whether the next buffer is available for writing
527  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
528  {
529  //The transmitter can accept another packet
530  osSetEvent(&interface->nicTxEvent);
531  }
532 
533  //Successful processing
534  return NO_ERROR;
535 }
536 
537 
538 /**
539  * @brief Receive a packet
540  * @param[in] interface Underlying network interface
541  * @return Error code
542  **/
543 
545 {
546  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
547  error_t error;
548  uint_t i;
549  uint_t j;
550  uint_t sofIndex;
551  uint_t eofIndex;
552  size_t n;
553  size_t size;
554  size_t length;
555 
556  //Initialize variables
557  size = 0;
558  sofIndex = UINT_MAX;
559  eofIndex = UINT_MAX;
560 
561  //Search for SOF and EOF flags
562  for(i = 0; i < SAM4E_ETH_RX_BUFFER_COUNT; i++)
563  {
564  //Point to the current entry
565  j = rxBufferIndex + i;
566 
567  //Wrap around to the beginning of the buffer if necessary
569  {
571  }
572 
573  //No more entries to process?
574  if((rxBufferDesc[j].address & GMAC_RX_OWNERSHIP) == 0)
575  {
576  //Stop processing
577  break;
578  }
579 
580  //A valid SOF has been found?
581  if((rxBufferDesc[j].status & GMAC_RX_SOF) != 0)
582  {
583  //Save the position of the SOF
584  sofIndex = i;
585  }
586 
587  //A valid EOF has been found?
588  if((rxBufferDesc[j].status & GMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
589  {
590  //Save the position of the EOF
591  eofIndex = i;
592  //Retrieve the length of the frame
593  size = rxBufferDesc[j].status & GMAC_RX_LENGTH;
594  //Limit the number of data to read
595  size = MIN(size, ETH_MAX_FRAME_SIZE);
596  //Stop processing since we have reached the end of the frame
597  break;
598  }
599  }
600 
601  //Determine the number of entries to process
602  if(eofIndex != UINT_MAX)
603  {
604  j = eofIndex + 1;
605  }
606  else if(sofIndex != UINT_MAX)
607  {
608  j = sofIndex;
609  }
610  else
611  {
612  j = i;
613  }
614 
615  //Total number of bytes that have been copied from the receive buffer
616  length = 0;
617 
618  //Process incoming frame
619  for(i = 0; i < j; i++)
620  {
621  //Any data to copy from current buffer?
622  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
623  {
624  //Calculate the number of bytes to read at a time
625  n = MIN(size, SAM4E_ETH_RX_BUFFER_SIZE);
626  //Copy data from receive buffer
627  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
628  //Update byte counters
629  length += n;
630  size -= n;
631  }
632 
633  //Mark the current buffer as free
634  rxBufferDesc[rxBufferIndex].address &= ~GMAC_RX_OWNERSHIP;
635 
636  //Point to the following entry
637  rxBufferIndex++;
638 
639  //Wrap around to the beginning of the buffer if necessary
640  if(rxBufferIndex >= SAM4E_ETH_RX_BUFFER_COUNT)
641  {
642  rxBufferIndex = 0;
643  }
644  }
645 
646  //Any packet to process?
647  if(length > 0)
648  {
649  NetRxAncillary ancillary;
650 
651  //Additional options can be passed to the stack along with the packet
652  ancillary = NET_DEFAULT_RX_ANCILLARY;
653 
654  //Pass the packet to the upper layer
655  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
656  //Valid packet received
657  error = NO_ERROR;
658  }
659  else
660  {
661  //No more data in the receive buffer
662  error = ERROR_BUFFER_EMPTY;
663  }
664 
665  //Return status code
666  return error;
667 }
668 
669 
670 /**
671  * @brief Configure MAC address filtering
672  * @param[in] interface Underlying network interface
673  * @return Error code
674  **/
675 
677 {
678  uint_t i;
679  uint_t j;
680  uint_t k;
681  uint8_t *p;
682  uint32_t hashTable[2];
683  MacAddr unicastMacAddr[3];
684  MacFilterEntry *entry;
685 
686  //Debug message
687  TRACE_DEBUG("Updating MAC filter...\r\n");
688 
689  //Set the MAC address of the station
690  GMAC->GMAC_SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
691  GMAC->GMAC_SA[0].GMAC_SAT = interface->macAddr.w[2];
692 
693  //The MAC supports 3 additional addresses for unicast perfect filtering
694  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
695  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
696  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
697 
698  //The hash table is used for multicast address filtering
699  hashTable[0] = 0;
700  hashTable[1] = 0;
701 
702  //The MAC address filter contains the list of MAC addresses to accept
703  //when receiving an Ethernet frame
704  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
705  {
706  //Point to the current entry
707  entry = &interface->macAddrFilter[i];
708 
709  //Valid entry?
710  if(entry->refCount > 0)
711  {
712  //Multicast address?
713  if(macIsMulticastAddr(&entry->addr))
714  {
715  //Point to the MAC address
716  p = entry->addr.b;
717 
718  //Apply the hash function
719  k = (p[0] >> 6) ^ p[0];
720  k ^= (p[1] >> 4) ^ (p[1] << 2);
721  k ^= (p[2] >> 2) ^ (p[2] << 4);
722  k ^= (p[3] >> 6) ^ p[3];
723  k ^= (p[4] >> 4) ^ (p[4] << 2);
724  k ^= (p[5] >> 2) ^ (p[5] << 4);
725 
726  //The hash value is reduced to a 6-bit index
727  k &= 0x3F;
728 
729  //Update hash table contents
730  hashTable[k / 32] |= (1 << (k % 32));
731  }
732  else
733  {
734  //Up to 3 additional MAC addresses can be specified
735  if(j < 3)
736  {
737  //Save the unicast address
738  unicastMacAddr[j++] = entry->addr;
739  }
740  }
741  }
742  }
743 
744  //Configure the first unicast address filter
745  if(j >= 1)
746  {
747  //The address is activated when SAT register is written
748  GMAC->GMAC_SA[1].GMAC_SAB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
749  GMAC->GMAC_SA[1].GMAC_SAT = unicastMacAddr[0].w[2];
750  }
751  else
752  {
753  //The address is deactivated when SAB register is written
754  GMAC->GMAC_SA[1].GMAC_SAB = 0;
755  }
756 
757  //Configure the second unicast address filter
758  if(j >= 2)
759  {
760  //The address is activated when SAT register is written
761  GMAC->GMAC_SA[2].GMAC_SAB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
762  GMAC->GMAC_SA[2].GMAC_SAT = unicastMacAddr[1].w[2];
763  }
764  else
765  {
766  //The address is deactivated when SAB register is written
767  GMAC->GMAC_SA[2].GMAC_SAB = 0;
768  }
769 
770  //Configure the third unicast address filter
771  if(j >= 3)
772  {
773  //The address is activated when SAT register is written
774  GMAC->GMAC_SA[3].GMAC_SAB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
775  GMAC->GMAC_SA[3].GMAC_SAT = unicastMacAddr[2].w[2];
776  }
777  else
778  {
779  //The address is deactivated when SAB register is written
780  GMAC->GMAC_SA[3].GMAC_SAB = 0;
781  }
782 
783  //Configure the multicast hash table
784  GMAC->GMAC_HRB = hashTable[0];
785  GMAC->GMAC_HRT = hashTable[1];
786 
787  //Debug message
788  TRACE_DEBUG(" HRB = %08" PRIX32 "\r\n", GMAC->GMAC_HRB);
789  TRACE_DEBUG(" HRT = %08" PRIX32 "\r\n", GMAC->GMAC_HRT);
790 
791  //Successful processing
792  return NO_ERROR;
793 }
794 
795 
796 /**
797  * @brief Adjust MAC configuration parameters for proper operation
798  * @param[in] interface Underlying network interface
799  * @return Error code
800  **/
801 
803 {
804  uint32_t config;
805 
806  //Read network configuration register
807  config = GMAC->GMAC_NCFGR;
808 
809  //10BASE-T or 100BASE-TX operation mode?
810  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
811  {
812  config |= GMAC_NCFGR_SPD;
813  }
814  else
815  {
816  config &= ~GMAC_NCFGR_SPD;
817  }
818 
819  //Half-duplex or full-duplex mode?
820  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
821  {
822  config |= GMAC_NCFGR_FD;
823  }
824  else
825  {
826  config &= ~GMAC_NCFGR_FD;
827  }
828 
829  //Write configuration value back to NCFGR register
830  GMAC->GMAC_NCFGR = config;
831 
832  //Successful processing
833  return NO_ERROR;
834 }
835 
836 
837 /**
838  * @brief Write PHY register
839  * @param[in] opcode Access type (2 bits)
840  * @param[in] phyAddr PHY address (5 bits)
841  * @param[in] regAddr Register address (5 bits)
842  * @param[in] data Register value
843  **/
844 
845 void sam4eEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
846  uint8_t regAddr, uint16_t data)
847 {
848  uint32_t temp;
849 
850  //Valid opcode?
851  if(opcode == SMI_OPCODE_WRITE)
852  {
853  //Set up a write operation
854  temp = GMAC_MAN_CLTTO | GMAC_MAN_OP(1) | GMAC_MAN_WTN(2);
855  //PHY address
856  temp |= GMAC_MAN_PHYA(phyAddr);
857  //Register address
858  temp |= GMAC_MAN_REGA(regAddr);
859  //Register value
860  temp |= GMAC_MAN_DATA(data);
861 
862  //Start a write operation
863  GMAC->GMAC_MAN = temp;
864  //Wait for the write to complete
865  while((GMAC->GMAC_NSR & GMAC_NSR_IDLE) == 0)
866  {
867  }
868  }
869  else
870  {
871  //The MAC peripheral only supports standard Clause 22 opcodes
872  }
873 }
874 
875 
876 /**
877  * @brief Read PHY register
878  * @param[in] opcode Access type (2 bits)
879  * @param[in] phyAddr PHY address (5 bits)
880  * @param[in] regAddr Register address (5 bits)
881  * @return Register value
882  **/
883 
884 uint16_t sam4eEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
885  uint8_t regAddr)
886 {
887  uint16_t data;
888  uint32_t temp;
889 
890  //Valid opcode?
891  if(opcode == SMI_OPCODE_READ)
892  {
893  //Set up a read operation
894  temp = GMAC_MAN_CLTTO | GMAC_MAN_OP(2) | GMAC_MAN_WTN(2);
895  //PHY address
896  temp |= GMAC_MAN_PHYA(phyAddr);
897  //Register address
898  temp |= GMAC_MAN_REGA(regAddr);
899 
900  //Start a read operation
901  GMAC->GMAC_MAN = temp;
902  //Wait for the read to complete
903  while((GMAC->GMAC_NSR & GMAC_NSR_IDLE) == 0)
904  {
905  }
906 
907  //Get register value
908  data = GMAC->GMAC_MAN & GMAC_MAN_DATA_Msk;
909  }
910  else
911  {
912  //The MAC peripheral only supports standard Clause 22 opcodes
913  data = 0;
914  }
915 
916  //Return the value of the PHY register
917  return data;
918 }
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:53
#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
#define GMAC_MII_MASK
@ 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
#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.
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 SMI_OPCODE_WRITE
Definition: nic.h:66
void sam4eEthTick(NetInterface *interface)
SAM4E Ethernet MAC timer handler.
Receive buffer descriptor.
#define GMAC_TX_USED
#define FALSE
Definition: os_port.h:46
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
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:104
@ 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:141
#define SAM4E_ETH_RX_BUFFER_COUNT
#define NetTxAncillary
Definition: net_misc.h:36
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:95
uint8_t length
Definition: tcp.h:368
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:107
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.
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:50
TCP/IP stack core.
NIC driver.
Definition: nic.h:286
#define GMAC_RX_OWNERSHIP
#define SAM4E_ETH_TX_BUFFER_COUNT
#define SAM4E_ETH_IRQ_GROUP_PRIORITY
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
@ 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