stm32f4x7_phy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 phy_gpio_config(void);
  11. static void phy_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. USE_Delay(10 * nCount);
  22. }
  23. /*!
  24. \brief setup ethernet system(GPIOs, clocks, MAC, DMA, systick)
  25. \param[in] none
  26. \param[out] none
  27. \retval none
  28. */
  29. void eth_init(void)
  30. {
  31. nvic_configuration();
  32. /* configure the GPIO ports for ethernet pins */
  33. phy_gpio_config();
  34. /* configure the ethernet MAC/DMA */
  35. phy_mac_dma_config();
  36. /* Get Ethernet link status*/
  37. if (ETH_ReadPHYRegister(ETHERNET_PHY_ADDRESS, PHY_SR) & 1)
  38. {
  39. EthStatus |= ETH_LINK_FLAG;
  40. }
  41. /* Configure the PHY to generate an interrupt on change of link status */
  42. Eth_Link_PHYITConfig(ETHERNET_PHY_ADDRESS);
  43. ETH_DMAITConfig(ETH_DMA_IT_R | 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 phy_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 phy_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. * @brief Link callback function, this function is called on change of link status.
  216. * @param The network interface
  217. * @retval None
  218. */
  219. void ETH_link_callback(struct netif *netif)
  220. {
  221. __IO uint32_t timeout = 0;
  222. uint32_t tmpreg;
  223. uint16_t RegValue;
  224. ip4_addr_t ipaddr;
  225. ip4_addr_t netmask;
  226. ip4_addr_t gw;
  227. if (netif_is_link_up(netif))
  228. {
  229. /* Restart the auto-negotiation */
  230. if (ETH_InitStructure.ETH_AutoNegotiation != ETH_AutoNegotiation_Disable)
  231. {
  232. /* Reset Timeout counter */
  233. timeout = 0;
  234. /* Enable auto-negotiation */
  235. ETH_WritePHYRegister(ETHERNET_PHY_ADDRESS, PHY_BCR, PHY_AutoNegotiation);
  236. /* Wait until the auto-negotiation will be completed */
  237. do
  238. {
  239. timeout++;
  240. } while (!(ETH_ReadPHYRegister(ETHERNET_PHY_ADDRESS, PHY_BSR) & PHY_AutoNego_Complete) && (timeout < (uint32_t)PHY_READ_TO));
  241. /* Reset Timeout counter */
  242. timeout = 0;
  243. /* Read the result of the auto-negotiation */
  244. RegValue = ETH_ReadPHYRegister(ETHERNET_PHY_ADDRESS, PHY_SR);
  245. /* Configure the MAC with the Duplex Mode fixed by the auto-negotiation process */
  246. if ((RegValue & PHY_DUPLEX_STATUS) != (uint16_t)RESET)
  247. {
  248. /* Set Ethernet duplex mode to Full-duplex following the auto-negotiation */
  249. ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
  250. }
  251. else
  252. {
  253. /* Set Ethernet duplex mode to Half-duplex following the auto-negotiation */
  254. ETH_InitStructure.ETH_Mode = ETH_Mode_HalfDuplex;
  255. }
  256. /* Configure the MAC with the speed fixed by the auto-negotiation process */
  257. if (RegValue & PHY_SPEED_STATUS)
  258. {
  259. /* Set Ethernet speed to 10M following the auto-negotiation */
  260. ETH_InitStructure.ETH_Speed = ETH_Speed_10M;
  261. }
  262. else
  263. {
  264. /* Set Ethernet speed to 100M following the auto-negotiation */
  265. ETH_InitStructure.ETH_Speed = ETH_Speed_100M;
  266. }
  267. /*------------------------ ETHERNET MACCR Re-Configuration --------------------*/
  268. /* Get the ETHERNET MACCR value */
  269. tmpreg = ETH->MACCR;
  270. /* Set the FES bit according to ETH_Speed value */
  271. /* Set the DM bit according to ETH_Mode value */
  272. tmpreg |= (uint32_t)(ETH_InitStructure.ETH_Speed | ETH_InitStructure.ETH_Mode);
  273. /* Write to ETHERNET MACCR */
  274. ETH->MACCR = (uint32_t)tmpreg;
  275. _eth_delay_(ETH_REG_WRITE_DELAY);
  276. tmpreg = ETH->MACCR;
  277. ETH->MACCR = tmpreg;
  278. }
  279. /* Restart MAC interface */
  280. ETH_Start();
  281. #ifdef USE_DHCP
  282. ipaddr.addr = 0;
  283. netmask.addr = 0;
  284. gw.addr = 0;
  285. DHCP_state = DHCP_START;
  286. #else
  287. IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  288. IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  289. IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  290. #endif /* USE_DHCP */
  291. netif_set_addr(&gnetif, &ipaddr, &netmask, &gw);
  292. /* When the netif is fully configured this function must be called.*/
  293. netif_set_up(&gnetif);
  294. /* 打印调试信息 */
  295. printf("LwIP Network Cable is now connected \r\n");
  296. #ifndef USE_DHCP
  297. /* Display static IP address */
  298. printf("LwIP Static IP address = %d.%d.%d.%d \r\n", IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  299. #endif /* USE_DHCP */
  300. }
  301. else
  302. {
  303. ETH_Stop();
  304. #ifdef USE_DHCP
  305. DHCP_state = DHCP_LINK_DOWN;
  306. dhcp_stop(netif);
  307. #endif /* USE_DHCP */
  308. /* When the netif link is down this function must be called.*/
  309. netif_set_down(&gnetif);
  310. /* 打印调试信息 */
  311. printf("LwIP Network Cable is unplugged \r\n");
  312. }
  313. }