CentOS7 / Ubuntu + ESP32 + CMake【荐】


相关资料

ESP-IDF编程指南
ESP-IDF代码仓库


详细内容

详情请见:https://www.daobanmojie.com/19.html

安装环境

安装依赖

Ubuntu: 更新apt,并安装依赖。

sudo apt update	
sudo apt upgrade
sudo apt-get install -y git wget flex bison gperf python3 python3-pip python3-setuptools cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0

CentOS7: 更新yum源,并安装依赖。

sudo yum -y update && sudo yum install git wget flex bison gperf python3 cmake ninja-build ccache dfu-util

Python: ESP需要使用Python3。查看Python版本python --version
如果默认python为python2,参照以下更改。
Ubuntu:

# 安装 Python 3:
sudo apt-get install python3 python3-pip python3-setuptools
# 设置 Python 3 为默认 Python 版本:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && alias pip=pip3

CentOS7:

# 安装 Python 3:
sudo yum -y update && sudo yum install python3 python3-pip python3-setuptools
# 设置 Python 3 为默认 Python 版本:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 && alias pip=pip3

配置 ESP-IDF

下载 ESP-IDF

下载代码仓库

mkdir -p ~/esp
cd ~/esp
git clone --recursive https://github.com/espressif/esp-idf.git

版本选择最新发布版,这篇文章时,最新发布版本为 v4.2,对应的commitc40f259

git checkout v4.2
git submodule update --init --recursive

或者直接下载对应版本,如:

git clone -b v4.2 --recursive https://github.com/espressif/esp-idf.git
配置使用工具

除了 ESP-IDF 本身,您还需要安装 ESP-IDF 使用的各种工具,比如编译器、调试器、Python 包等。
这些进入esp-idf目录,执行对应脚本即可。

cd ~/esp/esp-idf
./install.sh
配置环境变量

执行脚本,配置环境变量。[tag type=“danger”]注意:配置的是IDF虚拟环境,关闭终端后再次使用需重新配置。[/tag]

. ./export.sh  #或者执行  . $HOME/esp/esp-idf/export.sh

查看环境变量

env

创建工程

拷贝Hello Word!工程

ESP有很多工程可以参考,我们以hello_world为例。
拷贝hello_world工程到esp目录

cd ~/esp
cp -r $IDF_PATH/examples/get-started/hello_world .
配置工程

设置目标芯片,这里使用的是ESP32

cd ~/esp/hello_world
idf.py set-target esp32

esp32 — 适用于 ESP32-D0WD、ESP32-D2WD、ESP32-S0WD、ESP32-U4WDH、ESP32-PICO-D4
esp32s2— 适用于 ESP32-S2

运行工程配置工具 menuconfig

可以通过此菜单设置项目的具体变量,包括 Wi-Fi 网络名称、密码和处理器速度等。
hello_world 示例项目会以默认配置运行,因此也可以跳过使用 menuconfig 进行项目配置这一步骤。

idf.py menuconfig
menuconfig列表。
     SDK tool configuration  --->
     Build type  --->
     Application manager  --->
     Bootloader config  --->
     Security features  --->
     Serial flasher config  --->
     Partition Table  --->
     Compiler options  --->
     Component config  --->
     Compatibility options  --->

如果您使用的是 ESP32-DevKitC(板载 ESP32-SOLO-1 模组),请在烧写示例程序前,前往 menuconfig 中使能单核模式(CONFIG_FREERTOS_UNICORE)。

编译下载工程
编译工程

首次编译时间较长,耐心等待。

idf.py build

运行以上命令可以编译应用程序和所有 ESP-IDF 组件,
相当于执行了idf.py app idf.py bootloader idf.py partition_table
分别生成 bootloader bootloader.bin分区表 partition-table.bin应用程序二进制bin文件 hello-world.bin

查看端口

查看板子连接端口提示如下:

dmesg | grep tty
# [    0.083320] printk: console [tty0] enabled
# [    0.771868] 00:05: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A
# [79380.712416] usb 2-2.2: cp210x converter now attached to ttyUSB0

此时设备连接端口为ttyUSB0

烧录到设备

应用烧录指令idf.py -p PORT [-b BAUD] flash
PORT : 是我们连接开发板的串口名称;
BAUD : 为烧录的波特率[选填],默认波特率为 460800。

idf.py -p /dev/ttyUSB0 flash 
查看监视器

使用 idf.py -p PORT monitor 命令,监视 hello_world 工程的运行情况。

idf.py -p /dev/ttyUSB0 monitor

可使用快捷键 Ctrl+],退出 IDF 监视器。

监视器内容
Hello world!
This is ESP32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 2MB external flash
Restarting in 10 seconds...
Restarting in 9 seconds...
Restarting in 8 seconds...
Restarting in 7 seconds...
Restarting in 6 seconds...
Restarting in 5 seconds...
Restarting in 4 seconds...
Restarting in 3 seconds...
Restarting in 2 seconds...
Restarting in 1 seconds...
Restarting in 0 seconds...
Restarting now.
ets Jun  8 2016 00:22:57
更改项目

更改打印输出的内容

cd ~/esp/hello_world/main
vim hello_world_main.c
hello_world_main.c内容
/* Hello World Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"

#ifdef CONFIG_IDF_TARGET_ESP32
#define CHIP_NAME "ESP32"
#endif

#ifdef CONFIG_IDF_TARGET_ESP32S2BETA
#define CHIP_NAME "ESP32-S2 Beta"
#endif

void app_main(void)
{
    printf("Hello DaoBanMoJie!\n");
    printf("http://daobanmojie.com/19.html\n");

    /* Print chip information */
    esp_chip_info_t chip_info;
    esp_chip_info(&chip_info);
    printf("This is %s chip with %d CPU cores, WiFi%s%s, ",
            CHIP_NAME,
            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");

    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();
}

使用idf.py app,仅编译app内容,然后烧录并打印到监视器。

cd ~/esp/hello_world/
idf.py -p /dev/ttyUSB0 app-flash monitor # 仅构建并下载应用程序,之后打开监视器。

到这里整个工程搭建就结束了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盗版摩羯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值