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.

214 lines
5.7 KiB

12 months 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 ) /* MDK ARM Compiler */
  25. #include "lwip/sio.h"
  26. #endif /* MDK ARM Compiler */
  27. #include "ethernetif.h"
  28. /* USER CODE BEGIN 0 */
  29. /* USER CODE END 0 */
  30. /* Private function prototypes -----------------------------------------------*/
  31. static void ethernet_link_status_updated(struct netif *netif);
  32. /* ETH Variables initialization ----------------------------------------------*/
  33. void Error_Handler(void);
  34. /* USER CODE BEGIN 1 */
  35. /* USER CODE END 1 */
  36. /* Variables Initialization */
  37. struct netif gnetif;
  38. ip4_addr_t ipaddr;
  39. ip4_addr_t netmask;
  40. ip4_addr_t gw;
  41. uint8_t IP_ADDRESS[4];
  42. uint8_t NETMASK_ADDRESS[4];
  43. uint8_t GATEWAY_ADDRESS[4];
  44. /* USER CODE BEGIN 2 */
  45. /* USER CODE END 2 */
  46. /**
  47. * LwIP initialization function
  48. */
  49. void MX_LWIP_Init(void)
  50. {
  51. /* IP addresses initialization */
  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. /* USER CODE BEGIN IP_ADDRESSES */
  65. /* USER CODE END IP_ADDRESSES */
  66. /* Initilialize the LwIP stack with RTOS */
  67. tcpip_init( NULL, NULL );
  68. /* IP addresses initialization without DHCP (IPv4) */
  69. IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
  70. IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1] , NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
  71. IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);
  72. /* add the network interface (IPv4/IPv6) with RTOS */
  73. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  74. /* Registers the default network interface */
  75. netif_set_default(&gnetif);
  76. if (netif_is_link_up(&gnetif))
  77. {
  78. /* When the netif is fully configured this function must be called */
  79. netif_set_up(&gnetif);
  80. }
  81. else
  82. {
  83. /* When the netif link is down this function must be called */
  84. netif_set_down(&gnetif);
  85. }
  86. /* Set the link callback function, this function is called on change of link status*/
  87. netif_set_link_callback(&gnetif, ethernet_link_status_updated);
  88. /* Create the Ethernet link handler thread */
  89. /* USER CODE BEGIN H7_OS_THREAD_DEF_CREATE_CMSIS_RTOS_V1 */
  90. osThreadDef(EthLink, ethernet_link_thread, osPriorityBelowNormal, 0, configMINIMAL_STACK_SIZE *2);
  91. osThreadCreate (osThread(EthLink), &gnetif);
  92. /* USER CODE END H7_OS_THREAD_DEF_CREATE_CMSIS_RTOS_V1 */
  93. /* USER CODE BEGIN 3 */
  94. /* USER CODE END 3 */
  95. }
  96. #ifdef USE_OBSOLETE_USER_CODE_SECTION_4
  97. /* Kept to help code migration. (See new 4_1, 4_2... sections) */
  98. /* Avoid to use this user section which will become obsolete. */
  99. /* USER CODE BEGIN 4 */
  100. /* USER CODE END 4 */
  101. #endif
  102. /**
  103. * @brief Notify the User about the network interface config status
  104. * @param netif: the network interface
  105. * @retval None
  106. */
  107. static void ethernet_link_status_updated(struct netif *netif)
  108. {
  109. if (netif_is_up(netif))
  110. {
  111. /* USER CODE BEGIN 5 */
  112. /* USER CODE END 5 */
  113. }
  114. else /* netif is down */
  115. {
  116. /* USER CODE BEGIN 6 */
  117. /* USER CODE END 6 */
  118. }
  119. }
  120. #if defined ( __CC_ARM ) /* MDK ARM Compiler */
  121. /**
  122. * Opens a serial device for communication.
  123. *
  124. * @param devnum device number
  125. * @return handle to serial device if successful, NULL otherwise
  126. */
  127. sio_fd_t sio_open(u8_t devnum)
  128. {
  129. sio_fd_t sd;
  130. /* USER CODE BEGIN 7 */
  131. sd = 0; // dummy code
  132. /* USER CODE END 7 */
  133. return sd;
  134. }
  135. /**
  136. * Sends a single character to the serial device.
  137. *
  138. * @param c character to send
  139. * @param fd serial device handle
  140. *
  141. * @note This function will block until the character can be sent.
  142. */
  143. void sio_send(u8_t c, sio_fd_t fd)
  144. {
  145. /* USER CODE BEGIN 8 */
  146. /* USER CODE END 8 */
  147. }
  148. /**
  149. * Reads from the serial device.
  150. *
  151. * @param fd serial device handle
  152. * @param data pointer to data buffer for receiving
  153. * @param len maximum length (in bytes) of data to receive
  154. * @return number of bytes actually received - may be 0 if aborted by sio_read_abort
  155. *
  156. * @note This function will block until data can be received. The blocking
  157. * can be cancelled by calling sio_read_abort().
  158. */
  159. u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
  160. {
  161. u32_t recved_bytes;
  162. /* USER CODE BEGIN 9 */
  163. recved_bytes = 0; // dummy code
  164. /* USER CODE END 9 */
  165. return recved_bytes;
  166. }
  167. /**
  168. * Tries to read from the serial device. Same as sio_read but returns
  169. * immediately if no data is available and never blocks.
  170. *
  171. * @param fd serial device handle
  172. * @param data pointer to data buffer for receiving
  173. * @param len maximum length (in bytes) of data to receive
  174. * @return number of bytes actually received
  175. */
  176. u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
  177. {
  178. u32_t recved_bytes;
  179. /* USER CODE BEGIN 10 */
  180. recved_bytes = 0; // dummy code
  181. /* USER CODE END 10 */
  182. return recved_bytes;
  183. }
  184. #endif /* MDK ARM Compiler */