fixed LWIP bugs on stm32f407-st-discovery board

This commit is contained in:
wlyu 2022-06-30 06:14:19 +08:00
parent ad84a842b0
commit 8483f08271
9 changed files with 167 additions and 85 deletions

View File

@ -50,7 +50,7 @@
#include "lwip/sys.h"
#include "netif/etharp.h"
#include "err.h"
#include "ethernetif.h"
#include "enet_ethernetif.h"
#include "main.h"
#include <string.h>
@ -79,17 +79,17 @@
#define MAC_ADDR5 0
static struct netif *s_pxNetIf = NULL;
int32 s_xSemaphore = -1;
int32 s_xSemaphore = -1;
/* Ethernet Rx & Tx DMA Descriptors */
extern ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB], DMATxDscrTab[ETH_TXBUFNB];
/* Ethernet Receive buffers */
extern uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE];
extern uint8_t Rx_Buff[ETH_RXBUFNB][ETH_RX_BUF_SIZE];
/* Ethernet Transmit buffers */
extern uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE];
extern uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE];
/* Global pointers to track current transmit and receive descriptors */
extern ETH_DMADESCTypeDef *DMATxDescToSet;
@ -122,7 +122,7 @@ void LwIP_Periodic_Handle(__IO uint32_t localtime)
tcp_tmr();
}
#endif
/* ARP periodic process every 5s */
if ((localtime - ARPTimer) >= ARP_TMR_INTERVAL)
{
@ -140,11 +140,10 @@ void LwIP_Pkt_Handle(struct netif *netif)
void Time_Update_LwIP(void)
{
LocalTime += MS_PER_SYSTICK_F407;
LocalTime += MS_PER_SYSTICK;
}
/**
* In this function, the hardware should be initialized.
* Called from ethernetif_init().
@ -155,10 +154,10 @@ void Time_Update_LwIP(void)
static void low_level_init(struct netif *netif)
{
uint32_t i;
/* set netif MAC hardware address length */
netif->hwaddr_len = ETHARP_HWADDR_LEN;
/* set netif MAC hardware address */
netif->hwaddr[0] = MAC_ADDR0;
netif->hwaddr[1] = MAC_ADDR1;
@ -166,34 +165,34 @@ static void low_level_init(struct netif *netif)
netif->hwaddr[3] = MAC_ADDR3;
netif->hwaddr[4] = MAC_ADDR4;
netif->hwaddr[5] = MAC_ADDR5;
/* set netif maximum transfer unit */
netif->mtu = 1500;
/* Accept broadcast address and ARP traffic */
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
s_pxNetIf =netif;
/* create binary semaphore used for informing ethernetif of frame reception */
if (s_xSemaphore < 0)
{
s_xSemaphore = KSemaphoreCreate(0);
}
/* initialize MAC address in ethernet MAC */
ETH_MACAddressConfig(ETH_MAC_Address0, netif->hwaddr);
/* initialize MAC address in ethernet MAC */
ETH_MACAddressConfig(ETH_MAC_Address0, netif->hwaddr);
/* Initialize Tx Descriptors list: Chain Mode */
ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
/* Initialize Rx Descriptors list: Chain Mode */
ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
/* Enable MAC and DMA transmission and reception */
ETH_Start();
ETH_Start();
/* Enable Ethernet Rx interrrupt */
{
{
for(i=0; i<ETH_RXBUFNB; i++)
{
ETH_DMARxDescReceiveITConfig(&DMARxDscrTab[i], ENABLE);
@ -207,10 +206,10 @@ static void low_level_init(struct netif *netif)
{
ETH_DMATxDescChecksumInsertionConfig(&DMATxDscrTab[i], ETH_DMATxDesc_ChecksumTCPUDPICMPFull);
}
}
}
#endif
/* create the task that handles the ETH_MAC */
/* create the task that handles the ETH_MAC */
uint32 thr_id = KTaskCreate((signed char*) "Eth_if",
ethernetif_input,
NULL,
@ -249,17 +248,17 @@ static err_t low_level_output(struct netif *netif, struct pbuf *p)
struct pbuf *q;
uint32_t l = 0;
u8 *buffer ;
if(sem < 0)
{
sem = KSemaphoreCreate(1);
}
KSemaphoreObtain(sem, WAITING_FOREVER);
buffer = (u8 *)(DMATxDescToSet->Buffer1Addr);
for(q = p; q != NULL; q = q->next)
for(q = p; q != NULL; q = q->next)
{
memcpy((u8_t*)&buffer[l], q->payload, q->len);
l = l + q->len;
@ -272,8 +271,8 @@ static err_t low_level_output(struct netif *netif, struct pbuf *p)
}
/**
* Should allocate a pbuf and transfer the bytes of the incoming
@ -291,16 +290,16 @@ static struct pbuf * low_level_input(struct netif *netif)
FrameTypeDef frame;
u8 *buffer;
__IO ETH_DMADESCTypeDef *DMARxNextDesc;
p = NULL;
/* Get received frame */
frame = ETH_Get_Received_Frame_interrupt();
/* check that frame has no error */
if ((frame.descriptor->Status & ETH_DMARxDesc_ES) == (uint32_t)RESET)
{
/* Obtain the size of the packet and put it into the "len" variable. */
len = frame.length;
buffer = (u8 *)frame.buffer;
@ -310,15 +309,15 @@ static struct pbuf * low_level_input(struct netif *netif)
/* Copy received frame from ethernet driver buffer to stack buffer */
if (p != NULL)
{
{
for (q = p; q != NULL; q = q->next)
{
memcpy((u8_t*)q->payload, (u8_t*)&buffer[l], q->len);
l = l + q->len;
}
}
}
}
/* Release descriptors to DMA */
/* Check if received frame with multiple DMA buffer segments */
if (DMA_RX_FRAME_infos->Seg_Count > 1)
@ -329,24 +328,24 @@ static struct pbuf * low_level_input(struct netif *netif)
{
DMARxNextDesc = frame.descriptor;
}
/* Set Own bit in Rx descriptors: gives the buffers back to DMA */
for (i=0; i<DMA_RX_FRAME_infos->Seg_Count; i++)
{
{
DMARxNextDesc->Status = ETH_DMARxDesc_OWN;
DMARxNextDesc = (ETH_DMADESCTypeDef *)(DMARxNextDesc->Buffer2NextDescAddr);
}
/* Clear Segment_Count */
DMA_RX_FRAME_infos->Seg_Count =0;
/* When Rx Buffer unavailable flag is set: clear it and resume reception */
if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET)
if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET)
{
/* Clear RBUS ETHERNET DMA flag */
ETH->DMASR = ETH_DMASR_RBUS;
/* Resume DMA reception */
ETH->DMARPDR = 0;
}
@ -355,8 +354,8 @@ static struct pbuf * low_level_input(struct netif *netif)
/**
* This function is the ethernetif_input task, it is processed when a packet
* is ready to be read from the interface. It uses the function low_level_input()
* This function is the ethernetif_input task, it is processed when a packet
* is ready to be read from the interface. It uses the function low_level_input()
* that should handle the actual reception of bytes from the network
* interface. Then the type of the received packet is determined and
* the appropriate input function is called.
@ -366,7 +365,7 @@ static struct pbuf * low_level_input(struct netif *netif)
void ethernetif_input( void * pvParameters )
{
struct pbuf *p;
for( ;; )
{
if (KSemaphoreObtain( s_xSemaphore, WAITING_FOREVER)==EOK)
@ -381,24 +380,24 @@ void ethernetif_input( void * pvParameters )
}
}
}
}
}
void LoopGetMacPkg(void * pvParameters)
{
// Loop Get MAX Pkg
struct netif *netif = (struct netif *)pvParameters;
while (1)
{
{
/* check if any packet received */
if (ETH_CheckFrameReceived())
{
{
KPrintf("ETH_CheckFrameReceived invoke !\n");
/* process received ethernet packet */
LwIP_Pkt_Handle(netif);
}
/* handle periodic timers for LwIP */
LwIP_Periodic_Handle(LocalTime);
}
}
}
/**
@ -413,7 +412,7 @@ void LoopGetMacPkg(void * pvParameters)
* ERR_MEM if private data couldn't be allocated
* any other err_t on error
*/
err_t ethernetif_init(struct netif *netif)
err_t ethernetif0_init(struct netif *netif)
{
LWIP_ASSERT("netif != NULL", (netif != NULL));
@ -430,7 +429,7 @@ err_t ethernetif_init(struct netif *netif)
/* initialize the hardware */
low_level_init(netif);
etharp_init();
sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);

