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.

197 lines
5.1 KiB

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