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