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