You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.3 KiB

2 years ago
2 years ago
2 years ago
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * File Name : LWIP.c
  5. * Description : This file provides initialization code for LWIP
  6. * middleWare.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2024 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "lwip.h"
  22. #include "lwip/init.h"
  23. #include "lwip/netif.h"
  24. #if defined(__CC_ARM)
  25. #include "lwip/sio.h"
  26. #endif
  27. #include "ethernetif.h"
  28. //
  29. #include "base_service/base_service.h"
  30. #include "base_service/task_level_config.h"
  31. #define TAG "network"
  32. static struct netif gnetif;
  33. static ip4_addr_t ipaddr;
  34. static ip4_addr_t netmask;
  35. static ip4_addr_t gw;
  36. static uint8_t IP_ADDRESS[4];
  37. static uint8_t NETMASK_ADDRESS[4];
  38. static uint8_t GATEWAY_ADDRESS[4];
  39. uint8_t g_mac[6];
  40. static void ethernet_link_status_updated(struct netif *netif) {
  41. if (netif_is_up(netif)) {
  42. ZLOGI(TAG, "ethernet_link_status_updated: netif_is_up");
  43. } else {
  44. ZLOGI(TAG, "ethernet_link_status_updated: netif_is_down");
  45. }
  46. }
  47. void network_service_init() {
  48. ZLOGI(TAG, "network_service_init");
  49. *(uint32_t *)(IP_ADDRESS) = config_get()->ip;
  50. *(uint32_t *)(NETMASK_ADDRESS) = config_get()->netmask;
  51. *(uint32_t *)(GATEWAY_ADDRESS) = config_get()->gw;
  52. // IP_ADDRESS[0] = 192;
  53. // IP_ADDRESS[1] = 168;
  54. // IP_ADDRESS[2] = 8;
  55. // IP_ADDRESS[3] = 10;
  56. // NETMASK_ADDRESS[0] = 255;
  57. // NETMASK_ADDRESS[1] = 255;
  58. // NETMASK_ADDRESS[2] = 255;
  59. // NETMASK_ADDRESS[3] = 255;
  60. // GATEWAY_ADDRESS[0] = 192;
  61. // GATEWAY_ADDRESS[1] = 168;
  62. // GATEWAY_ADDRESS[2] = 8;
  63. // GATEWAY_ADDRESS[3] = 1;
  64. memcpy(g_mac, config_get()->mac, 6);
  65. /* Initilialize the LwIP stack with RTOS */
  66. tcpip_init(NULL, NULL);
  67. /* IP addresses initialization without DHCP (IPv4) */
  68. IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
  69. IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1], NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
  70. IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);
  71. /* add the network interface (IPv4/IPv6) with RTOS */
  72. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  73. /* Registers the default network interface */
  74. netif_set_default(&gnetif);
  75. if (netif_is_link_up(&gnetif)) {
  76. /* When the netif is fully configured this function must be called */
  77. netif_set_up(&gnetif);
  78. } else {
  79. /* When the netif link is down this function must be called */
  80. netif_set_down(&gnetif);
  81. }
  82. /* Set the link callback function, this function is called on change of link status*/
  83. netif_set_link_callback(&gnetif, ethernet_link_status_updated);
  84. osThreadDef(EthLink, ethernet_link_thread, NETWORK_REPORT_TASK_LEVEL, 0, 512);
  85. osThreadCreate(osThread(EthLink), &gnetif);
  86. }