f28m35x_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file f28m35x_eth_driver.c
3  * @brief F28M35x 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 "inc/hw_ethernet.h"
36 #include "inc/hw_ints.h"
37 #include "inc/hw_memmap.h"
38 #include "inc/hw_types.h"
39 #include "driverlib/gpio.h"
40 #include "driverlib/interrupt.h"
41 #include "driverlib/sysctl.h"
42 #include "core/net.h"
44 #include "debug.h"
45 
46 //Underlying network interface
47 static NetInterface *nicDriverInterface;
48 
49 //IAR EWARM compiler?
50 #if defined(__ICCARM__)
51 
52 //Transmit buffer
53 #pragma data_alignment = 4
54 static uint8_t txBuffer[ETH_MAX_FRAME_SIZE + 2];
55 //Receive buffer
56 #pragma data_alignment = 4
57 static uint8_t rxBuffer[ETH_MAX_FRAME_SIZE];
58 
59 //GCC compiler?
60 #else
61 
62 //Transmit buffer
63 static uint8_t txBuffer[ETH_MAX_FRAME_SIZE + 2] __attribute__((aligned(4)));
64 //Receive buffer
65 static uint8_t rxBuffer[ETH_MAX_FRAME_SIZE] __attribute__((aligned(4)));
66 
67 #endif
68 
69 
70 /**
71  * @brief F28M35x Ethernet MAC driver
72  **/
73 
75 {
77  ETH_MTU,
88  TRUE,
89  TRUE,
90  TRUE,
91  FALSE
92 };
93 
94 
95 /**
96  * @brief F28M35x Ethernet MAC driver initialization
97  * @param[in] interface Underlying network interface
98  * @return Error code
99  **/
100 
102 {
103  error_t error;
104  uint_t div;
105 #ifdef ti_sysbios_BIOS___VERS
106  Hwi_Params hwiParams;
107 #endif
108 
109  //Debug message
110  TRACE_INFO("Initializing F28M35x Ethernet MAC driver...\r\n");
111 
112  //Save underlying network interface
113  nicDriverInterface = interface;
114 
115  //Enable Ethernet controller clock
116  SysCtlPeripheralEnable(SYSCTL_PERIPH_ETH);
117  //Reset Ethernet controller
118  SysCtlPeripheralReset(SYSCTL_PERIPH_ETH);
119 
120  //GPIO configuration
121  f28m35xEthInitGpio(interface);
122 
123  //The MDC clock frequency cannot exceed 2.5MHz
124  div = SysCtlClockGet(20000000) / (2 * 2500000) - 1;
125  //Adjust MDC clock frequency
126  MAC_MDV_R = div & MAC_MDV_DIV_M;
127 
128  //Valid Ethernet PHY or switch driver?
129  if(interface->phyDriver != NULL)
130  {
131  //Ethernet PHY initialization
132  error = interface->phyDriver->init(interface);
133  }
134  else if(interface->switchDriver != NULL)
135  {
136  //Ethernet switch initialization
137  error = interface->switchDriver->init(interface);
138  }
139  else
140  {
141  //The interface is not properly configured
142  error = ERROR_FAILURE;
143  }
144 
145  //Any error to report?
146  if(error)
147  {
148  return error;
149  }
150 
151  //Set the MAC address of the station
152  MAC_IA0_R = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
153  MAC_IA1_R = interface->macAddr.w[2];
154 
155  //Enable automatic CRC generation and packet padding
156  MAC_TCTL_R = MAC_TCTL_DUPLEX | MAC_TCTL_CRC | MAC_TCTL_PADEN;
157  //Flush the receive FIFO and enable CRC verification
158  MAC_RCTL_R = MAC_RCTL_RSTFIFO | MAC_RCTL_BADCRC;
159 
160  //Configure Ethernet interrupts
161  MAC_IM_R = MAC_IM_TXEMPM | MAC_IM_RXINTM;
162 
163 #ifdef ti_sysbios_BIOS___VERS
164  //Configure Ethernet interrupt
165  Hwi_Params_init(&hwiParams);
166  hwiParams.enableInt = FALSE;
167  hwiParams.priority = F28M35X_ETH_IRQ_PRIORITY;
168 
169  //Register interrupt handler
170  Hwi_create(INT_ETH, (Hwi_FuncPtr) f28m35xEthIrqHandler, &hwiParams, NULL);
171 #else
172  //Register interrupt handler
173  IntRegister(INT_ETH, f28m35xEthIrqHandler);
174 
175  //Set priority grouping (3 bits for pre-emption priority, no bits for subpriority)
176  IntPriorityGroupingSet(F28M35X_ETH_IRQ_PRIORITY_GROUPING);
177  //Configure Ethernet interrupt priority
178  IntPrioritySet(INT_ETH, F28M35X_ETH_IRQ_PRIORITY);
179 #endif
180 
181  //Enable transmitter
182  MAC_TCTL_R |= MAC_TCTL_TXEN;
183  //Enable receiver
184  MAC_RCTL_R |= MAC_RCTL_RXEN;
185 
186  //Accept any packets from the upper layer
187  osSetEvent(&interface->nicTxEvent);
188 
189  //Successful initialization
190  return NO_ERROR;
191 }
192 
193 
194 /**
195  * @brief GPIO configuration
196  * @param[in] interface Underlying network interface
197  **/
198 
199 __weak_func void f28m35xEthInitGpio(NetInterface *interface)
200 {
201 //TMDSCNCDH52C1 evaluation board?
202 #if defined(USE_TMDSCNCDH52C1)
203  //Enable GPIO clocks
204  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
205  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
206  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
207  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
208  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH);
209  SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
210 
211  //Configure MII_TXD3 (PC4)
212  GPIODirModeSet(GPIO_PORTC_BASE, GPIO_PIN_4, GPIO_DIR_MODE_HW);
213  GPIOPadConfigSet(GPIO_PORTC_BASE, GPIO_PIN_4, GPIO_PIN_TYPE_STD);
214  GPIOPinConfigure(GPIO_PC4_MIITXD3);
215 
216  //Configure MII_MDIO (PE6)
217  GPIODirModeSet(GPIO_PORTE_BASE, GPIO_PIN_6, GPIO_DIR_MODE_HW);
218  GPIOPadConfigSet(GPIO_PORTE_BASE, GPIO_PIN_6, GPIO_PIN_TYPE_STD);
219  GPIOPinConfigure(GPIO_PE6_MIIMDIO);
220 
221  //Configure MII_RXD3 (PF5)
222  GPIODirModeSet(GPIO_PORTF_BASE, GPIO_PIN_5, GPIO_DIR_MODE_HW);
223  GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_5, GPIO_PIN_TYPE_STD);
224  GPIOPinConfigure(GPIO_PF5_MIIRXD3);
225 
226  //Configure MII_RXD2 (PG0)
227  GPIODirModeSet(GPIO_PORTG_BASE, GPIO_PIN_0, GPIO_DIR_MODE_HW);
228  GPIOPadConfigSet(GPIO_PORTG_BASE, GPIO_PIN_0, GPIO_PIN_TYPE_STD);
229  GPIOPinConfigure(GPIO_PG0_MIIRXD2);
230 
231  //Configure MII_RXD1 (PG1)
232  GPIODirModeSet(GPIO_PORTG_BASE, GPIO_PIN_1, GPIO_DIR_MODE_HW);
233  GPIOPadConfigSet(GPIO_PORTG_BASE, GPIO_PIN_1, GPIO_PIN_TYPE_STD);
234  GPIOPinConfigure(GPIO_PG1_MIIRXD1);
235 
236  //Configure MII_RXDV (PG3)
237  GPIODirModeSet(GPIO_PORTG_BASE, GPIO_PIN_3, GPIO_DIR_MODE_HW);
238  GPIOPadConfigSet(GPIO_PORTG_BASE, GPIO_PIN_3, GPIO_PIN_TYPE_STD);
239  GPIOPinConfigure(GPIO_PG3_MIIRXDV);
240 
241  //Configure MII_TXER (PG7)
242  GPIODirModeSet(GPIO_PORTG_BASE, GPIO_PIN_7, GPIO_DIR_MODE_HW);
243  GPIOPadConfigSet(GPIO_PORTG_BASE, GPIO_PIN_7, GPIO_PIN_TYPE_STD);
244  GPIOPinConfigure(GPIO_PG7_MIITXER);
245 
246  //Configure MII_RXD0 (PH1)
247  GPIODirModeSet(GPIO_PORTH_BASE, GPIO_PIN_1, GPIO_DIR_MODE_HW);
248  GPIOPadConfigSet(GPIO_PORTH_BASE, GPIO_PIN_1, GPIO_PIN_TYPE_STD);
249  GPIOPinConfigure(GPIO_PH1_MIIRXD0);
250 
251  //Configure MII_TXD2 (PH3)
252  GPIODirModeSet(GPIO_PORTH_BASE, GPIO_PIN_3, GPIO_DIR_MODE_HW);
253  GPIOPadConfigSet(GPIO_PORTH_BASE, GPIO_PIN_3, GPIO_PIN_TYPE_STD);
254  GPIOPinConfigure(GPIO_PH3_MIITXD2);
255 
256  //Configure MII_TXD1 (PH4)
257  GPIODirModeSet(GPIO_PORTH_BASE, GPIO_PIN_4, GPIO_DIR_MODE_HW);
258  GPIOPadConfigSet(GPIO_PORTH_BASE, GPIO_PIN_4, GPIO_PIN_TYPE_STD);
259  GPIOPinConfigure(GPIO_PH4_MIITXD1);
260 
261  //Configure MII_TXD0 (PH5)
262  GPIODirModeSet(GPIO_PORTH_BASE, GPIO_PIN_5, GPIO_DIR_MODE_HW);
263  GPIOPadConfigSet(GPIO_PORTH_BASE, GPIO_PIN_5, GPIO_PIN_TYPE_STD);
264  GPIOPinConfigure(GPIO_PH5_MIITXD0);
265 
266  //Configure MII_TXEN (PH6)
267  GPIODirModeSet(GPIO_PORTH_BASE, GPIO_PIN_6, GPIO_DIR_MODE_HW);
268  GPIOPadConfigSet(GPIO_PORTH_BASE, GPIO_PIN_6, GPIO_PIN_TYPE_STD);
269  GPIOPinConfigure(GPIO_PH6_MIITXEN);
270 
271  //Configure MII_TXCK (PH7)
272  GPIODirModeSet(GPIO_PORTH_BASE, GPIO_PIN_7, GPIO_DIR_MODE_HW);
273  GPIOPadConfigSet(GPIO_PORTH_BASE, GPIO_PIN_7, GPIO_PIN_TYPE_STD);
274  GPIOPinConfigure(GPIO_PH7_MIITXCK);
275 
276  //Configure MII_RXER (PJ0)
277  GPIODirModeSet(GPIO_PORTJ_BASE, GPIO_PIN_0, GPIO_DIR_MODE_HW);
278  GPIOPadConfigSet(GPIO_PORTJ_BASE, GPIO_PIN_0, GPIO_PIN_TYPE_STD);
279  GPIOPinConfigure(GPIO_PJ0_MIIRXER);
280 
281  //Configure MII_RXCK (PJ2)
282  GPIODirModeSet(GPIO_PORTJ_BASE, GPIO_PIN_2, GPIO_DIR_MODE_HW);
283  GPIOPadConfigSet(GPIO_PORTJ_BASE, GPIO_PIN_2, GPIO_PIN_TYPE_STD);
284  GPIOPinConfigure(GPIO_PJ2_MIIRXCK);
285 
286  //Configure MII_MDC (PJ3)
287  GPIODirModeSet(GPIO_PORTJ_BASE, GPIO_PIN_3, GPIO_DIR_MODE_HW);
288  GPIOPadConfigSet(GPIO_PORTJ_BASE, GPIO_PIN_3, GPIO_PIN_TYPE_STD);
289  GPIOPinConfigure(GPIO_PJ3_MIIMDC);
290 
291  //Configure MII_COL (PJ4)
292  GPIODirModeSet(GPIO_PORTJ_BASE, GPIO_PIN_4, GPIO_DIR_MODE_HW);
293  GPIOPadConfigSet(GPIO_PORTJ_BASE, GPIO_PIN_4, GPIO_PIN_TYPE_STD);
294  GPIOPinConfigure(GPIO_PJ4_MIICOL);
295 
296  //Configure MII_CRS (PJ5)
297  GPIODirModeSet(GPIO_PORTJ_BASE, GPIO_PIN_5, GPIO_DIR_MODE_HW);
298  GPIOPadConfigSet(GPIO_PORTJ_BASE, GPIO_PIN_5, GPIO_PIN_TYPE_STD);
299  GPIOPinConfigure(GPIO_PJ5_MIICRS);
300 
301  //Configure MII_PHYINTR (PJ6)
302  GPIODirModeSet(GPIO_PORTJ_BASE, GPIO_PIN_6, GPIO_DIR_MODE_HW);
303  GPIOPadConfigSet(GPIO_PORTJ_BASE, GPIO_PIN_6, GPIO_PIN_TYPE_STD);
304  GPIOPinConfigure(GPIO_PJ6_MIIPHYINTRn);
305 
306  //Configure MII_PHYRSTn (PJ7)
307  GPIODirModeSet(GPIO_PORTJ_BASE, GPIO_PIN_7, GPIO_DIR_MODE_HW);
308  GPIOPadConfigSet(GPIO_PORTJ_BASE, GPIO_PIN_7, GPIO_PIN_TYPE_STD);
309  GPIOPinConfigure(GPIO_PJ7_MIIPHYRSTn);
310 #endif
311 }
312 
313 
314 /**
315  * @brief F28M35x Ethernet MAC timer handler
316  *
317  * This routine is periodically called by the TCP/IP stack to handle periodic
318  * operations such as polling the link state
319  *
320  * @param[in] interface Underlying network interface
321  **/
322 
323 void f28m35xEthTick(NetInterface *interface)
324 {
325  //Valid Ethernet PHY or switch driver?
326  if(interface->phyDriver != NULL)
327  {
328  //Handle periodic operations
329  interface->phyDriver->tick(interface);
330  }
331  else if(interface->switchDriver != NULL)
332  {
333  //Handle periodic operations
334  interface->switchDriver->tick(interface);
335  }
336  else
337  {
338  //Just for sanity
339  }
340 }
341 
342 
343 /**
344  * @brief Enable interrupts
345  * @param[in] interface Underlying network interface
346  **/
347 
349 {
350 #ifdef ti_sysbios_BIOS___VERS
351  //Enable Ethernet MAC interrupts
352  Hwi_enableInterrupt(INT_ETH);
353 #else
354  //Enable Ethernet MAC interrupts
355  IntEnable(INT_ETH);
356 #endif
357 
358  //Valid Ethernet PHY or switch driver?
359  if(interface->phyDriver != NULL)
360  {
361  //Enable Ethernet PHY interrupts
362  interface->phyDriver->enableIrq(interface);
363  }
364  else if(interface->switchDriver != NULL)
365  {
366  //Enable Ethernet switch interrupts
367  interface->switchDriver->enableIrq(interface);
368  }
369  else
370  {
371  //Just for sanity
372  }
373 }
374 
375 
376 /**
377  * @brief Disable interrupts
378  * @param[in] interface Underlying network interface
379  **/
380 
382 {
383 #ifdef ti_sysbios_BIOS___VERS
384  //Disable Ethernet MAC interrupts
385  Hwi_disableInterrupt(INT_ETH);
386 #else
387  //Disable Ethernet MAC interrupts
388  IntDisable(INT_ETH);
389 #endif
390 
391  //Valid Ethernet PHY or switch driver?
392  if(interface->phyDriver != NULL)
393  {
394  //Disable Ethernet PHY interrupts
395  interface->phyDriver->disableIrq(interface);
396  }
397  else if(interface->switchDriver != NULL)
398  {
399  //Disable Ethernet switch interrupts
400  interface->switchDriver->disableIrq(interface);
401  }
402  else
403  {
404  //Just for sanity
405  }
406 }
407 
408 
409 /**
410  * @brief F28M35x Ethernet MAC interrupt service routine
411  **/
412 
414 {
415  bool_t flag;
416  uint32_t status;
417 
418  //Interrupt service routine prologue
419  osEnterIsr();
420 
421  //This flag will be set if a higher priority task must be woken
422  flag = FALSE;
423 
424  //Read interrupt status register
425  status = MAC_RIS_R;
426 
427  //Transmit FIFO empty?
428  if((status & MAC_RIS_TXEMP) != 0)
429  {
430  //Acknowledge TXEMP interrupt
431  MAC_IACK_R = MAC_IACK_TXEMP;
432 
433  //Check whether the transmit FIFO is available for writing
434  if((MAC_TR_R & MAC_TR_NEWTX) == 0)
435  {
436  //Notify the TCP/IP stack that the transmitter is ready to send
437  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
438  }
439  }
440 
441  //Packet received?
442  if((status & MAC_RIS_RXINT) != 0)
443  {
444  //Disable RXINT interrupt
445  MAC_IM_R &= ~MAC_IM_RXINTM;
446 
447  //Set event flag
448  nicDriverInterface->nicEvent = TRUE;
449  //Notify the TCP/IP stack of the event
450  flag |= osSetEventFromIsr(&netEvent);
451  }
452 
453  //Interrupt service routine epilogue
454  osExitIsr(flag);
455 }
456 
457 
458 /**
459  * @brief F28M35x Ethernet MAC event handler
460  * @param[in] interface Underlying network interface
461  **/
462 
464 {
465  //Packet received?
466  if((MAC_RIS_R & MAC_RIS_RXINT) != 0)
467  {
468  //Acknowledge RXINT interrupt
469  MAC_IACK_R = MAC_IACK_RXINT;
470 
471  //Process all the pending packets
472  while((MAC_NP_R & MAC_NP_NPR_M) != 0)
473  {
474  //Read incoming packet
475  f28m35xEthReceivePacket(interface);
476  }
477  }
478 
479  //Re-enable Ethernet interrupts
480  MAC_IM_R = MAC_IM_TXEMPM | MAC_IM_RXINTM;
481 }
482 
483 
484 /**
485  * @brief Send a packet
486  * @param[in] interface Underlying network interface
487  * @param[in] buffer Multi-part buffer containing the data to send
488  * @param[in] offset Offset to the first data byte
489  * @param[in] ancillary Additional options passed to the stack along with
490  * the packet
491  * @return Error code
492  **/
493 
495  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
496 {
497  size_t i;
498  size_t length;
499  uint32_t *p;
500 
501  //Retrieve the length of the packet
502  length = netBufferGetLength(buffer) - offset;
503 
504  //Check the frame length
505  if(length < sizeof(EthHeader) || length > ETH_MAX_FRAME_SIZE)
506  {
507  //The transmitter can accept another packet
508  osSetEvent(&interface->nicTxEvent);
509  //Report an error
510  return ERROR_INVALID_LENGTH;
511  }
512 
513  //Make sure the transmit FIFO is available for writing
514  if((MAC_TR_R & MAC_TR_NEWTX) != 0)
515  {
516  return ERROR_FAILURE;
517  }
518 
519  //Copy user data
520  netBufferRead(txBuffer + 2, buffer, offset, length);
521 
522  //The packet is preceded by a 16-bit length field
523  txBuffer[0] = LSB(length - sizeof(EthHeader));
524  txBuffer[1] = MSB(length - sizeof(EthHeader));
525 
526  //Point to the beginning of the packet
527  p = (uint32_t *) txBuffer;
528  //Compute the length of the packet in 32-bit words
529  length = (length + 5) / 4;
530 
531  //Copy packet to transmit FIFO
532  for(i = 0; i < length; i++)
533  {
534  MAC_DATA_R = p[i];
535  }
536 
537  //Start transmitting
538  MAC_TR_R = MAC_TR_NEWTX;
539 
540  //Data successfully written
541  return NO_ERROR;
542 }
543 
544 
545 /**
546  * @brief Receive a packet
547  * @param[in] interface Underlying network interface
548  * @return Error code
549  **/
550 
552 {
553  error_t error;
554  size_t i;
555  size_t n;
556  size_t length;
557  uint32_t data;
558  uint16_t *p;
559 
560  //Make sure the FIFO is not empty
561  if((MAC_NP_R & MAC_NP_NPR_M) != 0)
562  {
563  //Read the first word
564  data = MAC_DATA_R;
565  //Retrieve the total length of the packet
566  length = data & 0xFFFF;
567 
568  //Make sure the length field is valid
569  if(length > 2)
570  {
571  //Point to the beginning of the buffer
572  p = (uint16_t *) rxBuffer;
573 
574  //Retrieve the length of the frame
575  length -= 2;
576  //Limit the number of data to be read
578 
579  //Copy the first half word
580  if(n > 0)
581  {
582  *(p++) = (uint16_t) (data >> 16);
583  }
584 
585  //Copy data from receive FIFO
586  for(i = 2; i < n; i += 4)
587  {
588  //Read a 32-bit word from the FIFO
589  data = MAC_DATA_R;
590  //Write the 32-bit to the receive buffer
591  *(p++) = (uint16_t) data;
592  *(p++) = (uint16_t) (data >> 16);
593  }
594 
595  //Skip the remaining bytes
596  while(i < length)
597  {
598  //Read a 32-bit word from the FIFO
599  data = MAC_DATA_R;
600  //Increment byte counter
601  i += 4;
602  }
603 
604  //Valid packet received
605  error = NO_ERROR;
606  }
607  else
608  {
609  //Disable receiver
610  MAC_RCTL_R &= ~MAC_RCTL_RXEN;
611  //Flush the receive FIFO
612  MAC_RCTL_R |= MAC_RCTL_RSTFIFO;
613  //Re-enable receiver
614  MAC_RCTL_R |= MAC_RCTL_RXEN;
615 
616  //The packet is not valid
617  error = ERROR_INVALID_PACKET;
618  }
619  }
620  else
621  {
622  //No more data in the receive buffer
623  error = ERROR_BUFFER_EMPTY;
624  }
625 
626  //Check whether a valid packet has been received
627  if(!error)
628  {
629  NetRxAncillary ancillary;
630 
631  //Additional options can be passed to the stack along with the packet
632  ancillary = NET_DEFAULT_RX_ANCILLARY;
633 
634  //Pass the packet to the upper layer
635  nicProcessPacket(interface, rxBuffer, n, &ancillary);
636  }
637 
638  //Return status code
639  return error;
640 }
641 
642 
643 /**
644  * @brief Configure MAC address filtering
645  * @param[in] interface Underlying network interface
646  * @return Error code
647  **/
648 
650 {
651  uint_t i;
652  bool_t acceptMulticast;
653 
654  //Debug message
655  TRACE_DEBUG("Updating MAC filter...\r\n");
656 
657  //Set the MAC address of the station
658  MAC_IA0_R = interface->macAddr.w[0] | (interface->macAddr.w[1] << 16);
659  MAC_IA1_R = interface->macAddr.w[2];
660 
661  //This flag will be set if multicast addresses should be accepted
662  acceptMulticast = FALSE;
663 
664  //The MAC address filter contains the list of MAC addresses to accept
665  //when receiving an Ethernet frame
666  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
667  {
668  //Valid entry?
669  if(interface->macAddrFilter[i].refCount > 0)
670  {
671  //Accept multicast addresses
672  acceptMulticast = TRUE;
673  //We are done
674  break;
675  }
676  }
677 
678  //Enable or disable the reception of multicast frames
679  if(acceptMulticast)
680  {
681  MAC_RCTL_R |= MAC_RCTL_AMUL;
682  }
683  else
684  {
685  MAC_RCTL_R &= ~MAC_RCTL_AMUL;
686  }
687 
688  //Successful processing
689  return NO_ERROR;
690 }
691 
692 
693 /**
694  * @brief Adjust MAC configuration parameters for proper operation
695  * @param[in] interface Underlying network interface
696  * @return Error code
697  **/
698 
700 {
701  //Half-duplex or full-duplex mode?
702  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
703  {
704  MAC_TCTL_R |= MAC_TCTL_DUPLEX;
705  }
706  else
707  {
708  MAC_TCTL_R &= ~MAC_TCTL_DUPLEX;
709  }
710 
711  //Successful processing
712  return NO_ERROR;
713 }
714 
715 
716 /**
717  * @brief Write PHY register
718  * @param[in] opcode Access type (2 bits)
719  * @param[in] phyAddr PHY address (5 bits)
720  * @param[in] regAddr Register address (5 bits)
721  * @param[in] data Register value
722  **/
723 
724 void f28m35xEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
725  uint8_t regAddr, uint16_t data)
726 {
727  //Valid opcode?
728  if(opcode == SMI_OPCODE_WRITE)
729  {
730  //Set PHY address
731  MAC_MAR_R = phyAddr;
732  //Data to be written in the PHY register
733  MAC_MTXD_R = data & MAC_MTXD_MDTX_M;
734  //Start a write operation
735  MAC_MCTL_R = (regAddr << 3) | MAC_MCTL_WRITE | MAC_MCTL_START;
736 
737  //Wait for the write to complete
738  while((MAC_MCTL_R & MAC_MCTL_START) != 0)
739  {
740  }
741  }
742  else
743  {
744  //The MAC peripheral only supports standard Clause 22 opcodes
745  }
746 }
747 
748 
749 /**
750  * @brief Read PHY register
751  * @param[in] opcode Access type (2 bits)
752  * @param[in] phyAddr PHY address (5 bits)
753  * @param[in] regAddr Register address (5 bits)
754  * @return Register value
755  **/
756 
757 uint16_t f28m35xEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
758  uint8_t regAddr)
759 {
760  uint16_t data;
761 
762  //Valid opcode?
763  if(opcode == SMI_OPCODE_READ)
764  {
765  //Set PHY address
766  MAC_MAR_R = phyAddr;
767  //Start a read operation
768  MAC_MCTL_R = (regAddr << 3) | MAC_MCTL_START;
769 
770  //Wait for the read to complete
771  while((MAC_MCTL_R & MAC_MCTL_START) != 0)
772  {
773  }
774 
775  //Get register value
776  data = MAC_MRXD_R & MAC_MRXD_MDRX_M;
777  }
778  else
779  {
780  //The MAC peripheral only supports standard Clause 22 opcodes
781  data = 0;
782  }
783 
784  //Return the value of the PHY register
785  return data;
786 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
error_t f28m35xEthReceivePacket(NetInterface *interface)
Receive a packet.
uint8_t opcode
Definition: dns_common.h:188
int bool_t
Definition: compiler_port.h:53
#define F28M35X_ETH_IRQ_PRIORITY_GROUPING
__weak_func void f28m35xEthInitGpio(NetInterface *interface)
GPIO configuration.
#define netEvent
Definition: net_legacy.h:196
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define MAC_TR_R
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 MAC_RCTL_R
uint8_t p
Definition: ndp.h:300
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
error_t f28m35xEthInit(NetInterface *interface)
F28M35x Ethernet MAC driver initialization.
#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
void f28m35xEthEventHandler(NetInterface *interface)
F28M35x Ethernet MAC event handler.
const NicDriver f28m35xEthDriver
F28M35x Ethernet MAC driver.
uint16_t f28m35xEthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define MAC_IM_R
void f28m35xEthIrqHandler(void)
F28M35x Ethernet MAC interrupt service routine.
EthHeader
Definition: ethernet.h:223
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:392
void f28m35xEthDisableIrq(NetInterface *interface)
Disable interrupts.
#define osExitIsr(flag)
#define SMI_OPCODE_WRITE
Definition: nic.h:66
error_t f28m35xEthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define MAC_NP_R
#define FALSE
Definition: os_port.h:46
#define MAC_MTXD_R
error_t
Error codes.
Definition: error.h:43
#define MAC_RIS_R
error_t f28m35xEthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
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
@ ERROR_INVALID_PACKET
Definition: error.h:140
#define NetInterface
Definition: net.h:36
@ ERROR_INVALID_LENGTH
Definition: error.h:111
error_t f28m35xEthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
@ ERROR_BUFFER_EMPTY
Definition: error.h:141
#define NetTxAncillary
Definition: net_misc.h:36
#define MAC_MAR_R
#define MSB(x)
Definition: os_port.h:59
#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 LSB(x)
Definition: os_port.h:55
#define MIN(a, b)
Definition: os_port.h:63
#define rxBuffer
void f28m35xEthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
#define MAC_DATA_R
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define MAC_IACK_R
uint16_t regAddr
#define ETH_MTU
Definition: ethernet.h:116
uint8_t n
#define MAC_IA1_R
F28M35x Ethernet MAC driver.
#define osEnterIsr()
#define MAC_MDV_R
void f28m35xEthEnableIrq(NetInterface *interface)
Enable interrupts.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define MAC_MCTL_R
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
#define MAC_MRXD_R
#define F28M35X_ETH_IRQ_PRIORITY
NIC driver.
Definition: nic.h:286
#define MAC_TCTL_R
#define MAC_IA0_R
@ 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
void f28m35xEthTick(NetInterface *interface)
F28M35x Ethernet MAC timer handler.