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