View File

@ -0,0 +1,85 @@
/**
******************************************************************************
* File Name : ethernetif.h
* Description : This file provides initialization code for LWIP
* middleWare.
******************************************************************************
* This notice applies to any and all portions of this file
* that are not between comment pairs USER CODE BEGIN and
* USER CODE END. Other portions of this file, whether
* inserted by the user or by software development tools
* are owned by their respective copyright owners.
*
* Copyright (c) 2018 STMicroelectronics International N.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted, provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of other
* contributors to this software may be used to endorse or promote products
* derived from this software without specific written permission.
* 4. This software, including modifications and/or derivative works of this
* software, must execute solely and exclusively on microcontroller or
* microprocessor devices manufactured by or for STMicroelectronics.
* 5. Redistribution and use of this software other than as permitted under
* this license is void and will automatically terminate your rights under
* this license.
*
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#ifndef __ETHERNETIF_H__
#define __ETHERNETIF_H__
#include "lwip/err.h"
#include "lwip/netif.h"
/* Within 'USER CODE' section, code will be kept by default at each generation */
/* USER CODE BEGIN 0 */
#define NETIF_MTU ( 1500 )
#define NETIF_IN_TASK_STACK_SIZE ( 1024 )
#define NETIF_IN_TASK_PRIORITY ( 3 )
#define NETIF_OUT_TASK_STACK_SIZE ( 1024 )
#define NETIF_OUT_TASK_PRIORITY ( 3 )
/* USER CODE END 0 */
/* Exported functions ------------------------------------------------------- */
err_t ethernetif0_init(struct netif *netif);
void ethernetif_input( void *argument );
void ethernetif_output( void *argument );
void ethernetif_update_config(struct netif *netif);
void ethernetif_notify_conn_changed(struct netif *netif);
u32_t sys_jiffies(void);
u32_t sys_now(void);
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
#endif
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View File

