ESP32 series of documents - compile and run the "hello world"

introduction

Hello worldAll programs are for beginners to learn programming or commissioning the most basic and simple program, but for a beginner just contact ESP32 chip, the Hello worldprogram can help us better understand the entire system can also be used to determine the language compiler, program development environment and runtime environment has been installed properly.
The following article will elaborate ESP32on the Hello worldimplementation framework and procedures of the program.
Before reading this chapter, make sure you have installed the appropriate IDF and the environment in accordance with the GCC compiler tutorial .
Reference: https://esp-idf.readthedocs.io/zh_CN/latest/get-started/index.html

ESP-IDF framework Summary

ESP32Frame is used ESP-IDF(Espressif IoT Development Framework), ESP-IDFcan be seen as different functional components (Component) integration, each component (Component) generally a class of functions.

ESP-IDFThese components allow easy configuration to use, the user can freely select these components into your own project.
The fatfsassembly is to achieve the FAT file system; drivercomponent contains ESP32all the peripheral driver owned.
The parameters of these components by the make menuconfigmodified configuration.

In use ESP32when developing complex programs, you can put some sort of function as a component placed in the main directory.
ESP-IDF will automatically compile and link this component to the final program.

Hello world framework

For the Hello worldprogram, we can see the directory structure is as follows:

    - hello_world/
             - Makefile
             - main/       - hello_world_main.c
                           - component.mk
             - README.md
             - sdkconfig(编译生成或者自己添加)
             - build/(编译生成)

Hello world this item mainly includes the following elements:

1. Makefile

The project Makefilewill set the PROJECT_NAMEvariable, the compiler generates the user BINfile name from the file name for this; and will also contain ESP_IDFthe core of the Makefile: $ (IDF_PATH) /make/project.mk, noted that only contains this Makefilefile will eventually link to ESP-IDFgo up, otherwise it will be unable to use all components. General simple demo only need to include these two, but may be separately provided the project needs to be compiled and configuration makefunctions.

2. main directory

mainDirectory contains the source code of the program and a component.mkfile, which component.mkwill be integrated into the user program compiled ESP-IDFin, which can be empty or may contain only some of the control variables defined componentcompilation process. If you do not component.mkfile, ESP-IDF will not compile this directory, the runtime can not be found will be reported out of app_mainthe error.

3. sdkconfig

sdkconfigThe file will be automatically generated after compilation, which mainly contains the user's program configuration information, through the use of this document may make menuconfigbe changed, the configuration information on all of componentthe entry into force (including ESP-IDF), the average user can create a sdkconfig.defaultsfile to save the project must be of configuration items, this prevents other users tinker with the configuration results in a compile error.

4. build directory

buildCatalog generation at compile time, the compiled output file is placed in a folder, which contains each of the componentintermediate results and the final binaries.

Hello world Code Description

	#include <stdio.h>

	#include "freertos/FreeRTOS.h"
	#include "freertos/task.h"
	#include "esp_system.h"
	#include "esp_spi_flash.h" 

The above is Hello worlda program for the required header file, which contains a standard header file, two FreeRTOSheaders and two Yue Xin header files (to esp_begin with).

	void app_main()
	{

app_mainFor the entire program entry, the system will automatically find app_mainfunction, and has since the beginning of the function execution (similar to the C language mainfunctions). In this function, you can initialize some applications, such as creating other task, app_mainthe function can not be returned, otherwise the main task will be deleted lead to crash.

    /* Print chip information */
    esp_chip_info_t chip_info;
    esp_chip_info(&chip_info);
    printf("This is ESP32 chip with %d CPU cores, WiFi%s%s, ",
            chip_info.cores,
            (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
            (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");

    printf("silicon revision %d, ", chip_info.revision);

    printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
            (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

Esp interfaces are beginning to function Lexin provided above code chip main print information, esp_chip_info()function esp_system.hdefinitions, and functions to acquire information of the chip, in order to esp_chip_info_treturn, which contains CPUinformation auditing, chip version number, spi_flash_get_chip_size()function esp_spi_flash.hdefinition , is to get flashthe size.

    for (int i = 10; i >= 0; i--) {
        printf("Restarting in %d seconds...\n", i);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
    printf("Restarting now.\n");
    fflush(stdout);
    esp_restart();
}

10 cycles, printing the message once every 1s, the end of the cycle, prints and outputs all cached data immediately, then call esp_restart () to restart, in which esp_restart()in the esp_system.hdefinition of a soft reset chip.

Code results are as follows:
Hello world operating results

As can be seen from the above code, Hello worldprocedures and other embedded entry program is not very different, just pay attention to the relevant Lexin API can be in use.

From the architecture point of view, esp-idfthe framework relies on its unique components (component) characteristics, reduced coupling between various components, so that customers can easily integrate all functions esp-idf in, and can easily develop your own code.

Published 57 original articles · won praise 76 · views 120 000 +

Guess you like

Origin blog.csdn.net/espressif/article/details/85799241