pic32cx_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file pic32cx_eth_driver.c
3  * @brief PIC32CX SG41/SG60/SG61 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.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 "pic32c.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 PIC32CX Ethernet MAC driver
86  **/
87 
89 {
91  ETH_MTU,
102  TRUE,
103  TRUE,
104  TRUE,
105  FALSE
106 };
107 
108 
109 /**
110  * @brief PIC32CX 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 PIC32CX Ethernet MAC...\r\n");
122 
123  //Save underlying network interface
124  nicDriverInterface = interface;
125 
126 #if defined(__PIC32CX2051BZ62132__) || defined(__PIC32CX2051BZ62132__)
127 #else
128  //Enable GMAC bus clocks (CLK_GMAC_APB and CLK_GMAC_AHB)
129  MCLK_REGS->MCLK_APBCMASK |= MCLK_APBCMASK_GMAC_Msk;
130  MCLK_REGS->MCLK_AHBMASK |= MCLK_AHBMASK_GMAC_Msk;
131 #endif
132 
133  //Disable transmit and receive circuits
134  GMAC_REGS->GMAC_NCR = 0;
135 
136  //GPIO configuration
137  pic32cxEthInitGpio(interface);
138 
139  //Configure MDC clock speed
140  GMAC_REGS->GMAC_NCFGR = GMAC_NCFGR_CLK(5);
141  //Enable management port (MDC and MDIO)
142  GMAC_REGS->GMAC_NCR |= GMAC_NCR_MPE_Msk;
143 
144  //Valid Ethernet PHY or switch driver?
145  if(interface->phyDriver != NULL)
146  {
147  //Ethernet PHY initialization
148  error = interface->phyDriver->init(interface);
149  }
150  else if(interface->switchDriver != NULL)
151  {
152  //Ethernet switch initialization
153  error = interface->switchDriver->init(interface);
154  }
155  else
156  {
157  //The interface is not properly configured
158  error = ERROR_FAILURE;
159  }
160 
161  //Any error to report?
162  if(error)
163  {
164  return error;
165  }
166 
167  //Set the MAC address of the station
168  GMAC_REGS->SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
169  GMAC_REGS->SA[0].GMAC_SAT = interface->macAddr.w[2];
170 
171  //The MAC supports 3 additional addresses for unicast perfect filtering
172  GMAC_REGS->SA[1].GMAC_SAB = 0;
173  GMAC_REGS->SA[2].GMAC_SAB = 0;
174  GMAC_REGS->SA[3].GMAC_SAB = 0;
175 
176  //Initialize hash table
177  GMAC_REGS->GMAC_HRB = 0;
178  GMAC_REGS->GMAC_HRT = 0;
179 
180  //Configure the receive filter
182 
183  //Initialize buffer descriptors
184  pic32cxEthInitBufferDesc(interface);
185 
186  //Clear transmit status register
190 
191  //Clear receive status register
194 
195  //First disable all GMAC interrupts
196  GMAC_REGS->GMAC_IDR = 0xFFFFFFFF;
197 
198  //Only the desired ones are enabled
202 
203  //Read GMAC_ISR register to clear any pending interrupt
204  status = GMAC_REGS->GMAC_ISR;
205  (void) status;
206 
207  //Set priority grouping (3 bits for pre-emption priority, no bits for subpriority)
208  NVIC_SetPriorityGrouping(PIC32CX_ETH_IRQ_PRIORITY_GROUPING);
209 
210  //Configure GMAC interrupt priority
211  NVIC_SetPriority(GMAC_IRQn, NVIC_EncodePriority(PIC32CX_ETH_IRQ_PRIORITY_GROUPING,
213 
214  //Enable the GMAC to transmit and receive data
216 
217  //Accept any packets from the upper layer
218  osSetEvent(&interface->nicTxEvent);
219 
220  //Successful initialization
221  return NO_ERROR;
222 }
223 
224 
225 /**
226  * @brief GPIO configuration
227  * @param[in] interface Underlying network interface
228  **/
229 
230 __weak_func void pic32cxEthInitGpio(NetInterface *interface)
231 {
232 //PIC32CX SG41/SG61 Curiosity Ultra evaluation board?
233 #if defined(USE_PIC32CX_SG41_CURIOSITY_ULTRA) || \
234  defined(USE_PIC32CX_SG61_CURIOSITY_ULTRA)
235  uint32_t temp;
236 
237  //Enable PORT bus clock (CLK_PORT_APB)
238  MCLK_REGS->MCLK_APBBMASK |= MCLK_APBBMASK_PORT_Msk;
239 
240  //Configure GRX1 (PA12)
241  PORT_REGS->GROUP[0].PORT_PINCFG[12] |= PORT_PINCFG_PMUXEN_Msk;
242  temp = PORT_REGS->GROUP[0].PORT_PMUX[6] & ~PORT_PMUX_PMUXE_Msk;
243  PORT_REGS->GROUP[0].PORT_PMUX[6] = temp | PORT_PMUX_PMUXE(MUX_PA12L_GMAC_GRX1);
244 
245  //Configure GRX0 (PA13)
246  PORT_REGS->GROUP[0].PORT_PINCFG[13] |= PORT_PINCFG_PMUXEN_Msk;
247  temp = PORT_REGS->GROUP[0].PORT_PMUX[6] & ~PORT_PMUX_PMUXO_Msk;
248  PORT_REGS->GROUP[0].PORT_PMUX[6] = temp | PORT_PMUX_PMUXO(MUX_PA13L_GMAC_GRX0);
249 
250  //Configure GTXCK (PA14)
251  PORT_REGS->GROUP[0].PORT_PINCFG[14] |= PORT_PINCFG_PMUXEN_Msk;
252  temp = PORT_REGS->GROUP[0].PORT_PMUX[7] & ~PORT_PMUX_PMUXE_Msk;
253  PORT_REGS->GROUP[0].PORT_PMUX[7] = temp | PORT_PMUX_PMUXE(MUX_PA14L_GMAC_GTXCK);
254 
255  //Configure GRXER (PA15)
256  PORT_REGS->GROUP[0].PORT_PINCFG[15] |= PORT_PINCFG_PMUXEN_Msk;
257  temp = PORT_REGS->GROUP[0].PORT_PMUX[7] & ~PORT_PMUX_PMUXO_Msk;
258  PORT_REGS->GROUP[0].PORT_PMUX[7] = temp | PORT_PMUX_PMUXO(MUX_PA15L_GMAC_GRXER);
259 
260  //Configure GTXEN (PA17)
261  PORT_REGS->GROUP[0].PORT_PINCFG[17] |= PORT_PINCFG_DRVSTR_Msk;
262  PORT_REGS->GROUP[0].PORT_PINCFG[17] |= PORT_PINCFG_PMUXEN_Msk;
263  temp = PORT_REGS->GROUP[0].PORT_PMUX[8] & ~PORT_PMUX_PMUXO_Msk;
264  PORT_REGS->GROUP[0].PORT_PMUX[8] = temp | PORT_PMUX_PMUXO(MUX_PA17L_GMAC_GTXEN);
265 
266  //Configure GTX0 (PA18)
267  PORT_REGS->GROUP[0].PORT_PINCFG[18] |= PORT_PINCFG_DRVSTR_Msk;
268  PORT_REGS->GROUP[0].PORT_PINCFG[18] |= PORT_PINCFG_PMUXEN_Msk;
269  temp = PORT_REGS->GROUP[0].PORT_PMUX[9] & ~PORT_PMUX_PMUXE_Msk;
270  PORT_REGS->GROUP[0].PORT_PMUX[9] = temp | PORT_PMUX_PMUXE(MUX_PA18L_GMAC_GTX0);
271 
272  //Configure GTX1 (PA19)
273  PORT_REGS->GROUP[0].PORT_PINCFG[19] |= PORT_PINCFG_DRVSTR_Msk;
274  PORT_REGS->GROUP[0].PORT_PINCFG[19] |= PORT_PINCFG_PMUXEN_Msk;
275  temp = PORT_REGS->GROUP[0].PORT_PMUX[9] & ~PORT_PMUX_PMUXO_Msk;
276  PORT_REGS->GROUP[0].PORT_PMUX[9] = temp | PORT_PMUX_PMUXO(MUX_PA19L_GMAC_GTX1);
277 
278  //Configure GRXDV (PC20)
279  PORT_REGS->GROUP[2].PORT_PINCFG[20] |= PORT_PINCFG_PMUXEN_Msk;
280  temp = PORT_REGS->GROUP[2].PORT_PMUX[10] & ~PORT_PMUX_PMUXE_Msk;
281  PORT_REGS->GROUP[2].PORT_PMUX[10] = temp | PORT_PMUX_PMUXE(MUX_PC20L_GMAC_GRXDV);
282 
283  //Configure GMDC (PC22)
284  PORT_REGS->GROUP[2].PORT_PINCFG[22] |= PORT_PINCFG_PMUXEN_Msk;
285  temp = PORT_REGS->GROUP[2].PORT_PMUX[11] & ~PORT_PMUX_PMUXE_Msk;
286  PORT_REGS->GROUP[2].PORT_PMUX[11] = temp | PORT_PMUX_PMUXE(MUX_PC22L_GMAC_GMDC);
287 
288  //Configure GMDIO (PC23)
289  PORT_REGS->GROUP[2].PORT_PINCFG[23] |= PORT_PINCFG_PMUXEN_Msk;
290  temp = PORT_REGS->GROUP[2].PORT_PMUX[11] & ~PORT_PMUX_PMUXO_Msk;
291  PORT_REGS->GROUP[2].PORT_PMUX[11] = temp | PORT_PMUX_PMUXO(MUX_PC23L_GMAC_GMDIO);
292 
293  //Select RMII operation mode
294  GMAC_REGS->GMAC_UR &= ~GMAC_UR_MII_Msk;
295 
296  //Configure PHY_RESET (PC18) as an output
297  PORT_REGS->GROUP[2].PORT_DIRSET = PORT_PC18;
298 
299  //Reset PHY transceiver
300  PORT_REGS->GROUP[2].PORT_OUTCLR = PORT_PC18;
301  sleep(10);
302  PORT_REGS->GROUP[2].PORT_OUTSET = PORT_PC18;
303  sleep(10);
304 #endif
305 }
306 
307 
308 /**
309  * @brief Initialize buffer descriptors
310  * @param[in] interface Underlying network interface
311  **/
312 
314 {
315  uint_t i;
316  uint32_t address;
317 
318  //Initialize TX buffer descriptors
319  for(i = 0; i < PIC32CX_ETH_TX_BUFFER_COUNT; i++)
320  {
321  //Calculate the address of the current TX buffer
322  address = (uint32_t) txBuffer[i];
323  //Write the address to the descriptor entry
324  txBufferDesc[i].address = address;
325  //Initialize status field
326  txBufferDesc[i].status = GMAC_TX_USED;
327  }
328 
329  //Mark the last descriptor entry with the wrap flag
330  txBufferDesc[i - 1].status |= GMAC_TX_WRAP;
331  //Initialize TX buffer index
332  txBufferIndex = 0;
333 
334  //Initialize RX buffer descriptors
335  for(i = 0; i < PIC32CX_ETH_RX_BUFFER_COUNT; i++)
336  {
337  //Calculate the address of the current RX buffer
338  address = (uint32_t) rxBuffer[i];
339  //Write the address to the descriptor entry
340  rxBufferDesc[i].address = address & GMAC_RX_ADDRESS;
341  //Clear status field
342  rxBufferDesc[i].status = 0;
343  }
344 
345  //Mark the last descriptor entry with the wrap flag
346  rxBufferDesc[i - 1].address |= GMAC_RX_WRAP;
347  //Initialize RX buffer index
348  rxBufferIndex = 0;
349 
350  //Start location of the TX descriptor list
351  GMAC_REGS->GMAC_TBQB = (uint32_t) txBufferDesc;
352  //Start location of the RX descriptor list
353  GMAC_REGS->GMAC_RBQB = (uint32_t) rxBufferDesc;
354 }
355 
356 
357 /**
358  * @brief PIC32CX Ethernet MAC timer handler
359  *
360  * This routine is periodically called by the TCP/IP stack to handle periodic
361  * operations such as polling the link state
362  *
363  * @param[in] interface Underlying network interface
364  **/
365 
366 void pic32cxEthTick(NetInterface *interface)
367 {
368  //Valid Ethernet PHY or switch driver?
369  if(interface->phyDriver != NULL)
370  {
371  //Handle periodic operations
372  interface->phyDriver->tick(interface);
373  }
374  else if(interface->switchDriver != NULL)
375  {
376  //Handle periodic operations
377  interface->switchDriver->tick(interface);
378  }
379  else
380  {
381  //Just for sanity
382  }
383 }
384 
385 
386 /**
387  * @brief Enable interrupts
388  * @param[in] interface Underlying network interface
389  **/
390 
392 {
393  //Enable Ethernet MAC interrupts
394  NVIC_EnableIRQ(GMAC_IRQn);
395 
396  //Valid Ethernet PHY or switch driver?
397  if(interface->phyDriver != NULL)
398  {
399  //Enable Ethernet PHY interrupts
400  interface->phyDriver->enableIrq(interface);
401  }
402  else if(interface->switchDriver != NULL)
403  {
404  //Enable Ethernet switch interrupts
405  interface->switchDriver->enableIrq(interface);
406  }
407  else
408  {
409  //Just for sanity
410  }
411 }
412 
413 
414 /**
415  * @brief Disable interrupts
416  * @param[in] interface Underlying network interface
417  **/
418 
420 {
421  //Disable Ethernet MAC interrupts
422  NVIC_DisableIRQ(GMAC_IRQn);
423 
424  //Valid Ethernet PHY or switch driver?
425  if(interface->phyDriver != NULL)
426  {
427  //Disable Ethernet PHY interrupts
428  interface->phyDriver->disableIrq(interface);
429  }
430  else if(interface->switchDriver != NULL)
431  {
432  //Disable Ethernet switch interrupts
433  interface->switchDriver->disableIrq(interface);
434  }
435  else
436  {
437  //Just for sanity
438  }
439 }
440 
441 
442 /**
443  * @brief PIC32CX Ethernet MAC interrupt service routine
444  **/
445 
446 void GMAC_Handler(void)
447 {
448  bool_t flag;
449  volatile uint32_t isr;
450  volatile uint32_t tsr;
451  volatile uint32_t rsr;
452 
453  //Interrupt service routine prologue
454  osEnterIsr();
455 
456  //This flag will be set if a higher priority task must be woken
457  flag = FALSE;
458 
459  //Each time the software reads GMAC_ISR, it has to check the contents
460  //of GMAC_TSR, GMAC_RSR and GMAC_NSR
461  isr = GMAC_REGS->GMAC_ISR;
462  tsr = GMAC_REGS->GMAC_TSR;
463  rsr = GMAC_REGS->GMAC_RSR;
464  (void) isr;
465 
466  //Packet transmitted?
467  if((tsr & (GMAC_TSR_HRESP_Msk | GMAC_TSR_UND_Msk |
470  {
471  //Only clear TSR flags that are currently set
472  GMAC_REGS->GMAC_TSR = tsr;
473 
474  //Check whether the TX buffer is available for writing
475  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
476  {
477  //Notify the TCP/IP stack that the transmitter is ready to send
478  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
479  }
480  }
481 
482  //Packet received?
484  GMAC_RSR_BNA_Msk)) != 0)
485  {
486  //Set event flag
487  nicDriverInterface->nicEvent = TRUE;
488  //Notify the TCP/IP stack of the event
489  flag |= osSetEventFromIsr(&nicDriverInterface->netContext->event);
490  }
491 
492  //Interrupt service routine epilogue
493  osExitIsr(flag);
494 }
495 
496 
497 /**
498  * @brief PIC32CX Ethernet MAC event handler
499  * @param[in] interface Underlying network interface
500  **/
501 
503 {
504  error_t error;
505  uint32_t rsr;
506 
507  //Read receive status
508  rsr = GMAC_REGS->GMAC_RSR;
509 
510  //Packet received?
512  GMAC_RSR_BNA_Msk)) != 0)
513  {
514  //Only clear RSR flags that are currently set
515  GMAC_REGS->GMAC_RSR = rsr;
516 
517  //Process all pending packets
518  do
519  {
520  //Read incoming packet
521  error = pic32cxEthReceivePacket(interface);
522 
523  //No more data in the receive buffer?
524  } while(error != ERROR_BUFFER_EMPTY);
525  }
526 }
527 
528 
529 /**
530  * @brief Send a packet
531  * @param[in] interface Underlying network interface
532  * @param[in] buffer Multi-part buffer containing the data to send
533  * @param[in] offset Offset to the first data byte
534  * @param[in] ancillary Additional options passed to the stack along with
535  * the packet
536  * @return Error code
537  **/
538 
540  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
541 {
542  size_t length;
543 
544  //Retrieve the length of the packet
545  length = netBufferGetLength(buffer) - offset;
546 
547  //Check the frame length
549  {
550  //The transmitter can accept another packet
551  osSetEvent(&interface->nicTxEvent);
552  //Report an error
553  return ERROR_INVALID_LENGTH;
554  }
555 
556  //Make sure the current buffer is available for writing
557  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) == 0)
558  {
559  return ERROR_FAILURE;
560  }
561 
562  //Copy user data to the transmit buffer
563  netBufferRead(txBuffer[txBufferIndex], buffer, offset, length);
564 
565  //Set the necessary flags in the descriptor entry
566  if(txBufferIndex < (PIC32CX_ETH_TX_BUFFER_COUNT - 1))
567  {
568  //Write the status word
569  txBufferDesc[txBufferIndex].status = GMAC_TX_LAST |
571 
572  //Point to the next buffer
573  txBufferIndex++;
574  }
575  else
576  {
577  //Write the status word
578  txBufferDesc[txBufferIndex].status = GMAC_TX_WRAP | GMAC_TX_LAST |
580 
581  //Wrap around
582  txBufferIndex = 0;
583  }
584 
585  //Data synchronization barrier
586  __DSB();
587 
588  //Set the TSTART bit to initiate transmission
589  GMAC_REGS->GMAC_NCR |= GMAC_NCR_TSTART_Msk;
590 
591  //Check whether the next buffer is available for writing
592  if((txBufferDesc[txBufferIndex].status & GMAC_TX_USED) != 0)
593  {
594  //The transmitter can accept another packet
595  osSetEvent(&interface->nicTxEvent);
596  }
597 
598  //Successful processing
599  return NO_ERROR;
600 }
601 
602 
603 /**
604  * @brief Receive a packet
605  * @param[in] interface Underlying network interface
606  * @return Error code
607  **/
608 
610 {
611  static uint32_t temp[ETH_MAX_FRAME_SIZE / 4];
612  error_t error;
613  uint_t i;
614  uint_t j;
615  uint_t sofIndex;
616  uint_t eofIndex;
617  size_t n;
618  size_t size;
619  size_t length;
620 
621  //Initialize variables
622  size = 0;
623  sofIndex = UINT_MAX;
624  eofIndex = UINT_MAX;
625 
626  //Search for SOF and EOF flags
627  for(i = 0; i < PIC32CX_ETH_RX_BUFFER_COUNT; i++)
628  {
629  //Point to the current entry
630  j = rxBufferIndex + i;
631 
632  //Wrap around to the beginning of the buffer if necessary
634  {
636  }
637 
638  //No more entries to process?
639  if((rxBufferDesc[j].address & GMAC_RX_OWNERSHIP) == 0)
640  {
641  //Stop processing
642  break;
643  }
644 
645  //A valid SOF has been found?
646  if((rxBufferDesc[j].status & GMAC_RX_SOF) != 0)
647  {
648  //Save the position of the SOF
649  sofIndex = i;
650  }
651 
652  //A valid EOF has been found?
653  if((rxBufferDesc[j].status & GMAC_RX_EOF) != 0 && sofIndex != UINT_MAX)
654  {
655  //Save the position of the EOF
656  eofIndex = i;
657  //Retrieve the length of the frame
658  size = rxBufferDesc[j].status & GMAC_RX_LENGTH;
659  //Limit the number of data to read
660  size = MIN(size, ETH_MAX_FRAME_SIZE);
661  //Stop processing since we have reached the end of the frame
662  break;
663  }
664  }
665 
666  //Determine the number of entries to process
667  if(eofIndex != UINT_MAX)
668  {
669  j = eofIndex + 1;
670  }
671  else if(sofIndex != UINT_MAX)
672  {
673  j = sofIndex;
674  }
675  else
676  {
677  j = i;
678  }
679 
680  //Total number of bytes that have been copied from the receive buffer
681  length = 0;
682 
683  //Process incoming frame
684  for(i = 0; i < j; i++)
685  {
686  //Any data to copy from current buffer?
687  if(eofIndex != UINT_MAX && i >= sofIndex && i <= eofIndex)
688  {
689  //Calculate the number of bytes to read at a time
691  //Copy data from receive buffer
692  osMemcpy((uint8_t *) temp + length, rxBuffer[rxBufferIndex], n);
693  //Update byte counters
694  length += n;
695  size -= n;
696  }
697 
698  //Mark the current buffer as free
699  rxBufferDesc[rxBufferIndex].address &= ~GMAC_RX_OWNERSHIP;
700 
701  //Point to the following entry
702  rxBufferIndex++;
703 
704  //Wrap around to the beginning of the buffer if necessary
705  if(rxBufferIndex >= PIC32CX_ETH_RX_BUFFER_COUNT)
706  {
707  rxBufferIndex = 0;
708  }
709  }
710 
711  //Any packet to process?
712  if(length > 0)
713  {
714  NetRxAncillary ancillary;
715 
716  //Additional options can be passed to the stack along with the packet
717  ancillary = NET_DEFAULT_RX_ANCILLARY;
718 
719  //Pass the packet to the upper layer
720  nicProcessPacket(interface, (uint8_t *) temp, length, &ancillary);
721  //Valid packet received
722  error = NO_ERROR;
723  }
724  else
725  {
726  //No more data in the receive buffer
727  error = ERROR_BUFFER_EMPTY;
728  }
729 
730  //Return status code
731  return error;
732 }
733 
734 
735 /**
736  * @brief Configure MAC address filtering
737  * @param[in] interface Underlying network interface
738  * @return Error code
739  **/
740 
742 {
743  uint_t i;
744  uint_t j;
745  uint_t k;
746  uint8_t *p;
747  uint32_t hashTable[2];
748  MacAddr unicastMacAddr[3];
749  MacFilterEntry *entry;
750 
751  //Debug message
752  TRACE_DEBUG("Updating MAC filter...\r\n");
753 
754  //Set the MAC address of the station
755  GMAC_REGS->SA[0].GMAC_SAB = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
756  GMAC_REGS->SA[0].GMAC_SAT = interface->macAddr.w[2];
757 
758  //The MAC supports 3 additional addresses for unicast perfect filtering
759  unicastMacAddr[0] = MAC_UNSPECIFIED_ADDR;
760  unicastMacAddr[1] = MAC_UNSPECIFIED_ADDR;
761  unicastMacAddr[2] = MAC_UNSPECIFIED_ADDR;
762 
763  //The hash table is used for multicast address filtering
764  hashTable[0] = 0;
765  hashTable[1] = 0;
766 
767  //The MAC address filter contains the list of MAC addresses to accept
768  //when receiving an Ethernet frame
769  for(i = 0, j = 0; i < MAC_ADDR_FILTER_SIZE; i++)
770  {
771  //Point to the current entry
772  entry = &interface->macAddrFilter[i];
773 
774  //Valid entry?
775  if(entry->refCount > 0)
776  {
777  //Multicast address?
778  if(macIsMulticastAddr(&entry->addr))
779  {
780  //Point to the MAC address
781  p = entry->addr.b;
782 
783  //Apply the hash function
784  k = (p[0] >> 6) ^ p[0];
785  k ^= (p[1] >> 4) ^ (p[1] << 2);
786  k ^= (p[2] >> 2) ^ (p[2] << 4);
787  k ^= (p[3] >> 6) ^ p[3];
788  k ^= (p[4] >> 4) ^ (p[4] << 2);
789  k ^= (p[5] >> 2) ^ (p[5] << 4);
790 
791  //The hash value is reduced to a 6-bit index
792  k &= 0x3F;
793 
794  //Update hash table contents
795  hashTable[k / 32] |= (1 << (k % 32));
796  }
797  else
798  {
799  //Up to 3 additional MAC addresses can be specified
800  if(j < 3)
801  {
802  //Save the unicast address
803  unicastMacAddr[j++] = entry->addr;
804  }
805  }
806  }
807  }
808 
809  //Configure the first unicast address filter
810  if(j >= 1)
811  {
812  //The address is activated when SAT register is written
813  GMAC_REGS->SA[1].GMAC_SAB = unicastMacAddr[0].w[0] | (unicastMacAddr[0].w[1] << 16);
814  GMAC_REGS->SA[1].GMAC_SAT = unicastMacAddr[0].w[2];
815  }
816  else
817  {
818  //The address is deactivated when SAB register is written
819  GMAC_REGS->SA[1].GMAC_SAB = 0;
820  }
821 
822  //Configure the second unicast address filter
823  if(j >= 2)
824  {
825  //The address is activated when SAT register is written
826  GMAC_REGS->SA[2].GMAC_SAB = unicastMacAddr[1].w[0] | (unicastMacAddr[1].w[1] << 16);
827  GMAC_REGS->SA[2].GMAC_SAT = unicastMacAddr[1].w[2];
828  }
829  else
830  {
831  //The address is deactivated when SAB register is written
832  GMAC_REGS->SA[2].GMAC_SAB = 0;
833  }
834 
835  //Configure the third unicast address filter
836  if(j >= 3)
837  {
838  //The address is activated when SAT register is written
839  GMAC_REGS->SA[3].GMAC_SAB = unicastMacAddr[2].w[0] | (unicastMacAddr[2].w[1] << 16);
840  GMAC_REGS->SA[3].GMAC_SAT = unicastMacAddr[2].w[2];
841  }
842  else
843  {
844  //The address is deactivated when SAB register is written
845  GMAC_REGS->SA[3].GMAC_SAB = 0;
846  }
847 
848  //Configure the multicast hash table
849  GMAC_REGS->GMAC_HRB = hashTable[0];
850  GMAC_REGS->GMAC_HRT = hashTable[1];
851 
852  //Debug message
853  TRACE_DEBUG(" HRB = 0x%08" PRIX32 "\r\n", GMAC_REGS->GMAC_HRB);
854  TRACE_DEBUG(" HRT = 0x%08" PRIX32 "\r\n", GMAC_REGS->GMAC_HRT);
855 
856  //Successful processing
857  return NO_ERROR;
858 }
859 
860 
861 /**
862  * @brief Adjust MAC configuration parameters for proper operation
863  * @param[in] interface Underlying network interface
864  * @return Error code
865  **/
866 
868 {
869  uint32_t config;
870 
871  //Read network configuration register
872  config = GMAC_REGS->GMAC_NCFGR;
873 
874  //10BASE-T or 100BASE-TX operation mode?
875  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
876  {
877  config |= GMAC_NCFGR_SPD_Msk;
878  }
879  else
880  {
881  config &= ~GMAC_NCFGR_SPD_Msk;
882  }
883 
884  //Half-duplex or full-duplex mode?
885  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
886  {
887  config |= GMAC_NCFGR_FD_Msk;
888  }
889  else
890  {
891  config &= ~GMAC_NCFGR_FD_Msk;
892  }
893 
894  //Write configuration value back to NCFGR register
895  GMAC_REGS->GMAC_NCFGR = config;
896 
897  //Successful processing
898  return NO_ERROR;
899 }
900 
901 
902 /**
903  * @brief Write PHY register
904  * @param[in] opcode Access type (2 bits)
905  * @param[in] phyAddr PHY address (5 bits)
906  * @param[in] regAddr Register address (5 bits)
907  * @param[in] data Register value
908  **/
909 
910 void pic32cxEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
911  uint8_t regAddr, uint16_t data)
912 {
913  uint32_t temp;
914 
915  //Valid opcode?
916  if(opcode == SMI_OPCODE_WRITE)
917  {
918  //Set up a write operation
920  //PHY address
921  temp |= GMAC_MAN_PHYA(phyAddr);
922  //Register address
923  temp |= GMAC_MAN_REGA(regAddr);
924  //Register value
925  temp |= GMAC_MAN_DATA(data);
926 
927  //Start a write operation
928  GMAC_REGS->GMAC_MAN = temp;
929  //Wait for the write to complete
930  while((GMAC_REGS->GMAC_NSR & GMAC_NSR_IDLE_Msk) == 0)
931  {
932  }
933  }
934  else
935  {
936  //The MAC peripheral only supports standard Clause 22 opcodes
937  }
938 }
939 
940 
941 /**
942  * @brief Read PHY register
943  * @param[in] opcode Access type (2 bits)
944  * @param[in] phyAddr PHY address (5 bits)
945  * @param[in] regAddr Register address (5 bits)
946  * @return Register value
947  **/
948 
949 uint16_t pic32cxEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
950  uint8_t regAddr)
951 {
952  uint16_t data;
953  uint32_t temp;
954 
955  //Valid opcode?
956  if(opcode == SMI_OPCODE_READ)
957  {
958  //Set up a read operation
960  //PHY address
961  temp |= GMAC_MAN_PHYA(phyAddr);
962  //Register address
963  temp |= GMAC_MAN_REGA(regAddr);
964 
965  //Start a read operation
966  GMAC_REGS->GMAC_MAN = temp;
967  //Wait for the read to complete
968  while((GMAC_REGS->GMAC_NSR & GMAC_NSR_IDLE_Msk) == 0)
969  {
970  }
971 
972  //Get register value
973  data = GMAC_REGS->GMAC_MAN & GMAC_MAN_DATA_Msk;
974  }
975  else
976  {
977  //The MAC peripheral only supports standard Clause 22 opcodes
978  data = 0;
979  }
980 
981  //Return the value of the PHY register
982  return data;
983 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
#define GMAC_NCFGR_MTIHEN_Msk
error_t pic32cxEthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
#define GMAC_NCFGR_CLK
#define GMAC_IER_RLEX_Msk
uint8_t opcode
Definition: dns_common.h:191
int bool_t
Definition: compiler_port.h:63
#define GMAC_TX_LENGTH
error_t pic32cxEthInit(NetInterface *interface)
PIC32CX Ethernet MAC initialization.
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define GMAC_IER_HRESP_Msk
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 GMAC_NSR_IDLE_Msk
uint8_t p
Definition: ndp.h:300
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:224
#define sleep(delay)
Definition: os_port.h:313
#define GMAC_NCR_RXEN_Msk
#define ETH_MAX_FRAME_SIZE
Definition: ethernet.h:110
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:266
#define GMAC_TSR_RLE_Msk
#define PIC32CX_ETH_RX_BUFFER_COUNT
error_t pic32cxEthReceivePacket(NetInterface *interface)
Receive a packet.
#define GMAC_RX_WRAP
#define GMAC_TSR_UND_Msk
#define GMAC_MAN_PHYA
void pic32cxEthDisableIrq(NetInterface *interface)
Disable interrupts.
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 GMAC_TSR_TFC_Msk
#define osExitIsr(flag)
void pic32cxEthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
#define GMAC_RX_EOF
#define GMAC_MAN_DATA
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define GMAC_TSR_HRESP_Msk
error_t pic32cxEthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define GMAC_TX_USED
#define GMAC_MAN_OP
#define FALSE
Definition: os_port.h:46
#define GMAC_IRQn
#define GMAC_NCFGR_SPD_Msk
#define GMAC_TSR_TXGO_Msk
#define osMemcpy(dest, src, length)
Definition: os_port.h:147
__weak_func void pic32cxEthInitGpio(NetInterface *interface)
GPIO configuration.
error_t
Error codes.
Definition: error.h:43
PIC32CX SG41/SG60/SG61 Ethernet MAC driver.
#define GMAC_IER_RCOMP_Msk
#define GMAC_RX_ADDRESS
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
#define GMAC_NCR_MPE_Msk
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_BUFFER_EMPTY
Definition: error.h:142
#define GMAC_TSR_TXCOMP_Msk
#define PIC32CX_ETH_TX_BUFFER_COUNT
#define NetTxAncillary
Definition: net_misc.h:36
#define GMAC_NCFGR_FD_Msk
#define PIC32CX_ETH_IRQ_GROUP_PRIORITY
#define SMI_OPCODE_READ
Definition: nic.h:67
#define GMAC_NCR_TXEN_Msk
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
#define MIN(a, b)
Definition: os_port.h:63
#define GMAC_RSR_BNA_Msk
#define rxBuffer
#define GMAC_RX_SOF
#define GMAC_TSR_UBR_Msk
MacAddr
Definition: ethernet.h:197
void pic32cxEthEnableIrq(NetInterface *interface)
Enable interrupts.
void pic32cxEthTick(NetInterface *interface)
PIC32CX Ethernet MAC timer handler.
void pic32cxEthInitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
#define GMAC_NCR_TSTART_Msk
#define TRACE_DEBUG(...)
Definition: debug.h:119
#define PIC32CX_ETH_TX_BUFFER_SIZE
#define GMAC_IER_TUR_Msk
Receive buffer descriptor.
const NicDriver pic32cxEthDriver
PIC32CX Ethernet MAC driver.
#define GMAC_REGS
uint16_t regAddr
#define GMAC_RX_LENGTH
#define ETH_MTU
Definition: ethernet.h:116
#define GMAC_TX_LAST
uint8_t n
MAC filter table entry.
Definition: ethernet.h:264
Ipv6Addr address[]
Definition: ipv6.h:345
#define PIC32CX_ETH_IRQ_PRIORITY_GROUPING
#define osEnterIsr()
#define GMAC_IER_TFC_Msk
error_t pic32cxEthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
Transmit buffer descriptor.
#define GMAC_IER_ROVR_Msk
void pic32cxEthEventHandler(NetInterface *interface)
PIC32CX Ethernet MAC event handler.
#define GMAC_MAN_WTN
#define GMAC_RSR_HNO_Msk
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define GMAC_RSR_RXOVR_Msk
#define PIC32CX_ETH_IRQ_SUB_PRIORITY
#define GMAC_IER_TCOMP_Msk
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
#define PIC32CX_ETH_RX_BUFFER_SIZE
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
#define GMAC_RSR_REC_Msk
NIC driver.
Definition: nic.h:286
#define GMAC_RX_OWNERSHIP
#define GMAC_MAN_REGA
void GMAC_Handler(void)
PIC32CX Ethernet MAC interrupt service routine.
#define GMAC_NCFGR_MAXFS_Msk
#define GMAC_TSR_COL_Msk
#define GMAC_IER_RXUBR_Msk
#define GMAC_MAN_CLTTO_Msk
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:51
#define GMAC_MAN_DATA_Msk
@ NO_ERROR
Success.
Definition: error.h:44
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
uint16_t pic32cxEthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
Debugging facilities.
#define GMAC_TX_WRAP
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83