avr32_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file avr32_eth_driver.c
3  * @brief AVR32 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 <avr32/io.h>
37 #include "interrupt.h"
38 #include "intc.h"
39 #include "core/net.h"
41 #include "debug.h"
42 
43 //Underlying network interface
44 static NetInterface *nicDriverInterface;
45 
46 //IAR EWARM compiler?
47 #if defined(__ICCARM__)
48 
49 //TX buffer
50 #pragma data_alignment = 4
52 //RX buffer
53 #pragma data_alignment = 4
55 //TX buffer descriptors
56 #pragma data_alignment = 8
57 static Avr32TxBufferDesc txBufferDesc[AVR32_ETH_TX_BUFFER_COUNT];
58 //RX buffer descriptors
59 #pragma data_alignment = 8
60 static Avr32RxBufferDesc rxBufferDesc[AVR32_ETH_RX_BUFFER_COUNT];
61 
62 //GCC compiler?
63 #else
64 
65 //TX buffer
67  __attribute__((aligned(4)));
68 //RX buffer
70  __attribute__((aligned(4)));
71 //TX buffer descriptors
73  __attribute__((aligned(8)));
74 //RX buffer descriptors
76  __attribute__((aligned(8)));
77 
78 #endif
79 
80 //TX buffer index
81 static uint_t txBufferIndex;
82 //RX buffer index
83 static uint_t rxBufferIndex;
84 
85 
86 /**
87  * @brief AVR32 Ethernet MAC driver
88  **/
89 
91 {
93  ETH_MTU,
104  TRUE,
105  TRUE,
106  TRUE,
107  FALSE
108 };
109 
110 
111 /**
112  * @brief AVR32 Ethernet MAC initialization
113  * @param[in] interface Underlying network interface
114  * @return Error code
115  **/
116 
118 {
119  error_t error;
120  volatile uint32_t status;
121 
122  //Debug message
123  TRACE_INFO("Initializing AVR32 Ethernet MAC...\r\n");
124 
125  //Save underlying network interface
126  nicDriverInterface = interface;
127 
128  //Disable transmit and receive circuits
129  AVR32_MACB.ncr = 0;
130 
131  //GPIO configuration
132  avr32EthInitGpio(interface);
133 
134  //Configure MDC clock speed
135  AVR32_MACB.ncfgr = AVR32_MACB_NCFGR_CLK_DIV64;
136  //Enable management port (MDC and MDIO)
137  AVR32_MACB.ncr |= AVR32_MACB_NCR_MPE_MASK;
138 
139  //Valid Ethernet PHY or switch driver?
140  if(interface->phyDriver != NULL)
141  {
142  //Ethernet PHY initialization
143  error = interface->phyDriver->init(interface);
144  }
145  else if(interface->switchDriver != NULL)
146  {
147  //Ethernet switch initialization
148  error = interface->switchDriver->init(interface);
149  }
150  else
151  {
152  //The interface is not properly configured
153  error = ERROR_FAILURE;
154  }
155 
156  //Any error to report?
157  if(error)
158  {
159  return error;
160  }
161 
162  //Set the MAC address of the station
163  AVR32_MACB.sa1b = interface->macAddr.b[0] |
164  (interface->macAddr.b[1] << 8) |
165  (interface->macAddr.b[2] << 16) |
166  (interface->macAddr.b[3] << 24);
167 
168  AVR32_MACB.sa1t = interface->macAddr.b[4] |
169  (interface->macAddr.b[5] << 8);
170 
171  //Initialize hash table
172  AVR32_MACB.hrb = 0;
173  AVR32_MACB.hrt = 0;
174 
175  //Configure the receive filter
176  AVR32_MACB.ncfgr |= AVR32_MACB_NCFGR_BIG_MASK | AVR32_MACB_NCFGR_UNI_MASK |
177  AVR32_MACB_NCFGR_MTI_MASK;
178 
179  //Initialize buffer descriptors
180  avr32EthInitBufferDesc(interface);
181 
182  //Clear transmit status register
183  AVR32_MACB.tsr = AVR32_MACB_TSR_UND_MASK | AVR32_MACB_TSR_COMP_MASK |
184  AVR32_MACB_TSR_BEX_MASK | AVR32_MACB_TSR_TGO_MASK |
185  AVR32_MACB_TSR_RLE_MASK | AVR32_MACB_TSR_COL_MASK |
186  AVR32_MACB_TSR_UBR_MASK;
187 
188  //Clear receive status register
189  AVR32_MACB.rsr = AVR32_MACB_RSR_OVR_MASK | AVR32_MACB_RSR_REC_MASK |
190  AVR32_MACB_RSR_BNA_MASK;
191 
192  //First disable all EMAC interrupts
193  AVR32_MACB.idr = 0xFFFFFFFF;
194 
195  //Only the desired ones are enabled
196  AVR32_MACB.ier = AVR32_MACB_IER_ROVR_MASK | AVR32_MACB_IER_TCOMP_MASK |
197  AVR32_MACB_IER_TXERR_MASK | AVR32_MACB_IER_RLE_MASK |
198  AVR32_MACB_IER_TUND_MASK | AVR32_MACB_IER_RXUBR_MASK |
199  AVR32_MACB_IER_RCOMP_MASK;
200 
201  //Read EMAC_ISR register to clear any pending interrupt
202  status = AVR32_MACB.isr;
203  (void) status;
204 
205  //Register interrupt handler
206  INTC_register_interrupt(avr32EthIrqWrapper, AVR32_MACB_IRQ,
208 
209  //Enable the EMAC to transmit and receive data
210  AVR32_MACB.ncr |= AVR32_MACB_NCR_TE_MASK | AVR32_MACB_NCR_RE_MASK;
211 
212  //Accept any packets from the upper layer
213  osSetEvent(&interface->nicTxEvent);
214 
215  //Successful initialization
216  return NO_ERROR;
217 }
218 
219 
220 /**
221  * @brief GPIO configuration
222  * @param[in] interface Underlying network interface
223  **/
224 
225 __weak_func void avr32EthInitGpio(NetInterface *interface)
226 {
227 //EVK1105 evaluation board?
228 #if defined(USE_EVK1105)
229  //Assign RMII pins to peripheral A function
230  AVR32_GPIO.port[1].pmr0c = MACB_RMII_MASK;
231  AVR32_GPIO.port[1].pmr1c =MACB_RMII_MASK;
232 
233  //Disable the PIO from controlling the corresponding pins
234  AVR32_GPIO.port[1].gperc = MACB_RMII_MASK;
235 
236  //Select RMII operation mode
237  AVR32_MACB.usrio &= ~AVR32_MACB_USRIO_RMII_MASK;
238 #endif
239 }
240 
241 
242 /**
243  * @brief Initialize buffer descriptors
244  * @param[in] interface Underlying network interface
245  **/
246 
248 {
249  uint_t i;
250  uint32_t address;
251 
252  //Initialize TX buffer descriptors
253  for(i = 0; i < AVR32_ETH_TX_BUFFER_COUNT; i++)
254  {
255  //Calculate the address of the current TX buffer
256  address = (uint32_t) txBuffer[i];
257  //Write the address to the descriptor entry
258  txBufferDesc[i].address = address;
259  //Initialize status field
260  txBufferDesc[i].status = MACB_TX_USED;
261  }
262 
263  //Mark the last descriptor entry with the wrap flag
264  txBufferDesc[i - 1].status |= MACB_TX_WRAP;
265  //Initialize TX buffer index
266  txBufferIndex = 0;
267 
268  //Initialize RX buffer descriptors
269  for(i = 0; i < AVR32_ETH_RX_BUFFER_COUNT; i++)
270  {
271  //Calculate the address of the current RX buffer
272  address = (uint32_t) rxBuffer[i];
273  //Write the address to the descriptor entry
274  rxBufferDesc[i].address = address & MACB_RX_ADDRESS;
275  //Clear status field
276  rxBufferDesc[i].status = 0;
277  }
278 
279  //Mark the last descriptor entry with the wrap flag
280  rxBufferDesc[i - 1].address |= MACB_RX_WRAP;
281  //Initialize RX buffer index
282  rxBufferIndex = 0;
283 
284  //Start location of the TX descriptor list
285  AVR32_MACB.tbqp = (uint32_t) txBufferDesc;
286  //Start location of the RX descriptor list
287  AVR32_MACB.rbqp = (uint32_t) rxBufferDesc;
288 }
289 
290 
291 /**
292  * @brief AVR32 Ethernet MAC timer handler
293  *
294  * This routine is periodically called by the TCP/IP stack to handle periodic
295  * operations such as polling the link state
296  *
297  * @param[in] interface Underlying network interface
298  **/
299 
300 void avr32EthTick(NetInterface *interface)
301 {
302  //Valid Ethernet PHY or switch driver?
303  if(interface->phyDriver != NULL)
304  {
305  //Handle periodic operations
306  interface->phyDriver->tick(interface);
307  }
308  else if(interface->switchDriver != NULL)
309  {
310  //Handle periodic operations
311  interface->switchDriver->tick(interface);
312  }
313  else
314  {
315  //Just for sanity
316  }
317 }
318 
319 
320 /**
321  * @brief Enable interrupts
322  * @param[in] interface Underlying network interface
323  **/
324 
326 {
327  //Enable Ethernet MAC interrupts
328  Enable_global_interrupt();
329 
330  //Valid Ethernet PHY or switch driver?
331  if(interface->phyDriver != NULL)
332  {
333  //Enable Ethernet PHY interrupts
334  interface->phyDriver->enableIrq(interface);
335  }
336  else if(interface->switchDriver != NULL)
337  {
338  //Enable Ethernet switch interrupts
339  interface->switchDriver->enableIrq(interface);
340  }
341  else
342  {
343  //Just for sanity
344  }
345 }
346 
347 
348 /**
349  * @brief Disable interrupts
350  * @param[in] interface Underlying network interface
351  **/
352 
354 {
355  //Disable Ethernet MAC interrupts
356  Disable_global_interrupt();
357 
358  //Valid Ethernet PHY or switch driver?
359  if(interface->phyDriver != NULL)
360  {
361  //Disable Ethernet PHY interrupts
362  interface->phyDriver->disableIrq(interface);
363  }
364  else if(interface->switchDriver != NULL)
365  {
366  //Disable Ethernet switch interrupts
367  interface->switchDriver->disableIrq(interface);
368  }
369  else
370  {
371  //Just for sanity
372  }
373 }
374 
375 
376 /**
377  * @brief AVR32 Ethernet MAC interrupt wrapper
378  **/
379 
380  __attribute__((naked)) void avr32EthIrqWrapper(void)
381 {
382  //Interrupt service routine prologue
383  osEnterIsr();
384 
385  //Call Ethernet MAC interrupt handler
387 
388  //Interrupt service routine epilogue
389  osExitIsr(flag);
390 }
391 
392 
393 /**
394  * @brief AVR32 Ethernet MAC interrupt service routine
395  * @return TRUE if a higher priority task must be woken. Else FALSE is returned
396  **/
397 
399 {
400  bool_t flag;
401  volatile uint32_t isr;
402  volatile uint32_t tsr;
403  volatile uint32_t rsr;
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
409  //of EMAC_TSR, EMAC_RSR and EMAC_NSR
410  isr = AVR32_MACB.isr;
411  tsr = AVR32_MACB.tsr;
412  rsr = AVR32_MACB.rsr;
413  (void) isr;
414 
415  //Packet transmitted?
416  if((tsr & (AVR32_MACB_TSR_UND_MASK | AVR32_MACB_TSR_COMP_MASK |
417  AVR32_MACB_TSR_BEX_MASK | AVR32_MACB_TSR_TGO_MASK | AVR32_MACB_TSR_RLE_MASK |
418  AVR32_MACB_TSR_COL_MASK | AVR32_MACB_TSR_UBR_MASK)) != 0)
419  {
420  //Only clear TSR flags that are currently set
421  AVR32_MACB.tsr = tsr;
422 
423  //Check whether the TX buffer is available for writing
424  if((txBufferDesc[txBufferIndex].status & MACB_TX_USED) != 0)
425  {
426  //Notify the TCP/IP stack that the transmitter is ready to send
427  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
428  }
429  }
430 
431  //Packet received?
432  if((rsr & (AVR32_MACB_RSR_OVR_MASK | AVR32_MACB_RSR_REC_MASK |
433  AVR32_MACB_RSR_BNA_MASK)) != 0)
434  {
435  //Set event flag
436  nicDriverInterface->nicEvent = TRUE;
437  //Notify the TCP/IP stack of the event
438  flag |= osSetEventFromIsr(&netEvent);
439  }
440 
441  //A higher priority task must be woken?
442  return flag;
443 }
444 
445 
446 /**
447  * @brief AVR32 Ethernet MAC event handler
448  * @param[in] interface Underlying network interface
449  **/
450 
452 {
453  error_t error;
454  uint32_t rsr;
455 
456  //Read receive status
457  rsr = AVR32_MACB.rsr;
458 
459  //Packet received?
460  if((rsr & (AVR32_MACB_RSR_OVR_MASK | AVR32_MACB_RSR_REC_MASK | AVR32_MACB_RSR_BNA_MASK)) != 0)
461  {
462  //Only clear RSR flags that are currently set
463  AVR32_MACB.rsr = rsr;
464 
465  //Process all pending packets
466  do
467  {
468  //Read incoming packet
469  error = avr32EthReceivePacket(interface);
470 
471  //No more data in the receive buffer?
472  } while(error != ERROR_BUFFER_EMPTY);
473  }
474 }
475 
476 
477 /**
478  * @brief Send a packet
479  * @param[in] interface Underlying network interface
480  * @param[in] buffer Multi-part buffer containing the data to send
481  * @param[in] offset Offset to the first data byte
482  * @param[in] ancillary Additional options passed to the stack along with
483  * the packet
484  * @return Error code
485  **/
486 
488  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
489 {
490  size_t length;
491 
492  //Retrieve the length of the packet
493  length = netBufferGetLength(buffer) - offset;
494 
495  //Check the frame length
497  {
498  //The transmitter can accept another packet
499  osSetEvent(&interface->nicTxEvent);
500  //Report an error
501  return ERROR_INVALID_LENGTH;
502  }
503 
504  //Make sure the current buffer is available for writing
505  if((txBufferDesc[txBufferIndex].status & MACB_TX_USED) == 0)
506  {
507  return ERROR_FAILURE;
508  }
509 
510  //Copy user data to the transmit buffer
511  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
512 
513  //Set the necessary flags in the descriptor entry
514  if(txBufferIndex < (AVR32_ETH_TX_BUFFER_COUNT - 1))
515  {
516  //Write the status word
517  txBufferDesc[txBufferIndex].status = MACB_TX_LAST |
519 
520  //Point to the next buffer
521  txBufferIndex++;
522  }
523  else
524  {
525  //Write the status word
526  txBufferDesc[txBufferIndex].status = MACB_TX_WRAP | MACB_TX_LAST |
528 
529  //Wrap around
530  txBufferIndex = 0;
531  }
532 
533  //Set the TSTART bit to initiate transmission
534  AVR32_MACB.ncr |= AVR32_MACB_NCR_TSTART_MASK;
535 
536  //Check whether the next buffer is available for writing
537  if((txBufferDesc[txBufferIndex].status & MACB_TX_USED) != 0)
538  {
539  //The transmitter can accept another packet
540  osSetEvent(&interface->nicTxEvent);
541  }
542 
543  //Successful processing
544  return NO_ERROR;
545 }
546 
547 
548 /**
549  * @brief Receive a packet
550  * @param[in] interface Underlying network interface
551  * @return Error code
552  **/
553 
555 {
556  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
557  error_t error;
558  uint_t i;
559  uint_t j;
560  uint_t sofIndex;
561  uint_t eofIndex;
562  size_t n;
563  size_t size;
564  size_t length;
565 
566  //Initialize variables
567  size = 0;
568  sofIndex = UINT_MAX;
569  eofIndex = UINT_MAX;
570 
571  //Search for SOF and EOF flags
572  for(i = 0; i < AVR32_ETH_RX_BUFFER_COUNT; i++)
573  {
574  //Point to the current entry
575  j = rxBufferIndex + i;
576 
577  //Wrap around to the beginning of the buffer if necessary
579  {
581  }
582 
583  //No more entries to process?
584  if((rxBufferDesc[j].address & MACB_RX_OWNERSHIP) == 0)
585  {
586  //Stop processing
587  break;
588  }
589 
590  //A valid SOF has been found?
591  if((rxBufferDesc[j].status & MACB_RX_SOF) != 0)
592  {
593  //Save the position of the SOF
594  sofIndex = i;
595  }
596 
597  //A valid EOF has been found?
598  if((rxBufferDesc[j].status & MACB_RX_EOF) != 0 && sofIndex != UINT_MAX)
599  {
600  //Save the position of the EOF
601  eofIndex = i;
602  //Retrieve the length of the frame
603  size = rxBufferDesc[j].status & MACB_RX_LENGTH;
604  //Limit the number of data to read
605  size = MIN(size, ETH_MAX_FRAME_SIZE);
606  //Stop processing since we have reached the end of the frame
607  break;
608  }
609  }
610 
611  //Determine the number of entries to process
612  if(eofIndex != UINT_MAX)
613  {
614  j = eofIndex + 1;
615  }
616  else if(sofIndex != UINT_MAX)
617  {
618  j = sofIndex;
619  }
620  else
621  {
622  j = i;
623  }
624 
625  //Total number of bytes that have been copied from the receive buffer
626  length = 0;
627 
628  //Process incoming frame
629  for(i = 0; i < j; i++)
630  {
631  //Any data to copy from current buffer?
632  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
633  {
634  //Calculate the number of bytes to read at a time
635  n = MIN(size, AVR32_ETH_RX_BUFFER_SIZE);
636  //Copy data from receive buffer
637  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
638  //Update byte counters
639  length += n;
640  size -= n;
641  }
642 
643  //Mark the current buffer as free
644  rxBufferDesc[rxBufferIndex].address &= ~MACB_RX_OWNERSHIP;
645 
646  //Point to the following entry
647  rxBufferIndex++;
648 
649  //Wrap around to the beginning of the buffer if necessary
650  if(rxBufferIndex >= AVR32_ETH_RX_BUFFER_COUNT)
651  {
652  rxBufferIndex = 0;
653  }
654  }
655 
656  //Any packet to process?
657  if(length > 0)
658  {
659  NetRxAncillary ancillary;
660 
661  //Additional options can be passed to the stack along with the packet
662  ancillary = NET_DEFAULT_RX_ANCILLARY;
663 
664  //Pass the packet to the upper layer
665  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
666  //Valid packet received
667  error = NO_ERROR;
668  }
669  else
670  {
671  //No more data in the receive buffer
672  error = ERROR_BUFFER_EMPTY;
673  }
674 
675  //Return status code
676  return error;
677 }
678 
679 
680 /**
681  * @brief Configure MAC address filtering
682  * @param[in] interface Underlying network interface
683  * @return Error code
684  **/
685 
687 {
688  uint_t i;
689  uint_t k;
690  uint8_t *p;
691  uint32_t hashTable[2];
692  MacFilterEntry *entry;
693 
694  //Debug message
695  TRACE_DEBUG("Updating MAC filter...\r\n");
696 
697  //Set the MAC address of the station
698  AVR32_MACB.sa1b = interface->macAddr.b[0] |
699  (interface->macAddr.b[1] << 8) |
700  (interface->macAddr.b[2] << 16) |
701  (interface->macAddr.b[3] << 24);
702 
703  AVR32_MACB.sa1t = interface->macAddr.b[4] |
704  (interface->macAddr.b[5] << 8);
705 
706  //Clear hash table
707  hashTable[0] = 0;
708  hashTable[1] = 0;
709 
710  //The MAC address filter contains the list of MAC addresses to accept
711  //when receiving an Ethernet frame
712  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
713  {
714  //Point to the current entry
715  entry = &interface->macAddrFilter[i];
716 
717  //Valid entry?
718  if(entry->refCount > 0)
719  {
720  //Point to the MAC address
721  p = entry->addr.b;
722 
723  //Apply the hash function
724  k = (p[0] >> 6) ^ p[0];
725  k ^= (p[1] >> 4) ^ (p[1] << 2);
726  k ^= (p[2] >> 2) ^ (p[2] << 4);
727  k ^= (p[3] >> 6) ^ p[3];
728  k ^= (p[4] >> 4) ^ (p[4] << 2);
729  k ^= (p[5] >> 2) ^ (p[5] << 4);
730 
731  //The hash value is reduced to a 6-bit index
732  k &= 0x3F;
733 
734  //Update hash table contents
735  hashTable[k / 32] |= (1 << (k % 32));
736  }
737  }
738 
739  //Write the hash table
740  AVR32_MACB.hrb = hashTable[0];
741  AVR32_MACB.hrt = hashTable[1];
742 
743  //Debug message
744  TRACE_DEBUG(" HRB = %08" PRIX32 "\r\n", AVR32_MACB.hrb);
745  TRACE_DEBUG(" HRT = %08" PRIX32 "\r\n", AVR32_MACB.hrt);
746 
747  //Successful processing
748  return NO_ERROR;
749 }
750 
751 
752 /**
753  * @brief Adjust MAC configuration parameters for proper operation
754  * @param[in] interface Underlying network interface
755  * @return Error code
756  **/
757 
759 {
760  uint32_t config;
761 
762  //Read network configuration register
763  config = AVR32_MACB.ncfgr;
764 
765  //10BASE-T or 100BASE-TX operation mode?
766  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
767  {
768  config |= AVR32_MACB_NCFGR_SPD_MASK;
769  }
770  else
771  {
772  config &= ~AVR32_MACB_NCFGR_SPD_MASK;
773  }
774 
775  //Half-duplex or full-duplex mode?
776  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
777  {
778  config |= AVR32_MACB_NCFGR_FD_MASK;
779  }
780  else
781  {
782  config &= ~AVR32_MACB_NCFGR_FD_MASK;
783  }
784 
785  //Write configuration value back to NCFGR register
786  AVR32_MACB.ncfgr = config;
787 
788  //Successful processing
789  return NO_ERROR;
790 }
791 
792 
793 /**
794  * @brief Write PHY register
795  * @param[in] opcode Access type (2 bits)
796  * @param[in] phyAddr PHY address (5 bits)
797  * @param[in] regAddr Register address (5 bits)
798  * @param[in] data Register value
799  **/
800 
801 void avr32EthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
802  uint8_t regAddr, uint16_t data)
803 {
804  uint32_t temp;
805 
806  //Valid opcode?
807  if(opcode == SMI_OPCODE_WRITE)
808  {
809  //Set up a write operation
811  //PHY address
812  temp |= (phyAddr << AVR32_MACB_MAN_PHYA_OFFSET) & AVR32_MACB_MAN_PHYA_MASK;
813  //Register address
814  temp |= (regAddr << AVR32_MACB_MAN_REGA_OFFSET) & AVR32_MACB_MAN_REGA_MASK;
815  //Register value
816  temp |= data & AVR32_MACB_MAN_DATA_MASK;
817 
818  //Start a write operation
819  AVR32_MACB.man = temp;
820  //Wait for the write to complete
821  while((AVR32_MACB.nsr & AVR32_MACB_NSR_IDLE_MASK) == 0)
822  {
823  }
824  }
825  else
826  {
827  //The MAC peripheral only supports standard Clause 22 opcodes
828  }
829 }
830 
831 
832 /**
833  * @brief Read PHY register
834  * @param[in] opcode Access type (2 bits)
835  * @param[in] phyAddr PHY address (5 bits)
836  * @param[in] regAddr Register address (5 bits)
837  * @return Register value
838  **/
839 
840 uint16_t avr32EthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
841  uint8_t regAddr)
842 {
843  uint16_t data;
844  uint32_t temp;
845 
846  //Valid opcode?
847  if(opcode == SMI_OPCODE_READ)
848  {
849  //Set up a read operation
851  //PHY address
852  temp |= (phyAddr << AVR32_MACB_MAN_PHYA_OFFSET) & AVR32_MACB_MAN_PHYA_MASK;
853  //Register address
854  temp |= (regAddr << AVR32_MACB_MAN_REGA_OFFSET) & AVR32_MACB_MAN_REGA_MASK;
855 
856  //Start a read operation
857  AVR32_MACB.man = temp;
858  //Wait for the read to complete
859  while((AVR32_MACB.nsr & AVR32_MACB_NSR_IDLE_MASK) == 0)
860  {
861  }
862 
863  //Get register value
864  data = AVR32_MACB.man & AVR32_MACB_MAN_DATA_MASK;
865  }
866  else
867  {
868  //The MAC peripheral only supports standard Clause 22 opcodes
869  data = 0;
870  }
871 
872  //Return the value of the PHY register
873  return data;
874 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
__weak_func void avr32EthInitGpio(NetInterface *interface)
GPIO configuration.
void avr32EthInitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
uint8_t opcode
Definition: dns_common.h:188
int bool_t
Definition: compiler_port.h:53
#define AVR32_ETH_TX_BUFFER_COUNT
#define MACB_RMII_MASK
#define netEvent
Definition: net_legacy.h:196
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define AVR32_ETH_RX_BUFFER_COUNT
size_t netBufferRead(void *dest, const NetBuffer *src, size_t srcOffset, size_t length)
Read data from a multi-part buffer.
Definition: net_mem.c:690
#define MACB_TX_LENGTH
#define MACB_MAN_SOF_01
uint8_t p
Definition: ndp.h:300
void avr32EthDisableIrq(NetInterface *interface)
Disable interrupts.
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:222
#define ETH_MAX_FRAME_SIZE
Definition: ethernet.h:110
uint_t avr32EthReceivePacket(NetInterface *interface)
Receive a packet.
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
bool_t avr32EthIrqHandler(void)
AVR32 Ethernet MAC interrupt service routine.
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 osExitIsr(flag)
#define MACB_RX_LENGTH
#define SMI_OPCODE_WRITE
Definition: nic.h:66
AVR32 Ethernet MAC driver.
#define MACB_RX_WRAP
#define MACB_RX_SOF
#define AVR32_ETH_IRQ_PRIORITY
Transmit buffer descriptor.
#define FALSE
Definition: os_port.h:46
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define MACB_RX_OWNERSHIP
error_t
Error codes.
Definition: error.h:43
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:104
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
Receive buffer descriptor.
#define txBuffer
error_t avr32EthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:36
MacAddr addr
MAC address.
Definition: ethernet.h:263
#define MACB_RX_EOF
@ ERROR_INVALID_LENGTH
Definition: error.h:111
void avr32EthEnableIrq(NetInterface *interface)
Enable interrupts.
@ ERROR_BUFFER_EMPTY
Definition: error.h:141
void avr32EthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
#define NetTxAncillary
Definition: net_misc.h:36
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:95
#define AVR32_ETH_TX_BUFFER_SIZE
uint8_t length
Definition: tcp.h:368
#define MACB_MAN_CODE_10
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
void avr32EthEventHandler(NetInterface *interface)
AVR32 Ethernet MAC event handler.
error_t avr32EthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define MIN(a, b)
Definition: os_port.h:63
#define rxBuffer
void avr32EthTick(NetInterface *interface)
AVR32 Ethernet MAC timer handler.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define MACB_TX_WRAP
uint16_t regAddr
#define MACB_RX_ADDRESS
#define ETH_MTU
Definition: ethernet.h:116
uint8_t n
MAC filter table entry.
Definition: ethernet.h:262
Ipv6Addr address[]
Definition: ipv6.h:325
#define osEnterIsr()
uint16_t avr32EthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define MACB_MAN_RW_01
#define MACB_TX_LAST
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define MACB_MAN_RW_10
error_t avr32EthInit(NetInterface *interface)
AVR32 Ethernet MAC initialization.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
#define AVR32_ETH_RX_BUFFER_SIZE
const NicDriver avr32EthDriver
AVR32 Ethernet MAC driver.
void avr32EthIrqWrapper(void)
error_t avr32EthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
NIC driver.
Definition: nic.h:286
@ NO_ERROR
Success.
Definition: error.h:44
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
Debugging facilities.
#define MACB_TX_USED
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83