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