introduction
Hello world
All 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 world
program 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 ESP32
on the Hello world
implementation 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
ESP32
Frame is used ESP-IDF
(Espressif IoT Development Framework), ESP-IDF
can be seen as different functional components (Component) integration, each component (Component) generally a class of functions.
ESP-IDF
These components allow easy configuration to use, the user can freely select these components into your own project.
The fatfs
assembly is to achieve the FAT file system; driver
component contains ESP32
all the peripheral driver owned.
The parameters of these components by the make menuconfig
modified configuration.
In use ESP32
when 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 world
program, 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 Makefile
will set the PROJECT_NAME
variable, the compiler generates the user BIN
file name from the file name for this; and will also contain ESP_IDF
the core of the Makefile: $ (IDF_PATH) /make/project.mk, noted that only contains this Makefile
file will eventually link to ESP-IDF
go 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 make
functions.
2. main directory
main
Directory contains the source code of the program and a component.mk
file, which component.mk
will be integrated into the user program compiled ESP-IDF
in, which can be empty or may contain only some of the control variables defined component
compilation process. If you do not component.mk
file, ESP-IDF will not compile this directory, the runtime can not be found will be reported out of app_main
the error.
3. sdkconfig
sdkconfig
The file will be automatically generated after compilation, which mainly contains the user's program configuration information, through the use of this document may make menuconfig
be changed, the configuration information on all of component
the entry into force (including ESP-IDF
), the average user can create a sdkconfig.defaults
file 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
build
Catalog generation at compile time, the compiled output file is placed in a folder, which contains each of the component
intermediate 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 world
a program for the required header file, which contains a standard header file, twoFreeRTOS
headers and two Yue Xin header files (toesp_
begin with).
void app_main()
{
app_main
For the entire program entry, the system will automatically findapp_main
function, and has since the beginning of the function execution (similar to the C languagemain
functions). In this function, you can initialize some applications, such as creating othertask
,app_main
the 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()
functionesp_system.h
definitions, and functions to acquire information of the chip, in order toesp_chip_info_t
return, which containsCPU
information auditing, chip version number,spi_flash_get_chip_size()
functionesp_spi_flash.h
definition , is to getflash
the 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 theesp_system.h
definition of a soft reset chip.
Code results are as follows:
As can be seen from the above code, Hello world
procedures 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-idf
the 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.