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.

100 lines
3.2 KiB

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. #define TAG "network"
  31. static struct netif gnetif;
  32. static ip4_addr_t ipaddr;
  33. static ip4_addr_t netmask;
  34. static ip4_addr_t gw;
  35. static uint8_t IP_ADDRESS[4];
  36. static uint8_t NETMASK_ADDRESS[4];
  37. static uint8_t GATEWAY_ADDRESS[4];
  38. uint8_t g_mac[6];
  39. static void ethernet_link_status_updated(struct netif *netif) {
  40. if (netif_is_up(netif)) {
  41. ZLOGI(TAG, "ethernet_link_status_updated: netif_is_up");
  42. } else {
  43. ZLOGI(TAG, "ethernet_link_status_updated: netif_is_down");
  44. }
  45. }
  46. void network_service_init() {
  47. ZLOGI(TAG, "network_service_init");
  48. *(uint32_t *)(IP_ADDRESS) = config_get()->ip;
  49. *(uint32_t *)(NETMASK_ADDRESS) = config_get()->netmask;
  50. *(uint32_t *)(GATEWAY_ADDRESS) = config_get()->gw;
  51. // IP_ADDRESS[0] = 192;
  52. // IP_ADDRESS[1] = 168;
  53. // IP_ADDRESS[2] = 8;
  54. // IP_ADDRESS[3] = 10;
  55. // NETMASK_ADDRESS[0] = 255;
  56. // NETMASK_ADDRESS[1] = 255;
  57. // NETMASK_ADDRESS[2] = 255;
  58. // NETMASK_ADDRESS[3] = 255;
  59. // GATEWAY_ADDRESS[0] = 192;
  60. // GATEWAY_ADDRESS[1] = 168;
  61. // GATEWAY_ADDRESS[2] = 8;
  62. // GATEWAY_ADDRESS[3] = 1;
  63. memcpy(g_mac, config_get()->mac, 6);
  64. /* Initilialize the LwIP stack with RTOS */
  65. tcpip_init(NULL, NULL);
  66. /* IP addresses initialization without DHCP (IPv4) */
  67. IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
  68. IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1], NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
  69. IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);
  70. /* add the network interface (IPv4/IPv6) with RTOS */
  71. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  72. /* Registers the default network interface */
  73. netif_set_default(&gnetif);
  74. if (netif_is_link_up(&gnetif)) {
  75. /* When the netif is fully configured this function must be called */
  76. netif_set_up(&gnetif);
  77. } else {
  78. /* When the netif link is down this function must be called */
  79. netif_set_down(&gnetif);
  80. }
  81. /* Set the link callback function, this function is called on change of link status*/
  82. netif_set_link_callback(&gnetif, ethernet_link_status_updated);
  83. osThreadDef(EthLink, ethernet_link_thread, osPriorityBelowNormal, 0, 512);
  84. osThreadCreate(osThread(EthLink), &gnetif);
  85. }