enet_001.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * @brief Ethernet control functions
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2012
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #include "enet_001.h"
  32. /*****************************************************************************
  33. * Private types/enumerations/variables
  34. ****************************************************************************/
  35. /* Saved address for PHY and clock divider */
  36. STATIC uint32_t phyCfg;
  37. /*****************************************************************************
  38. * Public types/enumerations/variables
  39. ****************************************************************************/
  40. /*****************************************************************************
  41. * Private functions
  42. ****************************************************************************/
  43. /*****************************************************************************
  44. * Public functions
  45. ****************************************************************************/
  46. /* Resets ethernet interface */
  47. void IP_ENET_Reset(IP_ENET_001_Type *LPC_ENET)
  48. {
  49. /* This should be called prior to IP_ENET_Init. The MAC controller may
  50. not be ready for a call to init right away so a small delay should
  51. occur after this call. */
  52. LPC_ENET->DMA_BUS_MODE |= DMA_BM_SWR;
  53. }
  54. /* Sets the address of the interface */
  55. void IP_ENET_SetADDR(IP_ENET_001_Type *LPC_ENET, const uint8_t *macAddr)
  56. {
  57. /* Save MAC address */
  58. LPC_ENET->MAC_ADDR0_LOW = ((uint32_t) macAddr[3] << 24) |
  59. ((uint32_t) macAddr[2] << 16) | ((uint32_t) macAddr[1] << 8) |
  60. ((uint32_t) macAddr[0]);
  61. LPC_ENET->MAC_ADDR0_HIGH = ((uint32_t) macAddr[5] << 8) |
  62. ((uint32_t) macAddr[4]);
  63. }
  64. /* Initialize ethernet interface */
  65. void IP_ENET_Init(IP_ENET_001_Type *LPC_ENET)
  66. {
  67. /* Enhanced descriptors, burst length = 1 */
  68. LPC_ENET->DMA_BUS_MODE = DMA_BM_ATDS | DMA_BM_PBL(1) | DMA_BM_RPBL(1);
  69. /* Initial MAC configuration for checksum offload, full duplex,
  70. 100Mbps, disable receive own in half duplex, inter-frame gap
  71. of 64-bits */
  72. LPC_ENET->MAC_CONFIG = MAC_CFG_BL(0) | MAC_CFG_IPC | MAC_CFG_DM |
  73. MAC_CFG_DO | MAC_CFG_FES | MAC_CFG_PS | MAC_CFG_IFG(3);
  74. /* Setup default filter */
  75. LPC_ENET->MAC_FRAME_FILTER = MAC_FF_PR | MAC_FF_RA;
  76. /* Flush transmit FIFO */
  77. LPC_ENET->DMA_OP_MODE = DMA_OM_FTF;
  78. /* Setup DMA to flush receive FIFOs at 32 bytes, service TX FIFOs at
  79. 64 bytes */
  80. LPC_ENET->DMA_OP_MODE |= DMA_OM_RTC(1) | DMA_OM_TTC(0);
  81. /* Clear all MAC interrupts */
  82. LPC_ENET->DMA_STAT = DMA_ST_ALL;
  83. /* Enable MAC interrupts */
  84. LPC_ENET->DMA_INT_EN = 0;
  85. }
  86. /* Sets up the PHY link clock divider and PHY address */
  87. void IP_ENET_SetupMII(IP_ENET_001_Type *LPC_ENET, uint32_t div, uint8_t addr)
  88. {
  89. /* Save clock divider and PHY address in MII address register */
  90. phyCfg = MAC_MIIA_PA(addr) | MAC_MIIA_CR(div);
  91. }
  92. /*De-initialize the ethernet interface */
  93. void IP_ENET_DeInit(IP_ENET_001_Type *LPC_ENET)
  94. {
  95. /* Disable packet reception */
  96. LPC_ENET->MAC_CONFIG = 0;
  97. /* Flush transmit FIFO */
  98. LPC_ENET->DMA_OP_MODE = DMA_OM_FTF;
  99. /* Disable receive and transmit DMA processes */
  100. LPC_ENET->DMA_OP_MODE = 0;
  101. }
  102. /* Starts a PHY write via the MII */
  103. void IP_ENET_StartMIIWrite(IP_ENET_001_Type *LPC_ENET, uint8_t reg, uint16_t data)
  104. {
  105. /* Write value at PHY address and register */
  106. LPC_ENET->MAC_MII_ADDR = phyCfg | MAC_MIIA_GR(reg) | MAC_MIIA_W;
  107. LPC_ENET->MAC_MII_DATA = (uint32_t) data;
  108. LPC_ENET->MAC_MII_ADDR |= MAC_MIIA_GB;
  109. }
  110. /*Starts a PHY read via the MII */
  111. void IP_ENET_StartMIIRead(IP_ENET_001_Type *LPC_ENET, uint8_t reg)
  112. {
  113. /* Read value at PHY address and register */
  114. LPC_ENET->MAC_MII_ADDR = phyCfg | MAC_MIIA_GR(reg);
  115. LPC_ENET->MAC_MII_ADDR |= MAC_MIIA_GB;
  116. }
  117. /* Returns MII link (PHY) busy status */
  118. bool IP_ENET_IsMIIBusy(IP_ENET_001_Type *LPC_ENET)
  119. {
  120. if (LPC_ENET->MAC_MII_ADDR & MAC_MIIA_GB) {
  121. return true;
  122. }
  123. return false;
  124. }
  125. /* Enables or disables ethernet transmit */
  126. void IP_ENET_TXEnable(IP_ENET_001_Type *LPC_ENET, bool Enable)
  127. {
  128. if (Enable) {
  129. /* Descriptor list head pointers must be setup prior to enable */
  130. LPC_ENET->MAC_CONFIG |= MAC_CFG_TE;
  131. LPC_ENET->DMA_OP_MODE |= DMA_OM_ST;
  132. }
  133. else {
  134. LPC_ENET->MAC_CONFIG &= ~MAC_CFG_TE;
  135. }
  136. }
  137. /* Enables or disables ethernet packet reception */
  138. void IP_ENET_RXEnable(IP_ENET_001_Type *LPC_ENET, bool Enable)
  139. {
  140. if (Enable) {
  141. /* Descriptor list head pointers must be setup prior to enable */
  142. LPC_ENET->MAC_CONFIG |= MAC_CFG_RE;
  143. LPC_ENET->DMA_OP_MODE |= DMA_OM_SR;
  144. }
  145. else {
  146. LPC_ENET->MAC_CONFIG &= ~MAC_CFG_RE;
  147. }
  148. }
  149. /* Sets full or half duplex for the interface */
  150. void IP_ENET_SetDuplex(IP_ENET_001_Type *LPC_ENET, bool full)
  151. {
  152. if (full) {
  153. LPC_ENET->MAC_CONFIG |= MAC_CFG_DM;
  154. }
  155. else {
  156. LPC_ENET->MAC_CONFIG &= ~MAC_CFG_DM;
  157. }
  158. }
  159. /* Sets speed for the interface */
  160. void IP_ENET_SetSpeed(IP_ENET_001_Type *LPC_ENET, bool speed100)
  161. {
  162. if (speed100) {
  163. LPC_ENET->MAC_CONFIG |= MAC_CFG_FES;
  164. }
  165. else {
  166. LPC_ENET->MAC_CONFIG &= ~MAC_CFG_FES;
  167. }
  168. }
  169. /* Configures the initial ethernet descriptors */
  170. void IP_ENET_InitDescriptors(IP_ENET_001_Type *LPC_ENET,
  171. IP_ENET_001_ENHTXDESC_Type *pTXDescs, IP_ENET_001_ENHRXDESC_Type *pRXDescs)
  172. {
  173. /* Setup descriptor list base addresses */
  174. LPC_ENET->DMA_TRANS_DES_ADDR = (uint32_t) pTXDescs;
  175. LPC_ENET->DMA_REC_DES_ADDR = (uint32_t) pRXDescs;
  176. }