same54_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file same54_eth_driver.c
3  * @brief SAME54 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 SAME54 Ethernet MAC driver
86  **/
87 
89 {
91  ETH_MTU,
102  TRUE,
103  TRUE,
104  TRUE,
105  FALSE
106 };
107 
108 
109 /**
110  * @brief SAME54 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 SAME54 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  same54EthInitGpio(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  same54EthInitBufferDesc(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(SAME54_ETH_IRQ_PRIORITY_GROUPING);
206 
207  //Configure GMAC interrupt priority
208  NVIC_SetPriority(GMAC_IRQn, NVIC_EncodePriority(SAME54_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 same54EthInitGpio(NetInterface *interface)
228 {
229 //SAME54-Xplained-Pro evaluation board?
230 #if defined(USE_SAME54_XPLAINED_PRO)
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 GTXEN (PA17)
257  PORT_REGS->GROUP[0].PORT_PINCFG[17] |= PORT_PINCFG_DRVSTR_Msk;
258  PORT_REGS->GROUP[0].PORT_PINCFG[17] |= PORT_PINCFG_PMUXEN_Msk;
259  temp = PORT_REGS->GROUP[0].PORT_PMUX[8] & ~PORT_PMUX_PMUXO_Msk;
260  PORT_REGS->GROUP[0].PORT_PMUX[8] = temp | PORT_PMUX_PMUXO(MUX_PA17L_GMAC_GTXEN);
261 
262  //Configure GTX0 (PA18)
263  PORT_REGS->GROUP[0].PORT_PINCFG[18] |= PORT_PINCFG_DRVSTR_Msk;
264  PORT_REGS->GROUP[0].PORT_PINCFG[18] |= PORT_PINCFG_PMUXEN_Msk;
265  temp = PORT_REGS->GROUP[0].PORT_PMUX[9] & ~PORT_PMUX_PMUXE_Msk;
266  PORT_REGS->GROUP[0].PORT_PMUX[9] = temp | PORT_PMUX_PMUXE(MUX_PA18L_GMAC_GTX0);
267 
268  //Configure GTX1 (PA19)
269  PORT_REGS->GROUP[0].PORT_PINCFG[19] |= PORT_PINCFG_DRVSTR_Msk;
270  PORT_REGS->GROUP[0].PORT_PINCFG[19] |= PORT_PINCFG_PMUXEN_Msk;
271  temp = PORT_REGS->GROUP[0].PORT_PMUX[9] & ~PORT_PMUX_PMUXO_Msk;
272  PORT_REGS->GROUP[0].PORT_PMUX[9] = temp | PORT_PMUX_PMUXO(MUX_PA19L_GMAC_GTX1);
273 
274  //Configure GMDC (PC11)
275  PORT_REGS->GROUP[2].PORT_PINCFG[11] |= PORT_PINCFG_PMUXEN_Msk;
276  temp = PORT_REGS->GROUP[2].PORT_PMUX[5] & ~PORT_PMUX_PMUXO_Msk;
277  PORT_REGS->GROUP[2].PORT_PMUX[5] = temp | PORT_PMUX_PMUXO(MUX_PC11L_GMAC_GMDC);
278 
279  //Configure GMDIO (PC12)
280  PORT_REGS->GROUP[2].PORT_PINCFG[12] |= PORT_PINCFG_PMUXEN_Msk;
281  temp = PORT_REGS->GROUP[2].PORT_PMUX[6] & ~PORT_PMUX_PMUXE_Msk;
282  PORT_REGS->GROUP[2].PORT_PMUX[6] = temp | PORT_PMUX_PMUXE(MUX_PC12L_GMAC_GMDIO);
283 
284  //Configure GRXDV (PC20)
285  PORT_REGS->GROUP[2].PORT_PINCFG[20] |= PORT_PINCFG_PMUXEN_Msk;
286  temp = PORT_REGS->GROUP[2].PORT_PMUX[10] & ~PORT_PMUX_PMUXE_Msk;
287  PORT_REGS->GROUP[2].PORT_PMUX[10] = temp | PORT_PMUX_PMUXE(MUX_PC20L_GMAC_GRXDV);
288 
289  //Select RMII operation mode
290  GMAC_REGS->GMAC_UR &= ~GMAC_UR_MII_Msk;
291 
292  //Configure PHY_RESET (PC21) as an output
293  PORT_REGS->GROUP[2].PORT_DIRSET = PORT_PC21;
294 
295  //Reset PHY transceiver
296  PORT_REGS->GROUP[2].PORT_OUTCLR = PORT_PC21;
297  sleep(10);
298  PORT_REGS->GROUP[2].PORT_OUTSET = PORT_PC21;
299  sleep(10);
300 
301 //SAME54-Curiosity-Ultra evaluation board?
302 #elif defined(USE_SAME54_CURIOSITY_ULTRA)
303  uint32_t temp;
304 
305  //Enable PORT bus clock (CLK_PORT_APB)
306  MCLK_REGS->MCLK_APBBMASK |= MCLK_APBBMASK_PORT_Msk;
307 
308  //Configure GRX1 (PA12)
309  PORT_REGS->GROUP[0].PORT_PINCFG[12] |= PORT_PINCFG_PMUXEN_Msk;
310  temp = PORT_REGS->GROUP[0].PORT_PMUX[6] & ~PORT_PMUX_PMUXE_Msk;
311  PORT_REGS->GROUP[0].PORT_PMUX[6] = temp | PORT_PMUX_PMUXE(MUX_PA12L_GMAC_GRX1);
312 
313  //Configure GRX0 (PA13)
314  PORT_REGS->GROUP[0].PORT_PINCFG[13] |= PORT_PINCFG_PMUXEN_Msk;
315  temp = PORT_REGS->GROUP[0].PORT_PMUX[6] & ~PORT_PMUX_PMUXO_Msk;
316  PORT_REGS->GROUP[0].PORT_PMUX[6] = temp | PORT_PMUX_PMUXO(MUX_PA13L_GMAC_GRX0);
317 
318  //Configure GTXCK (PA14)
319  PORT_REGS->GROUP[0].PORT_PINCFG[14] |= PORT_PINCFG_PMUXEN_Msk;
320  temp = PORT_REGS->GROUP[0].PORT_PMUX[7] & ~PORT_PMUX_PMUXE_Msk;
321  PORT_REGS->GROUP[0].PORT_PMUX[7] = temp | PORT_PMUX_PMUXE(MUX_PA14L_GMAC_GTXCK);
322 
323  //Configure GRXER (PA15)
324  PORT_REGS->GROUP[0].PORT_PINCFG[15] |= PORT_PINCFG_PMUXEN_Msk;
325  temp = PORT_REGS->GROUP[0].PORT_PMUX[7] & ~PORT_PMUX_PMUXO_Msk;
326  PORT_REGS->GROUP[0].PORT_PMUX[7] = temp | PORT_PMUX_PMUXO(MUX_PA15L_GMAC_GRXER);
327 
328  //Configure GTXEN (PA17)
329  PORT_REGS->GROUP[0].PORT_PINCFG[17] |= PORT_PINCFG_DRVSTR_Msk;
330  PORT_REGS->GROUP[0].PORT_PINCFG[17] |= PORT_PINCFG_PMUXEN_Msk;
331  temp = PORT_REGS->GROUP[0].PORT_PMUX[8] & ~PORT_PMUX_PMUXO_Msk;
332  PORT_REGS->GROUP[0].PORT_PMUX[8] = temp | PORT_PMUX_PMUXO(MUX_PA17L_GMAC_GTXEN);
333 
334  //Configure GTX0 (PA18)
335  PORT_REGS->GROUP[0].PORT_PINCFG[18] |= PORT_PINCFG_DRVSTR_Msk;
336  PORT_REGS->GROUP[0].PORT_PINCFG[18] |= PORT_PINCFG_PMUXEN_Msk;
337  temp = PORT_REGS->GROUP[0].PORT_PMUX[9] & ~PORT_PMUX_PMUXE_Msk;
338  PORT_REGS->GROUP[0].PORT_PMUX[9] = temp | PORT_PMUX_PMUXE(MUX_PA18L_GMAC_GTX0);
339 
340  //Configure GTX1 (PA19)
341  PORT_REGS->GROUP[0].PORT_PINCFG[19] |= PORT_PINCFG_DRVSTR_Msk;
342  PORT_REGS->GROUP[0].PORT_PINCFG[19] |= PORT_PINCFG_PMUXEN_Msk;
343  temp = PORT_REGS->GROUP[0].PORT_PMUX[9] & ~PORT_PMUX_PMUXO_Msk;
344  PORT_REGS->GROUP[0].PORT_PMUX[9] = temp | PORT_PMUX_PMUXO(MUX_PA19L_GMAC_GTX1);
345 
346  //Configure GRXDV (PC20)
347  PORT_REGS->GROUP[2].PORT_PINCFG[20] |= PORT_PINCFG_PMUXEN_Msk;
348  temp = PORT_REGS->GROUP[2].PORT_PMUX[10] & ~PORT_PMUX_PMUXE_Msk;
349  PORT_REGS->GROUP[2].PORT_PMUX[10] = temp | PORT_PMUX_PMUXE(MUX_PC20L_GMAC_GRXDV);
350 
351  //Configure GMDC (PC22)
352  PORT_REGS->GROUP[2].PORT_PINCFG[22] |= PORT_PINCFG_PMUXEN_Msk;
353  temp = PORT_REGS->GROUP[2].PORT_PMUX[11] & ~PORT_PMUX_PMUXE_Msk;
354  PORT_REGS->GROUP[2].PORT_PMUX[11] = temp | PORT_PMUX_PMUXE(MUX_PC22L_GMAC_GMDC);
355 
356  //Configure GMDIO (PC23)
357  PORT_REGS->GROUP[2].PORT_PINCFG[23] |= PORT_PINCFG_PMUXEN_Msk;
358  temp = PORT_REGS->GROUP[2].PORT_PMUX[11] & ~PORT_PMUX_PMUXO_Msk;
359  PORT_REGS->GROUP[2].PORT_PMUX[11] = temp | PORT_PMUX_PMUXO(MUX_PC23L_GMAC_GMDIO);
360 
361  //Select RMII operation mode
362  GMAC_REGS->GMAC_UR &= ~GMAC_UR_MII_Msk;
363 
364  //Configure PHY_RESET (PC18) as an output
365  PORT_REGS->GROUP[2].PORT_DIRSET = PORT_PC18;
366 
367  //Reset PHY transceiver
368  PORT_REGS->GROUP[2].PORT_OUTCLR = PORT_PC18;
369  sleep(10);
370  PORT_REGS->GROUP[2].PORT_OUTSET = PORT_PC18;
371  sleep(10);
372 #endif
373 }
374 
375 
376 /**
377  * @brief Initialize buffer descriptors
378  * @param[in] interface Underlying network interface
379  **/
380 
382 {
383  uint_t i;
384  uint32_t address;
385 
386  //Initialize TX buffer descriptors
387  for(i = 0; i < SAME54_ETH_TX_BUFFER_COUNT; i++)
388  {
389  //Calculate the address of the current TX buffer
390  address = (uint32_t) txBuffer[i];
391  //Write the address to the descriptor entry
392  txBufferDesc[i].address = address;
393  //Initialize status field
394  txBufferDesc[i].status = GMAC_TX_USED;
395  }
396 
397  //Mark the last descriptor entry with the wrap flag
398  txBufferDesc[i - 1].status |= GMAC_TX_WRAP;
399  //Initialize TX buffer index
400  txBufferIndex = 0;
401 
402  //Initialize RX buffer descriptors
403  for(i = 0; i < SAME54_ETH_RX_BUFFER_COUNT; i++)
404  {
405  //Calculate the address of the current RX buffer
406  address = (uint32_t) rxBuffer[i];
407  //Write the address to the descriptor entry
408  rxBufferDesc[i].address = address & GMAC_RX_ADDRESS;
409  //Clear status field
410  rxBufferDesc[i].status = 0;
411  }
412 
413  //Mark the last descriptor entry with the wrap flag
414  rxBufferDesc[i - 1].address |= GMAC_RX_WRAP;
415  //Initialize RX buffer index
416  rxBufferIndex = 0;
417 
418  //Start location of the TX descriptor list
419  GMAC_REGS->GMAC_TBQB = (uint32_t) txBufferDesc;
420  //Start location of the RX descriptor list
421  GMAC_REGS->GMAC_RBQB = (uint32_t) rxBufferDesc;
422 }
423 
424 
425 /**
426  * @brief SAME54 Ethernet MAC timer handler
427  *
428  * This routine is periodically called by the TCP/IP stack to handle periodic
429  * operations such as polling the link state
430  *
431  * @param[in] interface Underlying network interface
432  **/
433 
434 void same54EthTick(NetInterface *interface)
435 {
436  //Valid Ethernet PHY or switch driver?
437  if(interface->phyDriver != NULL)
438  {
439  //Handle periodic operations
440  interface->phyDriver->tick(interface);
441  }
442  else if(interface->switchDriver != NULL)
443  {
444  //Handle periodic operations
445  interface->switchDriver->tick(interface);
446  }
447  else
448  {
449  //Just for sanity
450  }
451 }
452 
453 
454 /**
455  * @brief Enable interrupts
456  * @param[in] interface Underlying network interface
457  **/
458 
460 {
461  //Enable Ethernet MAC interrupts
462  NVIC_EnableIRQ(GMAC_IRQn);
463 
464  //Valid Ethernet PHY or switch driver?
465  if(interface->phyDriver != NULL)
466  {
467  //Enable Ethernet PHY interrupts
468  interface->phyDriver->enableIrq(interface);
469  }
470  else if(interface->switchDriver != NULL)
471  {
472  //Enable Ethernet switch interrupts
473  interface->switchDriver->enableIrq(interface);
474  }
475  else
476  {
477  //Just for sanity
478  }
479 }
480 
481 
482 /**
483  * @brief Disable interrupts
484  * @param[in] interface Underlying network interface
485  **/
486 
488 {
489  //Disable Ethernet MAC interrupts
490  NVIC_DisableIRQ(GMAC_IRQn);
491 
492  //Valid Ethernet PHY or switch driver?
493  if(interface->phyDriver != NULL)
494  {
495  //Disable Ethernet PHY interrupts
496  interface->phyDriver->disableIrq(interface);
497  }
498  else if(interface->switchDriver != NULL)
499  {
500  //Disable Ethernet switch interrupts
501  interface->switchDriver->disableIrq(interface);
502  }
503  else
504  {
505  //Just for sanity
506  }
507 }
508 
509 
510 /**
511  * @brief SAME54 Ethernet MAC interrupt service routine
512  **/
513 
514 void GMAC_Handler(void)
515 {
516  bool_t flag;
517  volatile uint32_t isr;
518  volatile uint32_t tsr;
519  volatile uint32_t rsr;
520 
521  //Interrupt service routine prologue
522  osEnterIsr();
523 
524  //This flag will be set if a higher priority task must be woken
525  flag = FALSE;
526 
527  //Each time the software reads GMAC_ISR, it has to check the contents
528  //of GMAC_TSR, GMAC_RSR and GMAC_NSR
529  isr = GMAC_REGS->GMAC_ISR;
530  tsr = GMAC_REGS->GMAC_TSR;
531  rsr = GMAC_REGS->GMAC_RSR;
532  (void) isr;
533 
534  //Packet transmitted?
535  if((tsr & (GMAC_TSR_HRESP_Msk | GMAC_TSR_UND_Msk |
536  GMAC_TSR_TXCOMP_Msk | GMAC_TSR_TFC_Msk | GMAC_TSR_TXGO_Msk |
537  GMAC_TSR_RLE_Msk | GMAC_TSR_COL_Msk | GMAC_TSR_UBR_Msk)) != 0)
538  {
539  //Only clear TSR flags that are currently set
540  GMAC_REGS->GMAC_TSR = tsr;
541 
542  //Check whether the TX buffer is available for writing
543  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
544  {
545  //Notify the TCP/IP stack that the transmitter is ready to send
546  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
547  }
548  }
549 
550  //Packet received?
551  if((rsr & (GMAC_RSR_HNO_Msk | GMAC_RSR_RXOVR_Msk | GMAC_RSR_REC_Msk |
552  GMAC_RSR_BNA_Msk)) != 0)
553  {
554  //Set event flag
555  nicDriverInterface->nicEvent = TRUE;
556  //Notify the TCP/IP stack of the event
557  flag |= osSetEventFromIsr(&netEvent);
558  }
559 
560  //Interrupt service routine epilogue
561  osExitIsr(flag);
562 }
563 
564 
565 /**
566  * @brief SAME54 Ethernet MAC event handler
567  * @param[in] interface Underlying network interface
568  **/
569 
571 {
572  error_t error;
573  uint32_t rsr;
574 
575  //Read receive status
576  rsr = GMAC_REGS->GMAC_RSR;
577 
578  //Packet received?
579  if((rsr & (GMAC_RSR_HNO_Msk | GMAC_RSR_RXOVR_Msk | GMAC_RSR_REC_Msk |
580  GMAC_RSR_BNA_Msk)) != 0)
581  {
582  //Only clear RSR flags that are currently set
583  GMAC_REGS->GMAC_RSR = rsr;
584 
585  //Process all pending packets
586  do
587  {
588  //Read incoming packet
589  error = same54EthReceivePacket(interface);
590 
591  //No more data in the receive buffer?
592  } while(error != ERROR_BUFFER_EMPTY);
593  }
594 }
595 
596 
597 /**
598  * @brief Send a packet
599  * @param[in] interface Underlying network interface
600  * @param[in] buffer Multi-part buffer containing the data to send
601  * @param[in] offset Offset to the first data byte
602  * @param[in] ancillary Additional options passed to the stack along with
603  * the packet
604  * @return Error code
605  **/
606 
608  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
609 {
610  size_t length;
611 
612  //Retrieve the length of the packet
613  length = netBufferGetLength(buffer) - offset;
614 
615  //Check the frame length
617  {
618  //The transmitter can accept another packet
619  osSetEvent(&interface->nicTxEvent);
620  //Report an error
621  return ERROR_INVALID_LENGTH;
622  }
623 
624  //Make sure the current buffer is available for writing
625  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) == 0)
626  {
627  return ERROR_FAILURE;
628  }
629 
630  //Copy user data to the transmit buffer
631  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
632 
633  //Set the necessary flags in the descriptor entry
634  if(txBufferIndex < (SAME54_ETH_TX_BUFFER_COUNT - 1))
635  {
636  //Write the status word
637  txBufferDesc[txBufferIndex].status = GMAC_TX_LAST |
639 
640  //Point to the next buffer
641  txBufferIndex++;
642  }
643  else
644  {
645  //Write the status word
646  txBufferDesc[txBufferIndex].status = GMAC_TX_WRAP | GMAC_TX_LAST |
648 
649  //Wrap around
650  txBufferIndex = 0;
651  }
652 
653  //Data synchronization barrier
654  __DSB();
655 
656  //Set the TSTART bit to initiate transmission
657  GMAC_REGS->GMAC_NCR |= GMAC_NCR_TSTART_Msk;
658 
659  //Check whether the next buffer is available for writing
660  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
661  {
662  //The transmitter can accept another packet
663  osSetEvent(&interface->nicTxEvent);
664  }
665 
666  //Successful processing
667  return NO_ERROR;
668 }
669 
670 
671 /**
672  * @brief Receive a packet
673  * @param[in] interface Underlying network interface
674  * @return Error code
675  **/
676 
678 {
679  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
680  error_t error;
681  uint_t i;
682  uint_t j;
683  uint_t sofIndex;
684  uint_t eofIndex;
685  size_t n;
686  size_t size;
687  size_t length;
688 
689  //Initialize variables
690  size = 0;
691  sofIndex = UINT_MAX;
692  eofIndex = UINT_MAX;
693 
694  //Search for SOF and EOF flags
695  for(i = 0; i < SAME54_ETH_RX_BUFFER_COUNT; i++)
696  {
697  //Point to the current entry
698  j = rxBufferIndex + i;
699 
700  //Wrap around to the beginning of the buffer if necessary
702  {
704  }
705 
706  //No more entries to process?
707  if((rxBufferDesc[j].address & GMAC_RX_OWNERSHIP) == 0)
708  {
709  //Stop processing
710  break;
711  }
712 
713  //A valid SOF has been found?
714  if((rxBufferDesc[j].status & GMAC_RX_SOF) != 0)
715  {
716  //Save the position of the SOF
717  sofIndex = i;
718  }
719 
720  //A valid EOF has been found?
721  if((rxBufferDesc[j].status & GMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
722  {
723  //Save the position of the EOF
724  eofIndex = i;
725  //Retrieve the length of the frame
726  size = rxBufferDesc[j].status & GMAC_RX_LENGTH;
727  //Limit the number of data to read
728  size = MIN(size, ETH_MAX_FRAME_SIZE);
729  //Stop processing since we have reached the end of the frame
730  break;
731  }
732  }
733 
734  //Determine the number of entries to process
735  if(eofIndex != UINT_MAX)
736  {
737  j = eofIndex + 1;
738  }
739  else if(sofIndex != UINT_MAX)
740  {
741  j = sofIndex;
742  }
743  else
744  {
745  j = i;
746  }
747 
748  //Total number of bytes that have been copied from the receive buffer
749  length = 0;
750 
751  //Process incoming frame
752  for(i = 0; i < j; i++)
753  {
754  //Any data to copy from current buffer?
755  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
756  {
757  //Calculate the number of bytes to read at a time
759  //Copy data from receive buffer
760  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
761  //Update byte counters
762  length += n;
763  size -= n;
764  }
765 
766  //Mark the current buffer as free
767  rxBufferDesc[rxBufferIndex].address &= ~GMAC_RX_OWNERSHIP;
768 
769  //Point to the following entry
770  rxBufferIndex++;
771 
772  //Wrap around to the beginning of the buffer if necessary
773  if(rxBufferIndex >= SAME54_ETH_RX_BUFFER_COUNT)
774  {
775  rxBufferIndex = 0;
776  }
777  }
778 
779  //Any packet to process?
780  if(length > 0)
781  {
782  NetRxAncillary ancillary;
783 
784  //Additional options can be passed to the stack along with the packet
785  ancillary = NET_DEFAULT_RX_ANCILLARY;
786 
787  //Pass the packet to the upper layer
788  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
789  //Valid packet received
790  error = NO_ERROR;
791  }
792  else
793  {
794  //No more data in the receive buffer
795  error = ERROR_BUFFER_EMPTY;
796  }
797 
798  //Return status code
799  return error;
800 }
801 
802 
803 /**
804  * @brief Configure MAC address filtering
805  * @param[in] interface Underlying network interface
806  * @return Error code
807  **/
808 
810 {
811  uint_t i;
812  uint_t j;
813  uint_t k;
814  uint8_t *p;
815  uint32_t hashTable[2];
816  MacAddr unicastMacAddr[3];
817  MacFilterEntry *entry;
818 
819  //Debug message
820  TRACE_DEBUG("Updating MAC filter...\r\n");
821 
822  //Set the MAC address of the station
823  GMAC_REGS->SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
824  GMAC_REGS->SA[0].GMAC_SAT = interface->macAddr.w[2];
825 
826  //The MAC supports 3 additional addresses for unicast perfect filtering
827  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
828  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
829  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
830 
831  //The hash table is used for multicast address filtering
832  hashTable[0] = 0;
833  hashTable[1] = 0;
834 
835  //The MAC address filter contains the list of MAC addresses to accept
836  //when receiving an Ethernet frame
837  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
838  {
839  //Point to the current entry
840  entry = &interface->macAddrFilter[i];
841 
842  //Valid entry?
843  if(entry->refCount > 0)
844  {
845  //Multicast address?
846  if(macIsMulticastAddr(&entry->addr))
847  {
848  //Point to the MAC address
849  p = entry->addr.b;
850 
851  //Apply the hash function
852  k = (p[0] >> 6) ^ p[0];
853  k ^= (p[1] >> 4) ^ (p[1] << 2);
854  k ^= (p[2] >> 2) ^ (p[2] << 4);
855  k ^= (p[3] >> 6) ^ p[3];
856  k ^= (p[4] >> 4) ^ (p[4] << 2);
857  k ^= (p[5] >> 2) ^ (p[5] << 4);
858 
859  //The hash value is reduced to a 6-bit index
860  k &= 0x3F;
861 
862  //Update hash table contents
863  hashTable[k / 32] |= (1 << (k % 32));
864  }
865  else
866  {
867  //Up to 3 additional MAC addresses can be specified
868  if(j < 3)
869  {
870  //Save the unicast address
871  unicastMacAddr[j++] = entry->addr;
872  }
873  }
874  }
875  }
876 
877  //Configure the first unicast address filter
878  if(j >= 1)
879  {
880  //The address is activated when SAT register is written
881  GMAC_REGS->SA[1].GMAC_SAB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
882  GMAC_REGS->SA[1].GMAC_SAT = unicastMacAddr[0].w[2];
883  }
884  else
885  {
886  //The address is deactivated when SAB register is written
887  GMAC_REGS->SA[1].GMAC_SAB = 0;
888  }
889 
890  //Configure the second unicast address filter
891  if(j >= 2)
892  {
893  //The address is activated when SAT register is written
894  GMAC_REGS->SA[2].GMAC_SAB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
895  GMAC_REGS->SA[2].GMAC_SAT = unicastMacAddr[1].w[2];
896  }
897  else
898  {
899  //The address is deactivated when SAB register is written
900  GMAC_REGS->SA[2].GMAC_SAB = 0;
901  }
902 
903  //Configure the third unicast address filter
904  if(j >= 3)
905  {
906  //The address is activated when SAT register is written
907  GMAC_REGS->SA[3].GMAC_SAB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
908  GMAC_REGS->SA[3].GMAC_SAT = unicastMacAddr[2].w[2];
909  }
910  else
911  {
912  //The address is deactivated when SAB register is written
913  GMAC_REGS->SA[3].GMAC_SAB = 0;
914  }
915 
916  //Configure the multicast hash table
917  GMAC_REGS->GMAC_HRB = hashTable[0];
918  GMAC_REGS->GMAC_HRT = hashTable[1];
919 
920  //Debug message
921  TRACE_DEBUG(" HRB = %08" PRIX32 "\r\n", GMAC_REGS->GMAC_HRB);
922  TRACE_DEBUG(" HRT = %08" PRIX32 "\r\n", GMAC_REGS->GMAC_HRT);
923 
924  //Successful processing
925  return NO_ERROR;
926 }
927 
928 
929 /**
930  * @brief Adjust MAC configuration parameters for proper operation
931  * @param[in] interface Underlying network interface
932  * @return Error code
933  **/
934 
936 {
937  uint32_t config;
938 
939  //Read network configuration register
940  config = GMAC_REGS->GMAC_NCFGR;
941 
942  //10BASE-T or 100BASE-TX operation mode?
943  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
944  {
945  config |= GMAC_NCFGR_SPD_Msk;
946  }
947  else
948  {
949  config &= ~GMAC_NCFGR_SPD_Msk;
950  }
951 
952  //Half-duplex or full-duplex mode?
953  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
954  {
955  config |= GMAC_NCFGR_FD_Msk;
956  }
957  else
958  {
959  config &= ~GMAC_NCFGR_FD_Msk;
960  }
961 
962  //Write configuration value back to NCFGR register
963  GMAC_REGS->GMAC_NCFGR = config;
964 
965  //Successful processing
966  return NO_ERROR;
967 }
968 
969 
970 /**
971  * @brief Write PHY register
972  * @param[in] opcode Access type (2 bits)
973  * @param[in] phyAddr PHY address (5 bits)
974  * @param[in] regAddr Register address (5 bits)
975  * @param[in] data Register value
976  **/
977 
978 void same54EthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
979  uint8_t regAddr, uint16_t data)
980 {
981  uint32_t temp;
982 
983  //Valid opcode?
984  if(opcode == SMI_OPCODE_WRITE)
985  {
986  //Set up a write operation
987  temp = GMAC_MAN_CLTTO_Msk | GMAC_MAN_OP(1) | GMAC_MAN_WTN(2);
988  //PHY address
989  temp |= GMAC_MAN_PHYA(phyAddr);
990  //Register address
991  temp |= GMAC_MAN_REGA(regAddr);
992  //Register value
993  temp |= GMAC_MAN_DATA(data);
994 
995  //Start a write operation
996  GMAC_REGS->GMAC_MAN = temp;
997  //Wait for the write to complete
998  while((GMAC_REGS->GMAC_NSR & GMAC_NSR_IDLE_Msk) == 0)
999  {
1000  }
1001  }
1002  else
1003  {
1004  //The MAC peripheral only supports standard Clause 22 opcodes
1005  }
1006 }
1007 
1008 
1009 /**
1010  * @brief Read PHY register
1011  * @param[in] opcode Access type (2 bits)
1012  * @param[in] phyAddr PHY address (5 bits)
1013  * @param[in] regAddr Register address (5 bits)
1014  * @return Register value
1015  **/
1016 
1017 uint16_t same54EthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
1018  uint8_t regAddr)
1019 {
1020  uint16_t data;
1021  uint32_t temp;
1022 
1023  //Valid opcode?
1024  if(opcode == SMI_OPCODE_READ)
1025  {
1026  //Set up a read operation
1027  temp = GMAC_MAN_CLTTO_Msk | GMAC_MAN_OP(2) | GMAC_MAN_WTN(2);
1028  //PHY address
1029  temp |= GMAC_MAN_PHYA(phyAddr);
1030  //Register address
1031  temp |= GMAC_MAN_REGA(regAddr);
1032 
1033  //Start a read operation
1034  GMAC_REGS->GMAC_MAN = temp;
1035  //Wait for the read to complete
1036  while((GMAC_REGS->GMAC_NSR & GMAC_NSR_IDLE_Msk) == 0)
1037  {
1038  }
1039 
1040  //Get register value
1041  data = GMAC_REGS->GMAC_MAN & GMAC_MAN_DATA_Msk;
1042  }
1043  else
1044  {
1045  //The MAC peripheral only supports standard Clause 22 opcodes
1046  data = 0;
1047  }
1048 
1049  //Return the value of the PHY register
1050  return data;
1051 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
#define SAME54_ETH_RX_BUFFER_SIZE
uint8_t opcode
Definition: dns_common.h:188
int bool_t
Definition: compiler_port.h:53
#define GMAC_TX_LENGTH
Transmit buffer descriptor.
error_t same54EthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
#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
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 SAME54_ETH_TX_BUFFER_SIZE
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:222
Receive buffer descriptor.
#define sleep(delay)
Definition: os_port.h:307
#define ETH_MAX_FRAME_SIZE
Definition: ethernet.h:110
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
#define GMAC_RX_WRAP
uint16_t same54EthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define SAME54_ETH_IRQ_SUB_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)
void same54EthEnableIrq(NetInterface *interface)
Enable interrupts.
#define GMAC_RX_EOF
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define SAME54_ETH_IRQ_GROUP_PRIORITY
#define GMAC_TX_USED
void same54EthTick(NetInterface *interface)
SAME54 Ethernet MAC timer handler.
__weak_func void same54EthInitGpio(NetInterface *interface)
GPIO configuration.
#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
void same54EthDisableIrq(NetInterface *interface)
Disable interrupts.
const NicDriver same54EthDriver
SAME54 Ethernet MAC driver.
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:104
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
error_t same54EthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define SAME54_ETH_RX_BUFFER_COUNT
#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
void GMAC_Handler(void)
SAME54 Ethernet MAC interrupt service routine.
#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
#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
uint16_t regAddr
void same54EthInitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
#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()
void same54EthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
error_t same54EthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
SAME54 Ethernet MAC driver.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
error_t same54EthInit(NetInterface *interface)
SAME54 Ethernet MAC initialization.
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
void same54EthEventHandler(NetInterface *interface)
SAME54 Ethernet MAC event handler.
NIC driver.
Definition: nic.h:286
#define GMAC_RX_OWNERSHIP
error_t same54EthReceivePacket(NetInterface *interface)
Receive a packet.
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
#define SAME54_ETH_IRQ_PRIORITY_GROUPING
#define SAME54_ETH_TX_BUFFER_COUNT
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83