1075 views|5 replies

410

Posts

3

Resources
The OP
 

[Jihai APM32F407 Tiny Board] Porting LWIP protocol stack [Copy link]

 

In this article, we will learn how to port the LWIP protocol stack and implement pinging the network. The official SDK package contains the lwip-1.4.1 version. Next, we will port the latest version to the development board.

1. Download source code:

Download the latest version of lwip source code

Download address: http://download.savannah.nongnu.org/releases/lwip/

2. Add files to the project

2.1. Unzip the downloaded source code to the Middlewares folder under the project file created in the previous article

2.2. Copy arch file

Copy the arch file in the ETH example under the official SDK to the Middlewares\lwip-2.1.3 file

2.3. Copy LWIP and network card files

2.4. Add files to the project

2.4.1、Add API

2.4.2. Add core

2.4.3. Add netif

2.4.4. Add arch

2.4.5. Add network card driver

2.4.6. Add header files

2.4.5. Add code to systick interrupt function

2.4.6. After adding the file, compile the code and modify the corresponding file according to the error prompt. I will not list it here.

3. Main Program

main.c

#include "main.h"
#include "Board.h"
#include "oled.h"
#include "stdio.h"
#include "usart.h"

/** Global pointers on Rx descriptor used to transmit and receive descriptors */
extern ETH_DMADescConfig_T  *DMARxDescToGet;

/** current Ethernet LocalTime value */
volatile uint32_t ETHTimer = 0;
/** lwip network interface structure for ethernetif */
struct netif UserNetif;

/** TCP periodic Timer */
uint32_t TCPTimer = 0;
/** ARP periodic Timer */
uint32_t ARPTimer = 0;
/** MAC address */
uint8_t SetMACaddr[6] = {0,0,0,0,0,8};

int main(void)
{
	SysTick_Init();
	init_led();
	//init_i2c1();
	I2CInit();
	init_usart();
	ConfigEthernet();
	LwIP_Init();
	
//OLED_Init();
//	led2_off();
//	led3_on();

	while (1)
	{
		/** check if any packet received */
		if (ETH_ReadRxPacketSize(DMARxDescToGet))
		{
				/** process received ethernet packet */
				LwIP_Pkt_Handle();
		}
				/** handle periodic timers for LwIP */
		LwIP_Periodic_Handle(ETHTimer);
	}
}

void LwIP_Init(void)
{
	ip4_addr_t ipaddr;
	ip4_addr_t netmask;
	ip4_addr_t gw;

    /** Initializes the dynamic memory heap */
    mem_init();

    /** Initializes the memory pools */
    memp_init();

    IP4_ADDR(&ipaddr, 192, 168, 1, 22);
    IP4_ADDR(&netmask, 255, 255 , 255, 0);
    IP4_ADDR(&gw, 192, 168, 1, 1);

    /** Config MAC Address */
    ETH_ConfigMACAddress(ETH_MAC_ADDRESS0, SetMACaddr);

    /** Add a network interface to the list of lwIP netifs */
    netif_add(&UserNetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);

    /** Registers the default network interface */
    netif_set_default(&UserNetif);

    /** When the netif is fully configured this function must be called */
    netif_set_up(&UserNetif);

    /** Use Com printf static IP address*/
    printf("\n  Static IP address   \r\n");
    printf("IP: %d.%d.%d.%d\r\n",192,168,1,22);
    printf("NETMASK: %d.%d.%d.%d\r\n",255,255,255,0);
    printf("Gateway: %d.%d.%d.%d\r\n",192,168,1,1);
}

/*!
 * @brief   This function received ethernet packet
 *
 * @param       None
 *
 * @retval      None
 */
void LwIP_Pkt_Handle(void)
{
  /** Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
  ethernetif_input(&UserNetif);
}

/*!
 * @brief       This function LwIP periodic tasks
 *
 * @param       ETHTimer the current Ethernet Timer value
 *
 * @retval      None
 */
void LwIP_Periodic_Handle(__IO uint32_t ETHTimer)
{
#if LWIP_TCP
    /** TCP periodic process every 250 ms */
    if (ETHTimer - TCPTimer >= TCP_TMR_INTERVAL)
    {
        TCPTimer =  ETHTimer;
        tcp_tmr();
    }
#endif

    /** ARP periodic process every 5s */
    if ((ETHTimer - ARPTimer) >= ARP_TMR_INTERVAL)
    {
        ARPTimer =  ETHTimer;
        etharp_tmr();
    }
}

4. Program operation

4.1. Connect the network cable to the hardware

4.2 Serial port output

4.3. Ping the network

008.png (37.77 KB, downloads: 0)

008.png
This post is from Domestic Chip Exchange

Latest reply

Could you please share the project code? Thank you. I used the tcp demo of the sdk today and it got stuck!!! The man function comes in, and the first clock initialization fails: [attach]702731[/attach]   Details Published on 2023-6-4 11:25
 
 

6580

Posts

0

Resources
2
 

Program transplantation is a science. Follow the host to learn how to transplant the LWIP protocol stack.

This post is from Domestic Chip Exchange

Comments

Learning Together  Details Published on 2023-5-31 09:29
 
 
 

410

Posts

3

Resources
3
 
Jacktang posted on 2023-5-31 07:28 Program transplantation is a science. Follow the OP to learn how to transplant the LWIP protocol stack

Learning Together

This post is from Domestic Chip Exchange
 
 
 

6832

Posts

11

Resources
4
 
Hey guys, is it difficult to learn lwip? How to do network control?
This post is from Domestic Chip Exchange
 
 
 

266

Posts

0

Resources
5
 

Like and follow,

This post is from Domestic Chip Exchange
Personal signature

gitee/casy

 
 
 

266

Posts

0

Resources
6
 

Could you please share the project code? Thank you.

I used the tcp demo of the sdk today and it got stuck!!!

The man function comes in, and the first clock initialization fails:

This post is from Domestic Chip Exchange
Personal signature

gitee/casy

 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list