lwip.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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) 2022 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. //#include "dhcp_app.h"
  29. #include <string.h>
  30. /* USER CODE BEGIN 0 */
  31. /* USER CODE END 0 */
  32. /* Private function prototypes -----------------------------------------------*/
  33. static void ethernet_link_status_updated(struct netif *netif);
  34. /* ETH Variables initialization ----------------------------------------------*/
  35. void Error_Handler(void);
  36. /* USER CODE BEGIN 1 */
  37. /* USER CODE END 1 */
  38. /* Variables Initialization */
  39. struct netif gnetif;
  40. ip4_addr_t ipaddr;
  41. ip4_addr_t netmask;
  42. ip4_addr_t gw;
  43. //uint8_t IP_ADDRESS[4];
  44. //uint8_t NETMASK_ADDRESS[4];
  45. //uint8_t GATEWAY_ADDRESS[4];
  46. /* USER CODE BEGIN OS_THREAD_ATTR_CMSIS_RTOS_V2 */
  47. #define INTERFACE_THREAD_STACK_SIZE ( 1024 )
  48. osThreadAttr_t attributes;
  49. /* USER CODE END OS_THREAD_ATTR_CMSIS_RTOS_V2 */
  50. /* USER CODE BEGIN 2 */
  51. /* ETH_CODE: workaround to call LOCK_TCPIP_CORE after tcpip_init in MX_LWIP_Init
  52. * This is to keep the code after MX code re-generation */
  53. static inline void tcpip_init_wrap(tcpip_init_done_fn tcpip_init_done, void *arg){
  54. tcpip_init(tcpip_init_done, arg);
  55. LOCK_TCPIP_CORE();
  56. }
  57. #define tcpip_init tcpip_init_wrap
  58. uint8_t is_link_up(void)
  59. {
  60. return netif_is_up(&gnetif);
  61. }
  62. /* USER CODE END 2 */
  63. /**
  64. * LwIP initialization function
  65. */
  66. void MX_LWIP_Init(void)
  67. {
  68. /* IP addresses initialization */
  69. // IP_ADDRESS[0] = 192;
  70. // IP_ADDRESS[1] = 168;
  71. // IP_ADDRESS[2] = 1;
  72. // IP_ADDRESS[3] = 10;
  73. // NETMASK_ADDRESS[0] = 255;
  74. // NETMASK_ADDRESS[1] = 255;
  75. // NETMASK_ADDRESS[2] = 255;
  76. // NETMASK_ADDRESS[3] = 0;
  77. // GATEWAY_ADDRESS[0] = 0;
  78. // GATEWAY_ADDRESS[1] = 0;
  79. // GATEWAY_ADDRESS[2] = 0;
  80. // GATEWAY_ADDRESS[3] = 0;
  81. /* USER CODE BEGIN IP_ADDRESSES */
  82. #if !defined(USE_DHCP)
  83. /* IP addresses initialization without DHCP (IPv4) */
  84. ipaddr_aton(STATIC_IP, &ipaddr);
  85. ipaddr_aton(STATIC_MASK, &netmask);
  86. ipaddr_aton(STATIC_GW, &gw);
  87. #else
  88. ip_addr_set_zero_ip4(&ipaddr);
  89. ip_addr_set_zero_ip4(&netmask);
  90. ip_addr_set_zero_ip4(&gw);
  91. #endif
  92. /* USER CODE END IP_ADDRESSES */
  93. /* Initilialize the LwIP stack with RTOS */
  94. tcpip_init( NULL, NULL );
  95. // IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
  96. // IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1] , NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
  97. // IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);
  98. /* add the network interface (IPv4/IPv6) with RTOS */
  99. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  100. /* Registers the default network interface */
  101. netif_set_default(&gnetif);
  102. if (netif_is_link_up(&gnetif))
  103. {
  104. /* When the netif is fully configured this function must be called */
  105. netif_set_up(&gnetif);
  106. }
  107. else
  108. {
  109. /* When the netif link is down this function must be called */
  110. netif_set_down(&gnetif);
  111. }
  112. /* Set the link callback function, this function is called on change of link status*/
  113. netif_set_link_callback(&gnetif, ethernet_link_status_updated);
  114. /* Create the Ethernet link handler thread */
  115. /* USER CODE BEGIN H7_OS_THREAD_NEW_CMSIS_RTOS_V2 */
  116. memset(&attributes, 0x0, sizeof(osThreadAttr_t));
  117. attributes.name = "EthLink";
  118. attributes.stack_size = INTERFACE_THREAD_STACK_SIZE * 2;
  119. attributes.priority = osPriorityBelowNormal;
  120. osThreadNew(ethernet_link_thread, &gnetif, &attributes);
  121. // DHCP_thread_init(&gnetif);
  122. /* USER CODE END H7_OS_THREAD_NEW_CMSIS_RTOS_V2 */
  123. /* USER CODE BEGIN 3 */
  124. /* Start DHCP negotiation for a network interface (IPv4) */
  125. #if USE_DHCP
  126. dhcp_start(&gnetif);
  127. #else
  128. netif_set_addr(&gnetif, ip_2_ip4(&ipaddr), ip_2_ip4(&netmask), ip_2_ip4(&gw));
  129. #endif
  130. /* ETH_CODE: call UNLOCK_TCPIP_CORE after we are done */
  131. UNLOCK_TCPIP_CORE();
  132. /* USER CODE END 3 */
  133. }
  134. #ifdef USE_OBSOLETE_USER_CODE_SECTION_4
  135. /* Kept to help code migration. (See new 4_1, 4_2... sections) */
  136. /* Avoid to use this user section which will become obsolete. */
  137. /* USER CODE BEGIN 4 */
  138. /* USER CODE END 4 */
  139. #endif
  140. /**
  141. * @brief Notify the User about the network interface config status
  142. * @param netif: the network interface
  143. * @retval None
  144. */
  145. static void ethernet_link_status_updated(struct netif *netif)
  146. {
  147. if (netif_is_up(netif))
  148. {
  149. /* USER CODE BEGIN 5 */
  150. /* USER CODE END 5 */
  151. }
  152. else /* netif is down */
  153. {
  154. /* USER CODE BEGIN 6 */
  155. /* USER CODE END 6 */
  156. }
  157. }
  158. #if defined ( __CC_ARM ) /* MDK ARM Compiler */
  159. /**
  160. * Opens a serial device for communication.
  161. *
  162. * @param devnum device number
  163. * @return handle to serial device if successful, NULL otherwise
  164. */
  165. sio_fd_t sio_open(u8_t devnum)
  166. {
  167. sio_fd_t sd;
  168. /* USER CODE BEGIN 7 */
  169. sd = 0; // dummy code
  170. /* USER CODE END 7 */
  171. return sd;
  172. }
  173. /**
  174. * Sends a single character to the serial device.
  175. *
  176. * @param c character to send
  177. * @param fd serial device handle
  178. *
  179. * @note This function will block until the character can be sent.
  180. */
  181. void sio_send(u8_t c, sio_fd_t fd)
  182. {
  183. /* USER CODE BEGIN 8 */
  184. /* USER CODE END 8 */
  185. }
  186. /**
  187. * Reads from the serial device.
  188. *
  189. * @param fd serial device handle
  190. * @param data pointer to data buffer for receiving
  191. * @param len maximum length (in bytes) of data to receive
  192. * @return number of bytes actually received - may be 0 if aborted by sio_read_abort
  193. *
  194. * @note This function will block until data can be received. The blocking
  195. * can be cancelled by calling sio_read_abort().
  196. */
  197. u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
  198. {
  199. u32_t recved_bytes;
  200. /* USER CODE BEGIN 9 */
  201. recved_bytes = 0; // dummy code
  202. /* USER CODE END 9 */
  203. return recved_bytes;
  204. }
  205. /**
  206. * Tries to read from the serial device. Same as sio_read but returns
  207. * immediately if no data is available and never blocks.
  208. *
  209. * @param fd serial device handle
  210. * @param data pointer to data buffer for receiving
  211. * @param len maximum length (in bytes) of data to receive
  212. * @return number of bytes actually received
  213. */
  214. u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
  215. {
  216. u32_t recved_bytes;
  217. /* USER CODE BEGIN 10 */
  218. recved_bytes = 0; // dummy code
  219. /* USER CODE END 10 */
  220. return recved_bytes;
  221. }
  222. #endif /* MDK ARM Compiler */