ESP32系列--第二篇 开发环境搭建-Ubuntu系统

一、目的

        

        在上一篇《ESP32系列--第一篇 初始ESP32》我们简单介绍了一下ESP32的基本信息,本篇我们将介绍如何搭建开发环境、如何进行程序编译、下载、运行。

        开发者可以基于ESP-IDF开发或者基于Arduino开发,本篇主要介绍ESP-IDF的开发过程(主要是考虑到ESP-IDF更适合作为开发功能复杂的产品)

        下面的视频是用ESP32实现的WIFI网络时钟

ws2812_network_clock

二、准备

         开发板

        

        系统环境

                如果是windows的同学可以通过虚拟机安装Ubuntu环境(具体安装过程请参考网上相关资料)

        Ubuntu 18.04

fire@fire-pd:~$ uname -a
Linux fire-pd 5.4.0-84-generic #94~18.04.1-Ubuntu SMP Thu Aug 26 23:17:46 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

        那么首先让我们打开一个terminal

        

三、实战

        参考资料

Get Started - ESP32 - — ESP-IDF Programming Guide latest documentationhttps://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html#                1、创建工作目录,将ESP32相关的内容统一放在此目录下

$ mkdir -p ~/workspace/third/esp32/
$ cd ~/workspace/third/esp32/

                2、下载依赖工具

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

                3、获取ESP-IDF仓库仓库

$ git clone --recursive https://github.com/espressif/esp-idf.git

                如果clone由于各种原因出错,并且本地没有esp-idf目录的情况下则一直重试以上命令;

                如果本地已经有了esp-idf目录

$ git clone --recursive https://github.com/espressif/esp-idf.git
fatal: destination path 'esp-idf' already exists and is not an empty directory.

                这个时候我们通过以下命令继续下载(多试几次)

$ cd esp-idf/
$ git submodule update --init --recursive

                所有的子模块安装完毕后,执行以下命令安装依赖工具

$ cd ~/workspace/third/esp-idf/
$ ./install.sh 
Detecting the Python interpreter
Checking "python" ...
/home/你的用户名/workspace/third/esp32/esp-idf/tools/detect_python.sh: line 16: python: command not found
Checking "python3" ...
Python 3.6.9
"python3" has been detected
Checking Python compatibility
Traceback (most recent call last):
  File "/home/你的用户名/workspace/third/esp32/esp-idf/tools/python_version_checker.py", line 45, in <module>
    check()
  File "/home/你的用户名/workspace/third/esp32/esp-idf/tools/python_version_checker.py", line 39, in check
    _ver_to_str(OLDEST_PYTHON_SUPPORTED), _ver_to_str(sys.version_info[:3])
RuntimeError: ESP-IDF supports Python 3.7 or newer but you are using Python 3.6.9. Please upgrade your installation as described in the documentation.

                当前esp-idf install.sh脚本需要python3.7或者更新的版本,请自行google安装

https://linuxize.com/post/how-to-install-python-3-8-on-ubuntu-18-04/https://linuxize.com/post/how-to-install-python-3-8-on-ubuntu-18-04/

https://www.itsupportwale.com/blog/how-to-upgrade-to-python-3-7-on-ubuntu-18-10/https://www.itsupportwale.com/blog/how-to-upgrade-to-python-3-7-on-ubuntu-18-10/

        How to Upgrade to Python 3.8 on Ubuntu — tech.serhatteker.comhttps://tech.serhatteker.com/post/2019-12/upgrade-python38-on-ubuntu/                

                python3.8安装成功后我们再次到esp-idf目录下执行

$ cd ~/workspace/third/esp32/esp-idf
$ ./install.sh

                这条命令会下载工具链以及其他一些开发工具,可能需要等待一些时间才能完成,如果失败了,就继续重试此命令直到成功。

        

                在命令行中输入

$ . ./export.sh
 . ./export.sh
