sam7x_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file sam7x_eth_driver.c
3  * @brief AT91SAM7X Ethernet MAC driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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.5.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 "at91sam7x256.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 Sam7xTxBufferDesc txBufferDesc[SAM7X_ETH_TX_BUFFER_COUNT];
56 //RX buffer descriptors
57 #pragma data_alignment = 4
58 static Sam7xRxBufferDesc rxBufferDesc[SAM7X_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 SAM7X Ethernet MAC driver
86  **/
87 
89 {
91  ETH_MTU,
102  TRUE,
103  TRUE,
104  TRUE,
105  FALSE
106 };
107 
108 
109 /**
110  * @brief SAM7X 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 SAM7X Ethernet MAC...\r\n");
122 
123  //Save underlying network interface
124  nicDriverInterface = interface;
125 
126  //Enable EMAC peripheral clock
127  AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_EMAC);
128 
129  //Disable transmit and receive circuits
130  AT91C_BASE_EMAC->EMAC_NCR = 0;
131 
132  //GPIO configuration
133  sam7xEthInitGpio(interface);
134 
135  //Configure MDC clock speed
136  AT91C_BASE_EMAC->EMAC_NCFGR = AT91C_EMAC_CLK_HCLK_32;
137  //Enable management port (MDC and MDIO)
138  AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_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  AT91C_BASE_EMAC->EMAC_SA1L = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
165  AT91C_BASE_EMAC->EMAC_SA1H = interface->macAddr.w[2];
166 
167  //The MAC supports 3 additional addresses for unicast perfect filtering
168  AT91C_BASE_EMAC->EMAC_SA2L = 0;
169  AT91C_BASE_EMAC->EMAC_SA3L = 0;
170  AT91C_BASE_EMAC->EMAC_SA4L = 0;
171 
172  //Initialize hash table
173  AT91C_BASE_EMAC->EMAC_HRB = 0;
174  AT91C_BASE_EMAC->EMAC_HRT = 0;
175 
176  //Configure the receive filter
177  AT91C_BASE_EMAC->EMAC_NCFGR |= AT91C_EMAC_BIG | AT91C_EMAC_MTI;
178 
179  //Initialize buffer descriptors
180  sam7xEthInitBufferDesc(interface);
181 
182  //Clear transmit status register
183  AT91C_BASE_EMAC->EMAC_TSR = AT91C_EMAC_UND | AT91C_EMAC_COMP |
184  AT91C_EMAC_BEX | AT91C_EMAC_TGO | AT91C_EMAC_RLES | AT91C_EMAC_COL |
185  AT91C_EMAC_UBR;
186 
187  //Clear receive status register
188  AT91C_BASE_EMAC->EMAC_RSR = AT91C_EMAC_OVR | AT91C_EMAC_REC |
189  AT91C_EMAC_BNA;
190 
191  //First disable all EMAC interrupts
192  AT91C_BASE_EMAC->EMAC_IDR = 0xFFFFFFFF;
193 
194  //Only the desired ones are enabled
195  AT91C_BASE_EMAC->EMAC_IER = AT91C_EMAC_ROVR | AT91C_EMAC_TCOMP |
196  AT91C_EMAC_TXERR | AT91C_EMAC_RLEX | AT91C_EMAC_TUNDR |
197  AT91C_EMAC_RXUBR | AT91C_EMAC_RCOMP;
198 
199  //Read EMAC_ISR register to clear any pending interrupt
200  status = AT91C_BASE_EMAC->EMAC_ISR;
201  (void) status;
202 
203  //Configure interrupt controller
204  AT91C_BASE_AIC->AIC_SMR[AT91C_ID_EMAC] = AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL | AT91C_AIC_PRIOR_LOWEST;
205  AT91C_BASE_AIC->AIC_SVR[AT91C_ID_EMAC] = (uint32_t) emacIrqWrapper;
206 
207  //Clear EMAC interrupt flag
208  AT91C_BASE_AIC->AIC_ICCR = (1 << AT91C_ID_EMAC);
209 
210  //Enable the EMAC to transmit and receive data
211  AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TE | AT91C_EMAC_RE;
212 
213  //Accept any packets from the upper layer
214  osSetEvent(&interface->nicTxEvent);
215 
216  //Successful initialization
217  return NO_ERROR;
218 }
219 
220 
221 /**
222  * @brief GPIO configuration
223  * @param[in] interface Underlying network interface
224  **/
225 
226 __weak_func void sam7xEthInitGpio(NetInterface *interface)
227 {
228 //SAM7-EX256 evaluation board?
229 #if defined(USE_SAM7_EX256)
230  uint32_t mask;
231 
232  //Enable PIO peripheral clock
233  AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_PIOB);
234 
235  //Configure MII pins
236  mask = AT91C_PB17_ERXCK | AT91C_PB16_ECOL | AT91C_PB15_ERXDV_ECRSDV |
237  AT91C_PB14_ERX3 | AT91C_PB13_ERX2 | AT91C_PB12_ETXER | AT91C_PB11_ETX3 |
238  AT91C_PB10_ETX2 | AT91C_PB9_EMDIO | AT91C_PB8_EMDC | AT91C_PB7_ERXER |
239  AT91C_PB6_ERX1 | AT91C_PB5_ERX0 | AT91C_PB4_ECRS | AT91C_PB3_ETX1 |
240  AT91C_PB2_ETX0 | AT91C_PB1_ETXEN | AT91C_PB0_ETXCK_EREFCK;
241 
242  //Disable pull-up resistors on MII pins
243  AT91C_BASE_PIOB->PIO_PPUDR = mask;
244  //Disable interrupts-on-change
245  AT91C_BASE_PIOB->PIO_IDR = mask;
246  //Assign MII pins to peripheral A function
247  AT91C_BASE_PIOB->PIO_ASR = mask;
248  //Disable the PIO from controlling the corresponding pins
249  AT91C_BASE_PIOB->PIO_PDR = mask;
250 
251  //Select MII operation mode and enable transceiver clock
252  AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_CLKEN;
253 #endif
254 }
255 
256 
257 /**
258  * @brief Initialize buffer descriptors
259  * @param[in] interface Underlying network interface
260  **/
261 
263 {
264  uint_t i;
265  uint32_t address;
266 
267  //Initialize TX buffer descriptors
268  for(i = 0; i < SAM7X_ETH_TX_BUFFER_COUNT; i++)
269  {
270  //Calculate the address of the current TX buffer
271  address = (uint32_t) txBuffer[i];
272  //Write the address to the descriptor entry
273  txBufferDesc[i].address = address;
274  //Initialize status field
275  txBufferDesc[i].status = AT91C_EMAC_TX_USED;
276  }
277 
278  //Mark the last descriptor entry with the wrap flag
279  txBufferDesc[i - 1].status |= AT91C_EMAC_TX_WRAP;
280  //Initialize TX buffer index
281  txBufferIndex = 0;
282 
283  //Initialize RX buffer descriptors
284  for(i = 0; i < SAM7X_ETH_RX_BUFFER_COUNT; i++)
285  {
286  //Calculate the address of the current RX buffer
287  address = (uint32_t) rxBuffer[i];
288  //Write the address to the descriptor entry
289  rxBufferDesc[i].address = address & AT91C_EMAC_RX_ADDRESS;
290  //Clear status field
291  rxBufferDesc[i].status = 0;
292  }
293 
294  //Mark the last descriptor entry with the wrap flag
295  rxBufferDesc[i - 1].address |= AT91C_EMAC_RX_WRAP;
296  //Initialize RX buffer index
297  rxBufferIndex = 0;
298 
299  //Start location of the TX descriptor list
300  AT91C_BASE_EMAC->EMAC_TBQP = (uint32_t) txBufferDesc;
301  //Start location of the RX descriptor list
302  AT91C_BASE_EMAC->EMAC_RBQP = (uint32_t) rxBufferDesc;
303 }
304 
305 
306 /**
307  * @brief SAM7X Ethernet MAC timer handler
308  *
309  * This routine is periodically called by the TCP/IP stack to handle periodic
310  * operations such as polling the link state
311  *
312  * @param[in] interface Underlying network interface
313  **/
314 
315 void sam7xEthTick(NetInterface *interface)
316 {
317  //Valid Ethernet PHY or switch driver?
318  if(interface->phyDriver != NULL)
319  {
320  //Handle periodic operations
321  interface->phyDriver->tick(interface);
322  }
323  else if(interface->switchDriver != NULL)
324  {
325  //Handle periodic operations
326  interface->switchDriver->tick(interface);
327  }
328  else
329  {
330  //Just for sanity
331  }
332 }
333 
334 
335 /**
336  * @brief Enable interrupts
337  * @param[in] interface Underlying network interface
338  **/
339 
341 {
342  //Enable Ethernet MAC interrupts
343  AT91C_BASE_AIC->AIC_IECR = (1 << AT91C_ID_EMAC);
344 
345  //Valid Ethernet PHY or switch driver?
346  if(interface->phyDriver != NULL)
347  {
348  //Enable Ethernet PHY interrupts
349  interface->phyDriver->enableIrq(interface);
350  }
351  else if(interface->switchDriver != NULL)
352  {
353  //Enable Ethernet switch interrupts
354  interface->switchDriver->enableIrq(interface);
355  }
356  else
357  {
358  //Just for sanity
359  }
360 }
361 
362 
363 /**
364  * @brief Disable interrupts
365  * @param[in] interface Underlying network interface
366  **/
367 
369 {
370  //Disable Ethernet MAC interrupts
371  AT91C_BASE_AIC->AIC_IDCR = (1 << AT91C_ID_EMAC);
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 SAM7X 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 of
409  //EMAC_TSR, EMAC_RSR and EMAC_NSR (see SAM7X errata 41.3.3.2)
410  isr = AT91C_BASE_EMAC->EMAC_ISR;
411  tsr = AT91C_BASE_EMAC->EMAC_TSR;
412  rsr = AT91C_BASE_EMAC->EMAC_RSR;
413  (void) isr;
414 
415  //Packet transmitted?
416  if((tsr & (AT91C_EMAC_UND | AT91C_EMAC_COMP | AT91C_EMAC_BEX |
417  AT91C_EMAC_TGO | AT91C_EMAC_RLES | AT91C_EMAC_COL | AT91C_EMAC_UBR)) != 0)
418  {
419  //Only clear TSR flags that are currently set
420  AT91C_BASE_EMAC->EMAC_TSR = tsr;
421 
422  //Check whether the TX buffer is available for writing
423  if((txBufferDesc[txBufferIndex].status & AT91C_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 & (AT91C_EMAC_OVR | AT91C_EMAC_REC | AT91C_EMAC_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  //Write AIC_EOICR register before exiting
440  AT91C_BASE_AIC->AIC_EOICR = 0;
441 
442  //Interrupt service routine epilogue
443  osExitIsr(flag);
444 }
445 
446 
447 /**
448  * @brief SAM7X Ethernet MAC event handler
449  * @param[in] interface Underlying network interface
450  **/
451 
453 {
454  error_t error;
455  uint32_t rsr;
456 
457  //Read receive status
458  rsr = AT91C_BASE_EMAC->EMAC_RSR;
459 
460  //Packet received?
461  if((rsr & (AT91C_EMAC_OVR | AT91C_EMAC_REC | AT91C_EMAC_BNA)) != 0)
462  {
463  //Only clear RSR flags that are currently set
464  AT91C_BASE_EMAC->EMAC_RSR = rsr;
465 
466  //Process all pending packets
467  do
468  {
469  //Read incoming packet
470  error = sam7xEthReceivePacket(interface);
471 
472  //No more data in the receive buffer?
473  } while(error != ERROR_BUFFER_EMPTY);
474  }
475 }
476 
477 
478 /**
479  * @brief Send a packet
480  * @param[in] interface Underlying network interface
481  * @param[in] buffer Multi-part buffer containing the data to send
482  * @param[in] offset Offset to the first data byte
483  * @param[in] ancillary Additional options passed to the stack along with
484  * the packet
485  * @return Error code
486  **/
487 
489  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
490 {
491  size_t length;
492 
493  //Retrieve the length of the packet
494  length = netBufferGetLength(buffer) - offset;
495 
496  //Check the frame length
498  {
499  //The transmitter can accept another packet
500  osSetEvent(&interface->nicTxEvent);
501  //Report an error
502  return ERROR_INVALID_LENGTH;
503  }
504 
505  //Make sure the current buffer is available for writing
506  if((txBufferDesc[txBufferIndex].status & AT91C_EMAC_TX_USED) == 0)
507  {
508  return ERROR_FAILURE;
509  }
510 
511  //Copy user data to the transmit buffer
512  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
513 
514  //Set the necessary flags in the descriptor entry
515  if(txBufferIndex < (SAM7X_ETH_TX_BUFFER_COUNT - 1))
516  {
517  //Write the status word
518  txBufferDesc[txBufferIndex].status = AT91C_EMAC_TX_LAST |
520 
521  //Point to the next buffer
522  txBufferIndex++;
523  }
524  else
525  {
526  //Write the status word
527  txBufferDesc[txBufferIndex].status = AT91C_EMAC_TX_WRAP |
529 
530  //Wrap around
531  txBufferIndex = 0;
532  }
533 
534  //Set the TSTART bit to initiate transmission
535  AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TSTART;
536 
537  //Check whether the next buffer is available for writing
538  if((txBufferDesc[txBufferIndex].status & AT91C_EMAC_TX_USED) != 0)
539  {
540  //The transmitter can accept another packet
541  osSetEvent(&interface->nicTxEvent);
542  }
543 
544  //Successful processing
545  return NO_ERROR;
546 }
547 
548 
549 /**
550  * @brief Receive a packet
551  * @param[in] interface Underlying network interface
552  * @return Error code
553  **/
554 
556 {
557  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
558  error_t error;
559  uint_t i;
560  uint_t j;
561  uint_t sofIndex;
562  uint_t eofIndex;
563  size_t n;
564  size_t size;
565  size_t length;
566 
567  //Initialize variables
568  size = 0;
569  sofIndex = UINT_MAX;
570  eofIndex = UINT_MAX;
571 
572  //Search for SOF and EOF flags
573  for(i = 0; i < SAM7X_ETH_RX_BUFFER_COUNT; i++)
574  {
575  //Point to the current entry
576  j = rxBufferIndex + i;
577 
578  //Wrap around to the beginning of the buffer if necessary
580  {
582  }
583 
584  //No more entries to process?
585  if((rxBufferDesc[j].address & AT91C_EMAC_RX_OWNERSHIP) == 0)
586  {
587  //Stop processing
588  break;
589  }
590 
591  //A valid SOF has been found?
592  if((rxBufferDesc[j].status & AT91C_EMAC_RX_SOF) != 0)
593  {
594  //Save the position of the SOF
595  sofIndex = i;
596  }
597 
598  //A valid EOF has been found?
599  if((rxBufferDesc[j].status & AT91C_EMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
600  {
601  //Save the position of the EOF
602  eofIndex = i;
603  //Retrieve the length of the frame
604  size = rxBufferDesc[j].status & AT91C_EMAC_RX_LENGTH;
605  //Limit the number of data to read
606  size = MIN(size, ETH_MAX_FRAME_SIZE);
607  //Stop processing since we have reached the end of the frame
608  break;
609  }
610  }
611 
612  //Determine the number of entries to process
613  if(eofIndex != UINT_MAX)
614  {
615  j = eofIndex + 1;
616  }
617  else if(sofIndex != UINT_MAX)
618  {
619  j = sofIndex;
620  }
621  else
622  {
623  j = i;
624  }
625 
626  //Total number of bytes that have been copied from the receive buffer
627  length = 0;
628 
629  //Process incoming frame
630  for(i = 0; i < j; i++)
631  {
632  //Any data to copy from current buffer?
633  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
634  {
635  //Calculate the number of bytes to read at a time
636  n = MIN(size, SAM7X_ETH_RX_BUFFER_SIZE);
637  //Copy data from receive buffer
638  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
639  //Update byte counters
640  length += n;
641  size -= n;
642  }
643 
644  //Mark the current buffer as free
645  rxBufferDesc[rxBufferIndex].address &= ~AT91C_EMAC_RX_OWNERSHIP;
646 
647  //Point to the following entry
648  rxBufferIndex++;
649 
650  //Wrap around to the beginning of the buffer if necessary
651  if(rxBufferIndex >= SAM7X_ETH_RX_BUFFER_COUNT)
652  {
653  rxBufferIndex = 0;
654  }
655  }
656 
657  //Any packet to process?
658  if(length > 0)
659  {
660  NetRxAncillary ancillary;
661 
662  //Additional options can be passed to the stack along with the packet
663  ancillary = NET_DEFAULT_RX_ANCILLARY;
664 
665  //Pass the packet to the upper layer
666  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
667  //Valid packet received
668  error = NO_ERROR;
669  }
670  else
671  {
672  //No more data in the receive buffer
673  error = ERROR_BUFFER_EMPTY;
674  }
675 
676  //Return status code
677  return error;
678 }
679 
680 
681 /**
682  * @brief Configure MAC address filtering
683  * @param[in] interface Underlying network interface
684  * @return Error code
685  **/
686 
688 {
689  uint_t i;
690  uint_t j;
691  uint_t k;
692  uint8_t *p;
693  uint32_t hashTable[2];
694  MacAddr unicastMacAddr[3];
695  MacFilterEntry *entry;
696 
697  //Debug message
698  TRACE_DEBUG("Updating MAC filter...\r\n");
699 
700  //Set the MAC address of the station
701  AT91C_BASE_EMAC->EMAC_SA1L = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
702  AT91C_BASE_EMAC->EMAC_SA1H = interface->macAddr.w[2];
703 
704  //The MAC supports 3 additional addresses for unicast perfect filtering
705  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
706  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
707  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
708 
709  //The hash table is used for multicast address filtering
710  hashTable[0] = 0;
711  hashTable[1] = 0;
712 
713  //The MAC address filter contains the list of MAC addresses to accept
714  //when receiving an Ethernet frame
715  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
716  {
717  //Point to the current entry
718  entry = &interface->macAddrFilter[i];
719 
720  //Valid entry?
721  if(entry->refCount > 0)
722  {
723  //Multicast address?
724  if(macIsMulticastAddr(&entry->addr))
725  {
726  //Point to the MAC address
727  p = entry->addr.b;
728 
729  //Apply the hash function
730  k = (p[0] >> 6) ^ p[0];
731  k ^= (p[1] >> 4) ^ (p[1] << 2);
732  k ^= (p[2] >> 2) ^ (p[2] << 4);
733  k ^= (p[3] >> 6) ^ p[3];
734  k ^= (p[4] >> 4) ^ (p[4] << 2);
735  k ^= (p[5] >> 2) ^ (p[5] << 4);
736 
737  //The hash value is reduced to a 6-bit index
738  k &= 0x3F;
739 
740  //Update hash table contents
741  hashTable[k / 32] |= (1 << (k % 32));
742  }
743  else
744  {
745  //Up to 3 additional MAC addresses can be specified
746  if(j < 3)
747  {
748  //Save the unicast address
749  unicastMacAddr[j++] = entry->addr;
750  }
751  }
752  }
753  }
754 
755  //Configure the first unicast address filter
756  if(j >= 1)
757  {
758  //The address is activated when SAH register is written
759  AT91C_BASE_EMAC->EMAC_SA2L = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
760  AT91C_BASE_EMAC->EMAC_SA2H = unicastMacAddr[0].w[2];
761  }
762  else
763  {
764  //The address is deactivated when SAL register is written
765  AT91C_BASE_EMAC->EMAC_SA2L = 0;
766  }
767 
768  //Configure the second unicast address filter
769  if(j >= 2)
770  {
771  //The address is activated when SAH register is written
772  AT91C_BASE_EMAC->EMAC_SA3L = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
773  AT91C_BASE_EMAC->EMAC_SA3H = unicastMacAddr[1].w[2];
774  }
775  else
776  {
777  //The address is deactivated when SAL register is written
778  AT91C_BASE_EMAC->EMAC_SA3L = 0;
779  }
780 
781  //Configure the third unicast address filter
782  if(j >= 3)
783  {
784  //The address is activated when SAH register is written
785  AT91C_BASE_EMAC->EMAC_SA4L = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
786  AT91C_BASE_EMAC->EMAC_SA4H = unicastMacAddr[2].w[2];
787  }
788  else
789  {
790  //The address is deactivated when SAL register is written
791  AT91C_BASE_EMAC->EMAC_SA4L = 0;
792  }
793 
794  //Configure the multicast hash table
795  AT91C_BASE_EMAC->EMAC_HRB = hashTable[0];
796  AT91C_BASE_EMAC->EMAC_HRT = hashTable[1];
797 
798  //Debug message
799  TRACE_DEBUG(" HRB = %08" PRIX32 "\r\n", AT91C_BASE_EMAC->EMAC_HRB);
800  TRACE_DEBUG(" HRT = %08" PRIX32 "\r\n", AT91C_BASE_EMAC->EMAC_HRT);
801 
802  //Successful processing
803  return NO_ERROR;
804 }
805 
806 
807 /**
808  * @brief Adjust MAC configuration parameters for proper operation
809  * @param[in] interface Underlying network interface
810  * @return Error code
811  **/
812 
814 {
815  uint32_t config;
816 
817  //Read network configuration register
818  config = AT91C_BASE_EMAC->EMAC_NCFGR;
819 
820  //10BASE-T or 100BASE-TX operation mode?
821  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
822  {
823  config |= AT91C_EMAC_SPD;
824  }
825  else
826  {
827  config &= ~AT91C_EMAC_SPD;
828  }
829 
830  //Half-duplex or full-duplex mode?
831  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
832  {
833  config |= AT91C_EMAC_FD;
834  }
835  else
836  {
837  config &= ~AT91C_EMAC_FD;
838  }
839 
840  //Write configuration value back to NCFGR register
841  AT91C_BASE_EMAC->EMAC_NCFGR = config;
842 
843  //Successful processing
844  return NO_ERROR;
845 }
846 
847 
848 /**
849  * @brief Write PHY register
850  * @param[in] opcode Access type (2 bits)
851  * @param[in] phyAddr PHY address (5 bits)
852  * @param[in] regAddr Register address (5 bits)
853  * @param[in] data Register value
854  **/
855 
856 void sam7xEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
857  uint8_t regAddr, uint16_t data)
858 {
859  uint32_t temp;
860 
861  //Valid opcode?
862  if(opcode == SMI_OPCODE_WRITE)
863  {
864  //Set up a write operation
866  //PHY address
867  temp |= (phyAddr << 23) & AT91C_EMAC_PHYA;
868  //Register address
869  temp |= (regAddr << 18) & AT91C_EMAC_REGA;
870  //Register value
871  temp |= data & AT91C_EMAC_DATA;
872 
873  //Start a write operation
874  AT91C_BASE_EMAC->EMAC_MAN = temp;
875  //Wait for the write to complete
876  while((AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE) == 0)
877  {
878  }
879  }
880  else
881  {
882  //The MAC peripheral only supports standard Clause 22 opcodes
883  }
884 }
885 
886 
887 /**
888  * @brief Read PHY register
889  * @param[in] opcode Access type (2 bits)
890  * @param[in] phyAddr PHY address (5 bits)
891  * @param[in] regAddr Register address (5 bits)
892  * @return Register value
893  **/
894 
895 uint16_t sam7xEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
896  uint8_t regAddr)
897 {
898  uint16_t data;
899  uint32_t temp;
900 
901  //Valid opcode?
902  if(opcode == SMI_OPCODE_READ)
903  {
904  //Set up a read operation
906  //PHY address
907  temp |= (phyAddr << 23) & AT91C_EMAC_PHYA;
908  //Register address
909  temp |= (regAddr << 18) & AT91C_EMAC_REGA;
910 
911  //Start a read operation
912  AT91C_BASE_EMAC->EMAC_MAN = temp;
913  //Wait for the read to complete
914  while((AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE) == 0)
915  {
916  }
917 
918  //Get register value
919  data = AT91C_BASE_EMAC->EMAC_MAN & AT91C_EMAC_DATA;
920  }
921  else
922  {
923  //The MAC peripheral only supports standard Clause 22 opcodes
924  data = 0;
925  }
926 
927  //Return the value of the PHY register
928  return data;
929 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
void sam7xEthEventHandler(NetInterface *interface)
SAM7X Ethernet MAC event handler.
AT91SAM7X Ethernet MAC driver.
#define AT91C_EMAC_CODE_10
uint8_t opcode
Definition: dns_common.h:188
int bool_t
Definition: compiler_port.h:61
#define SAM7X_ETH_TX_BUFFER_COUNT
#define netEvent
Definition: net_legacy.h:196
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
size_t netBufferRead(void *dest, const NetBuffer *src, size_t srcOffset, size_t length)
Read data from a multi-part buffer.
Definition: net_mem.c:690
#define AT91C_EMAC_RX_OWNERSHIP
uint8_t p
Definition: ndp.h:300
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
#define AT91C_EMAC_RW_10
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define TRUE
Definition: os_port.h:50
#define AT91C_EMAC_RX_WRAP
uint8_t data[]
Definition: ethernet.h:222
#define ETH_MAX_FRAME_SIZE
Definition: ethernet.h:110
#define AT91C_EMAC_RX_EOF
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
void emacIrqWrapper(void)
#define SAM7X_ETH_TX_BUFFER_SIZE
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:392
#define macIsMulticastAddr(macAddr)
Definition: ethernet.h:133
#define osExitIsr(flag)
#define SAM7X_ETH_RX_BUFFER_SIZE
void sam7xEthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define AT91C_EMAC_RW_01
void sam7xEthEnableIrq(NetInterface *interface)
Enable interrupts.
#define FALSE
Definition: os_port.h:46
#define AT91C_EMAC_RX_LENGTH
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
error_t
Error codes.
Definition: error.h:43
error_t sam7xEthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
Transmit buffer descriptor.
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:105
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
error_t sam7xEthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define txBuffer
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:36
MacAddr addr
MAC address.
Definition: ethernet.h:263
__weak_func void sam7xEthInitGpio(NetInterface *interface)
GPIO configuration.
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_BUFFER_EMPTY
Definition: error.h:142
Receive buffer descriptor.
#define NetTxAncillary
Definition: net_misc.h:36
uint8_t mask
Definition: web_socket.h:319
#define SMI_OPCODE_READ
Definition: nic.h:67
#define AT91C_EMAC_TX_LENGTH
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
#define MIN(a, b)
Definition: os_port.h:63
#define rxBuffer
#define AT91C_EMAC_SOF_01
MacAddr
Definition: ethernet.h:195
#define SAM7X_ETH_RX_BUFFER_COUNT
#define AT91C_EMAC_RX_SOF
error_t sam7xEthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
#define TRACE_DEBUG(...)
Definition: debug.h:119
error_t sam7xEthInit(NetInterface *interface)
SAM7X Ethernet MAC initialization.
uint16_t regAddr
void sam7xEthTick(NetInterface *interface)
SAM7X Ethernet MAC timer handler.
void sam7xEthDisableIrq(NetInterface *interface)
Disable interrupts.
#define ETH_MTU
Definition: ethernet.h:116
uint8_t n
MAC filter table entry.
Definition: ethernet.h:262
void sam7xEthIrqHandler(void)
SAM7X Ethernet MAC interrupt service routine.
Ipv6Addr address[]
Definition: ipv6.h:325
uint16_t sam7xEthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define osEnterIsr()
error_t sam7xEthReceivePacket(NetInterface *interface)
Receive a packet.
#define AT91C_EMAC_TX_LAST
#define AT91C_EMAC_RX_ADDRESS
#define AT91C_EMAC_TX_USED
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void sam7xEthInitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
unsigned int uint_t
Definition: compiler_port.h:57
#define AT91C_BASE_EMAC
TCP/IP stack core.
const NicDriver sam7xEthDriver
SAM7X Ethernet MAC driver.
NIC driver.
Definition: nic.h:286
#define AT91C_EMAC_TX_WRAP
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
@ NO_ERROR
Success.
Definition: error.h:44
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
Debugging facilities.
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83