[No OS] Ping based on STM32 porting LWIP 1.4.1

1 Introduction

To transplant LWIP 1.4.1 on the STM32 platform to realize the Ping function, some preparatory work needs to be done first, for example:

  1. "Data preparation for porting LWIP based on STM32"
  2. "Introduction to Porting LWIP Hardware Based on STM32"

The target platform for transplantation is STM32F429 . It was mentioned in the previous data preparation that the STM32 official website has STM32F4x7 microcontrollers.LwIP TCP/IP protocol stackFor the demo code, we can refer to the target platform to a large extent.

The transplantation process described below is based on the hardware platform STM32F429 , and is carried out on the basis of transplanting the STM32 standard library .

2.Main function

First of all, we transplanted the main function, because it is the entrance of the entire program. We can refer to the downloaded STM32F4x7 LwIP TCP/IP protocol stack demo code:
PATH: STM32F4x7_ETH_LwIP_V1.1.1\Project\Standalone\tcp_echo_client\src\main.c
Through the reference code, we need to achieve the following tasks:

  • SysTick_Init, Initialize the working system clock of STM32;
  • TIM3_Int_Init, Initialize Timer3, and serve as the local time heartbeat required by the LwIP protocol stack.
  • ETH_BSP_Config, Configure the Ethernet GPIO and DMA, and set the PHY network card (LAN8742A) related registers through the SMI interface.
  • LwIP_Init, Initialize the LwIP protocol stack;
  • ETH_CheckFrameReceivedDetect whether there is a data packet received through the loop, and LwIP_Pkt_Handleprocess the received network data packet.
  • LwIP_Periodic_Handle, Process LwIP periodic tasks;
int main(void)
{
    
    
	...
	
	// init systick
	SysTick_Init();

	//init timer3 10ms
	TIM3_Int_Init(100-1,8400-1);
	
	/* Configure ethernet (GPIOs, clocks, MAC, DMA) */
	ETH_BSP_Config();
	
	/* Initilaize the LwIP stack */
	LwIP_Init();

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

	//return 0;
}

3. Configure Ethernet (GPIOs, MAC, DMA)

To configure Ethernet, we can refer to the downloaded STM32F4x7 LwIP TCP/IP protocol stack demo code:
PATH: STM32F4x7_ETH_LwIP_V1.1.1\Project\Standalone\tcp_echo_client\src\stm32f4x7_eth_bsp.c
Configure GPIO as Ethernet function, MAC and DMA configuration directly follow the reference code.

Note: When configuring the Ethernet, an external PYH address is required. As mentioned in the previous hardware introduction, it should be set to PHYAddress = 0.

void ETH_BSP_Config(void)
{
    
    
  /* Configure the GPIO ports for ethernet pins */
  ETH_GPIO_Config();

  /* Configure the Ethernet MAC/DMA */
  ETH_MACDMA_Config();

  /* Get Ethernet link status*/
  if(GET_PHY_LINK_STATUS())
  {
    
    
    EthStatus |= ETH_LINK_FLAG;
  }
}

4. Add LwIP to the project

Copy the LwIP in the STM32F4x7 LwIP TCP/IP protocol stack demo code to the current project:
PATH: STM32F4x7_ETH_LwIP_V1.1.1\Utilities\Third_Party\lwip-1.4.1
Add the LwIP source file that needs to be compiled to the IDE tool. The Keil tool I use here.

  • LwIP source is too big, we do not need so many things, so we only need to LwIP source
    of the srcfiles in the folder can be added to it.

  • Respectively api, core, netifadded to the Keil project

  • Copy the Ethernet driver of the STM32F4x7 platform to the current project and rename it to STM32F429:
    PATH:STM32F4x7_ETH_LwIP_V1.1.1\Libraries\STM32F4x7_ETH_Driver

  • Add the path of the LwIP header file in the project, and check the C99 mode. The completed schematic diagram is as follows:

5. Solve the problem of compilation errors

  • Error 1 :
    error: #5: cannot open source input file "lwip/opt.h": No such file or directory
    Solution :
    make sure to add the correct header file path lwip-1.4.1\src\include.
  • Error 2 :
    ..\lwip-1.4.1\src\include\lwip/opt.h(45): error: #5: cannot open source input file "lwipopts.h": No such file or directory
    Solution :
    Copy the STM32F4x7 LwIP TCP/IP protocol stack demo code lwipopts.hto the current project,
    PATH:STM32F4x7_ETH_LwIP_V1.1.1\Project\Standalone\tcp_echo_client\inc\lwipopts.h

    Remarks: lwipopts.h is used to configure the relevant parameters of LwIP. Generally speaking, LwIP has parameter configuration by default, which is stored in the opt.h file. If the user does not configure it in the lwipopts.h file, then LwIP will use it. The default parameters of opt.h. Note that it is very important to define certain parameters during migration. This is very important to the performance of our LwIP, and even when the configuration is wrong, it can directly cause the operation of LwIP to crash. Here I just implement a simple Ping function, so just use the default configuration directly.

  • Error 3 :
    ..\lwip-1.4.1\src\include\lwip/arch.h(43): error: #5: cannot open source input file "arch/cc.h": No such file or directory
    Solution :
    a lwip-1.4.1\port\STM32F4x7\arch\cc.hcopy of the current project and added to the system header files path.

    Remarks: The cc.h file contains processor-related variable types, data structures, and byte alignment related macros.
    The basic variable types used in LwIP are named by the number of digits, which are abstract variable type definitions. Developers need to define according to the characteristics of the processor and compiler used. Generally, we directly define the variables as the basic types of the C language, such as Unsigned char, int, etc., in this way can ensure that the LwIP protocol stack is platform-independent. In addition, we can also define large and small endian modes, output debugging macros, and so on.

  • Error 4 :
    Error: L6218E: Undefined symbol LwIP_Init (referred from main.o).
    Error: L6218E: Undefined symbol LwIP_Periodic_Handle (referred from main.o).
    Error: L6218E: Undefined symbol LwIP_Pkt_Handle (referred from main.o).
    Solution : The
    main function was directly copied by reference when transplanting the main function, but these functions were not really implemented. We can continue to refer to the downloaded STM32F4x7 LwIP TCP/IP protocol stack demo code:
    PATH: STM32F4x7_ETH_LwIP_V1.1.1\Project\Standalone\tcp_echo_client\src\netconf.c
    Copy it to the current project, and then recompile.
  • Error 5 :
    netconf.c(36): error: #5: cannot open source input file "ethernetif.h": No such file or directory
    Solution :
    We can lwip-1.4.1\port\STM32F4x7\Standalone\ethernetif.hcopy to the current project, but found that there are still many functions that have not been implemented. Because we added the template of lwip-1.4.1 ( lwip-1.4.1\src\netif\ethernetif.c), this file needs to be customized for each platform. Here is the reference code ( lwip-1.4.1\port\STM32F4x7\Standalone\ethernetif.c), add it to the current project and recompile.

    Note: ethernetif.c is mainly the underlying driver function related to the network card.

6. Verification test

Finally, the verification test is successful, as follows:
Insert picture description here

7. Data download address

The complete code download address for successful transplantation is as follows:
https://download.csdn.net/download/ZHONGCAI0901/12921796

Guess you like

Origin blog.csdn.net/ZHONGCAI0901/article/details/109022185