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