lwip.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. ******************************************************************************
  3. * File Name : LWIP.c
  4. * Description : This file provides initialization code for LWIP
  5. * middleWare.
  6. ******************************************************************************
  7. * This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * Copyright (c) 2018 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* Includes ------------------------------------------------------------------*/
  50. #include "lwip.h"
  51. #include "lwip/init.h"
  52. #include "lwip/netif.h"
  53. #if defined ( __CC_ARM ) /* MDK ARM Compiler */
  54. #include "lwip/sio.h"
  55. #endif /* MDK ARM Compiler */
  56. /* USER CODE BEGIN 0 */
  57. /* USER CODE END 0 */
  58. /* Private function prototypes -----------------------------------------------*/
  59. /* ETH Variables initialization ----------------------------------------------*/
  60. void _Error_Handler(char * file, int line);
  61. /* USER CODE BEGIN 1 */
  62. /* USER CODE END 1 */
  63. /* Variables Initialization */
  64. struct netif gnetif;
  65. ip4_addr_t ipaddr;
  66. ip4_addr_t netmask;
  67. ip4_addr_t gw;
  68. uint8_t IP_ADDRESS[4];
  69. uint8_t NETMASK_ADDRESS[4];
  70. uint8_t GATEWAY_ADDRESS[4];
  71. /* USER CODE BEGIN 2 */
  72. /* USER CODE END 2 */
  73. /**
  74. * LwIP initialization function
  75. */
  76. void MX_LWIP_Init(void)
  77. {
  78. /* IP addresses initialization */
  79. IP_ADDRESS[0] = 172;
  80. IP_ADDRESS[1] = 16;
  81. IP_ADDRESS[2] = 18;
  82. IP_ADDRESS[3] = 171;
  83. NETMASK_ADDRESS[0] = 255;
  84. NETMASK_ADDRESS[1] = 255;
  85. NETMASK_ADDRESS[2] = 255;
  86. NETMASK_ADDRESS[3] = 0;
  87. GATEWAY_ADDRESS[0] = 0;
  88. GATEWAY_ADDRESS[1] = 0;
  89. GATEWAY_ADDRESS[2] = 0;
  90. GATEWAY_ADDRESS[3] = 0;
  91. /* Initilialize the LwIP stack without RTOS */
  92. lwip_init();
  93. /* IP addresses initialization without DHCP (IPv4) */
  94. IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
  95. IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1] , NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
  96. IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);
  97. /* add the network interface (IPv4/IPv6) without RTOS */
  98. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &ethernet_input);
  99. /* Registers the default network interface */
  100. netif_set_default(&gnetif);
  101. if (netif_is_link_up(&gnetif))
  102. {
  103. /* When the netif is fully configured this function must be called */
  104. netif_set_up(&gnetif);
  105. }
  106. else
  107. {
  108. /* When the netif link is down this function must be called */
  109. netif_set_down(&gnetif);
  110. }
  111. /* USER CODE BEGIN 3 */
  112. /* USER CODE END 3 */
  113. }
  114. #ifdef USE_OBSOLETE_USER_CODE_SECTION_4
  115. /* Kept to help code migration. (See new 4_1, 4_2... sections) */
  116. /* Avoid to use this user section which will become obsolete. */
  117. /* USER CODE BEGIN 4 */
  118. /* USER CODE END 4 */
  119. #endif
  120. /**
  121. * ----------------------------------------------------------------------
  122. * Function given to help user to continue LwIP Initialization
  123. * Up to user to complete or change this function ...
  124. * Up to user to call this function in main.c in while (1) of main(void)
  125. *-----------------------------------------------------------------------
  126. * Read a received packet from the Ethernet buffers
  127. * Send it to the lwIP stack for handling
  128. * Handle timeouts if LWIP_TIMERS is set and without RTOS
  129. * Handle the llink status if LWIP_NETIF_LINK_CALLBACK is set and without RTOS
  130. */
  131. void MX_LWIP_Process(void)
  132. {
  133. /* USER CODE BEGIN 4_1 */
  134. /* USER CODE END 4_1 */
  135. ethernetif_input(&gnetif);
  136. /* USER CODE BEGIN 4_2 */
  137. /* USER CODE END 4_2 */
  138. /* Handle timeouts */
  139. sys_check_timeouts();
  140. /* USER CODE BEGIN 4_3 */
  141. /* USER CODE END 4_3 */
  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 */
  208. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/