@ -69,6 +69,8 @@
#include "ethernet.h"
#include "enet_ethernetif.h"
#include <transform.h>
#include "connect_ethernet.h"
char lwip_ipaddr[] = {192, 168, 250, 253};
char lwip_netmask[] = {255, 255, 255, 0};
@ -469,14 +471,16 @@ void lwip_config_net(char *ip, char *mask, char *gw)
mem_range_t non_dma_memory[] = NON_DMA_MEMORY_ARRAY;
#endif /* FSL_FEATURE_SOC_LPC_ENET_COUNT */
ip4_addr_t net_ipaddr, net_netmask, net_gw;
ethernetif_config_t cfg = {
.phyAddress = BOARD_ENET0_PHY_ADDRESS,
.clockName = kCLOCK_CoreSysClk,
.macAddress = configMAC_ADDR,
#if defined(FSL_FEATURE_SOC_LPC_ENET_COUNT) && (FSL_FEATURE_SOC_LPC_ENET_COUNT > 0)
.non_dma_memory = non_dma_memory,
#endif /* FSL_FEATURE_SOC_LPC_ENET_COUNT */
};
#if defined(ethernetif_config_t)
ethernetif_config_t cfg = {
.phyAddress = BOARD_ENET0_PHY_ADDRESS,
.clockName = kCLOCK_CoreSysClk,
.macAddress = configMAC_ADDR,
};
#else
void *cfg = NULL;
#endif
ETH_BSP_Config();
@ -508,6 +512,7 @@ void lwip_config_net(char *ip, char *mask, char *gw)
netif_add(&gnetif, &net_ipaddr, &net_netmask, &net_gw, &cfg, ethernetif0_init,
ethernet_input);
netif_set_default(&gnetif);
netif_set_up(&gnetif);
@ -533,15 +538,16 @@ void lwip_config_tcp(char *ip, char *mask, char *gw)
mem_range_t non_dma_memory[] = NON_DMA_MEMORY_ARRAY;
#endif /* FSL_FEATURE_SOC_LPC_ENET_COUNT */
ip4_addr_t net_ipaddr, net_netmask, net_gw;
#if defined(ethernetif_config_t)
ethernetif_config_t cfg = {
.phyAddress = BOARD_ENET0_PHY_ADDRESS,
.clockName = kCLOCK_CoreSysClk,
.macAddress = configMAC_ADDR,
#if defined(FSL_FEATURE_SOC_LPC_ENET_COUNT) && (FSL_FEATURE_SOC_LPC_ENET_COUNT > 0)
.non_dma_memory = non_dma_memory,
#endif /* FSL_FEATURE_SOC_LPC_ENET_COUNT */
};
#else
void *cfg = NULL;
#endif
ETH_BSP_Config();
if(chk_lwip_bit(LWIP_INIT_FLAG))