Setting IDF_PATH to '/home/你的用户名/workspace/third/esp32/esp-idf'
Detecting the Python interpreter
Checking "python" ...
Checking "python3" ...
Python 3.8.12
"python3" has been detected
Checking Python compatibility
Adding ESP-IDF tools to PATH...
Using Python interpreter in /home/你的用户名/.espressif/python_env/idf5.0_py3.8_env/bin/python
Checking if Python packages are up to date...
Skipping the download of /home/你的用户名/.espressif/espidf.constraints.v5.0.txt because it was downloaded recently. If you believe that this is causing you trouble then remove it manually and re-run your install script.
Constraint file: /home/你的用户名/.espressif/espidf.constraints.v5.0.txt
Requirement files:
 - /home/你的用户名/workspace/third/esp32/esp-idf/tools/requirements/requirements.core.txt
Python being checked: /home/你的用户名/.espressif/python_env/idf5.0_py3.8_env/bin/python
Python requirements are satisfied.
Added the following directories to PATH:
  /home/你的用户名/workspace/third/esp32/esp-idf/components/esptool_py/esptool
  /home/你的用户名/workspace/third/esp32/esp-idf/components/espcoredump
  /home/你的用户名/workspace/third/esp32/esp-idf/components/partition_table
  /home/你的用户名/workspace/third/esp32/esp-idf/components/app_update
  /home/你的用户名/.espressif/tools/xtensa-esp32-elf/esp-2021r2-8.4.0/xtensa-esp32-elf/bin
  /home/你的用户名/.espressif/tools/xtensa-esp32s2-elf/esp-2021r2-8.4.0/xtensa-esp32s2-elf/bin
  /home/你的用户名/.espressif/tools/xtensa-esp32s3-elf/esp-2021r2-8.4.0/xtensa-esp32s3-elf/bin
  /home/你的用户名/.espressif/tools/riscv32-esp-elf/esp-2021r2-8.4.0/riscv32-esp-elf/bin
  /home/你的用户名/.espressif/tools/esp32ulp-elf/2.28.51-esp-20191205/esp32ulp-elf-binutils/bin
  /home/你的用户名/.espressif/tools/esp32s2ulp-elf/2.28.51-esp-20191205/esp32s2ulp-elf-binutils/bin
  /home/你的用户名/.espressif/tools/openocd-esp32/v0.11.0-esp32-20211220/openocd-esp32/bin
  /home/你的用户名/.espressif/python_env/idf5.0_py3.8_env/bin
  /home/你的用户名/workspace/third/esp32/esp-idf/tools
Done! You can now compile ESP-IDF projects.
Go to the project directory and run:

  idf.py build

             

           这样esp-idf开发环境就搭建好了,以后编译时就使用idf.py工具即可。但是考虑到每次打开terminal都需要在esp-idf目录下敲击此命令比较麻烦,我们可以这样做,在~/.bashrc目录下的最后一行加入如下命令并保存。

alias get_idf='. $HOME/workspace/third/esp32/esp-idf/export.sh'

              让我们重新打开一个新的terminal,然后输入get_idf即可,此时idf.py命令既可以使用了。

                4、编译示例工程

                首先我们从esp-idf目录下的examples目录拷贝hello_world工程

$ mkdir -p ~/workspace/work/esp32
$ cp -r ~/workspace/third/esp32/esp-idf/examples/get-started/hello_world ~/workspace/work/esp32/
$ cd ~/workspace/work/esp32/hello_world/
$ tree 
.
├── CMakeLists.txt
├── example_test.py
├── main
│   ├── CMakeLists.txt
│   └── hello_world_main.c
├── README.md
└── sdkconfig.ci

                 5、开始编译

$ idf.py set-target esp32
$ idf.py menuconfig

                set-target命令设置芯片平台,因为有esp32/esp32-s2/esp32-s3等等不同的芯片,故需要设置一下;

                menuconfig用于设置sdk以及一些编译选项、烧写选项;

                 此处我们只要设置一下flash选项即可,选择flash大小为4MB,然后保存退出。

                 然后在命令行输入

$ idf.py build

                此时屏幕会输出编译的过程,内容较多,直到编译完成

