sam9x6_eth1_driver.c
Go to the documentation of this file.
1 /**
2  * @file sam9x6_eth1_driver.c
3  * @brief SAM9X60 Ethernet MAC driver (EMAC0 instance)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include <limits.h>
36 #include "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
49 #pragma location = SAM9X6_ETH1_RAM_SECTION
51 //RX buffer
52 #pragma data_alignment = 8
53 #pragma location = SAM9X6_ETH1_RAM_SECTION
55 //TX buffer descriptors
56 #pragma data_alignment = 4
57 #pragma location = SAM9X6_ETH1_RAM_SECTION
59 //RX buffer descriptors
60 #pragma data_alignment = 4
61 #pragma location = SAM9X6_ETH1_RAM_SECTION
63 
64 //Keil MDK-ARM or GCC compiler?
65 #else
66 
67 //TX buffer
69  __attribute__((aligned(8), __section__(SAM9X6_ETH1_RAM_SECTION)));
70 //RX buffer
72  __attribute__((aligned(8), __section__(SAM9X6_ETH1_RAM_SECTION)));
73 //TX buffer descriptors
75  __attribute__((aligned(4), __section__(SAM9X6_ETH1_RAM_SECTION)));
76 //RX buffer descriptors
78  __attribute__((aligned(4), __section__(SAM9X6_ETH1_RAM_SECTION)));
79 
80 #endif
81 
82 //TX buffer index
83 static uint_t txBufferIndex;
84 //RX buffer index
85 static uint_t rxBufferIndex;
86 
87 
88 /**
89  * @brief SAM9X6 Ethernet MAC driver (EMAC0 instance)
90  **/
91 
93 {
95  ETH_MTU,
106  TRUE,
107  TRUE,
108  TRUE,
109  FALSE
110 };
111 
112 
113 /**
114  * @brief SAM9X6 Ethernet MAC initialization
115  * @param[in] interface Underlying network interface
116  * @return Error code
117  **/
118 
120 {
121  error_t error;
122  volatile uint32_t temp;
123 
124  //Debug message
125  TRACE_INFO("Initializing SAM9X6 Ethernet MAC (EMAC0)...\r\n");
126 
127  //Save underlying network interface
128  nicDriverInterface = interface;
129 
130  //Enable EMAC peripheral clock
131  PMC_REGS->PMC_PCR = PMC_PCR_PID(ID_EMAC0);
132  temp = PMC_REGS->PMC_PCR;
133  PMC_REGS->PMC_PCR = temp | PMC_PCR_CMD_Msk | PMC_PCR_EN_Msk;
134 
135  //Disable transmit and receive circuits
136  EMAC0_REGS->EMAC_NCR = 0;
137 
138  //GPIO configuration
139  sam9x6Eth1InitGpio(interface);
140 
141  //Configure MDC clock speed
142  EMAC0_REGS->EMAC_NCFGR = EMAC_NCFGR_CLK_MCK_64;
143  //Enable management port (MDC and MDIO)
144  EMAC0_REGS->EMAC_NCR |= EMAC_NCR_MPE_Msk;
145 
146  //Valid Ethernet PHY or switch driver?
147  if(interface->phyDriver != NULL)
148  {
149  //Ethernet PHY initialization
150  error = interface->phyDriver->init(interface);
151  }
152  else if(interface->switchDriver != NULL)
153  {
154  //Ethernet switch initialization
155  error = interface->switchDriver->init(interface);
156  }
157  else
158  {
159  //The interface is not properly configured
160  error = ERROR_FAILURE;
161  }
162 
163  //Any error to report?
164  if(error)
165  {
166  return error;
167  }
168 
169  //Set the MAC address of the station
170  EMAC0_REGS->EMAC_SA[0].EMAC_SAxB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
171  EMAC0_REGS->EMAC_SA[0].EMAC_SAxT = interface->macAddr.w[2];
172 
173  //The MAC supports 3 additional addresses for unicast perfect filtering
174  EMAC0_REGS->EMAC_SA[1].EMAC_SAxB = 0;
175  EMAC0_REGS->EMAC_SA[2].EMAC_SAxB = 0;
176  EMAC0_REGS->EMAC_SA[3].EMAC_SAxB = 0;
177 
178  //Initialize hash table
179  EMAC0_REGS->EMAC_HRB = 0;
180  EMAC0_REGS->EMAC_HRT = 0;
181 
182  //Configure the receive filter
183  EMAC0_REGS->EMAC_NCFGR |= EMAC_NCFGR_BIG_Msk | EMAC_NCFGR_MTI_Msk;
184 
185  //Initialize buffer descriptors
186  sam9x6Eth1InitBufferDesc(interface);
187 
188  //Clear transmit status register
189  EMAC0_REGS->EMAC_TSR = EMAC_TSR_UND_Msk | EMAC_TSR_COMP_Msk |
190  EMAC_TSR_BEX_Msk | EMAC_TSR_TGO_Msk | EMAC_TSR_RLES_Msk |
191  EMAC_TSR_COL_Msk | EMAC_TSR_UBR_Msk;
192 
193  //Clear receive status register
194  EMAC0_REGS->EMAC_RSR = EMAC_RSR_OVR_Msk | EMAC_RSR_REC_Msk |
195  EMAC_RSR_BNA_Msk;
196 
197  //First disable all EMAC interrupts
198  EMAC0_REGS->EMAC_IDR = 0xFFFFFFFF;
199 
200  //Only the desired ones are enabled
201  EMAC0_REGS->EMAC_IER = EMAC_IER_ROVR_Msk | EMAC_IER_TCOMP_Msk |
202  EMAC_IER_TXERR_Msk | EMAC_IER_RLE_Msk | EMAC_IER_TUND_Msk |
203  EMAC_IER_RXUBR_Msk | EMAC_IER_RCOMP_Msk;
204 
205  //Read EMAC_ISR register to clear any pending interrupt
206  temp = EMAC0_REGS->EMAC_ISR;
207  (void) temp;
208 
209  //Configure interrupt controller
210  AIC_REGS->AIC_SSR = ID_EMAC0;
211  AIC_REGS->AIC_SMR = AIC_SMR_SRCTYPE_INT_LEVEL_SENSITIVE | AIC_SMR_PRIOR(SAM9X6_ETH1_IRQ_PRIORITY);
212  AIC_REGS->AIC_SVR = (uint32_t) sam9x6Eth1IrqHandler;
213 
214  //Clear EMAC interrupt flag
215  AIC_REGS->AIC_ICCR = (1 << ID_EMAC0);
216 
217  //Enable the EMAC to transmit and receive data
218  EMAC0_REGS->EMAC_NCR |= EMAC_NCR_TE_Msk | EMAC_NCR_RE_Msk;
219 
220  //Accept any packets from the upper layer
221  osSetEvent(&interface->nicTxEvent);
222 
223  //Successful initialization
224  return NO_ERROR;
225 }
226 
227 
228 /**
229  * @brief GPIO configuration
230  * @param[in] interface Underlying network interface
231  **/
232 
233 __weak_func void sam9x6Eth1InitGpio(NetInterface *interface)
234 {
235 //SAM9X6-EK or SAM9X6 Curiosity evaluation board?
236 #if defined(USE_SAM9X6_EK) || defined(USE_SAM9X6_CURIOSITY)
237  uint32_t temp;
238  uint32_t mask;
239 
240  //Enable PIO peripheral clock
241  PMC_REGS->PMC_PCR = PMC_PCR_PID(ID_PIOB);
242  temp = PMC_REGS->PMC_PCR;
243  PMC_REGS->PMC_PCR = temp | PMC_PCR_CMD_Msk | PMC_PCR_EN_Msk;
244 
245  //Configure RMII pins
246  mask = PIO_PB10A_EMAC0_E0_TX1 | PIO_PB9A_EMAC0_E0_TX0 |
247  PIO_PB7A_EMAC0_E0_TXEN | PIO_PB6A_EMAC0_E0_MDC | PIO_PB5A_EMAC0_E0_MDIO |
248  PIO_PB4A_EMAC0_E0_TXCK | PIO_PB3A_EMAC0_E0_RXDV | PIO_PB2A_EMAC0_E0_RXER |
249  PIO_PB1A_EMAC0_E0_RX1 | PIO_PB0A_EMAC0_E0_RX0;
250 
251  //Disable pull-up resistors on RMII pins
252  PIOB_REGS->PIO_PUDR = mask;
253  //Disable interrupts-on-change
254  PIOB_REGS->PIO_IDR = mask;
255  //Assign RMII pins to to the relevant peripheral function
256  PIOB_REGS->PIO_ABCDSR[0] &= ~mask;
257  PIOB_REGS->PIO_ABCDSR[1] &= ~mask;
258  //Disable the PIO from controlling the corresponding pins
259  PIOB_REGS->PIO_PDR = mask;
260 
261  //Select RMII operation mode and enable transceiver clock
262  EMAC0_REGS->EMAC_USRIO = EMAC_USRIO_CLKEN_Msk | EMAC_USRIO_RMII_Msk;
263 #endif
264 }
265 
266 
267 /**
268  * @brief Initialize buffer descriptors
269  * @param[in] interface Underlying network interface
270  **/
271 
273 {
274  uint_t i;
275  uint32_t address;
276 
277  //Initialize TX buffer descriptors
278  for(i = 0; i < SAM9X6_ETH1_TX_BUFFER_COUNT; i++)
279  {
280  //Calculate the address of the current TX buffer
281  address = (uint32_t) txBuffer[i];
282  //Write the address to the descriptor entry
283  txBufferDesc[i].address = address;
284  //Initialize status field
285  txBufferDesc[i].status = EMAC_TX_USED;
286  }
287 
288  //Mark the last descriptor entry with the wrap flag
289  txBufferDesc[i - 1].status |= EMAC_TX_WRAP;
290  //Initialize TX buffer index
291  txBufferIndex = 0;
292 
293  //Initialize RX buffer descriptors
294  for(i = 0; i < SAM9X6_ETH1_RX_BUFFER_COUNT; i++)
295  {
296  //Calculate the address of the current RX buffer
297  address = (uint32_t) rxBuffer[i];
298  //Write the address to the descriptor entry
299  rxBufferDesc[i].address = address & EMAC_RX_ADDRESS;
300  //Clear status field
301  rxBufferDesc[i].status = 0;
302  }
303 
304  //Mark the last descriptor entry with the wrap flag
305  rxBufferDesc[i - 1].address |= EMAC_RX_WRAP;
306  //Initialize RX buffer index
307  rxBufferIndex = 0;
308 
309  //Start location of the TX descriptor list
310  EMAC0_REGS->EMAC_TBQP = (uint32_t) txBufferDesc;
311  //Start location of the RX descriptor list
312  EMAC0_REGS->EMAC_RBQP = (uint32_t) rxBufferDesc;
313 }
314 
315 
316 /**
317  * @brief SAM9X6 Ethernet MAC timer handler
318  *
319  * This routine is periodically called by the TCP/IP stack to handle periodic
320  * operations such as polling the link state
321  *
322  * @param[in] interface Underlying network interface
323  **/
324 
325 void sam9x6Eth1Tick(NetInterface *interface)
326 {
327  //Valid Ethernet PHY or switch driver?
328  if(interface->phyDriver != NULL)
329  {
330  //Handle periodic operations
331  interface->phyDriver->tick(interface);
332  }
333  else if(interface->switchDriver != NULL)
334  {
335  //Handle periodic operations
336  interface->switchDriver->tick(interface);
337  }
338  else
339  {
340  //Just for sanity
341  }
342 }
343 
344 
345 /**
346  * @brief Enable interrupts
347  * @param[in] interface Underlying network interface
348  **/
349 
351 {
352  //Enable Ethernet MAC interrupts
353  AIC_REGS->AIC_SSR = AIC_SSR_INTSEL(ID_EMAC0);
354  AIC_REGS->AIC_IECR = AIC_IECR_INTEN_Msk;
355 
356  //Valid Ethernet PHY or switch driver?
357  if(interface->phyDriver != NULL)
358  {
359  //Enable Ethernet PHY interrupts
360  interface->phyDriver->enableIrq(interface);
361  }
362  else if(interface->switchDriver != NULL)
363  {
364  //Enable Ethernet switch interrupts
365  interface->switchDriver->enableIrq(interface);
366  }
367  else
368  {
369  //Just for sanity
370  }
371 }
372 
373 
374 /**
375  * @brief Disable interrupts
376  * @param[in] interface Underlying network interface
377  **/
378 
380 {
381  //Disable Ethernet MAC interrupts
382  AIC_REGS->AIC_SSR = AIC_SSR_INTSEL(ID_EMAC0);
383  AIC_REGS->AIC_IDCR = AIC_IDCR_INTD_Msk;
384 
385  //Valid Ethernet PHY or switch driver?
386  if(interface->phyDriver != NULL)
387  {
388  //Disable Ethernet PHY interrupts
389  interface->phyDriver->disableIrq(interface);
390  }
391  else if(interface->switchDriver != NULL)
392  {
393  //Disable Ethernet switch interrupts
394  interface->switchDriver->disableIrq(interface);
395  }
396  else
397  {
398  //Just for sanity
399  }
400 }
401 
402 
403 /**
404  * @brief SAM9X6 Ethernet MAC interrupt service routine
405  **/
406 
408 {
409  bool_t flag;
410  volatile uint32_t isr;
411  volatile uint32_t tsr;
412  volatile uint32_t rsr;
413 
414  //Interrupt service routine prologue
415  osEnterIsr();
416 
417  //This flag will be set if a higher priority task must be woken
418  flag = FALSE;
419 
420  //Each time the software reads EMAC_ISR, it has to check the contents
421  //of EMAC_TSR, EMAC_RSR and EMAC_NSR
422  isr = EMAC0_REGS->EMAC_ISR;
423  tsr = EMAC0_REGS->EMAC_TSR;
424  rsr = EMAC0_REGS->EMAC_RSR;
425  (void) isr;
426 
427  //Packet transmitted?
428  if((tsr & (EMAC_TSR_UND_Msk | EMAC_TSR_COMP_Msk | EMAC_TSR_BEX_Msk |
429  EMAC_TSR_TGO_Msk | EMAC_TSR_RLES_Msk | EMAC_TSR_COL_Msk |
430  EMAC_TSR_UBR_Msk)) != 0)
431  {
432  //Only clear TSR flags that are currently set
433  EMAC0_REGS->EMAC_TSR = tsr;
434 
435  //Check whether the TX buffer is available for writing
436  if((txBufferDesc[txBufferIndex].status & EMAC_TX_USED) != 0)
437  {
438  //Notify the TCP/IP stack that the transmitter is ready to send
439  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
440  }
441  }
442 
443  //Packet received?
444  if((rsr & (EMAC_RSR_OVR_Msk | EMAC_RSR_REC_Msk | EMAC_RSR_BNA_Msk)) != 0)
445  {
446  //Set event flag
447  nicDriverInterface->nicEvent = TRUE;
448  //Notify the TCP/IP stack of the event
449  flag |= osSetEventFromIsr(&netEvent);
450  }
451 
452 #if (NET_RTOS_SUPPORT == DISABLED)
453  //Write AIC_EOICR register before exiting
454  AIC_REGS->AIC_EOICR = 0;
455 #endif
456 
457  //Interrupt service routine epilogue
458  osExitIsr(flag);
459 }
460 
461 
462 /**
463  * @brief SAM9X6 Ethernet MAC event handler
464  * @param[in] interface Underlying network interface
465  **/
466 
468 {
469  error_t error;
470  uint32_t rsr;
471 
472  //Read receive status
473  rsr = EMAC0_REGS->EMAC_RSR;
474 
475  //Packet received?
476  if((rsr & (EMAC_RSR_OVR_Msk | EMAC_RSR_REC_Msk | EMAC_RSR_BNA_Msk)) != 0)
477  {
478  //Only clear RSR flags that are currently set
479  EMAC0_REGS->EMAC_RSR = rsr;
480 
481  //Process all pending packets
482  do
483  {
484  //Read incoming packet
485  error = sam9x6Eth1ReceivePacket(interface);
486 
487  //No more data in the receive buffer?
488  } while(error != ERROR_BUFFER_EMPTY);
489  }
490 }
491 
492 
493 /**
494  * @brief Send a packet
495  * @param[in] interface Underlying network interface
496  * @param[in] buffer Multi-part buffer containing the data to send
497  * @param[in] offset Offset to the first data byte
498  * @param[in] ancillary Additional options passed to the stack along with
499  * the packet
500  * @return Error code
501  **/
502 
504  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
505 {
506  size_t length;
507 
508  //Retrieve the length of the packet
509  length = netBufferGetLength(buffer) - offset;
510 
511  //Check the frame length
513  {
514  //The transmitter can accept another packet
515  osSetEvent(&interface->nicTxEvent);
516  //Report an error
517  return ERROR_INVALID_LENGTH;
518  }
519 
520  //Make sure the current buffer is available for writing
521  if((txBufferDesc[txBufferIndex].status & EMAC_TX_USED) == 0)
522  {
523  return ERROR_FAILURE;
524  }
525 
526  //Copy user data to the transmit buffer
527  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
528 
529  //Set the necessary flags in the descriptor entry
530  if(txBufferIndex < (SAM9X6_ETH1_TX_BUFFER_COUNT - 1))
531  {
532  //Write the status word
533  txBufferDesc[txBufferIndex].status = EMAC_TX_LAST |
535 
536  //Point to the next buffer
537  txBufferIndex++;
538  }
539  else
540  {
541  //Write the status word
542  txBufferDesc[txBufferIndex].status = EMAC_TX_WRAP | EMAC_TX_LAST |
544 
545  //Wrap around
546  txBufferIndex = 0;
547  }
548 
549  //Set the TSTART bit to initiate transmission
550  EMAC0_REGS->EMAC_NCR |= EMAC_NCR_TSTART_Msk;
551 
552  //Check whether the next buffer is available for writing
553  if((txBufferDesc[txBufferIndex].status & EMAC_TX_USED) != 0)
554  {
555  //The transmitter can accept another packet
556  osSetEvent(&interface->nicTxEvent);
557  }
558 
559  //Successful processing
560  return NO_ERROR;
561 }
562 
563 
564 /**
565  * @brief Receive a packet
566  * @param[in] interface Underlying network interface
567  * @return Error code
568  **/
569 
571 {
572  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
573  error_t error;
574  uint_t i;
575  uint_t j;
576  uint_t sofIndex;
577  uint_t eofIndex;
578  size_t n;
579  size_t size;
580  size_t length;
581 
582  //Initialize variables
583  size = 0;
584  sofIndex = UINT_MAX;
585  eofIndex = UINT_MAX;
586 
587  //Search for SOF and EOF flags
588  for(i = 0; i < SAM9X6_ETH1_RX_BUFFER_COUNT; i++)
589  {
590  //Point to the current entry
591  j = rxBufferIndex + i;
592 
593  //Wrap around to the beginning of the buffer if necessary
595  {
597  }
598 
599  //No more entries to process?
600  if((rxBufferDesc[j].address & EMAC_RX_OWNERSHIP) == 0)
601  {
602  //Stop processing
603  break;
604  }
605 
606  //A valid SOF has been found?
607  if((rxBufferDesc[j].status & EMAC_RX_SOF) != 0)
608  {
609  //Save the position of the SOF
610  sofIndex = i;
611  }
612 
613  //A valid EOF has been found?
614  if((rxBufferDesc[j].status & EMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
615  {
616  //Save the position of the EOF
617  eofIndex = i;
618  //Retrieve the length of the frame
619  size = rxBufferDesc[j].status & EMAC_RX_LENGTH;
620  //Limit the number of data to read
621  size = MIN(size, ETH_MAX_FRAME_SIZE);
622  //Stop processing since we have reached the end of the frame
623  break;
624  }
625  }
626 
627  //Determine the number of entries to process
628  if(eofIndex != UINT_MAX)
629  {
630  j = eofIndex + 1;
631  }
632  else if(sofIndex != UINT_MAX)
633  {
634  j = sofIndex;
635  }
636  else
637  {
638  j = i;
639  }
640 
641  //Total number of bytes that have been copied from the receive buffer
642  length = 0;
643 
644  //Process incoming frame
645  for(i = 0; i < j; i++)
646  {
647  //Any data to copy from current buffer?
648  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
649  {
650  //Calculate the number of bytes to read at a time
652  //Copy data from receive buffer
653  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
654  //Update byte counters
655  length += n;
656  size -= n;
657  }
658 
659  //Mark the current buffer as free
660  rxBufferDesc[rxBufferIndex].address &= ~EMAC_RX_OWNERSHIP;
661 
662  //Point to the following entry
663  rxBufferIndex++;
664 
665  //Wrap around to the beginning of the buffer if necessary
666  if(rxBufferIndex >= SAM9X6_ETH1_RX_BUFFER_COUNT)
667  {
668  rxBufferIndex = 0;
669  }
670  }
671 
672  //Any packet to process?
673  if(length > 0)
674  {
675  NetRxAncillary ancillary;
676 
677  //Additional options can be passed to the stack along with the packet
678  ancillary = NET_DEFAULT_RX_ANCILLARY;
679 
680  //Pass the packet to the upper layer
681  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
682  //Valid packet received
683  error = NO_ERROR;
684  }
685  else
686  {
687  //No more data in the receive buffer
688  error = ERROR_BUFFER_EMPTY;
689  }
690 
691  //Return status code
692  return error;
693 }
694 
695 
696 /**
697  * @brief Configure MAC address filtering
698  * @param[in] interface Underlying network interface
699  * @return Error code
700  **/
701 
703 {
704  uint_t i;
705  uint_t j;
706  uint_t k;
707  uint8_t *p;
708  uint32_t hashTable[2];
709  MacAddr unicastMacAddr[3];
710  MacFilterEntry *entry;
711 
712  //Debug message
713  TRACE_DEBUG("Updating MAC filter...\r\n");
714 
715  //Set the MAC address of the station
716  EMAC0_REGS->EMAC_SA[0].EMAC_SAxB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
717  EMAC0_REGS->EMAC_SA[0].EMAC_SAxT = interface->macAddr.w[2];
718 
719  //The MAC supports 3 additional addresses for unicast perfect filtering
720  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
721  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
722  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
723 
724  //The hash table is used for multicast address filtering
725  hashTable[0] = 0;
726  hashTable[1] = 0;
727 
728  //The MAC address filter contains the list of MAC addresses to accept
729  //when receiving an Ethernet frame
730  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
731  {
732  //Point to the current entry
733  entry = &interface->macAddrFilter[i];
734 
735  //Valid entry?
736  if(entry->refCount > 0)
737  {
738  //Multicast address?
739  if(macIsMulticastAddr(&entry->addr))
740  {
741  //Point to the MAC address
742  p = entry->addr.b;
743 
744  //Apply the hash function
745  k = (p[0] >> 6) ^ p[0];
746  k ^= (p[1] >> 4) ^ (p[1] << 2);
747  k ^= (p[2] >> 2) ^ (p[2] << 4);
748  k ^= (p[3] >> 6) ^ p[3];
749  k ^= (p[4] >> 4) ^ (p[4] << 2);
750  k ^= (p[5] >> 2) ^ (p[5] << 4);
751 
752  //The hash value is reduced to a 6-bit index
753  k &= 0x3F;
754 
755  //Update hash table contents
756  hashTable[k / 32] |= (1 << (k % 32));
757  }
758  else
759  {
760  //Up to 3 additional MAC addresses can be specified
761  if(j < 3)
762  {
763  //Save the unicast address
764  unicastMacAddr[j++] = entry->addr;
765  }
766  }
767  }
768  }
769 
770  //Configure the first unicast address filter
771  if(j >= 1)
772  {
773  //The address is activated when SAH register is written
774  EMAC0_REGS->EMAC_SA[1].EMAC_SAxB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
775  EMAC0_REGS->EMAC_SA[1].EMAC_SAxT = unicastMacAddr[0].w[2];
776  }
777  else
778  {
779  //The address is deactivated when SAL register is written
780  EMAC0_REGS->EMAC_SA[1].EMAC_SAxB = 0;
781  }
782 
783  //Configure the second unicast address filter
784  if(j >= 2)
785  {
786  //The address is activated when SAH register is written
787  EMAC0_REGS->EMAC_SA[2].EMAC_SAxB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
788  EMAC0_REGS->EMAC_SA[2].EMAC_SAxT = unicastMacAddr[1].w[2];
789  }
790  else
791  {
792  //The address is deactivated when SAL register is written
793  EMAC0_REGS->EMAC_SA[2].EMAC_SAxB = 0;
794  }
795 
796  //Configure the third unicast address filter
797  if(j >= 3)
798  {
799  //The address is activated when SAH register is written
800  EMAC0_REGS->EMAC_SA[3].EMAC_SAxB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
801  EMAC0_REGS->EMAC_SA[4].EMAC_SAxT = unicastMacAddr[2].w[2];
802  }
803  else
804  {
805  //The address is deactivated when SAL register is written
806  EMAC0_REGS->EMAC_SA[3].EMAC_SAxB = 0;
807  }
808 
809  //Configure the multicast hash table
810  EMAC0_REGS->EMAC_HRB = hashTable[0];
811  EMAC0_REGS->EMAC_HRT = hashTable[1];
812 
813  //Debug message
814  TRACE_DEBUG(" HRB = %08" PRIX32 "\r\n", EMAC0_REGS->EMAC_HRB);
815  TRACE_DEBUG(" HRT = %08" PRIX32 "\r\n", EMAC0_REGS->EMAC_HRT);
816 
817  //Successful processing
818  return NO_ERROR;
819 }
820 
821 
822 /**
823  * @brief Adjust MAC configuration parameters for proper operation
824  * @param[in] interface Underlying network interface
825  * @return Error code
826  **/
827 
829 {
830  uint32_t config;
831 
832  //Read network configuration register
833  config = EMAC0_REGS->EMAC_NCFGR;
834 
835  //10BASE-T or 100BASE-TX operation mode?
836  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
837  {
838  config |= EMAC_NCFGR_SPD_Msk;
839  }
840  else
841  {
842  config &= ~EMAC_NCFGR_SPD_Msk;
843  }
844 
845  //Half-duplex or full-duplex mode?
846  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
847  {
848  config |= EMAC_NCFGR_FD_Msk;
849  }
850  else
851  {
852  config &= ~EMAC_NCFGR_FD_Msk;
853  }
854 
855  //Write configuration value back to NCFGR register
856  EMAC0_REGS->EMAC_NCFGR = config;
857 
858  //Successful processing
859  return NO_ERROR;
860 }
861 
862 
863 /**
864  * @brief Write PHY register
865  * @param[in] opcode Access type (2 bits)
866  * @param[in] phyAddr PHY address (5 bits)
867  * @param[in] regAddr Register address (5 bits)
868  * @param[in] data Register value
869  **/
870 
871 void sam9x6Eth1WritePhyReg(uint8_t opcode, uint8_t phyAddr,
872  uint8_t regAddr, uint16_t data)
873 {
874  uint32_t temp;
875 
876  //Valid opcode?
877  if(opcode == SMI_OPCODE_WRITE)
878  {
879  //Set up a write operation
880  temp = EMAC_MAN_SOF(1) | EMAC_MAN_RW(1) | EMAC_MAN_CODE(2);
881  //PHY address
882  temp |= EMAC_MAN_PHYA(phyAddr);
883  //Register address
884  temp |= EMAC_MAN_REGA(regAddr);
885  //Register value
886  temp |= EMAC_MAN_DATA(data);
887 
888  //Start a write operation
889  EMAC0_REGS->EMAC_MAN = temp;
890  //Wait for the write to complete
891  while((EMAC0_REGS->EMAC_NSR & EMAC_NSR_IDLE_Msk) == 0)
892  {
893  }
894  }
895  else
896  {
897  //The MAC peripheral only supports standard Clause 22 opcodes
898  }
899 }
900 
901 
902 /**
903  * @brief Read PHY register
904  * @param[in] opcode Access type (2 bits)
905  * @param[in] phyAddr PHY address (5 bits)
906  * @param[in] regAddr Register address (5 bits)
907  * @return Register value
908  **/
909 
910 uint16_t sam9x6Eth1ReadPhyReg(uint8_t opcode, uint8_t phyAddr,
911  uint8_t regAddr)
912 {
913  uint16_t data;
914  uint32_t temp;
915 
916  //Valid opcode?
917  if(opcode == SMI_OPCODE_READ)
918  {
919  //Set up a read operation
920  temp = EMAC_MAN_SOF(1) | EMAC_MAN_RW(2) | EMAC_MAN_CODE(2);
921  //PHY address
922  temp |= EMAC_MAN_PHYA(phyAddr);
923  //Register address
924  temp |= EMAC_MAN_REGA(regAddr);
925 
926  //Start a read operation
927  EMAC0_REGS->EMAC_MAN = temp;
928  //Wait for the read to complete
929  while((EMAC0_REGS->EMAC_NSR & EMAC_NSR_IDLE_Msk) == 0)
930  {
931  }
932 
933  //Get register value
934  data = EMAC0_REGS->EMAC_MAN & EMAC_MAN_DATA_Msk;
935  }
936  else
937  {
938  //The MAC peripheral only supports standard Clause 22 opcodes
939  data = 0;
940  }
941 
942  //Return the value of the PHY register
943  return data;
944 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
uint8_t opcode
Definition: dns_common.h:188
#define SAM9X6_ETH1_RX_BUFFER_SIZE
int bool_t
Definition: compiler_port.h:61
error_t sam9x6Eth1ReceivePacket(NetInterface *interface)
Receive a packet.
#define netEvent
Definition: net_legacy.h:196
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
void sam9x6Eth1DisableIrq(NetInterface *interface)
Disable interrupts.
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
void sam9x6Eth1InitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define EMAC_RX_WRAP
#define TRUE
Definition: os_port.h:50
error_t sam9x6Eth1SendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
uint8_t data[]
Definition: ethernet.h:222
#define ETH_MAX_FRAME_SIZE
Definition: ethernet.h:110
Transmit buffer descriptor.
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
SAM9X60 Ethernet MAC driver (EMAC0 instance)
#define EMAC_RX_EOF
__weak_func void sam9x6Eth1InitGpio(NetInterface *interface)
GPIO configuration.
#define EMAC_RX_OWNERSHIP
#define SAM9X6_ETH1_RX_BUFFER_COUNT
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
Receive buffer descriptor.
#define osExitIsr(flag)
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define EMAC_RX_LENGTH
uint16_t sam9x6Eth1ReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define FALSE
Definition: os_port.h:46
#define SAM9X6_ETH1_TX_BUFFER_SIZE
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
#define EMAC_TX_LENGTH
error_t
Error codes.
Definition: error.h:43
#define SAM9X6_ETH1_TX_BUFFER_COUNT
const NicDriver sam9x6Eth1Driver
SAM9X6 Ethernet MAC driver (EMAC0 instance)
error_t sam9x6Eth1UpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
void sam9x6Eth1WritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:105
@ 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:142
#define SAM9X6_ETH1_IRQ_PRIORITY
#define NetTxAncillary
Definition: net_misc.h:36
uint8_t mask
Definition: web_socket.h:319
void sam9x6Eth1Tick(NetInterface *interface)
SAM9X6 Ethernet MAC timer handler.
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
#define SAM9X6_ETH1_RAM_SECTION
#define MIN(a, b)
Definition: os_port.h:63
error_t sam9x6Eth1UpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define rxBuffer
MacAddr
Definition: ethernet.h:195
#define TRACE_DEBUG(...)
Definition: debug.h:119
void sam9x6Eth1IrqHandler(void)
SAM9X6 Ethernet MAC interrupt service routine.
uint16_t regAddr
#define EMAC_TX_USED
#define ETH_MTU
Definition: ethernet.h:116
uint8_t n
MAC filter table entry.
Definition: ethernet.h:262
void sam9x6Eth1EnableIrq(NetInterface *interface)
Enable interrupts.
Ipv6Addr address[]
Definition: ipv6.h:325
#define osEnterIsr()
#define EMAC_RX_ADDRESS
#define EMAC_TX_WRAP
#define EMAC_RX_SOF
error_t sam9x6Eth1Init(NetInterface *interface)
SAM9X6 Ethernet MAC initialization.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
NIC driver.
Definition: nic.h:286
void sam9x6Eth1EventHandler(NetInterface *interface)
SAM9X6 Ethernet MAC event handler.
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 EMAC_TX_LAST
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83