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