hd_eth.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #include "string.h"
  2. #include "includes.h"
  3. #include "lwip/netif.h"
  4. #include "arch/ethernetif.h"
  5. #include "arch/sys_arch.h"
  6. #include "lwip/init.h"
  7. #include "lwip/timers.h"
  8. #include "lwip/tcpip.h"
  9. #include "lwip/dhcp.h"
  10. #include "lwip/inet.h"
  11. struct netif gnetif;
  12. ETH_InitTypeDef ETH_InitStructure;
  13. uint32_t EthStatus = 0;
  14. //初始ETH设备端口
  15. void ETH_GPIO_Config(void)
  16. {
  17. GPIO_InitTypeDef GPIO_InitStructure;
  18. /* Enable GPIOs clocks */
  19. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOE |
  20. RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOG, ENABLE);
  21. /* Enable SYSCFG clock */
  22. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  23. /* MII/RMII Media interface selection --------------------------------------*/
  24. SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_MII);
  25. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  26. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  27. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  28. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  29. /* Configure PA1, PA2 and PA7 */
  30. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_7;
  31. GPIO_Init(GPIOA, &GPIO_InitStructure);
  32. GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_ETH);
  33. GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_ETH);
  34. GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_ETH);
  35. GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_ETH);
  36. GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_ETH);
  37. /* Configure PB5 and PB8 */
  38. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_8 | GPIO_Pin_10;
  39. GPIO_Init(GPIOB, &GPIO_InitStructure);
  40. GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_ETH);
  41. GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_ETH);
  42. GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_ETH);
  43. GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_ETH);
  44. /* Configure PC1, PC2, PC3, PC4 and PC5 */
  45. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
  46. GPIO_Init(GPIOC, &GPIO_InitStructure);
  47. GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_ETH);
  48. GPIO_PinAFConfig(GPIOC, GPIO_PinSource2, GPIO_AF_ETH);
  49. GPIO_PinAFConfig(GPIOC, GPIO_PinSource3, GPIO_AF_ETH);
  50. GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_ETH);
  51. GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_ETH);
  52. /* Configure PG11, PG14 and PG13 */
  53. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_13 | GPIO_Pin_14;
  54. GPIO_Init(GPIOG, &GPIO_InitStructure);
  55. GPIO_PinAFConfig(GPIOG, GPIO_PinSource11, GPIO_AF_ETH);
  56. GPIO_PinAFConfig(GPIOG, GPIO_PinSource13, GPIO_AF_ETH);
  57. GPIO_PinAFConfig(GPIOG, GPIO_PinSource14, GPIO_AF_ETH);
  58. }
  59. //硬件复位PHY
  60. void ETH_PHY_RESET(void)
  61. {
  62. GPIO_InitTypeDef GPIO_InitStructure;
  63. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
  64. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  65. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  66. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  67. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  68. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  69. GPIO_Init(GPIOE, &GPIO_InitStructure);
  70. GPIO_SetBits(GPIOE, GPIO_Pin_2);
  71. systick_delayms(10);
  72. GPIO_ResetBits(GPIOE, GPIO_Pin_2);
  73. systick_delayms(10);
  74. GPIO_SetBits(GPIOE, GPIO_Pin_2);
  75. systick_delayms(10);
  76. }
  77. //配置以太网接收中断
  78. void ETH_NVIC_Config(void)
  79. {
  80. NVIC_InitTypeDef NVIC_InitStructure;
  81. /* Enable the Ethernet global Interrupt */
  82. NVIC_InitStructure.NVIC_IRQChannel = ETH_IRQn;
  83. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  84. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  85. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  86. NVIC_Init(&NVIC_InitStructure);
  87. /* Enable the Ethernet Rx Interrupt */
  88. ETH_DMAClearITPendingBit(ETH_DMA_IT_R);
  89. ETH_DMAClearITPendingBit(ETH_DMA_IT_NIS);
  90. ETH_DMAITConfig(ETH_DMA_IT_NIS | ETH_DMA_IT_R, ENABLE);
  91. }
  92. //配置以太网外设
  93. static void ETH_MACDMA_Config(void)
  94. {
  95. ETH_InitTypeDef ETH_InitStructure;
  96. /* Enable ETHERNET clock */
  97. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC | RCC_AHB1Periph_ETH_MAC_Tx |
  98. RCC_AHB1Periph_ETH_MAC_Rx, ENABLE);
  99. /* Reset ETHERNET on AHB Bus */
  100. ETH_DeInit();
  101. /* Software reset */
  102. ETH_SoftwareReset();
  103. /* Wait for software reset */
  104. while (ETH_GetSoftwareResetStatus() == SET);
  105. /* ETHERNET Configuration --------------------------------------------------*/
  106. /* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */
  107. ETH_StructInit(&ETH_InitStructure);
  108. /* Fill ETH_InitStructure parametrs */
  109. /*------------------------ MAC -----------------------------------*/
  110. ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
  111. //ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Disable;
  112. // ETH_InitStructure.ETH_Speed = ETH_Speed_10M;
  113. // ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
  114. ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
  115. ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
  116. ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
  117. ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable;
  118. ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable;
  119. ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
  120. ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
  121. ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
  122. #ifdef CHECKSUM_BY_HARDWARE
  123. ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
  124. #endif
  125. /*------------------------ DMA -----------------------------------*/
  126. /* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
  127. the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
  128. if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
  129. ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
  130. ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
  131. ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
  132. ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
  133. ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
  134. ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Enable;
  135. ETH_InitStructure.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
  136. ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
  137. ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
  138. ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
  139. ETH_InitStructure.ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_2_1;
  140. /* Configure Ethernet */
  141. if(ETH_Init(&ETH_InitStructure, DP83848_PHY_ADDRESS)) EthStatus = ETH_INIT_FLAG;
  142. else EthStatus = 0;
  143. }
  144. //获取phy连接状态 1:连接 0:断开
  145. uint8_t hd_ethGetPhyStatus(void)
  146. {
  147. if(ETH_ReadPHYRegister(DP83848_PHY_ADDRESS, PHY_BSR) & 0x00000004) return 1;
  148. else return 0;
  149. }
  150. void ETH_BSP_Config(void)
  151. {
  152. ETH_GPIO_Config();
  153. ETH_PHY_RESET();
  154. ETH_MACDMA_Config();
  155. if(EthStatus & ETH_INIT_FLAG) ETH_NVIC_Config();
  156. if(hd_ethGetPhyStatus()) EthStatus |= ETH_LINK_FLAG;
  157. }
  158. //以太网中断处理函数
  159. extern OS_EVENT *ETH_RecvDataSem;
  160. void ETH_IRQHandler(void)
  161. {
  162. /* Frame received */
  163. if (ETH_GetDMAFlagStatus(ETH_DMA_FLAG_R) == SET)
  164. {
  165. OSSemPost(ETH_RecvDataSem);
  166. }
  167. /* Clear the interrupt flags. */
  168. /* Clear the Eth DMA Rx IT pending bits */
  169. ETH_DMAClearITPendingBit(ETH_DMA_IT_R);
  170. ETH_DMAClearITPendingBit(ETH_DMA_IT_NIS);
  171. }
  172. ///////////////////////////////////////////////////////PHY///////////////////////////////////////////////////////////////////////////
  173. void low_level_init(struct netif *netif);
  174. void hd_ethCheckLinkStatus(uint16_t phyAddr)
  175. {
  176. uint8_t linksta = hd_ethGetPhyStatus();
  177. if(EthStatus & ETH_INIT_FLAG)
  178. {
  179. if(EthStatus & ETH_LINK_FLAG)
  180. {
  181. if(linksta == 0)
  182. {
  183. netif_set_link_down(&gnetif);
  184. EthStatus &= ~ETH_LINK_FLAG;
  185. }
  186. }
  187. else
  188. {
  189. if(linksta != 0)
  190. {
  191. netif_set_link_up(&gnetif);
  192. EthStatus |= ETH_LINK_FLAG;
  193. }
  194. }
  195. }
  196. else
  197. {
  198. if(linksta != 0)
  199. {
  200. low_level_init(&gnetif);
  201. netif_set_link_up(&gnetif);
  202. #if LWIP_DHCP == 1
  203. dhcp_start(&gnetif);
  204. #endif
  205. }
  206. }
  207. }
  208. void eth_link_callback(struct netif *netif)
  209. {
  210. __IO uint32_t timeout = 0;
  211. uint32_t tmpreg,RegValue;
  212. if(netif_is_link_up(netif))
  213. {
  214. printf("network is up");
  215. if(ETH_InitStructure.ETH_AutoNegotiation != ETH_AutoNegotiation_Disable)
  216. {
  217. timeout = 0;
  218. ETH_WritePHYRegister(DP83848_PHY_ADDRESS, PHY_BCR, PHY_AutoNegotiation);
  219. do
  220. {
  221. timeout ++;
  222. }while(!(ETH_ReadPHYRegister(DP83848_PHY_ADDRESS, PHY_BSR) & PHY_AutoNego_Complete) && (timeout < (uint32_t)PHY_READ_TO));
  223. RegValue = ETH_ReadPHYRegister(DP83848_PHY_ADDRESS, PHY_SR);
  224. if(RegValue & PHY_DUPLEX_STATUS) ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
  225. else ETH_InitStructure.ETH_Mode = ETH_Mode_HalfDuplex;
  226. if(RegValue & PHY_SPEED_STATUS) ETH_InitStructure.ETH_Speed = ETH_Speed_10M;
  227. else ETH_InitStructure.ETH_Speed = ETH_Speed_100M;
  228. tmpreg = ETH->MACCR;
  229. tmpreg |= (uint32_t)(ETH_InitStructure.ETH_Speed | ETH_InitStructure.ETH_Mode);
  230. ETH->MACCR = (uint32_t)tmpreg;
  231. _eth_delay_(ETH_REG_WRITE_DELAY);
  232. tmpreg = ETH->MACCR;
  233. ETH->MACCR = tmpreg;
  234. }
  235. ETH_Start();
  236. netif_set_up(&gnetif);
  237. }
  238. else
  239. {
  240. printf("network is down");
  241. ETH_Stop();
  242. netif_set_down(&gnetif);
  243. }
  244. }
  245. void ethphycheck_timer(void *arg)
  246. {
  247. LWIP_UNUSED_ARG(arg);
  248. hd_ethCheckLinkStatus(DP83848_PHY_ADDRESS);
  249. sys_timeout(1000, ethphycheck_timer, NULL);
  250. }
  251. ///////////////////////////////////////////////////////LWIP///////////////////////////////////////////////////////////////////////////
  252. //初始化LWIP协议栈
  253. void lwIP_Init(void)
  254. {
  255. struct ip_addr ipaddr;
  256. struct ip_addr netmask;
  257. struct ip_addr gw;
  258. tcpip_init(NULL, NULL);
  259. #if LWIP_DHCP == 1
  260. ip_addr_set_zero(&ipaddr);
  261. ip_addr_set_zero(&netmask);
  262. ip_addr_set_zero(&gw);
  263. #else
  264. // IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
  265. // IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
  266. // IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
  267. #endif
  268. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input);
  269. netif_set_default(&gnetif);
  270. // netif_set_up(&gnetif);
  271. if(EthStatus == (ETH_LINK_FLAG | ETH_INIT_FLAG))
  272. {
  273. gnetif.flags |= NETIF_FLAG_LINK_UP;
  274. netif_set_up(&gnetif);
  275. ETH_Start();
  276. #if LWIP_DHCP == 1
  277. dhcp_start(&gnetif);
  278. while(ip_addr_cmp(&(gnetif.ip_addr), &ipaddr))
  279. {
  280. OSTimeDly(10/(1000/OS_TICKS_PER_SEC));
  281. }
  282. printf("dhcp ip_addr is %s \r\n", inet_ntoa(gnetif.ip_addr));
  283. #endif
  284. }
  285. else
  286. {
  287. netif_set_down(&gnetif);
  288. gnetif.flags &= ~NETIF_FLAG_LINK_UP;
  289. ETH_Stop();
  290. }
  291. netif_set_link_callback(&gnetif, eth_link_callback);
  292. sys_timeout(2000, ethphycheck_timer, NULL);
  293. }