lpc176x_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file lpc176x_eth_driver.c
3  * @brief LPC1764/66/67/68/69 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 "lpc17xx.h"
36 #include "core/net.h"
38 #include "debug.h"
39 
40 //Underlying network interface
41 static NetInterface *nicDriverInterface;
42 
43 //IAR EWARM compiler?
44 #if defined(__ICCARM__)
45 
46 //Transmit buffer
47 #pragma data_alignment = 4
49 //Receive buffer
50 #pragma data_alignment = 4
52 //Transmit descriptors
53 #pragma data_alignment = 4
55 //Transmit status array
56 #pragma data_alignment = 4
58 //Receive descriptors
59 #pragma data_alignment = 4
61 //Receive status array
62 #pragma data_alignment = 8
64 
65 //Keil MDK-ARM or GCC compiler?
66 #else
67 
68 //Transmit buffer
70  __attribute__((aligned(4)));
71 //Receive buffer
73  __attribute__((aligned(4)));
74 //Transmit descriptors
76  __attribute__((aligned(4)));
77 //Transmit status array
79  __attribute__((aligned(4)));
80 //Receive descriptors
82  __attribute__((aligned(4)));
83 //Receive status array
85  __attribute__((aligned(8)));
86 
87 #endif
88 
89 
90 /**
91  * @brief LPC176x Ethernet MAC driver
92  **/
93 
95 {
97  ETH_MTU,
108  TRUE,
109  TRUE,
110  TRUE,
111  FALSE
112 };
113 
114 
115 /**
116  * @brief LPC176x Ethernet MAC initialization
117  * @param[in] interface Underlying network interface
118  * @return Error code
119  **/
120 
122 {
123  error_t error;
124 
125  //Debug message
126  TRACE_INFO("Initializing LPC176x Ethernet MAC...\r\n");
127 
128  //Save underlying network interface
129  nicDriverInterface = interface;
130 
131  //Power up EMAC controller
132  LPC_SC->PCONP |= PCONP_PCENET;
133 
134  //GPIO configuration
135  lpc176xEthInitGpio(interface);
136 
137  //Reset host registers, transmit datapath and receive datapath
138  LPC_EMAC->Command = COMMAND_RX_RESET | COMMAND_TX_RESET | COMMAND_REG_RESET;
139 
140  //Reset EMAC controller
141  LPC_EMAC->MAC1 = MAC1_SOFT_RESET | MAC1_SIMULATION_RESET |
143 
144  //Initialize MAC related registers
145  LPC_EMAC->MAC1 = 0;
146  LPC_EMAC->MAC2 = MAC2_PAD_CRC_ENABLE | MAC2_CRC_ENABLE;
147  LPC_EMAC->IPGR = IPGR_DEFAULT_VALUE;
148  LPC_EMAC->CLRT = CLRT_DEFAULT_VALUE;
149 
150  //Select RMII mode
151  LPC_EMAC->Command = COMMAND_RMII;
152 
153  //Configure MDC clock
154  LPC_EMAC->MCFG = MCFG_CLOCK_SELECT_DIV44;
155  //Reset MII management interface
156  LPC_EMAC->MCFG |= MCFG_RESET_MII_MGMT;
157  LPC_EMAC->MCFG &= ~MCFG_RESET_MII_MGMT;
158 
159  //Valid Ethernet PHY or switch driver?
160  if(interface->phyDriver != NULL)
161  {
162  //Ethernet PHY initialization
163  error = interface->phyDriver->init(interface);
164  }
165  else if(interface->switchDriver != NULL)
166  {
167  //Ethernet switch initialization
168  error = interface->switchDriver->init(interface);
169  }
170  else
171  {
172  //The interface is not properly configured
173  error = ERROR_FAILURE;
174  }
175 
176  //Any error to report?
177  if(error)
178  {
179  return error;
180  }
181 
182  //Initialize TX and RX descriptor arrays
183  lpc176xEthInitDesc(interface);
184 
185  //Set the MAC address of the station
186  LPC_EMAC->SA0 = interface->macAddr.w[2];
187  LPC_EMAC->SA1 = interface->macAddr.w[1];
188  LPC_EMAC->SA2 = interface->macAddr.w[0];
189 
190  //Initialize hash table
191  LPC_EMAC->HashFilterL = 0;
192  LPC_EMAC->HashFilterH = 0;
193 
194  //Configure the receive filter
195  LPC_EMAC->RxFilterCtrl = RFC_ACCEPT_PERFECT_EN |
197 
198  //Program the MAXF register with the maximum frame length to be accepted
199  LPC_EMAC->MAXF = LPC176X_ETH_RX_BUFFER_SIZE;
200 
201  //Reset EMAC interrupt flags
202  LPC_EMAC->IntClear = 0xFFFF;
203  //Enable desired EMAC interrupts
204  LPC_EMAC->IntEnable = INT_TX_DONE | INT_RX_DONE;
205 
206  //Set priority grouping (5 bits for pre-emption priority, no bits for subpriority)
207  NVIC_SetPriorityGrouping(LPC176X_ETH_IRQ_PRIORITY_GROUPING);
208 
209  //Configure Ethernet interrupt priority
210  NVIC_SetPriority(ENET_IRQn, NVIC_EncodePriority(LPC176X_ETH_IRQ_PRIORITY_GROUPING,
212 
213  //Enable transmission and reception
214  LPC_EMAC->Command |= COMMAND_TX_ENABLE | COMMAND_RX_ENABLE;
215  //Allow frames to be received
216  LPC_EMAC->MAC1 |= MAC1_RECEIVE_ENABLE;
217 
218  //Accept any packets from the upper layer
219  osSetEvent(&interface->nicTxEvent);
220 
221  //Successful initialization
222  return NO_ERROR;
223 }
224 
225 
226 /**
227  * @brief GPIO configuration
228  * @param[in] interface Underlying network interface
229  **/
230 
231 __weak_func void lpc176xEthInitGpio(NetInterface *interface)
232 {
233 //LPC1766-STK or LPCXpresso1769 evaluation board?
234 #if defined(USE_LPC1766_STK) || defined(USE_LPCXPRESSO_1769)
235  //Configure P1.0 (ENET_TXD0), P1.1 (ENET_TXD1), P1.4 (ENET_TX_EN), P1.8 (ENET_CRS),
236  //P1.9 (ENET_RXD0), P1.10 (ENET_RXD1), P1.14 (RX_ER) and P1.15 (ENET_REF_CLK)
237  LPC_PINCON->PINSEL2 &= ~(PINSEL2_P1_0_MASK | PINSEL2_P1_1_MASK |
238  PINSEL2_P1_4_MASK | PINSEL2_P1_8_MASK | PINSEL2_P1_9_MASK |
239  PINSEL2_P1_10_MASK | PINSEL2_P1_14_MASK | PINSEL2_P1_15_MASK);
240 
241  LPC_PINCON->PINSEL2 |= PINSEL2_P1_0_ENET_TXD0 | PINSEL2_P1_1_ENET_TXD1 |
242  PINSEL2_P1_4_ENET_TX_EN | PINSEL2_P1_8_ENET_CRS | PINSEL2_P1_9_ENET_RXD0 |
243  PINSEL2_P1_10_ENET_RXD1 | PINSEL2_P1_14_ENET_RX_ER | PINSEL2_P1_15_ENET_REF_CLK;
244 
245  //Configure P1.16 (ENET_MDC) and P1.17 (ENET_MDIO)
246  LPC_PINCON->PINSEL3 &= ~(PINSEL3_P1_16_MASK | PINSEL3_P1_17_MASK);
247  LPC_PINCON->PINSEL3 |= PINSEL3_P1_16_ENET_MDC | PINSEL3_P1_17_ENET_MDIO;
248 #endif
249 }
250 
251 
252 /**
253  * @brief Initialize TX and RX descriptors
254  * @param[in] interface Underlying network interface
255  **/
256 
258 {
259  uint_t i;
260 
261  //Initialize TX descriptors
262  for(i = 0; i < LPC176X_ETH_TX_BUFFER_COUNT; i++)
263  {
264  //Base address of the buffer containing transmit data
265  txDesc[i].packet = (uint32_t) txBuffer[i];
266  //Transmit descriptor control word
267  txDesc[i].control = 0;
268  //Transmit status information word
269  txStatus[i].info = 0;
270  }
271 
272  //Initialize RX descriptors
273  for(i = 0; i < LPC176X_ETH_RX_BUFFER_COUNT; i++)
274  {
275  //Base address of the buffer for storing receive data
276  rxDesc[i].packet = (uint32_t) rxBuffer[i];
277  //Receive descriptor control word
279  //Receive status information word
280  rxStatus[i].info = 0;
281  //Receive status HashCRC word
282  rxStatus[i].hashCrc = 0;
283  }
284 
285  //Initialize EMAC transmit descriptor registers
286  LPC_EMAC->TxDescriptor = (uint32_t) txDesc;
287  LPC_EMAC->TxStatus = (uint32_t) txStatus;
288  LPC_EMAC->TxDescriptorNumber = LPC176X_ETH_TX_BUFFER_COUNT - 1;
289  LPC_EMAC->TxProduceIndex = 0;
290 
291  //Initialize EMAC receive descriptor registers
292  LPC_EMAC->RxDescriptor = (uint32_t) rxDesc;
293  LPC_EMAC->RxStatus = (uint32_t) rxStatus;
294  LPC_EMAC->RxDescriptorNumber = LPC176X_ETH_RX_BUFFER_COUNT - 1;
295  LPC_EMAC->RxConsumeIndex = 0;
296 }
297 
298 
299 /**
300  * @brief LPC176x Ethernet MAC timer handler
301  *
302  * This routine is periodically called by the TCP/IP stack to handle periodic
303  * operations such as polling the link state
304  *
305  * @param[in] interface Underlying network interface
306  **/
307 
308 void lpc176xEthTick(NetInterface *interface)
309 {
310  //Valid Ethernet PHY or switch driver?
311  if(interface->phyDriver != NULL)
312  {
313  //Handle periodic operations
314  interface->phyDriver->tick(interface);
315  }
316  else if(interface->switchDriver != NULL)
317  {
318  //Handle periodic operations
319  interface->switchDriver->tick(interface);
320  }
321  else
322  {
323  //Just for sanity
324  }
325 }
326 
327 
328 /**
329  * @brief Enable interrupts
330  * @param[in] interface Underlying network interface
331  **/
332 
334 {
335  //Enable Ethernet MAC interrupts
336  NVIC_EnableIRQ(ENET_IRQn);
337 
338  //Valid Ethernet PHY or switch driver?
339  if(interface->phyDriver != NULL)
340  {
341  //Enable Ethernet PHY interrupts
342  interface->phyDriver->enableIrq(interface);
343  }
344  else if(interface->switchDriver != NULL)
345  {
346  //Enable Ethernet switch interrupts
347  interface->switchDriver->enableIrq(interface);
348  }
349  else
350  {
351  //Just for sanity
352  }
353 }
354 
355 
356 /**
357  * @brief Disable interrupts
358  * @param[in] interface Underlying network interface
359  **/
360 
362 {
363  //Disable Ethernet MAC interrupts
364  NVIC_DisableIRQ(ENET_IRQn);
365 
366  //Valid Ethernet PHY or switch driver?
367  if(interface->phyDriver != NULL)
368  {
369  //Disable Ethernet PHY interrupts
370  interface->phyDriver->disableIrq(interface);
371  }
372  else if(interface->switchDriver != NULL)
373  {
374  //Disable Ethernet switch interrupts
375  interface->switchDriver->disableIrq(interface);
376  }
377  else
378  {
379  //Just for sanity
380  }
381 }
382 
383 
384 /**
385  * @brief LPC176x Ethernet MAC interrupt service routine
386  **/
387 
388 void ENET_IRQHandler(void)
389 {
390  uint_t i;
391  bool_t flag;
392  uint32_t status;
393 
394  //Interrupt service routine prologue
395  osEnterIsr();
396 
397  //This flag will be set if a higher priority task must be woken
398  flag = FALSE;
399 
400  //Read interrupt status register
401  status = LPC_EMAC->IntStatus;
402 
403  //Packet transmitted?
404  if((status & INT_TX_DONE) != 0)
405  {
406  //Clear TxDone interrupt flag
407  LPC_EMAC->IntClear = INT_TX_DONE;
408 
409  //Get the index of the next descriptor
410  i = LPC_EMAC->TxProduceIndex + 1;
411 
412  //Wrap around if necessary
414  {
415  i = 0;
416  }
417 
418  //Check whether the TX buffer is available for writing
419  if(i != LPC_EMAC->TxConsumeIndex)
420  {
421  //Notify the TCP/IP stack that the transmitter is ready to send
422  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
423  }
424  }
425 
426  //Packet received?
427  if((status & INT_RX_DONE) != 0)
428  {
429  //Disable RxDone interrupts
430  LPC_EMAC->IntEnable &= ~INT_RX_DONE;
431 
432  //Set event flag
433  nicDriverInterface->nicEvent = TRUE;
434  //Notify the TCP/IP stack of the event
435  flag |= osSetEventFromIsr(&netEvent);
436  }
437 
438  //Interrupt service routine epilogue
439  osExitIsr(flag);
440 }
441 
442 
443 /**
444  * @brief LPC176x Ethernet MAC event handler
445  * @param[in] interface Underlying network interface
446  **/
447 
449 {
450  error_t error;
451 
452  //Packet received?
453  if((LPC_EMAC->IntStatus & INT_RX_DONE) != 0)
454  {
455  //Clear RxDone interrupt flag
456  LPC_EMAC->IntClear = INT_RX_DONE;
457 
458  //Process all pending packets
459  do
460  {
461  //Read incoming packet
462  error = lpc176xEthReceivePacket(interface);
463 
464  //No more data in the receive buffer?
465  } while(error != ERROR_BUFFER_EMPTY);
466  }
467 
468  //Re-enable TxDone and RxDone interrupts
469  LPC_EMAC->IntEnable = INT_TX_DONE | INT_RX_DONE;
470 }
471 
472 
473 /**
474  * @brief Send a packet
475  * @param[in] interface Underlying network interface
476  * @param[in] buffer Multi-part buffer containing the data to send
477  * @param[in] offset Offset to the first data byte
478  * @param[in] ancillary Additional options passed to the stack along with
479  * the packet
480  * @return Error code
481  **/
482 
484  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
485 {
486  uint_t i;
487  uint_t j;
488  size_t length;
489 
490  //Retrieve the length of the packet
491  length = netBufferGetLength(buffer) - offset;
492 
493  //Check the frame length
494  if(!length)
495  {
496  //The transmitter can accept another packet
497  osSetEvent(&interface->nicTxEvent);
498  //We are done since the buffer is empty
499  return NO_ERROR;
500  }
502  {
503  //The transmitter can accept another packet
504  osSetEvent(&interface->nicTxEvent);
505  //Report an error
506  return ERROR_INVALID_LENGTH;
507  }
508 
509  //Get the index of the current descriptor
510  i = LPC_EMAC->TxProduceIndex;
511  //Get the index of the next descriptor
512  j = i + 1;
513 
514  //Wrap around if necessary
516  {
517  j = 0;
518  }
519 
520  //Check whether the transmit descriptor array is full
521  if(j == LPC_EMAC->TxConsumeIndex)
522  {
523  return ERROR_FAILURE;
524  }
525 
526  //Copy user data to the transmit buffer
527  netBufferRead((uint8_t *) txDesc[i].packet, buffer, offset, length);
528 
529  //Write the transmit control word
530  txDesc[i].control = TX_CTRL_INTERRUPT | TX_CTRL_LAST |
532 
533  //Increment index and wrap around if necessary
534  if(++i >= LPC176X_ETH_TX_BUFFER_COUNT)
535  {
536  i = 0;
537  }
538 
539  //Save the resulting value
540  LPC_EMAC->TxProduceIndex = i;
541 
542  //Get the index of the next descriptor
543  j = i + 1;
544 
545  //Wrap around if necessary
547  {
548  j = 0;
549  }
550 
551  //Check whether the next buffer is available for writing
552  if(j != LPC_EMAC->TxConsumeIndex)
553  {
554  //The transmitter can accept another packet
555  osSetEvent(&interface->nicTxEvent);
556  }
557 
558  //Successful write operation
559  return NO_ERROR;
560 }
561 
562 
563 /**
564  * @brief Receive a packet
565  * @param[in] interface Underlying network interface
566  * @return Error code
567  **/
568 
570 {
571  error_t error;
572  size_t n;
573  uint_t i;
574  NetRxAncillary ancillary;
575 
576  //Point to the current descriptor
577  i = LPC_EMAC->RxConsumeIndex;
578 
579  //Current buffer available for reading?
580  if(i != LPC_EMAC->RxProduceIndex)
581  {
582  //Retrieve the length of the frame
583  n = (rxStatus[i].info & RX_STATUS_SIZE) + 1;
584  //Limit the number of data to read
586 
587  //Additional options can be passed to the stack along with the packet
588  ancillary = NET_DEFAULT_RX_ANCILLARY;
589 
590  //Pass the packet to the upper layer
591  nicProcessPacket(interface, (uint8_t *) rxDesc[i].packet, n, &ancillary);
592 
593  //Increment index and wrap around if necessary
594  if(++i >= LPC176X_ETH_RX_BUFFER_COUNT)
595  {
596  i = 0;
597  }
598 
599  //Save the resulting value
600  LPC_EMAC->RxConsumeIndex = i;
601 
602  //Valid packet received
603  error = NO_ERROR;
604  }
605  else
606  {
607  //No more data in the receive buffer
608  error = ERROR_BUFFER_EMPTY;
609  }
610 
611  //Return status code
612  return error;
613 }
614 
615 
616 /**
617  * @brief Configure MAC address filtering
618  * @param[in] interface Underlying network interface
619  * @return Error code
620  **/
621 
623 {
624  uint_t i;
625  uint_t k;
626  uint32_t crc;
627  uint32_t hashTable[2];
628  MacFilterEntry *entry;
629 
630  //Debug message
631  TRACE_DEBUG("Updating MAC filter...\r\n");
632 
633  //Set the MAC address of the station
634  LPC_EMAC->SA0 = interface->macAddr.w[2];
635  LPC_EMAC->SA1 = interface->macAddr.w[1];
636  LPC_EMAC->SA2 = interface->macAddr.w[0];
637 
638  //Clear hash table
639  hashTable[0] = 0;
640  hashTable[1] = 0;
641 
642  //The MAC address filter contains the list of MAC addresses to accept
643  //when receiving an Ethernet frame
644  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
645  {
646  //Point to the current entry
647  entry = &interface->macAddrFilter[i];
648 
649  //Valid entry?
650  if(entry->refCount > 0)
651  {
652  //Compute CRC over the current MAC address
653  crc = lpc176xEthCalcCrc(&entry->addr, sizeof(MacAddr));
654  //Bits [28:23] are used to form the hash
655  k = (crc >> 23) & 0x3F;
656  //Update hash table contents
657  hashTable[k / 32] |= (1 << (k % 32));
658  }
659  }
660 
661  //Write the hash table
662  LPC_EMAC->HashFilterL = hashTable[0];
663  LPC_EMAC->HashFilterH = hashTable[1];
664 
665  //Debug message
666  TRACE_DEBUG(" HashFilterL = %08" PRIX32 "\r\n", LPC_EMAC->HashFilterL);
667  TRACE_DEBUG(" HashFilterH = %08" PRIX32 "\r\n", LPC_EMAC->HashFilterH);
668 
669  //Successful processing
670  return NO_ERROR;
671 }
672 
673 
674 /**
675  * @brief Adjust MAC configuration parameters for proper operation
676  * @param[in] interface Underlying network interface
677  * @return Error code
678  **/
679 
681 {
682  //10BASE-T or 100BASE-TX operation mode?
683  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
684  {
685  LPC_EMAC->SUPP = SUPP_SPEED;
686  }
687  else
688  {
689  LPC_EMAC->SUPP = 0;
690  }
691 
692  //Half-duplex or full-duplex mode?
693  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
694  {
695  //The MAC operates in full-duplex mode
696  LPC_EMAC->MAC2 |= MAC2_FULL_DUPLEX;
697  LPC_EMAC->Command |= COMMAND_FULL_DUPLEX;
698  //Configure Back-to-Back Inter-Packet Gap
699  LPC_EMAC->IPGT = IPGT_FULL_DUPLEX;
700  }
701  else
702  {
703  //The MAC operates in half-duplex mode
704  LPC_EMAC->MAC2 &= ~MAC2_FULL_DUPLEX;
705  LPC_EMAC->Command &= ~COMMAND_FULL_DUPLEX;
706  //Configure Back-to-Back Inter-Packet Gap
707  LPC_EMAC->IPGT = IPGT_HALF_DUPLEX;
708  }
709 
710  //Successful processing
711  return NO_ERROR;
712 }
713 
714 
715 /**
716  * @brief Write PHY register
717  * @param[in] opcode Access type (2 bits)
718  * @param[in] phyAddr PHY address (5 bits)
719  * @param[in] regAddr Register address (5 bits)
720  * @param[in] data Register value
721  **/
722 
723 void lpc176xEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
724  uint8_t regAddr, uint16_t data)
725 {
726  //Valid opcode?
727  if(opcode == SMI_OPCODE_WRITE)
728  {
729  //Clear MCMD register
730  LPC_EMAC->MCMD = 0;
731 
732  //PHY address
733  LPC_EMAC->MADR = (phyAddr << 8) & MADR_PHY_ADDRESS;
734  //Register address
735  LPC_EMAC->MADR |= regAddr & MADR_REGISTER_ADDRESS;
736  //Data to be written in the PHY register
737  LPC_EMAC->MWTD = data & MWTD_WRITE_DATA;
738 
739  //Wait for the write to complete
740  while((LPC_EMAC->MIND & MIND_BUSY) != 0)
741  {
742  }
743  }
744  else
745  {
746  //The MAC peripheral only supports standard Clause 22 opcodes
747  }
748 }
749 
750 
751 /**
752  * @brief Read PHY register
753  * @param[in] opcode Access type (2 bits)
754  * @param[in] phyAddr PHY address (5 bits)
755  * @param[in] regAddr Register address (5 bits)
756  * @return Register value
757  **/
758 
759 uint16_t lpc176xEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
760  uint8_t regAddr)
761 {
762  uint16_t data;
763 
764  //Valid opcode?
765  if(opcode == SMI_OPCODE_READ)
766  {
767  //PHY address
768  LPC_EMAC->MADR = (phyAddr << 8) & MADR_PHY_ADDRESS;
769  //Register address
770  LPC_EMAC->MADR |= regAddr & MADR_REGISTER_ADDRESS;
771 
772  //Start a read operation
773  LPC_EMAC->MCMD = MCMD_READ;
774  //Wait for the read to complete
775  while((LPC_EMAC->MIND & MIND_BUSY) != 0)
776  {
777  }
778 
779  //Clear MCMD register
780  LPC_EMAC->MCMD = 0;
781 
782  //Get register value
783  data = LPC_EMAC->MRDD & MRDD_READ_DATA;
784  }
785  else
786  {
787  //The MAC peripheral only supports standard Clause 22 opcodes
788  data = 0;
789  }
790 
791  //Return the value of the PHY register
792  return data;
793 }
794 
795 
796 /**
797  * @brief CRC calculation
798  * @param[in] data Pointer to the data over which to calculate the CRC
799  * @param[in] length Number of bytes to process
800  * @return Resulting CRC value
801  **/
802 
803 uint32_t lpc176xEthCalcCrc(const void *data, size_t length)
804 {
805  uint_t i;
806  uint_t j;
807  uint32_t crc;
808  const uint8_t *p;
809 
810  //Point to the data over which to calculate the CRC
811  p = (uint8_t *) data;
812  //CRC preset value
813  crc = 0xFFFFFFFF;
814 
815  //Loop through data
816  for(i = 0; i < length; i++)
817  {
818  //The message is processed bit by bit
819  for(j = 0; j < 8; j++)
820  {
821  //Update CRC value
822  if((((crc >> 31) ^ (p[i] >> j)) & 0x01) != 0)
823  {
824  crc = (crc << 1) ^ 0x04C11DB7;
825  }
826  else
827  {
828  crc = crc << 1;
829  }
830  }
831  }
832 
833  //Return CRC value
834  return crc;
835 }
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
Transmit descriptor.
#define MADR_PHY_ADDRESS
LPC1764/66/67/68/69 Ethernet MAC driver.
#define MCFG_CLOCK_SELECT_DIV44
uint8_t opcode
Definition: dns_common.h:188
int bool_t
Definition: compiler_port.h:53
#define netEvent
Definition: net_legacy.h:196
#define TX_CTRL_SIZE
#define LPC176X_ETH_IRQ_SUB_PRIORITY
@ 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
void lpc176xEthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
uint8_t p
Definition: ndp.h:300
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
error_t lpc176xEthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define IPGR_DEFAULT_VALUE
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:222
#define COMMAND_TX_ENABLE
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
#define IPGT_FULL_DUPLEX
uint32_t lpc176xEthCalcCrc(const void *data, size_t length)
CRC calculation.
#define MAC1_SOFT_RESET
#define TX_CTRL_CRC
#define COMMAND_RMII
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 TX_CTRL_INTERRUPT
__weak_func void lpc176xEthInitGpio(NetInterface *interface)
GPIO configuration.
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define RX_CTRL_INTERRUPT
#define MAC1_RESET_TX
#define MAC1_RECEIVE_ENABLE
#define SUPP_SPEED
#define MWTD_WRITE_DATA
#define MAC2_FULL_DUPLEX
#define CLRT_DEFAULT_VALUE
#define FALSE
Definition: os_port.h:46
#define MAC1_SIMULATION_RESET
void lpc176xEthTick(NetInterface *interface)
LPC176x Ethernet MAC timer handler.
error_t
Error codes.
Definition: error.h:43
#define COMMAND_RX_RESET
#define COMMAND_RX_ENABLE
Receive status.
Transmit status.
error_t lpc176xEthInit(NetInterface *interface)
LPC176x Ethernet MAC initialization.
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:104
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
Receive descriptor.
#define LPC176X_ETH_TX_BUFFER_COUNT
#define RFC_ACCEPT_BROADCAST_EN
#define txBuffer
void lpc176xEthDisableIrq(NetInterface *interface)
Disable interrupts.
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:36
#define INT_TX_DONE
MacAddr addr
MAC address.
Definition: ethernet.h:263
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_BUFFER_EMPTY
Definition: error.h:141
#define NetTxAncillary
Definition: net_misc.h:36
#define SMI_OPCODE_READ
Definition: nic.h:67
#define COMMAND_TX_RESET
#define MAC1_RESET_MCS_TX
error_t lpc176xEthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
#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 MAC2_PAD_CRC_ENABLE
#define COMMAND_FULL_DUPLEX
#define MIN(a, b)
Definition: os_port.h:63
#define TX_CTRL_LAST
#define TX_CTRL_PAD
#define MAC1_RESET_MCS_RX
#define rxBuffer
uint16_t lpc176xEthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
MacAddr
Definition: ethernet.h:195
#define LPC176X_ETH_RX_BUFFER_SIZE
void lpc176xEthInitDesc(NetInterface *interface)
Initialize TX and RX descriptors.
#define MIND_BUSY
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define LPC176X_ETH_TX_BUFFER_SIZE
uint16_t regAddr
#define LPC176X_ETH_IRQ_GROUP_PRIORITY
#define RX_STATUS_SIZE
#define ETH_MTU
Definition: ethernet.h:116
uint8_t n
MAC filter table entry.
Definition: ethernet.h:262
#define MRDD_READ_DATA
#define osEnterIsr()
#define MAC2_CRC_ENABLE
const NicDriver lpc176xEthDriver
LPC176x Ethernet MAC driver.
error_t lpc176xEthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
#define LPC176X_ETH_IRQ_PRIORITY_GROUPING
#define IPGT_HALF_DUPLEX
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define MCFG_RESET_MII_MGMT
#define MCMD_READ
#define RFC_ACCEPT_MULTICAST_HASH_EN
#define INT_RX_DONE
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
void ENET_IRQHandler(void)
LPC176x Ethernet MAC interrupt service routine.
#define COMMAND_REG_RESET
NIC driver.
Definition: nic.h:286
void lpc176xEthEnableIrq(NetInterface *interface)
Enable interrupts.
#define MADR_REGISTER_ADDRESS
#define RFC_ACCEPT_PERFECT_EN
#define MAC1_RESET_RX
error_t lpc176xEthReceivePacket(NetInterface *interface)
Receive a packet.
#define LPC176X_ETH_RX_BUFFER_COUNT
@ NO_ERROR
Success.
Definition: error.h:44
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
Debugging facilities.
void lpc176xEthEventHandler(NetInterface *interface)
LPC176x Ethernet MAC event handler.
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83