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.

241 lines
6.3 KiB

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