1 | /**
|
---|
2 | ******************************************************************************
|
---|
3 | * File Name : ethernetif.c
|
---|
4 | * Description : This file provides code for the configuration
|
---|
5 | * of the Target/ethernetif.c MiddleWare.
|
---|
6 | ******************************************************************************
|
---|
7 | * @attention
|
---|
8 | *
|
---|
9 | * <h2><center>© Copyright (c) 2021 STMicroelectronics.
|
---|
10 | * All rights reserved.</center></h2>
|
---|
11 | *
|
---|
12 | * This software component is licensed by ST under Ultimate Liberty license
|
---|
13 | * SLA0044, the "License"; You may not use this file except in compliance with
|
---|
14 | * the License. You may obtain a copy of the License at:
|
---|
15 | * www.st.com/SLA0044
|
---|
16 | *
|
---|
17 | ******************************************************************************
|
---|
18 | */
|
---|
19 |
|
---|
20 | /* Includes ------------------------------------------------------------------*/
|
---|
21 | #include "main.h"
|
---|
22 | #include "lwip/opt.h"
|
---|
23 |
|
---|
24 | #include "lwip/timeouts.h"
|
---|
25 | #include "netif/ethernet.h"
|
---|
26 | #include "netif/etharp.h"
|
---|
27 | #include "lwip/ethip6.h"
|
---|
28 | #include "ethernetif.h"
|
---|
29 | #include <string.h>
|
---|
30 | #include "cmsis_os.h"
|
---|
31 | #include "lwip/tcpip.h"
|
---|
32 | #include "lwip.h"
|
---|
33 | /* Within 'USER CODE' section, code will be kept by default at each generation */
|
---|
34 | /* USER CODE BEGIN 0 */
|
---|
35 |
|
---|
36 | /* USER CODE END 0 */
|
---|
37 |
|
---|
38 | /* Private define ------------------------------------------------------------*/
|
---|
39 | /* The time to block waiting for input. */
|
---|
40 | #define TIME_WAITING_FOR_INPUT ( portMAX_DELAY )
|
---|
41 | /* USER CODE BEGIN OS_THREAD_STACK_SIZE_WITH_RTOS */
|
---|
42 | /* Stack size of the interface thread */
|
---|
43 | #define INTERFACE_THREAD_STACK_SIZE ( 1500 )
|
---|
44 | /* USER CODE END OS_THREAD_STACK_SIZE_WITH_RTOS */
|
---|
45 | /* Network interface name */
|
---|
46 | #define IFNAME0 's'
|
---|
47 | #define IFNAME1 't'
|
---|
48 |
|
---|
49 | /* USER CODE BEGIN 1 */
|
---|
50 | struct eth_addr MACAddr = {0x02, 0xAD, 0xC8, 0xFF, 0xFF, 0xFF};
|
---|
51 | /* USER CODE END 1 */
|
---|
52 | extern network_settings table_network[MAX_IP];
|
---|
53 | /* Private variables ---------------------------------------------------------*/
|
---|
54 | #if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
---|
55 | #pragma data_alignment=4
|
---|
56 | #endif
|
---|
57 | __ALIGN_BEGIN ETH_DMADescTypeDef DMARxDscrTab[ETH_RXBUFNB] __ALIGN_END;/* Ethernet Rx MA Descriptor */
|
---|
58 |
|
---|
59 | #if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
---|
60 | #pragma data_alignment=4
|
---|
61 | #endif
|
---|
62 | __ALIGN_BEGIN ETH_DMADescTypeDef DMATxDscrTab[ETH_TXBUFNB] __ALIGN_END;/* Ethernet Tx DMA Descriptor */
|
---|
63 |
|
---|
64 | #if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
---|
65 | #pragma data_alignment=4
|
---|
66 | #endif
|
---|
67 | __ALIGN_BEGIN uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE] __ALIGN_END; /* Ethernet Receive Buffer */
|
---|
68 |
|
---|
69 | #if defined ( __ICCARM__ ) /*!< IAR Compiler */
|
---|
70 | #pragma data_alignment=4
|
---|
71 | #endif
|
---|
72 | __ALIGN_BEGIN uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE] __ALIGN_END; /* Ethernet Transmit Buffer */
|
---|
73 |
|
---|
74 | /* USER CODE BEGIN 2 */
|
---|
75 |
|
---|
76 | /* USER CODE END 2 */
|
---|
77 |
|
---|
78 | /* Semaphore to signal incoming packets */
|
---|
79 | osSemaphoreId s_xSemaphore = NULL;
|
---|
80 | /* Global Ethernet handle */
|
---|
81 | ETH_HandleTypeDef heth;
|
---|
82 |
|
---|
83 | /* USER CODE BEGIN 3 */
|
---|
84 |
|
---|
85 | /* USER CODE END 3 */
|
---|
86 |
|
---|
87 | /* Private functions ---------------------------------------------------------*/
|
---|
88 |
|
---|
89 | void HAL_ETH_MspInit(ETH_HandleTypeDef* ethHandle)
|
---|
90 | {
|
---|
91 | GPIO_InitTypeDef GPIO_InitStruct = {0};
|
---|
92 | if(ethHandle->Instance==ETH)
|
---|
93 | {
|
---|
94 | /* USER CODE BEGIN ETH_MspInit 0 */
|
---|
95 |
|
---|
96 | /* USER CODE END ETH_MspInit 0 */
|
---|
97 | /* Enable Peripheral clock */
|
---|
98 | __HAL_RCC_ETH_CLK_ENABLE();
|
---|
99 |
|
---|
100 | __HAL_RCC_GPIOC_CLK_ENABLE();
|
---|
101 | __HAL_RCC_GPIOA_CLK_ENABLE();
|
---|
102 | __HAL_RCC_GPIOB_CLK_ENABLE();
|
---|
103 | /**ETH GPIO Configuration
|
---|
104 | PC1 ------> ETH_MDC
|
---|
105 | PA1 ------> ETH_REF_CLK
|
---|
106 | PA2 ------> ETH_MDIO
|
---|
107 | PA7 ------> ETH_CRS_DV
|
---|
108 | PC4 ------> ETH_RXD0
|
---|
109 | PC5 ------> ETH_RXD1
|
---|
110 | PB11 ------> ETH_TX_EN
|
---|
111 | PB12 ------> ETH_TXD0
|
---|
112 | PB13 ------> ETH_TXD1
|
---|
113 | */
|
---|
114 | GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
|
---|
115 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
---|
116 | GPIO_InitStruct.Pull = GPIO_NOPULL;
|
---|
117 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
---|
118 | GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
|
---|
119 | HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
---|
120 |
|
---|
121 | GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_7;
|
---|
122 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
---|
123 | GPIO_InitStruct.Pull = GPIO_NOPULL;
|
---|
124 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
---|
125 | GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
|
---|
126 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
---|
127 |
|
---|
128 | GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13;
|
---|
129 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
---|
130 | GPIO_InitStruct.Pull = GPIO_NOPULL;
|
---|
131 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
---|
132 | GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
|
---|
133 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
---|
134 |
|
---|
135 | /* Peripheral interrupt init */
|
---|
136 | HAL_NVIC_SetPriority(ETH_IRQn, 5, 0);
|
---|
137 | HAL_NVIC_EnableIRQ(ETH_IRQn);
|
---|
138 | /* USER CODE BEGIN ETH_MspInit 1 */
|
---|
139 |
|
---|
140 | /* USER CODE END ETH_MspInit 1 */
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | void HAL_ETH_MspDeInit(ETH_HandleTypeDef* ethHandle)
|
---|
145 | {
|
---|
146 | if(ethHandle->Instance==ETH)
|
---|
147 | {
|
---|
148 | /* USER CODE BEGIN ETH_MspDeInit 0 */
|
---|
149 |
|
---|
150 | /* USER CODE END ETH_MspDeInit 0 */
|
---|
151 | /* Peripheral clock disable */
|
---|
152 | __HAL_RCC_ETH_CLK_DISABLE();
|
---|
153 |
|
---|
154 | /**ETH GPIO Configuration
|
---|
155 | PC1 ------> ETH_MDC
|
---|
156 | PA1 ------> ETH_REF_CLK
|
---|
157 | PA2 ------> ETH_MDIO
|
---|
158 | PA7 ------> ETH_CRS_DV
|
---|
159 | PC4 ------> ETH_RXD0
|
---|
160 | PC5 ------> ETH_RXD1
|
---|
161 | PB11 ------> ETH_TX_EN
|
---|
162 | PB12 ------> ETH_TXD0
|
---|
163 | PB13 ------> ETH_TXD1
|
---|
164 | */
|
---|
165 | HAL_GPIO_DeInit(GPIOC, GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5);
|
---|
166 |
|
---|
167 | HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_7);
|
---|
168 |
|
---|
169 | HAL_GPIO_DeInit(GPIOB, GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13);
|
---|
170 |
|
---|
171 | /* Peripheral interrupt Deinit*/
|
---|
172 | HAL_NVIC_DisableIRQ(ETH_IRQn);
|
---|
173 |
|
---|
174 | /* USER CODE BEGIN ETH_MspDeInit 1 */
|
---|
175 |
|
---|
176 | /* USER CODE END ETH_MspDeInit 1 */
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * @brief Ethernet Rx Transfer completed callback
|
---|
182 | * @param heth: ETH handle
|
---|
183 | * @retval None
|
---|
184 | */
|
---|
185 | void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
|
---|
186 | {
|
---|
187 | osSemaphoreRelease(s_xSemaphore);
|
---|
188 | }
|
---|
189 |
|
---|
190 | /* USER CODE BEGIN 4 */
|
---|
191 |
|
---|
192 | /* USER CODE END 4 */
|
---|
193 |
|
---|
194 | /*******************************************************************************
|
---|
195 | LL Driver Interface ( LwIP stack --> ETH)
|
---|
196 | *******************************************************************************/
|
---|
197 | /**
|
---|
198 | * In this function, the hardware should be initialized.
|
---|
199 | * Called from ethernetif_init().
|
---|
200 | *
|
---|
201 | * @param netif the already initialized lwip network interface structure
|
---|
202 | * for this ethernetif
|
---|
203 | */
|
---|
204 | //void ethernetif_input(void * argument);
|
---|
205 |
|
---|
206 | void mac_init(void)
|
---|
207 | {
|
---|
208 | // HAL_StatusTypeDef hal_eth_init_status;
|
---|
209 |
|
---|
210 | heth.Instance = ETH;
|
---|
211 | heth.Init.AutoNegotiation = ETH_AUTONEGOTIATION_ENABLE;
|
---|
212 | heth.Init.Speed = ETH_SPEED_100M;
|
---|
213 | heth.Init.DuplexMode = ETH_MODE_FULLDUPLEX;
|
---|
214 | heth.Init.PhyAddress = PHY_KSZ8081_PHY_ADDRESS;
|
---|
215 | heth.Init.MACAddr = MACAddr.addr;
|
---|
216 | heth.Init.RxMode = ETH_RXINTERRUPT_MODE;
|
---|
217 | heth.Init.ChecksumMode = ETH_CHECKSUM_BY_HARDWARE;
|
---|
218 | heth.Init.MediaInterface = ETH_MEDIA_INTERFACE_RMII;
|
---|
219 |
|
---|
220 | /* USER CODE BEGIN MACADDRESS */
|
---|
221 | ETH->MACIMR = ETH_MACIMR_TSTIM | ETH_MACIMR_PMTIM;
|
---|
222 | ETH->MMCRIMR = ETH_MMCRIMR_RGUFM | ETH_MMCRIMR_RFAEM | ETH_MMCRIMR_RFCEM;
|
---|
223 | ETH->MMCTIMR = ETH_MMCTIMR_TGFM | ETH_MMCTIMR_TGFMSCM | ETH_MMCTIMR_TGFSCM;
|
---|
224 | /* USER CODE END MACADDRESS */
|
---|
225 |
|
---|
226 | HAL_ETH_Init(&heth);
|
---|
227 |
|
---|
228 | /* Initialize Tx Descriptors list: Chain Mode */
|
---|
229 | HAL_ETH_DMATxDescListInit(&heth, DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
|
---|
230 |
|
---|
231 | /* Initialize Rx Descriptors list: Chain Mode */
|
---|
232 | HAL_ETH_DMARxDescListInit(&heth, DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
|
---|
233 |
|
---|
234 | if (s_xSemaphore == NULL){
|
---|
235 |
|
---|
236 | osSemaphoreDef(SEM);
|
---|
237 | s_xSemaphore = osSemaphoreCreate(osSemaphore(SEM), 1);
|
---|
238 | osThreadDef(EthIf, ethernetif_input, osPriorityRealtime, 0, INTERFACE_THREAD_STACK_SIZE); // osPriorityRealtime = 3 è ïðè ñîçäàíèè çàäà÷è åùå +3; ïðèîðèòåò áóäåò 6
|
---|
239 | osThreadCreate (osThread(EthIf), NULL);
|
---|
240 | }
|
---|
241 |
|
---|
242 | HAL_ETH_Start(&heth);
|
---|
243 |
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | //static void low_level_init(struct netif *netif)
|
---|
248 | //{
|
---|
249 | //
|
---|
250 | //#if LWIP_ARP || LWIP_ETHERNET
|
---|
251 |
|
---|
252 | // /* set MAC hardware address length */
|
---|
253 | // netif->hwaddr_len = ETH_HWADDR_LEN;
|
---|
254 |
|
---|
255 | // /* set MAC hardware address */
|
---|
256 | // netif->hwaddr[0] = heth.Init.MACAddr[0];
|
---|
257 | // netif->hwaddr[1] = heth.Init.MACAddr[1];
|
---|
258 | // netif->hwaddr[2] = heth.Init.MACAddr[2];
|
---|
259 | // netif->hwaddr[3] = heth.Init.MACAddr[3];
|
---|
260 | // netif->hwaddr[4] = heth.Init.MACAddr[4];
|
---|
261 | // netif->hwaddr[5] = heth.Init.MACAddr[5];
|
---|
262 |
|
---|
263 | // /* maximum transfer unit */
|
---|
264 | // netif->mtu = 1500;
|
---|
265 | //netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
|
---|
266 | // /* Accept broadcast address and ARP traffic */
|
---|
267 | // /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
|
---|
268 | // #if LWIP_ARP
|
---|
269 | // netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
|
---|
270 | // #else
|
---|
271 | // netif->flags |= NETIF_FLAG_BROADCAST;
|
---|
272 | // #endif /* LWIP_ARP */
|
---|
273 | //#endif /* LWIP_ARP || LWIP_ETHERNET */
|
---|
274 |
|
---|
275 | //}
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * This function should do the actual transmission of the packet. The packet is
|
---|
279 | * contained in the pbuf that is passed to the function. This pbuf
|
---|
280 | * might be chained.
|
---|
281 | *
|
---|
282 | * @param netif the lwip network interface structure for this ethernetif
|
---|
283 | * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
|
---|
284 | * @return ERR_OK if the packet could be sent
|
---|
285 | * an err_t value if the packet couldn't be sent
|
---|
286 | *
|
---|
287 | * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
|
---|
288 | * strange results. You might consider waiting for space in the DMA queue
|
---|
289 | * to become available since the stack doesn't retry to send a packet
|
---|
290 | * dropped because of memory failure (except for the TCP timers).
|
---|
291 | */
|
---|
292 |
|
---|
293 | static err_t low_level_output(struct netif *netif, struct pbuf *p)
|
---|
294 | {
|
---|
295 | err_t errval;
|
---|
296 | struct pbuf *q;
|
---|
297 | uint8_t *buffer = (uint8_t *)(heth.TxDesc->Buffer1Addr);
|
---|
298 | __IO ETH_DMADescTypeDef *DmaTxDesc;
|
---|
299 | uint32_t framelength = 0;
|
---|
300 | uint32_t bufferoffset = 0;
|
---|
301 | uint32_t byteslefttocopy = 0;
|
---|
302 | uint32_t payloadoffset = 0;
|
---|
303 | DmaTxDesc = heth.TxDesc;
|
---|
304 | bufferoffset = 0;
|
---|
305 |
|
---|
306 | /* copy frame from pbufs to driver buffers */
|
---|
307 | for(q = p; q != NULL; q = q->next)
|
---|
308 | {
|
---|
309 | /* Is this buffer available? If not, goto error */
|
---|
310 | if((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
|
---|
311 | {
|
---|
312 | errval = ERR_USE;
|
---|
313 | goto error;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /* Get bytes in current lwIP buffer */
|
---|
317 | byteslefttocopy = q->len;
|
---|
318 | payloadoffset = 0;
|
---|
319 |
|
---|
320 | /* Check if the length of data to copy is bigger than Tx buffer size*/
|
---|
321 | while( (byteslefttocopy + bufferoffset) > ETH_TX_BUF_SIZE )
|
---|
322 | {
|
---|
323 | /* Copy data to Tx buffer*/
|
---|
324 | memcpy( (uint8_t*)((uint8_t*)buffer + bufferoffset), (uint8_t*)((uint8_t*)q->payload + payloadoffset), (ETH_TX_BUF_SIZE - bufferoffset) );
|
---|
325 |
|
---|
326 | /* Point to next descriptor */
|
---|
327 | DmaTxDesc = (ETH_DMADescTypeDef *)(DmaTxDesc->Buffer2NextDescAddr);
|
---|
328 |
|
---|
329 | /* Check if the buffer is available */
|
---|
330 | if((DmaTxDesc->Status & ETH_DMATXDESC_OWN) != (uint32_t)RESET)
|
---|
331 | {
|
---|
332 | errval = ERR_USE;
|
---|
333 | goto error;
|
---|
334 | }
|
---|
335 |
|
---|
336 | buffer = (uint8_t *)(DmaTxDesc->Buffer1Addr);
|
---|
337 |
|
---|
338 | byteslefttocopy = byteslefttocopy - (ETH_TX_BUF_SIZE - bufferoffset);
|
---|
339 | payloadoffset = payloadoffset + (ETH_TX_BUF_SIZE - bufferoffset);
|
---|
340 | framelength = framelength + (ETH_TX_BUF_SIZE - bufferoffset);
|
---|
341 | bufferoffset = 0;
|
---|
342 | }
|
---|
343 |
|
---|
344 | /* Copy the remaining bytes */
|
---|
345 | memcpy( (uint8_t*)((uint8_t*)buffer + bufferoffset), (uint8_t*)((uint8_t*)q->payload + payloadoffset), byteslefttocopy );
|
---|
346 | bufferoffset = bufferoffset + byteslefttocopy;
|
---|
347 | framelength = framelength + byteslefttocopy;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /* Prepare transmit descriptors to give to DMA */
|
---|
351 | HAL_ETH_TransmitFrame(&heth, framelength);
|
---|
352 |
|
---|
353 | errval = ERR_OK;
|
---|
354 |
|
---|
355 | error:
|
---|
356 | /* When Transmit Underflow flag is set, clear it and issue a Transmit Poll Demand to resume transmission */
|
---|
357 | if ((heth.Instance->DMASR & ETH_DMASR_TUS) != (uint32_t)RESET)
|
---|
358 | {
|
---|
359 | /* Clear TUS ETHERNET DMA flag */
|
---|
360 | heth.Instance->DMASR = ETH_DMASR_TUS;
|
---|
361 |
|
---|
362 | /* Resume DMA transmission*/
|
---|
363 | heth.Instance->DMATPDR = 0;
|
---|
364 | }
|
---|
365 | return errval;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * Should allocate a pbuf and transfer the bytes of the incoming
|
---|
370 | * packet from the interface into the pbuf.
|
---|
371 | *
|
---|
372 | * @param netif the lwip network interface structure for this ethernetif
|
---|
373 | * @return a pbuf filled with the received packet (including MAC header)
|
---|
374 | * NULL on memory error
|
---|
375 | */
|
---|
376 | static struct pbuf * low_level_input(struct netif *netif)
|
---|
377 | {
|
---|
378 | struct pbuf *p = NULL;
|
---|
379 | struct pbuf *q = NULL;
|
---|
380 | uint16_t len = 0;
|
---|
381 | uint8_t *buffer;
|
---|
382 | __IO ETH_DMADescTypeDef *dmarxdesc;
|
---|
383 | uint32_t bufferoffset = 0;
|
---|
384 | uint32_t payloadoffset = 0;
|
---|
385 | uint32_t byteslefttocopy = 0;
|
---|
386 | uint32_t i=0;
|
---|
387 | HAL_StatusTypeDef status;
|
---|
388 | /* get received frame */
|
---|
389 |
|
---|
390 |
|
---|
391 | LOCK_TCPIP_CORE();
|
---|
392 | status = HAL_ETH_GetReceivedFrame_IT(&heth);
|
---|
393 | UNLOCK_TCPIP_CORE();
|
---|
394 |
|
---|
395 | if (status != HAL_OK) return NULL;
|
---|
396 | //else __DEBUG(DEBUG_ETH, "low level receive OK\r\n");
|
---|
397 |
|
---|
398 |
|
---|
399 | /* Obtain the size of the packet and put it into the "len" variable. */
|
---|
400 | len = heth.RxFrameInfos.length;
|
---|
401 | buffer = (uint8_t *)heth.RxFrameInfos.buffer;
|
---|
402 |
|
---|
403 | if (len > 0)
|
---|
404 | {
|
---|
405 | /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
|
---|
406 | p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
|
---|
407 | }
|
---|
408 |
|
---|
409 | if (p != NULL)
|
---|
410 | {
|
---|
411 | dmarxdesc = heth.RxFrameInfos.FSRxDesc;
|
---|
412 | bufferoffset = 0;
|
---|
413 | for(q = p; q != NULL; q = q->next)
|
---|
414 | {
|
---|
415 | byteslefttocopy = q->len;
|
---|
416 | payloadoffset = 0;
|
---|
417 |
|
---|
418 | /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/
|
---|
419 | while( (byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE )
|
---|
420 | {
|
---|
421 | /* Copy data to pbuf */
|
---|
422 | memcpy( (uint8_t*)((uint8_t*)q->payload + payloadoffset), (uint8_t*)((uint8_t*)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset));
|
---|
423 |
|
---|
424 | /* Point to next descriptor */
|
---|
425 | dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
|
---|
426 | buffer = (uint8_t *)(dmarxdesc->Buffer1Addr);
|
---|
427 |
|
---|
428 | byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
|
---|
429 | payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
|
---|
430 | bufferoffset = 0;
|
---|
431 | }
|
---|
432 | /* Copy remaining data in pbuf */
|
---|
433 | memcpy( (uint8_t*)((uint8_t*)q->payload + payloadoffset), (uint8_t*)((uint8_t*)buffer + bufferoffset), byteslefttocopy);
|
---|
434 | bufferoffset = bufferoffset + byteslefttocopy;
|
---|
435 | }
|
---|
436 | }
|
---|
437 |
|
---|
438 | /* Release descriptors to DMA */
|
---|
439 | /* Point to first descriptor */
|
---|
440 | dmarxdesc = heth.RxFrameInfos.FSRxDesc;
|
---|
441 | /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
|
---|
442 | for (i=0; i< heth.RxFrameInfos.SegCount; i++)
|
---|
443 | {
|
---|
444 | dmarxdesc->Status |= ETH_DMARXDESC_OWN;
|
---|
445 | dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
|
---|
446 | }
|
---|
447 |
|
---|
448 | /* Clear Segment_Count */
|
---|
449 | heth.RxFrameInfos.SegCount =0;
|
---|
450 |
|
---|
451 | /* When Rx Buffer unavailable flag is set: clear it and resume reception */
|
---|
452 | if ((heth.Instance->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET)
|
---|
453 | {
|
---|
454 | /* Clear RBUS ETHERNET DMA flag */
|
---|
455 | heth.Instance->DMASR = ETH_DMASR_RBUS;
|
---|
456 | /* Resume DMA reception */
|
---|
457 | heth.Instance->DMARPDR = 0;
|
---|
458 | }
|
---|
459 | return p;
|
---|
460 | }
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * This function should be called when a packet is ready to be read
|
---|
464 | * from the interface. It uses the function low_level_input() that
|
---|
465 | * should handle the actual reception of bytes from the network
|
---|
466 | * interface. Then the type of the received packet is determined and
|
---|
467 | * the appropriate input function is called.
|
---|
468 | *
|
---|
469 | * @param netif the lwip network interface structure for this ethernetif
|
---|
470 | */
|
---|
471 | //uint32_t RxCount = 0;
|
---|
472 |
|
---|
473 | /**void my_ethernetif_input(struct netif *netif)
|
---|
474 | {
|
---|
475 | struct eth_hdr *ethhdr;
|
---|
476 | struct pbuf *p;
|
---|
477 | do{
|
---|
478 | p = low_level_input(netif);
|
---|
479 | if(p == NULL) return;
|
---|
480 |
|
---|
481 | heth.RxDesc->Status = ETH_DMARXDESC_OWN;
|
---|
482 | ethhdr = p->payload;
|
---|
483 | if((eth_addr_cmp(ðhdr->dest, &MACAddr)) && (htons(ethhdr->type) == ETHTYPE_IP)){
|
---|
484 | RxCount++;
|
---|
485 | __DEBUG(DEBUG_ETH, "rx packet count - %d\r\n", RxCount);
|
---|
486 | }
|
---|
487 | if (netif->input( p, netif) != ERR_OK )
|
---|
488 | {
|
---|
489 | pbuf_free(p);
|
---|
490 | p = NULL;
|
---|
491 | }
|
---|
492 |
|
---|
493 | }while(1);
|
---|
494 | }*/
|
---|
495 | //#include "ip_addr.h"
|
---|
496 |
|
---|
497 | void ethernetif_inp(void)
|
---|
498 | {
|
---|
499 | // struct pbuf *p;
|
---|
500 | // //network_settings *net_table =(network_settings*) argument;;
|
---|
501 | //// struct netif *netif;
|
---|
502 | // struct ip_hdr *iphdr;
|
---|
503 | // struct eth_hdr *ethhdr;
|
---|
504 | // ip4_addr_t addrIP;
|
---|
505 | // uint32_t dest = 0xC800A8C0;
|
---|
506 | // ip4_addr_t addrIP;
|
---|
507 | // uint8_t iphdr_hlen;
|
---|
508 | // ip4_addr_p_t ret;
|
---|
509 | // struct udp_hdr *udphdr;
|
---|
510 | // struct udp_hdr *udphdr;
|
---|
511 | // network_settings *net_table =(network_settings*) argument;;
|
---|
512 | //
|
---|
513 | // if(osSemaphoreWait(s_xSemaphore, portMAX_DELAY) == osOK){
|
---|
514 | // do
|
---|
515 | // {
|
---|
516 | // // LOCK_TCPIP_CORE();
|
---|
517 | // p = low_level_input(NULL);
|
---|
518 | // if (p != NULL)
|
---|
519 | // {
|
---|
520 | //
|
---|
521 | //// if (netif_list->input( p, &table_network[1].netif) != ERR_OK )
|
---|
522 | //// {
|
---|
523 | //// pbuf_free(p);
|
---|
524 | //// p = NULL;
|
---|
525 | //// }
|
---|
526 | //
|
---|
527 | // ethhdr = p->payload;
|
---|
528 | // if (eth_addr_cmp(ðhdr->dest, ðbroadcast)){
|
---|
529 | // __DEBUG(DEBUG_SERVCE, "broadcast ");
|
---|
530 | // }
|
---|
531 | //
|
---|
532 | // pbuf_header(p, -14);
|
---|
533 | // iphdr = (struct ip_hdr *)p->payload;
|
---|
534 | // __DEBUG(DEBUG_SERVCE, "ip dest - %08X\r\n", iphdr->dest);
|
---|
535 | // pbuf_header(p, 14);
|
---|
536 | //
|
---|
537 | // for(uint8_t i = 0; i < MAX_IP; i++){
|
---|
538 | // ip4addr_aton(table_network[i].ipv4_addr, &addrIP);
|
---|
539 | // if(addrIP.addr == iphdr->dest.addr){
|
---|
540 | //
|
---|
541 | //// if ((netif_list + i)->input( p, (netif_list + i)) != ERR_OK ){
|
---|
542 | //// pbuf_free(p);
|
---|
543 | //// p = NULL;
|
---|
544 | //// return;
|
---|
545 | //// }
|
---|
546 | // }
|
---|
547 | // }
|
---|
548 | // pbuf_free(p);
|
---|
549 | // p = NULL;
|
---|
550 | //
|
---|
551 | //
|
---|
552 | //
|
---|
553 | //
|
---|
554 | //
|
---|
555 | //
|
---|
556 | //// if (netif_list->input( p, &table_network[0].netif) != ERR_OK )
|
---|
557 | //// {
|
---|
558 | ////
|
---|
559 | //// }
|
---|
560 | //
|
---|
561 | //
|
---|
562 | //// if (netif_list->input( p, netif_list) != ERR_OK )
|
---|
563 | //// {
|
---|
564 | //// pbuf_free(p);
|
---|
565 | //// p = NULL;
|
---|
566 | //// }
|
---|
567 | // //heth.RxDesc->Status = ETH_DMARXDESC_OWN;
|
---|
568 | // /* ethhdr = p->payload;
|
---|
569 | // iphdr_hlen = IPH_HL_BYTES(iphdr);
|
---|
570 | //// pbuf_header(p, -iphdr_hlen);
|
---|
571 | //// struct udp_hdr *udphdr = (struct udp_hdr *)p->payload;
|
---|
572 | //// pbuf_header(p, iphdr_hlen);
|
---|
573 | // if((eth_addr_cmp(ðhdr->dest, &MACAddr)) && (htons(ethhdr->type) == ETHTYPE_IP)){
|
---|
574 | // RxCount++;
|
---|
575 | // __DEBUG(DEBUG_ETH, "rx packet count - %d\r\n", RxCount);
|
---|
576 | // } */
|
---|
577 | //
|
---|
578 | //// ethhdr = p->payload;
|
---|
579 | //// if (eth_addr_cmp(ðhdr->dest, ðbroadcast)){ // øèðîêîâåùàëêà
|
---|
580 | //// iphdr = (struct ip_hdr *)p->payload;
|
---|
581 | //// for(uint8_t i = 0; i < MAX_IP; i++){
|
---|
582 | //// ip4addr_aton(net_table[i].ipv4_addr, &addrIP);
|
---|
583 | //// ret = addrIP.addr;
|
---|
584 | //// if(ip_addr_cmp(&addrIP, iphdr->dest)){
|
---|
585 | //// p->if_idx = i;
|
---|
586 | //// if (netif->input( p, netif_list) != ERR_OK )
|
---|
587 | //// {
|
---|
588 | //// pbuf_free(p);
|
---|
589 | //// p = NULL;
|
---|
590 | //// }
|
---|
591 | //// break;
|
---|
592 | //// }
|
---|
593 | //// }
|
---|
594 | //// }
|
---|
595 | //// for(uint8_t i = 0; i < MAX_IP; i++){
|
---|
596 | //// if(eth_addr_cmp(ðhdr->dest, &net_table[i].haddr)){
|
---|
597 | ////
|
---|
598 | ////
|
---|
599 | //// p->if_idx = i;
|
---|
600 | //// if (netif->input( p, netif_list) != ERR_OK )
|
---|
601 | //// {
|
---|
602 | //// pbuf_free(p);
|
---|
603 | //// p = NULL;
|
---|
604 | //// }
|
---|
605 | //// break;
|
---|
606 | //// /*pbuf_header(p, -iphdr_hlen);
|
---|
607 | //// udphdr = (struct udp_hdr *)p->payload;
|
---|
608 | //// pbuf_header(p, iphdr_hlen);*/
|
---|
609 | ////
|
---|
610 | ////
|
---|
611 | //// //ip_addr_cmp(, addrIP)
|
---|
612 | //// }
|
---|
613 | //// }
|
---|
614 | //
|
---|
615 | //// if (netif->input( p, netif) != ERR_OK )
|
---|
616 | //// {
|
---|
617 | //// pbuf_free(p);
|
---|
618 | //// p = NULL;
|
---|
619 | //// }
|
---|
620 | // }
|
---|
621 | // // UNLOCK_TCPIP_CORE();
|
---|
622 | // } while(p!=NULL);
|
---|
623 | // }
|
---|
624 | }
|
---|
625 |
|
---|
626 | #include "log_and_debug.h"
|
---|
627 | #include "stm32f4xx_hal.h"
|
---|
628 | void ethernetif_input(void const * argument)
|
---|
629 | {
|
---|
630 | struct pbuf *p;
|
---|
631 | struct netif *netif;
|
---|
632 | struct ip_hdr *iphdr;
|
---|
633 | struct eth_hdr *ethhdr;
|
---|
634 | struct etharp_hdr *arphdr;
|
---|
635 | ip4_addr_t dipaddr;
|
---|
636 |
|
---|
637 | for( ;; )
|
---|
638 | {
|
---|
639 | if(osSemaphoreWait(s_xSemaphore, TIME_WAITING_FOR_INPUT) == osOK)
|
---|
640 | {
|
---|
641 | do
|
---|
642 | {
|
---|
643 | p = low_level_input(NULL);
|
---|
644 | if (p != NULL)
|
---|
645 | {
|
---|
646 | netif = NULL;
|
---|
647 | ethhdr = p->payload;
|
---|
648 | pbuf_header(p, -14);
|
---|
649 | switch (htons(ethhdr->type)) {
|
---|
650 | case ETHTYPE_IP:
|
---|
651 | iphdr = (struct ip_hdr *)p->payload;
|
---|
652 | NETIF_FOREACH(netif){
|
---|
653 | if(iphdr->dest.addr == netif->ip_addr.addr) {
|
---|
654 | if((netif->flags & NETIF_FLAG_UP) == NETIF_FLAG_UP) break;
|
---|
655 | }
|
---|
656 | }
|
---|
657 | break;
|
---|
658 | case ETHTYPE_ARP:
|
---|
659 | arphdr = (struct etharp_hdr *)p->payload;
|
---|
660 | IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&dipaddr, &arphdr->dipaddr);
|
---|
661 | NETIF_FOREACH(netif){
|
---|
662 | if(dipaddr.addr == netif->ip_addr.addr) {
|
---|
663 | if((netif->flags & NETIF_FLAG_UP) == NETIF_FLAG_UP) break;
|
---|
664 | }
|
---|
665 | }
|
---|
666 | break;
|
---|
667 | }
|
---|
668 | pbuf_header(p, 14);
|
---|
669 |
|
---|
670 | if(netif != NULL){
|
---|
671 | if (netif->input(p, netif) != ERR_OK )
|
---|
672 | {
|
---|
673 | pbuf_free(p);
|
---|
674 | p = NULL;
|
---|
675 | }
|
---|
676 | }
|
---|
677 | else {
|
---|
678 | pbuf_free(p);
|
---|
679 | p = NULL;
|
---|
680 | }
|
---|
681 | }
|
---|
682 | } while(p!=NULL);
|
---|
683 | }
|
---|
684 | }
|
---|
685 | }
|
---|
686 |
|
---|
687 | #if !LWIP_ARP
|
---|
688 | /**
|
---|
689 | * This function has to be completed by user in case of ARP OFF.
|
---|
690 | *
|
---|
691 | * @param netif the lwip network interface structure for this ethernetif
|
---|
692 | * @return ERR_OK if ...
|
---|
693 | */
|
---|
694 | static err_t low_level_output_arp_off(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr)
|
---|
695 | {
|
---|
696 | err_t errval;
|
---|
697 | errval = ERR_OK;
|
---|
698 |
|
---|
699 | /* USER CODE BEGIN 5 */
|
---|
700 |
|
---|
701 | /* USER CODE END 5 */
|
---|
702 |
|
---|
703 | return errval;
|
---|
704 |
|
---|
705 | }
|
---|
706 | #endif /* LWIP_ARP */
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * Should be called at the beginning of the program to set up the
|
---|
710 | * network interface. It calls the function low_level_init() to do the
|
---|
711 | * actual setup of the hardware.
|
---|
712 | *
|
---|
713 | * This function should be passed as a parameter to netif_add().
|
---|
714 | *
|
---|
715 | * @param netif the lwip network interface structure for this ethernetif
|
---|
716 | * @return ERR_OK if the loopif is initialized
|
---|
717 | * ERR_MEM if private data couldn't be allocated
|
---|
718 | * any other err_t on error
|
---|
719 | */
|
---|
720 | err_t ethernetif_init(struct netif *netif)
|
---|
721 | {
|
---|
722 | LWIP_ASSERT("netif != NULL", (netif != NULL));
|
---|
723 |
|
---|
724 | #if LWIP_NETIF_HOSTNAME
|
---|
725 | /* Initialize interface hostname */
|
---|
726 | netif->hostname = "lwip";
|
---|
727 | #endif /* LWIP_NETIF_HOSTNAME */
|
---|
728 |
|
---|
729 | netif->name[0] = IFNAME0;
|
---|
730 | netif->name[1] = IFNAME1;
|
---|
731 | /* We directly use etharp_output() here to save a function call.
|
---|
732 | * You can instead declare your own function an call etharp_output()
|
---|
733 | * from it if you have to do some checks before sending (e.g. if link
|
---|
734 | * is available...) */
|
---|
735 |
|
---|
736 | #if LWIP_IPV4
|
---|
737 | #if LWIP_ARP || LWIP_ETHERNET
|
---|
738 | #if LWIP_ARP
|
---|
739 | netif->output = etharp_output;
|
---|
740 | #else
|
---|
741 | /* The user should write its own code in low_level_output_arp_off function */
|
---|
742 | netif->output = low_level_output_arp_off;
|
---|
743 | #endif /* LWIP_ARP */
|
---|
744 | #endif /* LWIP_ARP || LWIP_ETHERNET */
|
---|
745 | #endif /* LWIP_IPV4 */
|
---|
746 |
|
---|
747 | #if LWIP_IPV6
|
---|
748 | netif->output_ip6 = ethip6_output;
|
---|
749 | #endif /* LWIP_IPV6 */
|
---|
750 |
|
---|
751 | netif->linkoutput = low_level_output;
|
---|
752 |
|
---|
753 | /* initialize the hardware */
|
---|
754 | // low_level_init(netif);
|
---|
755 |
|
---|
756 | return ERR_OK;
|
---|
757 | }
|
---|
758 |
|
---|
759 | /* USER CODE BEGIN 6 */
|
---|
760 |
|
---|
761 | /**
|
---|
762 | * @brief Returns the current time in milliseconds
|
---|
763 | * when LWIP_TIMERS == 1 and NO_SYS == 1
|
---|
764 | * @param None
|
---|
765 | * @retval Time
|
---|
766 | */
|
---|
767 | u32_t sys_jiffies(void)
|
---|
768 | {
|
---|
769 | return HAL_GetTick();
|
---|
770 | }
|
---|
771 |
|
---|
772 | /**
|
---|
773 | * @brief Returns the current time in milliseconds
|
---|
774 | * when LWIP_TIMERS == 1 and NO_SYS == 1
|
---|
775 | * @param None
|
---|
776 | * @retval Time
|
---|
777 | */
|
---|
778 | u32_t sys_now(void)
|
---|
779 | {
|
---|
780 | return HAL_GetTick();
|
---|
781 | }
|
---|
782 |
|
---|
783 | /* USER CODE END 6 */
|
---|
784 |
|
---|
785 | /* USER CODE BEGIN 7 */
|
---|
786 |
|
---|
787 | /* USER CODE END 7 */
|
---|
788 |
|
---|
789 | #if LWIP_NETIF_LINK_CALLBACK
|
---|
790 | /**
|
---|
791 | * @brief Link callback function, this function is called on change of link status
|
---|
792 | * to update low level driver configuration.
|
---|
793 | * @param netif: The network interface
|
---|
794 | * @retval None
|
---|
795 | */
|
---|
796 | void ethernetif_update_config(struct netif *netif)
|
---|
797 | {
|
---|
798 | __IO uint32_t tickstart = 0;
|
---|
799 | uint32_t regvalue = 0;
|
---|
800 |
|
---|
801 | if(netif_is_link_up(netif))
|
---|
802 | {
|
---|
803 | /* Restart the auto-negotiation */
|
---|
804 | if(heth.Init.AutoNegotiation != ETH_AUTONEGOTIATION_DISABLE)
|
---|
805 | {
|
---|
806 | /* Enable Auto-Negotiation */
|
---|
807 | HAL_ETH_WritePHYRegister(&heth, PHY_BCR, PHY_AUTONEGOTIATION);
|
---|
808 |
|
---|
809 | /* Get tick */
|
---|
810 | tickstart = HAL_GetTick();
|
---|
811 |
|
---|
812 | /* Wait until the auto-negotiation will be completed */
|
---|
813 | do
|
---|
814 | {
|
---|
815 | HAL_ETH_ReadPHYRegister(&heth, PHY_BSR, ®value);
|
---|
816 |
|
---|
817 | /* Check for the Timeout ( 1s ) */
|
---|
818 | if((HAL_GetTick() - tickstart ) > 1000)
|
---|
819 | {
|
---|
820 | /* In case of timeout */
|
---|
821 | goto error;
|
---|
822 | }
|
---|
823 | } while (((regvalue & PHY_AUTONEGO_COMPLETE) != PHY_AUTONEGO_COMPLETE));
|
---|
824 |
|
---|
825 | /* Read the result of the auto-negotiation */
|
---|
826 | HAL_ETH_ReadPHYRegister(&heth, PHY_SR, ®value);
|
---|
827 |
|
---|
828 | /* Configure the MAC with the Duplex Mode fixed by the auto-negotiation process */
|
---|
829 | if((regvalue & PHY_DUPLEX_STATUS) != (uint32_t)RESET)
|
---|
830 | {
|
---|
831 | /* Set Ethernet duplex mode to Full-duplex following the auto-negotiation */
|
---|
832 | heth.Init.DuplexMode = ETH_MODE_FULLDUPLEX;
|
---|
833 | }
|
---|
834 | else
|
---|
835 | {
|
---|
836 | /* Set Ethernet duplex mode to Half-duplex following the auto-negotiation */
|
---|
837 | heth.Init.DuplexMode = ETH_MODE_HALFDUPLEX;
|
---|
838 | }
|
---|
839 | /* Configure the MAC with the speed fixed by the auto-negotiation process */
|
---|
840 | if(regvalue & PHY_SPEED_STATUS)
|
---|
841 | {
|
---|
842 | /* Set Ethernet speed to 10M following the auto-negotiation */
|
---|
843 | heth.Init.Speed = ETH_SPEED_10M;
|
---|
844 | }
|
---|
845 | else
|
---|
846 | {
|
---|
847 | /* Set Ethernet speed to 100M following the auto-negotiation */
|
---|
848 | heth.Init.Speed = ETH_SPEED_100M;
|
---|
849 | }
|
---|
850 | }
|
---|
851 | else /* AutoNegotiation Disable */
|
---|
852 | {
|
---|
853 | error :
|
---|
854 | /* Check parameters */
|
---|
855 | assert_param(IS_ETH_SPEED(heth.Init.Speed));
|
---|
856 | assert_param(IS_ETH_DUPLEX_MODE(heth.Init.DuplexMode));
|
---|
857 |
|
---|
858 | /* Set MAC Speed and Duplex Mode to PHY */
|
---|
859 | HAL_ETH_WritePHYRegister(&heth, PHY_BCR, ((uint16_t)(heth.Init.DuplexMode >> 3) |
|
---|
860 | (uint16_t)(heth.Init.Speed >> 1)));
|
---|
861 | }
|
---|
862 |
|
---|
863 | /* ETHERNET MAC Re-Configuration */
|
---|
864 | HAL_ETH_ConfigMAC(&heth, (ETH_MACInitTypeDef *) NULL);
|
---|
865 |
|
---|
866 | /* Restart MAC interface */
|
---|
867 | HAL_ETH_Start(&heth);
|
---|
868 | }
|
---|
869 | else
|
---|
870 | {
|
---|
871 | /* Stop MAC interface */
|
---|
872 | HAL_ETH_Stop(&heth);
|
---|
873 | }
|
---|
874 |
|
---|
875 | ethernetif_notify_conn_changed(netif);
|
---|
876 | }
|
---|
877 |
|
---|
878 | /* USER CODE BEGIN 8 */
|
---|
879 | /**
|
---|
880 | * @brief This function notify user about link status changement.
|
---|
881 | * @param netif: the network interface
|
---|
882 | * @retval None
|
---|
883 | */
|
---|
884 | __weak void ethernetif_notify_conn_changed(struct netif *netif)
|
---|
885 | {
|
---|
886 | /* NOTE : This is function could be implemented in user file
|
---|
887 | when the callback is needed,
|
---|
888 | */
|
---|
889 |
|
---|
890 | }
|
---|
891 | /* USER CODE END 8 */
|
---|
892 | #endif /* LWIP_NETIF_LINK_CALLBACK */
|
---|
893 |
|
---|
894 | /* USER CODE BEGIN 9 */
|
---|
895 |
|
---|
896 | /* USER CODE END 9 */
|
---|
897 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
---|
898 |
|
---|