sam9x6_eth2_driver.c
Go to the documentation of this file.
1 /**
2  * @file sam9x6_eth2_driver.c
3  * @brief SAM9X60 Ethernet MAC driver (EMAC1 instance)
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
49 #pragma location = SAM9X6_ETH2_RAM_SECTION
51 //RX buffer
52 #pragma data_alignment = 8
53 #pragma location = SAM9X6_ETH2_RAM_SECTION
55 //TX buffer descriptors
56 #pragma data_alignment = 4
57 #pragma location = SAM9X6_ETH2_RAM_SECTION
59 //RX buffer descriptors
60 #pragma data_alignment = 4
61 #pragma location = SAM9X6_ETH2_RAM_SECTION
63 
64 //Keil MDK-ARM or GCC compiler?
65 #else
66 
67 //TX buffer
69  __attribute__((aligned(8), __section__(SAM9X6_ETH2_RAM_SECTION)));
70 //RX buffer
72  __attribute__((aligned(8), __section__(SAM9X6_ETH2_RAM_SECTION)));
73 //TX buffer descriptors
75  __attribute__((aligned(4), __section__(SAM9X6_ETH2_RAM_SECTION)));
76 //RX buffer descriptors
78  __attribute__((aligned(4), __section__(SAM9X6_ETH2_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 (EMAC1 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 (EMAC1)...\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_EMAC1);
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  EMAC1_REGS->EMAC_NCR = 0;
137 
138  //GPIO configuration
139  sam9x6Eth2InitGpio(interface);
140 
141  //Configure MDC clock speed
142  EMAC1_REGS->EMAC_NCFGR = EMAC_NCFGR_CLK_MCK_64;
143  //Enable management port (MDC and MDIO)
144  EMAC1_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  EMAC1_REGS->EMAC_SA[0].EMAC_SAxB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
171  EMAC1_REGS->EMAC_SA[0].EMAC_SAxT = interface->macAddr.w[2];
172 
173  //The MAC supports 3 additional addresses for unicast perfect filtering
174  EMAC1_REGS->EMAC_SA[1].EMAC_SAxB = 0;
175  EMAC1_REGS->EMAC_SA[2].EMAC_SAxB = 0;
176  EMAC1_REGS->EMAC_SA[3].EMAC_SAxB = 0;
177 
178  //Initialize hash table
179  EMAC1_REGS->EMAC_HRB = 0;
180  EMAC1_REGS->EMAC_HRT = 0;
181 
182  //Configure the receive filter
183  EMAC1_REGS->EMAC_NCFGR |= EMAC_NCFGR_BIG_Msk | EMAC_NCFGR_MTI_Msk;
184 
185  //Initialize buffer descriptors
186  sam9x6Eth2InitBufferDesc(interface);
187 
188  //Clear transmit status register
189  EMAC1_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  EMAC1_REGS->EMAC_RSR = EMAC_RSR_OVR_Msk | EMAC_RSR_REC_Msk |
195  EMAC_RSR_BNA_Msk;
196 
197  //First disable all EMAC interrupts
198  EMAC1_REGS->EMAC_IDR = 0xFFFFFFFF;
199 
200  //Only the desired ones are enabled
201  EMAC1_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 = EMAC1_REGS->EMAC_ISR;
207  (void) temp;
208 
209  //Configure interrupt controller
210  AIC_REGS->AIC_SSR = ID_EMAC1;
211  AIC_REGS->AIC_SMR = AIC_SMR_SRCTYPE_INT_LEVEL_SENSITIVE | AIC_SMR_PRIOR(SAM9X6_ETH2_IRQ_PRIORITY);
212  AIC_REGS->AIC_SVR = (uint32_t) sam9x6Eth2IrqHandler;
213 
214  //Clear EMAC interrupt flag
215  AIC_REGS->AIC_ICCR = (1 << ID_EMAC1);
216 
217  //Enable the EMAC to transmit and receive data
218  EMAC1_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 sam9x6Eth2InitGpio(NetInterface *interface)
234 {
235 }
236 
237 
238 /**
239  * @brief Initialize buffer descriptors
240  * @param[in] interface Underlying network interface
241  **/
242 
244 {
245  uint_t i;
246  uint32_t address;
247 
248  //Initialize TX buffer descriptors
249  for(i = 0; i < SAM9X6_ETH2_TX_BUFFER_COUNT; i++)
250  {
251  //Calculate the address of the current TX buffer
252  address = (uint32_t) txBuffer[i];
253  //Write the address to the descriptor entry
254  txBufferDesc[i].address = address;
255  //Initialize status field
256  txBufferDesc[i].status = EMAC_TX_USED;
257  }
258 
259  //Mark the last descriptor entry with the wrap flag
260  txBufferDesc[i - 1].status |= EMAC_TX_WRAP;
261  //Initialize TX buffer index
262  txBufferIndex = 0;
263 
264  //Initialize RX buffer descriptors
265  for(i = 0; i < SAM9X6_ETH2_RX_BUFFER_COUNT; i++)
266  {
267  //Calculate the address of the current RX buffer
268  address = (uint32_t) rxBuffer[i];
269  //Write the address to the descriptor entry
270  rxBufferDesc[i].address = address & EMAC_RX_ADDRESS;
271  //Clear status field
272  rxBufferDesc[i].status = 0;
273  }
274 
275  //Mark the last descriptor entry with the wrap flag
276  rxBufferDesc[i - 1].address |= EMAC_RX_WRAP;
277  //Initialize RX buffer index
278  rxBufferIndex = 0;
279 
280  //Start location of the TX descriptor list
281  EMAC1_REGS->EMAC_TBQP = (uint32_t) txBufferDesc;
282  //Start location of the RX descriptor list
283  EMAC1_REGS->EMAC_RBQP = (uint32_t) rxBufferDesc;
284 }
285 
286 
287 /**
288  * @brief SAM9X6 Ethernet MAC timer handler
289  *
290  * This routine is periodically called by the TCP/IP stack to handle periodic
291  * operations such as polling the link state
292  *
293  * @param[in] interface Underlying network interface
294  **/
295 
296 void sam9x6Eth2Tick(NetInterface *interface)
297 {
298  //Valid Ethernet PHY or switch driver?
299  if(interface->phyDriver != NULL)
300  {
301  //Handle periodic operations
302  interface->phyDriver->tick(interface);
303  }
304  else if(interface->switchDriver != NULL)
305  {
306  //Handle periodic operations
307  interface->switchDriver->tick(interface);
308  }
309  else
310  {
311  //Just for sanity
312  }
313 }
314 
315 
316 /**
317  * @brief Enable interrupts
318  * @param[in] interface Underlying network interface
319  **/
320 
322 {
323  //Enable Ethernet MAC interrupts
324  AIC_REGS->AIC_SSR = AIC_SSR_INTSEL(ID_EMAC1);
325  AIC_REGS->AIC_IECR = AIC_IECR_INTEN_Msk;
326 
327  //Valid Ethernet PHY or switch driver?
328  if(interface->phyDriver != NULL)
329  {
330  //Enable Ethernet PHY interrupts
331  interface->phyDriver->enableIrq(interface);
332  }
333  else if(interface->switchDriver != NULL)
334  {
335  //Enable Ethernet switch interrupts
336  interface->switchDriver->enableIrq(interface);
337  }
338  else
339  {
340  //Just for sanity
341  }
342 }
343 
344 
345 /**
346  * @brief Disable interrupts
347  * @param[in] interface Underlying network interface
348  **/
349 
351 {
352  //Disable Ethernet MAC interrupts
353  AIC_REGS->AIC_SSR = AIC_SSR_INTSEL(ID_EMAC1);
354  AIC_REGS->AIC_IDCR = AIC_IDCR_INTD_Msk;
355 
356  //Valid Ethernet PHY or switch driver?
357  if(interface->phyDriver != NULL)
358  {
359  //Disable Ethernet PHY interrupts
360  interface->phyDriver->disableIrq(interface);
361  }
362  else if(interface->switchDriver != NULL)
363  {
364  //Disable Ethernet switch interrupts
365  interface->switchDriver->disableIrq(interface);
366  }
367  else
368  {
369  //Just for sanity
370  }
371 }
372 
373 
374 /**
375  * @brief SAM9X6 Ethernet MAC interrupt service routine
376  **/
377 
379 {
380  bool_t flag;
381  volatile uint32_t isr;
382  volatile uint32_t tsr;
383  volatile uint32_t rsr;
384 
385  //Interrupt service routine prologue
386  osEnterIsr();
387 
388  //This flag will be set if a higher priority task must be woken
389  flag = FALSE;
390 
391  //Each time the software reads EMAC_ISR, it has to check the contents
392  //of EMAC_TSR, EMAC_RSR and EMAC_NSR
393  isr = EMAC1_REGS->EMAC_ISR;
394  tsr = EMAC1_REGS->EMAC_TSR;
395  rsr = EMAC1_REGS->EMAC_RSR;
396  (void) isr;
397 
398  //Packet transmitted?
399  if((tsr & (EMAC_TSR_UND_Msk | EMAC_TSR_COMP_Msk | EMAC_TSR_BEX_Msk |
400  EMAC_TSR_TGO_Msk | EMAC_TSR_RLES_Msk | EMAC_TSR_COL_Msk |
401  EMAC_TSR_UBR_Msk)) != 0)
402  {
403  //Only clear TSR flags that are currently set
404  EMAC1_REGS->EMAC_TSR = tsr;
405 
406  //Check whether the TX buffer is available for writing
407  if((txBufferDesc[txBufferIndex].status & EMAC_TX_USED) != 0)
408  {
409  //Notify the TCP/IP stack that the transmitter is ready to send
410  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
411  }
412  }
413 
414  //Packet received?
415  if((rsr & (EMAC_RSR_OVR_Msk | EMAC_RSR_REC_Msk | EMAC_RSR_BNA_Msk)) != 0)
416  {
417  //Set event flag
418  nicDriverInterface->nicEvent = TRUE;
419  //Notify the TCP/IP stack of the event
420  flag |= osSetEventFromIsr(&netEvent);
421  }
422 
423 #if (NET_RTOS_SUPPORT == DISABLED)
424  //Write AIC_EOICR register before exiting
425  AIC_REGS->AIC_EOICR = 0;
426 #endif
427 
428  //Interrupt service routine epilogue
429  osExitIsr(flag);
430 }
431 
432 
433 /**
434  * @brief SAM9X6 Ethernet MAC event handler
435  * @param[in] interface Underlying network interface
436  **/
437 
439 {
440  error_t error;
441  uint32_t rsr;
442 
443  //Read receive status
444  rsr = EMAC1_REGS->EMAC_RSR;
445 
446  //Packet received?
447  if((rsr & (EMAC_RSR_OVR_Msk | EMAC_RSR_REC_Msk | EMAC_RSR_BNA_Msk)) != 0)
448  {
449  //Only clear RSR flags that are currently set
450  EMAC1_REGS->EMAC_RSR = rsr;
451 
452  //Process all pending packets
453  do
454  {
455  //Read incoming packet
456  error = sam9x6Eth2ReceivePacket(interface);
457 
458  //No more data in the receive buffer?
459  } while(error != ERROR_BUFFER_EMPTY);
460  }
461 }
462 
463 
464 /**
465  * @brief Send a packet
466  * @param[in] interface Underlying network interface
467  * @param[in] buffer Multi-part buffer containing the data to send
468  * @param[in] offset Offset to the first data byte
469  * @param[in] ancillary Additional options passed to the stack along with
470  * the packet
471  * @return Error code
472  **/
473 
475  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
476 {
477  size_t length;
478 
479  //Retrieve the length of the packet
480  length = netBufferGetLength(buffer) - offset;
481 
482  //Check the frame length
484  {
485  //The transmitter can accept another packet
486  osSetEvent(&interface->nicTxEvent);
487  //Report an error
488  return ERROR_INVALID_LENGTH;
489  }
490 
491  //Make sure the current buffer is available for writing
492  if((txBufferDesc[txBufferIndex].status & EMAC_TX_USED) == 0)
493  {
494  return ERROR_FAILURE;
495  }
496 
497  //Copy user data to the transmit buffer
498  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
499 
500  //Set the necessary flags in the descriptor entry
501  if(txBufferIndex < (SAM9X6_ETH2_TX_BUFFER_COUNT - 1))
502  {
503  //Write the status word
504  txBufferDesc[txBufferIndex].status = EMAC_TX_LAST |
506 
507  //Point to the next buffer
508  txBufferIndex++;
509  }
510  else
511  {
512  //Write the status word
513  txBufferDesc[txBufferIndex].status = EMAC_TX_WRAP | EMAC_TX_LAST |
515 
516  //Wrap around
517  txBufferIndex = 0;
518  }
519 
520  //Set the TSTART bit to initiate transmission
521  EMAC1_REGS->EMAC_NCR |= EMAC_NCR_TSTART_Msk;
522 
523  //Check whether the next buffer is available for writing
524  if((txBufferDesc[txBufferIndex].status & EMAC_TX_USED) != 0)
525  {
526  //The transmitter can accept another packet
527  osSetEvent(&interface->nicTxEvent);
528  }
529 
530  //Successful processing
531  return NO_ERROR;
532 }
533 
534 
535 /**
536  * @brief Receive a packet
537  * @param[in] interface Underlying network interface
538  * @return Error code
539  **/
540 
542 {
543  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
544  error_t error;
545  uint_t i;
546  uint_t j;
547  uint_t sofIndex;
548  uint_t eofIndex;
549  size_t n;
550  size_t size;
551  size_t length;
552 
553  //Initialize variables
554  size = 0;
555  sofIndex = UINT_MAX;
556  eofIndex = UINT_MAX;
557 
558  //Search for SOF and EOF flags
559  for(i = 0; i < SAM9X6_ETH2_RX_BUFFER_COUNT; i++)
560  {
561  //Point to the current entry
562  j = rxBufferIndex + i;
563 
564  //Wrap around to the beginning of the buffer if necessary
566  {
568  }
569 
570  //No more entries to process?
571  if((rxBufferDesc[j].address & EMAC_RX_OWNERSHIP) == 0)
572  {
573  //Stop processing
574  break;
575  }
576 
577  //A valid SOF has been found?
578  if((rxBufferDesc[j].status & EMAC_RX_SOF) != 0)
579  {
580  //Save the position of the SOF
581  sofIndex = i;
582  }
583 
584  //A valid EOF has been found?
585  if((rxBufferDesc[j].status & EMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
586  {
587  //Save the position of the EOF
588  eofIndex = i;
589  //Retrieve the length of the frame
590  size = rxBufferDesc[j].status & EMAC_RX_LENGTH;
591  //Limit the number of data to read
592  size = MIN(size, ETH_MAX_FRAME_SIZE);
593  //Stop processing since we have reached the end of the frame
594  break;
595  }
596  }
597 
598  //Determine the number of entries to process
599  if(eofIndex != UINT_MAX)
600  {
601  j = eofIndex + 1;
602  }
603  else if(sofIndex != UINT_MAX)
604  {
605  j = sofIndex;
606  }
607  else
608  {
609  j = i;
610  }
611 
612  //Total number of bytes that have been copied from the receive buffer
613  length = 0;
614 
615  //Process incoming frame
616  for(i = 0; i < j; i++)
617  {
618  //Any data to copy from current buffer?
619  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
620  {
621  //Calculate the number of bytes to read at a time
623  //Copy data from receive buffer
624  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
625  //Update byte counters
626  length += n;
627  size -= n;
628  }
629 
630  //Mark the current buffer as free
631  rxBufferDesc[rxBufferIndex].address &= ~EMAC_RX_OWNERSHIP;
632 
633  //Point to the following entry
634  rxBufferIndex++;
635 
636  //Wrap around to the beginning of the buffer if necessary
637  if(rxBufferIndex >= SAM9X6_ETH2_RX_BUFFER_COUNT)
638  {
639  rxBufferIndex = 0;
640  }
641  }
642 
643  //Any packet to process?
644  if(length > 0)
645  {
646  NetRxAncillary ancillary;
647 
648  //Additional options can be passed to the stack along with the packet
649  ancillary = NET_DEFAULT_RX_ANCILLARY;
650 
651  //Pass the packet to the upper layer
652  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
653  //Valid packet received
654  error = NO_ERROR;
655  }
656  else
657  {
658  //No more data in the receive buffer
659  error = ERROR_BUFFER_EMPTY;
660  }
661 
662  //Return status code
663  return error;
664 }
665 
666 
667 /**
668  * @brief Configure MAC address filtering
669  * @param[in] interface Underlying network interface
670  * @return Error code
671  **/
672 
674 {
675  uint_t i;
676  uint_t j;
677  uint_t k;
678  uint8_t *p;
679  uint32_t hashTable[2];
680  MacAddr unicastMacAddr[3];
681  MacFilterEntry *entry;
682 
683  //Debug message
684  TRACE_DEBUG("Updating MAC filter...\r\n");
685 
686  //Set the MAC address of the station
687  EMAC1_REGS->EMAC_SA[0].EMAC_SAxB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
688  EMAC1_REGS->EMAC_SA[0].EMAC_SAxT = interface->macAddr.w[2];
689 
690  //The MAC supports 3 additional addresses for unicast perfect filtering
691  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
692  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
693  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
694 
695  //The hash table is used for multicast address filtering
696  hashTable[0] = 0;
697  hashTable[1] = 0;
698 
699  //The MAC address filter contains the list of MAC addresses to accept
700  //when receiving an Ethernet frame
701  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
702  {
703  //Point to the current entry
704  entry = &interface->macAddrFilter[i];
705 
706  //Valid entry?
707  if(entry->refCount > 0)
708  {
709  //Multicast address?
710  if(macIsMulticastAddr(&entry->addr))
711  {
712  //Point to the MAC address
713  p = entry->addr.b;
714 
715  //Apply the hash function
716  k = (p[0] >> 6) ^ p[0];
717  k ^= (p[1] >> 4) ^ (p[1] << 2);
718  k ^= (p[2] >> 2) ^ (p[2] << 4);
719  k ^= (p[3] >> 6) ^ p[3];
720  k ^= (p[4] >> 4) ^ (p[4] << 2);
721  k ^= (p[5] >> 2) ^ (p[5] << 4);
722 
723  //The hash value is reduced to a 6-bit index
724  k &= 0x3F;
725 
726  //Update hash table contents
727  hashTable[k / 32] |= (1 << (k % 32));
728  }
729  else
730  {
731  //Up to 3 additional MAC addresses can be specified
732  if(j < 3)
733  {
734  //Save the unicast address
735  unicastMacAddr[j++] = entry->addr;
736  }
737  }
738  }
739  }
740 
741  //Configure the first unicast address filter
742  if(j >= 1)
743  {
744  //The address is activated when SAH register is written
745  EMAC1_REGS->EMAC_SA[1].EMAC_SAxB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
746  EMAC1_REGS->EMAC_SA[1].EMAC_SAxT = unicastMacAddr[0].w[2];
747  }
748  else
749  {
750  //The address is deactivated when SAL register is written
751  EMAC1_REGS->EMAC_SA[1].EMAC_SAxB = 0;
752  }
753 
754  //Configure the second unicast address filter
755  if(j >= 2)
756  {
757  //The address is activated when SAH register is written
758  EMAC1_REGS->EMAC_SA[2].EMAC_SAxB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
759  EMAC1_REGS->EMAC_SA[2].EMAC_SAxT = unicastMacAddr[1].w[2];
760  }
761  else
762  {
763  //The address is deactivated when SAL register is written
764  EMAC1_REGS->EMAC_SA[2].EMAC_SAxB = 0;
765  }
766 
767  //Configure the third unicast address filter
768  if(j >= 3)
769  {
770  //The address is activated when SAH register is written
771  EMAC1_REGS->EMAC_SA[3].EMAC_SAxB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
772  EMAC1_REGS->EMAC_SA[3].EMAC_SAxT = unicastMacAddr[2].w[2];
773  }
774  else
775  {
776  //The address is deactivated when SAL register is written
777  EMAC1_REGS->EMAC_SA[3].EMAC_SAxB = 0;
778  }
779 
780  //Configure the multicast hash table
781  EMAC1_REGS->EMAC_HRB = hashTable[0];
782  EMAC1_REGS->EMAC_HRT = hashTable[1];
783 
784  //Debug message
785  TRACE_DEBUG(" HRB = %08" PRIX32 "\r\n", EMAC1_REGS->EMAC_HRB);
786  TRACE_DEBUG(" HRT = %08" PRIX32 "\r\n", EMAC1_REGS->EMAC_HRT);
787 
788  //Successful processing
789  return NO_ERROR;
790 }
791 
792 
793 /**
794  * @brief Adjust MAC configuration parameters for proper operation
795  * @param[in] interface Underlying network interface
796  * @return Error code
797  **/
798 
800 {
801  uint32_t config;
802 
803  //Read network configuration register
804  config = EMAC1_REGS->EMAC_NCFGR;
805 
806  //10BASE-T or 100BASE-TX operation mode?
807  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
808  {
809  config |= EMAC_NCFGR_SPD_Msk;
810  }
811  else
812  {
813  config &= ~EMAC_NCFGR_SPD_Msk;
814  }
815 
816  //Half-duplex or full-duplex mode?
817  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
818  {
819  config |= EMAC_NCFGR_FD_Msk;
820  }
821  else
822  {
823  config &= ~EMAC_NCFGR_FD_Msk;
824  }
825 
826  //Write configuration value back to NCFGR register
827  EMAC1_REGS->EMAC_NCFGR = config;
828 
829  //Successful processing
830  return NO_ERROR;
831 }
832 
833 
834 /**
835  * @brief Write PHY register
836  * @param[in] opcode Access type (2 bits)
837  * @param[in] phyAddr PHY address (5 bits)
838  * @param[in] regAddr Register address (5 bits)
839  * @param[in] data Register value
840  **/
841 
842 void sam9x6Eth2WritePhyReg(uint8_t opcode, uint8_t phyAddr,
843  uint8_t regAddr, uint16_t data)
844 {
845  uint32_t temp;
846 
847  //Valid opcode?
848  if(opcode == SMI_OPCODE_WRITE)
849  {
850  //Set up a write operation
851  temp = EMAC_MAN_SOF(1) | EMAC_MAN_RW(1) | EMAC_MAN_CODE(2);
852  //PHY address
853  temp |= EMAC_MAN_PHYA(phyAddr);
854  //Register address
855  temp |= EMAC_MAN_REGA(regAddr);
856  //Register value
857  temp |= EMAC_MAN_DATA(data);
858 
859  //Start a write operation
860  EMAC1_REGS->EMAC_MAN = temp;
861  //Wait for the write to complete
862  while((EMAC1_REGS->EMAC_NSR & EMAC_NSR_IDLE_Msk) == 0)
863  {
864  }
865  }
866  else
867  {
868  //The MAC peripheral only supports standard Clause 22 opcodes
869  }
870 }
871 
872 
873 /**
874  * @brief Read PHY register
875  * @param[in] opcode Access type (2 bits)
876  * @param[in] phyAddr PHY address (5 bits)
877  * @param[in] regAddr Register address (5 bits)
878  * @return Register value
879  **/
880 
881 uint16_t sam9x6Eth2ReadPhyReg(uint8_t opcode, uint8_t phyAddr,
882  uint8_t regAddr)
883 {
884  uint16_t data;
885  uint32_t temp;
886 
887  //Valid opcode?
888  if(opcode == SMI_OPCODE_READ)
889  {
890  //Set up a read operation
891  temp = EMAC_MAN_SOF(1) | EMAC_MAN_RW(2) | EMAC_MAN_CODE(2);
892  //PHY address
893  temp |= EMAC_MAN_PHYA(phyAddr);
894  //Register address
895  temp |= EMAC_MAN_REGA(regAddr);
896 
897  //Start a read operation
898  EMAC1_REGS->EMAC_MAN = temp;
899  //Wait for the read to complete
900  while((EMAC1_REGS->EMAC_NSR & EMAC_NSR_IDLE_Msk) == 0)
901  {
902  }
903 
904  //Get register value
905  data = EMAC1_REGS->EMAC_MAN & EMAC_MAN_DATA_Msk;
906  }
907  else
908  {
909  //The MAC peripheral only supports standard Clause 22 opcodes
910  data = 0;
911  }
912 
913  //Return the value of the PHY register
914  return data;
915 }
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
uint16_t sam9x6Eth2ReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
void sam9x6Eth2InitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
int bool_t
Definition: compiler_port.h:53
#define netEvent
Definition: net_legacy.h:196
#define SAM9X6_ETH2_IRQ_PRIORITY
@ 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
Transmit buffer descriptor.
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define EMAC_RX_WRAP
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:222
#define ETH_MAX_FRAME_SIZE
Definition: ethernet.h:110
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
#define EMAC_RX_EOF
__weak_func void sam9x6Eth2InitGpio(NetInterface *interface)
GPIO configuration.
#define EMAC_RX_OWNERSHIP
Receive buffer descriptor.
void sam9x6Eth2Tick(NetInterface *interface)
SAM9X6 Ethernet MAC timer handler.
#define SAM9X6_ETH2_TX_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
#define osExitIsr(flag)
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define EMAC_RX_LENGTH
#define SAM9X6_ETH2_RX_BUFFER_SIZE
#define FALSE
Definition: os_port.h:46
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define EMAC_TX_LENGTH
#define SAM9X6_ETH2_RX_BUFFER_COUNT
error_t
Error codes.
Definition: error.h:43
#define SAM9X6_ETH2_TX_BUFFER_SIZE
void sam9x6Eth2EnableIrq(NetInterface *interface)
Enable interrupts.
void sam9x6Eth2EventHandler(NetInterface *interface)
SAM9X6 Ethernet MAC event handler.
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:104
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
error_t sam9x6Eth2ReceivePacket(NetInterface *interface)
Receive a packet.
#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 sam9x6Eth2SendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
#define NetTxAncillary
Definition: net_misc.h:36
#define SAM9X6_ETH2_RAM_SECTION
#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
MacAddr
Definition: ethernet.h:195
error_t sam9x6Eth2Init(NetInterface *interface)
SAM9X6 Ethernet MAC initialization.
#define TRACE_DEBUG(...)
Definition: debug.h:107
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
Ipv6Addr address[]
Definition: ipv6.h:325
error_t sam9x6Eth2UpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define osEnterIsr()
const NicDriver sam9x6Eth2Driver
SAM9X6 Ethernet MAC driver (EMAC1 instance)
#define EMAC_RX_ADDRESS
#define EMAC_TX_WRAP
#define EMAC_RX_SOF
void sam9x6Eth2IrqHandler(void)
SAM9X6 Ethernet MAC interrupt service routine.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
SAM9X60 Ethernet MAC driver (EMAC1 instance)
void sam9x6Eth2DisableIrq(NetInterface *interface)
Disable interrupts.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
NIC driver.
Definition: nic.h:286
error_t sam9x6Eth2UpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
@ NO_ERROR
Success.
Definition: error.h:44
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
Debugging facilities.
void sam9x6Eth2WritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
#define EMAC_TX_LAST
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83