stm32f4x7_phy.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "stm32f4x7_phy.h"
  2. #include "interface.h"
  3. #include "lwip/api.h"
  4. #include "lwip/sockets.h"
  5. #include "netconf.h"
  6. #include "stm32f4x7_eth.h"
  7. ETH_InitTypeDef ETH_InitStructure;
  8. __IO uint32_t EthStatus = 0;
  9. extern struct netif gnetif;
  10. static void dm9161_gpio_config(void);
  11. static void dm9161_mac_dma_config(void);
  12. static void nvic_configuration(void);
  13. /**
  14. * @brief Inserts a delay time.
  15. * @param nCount: number of 10ms periods to wait for.
  16. * @retval None
  17. */
  18. void ETH_Delay10ms(uint32_t nCount)
  19. {
  20. // OSTimeDly(10 * nCount);
  21. }
  22. /*!
  23. \brief setup ethernet system(GPIOs, clocks, MAC, DMA, systick)
  24. \param[in] none
  25. \param[out] none
  26. \retval none
  27. */
  28. void enet_system_setup(void)
  29. {
  30. nvic_configuration();
  31. /* configure the GPIO ports for ethernet pins */
  32. dm9161_gpio_config();
  33. /* configure the ethernet MAC/DMA */
  34. dm9161_mac_dma_config();
  35. /* Get Ethernet link status*/
  36. if (ETH_ReadPHYRegister(ETHERNET_PHY_ADDRESS, PHY_SR) & 1)
  37. {
  38. EthStatus |= ETH_LINK_FLAG;
  39. }
  40. /* Configure the PHY to generate an interrupt on change of link status */
  41. Eth_Link_PHYITConfig(ETHERNET_PHY_ADDRESS);
  42. ETH_DMAITConfig(ETH_DMA_IT_R, ENABLE);
  43. ETH_DMAITConfig(ETH_DMA_IT_NIS, ENABLE);
  44. }
  45. /*!
  46. \brief configures the ethernet interface
  47. \param[in] none
  48. \param[out] none
  49. \retval none
  50. */
  51. static void dm9161_mac_dma_config(void)
  52. {
  53. /* Reset ETHERNET on AHB Bus */
  54. ETH_DeInit();
  55. /* Software reset */
  56. ETH_SoftwareReset();
  57. /* Wait for software reset */
  58. while (ETH_GetSoftwareResetStatus() == SET)
  59. ;
  60. /* ETHERNET Configuration ------------------------------*/
  61. /* 缺省配置ETH_InitStructure */
  62. ETH_StructInit(&ETH_InitStructure);
  63. /* Fill ETH_InitStructure parametrs */
  64. /*-------------------- MAC ----------------------------*/
  65. /* 开启网络自适应功能,速度和工作模式无需配置 */
  66. ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
  67. // ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Disable;
  68. // ETH_InitStructure.ETH_Speed = ETH_Speed_10M;
  69. // ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
  70. /* 关闭反馈 */
  71. ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
  72. /* 关闭重传功能 */
  73. ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
  74. /* 关闭自动去除PDA/CRC功能 */
  75. ETH_InitStructure.ETH_AutomaticPadCRCStrip =
  76. ETH_AutomaticPadCRCStrip_Disable;
  77. /* 关闭接收所有的帧 */
  78. ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable;
  79. /* 允许接收所有广播帧 */
  80. ETH_InitStructure.ETH_BroadcastFramesReception =
  81. ETH_BroadcastFramesReception_Enable;
  82. /* 关闭混合模式的地址过滤 */
  83. ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
  84. /* 对于组播地址使用完美地址过滤 */
  85. ETH_InitStructure.ETH_MulticastFramesFilter =
  86. ETH_MulticastFramesFilter_Perfect;
  87. /* 对单播地址使用完美地址过滤 */
  88. ETH_InitStructure.ETH_UnicastFramesFilter =
  89. ETH_UnicastFramesFilter_Perfect;
  90. #ifdef CHECKSUM_BY_HARDWARE
  91. /* 开启ipv4和TCP/UDP/ICMP的帧校验和卸载 */
  92. ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
  93. #endif
  94. /*------------------------ DMA -------------------------------*/
  95. /*当我们使用帧校验和卸载功能的时候,一定要使能存储转发模式,存储
  96. 转发模式中要保证整个帧存储在FIFO中, 这样MAC能插入/识别出帧校验
  97. 值,当真校验正确的时候DMA就可以处理帧,否则就丢弃掉该帧*/
  98. /* 开启丢弃TCP/IP错误帧 */
  99. ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame =
  100. ETH_DropTCPIPChecksumErrorFrame_Enable;
  101. /* 开启接收数据的存储转发模式 */
  102. ETH_InitStructure.ETH_ReceiveStoreForward =
  103. ETH_ReceiveStoreForward_Enable;
  104. /* 开启发送数据的存储转发模式 */
  105. ETH_InitStructure.ETH_TransmitStoreForward =
  106. ETH_TransmitStoreForward_Enable;
  107. /* 禁止转发错误帧 */
  108. ETH_InitStructure.ETH_ForwardErrorFrames =
  109. ETH_ForwardErrorFrames_Disable;
  110. /* 不转发过小的好帧 */
  111. ETH_InitStructure.ETH_ForwardUndersizedGoodFrames =
  112. ETH_ForwardUndersizedGoodFrames_Disable;
  113. /* 打开处理第二帧功能 */
  114. ETH_InitStructure.ETH_SecondFrameOperate =
  115. ETH_SecondFrameOperate_Enable;
  116. /* 开启DMA传输的地址对齐功能 */
  117. ETH_InitStructure.ETH_AddressAlignedBeats =
  118. ETH_AddressAlignedBeats_Enable;
  119. /* 开启固定突发功能 */
  120. ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
  121. /* DMA发送的最大突发长度为32个节拍 */
  122. ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
  123. /*DMA接收的最大突发长度为32个节拍 */
  124. ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
  125. ETH_InitStructure.ETH_DMAArbitration =
  126. ETH_DMAArbitration_RoundRobin_RxTx_2_1;
  127. /* 配置ETH */
  128. EthStatus = ETH_Init(&ETH_InitStructure, ETHERNET_PHY_ADDRESS);
  129. }
  130. /*!
  131. \brief configures the nested vectored interrupt controller
  132. \param[in] none
  133. \param[out] none
  134. \retval none
  135. */
  136. static void nvic_configuration(void)
  137. {
  138. NVIC_InitTypeDef NVIC_InitStructure;
  139. NVIC_InitStructure.NVIC_IRQChannel = ETH_IRQn;
  140. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  141. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  142. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  143. NVIC_Init(&NVIC_InitStructure);
  144. }
  145. /*!
  146. \brief configures the different GPIO ports
  147. \param[in] none
  148. \param[out] none
  149. \retval none
  150. */
  151. static void dm9161_gpio_config(void)
  152. {
  153. /* Enable SYSCFG clock */
  154. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  155. #ifdef MII_MODE
  156. #ifdef PHY_CLOCK_MCO
  157. /* output HXTAL clock (25MHz) on CKOUT0 pin(PA8) to clock the PHY */
  158. RCC_MCO1Config(RCC_MCO1Source_HSE, RCC_MCO1Div_1);
  159. #endif /* PHY_CLOCK_MCO */
  160. SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_MII);
  161. #elif defined RMII_MODE
  162. SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII);
  163. #endif
  164. }
  165. /**
  166. * @brief This function handles Ethernet link status.
  167. * @param None
  168. * @retval None
  169. */
  170. void Eth_Link_ITHandler(uint16_t PHYAddress)
  171. {
  172. /* Check whether the link interrupt has occurred or not */
  173. if (((ETH_ReadPHYRegister(PHYAddress, PHY_MISR)) & PHY_LINK_STATUS) != 0)
  174. {
  175. if ((ETH_ReadPHYRegister(PHYAddress, PHY_SR) & 1))
  176. {
  177. netif_set_link_up(&gnetif);
  178. }
  179. else
  180. {
  181. netif_set_link_down(&gnetif);
  182. }
  183. }
  184. }
  185. /**
  186. * @brief Configure the PHY to generate an interrupt on change of link status.
  187. * @param PHYAddress: external PHY address
  188. * @retval None
  189. */
  190. uint32_t Eth_Link_PHYITConfig(uint16_t PHYAddress)
  191. {
  192. uint16_t tmpreg = 0;
  193. /* Read MICR register */
  194. tmpreg = ETH_ReadPHYRegister(PHYAddress, PHY_MICR);
  195. /* Enable output interrupt events to signal via the INT pin */
  196. tmpreg |= (uint16_t)(PHY_MICR_INT_EN | PHY_MICR_INT_OE);
  197. if (!(ETH_WritePHYRegister(PHYAddress, PHY_MICR, tmpreg)))
  198. {
  199. /* Return ERROR in case of write timeout */
  200. return ETH_ERROR;
  201. }
  202. /* Read MISR register */
  203. tmpreg = ETH_ReadPHYRegister(PHYAddress, PHY_MISR);
  204. /* Enable Interrupt on change of link status */
  205. tmpreg |= (uint16_t)PHY_MISR_LINK_INT_EN;
  206. if (!(ETH_WritePHYRegister(PHYAddress, PHY_MISR, tmpreg)))
  207. {
  208. /* Return ERROR in case of write timeout */
  209. return ETH_ERROR;
  210. }
  211. /* Return SUCCESS */
  212. return ETH_SUCCESS;
  213. }
  214. /**
  215. * @note ETH_Get_Link_Status
  216. * @brief Get Ethernet link status
  217. * @param None
  218. * @retval None
  219. */
  220. uint8_t ETH_Get_Link_Status(void)
  221. {
  222. return (ETH_ReadPHYRegister(ETHERNET_PHY_ADDRESS, PHY_BSR) & PHY_Linked_Status);
  223. }