ESP32入门之环境搭建

ESP32入门

官方指南

快速入门

环境搭建(Ubuntu in WSL)

WSL

Version:WSL1
由于WSL2还不支持串口,所以只能采用WSL1来搭环境,可以采用以下的两种方式

  1. 系统迁移
    win中将WSL2的系统迁移可以使用wsl --set-version [system_name] 1,将指定名称的WSL系统迁移到版本1
  2. 直接装一个新的wsl系统
    如果原本的系统储存已经很大了,方法一要运行一段时间
    在确定是WSL1后ls /dev,会看到很多ttyS*的设备号,这些设备号对应win设备管理器->端口的端口号(ttyS1<->com1)
    在这里插入图片描述

在这里插入图片描述

采用的ESP32是直接通过micro usb直接连接到电脑上的,驱动程序可以去Download and Install VCP Drivers下载,我下载的是CP210x Windows Drivers,驱动问题很可能导致后面写入出现timeout问题。
安装驱动后设备管理器没有感叹号就可以了,win10在设备管理器里面对应的设备右键->属性,可以设置波特率等
在这里插入图片描述

在高级里面可以切换使用的com口,如果在ESP32连接后出现对象名已存在的问题,只要切换没有用到的com口就可以,最后要记住这个端口在linux里面的对应值ttyS*,驱动有问题啥的建议直接卸载设备重装,烧写就是跪在这里。

抓code

官方提供的抓code方式

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

其中–recursive的意思是把该项目的其他子模块项目也一起抓下载,这样才能full build,项目大了这样分project code也可以理解,但这样容易出现抓code 错误,一般可能是某个子模块抓不成功最后导致整个项目不完整,解决方式参考git clone —recursive 快速高效下载方法,大致流程是

git clone https://github.com.cnpmjs.org/espressif/esp-idf.git           //加那个.cnpmjs.org是使用国内镜像,可以抓快点

然后修改.gitmodules,这个文件相当于子模块的索引文件,在vsc里面打开这个文件后把…/…/的改成对应的github网址
在这里插入图片描述

可以看到也加了.cnpmjs.org,这样子模块也可以加速抓code
文件改后运行

git submodule sync
git submodule update --init --recursive

会自动去抓子模块代码
抓code后要设置工具,主要使用

./install.sh
. ./export.sh

export.sh实际上是把各种工具(eg:idf.py)添加到环境变量里面,这意味着每次build esp project都要有这部分环境变量(都要运行. ./export.sh),当然可以加入到bashrc里面作为永久变量,不过官方不推荐,因为不是每次登录都需要这些环境变量,每次运行这个动作是冗余的

build code

按照官网就好,可以试试抓下来的路径/root/esp/esp-idf/examples/get-started/hello_world这个project,软链接或者直接在这个目录下编译idf.py build,然后烧写idf.py -p PORT [-b BAUD] flash,PORT对应之前记录的/dev/ttyS*(路径要写全),波特率最好设置和win设备管理器一样,最好加这个参数,官方默认值是460800,没对上可能导致header error。

烧写成功的打印是

