lwip_dm9k.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 = {0}; // 定义一个网络接口
  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. lwip_dm9k_default_ip_set(&dm9kdev); //设置默认IP等信息
  61. dm9k_init();
  62. #ifdef USE_DHCP //使用动态IP
  63. ip_addr_set_zero_ip4(&ipaddr);
  64. ip_addr_set_zero_ip4(&netmask);
  65. ip_addr_set_zero_ip4(&gw);
  66. #else //使用静态IP
  67. IP4_ADDR(&ipaddr, dm9kdev.ip[0], dm9kdev.ip[1], dm9kdev.ip[2], dm9kdev.ip[3]);
  68. IP4_ADDR(&netmask, dm9kdev.netmask[0], dm9kdev.netmask[1], dm9kdev.netmask[2], dm9kdev.netmask[3]);
  69. IP4_ADDR(&gw, dm9kdev.gateway[0], dm9kdev.gateway[1], dm9kdev.gateway[2], dm9kdev.gateway[3]);
  70. #endif
  71. netif_add(&dm9k_netif, &ipaddr, &netmask, &gw, NULL, &ethernetif_dm9k_init, &tcpip_input); //向网卡列表中添加一个网口
  72. netif_set_default(&dm9k_netif); //设置netif为默认网口
  73. /* Set the link callback function, this function is called on change of link status*/
  74. #if LWIP_NETIF_LINK_CALLBACK
  75. netif_set_link_callback(&dm9k_netif, ethernet_link_status_updated);
  76. #endif
  77. OSTaskCreateExt((void (*)(void *))ethernet_dm9k_link_thread,
  78. (void const *)&dm9k_netif,
  79. (OS_STK *)&dm9k_task_stk[DM9K_TASK_STK_SIZE - 1],
  80. (INT8U)DM9K_TASK_PRIO,
  81. (INT16U)DM9K_TASK_PRIO,
  82. (OS_STK *)&dm9k_task_stk[0],
  83. (INT32U)DM9K_TASK_STK_SIZE,
  84. (void *)0,
  85. (INT16U)OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR | OS_TASK_OPT_SAVE_FP);
  86. // if (DM9K_LINK_STATUS == 1)
  87. // {
  88. // dm9k_netif.flags |= NETIF_FLAG_LINK_UP;
  89. // netif_set_up(&dm9k_netif);
  90. // }
  91. }
  92. /**
  93. * @brief This function notify user about link status changement.
  94. * @param netif: the network interface
  95. * @retval None
  96. */
  97. static void ethernet_link_status_updated(struct netif *netif)
  98. {
  99. #if LWIP_DHCP
  100. if (DHCPSemaphore == NULL)
  101. {
  102. osSemaphoreDef(ETH_SEM);
  103. DHCPSemaphore = osSemaphoreCreate(osSemaphore(ETH_SEM), 1);
  104. }
  105. #else
  106. ip_addr_t ipaddr;
  107. ip_addr_t netmask;
  108. ip_addr_t gw;
  109. #endif /* USE_DHCP */
  110. if (netif_is_link_up(netif))
  111. {
  112. #if LWIP_DHCP
  113. /* Update DHCP state machine */
  114. dm9kdev.dhcpstatus = DHCP_START;
  115. osSemaphoreRelease(DHCPSemaphore);
  116. #else
  117. IP4_ADDR(&ipaddr, dm9kdev.ip[0], dm9kdev.ip[1], dm9kdev.ip[2], dm9kdev.ip[3]);
  118. IP4_ADDR(&netmask, dm9kdev.netmask[0], dm9kdev.netmask[1], dm9kdev.netmask[2], dm9kdev.netmask[3]);
  119. IP4_ADDR(&gw, dm9kdev.gateway[0], dm9kdev.gateway[1], dm9kdev.gateway[2], dm9kdev.gateway[3]);
  120. dm9kdev.dhcpstatus = DHCP_ADDRESS_ASSIGNED; //标记网络开启成功。
  121. #endif /* USE_DHCP */
  122. }
  123. else
  124. {
  125. #if LWIP_DHCP
  126. /* Update DHCP state machine */
  127. osSemaphoreRelease(DHCPSemaphore);
  128. #endif /* USE_DHCP */
  129. dm9kdev.dhcpstatus = DHCP_LINK_DOWN;
  130. }
  131. }