[94/95] Generating binary image from built executable
esptool.py v3.3-dev
Creating esp32 image...
Merged 1 ELF section
Successfully created esp32 image.
Generated /home/你的用户名/workspace/work/esp32/hello_world/build/bootloader/bootloader.bin
[95/95] cd /home/你的用户名/workspace/work/esp32/hello_world/build/bootloader/esp-idf/esptool_py && /home/你的用户名/.espressif/python_env/idf5.0_py3.8_env/bin/python /home/你的用户名/workspace/third/esp32/esp-idf/components/partition_table/check_sizes.py --offset 0x8000 bootloader 0x1000 /home/你的用户名/workspace/work/esp32/hello_world/build/bootloader/bootloader.bin
Bootloader binary size 0x6350 bytes. 0xcb0 bytes (11%) free.
[883/884] Generating binary image from built executable
esptool.py v3.3-dev
Creating esp32 image...
Merged 2 ELF sections
Successfully created esp32 image.
Generated /home/你的用户名/workspace/work/esp32/hello_world/build/hello_world.bin
[884/884] cd /home/你的用户名/workspace/work/esp32/hello_world/build/es.../home/你的用户名/workspace/work/esp32/hello_world/build/hello_world.bin
hello_world.bin binary size 0x29800 bytes. Smallest app partition is 0x100000 bytes. 0xd6800 bytes (84%) free.

Project build complete. To flash, run this command:
/home/你的用户名/.espressif/python_env/idf5.0_py3.8_env/bin/python ../../../third/esp32/esp-idf/components/esptool_py/esptool/esptool.py -p (PORT) -b 460800 --before default_reset --after hard_reset --chip esp32  write_flash --flash_mode dio --flash_size detect --flash_freq 40m 0x1000 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin 0x10000 build/hello_world.bin
or run 'idf.py -p (PORT) flash'

                如果看到这样的结果,说明编译完成,下面就可以烧写了。

$ idf.py flash

                此时可能会报错

$ idf.py flash
Executing action: flash
Serial port /dev/ttyUSB0
/dev/ttyUSB0 failed to connect: [Errno 13] could not open port /dev/ttyUSB0: [Errno 13] Permission denied: '/dev/ttyUSB0'
No serial ports found. Connect a device, or use '-p PORT' option to set a specific port.

                通过此条命令解决,需要重新登录或者重启电脑

$ sudo usermod -a -G dialout $USER

                   重启后重新进入~/workspace/work/esp32/hello_world/

