LwIP Application Notes (1): Some preliminaries for LwIP transplantation

foreword

From March to June 2021, I used some spare time after work to read the book "Things about Embedded Networks", which introduces the LwIP protocol. Now I have basically read this book, so I started After reading this column, I plan to use a few articles to summarize the gains and deepen my understanding at the same time.
Overall, I plan to spend a month updating five articles:

  1. The first article, which is this article, intends to talk about some basic knowledge of the TCP/IP protocol, and at the same time summarize the code structure of LwIP, talk about the initialization process of LwIP and how LwIP interacts with the underlying network card. It paves the way for the transplantation of the second article.
  2. The second article will summarize how to transplant LwIP without an operating system environment. After completing this step, we can use the RAW API in LwIP to develop TCP/IP applications with the help of callback programming.
  3. The third article will summarize how to transplant the LwIP system simulation layer in the presence of RTOS. After completing this step, we can use the NETCONN API and the Socket interface layer, which are simpler than the RAW API, in a multitasking environment. Develop TCP/IP applications.
  4. In the fourth article, I will talk about how to do preliminary UDP protocol programming based on LwIP. If time is enough, I will also summarize the use process of using NETCONN API and RAW API in UDP protocol programming. If there is not enough time, I will only summarize NETCONN API.
  5. In the fifth article, I will talk about how to perform preliminary TCP protocol programming based on LwIP. If there is enough time, I will also summarize the usage process of using NETCONN API and RAW API in UDP protocol programming. If there is not enough time, I will only summarize NETCONN API.

In the future, if the author has the opportunity to learn more about LwIP, I will continue to update other articles, but this may be based on years in the future. . .

TCP/IP protocol stack and LwIP

As a network protocol stack, TCP/IP provides a network access interface for upper application layer programs through a bunch of protocols distributed at various levels. For network-related protocols, there is an OSI model that describes what the protocol does at each level. The author once translated an article introducing the OSI model, see: [Translation] OSI model: 7 levels in network communication , but The TCP/IP protocol stack existed before this model came out, so it has its own layering method, but fortunately, the two can still find a corresponding relationship. The author made a picture by himself. This picture compares the layering method of the TCP/IP protocol stack and the layering method of the OSI model, and lists what each layer of TCP/IP is generally doing.
insert image description hereLwIP implements the main protocols of the network interconnection layer and the transport layer, and also provides some main application layer protocols. The underlying network interface layer, the most common one is Ethernet, which is divided into two parts, MAC and PHY, which are generally implemented by the network card. The main work in our transplantation process is to package the interface provided by the network card driver into the interface required by LwIP. look, and then provided to the network interface layer of LwIP to use.
The following figure illustrates the main folder structure of LwIP and the main protocols implemented in each layer. This figure is based on LwIP2.1.2, which is also the latest LwIP version. It is said that this version was released a few years ago.
insert image description here

The initialization process of LwIP and what needs to be done for transplantation

There are some differences in the LwIP initialization process when there is an operating system and when there is no operating system, but the basic process is the same. The following figure generally illustrates the entire initialization process.

insert image description here

The purpose of sorting out the initialization process of LwIP is to understand what we need to do when transplanting. During the entire transplanting process, there are three places that need to be implemented by writing code yourself:

  1. Declare and define a variable of netif structure type to describe the network interface;
  2. Implement an initialization function of the underlying network card, and pass it in to initialize the underlying network card when executing the netif_add function;
  3. Polling the network card or responding to the interruption of the network card, and passing the received data packet to LwIP for processing;

Considering the complexity of steps 2 and 3, the developers of LwIP provided some help. There is also a compressed package named contrib-2.1.0 in the download directory of the LwIP official website, which organizes various sample codes contributed by developers. There is ethernetif.c under contrib-2.1.0/examples/ethernetif , this file provides the underlying network card initialization function err_t ethernetif_init(struct netif *netif) and the function void ethernetif_input(struct netif *netif) for receiving and processing network packets. What we need to do is to follow the prompts in the ethernetif.c file and the actual situation Improve the functions, and then pass ethernetif_init into the netif_add function to initialize the network card, and when the network card receives the data packet, call ethernetif_input to read the data from the network card and submit it to LwIP for processing. The call relationship of each function in the ethernetif.c file is roughly shown in the figure below.
insert image description here

After understanding these, the next step is to start the transplantation of LwIP. I will try my best to publish the next article as soon as possible. ^_^

Guess you like

Origin blog.csdn.net/lczdk/article/details/118197251