...
[ 75%] Built target __idf_jsmn
[ 80%] Built target __idf_mqtt
[ 80%] Built target __idf_freemodbus
[ 81%] Built target __idf_openssl
[ 92%] Built target __idf_libsodium
[ 93%] Built target __idf_spiffs
[ 94%] Built target __idf_protocomm
[ 94%] Built target __idf_mdns
[ 95%] Built target __idf_cmock
[ 97%] Built target __idf_fatfs
[ 98%] Built target __idf_esp_local_ctrl
[ 99%] Built target __idf_wifi_provisioning
[ 99%] Built target __idf_main
[ 99%] Built target __ldgen_output_esp32.project.ld
[100%] Built target hello-world.elf
[100%] Built target gen_project_binary
hello-world.bin binary size 0x280b0 bytes. Smallest app partition is 0x100000 bytes. 0xd7f50 bytes (84%) free.
[100%] Built target app_check_size
[100%] Built target app
esptool.py esp32 -p /dev/ttyS3 -b 115200 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 2MB 0x1000 bootloader/bootloader.bin 0x10000 hello-world.bin 0x8000 partition_table/partition-table.bin
esptool.py v3.1-dev
Serial port /dev/ttyS3
Connecting....
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 08:3a:f2:52:9a:98
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Flash will be erased from 0x00001000 to 0x00007fff...
Flash will be erased from 0x00010000 to 0x00038fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Compressed 24784 bytes to 15374...
Writing at 0x00001000... (100 %)
Wrote 24784 bytes (15374 compressed) at 0x00001000 in 1.8 seconds (effective 111.6 kbit/s)...
Hash of data verified.
Compressed 164016 bytes to 86491...
Writing at 0x00010000... (16 %)
Writing at 0x0001ab78... (33 %)
Writing at 0x0002035e... (50 %)
Writing at 0x00025b78... (66 %)
Writing at 0x0002da4c... (83 %)
Writing at 0x000365ef... (100 %)
Wrote 164016 bytes (86491 compressed) at 0x00010000 in 7.8 seconds (effective 167.4 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 103...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (103 compressed) at 0x00008000 in 0.1 seconds (effective 327.1 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
[100%] Built target flash
Done

如果过程中卡在connect...,如果错误是和esptool.py相关,可以试着check几个地方

  1. 驱动是否正常,尝试卸载驱动重装
  2. 端口配置是否正常,波特率,ttyS*和com号是不是相同
  3. 线材有没有问题
    基本是这几个地方,官方是使用python做端口的处理,可以自己写个脚本测试看看,我自己写了一个在wsl(ubuntu)上运行,然后在win上用虚拟串口和串口助手配合做了测试,排除了wsl的问题
import serial
import threading
import time

portname="/dev/ttyS2"

ser = serial.Serial(portname, 115200)
thread_on=True

def sends():
    """ 发送数据 """
    global thread_on
    while thread_on:
        inp = input("请输入要发送的字符:")
        if(inp!="dead" and ser):
            inp = inp.encode('utf-8')
            ser.write(inp)  # 发送
        else:
            thread_on=False


def reads():
    """ 读取数据 """
    global thread_on
    out = ''
    while thread_on:
        while ser.inWaiting() > 0:
            out += ser.read(1).decode()  # 一个一个的读取
        if out != '':
            print(out)
            out = ''


if __name__ == '__main__':
    print("hello")
    t1 = threading.Thread(target=sends, name='sends')
    t2 = threading.Thread(target=reads, name='reads')
    t1.start()
    t2.start()

官方提到这个问题的解决是按boot后按en,当串口助手显示

rst:0x1 (POWERON_RESET),boot:0x3 (DOWNLOAD_BOOT(UART0/UART1/SDIO_REI_REO_V2))
waiting for download

实际上就完成了官方要我们做的硬件复位功能,如果没有解决问题就要去check其他

Debug

官方有一个监听端口输出的工具,使用idf.py -p PORT monitor,会自动运行并打印输出,运行结束后会重启,效果如下

I (235) cpu_start: ELF file SHA256:  5c198ef1b1f74ead...
I (241) cpu_start: ESP-IDF:          v4.4-dev-1594-g1d7068e4b-dirty
I (248) heap_init: Initializing. RAM available for dynamic allocation:
I (255) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (261) heap_init: At 3FFB2BA0 len 0002D460 (181 KiB): DRAM
I (267) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (274) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (280) heap_init: At 4008A7F0 len 00015810 (86 KiB): IRAM
I (287) spi_flash: detected chip: generic
I (291) spi_flash: flash io: dio
W (295) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (309) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Hello world!
---------------------------------------------CGX---------------------------------------------
This is esp32 chip with 2 CPU core(s), WiFi/BT/BLE, silicon revision 1, 2MB external flash
Minimum free heap size: 294496 bytes
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...

里面那句CGX是我在hello_world/main/hello_world_main.c里面增加的打印

总结

乐鑫的工程还没有实际使用过,这里主要是环境搭建,貌似乐鑫内部使用的是类似于ucos的系统,只不过都写好了,不需要用户去移植,只要根据他们的api添加自定义的软件就可以,类似于Android开发,对开发者来说方便,但如果想学内核,这里没有哦。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值