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