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