The role and difference of the three major components of esp_wifi, lwip, and esp_netif in ESP32 components

1. What is esp_wifi (wifi driver library), esp_netif (network interface) and lwip (lightweight TCP/IP network protocol stack) of esp32? What is the relationship between the three?

The esp_wifi driver library user controls the wifi hardware unit;

lwip is a layer of pure software, lightweight TCP/IP protocol stack;

esp_netif is an API for operating TCP/IP protocol stack officially provided by esp.

 

From the above ESP32 functional block diagram, it can be seen that wifi is an independent piece of hardware in the single-chip microcomputer, which realizes the purpose of sending data wirelessly. Similarly, Bluetooth, Ethernet, etc. are all independent pieces of hardware in the microcontroller. So since it is hardware, we need to perform operations such as initial configuration before using wifi to send and receive data.

The above is a diagram describing the operation relationship between esp_wifi (wifi driver library), esp_netif (network interface), and lwip (lightweight TCP/IP network protocol stack) in the official ESP32 document.

ESP-NETIF is a portable interface for application programs to access the protocol stack that is officially encapsulated by ESP32 on top of the TCP/IP protocol stack, which means that the IP protocol stack does not need to be directly accessed by user applications.

From (A), the user application calls the API interface in esp_netif to initialize the TCP/IP protocol stack, and then calls the API interface in esp_wifi to initialize the wifi hardware. The next step is the data transmission and reception process. The data sent by the user program passes through the TCP/IP protocol. After the processing of the IP protocol stack is packaged, it arrives at the wifi driver, and finally sent out through the physical hardware electromagnetic wave. In general, it is the user layer-"TCP/IP protocol stack-"wifi hardware -" peer end.

2. Interpretation of ESP-NETIF interaction. Document link: https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/api-reference/network/esp_netif.html

The ESP-NETIF library has two functions.

(1). Provides an abstraction layer on top of the TCP/IP protocol stack, allowing users to choose the IP stack to use when possible in the future;

(2). The API of this abstraction layer is thread-safe, even if the underlying TCP/IP protocol stack interface is not thread-safe.

At present, the ESP-IDF framework only implements the ESP-NETIF abstraction layer for the lightweight TCP/IP protocol stack, but the adapter (referring to the ESP-NETIF abstraction layer mentioned above) itself is not visible even if it is different. The realization is also possible.

Some API interface functions of the ESP-NETIF abstraction layer can be called by applications, such as obtaining and setting IP addresses, and configuring DHCP. Other interface functions are called by the network driver layer (here I think it is the wifi driver layer).

In many cases, applications do not need to call the API interface of the ESP-NETIF abstraction layer, because they will be called by default network events.

The ESP-NETIF component is the successor of the tcpip adapter, and the tcpip adapter has been abandoned since IDF v4.1. Please refer to the  TCP/IP Adapter Migration Guide  to replace the tcpip adapter with ESP-NETIF.

(3). ESP-NETIF interaction

A) User code, boiler plate。

    A. Initialization code

  • In general, applications that use the IO driver as the communication medium and configure the TCP/IP protocol stack are abstract when using the ESP-NETIF APIs, see the following description.
  • Initialize the IO driver.
  • Create a new ESP-NETIF instance and configure it.
  • Connect the IO driver to the ESP-NETIF instance (here, my understanding is that the wifi hardware unit establishes a connection with the ESP-NETIF network interface through the IO driver).
  • Configure event handling function (handling wifi hardware unit events and tcp/ip protocol stack events).

    B. Use the network interface interaction of ESP-NETIF API.

  • Obtain the relevant parameters under TCP/IP.
  • Receive IP events (connect/disconnect).
  • Control the life cycle of the application (setting the interface on and off).

B) Communication driver, IO driver, media driver.

  • The communication driver related to ESP-NETIF plays two important roles: to be added later.

C) ESP-NETIF, former tcpip_adapter

  • Will be added later.

D) Network stack

  • Will be added later.

3. Interpretation of ESP32 wifi related codes.

The simplest networking code

#include "esp_wifi.h"

#include "esp_event.h"

#include "nvs_flash.h"

#include "esp_log.h"

#include "string.h"

static const char* TAG = "test esp_wifi";

static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)

{

    ESP_LOGI(TAG, "----> event_base: %s event_id: %d", event_base, event_id);

    if(event_base == WIFI_EVENT) {

        switch(event_id) {

            case WIFI_EVENT_STA_START:

                ESP_LOGI(TAG, "----> %s", "WIFI_EVENT_STA_START");

                break;

            case WIFI_EVENT_STA_DISCONNECTED:

                ESP_LOGI(TAG, "----> %s", "WIFI_EVENT_STA_DISCONNECTED");

                break;

            default:

                break;

        }

    }else if(event_base == IP_EVENT) {

        ip_event_got_ip_t *event = (ip_event_got_ip_t*)event_data;

        switch(event_id) {

            case IP_EVENT_STA_GOT_IP:

                ESP_LOGI(TAG, "----> IP_EVENT_STA_GOT_IP ip:");

                ESP_LOGI(TAG, "----> sta ip:"IPSTR, IP2STR(&event->ip_info.ip));

                ESP_LOGI(TAG, "----> mask:"IPSTR, IP2STR(&event->ip_info.netmask));

                ESP_LOGI(TAG, "----> gw:"IPSTR, IP2STR(&event->ip_info.gw));

                break;

            default:

                break;

        }

    }else {

        ESP_LOGI(TAG, "----> %s", "unknown event_base");

    }

 

    return;

}

void app_main()

{

    esp_err_t ret = nvs_flash_init();

    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {

        ESP_ERROR_CHECK(nvs_flash_erase());

        ret = nvs_flash_init();

    }

    ESP_ERROR_CHECK(ret);

    esp_event_loop_create_default();

    esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL);

    esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL);

 

    //Network interface initialization

    esp_netif_init();

    esp_netif_create_default_wifi_sta();        

 

    //wifi initialization

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();

    esp_wifi_init(&cfg);

    wifi_config_t wifi_config = {

        .sta.ssid = "WX-JGGY",

        .sta.password = "juguogongyudb"

    };

    esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);

    esp_wifi_set_mode(WIFI_MODE_STA);

    esp_wifi_start(); //Start wifi

    esp_wifi_connect(); //Start connection to ap

 

    while(1) {

        vTaskDelay(1000 / portTICK_PERIOD_MS);

        printf("hello world\n");

    }

}

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_34473570/article/details/108560904