Flat head RVB2601 board -- NETMGR and WIFI

Posted by dreamdelerium on Thu, 06 Jan 2022 03:50:15 +0100

Author: bigbat

1, Introduction

W800 wifi chip is integrated in ch2601 development board. This chip is a complete mcu chip. Theoretically, there is no need for the main chip of ch2601 to worry about network communication. Just send instructions and data to w800. Ch2601 interacts with w800 chip through SPI bus and communicates with AT command. In order to integrate a variety of network devices, the system of ch2601 chip adopts a layered way to communicate with system applications.

As can be seen from the figure, YoC platform provides a complete network framework to support different network connection chip types and diversified application scenarios. The framework is mainly divided into four layers: network application layer, network interface layer, network chip driver layer and bottom peripheral driver layer.

2, Network management layer

The network interface layer is mainly divided into two channels. One is the data channel, which is responsible for sending and receiving network data. It can be implemented by calling the socket adaptation layer SAL or LWIP interface, and provides a set of standard BSD socket API s for the upper layer. The second is the management channel, that is, the network manager netmgr, which is responsible for the networking, connection, management, status query and other functions of all networks.

At present, YoC platform supports many different network chip types, such as esp8266, rtl8723ds, GPRS, etc. The network chip driver layer provides the interface docking of the network layer, link layer and driver of the specific chip to meet the requirements of the network interface layer.

The network chip may be externally connected through SDIO, AT and other interfaces. The bottom peripheral driver layer is used to realize the data interaction requirements between the main control chip and the external network chip.

The network manager is the core of wifi connection and management (netmgr). The network manager is responsible for the networking management functions of all networks, as follows:

  • Link layer startup / initialization / configuration / reset, etc
  • Acquisition of link layer equipment information
  • Link layer event processing and forwarding (linkup,linkdown)
  • Network IP configuration (DHCP or static IP)

3, W800 drive

In order to use wifi for networking, you need to call the API of netmgr. Let me test this process:

1. First, you need to initialize the w800 network connection

2. Manage connections and register events.

3. Network communication

How to initialize the W800 chip, I refer to the routine code of the web player.

#include <drv/pin.h>
#include <devices/w800.h>

netmgr_hdl_t app_netmgr_hdl;

static void network_init()
{
    w800_wifi_param_t w800_param;
    /* init wifi driver and network */
    w800_param.reset_pin      = PA21;
    w800_param.baud           = 1*1000000;
    w800_param.cs_pin         = PA15;
    w800_param.wakeup_pin     = PA25;
    w800_param.int_pin        = PA22;
    w800_param.channel_id     = 0;
    w800_param.buffer_size    = 4*1024;

    wifi_w800_register(NULL, &w800_param);
    app_netmgr_hdl = netmgr_dev_wifi_init();

    if (app_netmgr_hdl) {
        utask_t *task = utask_new("netmgr", 2 * 1024, QUEUE_MSG_COUNT, AOS_DEFAULT_APP_PRI);
        netmgr_service_init(task);
        netmgr_config_wifi(app_netmgr_hdl, "test", 4, "test123456", 10);
        netmgr_start(app_netmgr_hdl);
    }
}

Note the include file. Many routines in the official materials do not describe how to reference them.

netmgr_config_wifi(app_netmgr_hdl, "test", 4, "test123456", 10);

The annotated statements are wifi ssid and networking password. Just call this function to connect to the network. At this point, you can ping the network.

How to manage the connection is also a very important procedure. If the connection is disconnected or needs to be reconnected, system events need to be used for how to manage.

/* network event callback */
void user_local_event_cb(uint32_t event_id, const void *param, void *context)
{
    char url[128];
    if (event_id == EVENT_NETMGR_GOT_IP) { // Network connection succeeded
        printf("Net up");
    } else if (event_id == EVENT_NETMGR_NET_DISCON) {
        if ((int)param == NET_DISCON_REASON_DHCP_ERROR) {
            printf("Net down"); 
            netmgr_reset(netmgr_get_handle("wifi"), 30); // The network connection failed. It will be reconnected automatically after 30s
        }
    }
}


int main(void)
{
    board_yoc_init();
    event_service_init(NULL);                     // Publish subscribe service initialization
    LOGD(TAG, "%s\n", aos_get_app_version());
    oled_init();
    network_init();
    
    /* Subscribe */
    event_subscribe(EVENT_NETMGR_GOT_IP, user_local_event_cb, NULL); // Subscription network connection success event
    event_subscribe(EVENT_NETMGR_NET_DISCON, user_local_event_cb, NULL); // Subscription network connection failure event
    
    while (1) {
        LOGD(TAG, "Hello world! YoC");
        aos_msleep(1000);
    }

    return 0;
}

4, wifi settings

The above code is not complicated, mainly three points,

1. Construct an event function to handle events

2. Register event function, event_subscribe(EVENT_NETMGR_GOT_IP, user_local_event_cb, NULL); // Subscription network connection success event

3. Initialize event function, event_service_init(NULL); // Publish subscribe service initialization

This sentence is very important. Without it, the whole system will stop and the system will not run.

The whole networking process is completed. Later, I will test how to write applications using the network high-level interface.

You can see the event message of network startup, "Net up" and IP address "192.168.1.104"

This article is derived from: Pingtouge chip open community

Topics: IoT risc-v