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.

227 lines
5.8 KiB

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