$ cd ~/workspace/work/esp32/hello_world/
$ get_idf
$ idf.py flash
Executing action: flash
Serial port /dev/ttyUSB0
Connecting......
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting......
Detecting chip type... ESP32
Running ninja in directory /home/你的用户名/workspace/work/esp32/hello_world/build
Executing "ninja flash"...
[1/5] cd /home/你的用户名/workspace/work/esp32/hello_world/build/esp-idf/esptool_py...n-table.bin /home/你的用户名/workspace/work/esp32/hello_world/build/hello_world.bin
hello_world.bin binary size 0x29800 bytes. Smallest app partition is 0x100000 bytes. 0xd6800 bytes (84%) free.
[2/5] Performing build step for 'bootloader'
[1/1] cd /home/你的用户名/workspace/work/esp32/hello_world/build/bootloader/esp-idf/esptool_py && /home/你的用户名/.espressif/python_env/idf5.0_py3.8_env/bin/python /home/你的用户名/workspace/third/esp32/esp-idf/components/partition_table/check_sizes.py --offset 0x8000 bootloader 0x1000 /home/你的用户名/workspace/work/esp32/hello_world/build/bootloader/bootloader.bin
Bootloader binary size 0x6350 bytes. 0xcb0 bytes (11%) free.
[2/3] cd /home/你的用户名/workspace/third/esp32/esp-idf/components/esptool_py && /s...ire/workspace/third/esp32/esp-idf/components/esptool_py/run_serial_tool.cmake
esptool.py esp32 -p /dev/ttyUSB0 -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 4MB 0x1000 bootloader/bootloader.bin 0x10000 hello_world.bin 0x8000 partition_table/partition-table.bin
esptool.py v3.3-dev
Serial port /dev/ttyUSB0
Connecting.........
Chip is ESP32-D0WD (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: bc:dd:c2:cf:f3:04
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Flash will be erased from 0x00001000 to 0x00007fff...
Flash will be erased from 0x00010000 to 0x00039fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Compressed 25424 bytes to 15906...
Writing at 0x00001000... (100 %)
Wrote 25424 bytes (15906 compressed) at 0x00001000 in 0.8 seconds (effective 268.0 kbit/s)...
Hash of data verified.
Compressed 169984 bytes to 89582...
Writing at 0x00010000... (16 %)
Writing at 0x0001b0ff... (33 %)
Writing at 0x000208b8... (50 %)
Writing at 0x00026067... (66 %)
Writing at 0x0002e682... (83 %)
Writing at 0x00036a64... (100 %)
Wrote 169984 bytes (89582 compressed) at 0x00010000 in 2.4 seconds (effective 570.2 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 444.3 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
Done

                烧写完成后通过串口看程序执行

        

$ idf.py monitor
Executing action: monitor
Serial port /dev/ttyUSB0
Connecting.....
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting....
Detecting chip type... ESP32
Running idf_monitor in directory /home/你的用户名/workspace/work/esp32/hello_world
Executing "/home/你的用户名/.espressif/python_env/idf5.0_py3.8_env/bin/python /home/你的用户名/workspace/third/esp32/esp-idf/tools/idf_monitor.py -p /dev/ttyUSB0 -b 115200 --toolchain-prefix xtensa-esp32-elf- --target esp32 --revision 0 /home/你的用户名/workspace/work/esp32/hello_world/build/hello_world.elf -m '/home/你的用户名/.espressif/python_env/idf5.0_py3.8_env/bin/python' '/home/你的用户名/workspace/third/esp32/esp-idf/tools/idf.py'"...
--- idf_monitor on /dev/ttyUSB0 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:6752
load:0x40078000,len:14796
load:0x40080400,len:3792
0x40080400: _init at ??:?

entry 0x40080694
I (27) boot: ESP-IDF v5.0-dev-1599-gb66cc63c41 2nd stage bootloader
I (27) boot: compile time 14:48:13
I (28) boot: chip revision: 1
I (32) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (39) boot.esp32: SPI Speed      : 40MHz
I (44) boot.esp32: SPI Mode       : DIO
I (48) boot.esp32: SPI Flash Size : 4MB
I (53) boot: Enabling RNG early entropy source...
I (58) boot: Partition Table:
I (62) boot: ## Label            Usage          Type ST Offset   Length
I (69) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (76) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (84) boot:  2 factory          factory app      00 00 00010000 00100000
I (91) boot: End of partition table
I (96) boot_comm: chip revision: 1, min. application chip revision: 0
I (103) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=0782ch ( 30764) map
I (122) esp_image: segment 1: paddr=00017854 vaddr=3ffb0000 size=0243ch (  9276) load
I (126) esp_image: segment 2: paddr=00019c98 vaddr=40080000 size=06380h ( 25472) load
I (141) esp_image: segment 3: paddr=00020020 vaddr=400d0020 size=14978h ( 84344) map
I (172) esp_image: segment 4: paddr=000349a0 vaddr=40086380 size=04e24h ( 20004) load
I (180) esp_image: segment 5: paddr=000397cc vaddr=50000000 size=00010h (    16) load
I (186) boot: Loaded app from partition at offset 0x10000
I (186) boot: Disabling RNG early entropy source...
I (200) cpu_start: Pro cpu up.
I (201) cpu_start: Starting app cpu, entry point is 0x40081004
0x40081004: call_start_cpu1 at /home/你的用户名/workspace/third/esp32/esp-idf/components/esp_system/port/cpu_start.c:152

I (0) cpu_start: App cpu up.
I (215) cpu_start: Pro cpu start user code
I (215) cpu_start: cpu freq: 160000000 Hz
I (215) cpu_start: Application information:
I (219) cpu_start: Project name:     hello_world
I (225) cpu_start: App version:      1
I (229) cpu_start: Compile time:     Feb 20 2022 14:48:01
I (235) cpu_start: ELF file SHA256:  1d67701d2b239735...
I (241) cpu_start: ESP-IDF:          v5.0-dev-1599-gb66cc63c41
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 3FFB2D30 len 0002D2D0 (180 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 4008B1A4 len 00014E5C (83 KiB): IRAM
I (287) spi_flash: detected chip: generic
I (291) spi_flash: flash io: dio
I (296) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Hello world!
This is esp32 chip with 2 CPU core(s), WiFi/BT/BLE, silicon revision 1, 4MB external flash
Minimum free heap size: 296120 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...

        至此,ESP32 ESP-IDF开发环境就讲解完了。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值