same53_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file same53_eth_driver.c
3  * @brief SAME53 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 "sam.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
56 //RX buffer descriptors
57 #pragma data_alignment = 4
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 SAME53 Ethernet MAC driver
86  **/
87 
89 {
91  ETH_MTU,
102  TRUE,
103  TRUE,
104  TRUE,
105  FALSE
106 };
107 
108 
109 /**
110  * @brief SAME53 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 SAME53 Ethernet MAC...\r\n");
122 
123  //Save underlying network interface
124  nicDriverInterface = interface;
125 
126  //Enable GMAC bus clocks (CLK_GMAC_APB and CLK_GMAC_AHB)
127  MCLK_REGS->MCLK_APBCMASK |= MCLK_APBCMASK_GMAC_Msk;
128  MCLK_REGS->MCLK_AHBMASK |= MCLK_AHBMASK_GMAC_Msk;
129 
130  //Disable transmit and receive circuits
131  GMAC_REGS->GMAC_NCR = 0;
132 
133  //GPIO configuration
134  same53EthInitGpio(interface);
135 
136  //Configure MDC clock speed
137  GMAC_REGS->GMAC_NCFGR = GMAC_NCFGR_CLK(5);
138  //Enable management port (MDC and MDIO)
139  GMAC_REGS->GMAC_NCR |= GMAC_NCR_MPE_Msk;
140 
141  //Valid Ethernet PHY or switch driver?
142  if(interface->phyDriver != NULL)
143  {
144  //Ethernet PHY initialization
145  error = interface->phyDriver->init(interface);
146  }
147  else if(interface->switchDriver != NULL)
148  {
149  //Ethernet switch initialization
150  error = interface->switchDriver->init(interface);
151  }
152  else
153  {
154  //The interface is not properly configured
155  error = ERROR_FAILURE;
156  }
157 
158  //Any error to report?
159  if(error)
160  {
161  return error;
162  }
163 
164  //Set the MAC address of the station
165  GMAC_REGS->SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
166  GMAC_REGS->SA[0].GMAC_SAT = interface->macAddr.w[2];
167 
168  //The MAC supports 3 additional addresses for unicast perfect filtering
169  GMAC_REGS->SA[1].GMAC_SAB = 0;
170  GMAC_REGS->SA[2].GMAC_SAB = 0;
171  GMAC_REGS->SA[3].GMAC_SAB = 0;
172 
173  //Initialize hash table
174  GMAC_REGS->GMAC_HRB = 0;
175  GMAC_REGS->GMAC_HRT = 0;
176 
177  //Configure the receive filter
178  GMAC_REGS->GMAC_NCFGR |= GMAC_NCFGR_MAXFS_Msk | GMAC_NCFGR_MTIHEN_Msk;
179 
180  //Initialize buffer descriptors
181  same53EthInitBufferDesc(interface);
182 
183  //Clear transmit status register
184  GMAC_REGS->GMAC_TSR = GMAC_TSR_HRESP_Msk | GMAC_TSR_UND_Msk |
185  GMAC_TSR_TXCOMP_Msk | GMAC_TSR_TFC_Msk | GMAC_TSR_TXGO_Msk |
186  GMAC_TSR_RLE_Msk | GMAC_TSR_COL_Msk | GMAC_TSR_UBR_Msk;
187 
188  //Clear receive status register
189  GMAC_REGS->GMAC_RSR = GMAC_RSR_HNO_Msk | GMAC_RSR_RXOVR_Msk |
190  GMAC_RSR_REC_Msk | GMAC_RSR_BNA_Msk;
191 
192  //First disable all GMAC interrupts
193  GMAC_REGS->GMAC_IDR = 0xFFFFFFFF;
194 
195  //Only the desired ones are enabled
196  GMAC_REGS->GMAC_IER = GMAC_IER_HRESP_Msk | GMAC_IER_ROVR_Msk |
197  GMAC_IER_TCOMP_Msk | GMAC_IER_TFC_Msk | GMAC_IER_RLEX_Msk |
198  GMAC_IER_TUR_Msk | GMAC_IER_RXUBR_Msk | GMAC_IER_RCOMP_Msk;
199 
200  //Read GMAC_ISR register to clear any pending interrupt
201  status = GMAC_REGS->GMAC_ISR;
202  (void) status;
203 
204  //Set priority grouping (3 bits for pre-emption priority, no bits for subpriority)
205  NVIC_SetPriorityGrouping(SAME53_ETH_IRQ_PRIORITY_GROUPING);
206 
207  //Configure GMAC interrupt priority
208  NVIC_SetPriority(GMAC_IRQn, NVIC_EncodePriority(SAME53_ETH_IRQ_PRIORITY_GROUPING,
210 
211  //Enable the GMAC to transmit and receive data
212  GMAC_REGS->GMAC_NCR |= GMAC_NCR_TXEN_Msk | GMAC_NCR_RXEN_Msk;
213 
214  //Accept any packets from the upper layer
215  osSetEvent(&interface->nicTxEvent);
216 
217  //Successful initialization
218  return NO_ERROR;
219 }
220 
221 
222 /**
223  * @brief GPIO configuration
224  * @param[in] interface Underlying network interface
225  **/
226 
227 __weak_func void same53EthInitGpio(NetInterface *interface)
228 {
229 //EVB-LAN9255 evaluation board?
230 #if defined(USE_EVB_LAN9255)
231  uint32_t temp;
232 
233  //Enable PORT bus clock (CLK_PORT_APB)
234  MCLK_REGS->MCLK_APBBMASK |= MCLK_APBBMASK_PORT_Msk;
235 
236  //Configure GRX1 (PA12)
237  PORT_REGS->GROUP[0].PORT_PINCFG[12] |= PORT_PINCFG_PMUXEN_Msk;
238  temp = PORT_REGS->GROUP[0].PORT_PMUX[6] & ~PORT_PMUX_PMUXE_Msk;
239  PORT_REGS->GROUP[0].PORT_PMUX[6] = temp | PORT_PMUX_PMUXE(MUX_PA12L_GMAC_GRX1);
240 
241  //Configure GRX0 (PA13)
242  PORT_REGS->GROUP[0].PORT_PINCFG[13] |= PORT_PINCFG_PMUXEN_Msk;
243  temp = PORT_REGS->GROUP[0].PORT_PMUX[6] & ~PORT_PMUX_PMUXO_Msk;
244  PORT_REGS->GROUP[0].PORT_PMUX[6] = temp | PORT_PMUX_PMUXO(MUX_PA13L_GMAC_GRX0);
245 
246  //Configure GTXCK (PA14)
247  PORT_REGS->GROUP[0].PORT_PINCFG[14] |= PORT_PINCFG_PMUXEN_Msk;
248  temp = PORT_REGS->GROUP[0].PORT_PMUX[7] & ~PORT_PMUX_PMUXE_Msk;
249  PORT_REGS->GROUP[0].PORT_PMUX[7] = temp | PORT_PMUX_PMUXE(MUX_PA14L_GMAC_GTXCK);
250 
251  //Configure GRXER (PA15)
252  PORT_REGS->GROUP[0].PORT_PINCFG[15] |= PORT_PINCFG_PMUXEN_Msk;
253  temp = PORT_REGS->GROUP[0].PORT_PMUX[7] & ~PORT_PMUX_PMUXO_Msk;
254  PORT_REGS->GROUP[0].PORT_PMUX[7] = temp | PORT_PMUX_PMUXO(MUX_PA15L_GMAC_GRXER);
255 
256  //Configure GRXDV (PA16)
257  PORT_REGS->GROUP[0].PORT_PINCFG[16] |= PORT_PINCFG_PMUXEN_Msk;
258  temp = PORT_REGS->GROUP[0].PORT_PMUX[8] & ~PORT_PMUX_PMUXE_Msk;
259  PORT_REGS->GROUP[0].PORT_PMUX[8] = temp | PORT_PMUX_PMUXE(MUX_PA16L_GMAC_GRXDV);
260 
261  //Configure GTXEN (PA17)
262  PORT_REGS->GROUP[0].PORT_PINCFG[17] |= PORT_PINCFG_DRVSTR_Msk;
263  PORT_REGS->GROUP[0].PORT_PINCFG[17] |= PORT_PINCFG_PMUXEN_Msk;
264  temp = PORT_REGS->GROUP[0].PORT_PMUX[8] & ~PORT_PMUX_PMUXO_Msk;
265  PORT_REGS->GROUP[0].PORT_PMUX[8] = temp | PORT_PMUX_PMUXO(MUX_PA17L_GMAC_GTXEN);
266 
267  //Configure GTX0 (PA18)
268  PORT_REGS->GROUP[0].PORT_PINCFG[18] |= PORT_PINCFG_DRVSTR_Msk;
269  PORT_REGS->GROUP[0].PORT_PINCFG[18] |= PORT_PINCFG_PMUXEN_Msk;
270  temp = PORT_REGS->GROUP[0].PORT_PMUX[9] & ~PORT_PMUX_PMUXE_Msk;
271  PORT_REGS->GROUP[0].PORT_PMUX[9] = temp | PORT_PMUX_PMUXE(MUX_PA18L_GMAC_GTX0);
272 
273  //Configure GTX1 (PA19)
274  PORT_REGS->GROUP[0].PORT_PINCFG[19] |= PORT_PINCFG_DRVSTR_Msk;
275  PORT_REGS->GROUP[0].PORT_PINCFG[19] |= PORT_PINCFG_PMUXEN_Msk;
276  temp = PORT_REGS->GROUP[0].PORT_PMUX[9] & ~PORT_PMUX_PMUXO_Msk;
277  PORT_REGS->GROUP[0].PORT_PMUX[9] = temp | PORT_PMUX_PMUXO(MUX_PA19L_GMAC_GTX1);
278 
279  //Configure GMDC (PA20)
280  PORT_REGS->GROUP[0].PORT_PINCFG[20] |= PORT_PINCFG_PMUXEN_Msk;
281  temp = PORT_REGS->GROUP[0].PORT_PMUX[10] & ~PORT_PMUX_PMUXE_Msk;
282  PORT_REGS->GROUP[0].PORT_PMUX[10] = temp | PORT_PMUX_PMUXE(MUX_PA20L_GMAC_GMDC);
283 
284  //Configure GMDIO (PA21)
285  PORT_REGS->GROUP[0].PORT_PINCFG[21] |= PORT_PINCFG_PMUXEN_Msk;
286  temp = PORT_REGS->GROUP[0].PORT_PMUX[10] & ~PORT_PMUX_PMUXO_Msk;
287  PORT_REGS->GROUP[0].PORT_PMUX[10] = temp | PORT_PMUX_PMUXO(MUX_PA21L_GMAC_GMDIO);
288 
289  //Select RMII operation mode
290  GMAC_REGS->GMAC_UR &= ~GMAC_UR_MII_Msk;
291 #endif
292 }
293 
294 
295 /**
296  * @brief Initialize buffer descriptors
297  * @param[in] interface Underlying network interface
298  **/
299 
301 {
302  uint_t i;
303  uint32_t address;
304 
305  //Initialize TX buffer descriptors
306  for(i = 0; i < SAME53_ETH_TX_BUFFER_COUNT; i++)
307  {
308  //Calculate the address of the current TX buffer
309  address = (uint32_t) txBuffer[i];
310  //Write the address to the descriptor entry
311  txBufferDesc[i].address = address;
312  //Initialize status field
313  txBufferDesc[i].status = GMAC_TX_USED;
314  }
315 
316  //Mark the last descriptor entry with the wrap flag
317  txBufferDesc[i - 1].status |= GMAC_TX_WRAP;
318  //Initialize TX buffer index
319  txBufferIndex = 0;
320 
321  //Initialize RX buffer descriptors
322  for(i = 0; i < SAME53_ETH_RX_BUFFER_COUNT; i++)
323  {
324  //Calculate the address of the current RX buffer
325  address = (uint32_t) rxBuffer[i];
326  //Write the address to the descriptor entry
327  rxBufferDesc[i].address = address & GMAC_RX_ADDRESS;
328  //Clear status field
329  rxBufferDesc[i].status = 0;
330  }
331 
332  //Mark the last descriptor entry with the wrap flag
333  rxBufferDesc[i - 1].address |= GMAC_RX_WRAP;
334  //Initialize RX buffer index
335  rxBufferIndex = 0;
336 
337  //Start location of the TX descriptor list
338  GMAC_REGS->GMAC_TBQB = (uint32_t) txBufferDesc;
339  //Start location of the RX descriptor list
340  GMAC_REGS->GMAC_RBQB = (uint32_t) rxBufferDesc;
341 }
342 
343 
344 /**
345  * @brief SAME53 Ethernet MAC timer handler
346  *
347  * This routine is periodically called by the TCP/IP stack to handle periodic
348  * operations such as polling the link state
349  *
350  * @param[in] interface Underlying network interface
351  **/
352 
353 void same53EthTick(NetInterface *interface)
354 {
355  //Valid Ethernet PHY or switch driver?
356  if(interface->phyDriver != NULL)
357  {
358  //Handle periodic operations
359  interface->phyDriver->tick(interface);
360  }
361  else if(interface->switchDriver != NULL)
362  {
363  //Handle periodic operations
364  interface->switchDriver->tick(interface);
365  }
366  else
367  {
368  //Just for sanity
369  }
370 }
371 
372 
373 /**
374  * @brief Enable interrupts
375  * @param[in] interface Underlying network interface
376  **/
377 
379 {
380  //Enable Ethernet MAC interrupts
381  NVIC_EnableIRQ(GMAC_IRQn);
382 
383  //Valid Ethernet PHY or switch driver?
384  if(interface->phyDriver != NULL)
385  {
386  //Enable Ethernet PHY interrupts
387  interface->phyDriver->enableIrq(interface);
388  }
389  else if(interface->switchDriver != NULL)
390  {
391  //Enable Ethernet switch interrupts
392  interface->switchDriver->enableIrq(interface);
393  }
394  else
395  {
396  //Just for sanity
397  }
398 }
399 
400 
401 /**
402  * @brief Disable interrupts
403  * @param[in] interface Underlying network interface
404  **/
405 
407 {
408  //Disable Ethernet MAC interrupts
409  NVIC_DisableIRQ(GMAC_IRQn);
410 
411  //Valid Ethernet PHY or switch driver?
412  if(interface->phyDriver != NULL)
413  {
414  //Disable Ethernet PHY interrupts
415  interface->phyDriver->disableIrq(interface);
416  }
417  else if(interface->switchDriver != NULL)
418  {
419  //Disable Ethernet switch interrupts
420  interface->switchDriver->disableIrq(interface);
421  }
422  else
423  {
424  //Just for sanity
425  }
426 }
427 
428 
429 /**
430  * @brief SAME53 Ethernet MAC interrupt service routine
431  **/
432 
433 void GMAC_Handler(void)
434 {
435  bool_t flag;
436  volatile uint32_t isr;
437  volatile uint32_t tsr;
438  volatile uint32_t rsr;
439 
440  //Interrupt service routine prologue
441  osEnterIsr();
442 
443  //This flag will be set if a higher priority task must be woken
444  flag = FALSE;
445 
446  //Each time the software reads GMAC_ISR, it has to check the contents
447  //of GMAC_TSR, GMAC_RSR and GMAC_NSR
448  isr = GMAC_REGS->GMAC_ISR;
449  tsr = GMAC_REGS->GMAC_TSR;
450  rsr = GMAC_REGS->GMAC_RSR;
451  (void) isr;
452 
453  //Packet transmitted?
454  if((tsr & (GMAC_TSR_HRESP_Msk | GMAC_TSR_UND_Msk |
455  GMAC_TSR_TXCOMP_Msk | GMAC_TSR_TFC_Msk | GMAC_TSR_TXGO_Msk |
456  GMAC_TSR_RLE_Msk | GMAC_TSR_COL_Msk | GMAC_TSR_UBR_Msk)) != 0)
457  {
458  //Only clear TSR flags that are currently set
459  GMAC_REGS->GMAC_TSR = tsr;
460 
461  //Check whether the TX buffer is available for writing
462  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
463  {
464  //Notify the TCP/IP stack that the transmitter is ready to send
465  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
466  }
467  }
468 
469  //Packet received?
470  if((rsr & (GMAC_RSR_HNO_Msk | GMAC_RSR_RXOVR_Msk | GMAC_RSR_REC_Msk |
471  GMAC_RSR_BNA_Msk)) != 0)
472  {
473  //Set event flag
474  nicDriverInterface->nicEvent = TRUE;
475  //Notify the TCP/IP stack of the event
476  flag |= osSetEventFromIsr(&netEvent);
477  }
478 
479  //Interrupt service routine epilogue
480  osExitIsr(flag);
481 }
482 
483 
484 /**
485  * @brief SAME53 Ethernet MAC event handler
486  * @param[in] interface Underlying network interface
487  **/
488 
490 {
491  error_t error;
492  uint32_t rsr;
493 
494  //Read receive status
495  rsr = GMAC_REGS->GMAC_RSR;
496 
497  //Packet received?
498  if((rsr & (GMAC_RSR_HNO_Msk | GMAC_RSR_RXOVR_Msk | GMAC_RSR_REC_Msk |
499  GMAC_RSR_BNA_Msk)) != 0)
500  {
501  //Only clear RSR flags that are currently set
502  GMAC_REGS->GMAC_RSR = rsr;
503 
504  //Process all pending packets
505  do
506  {
507  //Read incoming packet
508  error = same53EthReceivePacket(interface);
509 
510  //No more data in the receive buffer?
511  } while(error != ERROR_BUFFER_EMPTY);
512  }
513 }
514 
515 
516 /**
517  * @brief Send a packet
518  * @param[in] interface Underlying network interface
519  * @param[in] buffer Multi-part buffer containing the data to send
520  * @param[in] offset Offset to the first data byte
521  * @param[in] ancillary Additional options passed to the stack along with
522  * the packet
523  * @return Error code
524  **/
525 
527  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
528 {
529  size_t length;
530 
531  //Retrieve the length of the packet
532  length = netBufferGetLength(buffer) - offset;
533 
534  //Check the frame length
536  {
537  //The transmitter can accept another packet
538  osSetEvent(&interface->nicTxEvent);
539  //Report an error
540  return ERROR_INVALID_LENGTH;
541  }
542 
543  //Make sure the current buffer is available for writing
544  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) == 0)
545  {
546  return ERROR_FAILURE;
547  }
548 
549  //Copy user data to the transmit buffer
550  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
551 
552  //Set the necessary flags in the descriptor entry
553  if(txBufferIndex < (SAME53_ETH_TX_BUFFER_COUNT - 1))
554  {
555  //Write the status word
556  txBufferDesc[txBufferIndex].status = GMAC_TX_LAST |
558 
559  //Point to the next buffer
560  txBufferIndex++;
561  }
562  else
563  {
564  //Write the status word
565  txBufferDesc[txBufferIndex].status = GMAC_TX_WRAP | GMAC_TX_LAST |
567 
568  //Wrap around
569  txBufferIndex = 0;
570  }
571 
572  //Data synchronization barrier
573  __DSB();
574 
575  //Set the TSTART bit to initiate transmission
576  GMAC_REGS->GMAC_NCR |= GMAC_NCR_TSTART_Msk;
577 
578  //Check whether the next buffer is available for writing
579  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
580  {
581  //The transmitter can accept another packet
582  osSetEvent(&interface->nicTxEvent);
583  }
584 
585  //Successful processing
586  return NO_ERROR;
587 }
588 
589 
590 /**
591  * @brief Receive a packet
592  * @param[in] interface Underlying network interface
593  * @return Error code
594  **/
595 
597 {
598  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
599  error_t error;
600  uint_t i;
601  uint_t j;
602  uint_t sofIndex;
603  uint_t eofIndex;
604  size_t n;
605  size_t size;
606  size_t length;
607 
608  //Initialize variables
609  size = 0;
610  sofIndex = UINT_MAX;
611  eofIndex = UINT_MAX;
612 
613  //Search for SOF and EOF flags
614  for(i = 0; i < SAME53_ETH_RX_BUFFER_COUNT; i++)
615  {
616  //Point to the current entry
617  j = rxBufferIndex + i;
618 
619  //Wrap around to the beginning of the buffer if necessary
621  {
623  }
624 
625  //No more entries to process?
626  if((rxBufferDesc[j].address & GMAC_RX_OWNERSHIP) == 0)
627  {
628  //Stop processing
629  break;
630  }
631 
632  //A valid SOF has been found?
633  if((rxBufferDesc[j].status & GMAC_RX_SOF) != 0)
634  {
635  //Save the position of the SOF
636  sofIndex = i;
637  }
638 
639  //A valid EOF has been found?
640  if((rxBufferDesc[j].status & GMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
641  {
642  //Save the position of the EOF
643  eofIndex = i;
644  //Retrieve the length of the frame
645  size = rxBufferDesc[j].status & GMAC_RX_LENGTH;
646  //Limit the number of data to read
647  size = MIN(size, ETH_MAX_FRAME_SIZE);
648  //Stop processing since we have reached the end of the frame
649  break;
650  }
651  }
652 
653  //Determine the number of entries to process
654  if(eofIndex != UINT_MAX)
655  {
656  j = eofIndex + 1;
657  }
658  else if(sofIndex != UINT_MAX)
659  {
660  j = sofIndex;
661  }
662  else
663  {
664  j = i;
665  }
666 
667  //Total number of bytes that have been copied from the receive buffer
668  length = 0;
669 
670  //Process incoming frame
671  for(i = 0; i < j; i++)
672  {
673  //Any data to copy from current buffer?
674  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
675  {
676  //Calculate the number of bytes to read at a time
678  //Copy data from receive buffer
679  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
680  //Update byte counters
681  length += n;
682  size -= n;
683  }
684 
685  //Mark the current buffer as free
686  rxBufferDesc[rxBufferIndex].address &= ~GMAC_RX_OWNERSHIP;
687 
688  //Point to the following entry
689  rxBufferIndex++;
690 
691  //Wrap around to the beginning of the buffer if necessary
692  if(rxBufferIndex >= SAME53_ETH_RX_BUFFER_COUNT)
693  {
694  rxBufferIndex = 0;
695  }
696  }
697 
698  //Any packet to process?
699  if(length > 0)
700  {
701  NetRxAncillary ancillary;
702 
703  //Additional options can be passed to the stack along with the packet
704  ancillary = NET_DEFAULT_RX_ANCILLARY;
705 
706  //Pass the packet to the upper layer
707  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
708  //Valid packet received
709  error = NO_ERROR;
710  }
711  else
712  {
713  //No more data in the receive buffer
714  error = ERROR_BUFFER_EMPTY;
715  }
716 
717  //Return status code
718  return error;
719 }
720 
721 
722 /**
723  * @brief Configure MAC address filtering
724  * @param[in] interface Underlying network interface
725  * @return Error code
726  **/
727 
729 {
730  uint_t i;
731  uint_t j;
732  uint_t k;
733  uint8_t *p;
734  uint32_t hashTable[2];
735  MacAddr unicastMacAddr[3];
736  MacFilterEntry *entry;
737 
738  //Debug message
739  TRACE_DEBUG("Updating MAC filter...\r\n");
740 
741  //Set the MAC address of the station
742  GMAC_REGS->SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
743  GMAC_REGS->SA[0].GMAC_SAT = interface->macAddr.w[2];
744 
745  //The MAC supports 3 additional addresses for unicast perfect filtering
746  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
747  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
748  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
749 
750  //The hash table is used for multicast address filtering
751  hashTable[0] = 0;
752  hashTable[1] = 0;
753 
754  //The MAC address filter contains the list of MAC addresses to accept
755  //when receiving an Ethernet frame
756  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
757  {
758  //Point to the current entry
759  entry = &interface->macAddrFilter[i];
760 
761  //Valid entry?
762  if(entry->refCount > 0)
763  {
764  //Multicast address?
765  if(macIsMulticastAddr(&entry->addr))
766  {
767  //Point to the MAC address
768  p = entry->addr.b;
769 
770  //Apply the hash function
771  k = (p[0] >> 6) ^ p[0];
772  k ^= (p[1] >> 4) ^ (p[1] << 2);
773  k ^= (p[2] >> 2) ^ (p[2] << 4);
774  k ^= (p[3] >> 6) ^ p[3];
775  k ^= (p[4] >> 4) ^ (p[4] << 2);
776  k ^= (p[5] >> 2) ^ (p[5] << 4);
777 
778  //The hash value is reduced to a 6-bit index
779  k &= 0x3F;
780 
781  //Update hash table contents
782  hashTable[k / 32] |= (1 << (k % 32));
783  }
784  else
785  {
786  //Up to 3 additional MAC addresses can be specified
787  if(j < 3)
788  {
789  //Save the unicast address
790  unicastMacAddr[j++] = entry->addr;
791  }
792  }
793  }
794  }
795 
796  //Configure the first unicast address filter
797  if(j >= 1)
798  {
799  //The address is activated when SAT register is written
800  GMAC_REGS->SA[1].GMAC_SAB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
801  GMAC_REGS->SA[1].GMAC_SAT = unicastMacAddr[0].w[2];
802  }
803  else
804  {
805  //The address is deactivated when SAB register is written
806  GMAC_REGS->SA[1].GMAC_SAB = 0;
807  }
808 
809  //Configure the second unicast address filter
810  if(j >= 2)
811  {
812  //The address is activated when SAT register is written
813  GMAC_REGS->SA[2].GMAC_SAB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
814  GMAC_REGS->SA[2].GMAC_SAT = unicastMacAddr[1].w[2];
815  }
816  else
817  {
818  //The address is deactivated when SAB register is written
819  GMAC_REGS->SA[2].GMAC_SAB = 0;
820  }
821 
822  //Configure the third unicast address filter
823  if(j >= 3)
824  {
825  //The address is activated when SAT register is written
826  GMAC_REGS->SA[3].GMAC_SAB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
827  GMAC_REGS->SA[3].GMAC_SAT = unicastMacAddr[2].w[2];
828  }
829  else
830  {
831  //The address is deactivated when SAB register is written
832  GMAC_REGS->SA[3].GMAC_SAB = 0;
833  }
834 
835  //Configure the multicast hash table
836  GMAC_REGS->GMAC_HRB = hashTable[0];
837  GMAC_REGS->GMAC_HRT = hashTable[1];
838 
839  //Debug message
840  TRACE_DEBUG(" HRB = %08" PRIX32 "\r\n", GMAC_REGS->GMAC_HRB);
841  TRACE_DEBUG(" HRT = %08" PRIX32 "\r\n", GMAC_REGS->GMAC_HRT);
842 
843  //Successful processing
844  return NO_ERROR;
845 }
846 
847 
848 /**
849  * @brief Adjust MAC configuration parameters for proper operation
850  * @param[in] interface Underlying network interface
851  * @return Error code
852  **/
853 
855 {
856  uint32_t config;
857 
858  //Read network configuration register
859  config = GMAC_REGS->GMAC_NCFGR;
860 
861  //10BASE-T or 100BASE-TX operation mode?
862  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
863  {
864  config |= GMAC_NCFGR_SPD_Msk;
865  }
866  else
867  {
868  config &= ~GMAC_NCFGR_SPD_Msk;
869  }
870 
871  //Half-duplex or full-duplex mode?
872  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
873  {
874  config |= GMAC_NCFGR_FD_Msk;
875  }
876  else
877  {
878  config &= ~GMAC_NCFGR_FD_Msk;
879  }
880 
881  //Write configuration value back to NCFGR register
882  GMAC_REGS->GMAC_NCFGR = config;
883 
884  //Successful processing
885  return NO_ERROR;
886 }
887 
888 
889 /**
890  * @brief Write PHY register
891  * @param[in] opcode Access type (2 bits)
892  * @param[in] phyAddr PHY address (5 bits)
893  * @param[in] regAddr Register address (5 bits)
894  * @param[in] data Register value
895  **/
896 
897 void same53EthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
898  uint8_t regAddr, uint16_t data)
899 {
900  uint32_t temp;
901 
902  //Valid opcode?
903  if(opcode == SMI_OPCODE_WRITE)
904  {
905  //Set up a write operation
906  temp = GMAC_MAN_CLTTO_Msk | GMAC_MAN_OP(1) | GMAC_MAN_WTN(2);
907  //PHY address
908  temp |= GMAC_MAN_PHYA(phyAddr);
909  //Register address
910  temp |= GMAC_MAN_REGA(regAddr);
911  //Register value
912  temp |= GMAC_MAN_DATA(data);
913 
914  //Start a write operation
915  GMAC_REGS->GMAC_MAN = temp;
916  //Wait for the write to complete
917  while((GMAC_REGS->GMAC_NSR & GMAC_NSR_IDLE_Msk) == 0)
918  {
919  }
920  }
921  else
922  {
923  //The MAC peripheral only supports standard Clause 22 opcodes
924  }
925 }
926 
927 
928 /**
929  * @brief Read PHY register
930  * @param[in] opcode Access type (2 bits)
931  * @param[in] phyAddr PHY address (5 bits)
932  * @param[in] regAddr Register address (5 bits)
933  * @return Register value
934  **/
935 
936 uint16_t same53EthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
937  uint8_t regAddr)
938 {
939  uint16_t data;
940  uint32_t temp;
941 
942  //Valid opcode?
943  if(opcode == SMI_OPCODE_READ)
944  {
945  //Set up a read operation
946  temp = GMAC_MAN_CLTTO_Msk | GMAC_MAN_OP(2) | GMAC_MAN_WTN(2);
947  //PHY address
948  temp |= GMAC_MAN_PHYA(phyAddr);
949  //Register address
950  temp |= GMAC_MAN_REGA(regAddr);
951 
952  //Start a read operation
953  GMAC_REGS->GMAC_MAN = temp;
954  //Wait for the read to complete
955  while((GMAC_REGS->GMAC_NSR & GMAC_NSR_IDLE_Msk) == 0)
956  {
957  }
958 
959  //Get register value
960  data = GMAC_REGS->GMAC_MAN & GMAC_MAN_DATA_Msk;
961  }
962  else
963  {
964  //The MAC peripheral only supports standard Clause 22 opcodes
965  data = 0;
966  }
967 
968  //Return the value of the PHY register
969  return data;
970 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
error_t same53EthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
void same53EthTick(NetInterface *interface)
SAME53 Ethernet MAC timer handler.
uint8_t opcode
Definition: dns_common.h:188
int bool_t
Definition: compiler_port.h:53
#define GMAC_TX_LENGTH
#define netEvent
Definition: net_legacy.h:196
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
Transmit buffer descriptor.
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:222
#define ETH_MAX_FRAME_SIZE
Definition: ethernet.h:110
#define SAME53_ETH_IRQ_SUB_PRIORITY
Receive buffer descriptor.
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
#define GMAC_RX_WRAP
SAME53 Ethernet MAC driver.
void same53EthEnableIrq(NetInterface *interface)
Enable interrupts.
#define SAME53_ETH_IRQ_PRIORITY_GROUPING
#define SAME53_ETH_IRQ_GROUP_PRIORITY
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 same53EthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
#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
error_t same53EthInit(NetInterface *interface)
SAME53 Ethernet MAC initialization.
#define GMAC_RX_ADDRESS
#define SAME53_ETH_RX_BUFFER_SIZE
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:104
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
#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
error_t same53EthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define NetTxAncillary
Definition: net_misc.h:36
void GMAC_Handler(void)
SAME53 Ethernet MAC interrupt service routine.
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:95
uint8_t length
Definition: tcp.h:368
void same53EthDisableIrq(NetInterface *interface)
Disable interrupts.
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
#define rxBuffer
#define GMAC_RX_SOF
uint16_t same53EthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
MacAddr
Definition: ethernet.h:195
#define TRACE_DEBUG(...)
Definition: debug.h:107
uint16_t regAddr
#define SAME53_ETH_RX_BUFFER_COUNT
#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 osEnterIsr()
#define SAME53_ETH_TX_BUFFER_COUNT
__weak_func void same53EthInitGpio(NetInterface *interface)
GPIO configuration.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
error_t same53EthReceivePacket(NetInterface *interface)
Receive a packet.
void same53EthEventHandler(NetInterface *interface)
SAME53 Ethernet MAC event handler.
error_t same53EthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
#define SAME53_ETH_TX_BUFFER_SIZE
NIC driver.
Definition: nic.h:286
#define GMAC_RX_OWNERSHIP
void same53EthInitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
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.
#define GMAC_TX_WRAP
const NicDriver same53EthDriver
SAME53 Ethernet MAC driver.
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83