lpc178x_eth_driver.c
Go to the documentation of this file.
1 /**
2  * @file lpc178x_eth_driver.c
3  * @brief LPC1786/88 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.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include "lpc177x_8x.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 LPC178x Ethernet MAC driver
92  **/
93 
95 {
97  ETH_MTU,
108  TRUE,
109  TRUE,
110  TRUE,
111  FALSE
112 };
113 
114 
115 /**
116  * @brief LPC178x 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 LPC178x 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  lpc178xEthInitGpio(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_DIV48;
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  lpc178xEthInitDesc(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 = LPC178X_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(LPC178X_ETH_IRQ_PRIORITY_GROUPING);
208 
209  //Configure Ethernet interrupt priority
210  NVIC_SetPriority(ENET_IRQn, NVIC_EncodePriority(LPC178X_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 lpc178xEthInitGpio(NetInterface *interface)
232 {
233 //LPC1788-32 Developer's Kit?
234 #if defined(USE_LPC1788_32_DEV_KIT)
235  //Power up GPIO
236  LPC_SC->PCONP |= PCONP_PCGPIO;
237 
238  //Configure P1.0 (ENET_TXD0)
239  LPC_IOCON->P1_0 = IOCON_SLEW | IOCON_FUNC_1;
240  //Configure P1.1 (ENET_TXD1)
241  LPC_IOCON->P1_1 = IOCON_SLEW | IOCON_FUNC_1;
242  //Configure P1.4 (ENET_TX_EN)
243  LPC_IOCON->P1_4 = IOCON_SLEW | IOCON_FUNC_1;
244  //Configure P1.8 (ENET_CRS)
245  LPC_IOCON->P1_8 = IOCON_SLEW | IOCON_FUNC_1;
246  //Configure P1.9 (ENET_RXD0)
247  LPC_IOCON->P1_9 = IOCON_SLEW | IOCON_FUNC_1;
248  //Configure P1.10 (ENET_RXD1)
249  LPC_IOCON->P1_10 = IOCON_SLEW | IOCON_FUNC_1;
250  //Configure P1.14 (RX_ER)
251  LPC_IOCON->P1_14 = IOCON_SLEW | IOCON_FUNC_1;
252  //Configure P1.15 (ENET_REF_CLK)
253  LPC_IOCON->P1_15 = IOCON_SLEW | IOCON_FUNC_1;
254  //Configure P1.16 (ENET_MDC)
255  LPC_IOCON->P1_16 = IOCON_MODE_PULL_UP | IOCON_FUNC_1;
256  //Configure P1.17 (ENET_MDIO)
257  LPC_IOCON->P1_17 = IOCON_MODE_PULL_UP | IOCON_FUNC_1;
258 #endif
259 }
260 
261 
262 /**
263  * @brief Initialize TX and RX descriptors
264  * @param[in] interface Underlying network interface
265  **/
266 
268 {
269  uint_t i;
270 
271  //Initialize TX descriptors
272  for(i = 0; i < LPC178X_ETH_TX_BUFFER_COUNT; i++)
273  {
274  //Base address of the buffer containing transmit data
275  txDesc[i].packet = (uint32_t) txBuffer[i];
276  //Transmit descriptor control word
277  txDesc[i].control = 0;
278  //Transmit status information word
279  txStatus[i].info = 0;
280  }
281 
282  //Initialize RX descriptors
283  for(i = 0; i < LPC178X_ETH_RX_BUFFER_COUNT; i++)
284  {
285  //Base address of the buffer for storing receive data
286  rxDesc[i].packet = (uint32_t) rxBuffer[i];
287  //Receive descriptor control word
289  //Receive status information word
290  rxStatus[i].info = 0;
291  //Receive status HashCRC word
292  rxStatus[i].hashCrc = 0;
293  }
294 
295  //Initialize EMAC transmit descriptor registers
296  LPC_EMAC->TxDescriptor = (uint32_t) txDesc;
297  LPC_EMAC->TxStatus = (uint32_t) txStatus;
298  LPC_EMAC->TxDescriptorNumber = LPC178X_ETH_TX_BUFFER_COUNT - 1;
299  LPC_EMAC->TxProduceIndex = 0;
300 
301  //Initialize EMAC receive descriptor registers
302  LPC_EMAC->RxDescriptor = (uint32_t) rxDesc;
303  LPC_EMAC->RxStatus = (uint32_t) rxStatus;
304  LPC_EMAC->RxDescriptorNumber = LPC178X_ETH_RX_BUFFER_COUNT - 1;
305  LPC_EMAC->RxConsumeIndex = 0;
306 }
307 
308 
309 /**
310  * @brief LPC178x Ethernet MAC timer handler
311  *
312  * This routine is periodically called by the TCP/IP stack to handle periodic
313  * operations such as polling the link state
314  *
315  * @param[in] interface Underlying network interface
316  **/
317 
318 void lpc178xEthTick(NetInterface *interface)
319 {
320  //Valid Ethernet PHY or switch driver?
321  if(interface->phyDriver != NULL)
322  {
323  //Handle periodic operations
324  interface->phyDriver->tick(interface);
325  }
326  else if(interface->switchDriver != NULL)
327  {
328  //Handle periodic operations
329  interface->switchDriver->tick(interface);
330  }
331  else
332  {
333  //Just for sanity
334  }
335 }
336 
337 
338 /**
339  * @brief Enable interrupts
340  * @param[in] interface Underlying network interface
341  **/
342 
344 {
345  //Enable Ethernet MAC interrupts
346  NVIC_EnableIRQ(ENET_IRQn);
347 
348  //Valid Ethernet PHY or switch driver?
349  if(interface->phyDriver != NULL)
350  {
351  //Enable Ethernet PHY interrupts
352  interface->phyDriver->enableIrq(interface);
353  }
354  else if(interface->switchDriver != NULL)
355  {
356  //Enable Ethernet switch interrupts
357  interface->switchDriver->enableIrq(interface);
358  }
359  else
360  {
361  //Just for sanity
362  }
363 }
364 
365 
366 /**
367  * @brief Disable interrupts
368  * @param[in] interface Underlying network interface
369  **/
370 
372 {
373  //Disable Ethernet MAC interrupts
374  NVIC_DisableIRQ(ENET_IRQn);
375 
376  //Valid Ethernet PHY or switch driver?
377  if(interface->phyDriver != NULL)
378  {
379  //Disable Ethernet PHY interrupts
380  interface->phyDriver->disableIrq(interface);
381  }
382  else if(interface->switchDriver != NULL)
383  {
384  //Disable Ethernet switch interrupts
385  interface->switchDriver->disableIrq(interface);
386  }
387  else
388  {
389  //Just for sanity
390  }
391 }
392 
393 
394 /**
395  * @brief LPC178x Ethernet MAC interrupt service routine
396  **/
397 
398 void ENET_IRQHandler(void)
399 {
400  uint_t i;
401  bool_t flag;
402  uint32_t status;
403 
404  //Interrupt service routine prologue
405  osEnterIsr();
406 
407  //This flag will be set if a higher priority task must be woken
408  flag = FALSE;
409 
410  //Read interrupt status register
411  status = LPC_EMAC->IntStatus;
412 
413  //Packet transmitted?
414  if((status & INT_TX_DONE) != 0)
415  {
416  //Clear TxDone interrupt flag
417  LPC_EMAC->IntClear = INT_TX_DONE;
418 
419  //Get the index of the next descriptor
420  i = LPC_EMAC->TxProduceIndex + 1;
421 
422  //Wrap around if necessary
424  {
425  i = 0;
426  }
427 
428  //Check whether the TX buffer is available for writing
429  if(i != LPC_EMAC->TxConsumeIndex)
430  {
431  //Notify the TCP/IP stack that the transmitter is ready to send
432  flag |= osSetEventFromIsr(&nicDriverInterface->nicTxEvent);
433  }
434  }
435 
436  //Packet received?
437  if((status & INT_RX_DONE) != 0)
438  {
439  //Disable RxDone interrupts
440  LPC_EMAC->IntEnable &= ~INT_RX_DONE;
441 
442  //Set event flag
443  nicDriverInterface->nicEvent = TRUE;
444  //Notify the TCP/IP stack of the event
445  flag |= osSetEventFromIsr(&netEvent);
446  }
447 
448  //Interrupt service routine epilogue
449  osExitIsr(flag);
450 }
451 
452 
453 /**
454  * @brief LPC178x Ethernet MAC event handler
455  * @param[in] interface Underlying network interface
456  **/
457 
459 {
460  error_t error;
461 
462  //Packet received?
463  if((LPC_EMAC->IntStatus & INT_RX_DONE) != 0)
464  {
465  //Clear RxDone interrupt flag
466  LPC_EMAC->IntClear = INT_RX_DONE;
467 
468  //Process all pending packets
469  do
470  {
471  //Read incoming packet
472  error = lpc178xEthReceivePacket(interface);
473 
474  //No more data in the receive buffer?
475  } while(error != ERROR_BUFFER_EMPTY);
476  }
477 
478  //Re-enable TxDone and RxDone interrupts
479  LPC_EMAC->IntEnable = INT_TX_DONE | INT_RX_DONE;
480 }
481 
482 
483 /**
484  * @brief Send a packet
485  * @param[in] interface Underlying network interface
486  * @param[in] buffer Multi-part buffer containing the data to send
487  * @param[in] offset Offset to the first data byte
488  * @param[in] ancillary Additional options passed to the stack along with
489  * the packet
490  * @return Error code
491  **/
492 
494  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
495 {
496  uint_t i;
497  uint_t j;
498  size_t length;
499 
500  //Retrieve the length of the packet
501  length = netBufferGetLength(buffer) - offset;
502 
503  //Check the frame length
504  if(!length)
505  {
506  //The transmitter can accept another packet
507  osSetEvent(&interface->nicTxEvent);
508  //We are done since the buffer is empty
509  return NO_ERROR;
510  }
512  {
513  //The transmitter can accept another packet
514  osSetEvent(&interface->nicTxEvent);
515  //Report an error
516  return ERROR_INVALID_LENGTH;
517  }
518 
519  //Get the index of the current descriptor
520  i = LPC_EMAC->TxProduceIndex;
521  //Get the index of the next descriptor
522  j = i + 1;
523 
524  //Wrap around if necessary
526  {
527  j = 0;
528  }
529 
530  //Check whether the transmit descriptor array is full
531  if(j == LPC_EMAC->TxConsumeIndex)
532  {
533  return ERROR_FAILURE;
534  }
535 
536  //Copy user data to the transmit buffer
537  netBufferRead((uint8_t *) txDesc[i].packet, buffer, offset, length);
538 
539  //Write the transmit control word
540  txDesc[i].control = TX_CTRL_INTERRUPT | TX_CTRL_LAST |
542 
543  //Increment index and wrap around if necessary
544  if(++i >= LPC178X_ETH_TX_BUFFER_COUNT)
545  {
546  i = 0;
547  }
548 
549  //Save the resulting value
550  LPC_EMAC->TxProduceIndex = i;
551 
552  //Get the index of the next descriptor
553  j = i + 1;
554 
555  //Wrap around if necessary
557  {
558  j = 0;
559  }
560 
561  //Check whether the next buffer is available for writing
562  if(j != LPC_EMAC->TxConsumeIndex)
563  {
564  //The transmitter can accept another packet
565  osSetEvent(&interface->nicTxEvent);
566  }
567 
568  //Successful write operation
569  return NO_ERROR;
570 }
571 
572 
573 /**
574  * @brief Receive a packet
575  * @param[in] interface Underlying network interface
576  * @return Error code
577  **/
578 
580 {
581  error_t error;
582  size_t n;
583  uint_t i;
584  NetRxAncillary ancillary;
585 
586  //Point to the current descriptor
587  i = LPC_EMAC->RxConsumeIndex;
588 
589  //Current buffer available for reading?
590  if(i != LPC_EMAC->RxProduceIndex)
591  {
592  //Retrieve the length of the frame
593  n = (rxStatus[i].info & RX_STATUS_SIZE) + 1;
594  //Limit the number of data to read
596 
597  //Additional options can be passed to the stack along with the packet
598  ancillary = NET_DEFAULT_RX_ANCILLARY;
599 
600  //Pass the packet to the upper layer
601  nicProcessPacket(interface, (uint8_t *) rxDesc[i].packet, n, &ancillary);
602 
603  //Increment index and wrap around if necessary
604  if(++i >= LPC178X_ETH_RX_BUFFER_COUNT)
605  {
606  i = 0;
607  }
608 
609  //Save the resulting value
610  LPC_EMAC->RxConsumeIndex = i;
611 
612  //Valid packet received
613  error = NO_ERROR;
614  }
615  else
616  {
617  //No more data in the receive buffer
618  error = ERROR_BUFFER_EMPTY;
619  }
620 
621  //Return status code
622  return error;
623 }
624 
625 
626 /**
627  * @brief Configure MAC address filtering
628  * @param[in] interface Underlying network interface
629  * @return Error code
630  **/
631 
633 {
634  uint_t i;
635  uint_t k;
636  uint32_t crc;
637  uint32_t hashTable[2];
638  MacFilterEntry *entry;
639 
640  //Debug message
641  TRACE_DEBUG("Updating MAC filter...\r\n");
642 
643  //Set the MAC address of the station
644  LPC_EMAC->SA0 = interface->macAddr.w[2];
645  LPC_EMAC->SA1 = interface->macAddr.w[1];
646  LPC_EMAC->SA2 = interface->macAddr.w[0];
647 
648  //Clear hash table
649  hashTable[0] = 0;
650  hashTable[1] = 0;
651 
652  //The MAC address filter contains the list of MAC addresses to accept
653  //when receiving an Ethernet frame
654  for(i = 0; i < MAC_ADDR_FILTER_SIZE; i++)
655  {
656  //Point to the current entry
657  entry = &interface->macAddrFilter[i];
658 
659  //Valid entry?
660  if(entry->refCount > 0)
661  {
662  //Compute CRC over the current MAC address
663  crc = lpc178xEthCalcCrc(&entry->addr, sizeof(MacAddr));
664  //Bits [28:23] are used to form the hash
665  k = (crc >> 23) & 0x3F;
666  //Update hash table contents
667  hashTable[k / 32] |= (1 << (k % 32));
668  }
669  }
670 
671  //Write the hash table
672  LPC_EMAC->HashFilterL = hashTable[0];
673  LPC_EMAC->HashFilterH = hashTable[1];
674 
675  //Debug message
676  TRACE_DEBUG(" HashFilterL = %08" PRIX32 "\r\n", LPC_EMAC->HashFilterL);
677  TRACE_DEBUG(" HashFilterH = %08" PRIX32 "\r\n", LPC_EMAC->HashFilterH);
678 
679  //Successful processing
680  return NO_ERROR;
681 }
682 
683 
684 /**
685  * @brief Adjust MAC configuration parameters for proper operation
686  * @param[in] interface Underlying network interface
687  * @return Error code
688  **/
689 
691 {
692  //10BASE-T or 100BASE-TX operation mode?
693  if(interface->linkSpeed == NIC_LINK_SPEED_100MBPS)
694  {
695  LPC_EMAC->SUPP = SUPP_SPEED;
696  }
697  else
698  {
699  LPC_EMAC->SUPP = 0;
700  }
701 
702  //Half-duplex or full-duplex mode?
703  if(interface->duplexMode == NIC_FULL_DUPLEX_MODE)
704  {
705  //The MAC operates in full-duplex mode
706  LPC_EMAC->MAC2 |= MAC2_FULL_DUPLEX;
707  LPC_EMAC->Command |= COMMAND_FULL_DUPLEX;
708  //Configure Back-to-Back Inter-Packet Gap
709  LPC_EMAC->IPGT = IPGT_FULL_DUPLEX;
710  }
711  else
712  {
713  //The MAC operates in half-duplex mode
714  LPC_EMAC->MAC2 &= ~MAC2_FULL_DUPLEX;
715  LPC_EMAC->Command &= ~COMMAND_FULL_DUPLEX;
716  //Configure Back-to-Back Inter-Packet Gap
717  LPC_EMAC->IPGT = IPGT_HALF_DUPLEX;
718  }
719 
720  //Successful processing
721  return NO_ERROR;
722 }
723 
724 
725 /**
726  * @brief Write PHY register
727  * @param[in] opcode Access type (2 bits)
728  * @param[in] phyAddr PHY address (5 bits)
729  * @param[in] regAddr Register address (5 bits)
730  * @param[in] data Register value
731  **/
732 
733 void lpc178xEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
734  uint8_t regAddr, uint16_t data)
735 {
736  //Valid opcode?
737  if(opcode == SMI_OPCODE_WRITE)
738  {
739  //Clear MCMD register
740  LPC_EMAC->MCMD = 0;
741 
742  //PHY address
743  LPC_EMAC->MADR = (phyAddr << 8) & MADR_PHY_ADDRESS;
744  //Register address
745  LPC_EMAC->MADR |= regAddr & MADR_REGISTER_ADDRESS;
746  //Data to be written in the PHY register
747  LPC_EMAC->MWTD = data & MWTD_WRITE_DATA;
748 
749  //Wait for the write to complete
750  while((LPC_EMAC->MIND & MIND_BUSY) != 0)
751  {
752  }
753  }
754  else
755  {
756  //The MAC peripheral only supports standard Clause 22 opcodes
757  }
758 }
759 
760 
761 /**
762  * @brief Read PHY register
763  * @param[in] opcode Access type (2 bits)
764  * @param[in] phyAddr PHY address (5 bits)
765  * @param[in] regAddr Register address (5 bits)
766  * @return Register value
767  **/
768 
769 uint16_t lpc178xEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
770  uint8_t regAddr)
771 {
772  uint16_t data;
773 
774  //Valid opcode?
775  if(opcode == SMI_OPCODE_READ)
776  {
777  //PHY address
778  LPC_EMAC->MADR = (phyAddr << 8) & MADR_PHY_ADDRESS;
779  //Register address
780  LPC_EMAC->MADR |= regAddr & MADR_REGISTER_ADDRESS;
781 
782  //Start a read operation
783  LPC_EMAC->MCMD = MCMD_READ;
784  //Wait for the read to complete
785  while((LPC_EMAC->MIND & MIND_BUSY) != 0)
786  {
787  }
788 
789  //Clear MCMD register
790  LPC_EMAC->MCMD = 0;
791 
792  //Get register value
793  data = LPC_EMAC->MRDD & MRDD_READ_DATA;
794  }
795  else
796  {
797  //The MAC peripheral only supports standard Clause 22 opcodes
798  data = 0;
799  }
800 
801  //Return the value of the PHY register
802  return data;
803 }
804 
805 
806 /**
807  * @brief CRC calculation
808  * @param[in] data Pointer to the data over which to calculate the CRC
809  * @param[in] length Number of bytes to process
810  * @return Resulting CRC value
811  **/
812 
813 uint32_t lpc178xEthCalcCrc(const void *data, size_t length)
814 {
815  uint_t i;
816  uint_t j;
817  uint32_t crc;
818  const uint8_t *p;
819 
820  //Point to the data over which to calculate the CRC
821  p = (uint8_t *) data;
822  //CRC preset value
823  crc = 0xFFFFFFFF;
824 
825  //Loop through data
826  for(i = 0; i < length; i++)
827  {
828  //The message is processed bit by bit
829  for(j = 0; j < 8; j++)
830  {
831  //Update CRC value
832  if((((crc >> 31) ^ (p[i] >> j)) & 0x01) != 0)
833  {
834  crc = (crc << 1) ^ 0x04C11DB7;
835  }
836  else
837  {
838  crc = crc << 1;
839  }
840  }
841  }
842 
843  //Return CRC value
844  return crc;
845 }
#define rxBuffer
#define txBuffer
__attribute__((naked))
AVR32 Ethernet MAC interrupt wrapper.
unsigned int uint_t
Definition: compiler_port.h:50
int bool_t
Definition: compiler_port.h:53
Debugging facilities.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define TRACE_INFO(...)
Definition: debug.h:95
uint8_t n
uint8_t opcode
Definition: dns_common.h:188
error_t
Error codes.
Definition: error.h:43
@ ERROR_BUFFER_EMPTY
Definition: error.h:141
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
#define ETH_MTU
Definition: ethernet.h:116
uint8_t data[]
Definition: ethernet.h:222
MacAddr
Definition: ethernet.h:195
#define MAC_ADDR_FILTER_SIZE
Definition: ethernet.h:95
#define MCFG_RESET_MII_MGMT
#define MAC1_SOFT_RESET
#define COMMAND_TX_RESET
#define MWTD_WRITE_DATA
#define TX_CTRL_INTERRUPT
#define COMMAND_RX_ENABLE
#define COMMAND_RMII
#define MADR_REGISTER_ADDRESS
#define SUPP_SPEED
#define MAC1_RESET_TX
#define COMMAND_FULL_DUPLEX
#define TX_CTRL_PAD
#define MAC2_FULL_DUPLEX
#define TX_CTRL_SIZE
#define MAC2_CRC_ENABLE
#define MCFG_CLOCK_SELECT_DIV48
#define MAC1_RESET_MCS_TX
#define MAC2_PAD_CRC_ENABLE
#define COMMAND_REG_RESET
#define COMMAND_TX_ENABLE
#define MADR_PHY_ADDRESS
#define RFC_ACCEPT_BROADCAST_EN
#define IPGT_FULL_DUPLEX
#define MAC1_SIMULATION_RESET
#define INT_TX_DONE
#define IPGT_HALF_DUPLEX
#define RX_CTRL_INTERRUPT
#define MAC1_RESET_MCS_RX
#define COMMAND_RX_RESET
#define CLRT_DEFAULT_VALUE
#define MAC1_RESET_RX
#define MRDD_READ_DATA
#define MAC1_RECEIVE_ENABLE
#define IPGR_DEFAULT_VALUE
#define RFC_ACCEPT_PERFECT_EN
#define TX_CTRL_CRC
#define RX_STATUS_SIZE
#define INT_RX_DONE
#define MCMD_READ
#define RFC_ACCEPT_MULTICAST_HASH_EN
#define TX_CTRL_LAST
#define MIND_BUSY
const NicDriver lpc178xEthDriver
LPC178x Ethernet MAC driver.
uint16_t lpc178xEthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
error_t lpc178xEthReceivePacket(NetInterface *interface)
Receive a packet.
void lpc178xEthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
void ENET_IRQHandler(void)
LPC178x Ethernet MAC interrupt service routine.
uint32_t lpc178xEthCalcCrc(const void *data, size_t length)
CRC calculation.
error_t lpc178xEthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
error_t lpc178xEthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
error_t lpc178xEthInit(NetInterface *interface)
LPC178x Ethernet MAC initialization.
error_t lpc178xEthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
void lpc178xEthEnableIrq(NetInterface *interface)
Enable interrupts.
void lpc178xEthEventHandler(NetInterface *interface)
LPC178x Ethernet MAC event handler.
__weak_func void lpc178xEthInitGpio(NetInterface *interface)
GPIO configuration.
void lpc178xEthInitDesc(NetInterface *interface)
Initialize TX and RX descriptors.
void lpc178xEthTick(NetInterface *interface)
LPC178x Ethernet MAC timer handler.
void lpc178xEthDisableIrq(NetInterface *interface)
Disable interrupts.
LPC1786/88 Ethernet MAC driver.
#define LPC178X_ETH_TX_BUFFER_SIZE
#define LPC178X_ETH_IRQ_PRIORITY_GROUPING
#define LPC178X_ETH_RX_BUFFER_SIZE
#define LPC178X_ETH_TX_BUFFER_COUNT
#define LPC178X_ETH_IRQ_SUB_PRIORITY
#define LPC178X_ETH_RX_BUFFER_COUNT
#define LPC178X_ETH_IRQ_GROUP_PRIORITY
uint16_t regAddr
uint8_t p
Definition: ndp.h:300
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define netEvent
Definition: net_legacy.h:196
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
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:674
const NetRxAncillary NET_DEFAULT_RX_ANCILLARY
Definition: net_misc.c:101
#define NetRxAncillary
Definition: net_misc.h:40
#define NetTxAncillary
Definition: net_misc.h:36
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length, NetRxAncillary *ancillary)
Handle a packet received by the network controller.
Definition: nic.c:391
#define SMI_OPCODE_WRITE
Definition: nic.h:66
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:83
#define SMI_OPCODE_READ
Definition: nic.h:67
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
#define MIN(a, b)
Definition: os_port.h:63
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define osEnterIsr()
#define osExitIsr(flag)
Receive descriptor.
Receive status.
Transmit descriptor.
Transmit status.
MAC filter table entry.
Definition: ethernet.h:262
MacAddr addr
MAC address.
Definition: ethernet.h:263
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:264
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
NIC driver.
Definition: nic.h:283
uint8_t length
Definition: tcp.h:368