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.

237 lines
6.2 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
  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. /* USER CODE END 3 */
  114. }
  115. #ifdef USE_OBSOLETE_USER_CODE_SECTION_4
  116. /* Kept to help code migration. (See new 4_1, 4_2... sections) */
  117. /* Avoid to use this user section which will become obsolete. */
  118. /* USER CODE BEGIN 4 */
  119. /* USER CODE END 4 */
  120. #endif
  121. /**
  122. * @brief Notify the User about the network interface config status
  123. * @param netif: the network interface
  124. * @retval None
  125. */
  126. static void ethernet_link_status_updated(struct netif *netif)
  127. {
  128. if (netif_is_up(netif))
  129. {
  130. /* USER CODE BEGIN 5 */
  131. /* USER CODE END 5 */
  132. }
  133. else /* netif is down */
  134. {
  135. /* USER CODE BEGIN 6 */
  136. /* USER CODE END 6 */
  137. }
  138. }
  139. #if defined ( __CC_ARM ) /* MDK ARM Compiler */
  140. /**
  141. * Opens a serial device for communication.
  142. *
  143. * @param devnum device number
  144. * @return handle to serial device if successful, NULL otherwise
  145. */
  146. sio_fd_t sio_open(u8_t devnum)
  147. {
  148. sio_fd_t sd;
  149. /* USER CODE BEGIN 7 */
  150. sd = 0; // dummy code
  151. /* USER CODE END 7 */
  152. return sd;
  153. }
  154. /**
  155. * Sends a single character to the serial device.
  156. *
  157. * @param c character to send
  158. * @param fd serial device handle
  159. *
  160. * @note This function will block until the character can be sent.
  161. */
  162. void sio_send(u8_t c, sio_fd_t fd)
  163. {
  164. /* USER CODE BEGIN 8 */
  165. /* USER CODE END 8 */
  166. }
  167. /**
  168. * Reads from the serial device.
  169. *
  170. * @param fd serial device handle
  171. * @param data pointer to data buffer for receiving
  172. * @param len maximum length (in bytes) of data to receive
  173. * @return number of bytes actually received - may be 0 if aborted by sio_read_abort
  174. *
  175. * @note This function will block until data can be received. The blocking
  176. * can be cancelled by calling sio_read_abort().
  177. */
  178. u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
  179. {
  180. u32_t recved_bytes;
  181. /* USER CODE BEGIN 9 */
  182. recved_bytes = 0; // dummy code
  183. /* USER CODE END 9 */
  184. return recved_bytes;
  185. }
  186. /**
  187. * Tries to read from the serial device. Same as sio_read but returns
  188. * immediately if no data is available and never blocks.
  189. *
  190. * @param fd serial device handle
  191. * @param data pointer to data buffer for receiving
  192. * @param len maximum length (in bytes) of data to receive
  193. * @return number of bytes actually received
  194. */
  195. u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
  196. {
  197. u32_t recved_bytes;
  198. /* USER CODE BEGIN 10 */
  199. recved_bytes = 0; // dummy code
  200. /* USER CODE END 10 */
  201. return recved_bytes;
  202. }
  203. #endif /* MDK ARM Compiler */