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-2026 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.6.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(&nicDriverInterface->netContext->event);
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  //Data synchronization barrier
545  __dsb(15);
546 
547  //Set the TSTART bit to initiate transmission
548  AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TSTART;
549 
550  //Check whether the next buffer is available for writing
551  if((txBufferDesc[txBufferIndex].status & AT91C_EMAC_TX_USED) != 0)
552  {
553  //The transmitter can accept another packet
554  osSetEvent(&interface->nicTxEvent);
555  }
556 
557  //Successful processing
558  return NO_ERROR;
559 }
560 
561 
562 /**
563  * @brief Receive a packet
564  * @param[in] interface Underlying network interface
565  * @return Error code
566  **/
567 
569 {
570  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
571  error_t error;
572  uint_t i;
573  uint_t j;
574  uint_t sofIndex;
575  uint_t eofIndex;
576  size_t n;
577  size_t size;
578  size_t length;
579 
580  //Initialize variables
581  size = 0;
582  sofIndex = UINT_MAX;
583  eofIndex = UINT_MAX;
584 
585  //Search for SOF and EOF flags
586  for(i = 0; i < SAM9263_ETH_RX_BUFFER_COUNT; i++)
587  {
588  //Point to the current entry
589  j = rxBufferIndex + i;
590 
591  //Wrap around to the beginning of the buffer if necessary
593  {
595  }
596 
597  //No more entries to process?
598  if((rxBufferDesc[j].address & AT91C_EMAC_RX_OWNERSHIP) == 0)
599  {
600  //Stop processing
601  break;
602  }
603 
604  //A valid SOF has been found?
605  if((rxBufferDesc[j].status & AT91C_EMAC_RX_SOF) != 0)
606  {
607  //Save the position of the SOF
608  sofIndex = i;
609  }
610 
611  //A valid EOF has been found?
612  if((rxBufferDesc[j].status & AT91C_EMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
613  {
614  //Save the position of the EOF
615  eofIndex = i;
616  //Retrieve the length of the frame
617  size = rxBufferDesc[j].status & AT91C_EMAC_RX_LENGTH;
618  //Limit the number of data to read
619  size = MIN(size, ETH_MAX_FRAME_SIZE);
620  //Stop processing since we have reached the end of the frame
621  break;
622  }
623  }
624 
625  //Determine the number of entries to process
626  if(eofIndex != UINT_MAX)
627  {
628  j = eofIndex + 1;
629  }
630  else if(sofIndex != UINT_MAX)
631  {
632  j = sofIndex;
633  }
634  else
635  {
636  j = i;
637  }
638 
639  //Total number of bytes that have been copied from the receive buffer
640  length = 0;
641 
642  //Process incoming frame
643  for(i = 0; i < j; i++)
644  {
645  //Any data to copy from current buffer?
646  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
647  {
648  //Calculate the number of bytes to read at a time
650  //Copy data from receive buffer
651  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
652  //Update byte counters
653  length += n;
654  size -= n;
655  }
656 
657  //Mark the current buffer as free
658  rxBufferDesc[rxBufferIndex].address &= ~AT91C_EMAC_RX_OWNERSHIP;
659 
660  //Point to the following entry
661  rxBufferIndex++;
662 
663  //Wrap around to the beginning of the buffer if necessary
664  if(rxBufferIndex >= SAM9263_ETH_RX_BUFFER_COUNT)
665  {
666  rxBufferIndex = 0;
667  }
668  }
669 
670  //Any packet to process?
671  if(length > 0)
672  {
673  NetRxAncillary ancillary;
674 
675  //Additional options can be passed to the stack along with the packet
676  ancillary = NET_DEFAULT_RX_ANCILLARY;
677 
678  //Pass the packet to the upper layer
679  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
680  //Valid packet received
681  error = NO_ERROR;
682  }
683  else
684  {
685  //No more data in the receive buffer
686  error = ERROR_BUFFER_EMPTY;
687  }
688 
689  //Return status code
690  return error;
691 }
692 
693 
694 /**
695  * @brief Configure MAC address filtering
696  * @param[in] interface Underlying network interface
697  * @return Error code
698  **/
699 
701 {
702  uint_t i;
703  uint_t j;
704  uint_t k;
705  uint8_t *p;
706  uint32_t hashTable[2];
707  MacAddr unicastMacAddr[3];
708  MacFilterEntry *entry;
709 
710  //Debug message
711  TRACE_DEBUG("Updating MAC filter...\r\n");
712 
713  //Set the MAC address of the station
714  AT91C_BASE_EMAC->EMAC_SA1L = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
715  AT91C_BASE_EMAC->EMAC_SA1H = interface->macAddr.w[2];
716 
717  //The MAC supports 3 additional addresses for unicast perfect filtering
718  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
719  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
720  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
721 
722  //The hash table is used for multicast address filtering
723  hashTable[0] = 0;
724  hashTable[1] = 0;
725 
726  //The MAC address filter contains the list of MAC addresses to accept
727  //when receiving an Ethernet frame
728  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
729  {
730  //Point to the current entry
731  entry = &interface->macAddrFilter[i];
732 
733  //Valid entry?
734  if(entry->refCount > 0)
735  {
736  //Multicast address?
737  if(macIsMulticastAddr(&entry->addr))
738  {
739  //Point to the MAC address
740  p = entry->addr.b;
741 
742  //Apply the hash function
743  k = (p[0] >> 6) ^ p[0];
744  k ^= (p[1] >> 4) ^ (p[1] << 2);
745  k ^= (p[2] >> 2) ^ (p[2] << 4);
746  k ^= (p[3] >> 6) ^ p[3];
747  k ^= (p[4] >> 4) ^ (p[4] << 2);
748  k ^= (p[5] >> 2) ^ (p[5] << 4);
749 
750  //The hash value is reduced to a 6-bit index
751  k &= 0x3F;
752 
753  //Update hash table contents
754  hashTable[k / 32] |= (1 << (k % 32));
755  }
756  else
757  {
758  //Up to 3 additional MAC addresses can be specified
759  if(j < 3)
760  {
761  //Save the unicast address
762  unicastMacAddr[j++] = entry->addr;
763  }
764  }
765  }
766  }
767 
768  //Configure the first unicast address filter
769  if(j >= 1)
770  {
771  //The address is activated when SAH register is written
772  AT91C_BASE_EMAC->EMAC_SA2L = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
773  AT91C_BASE_EMAC->EMAC_SA2H = unicastMacAddr[0].w[2];
774  }
775  else
776  {
777  //The address is deactivated when SAL register is written
778  AT91C_BASE_EMAC->EMAC_SA2L = 0;
779  }
780 
781  //Configure the second unicast address filter
782  if(j >= 2)
783  {
784  //The address is activated when SAH register is written
785  AT91C_BASE_EMAC->EMAC_SA3L = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
786  AT91C_BASE_EMAC->EMAC_SA3H = unicastMacAddr[1].w[2];
787  }
788  else
789  {
790  //The address is deactivated when SAL register is written
791  AT91C_BASE_EMAC->EMAC_SA3L = 0;
792  }
793 
794  //Configure the third unicast address filter
795  if(j >= 3)
796  {
797  //The address is activated when SAH register is written
798  AT91C_BASE_EMAC->EMAC_SA4L = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
799  AT91C_BASE_EMAC->EMAC_SA4H = unicastMacAddr[2].w[2];
800  }
801  else
802  {
803  //The address is deactivated when SAL register is written
804  AT91C_BASE_EMAC->EMAC_SA4L = 0;
805  }
806 
807  //Configure the multicast hash table
808  AT91C_BASE_EMAC->EMAC_HRB = hashTable[0];
809  AT91C_BASE_EMAC->EMAC_HRT = hashTable[1];
810 
811  //Debug message
812  TRACE_DEBUG(" HRB = 0x%08" PRIX32 "\r\n", AT91C_BASE_EMAC->EMAC_HRB);
813  TRACE_DEBUG(" HRT = 0x%08" PRIX32 "\r\n", AT91C_BASE_EMAC->EMAC_HRT);
814 
815  //Successful processing
816  return NO_ERROR;
817 }
818 
819 
820 /**
821  * @brief Adjust MAC configuration parameters for proper operation
822  * @param[in] interface Underlying network interface
823  * @return Error code
824  **/
825 
827 {
828  uint32_t config;
829 
830  //Read network configuration register
831  config = AT91C_BASE_EMAC->EMAC_NCFGR;
832 
833  //10BASE-T or 100BASE-TX operation mode?
834  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
835  {
836  config |= AT91C_EMAC_SPD;
837  }
838  else
839  {
840  config &= ~AT91C_EMAC_SPD;
841  }
842 
843  //Half-duplex or full-duplex mode?
844  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
845  {
846  config |= AT91C_EMAC_FD;
847  }
848  else
849  {
850  config &= ~AT91C_EMAC_FD;
851  }
852 
853  //Write configuration value back to NCFGR register
854  AT91C_BASE_EMAC->EMAC_NCFGR = config;
855 
856  //Successful processing
857  return NO_ERROR;
858 }
859 
860 
861 /**
862  * @brief Write PHY register
863  * @param[in] opcode Access type (2 bits)
864  * @param[in] phyAddr PHY address (5 bits)
865  * @param[in] regAddr Register address (5 bits)
866  * @param[in] data Register value
867  **/
868 
869 void sam9263EthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
870  uint8_t regAddr, uint16_t data)
871 {
872  uint32_t temp;
873 
874  //Valid opcode?
875  if(opcode == SMI_OPCODE_WRITE)
876  {
877  //Set up a write operation
879  //PHY address
880  temp |= (phyAddr << 23) & AT91C_EMAC_PHYA;
881  //Register address
882  temp |= (regAddr << 18) & AT91C_EMAC_REGA;
883  //Register value
884  temp |= data & AT91C_EMAC_DATA;
885 
886  //Start a write operation
887  AT91C_BASE_EMAC->EMAC_MAN = temp;
888  //Wait for the write to complete
889  while((AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE) == 0)
890  {
891  }
892  }
893  else
894  {
895  //The MAC peripheral only supports standard Clause 22 opcodes
896  }
897 }
898 
899 
900 /**
901  * @brief Read PHY register
902  * @param[in] opcode Access type (2 bits)
903  * @param[in] phyAddr PHY address (5 bits)
904  * @param[in] regAddr Register address (5 bits)
905  * @return Register value
906  **/
907 
908 uint16_t sam9263EthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
909  uint8_t regAddr)
910 {
911  uint16_t data;
912  uint32_t temp;
913 
914  //Valid opcode?
915  if(opcode == SMI_OPCODE_READ)
916  {
917  //Set up a read operation
919  //PHY address
920  temp |= (phyAddr << 23) & AT91C_EMAC_PHYA;
921  //Register address
922  temp |= (regAddr << 18) & AT91C_EMAC_REGA;
923 
924  //Start a read operation
925  AT91C_BASE_EMAC->EMAC_MAN = temp;
926  //Wait for the read to complete
927  while((AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE) == 0)
928  {
929  }
930 
931  //Get register value
932  data = AT91C_BASE_EMAC->EMAC_MAN & AT91C_EMAC_DATA;
933  }
934  else
935  {
936  //The MAC peripheral only supports standard Clause 22 opcodes
937  data = 0;
938  }
939 
940  //Return the value of the PHY register
941  return data;
942 }
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:191
int bool_t
Definition: compiler_port.h:63
void sam9263EthInitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
__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:224
#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:266
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:418
#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:103
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
#define txBuffer
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:40
MacAddr addr
MAC address.
Definition: ethernet.h:265
@ 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:197
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:264
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:345
#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:51
@ 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