View File

@ -45,6 +45,12 @@
#include <lwip/arch.h>
#include <lwip/netif.h>
typedef int32 sys_sem_t;
typedef int32 sys_mutex_t;
typedef int32 sys_mbox_t;
typedef int32 sys_thread_t;
typedef x_base sys_prot_t;
#include "tcpip.h"
#include <xs_base.h>
@ -65,12 +71,6 @@
#define SYS_SEM_NULL -1
#define SYS_MRTEX_NULL SYS_SEM_NULL
typedef int32 sys_sem_t;
typedef int32 sys_mutex_t;
typedef int32 sys_mbox_t;
typedef int32 sys_thread_t;
typedef x_base sys_prot_t;
#define MS_PER_SYSTICK (1000 / TICK_PER_SECOND)
//debug rtos with IRQ

View File

@ -19,6 +19,7 @@
*/
#include "board.h"
#include "shell.h"
#include "sys_arch.h"
/******************************************************************************/

View File

@ -23,20 +23,12 @@
#if LWIP_IPV4 && LWIP_DHCP
#include "lwip/timeouts.h"
#include "lwip/ip_addr.h"
#include "lwip/init.h"
#include "lwip/dhcp.h"
#include "lwip/prot/dhcp.h"
#include "netif/ethernet.h"
#include "enet_ethernetif.h"
#include "board.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "fsl_gpio.h"
#include "fsl_iomuxc.h"
#include "shell.h"
#include "sys_arch.h"
#define LWIP_DHCP_TIME 10000 // 10s
@ -99,7 +91,6 @@ int LwipPrintDHCP(struct netif *netif)
break;
default:
lw_notice("%u", dhcp_last_state);
assert(0);
break;
}
lw_notice("\r\n");

View File

@ -24,15 +24,13 @@
#include "ping.h"
#include "lwip/timeouts.h"
#include "lwip/init.h"
#include "netif/ethernet.h"
//#include "lwip/timeouts.h"
//#include "lwip/init.h"
//#include "netif/ethernet.h"
#include "board.h"
#include "pin_mux.h"
#include "clock_config.h"
#include <sys_arch.h>
#include "shell.h"
#include "sys_arch.h"
#include "connect_ethernet.h"
char test_ip_addr[] = {192, 168, 250, 253};

View File

@ -19,6 +19,7 @@
*/
#include "board.h"
#include "shell.h"
#include "lwip_demo.h"
#include "sys_arch.h"
#include "lwip/sockets.h"

View File

@ -21,6 +21,7 @@
#include "sys_arch.h"
#include "lwip/udp.h"
#include "lwip/sockets.h"
#include "shell.h"
#define PBUF_SIZE 27