lwip_dm9k.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "lwip_dm9k.h"
  2. #include "dm9k.h"
  3. #include "ethernetif_dm9k.h"
  4. #include "lwip/tcpip.h"
  5. #include "lwip_init.h"
  6. #include "malloc.h"
  7. #include "memory_manager.h"
  8. struct netif dm9k_netif; // 定义一个网络接口
  9. #define DM9K_TASK_PRIO 10
  10. #define DM9K_TASK_STK_SIZE 512
  11. CPU_STK dm9k_task_stk[DM9K_TASK_STK_SIZE];
  12. static void ethernet_link_status_updated(struct netif *netif);
  13. // lwip 默认IP设置
  14. void lwip_dm9k_default_ip_set(__lwip_dev *lwipx)
  15. {
  16. // 默认远端IP为:192.168.1.100
  17. lwipx->remoteip[0] = 192;
  18. lwipx->remoteip[1] = 168;
  19. lwipx->remoteip[2] = 1;
  20. lwipx->remoteip[3] = 10;
  21. // MAC地址设置(高三字节固定为:2.0.0,低三字节用STM32唯一ID)
  22. lwipx->mac[0] = 2; // 高三字节(IEEE称之为组织唯一ID,OUI)地址固定为:2.0.0
  23. lwipx->mac[1] = 0;
  24. lwipx->mac[2] = 0;
  25. lwipx->mac[3] = 0x90; // 低三字节用STM32的唯一ID
  26. lwipx->mac[4] = 0x00;
  27. lwipx->mac[5] = 0xae;
  28. // 默认本地IP为:192.168.1.30
  29. lwipx->ip[0] = 192;
  30. lwipx->ip[1] = 168;
  31. lwipx->ip[2] = 1;
  32. lwipx->ip[3] = 40;
  33. // 默认子网掩码:255.255.255.0
  34. lwipx->netmask[0] = 255;
  35. lwipx->netmask[1] = 255;
  36. lwipx->netmask[2] = 255;
  37. lwipx->netmask[3] = 0;
  38. // 默认网关:192.168.1.1
  39. lwipx->gateway[0] = 192;
  40. lwipx->gateway[1] = 168;
  41. lwipx->gateway[2] = 1;
  42. lwipx->gateway[3] = 1;
  43. lwipx->dhcpstatus = 0; // 没有DHCP
  44. }
  45. void ethernet_dm9k_link_thread(void const *argument)
  46. {
  47. struct netif *netif = (struct netif *)(argument);
  48. for (;;)
  49. {
  50. ethernet_link_check_state(netif);
  51. OSTimeDly(2000);
  52. }
  53. }
  54. // LWIP初始化(LWIP启动的时候使用)
  55. void lwip_dm9k_init(void)
  56. {
  57. ip4_addr_t ipaddr;
  58. ip4_addr_t netmask;
  59. ip4_addr_t gw;
  60. uint8_t linkchanged = 0;
  61. lwip_dm9k_default_ip_set(&dm9kdev); // 设置默认IP等信息
  62. dm9k_init();
  63. #ifdef USE_DHCP // 使用动态IP
  64. ip_addr_set_zero_ip4(&ipaddr);
  65. ip_addr_set_zero_ip4(&netmask);
  66. ip_addr_set_zero_ip4(&gw);
  67. #else // 使用静态IP
  68. IP4_ADDR(&ipaddr, dm9kdev.ip[0], dm9kdev.ip[1], dm9kdev.ip[2], dm9kdev.ip[3]);
  69. IP4_ADDR(&netmask, dm9kdev.netmask[0], dm9kdev.netmask[1], dm9kdev.netmask[2], dm9kdev.netmask[3]);
  70. IP4_ADDR(&gw, dm9kdev.gateway[0], dm9kdev.gateway[1], dm9kdev.gateway[2], dm9kdev.gateway[3]);
  71. #endif
  72. netif_add(&dm9k_netif, &ipaddr, &netmask, &gw, NULL, &ethernetif_dm9k_init, &tcpip_input); // 向网卡列表中添加一个网口
  73. netif_set_default(&dm9k_netif); // 设置netif为默认网口
  74. printf("LwIP Registers dm9k_netif default network interface....\r\n");
  75. /* Set the link callback function, this function is called on change of link status*/
  76. #if LWIP_NETIF_LINK_CALLBACK
  77. netif_set_link_callback(&dm9k_netif, ethernet_link_status_updated);
  78. #endif
  79. // OSTaskCreateExt((void (*)(void *))ethernet_dm9k_link_thread,
  80. // (void const *)&dm9k_netif,
  81. // (OS_STK *)&dm9k_task_stk[DM9K_TASK_STK_SIZE - 1],
  82. // (INT8U)DM9K_TASK_PRIO,
  83. // (INT16U)DM9K_TASK_PRIO,
  84. // (OS_STK *)&dm9k_task_stk[0],
  85. // (INT32U)DM9K_TASK_STK_SIZE,
  86. // (void *)0,
  87. // (INT16U)OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR | OS_TASK_OPT_SAVE_FP);
  88. linkchanged = dm9k_linkstat();
  89. if (netif_is_link_up(&dm9k_netif) && (linkchanged == 0))
  90. {
  91. printf("dm9k LwIP Error interface....\r\n");
  92. netif_set_down(&dm9k_netif);
  93. netif_set_link_down(&dm9k_netif);
  94. }
  95. else if (!netif_is_link_up(&dm9k_netif) && (linkchanged == 1))
  96. {
  97. printf("dm9k LwIP Finish interface....\r\n");
  98. netif_set_up(&dm9k_netif);
  99. netif_set_link_up(&dm9k_netif);
  100. }
  101. // ethernet_link_check_state(&dm9k_netif);
  102. // if (ethernet_link_check_state() == 1)
  103. // {
  104. // dm9k_netif.flags |= NETIF_FLAG_LINK_UP;
  105. // netif_set_up(&dm9k_netif);
  106. // }
  107. }
  108. /**
  109. * @brief This function notify user about link status changement.
  110. * @param netif: the network interface
  111. * @retval None
  112. */
  113. static void ethernet_link_status_updated(struct netif *netif)
  114. {
  115. #if LWIP_DHCP
  116. if (DHCPSemaphore == NULL)
  117. {
  118. osSemaphoreDef(ETH_SEM);
  119. DHCPSemaphore = osSemaphoreCreate(osSemaphore(ETH_SEM), 1);
  120. }
  121. #else
  122. ip_addr_t ipaddr;
  123. ip_addr_t netmask;
  124. ip_addr_t gw;
  125. #endif /* USE_DHCP */
  126. if (netif_is_link_up(netif))
  127. {
  128. #if LWIP_DHCP
  129. /* Update DHCP state machine */
  130. dm9kdev.dhcpstatus = DHCP_START;
  131. osSemaphoreRelease(DHCPSemaphore);
  132. #else
  133. IP4_ADDR(&ipaddr, dm9kdev.ip[0], dm9kdev.ip[1], dm9kdev.ip[2], dm9kdev.ip[3]);
  134. IP4_ADDR(&netmask, dm9kdev.netmask[0], dm9kdev.netmask[1], dm9kdev.netmask[2], dm9kdev.netmask[3]);
  135. IP4_ADDR(&gw, dm9kdev.gateway[0], dm9kdev.gateway[1], dm9kdev.gateway[2], dm9kdev.gateway[3]);
  136. dm9kdev.dhcpstatus = DHCP_ADDRESS_ASSIGNED; // 标记网络开启成功。
  137. #endif /* USE_DHCP */
  138. }
  139. else
  140. {
  141. #if LWIP_DHCP
  142. /* Update DHCP state machine */
  143. osSemaphoreRelease(DHCPSemaphore);
  144. #endif /* USE_DHCP */
  145. dm9kdev.dhcpstatus = DHCP_LINK_DOWN;
  146. }
  147. }