开始

开发板图片

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


cd ~/esp/esp-idf
./install.sh esp32

//导出环境变量
. ./export.sh


cd ~/esp
cp -r $IDF_PATH/examples/get-started/hello_world .

配置

设置目标芯片 :


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

其他配置:

idf.py menuconfig //可以不用执行

编译

执行命令 idf.py build 即可。一个成功的案例:

Executing action: all (aliases: build)
Running ninja in directory /Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world/build
Executing "ninja all"...
[13/924] Generating ../../partition_table/partition-table.binPartition table binary generated. Contents:
*******************************************************************************
# ESP-IDF Partition Table
# Name, Type, SubType, Offset, Size, Flags
nvs,data,nvs,0x9000,24K,
phy_init,data,phy,0xf000,4K,
factory,app,factory,0x10000,1M,
*******************************************************************************
[503/924] Performing configure step for 'bootloader'-- Found Git: /usr/bin/git (found version "2.39.3 (Apple Git-146)")
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /Users/kyson/.espressif/tools/xtensa-esp32-elf/esp-12.2.0_20230208/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Users/kyson/.espressif/tools/xtensa-esp32-elf/esp-12.2.0_20230208/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Users/kyson/.espressif/tools/xtensa-esp32-elf/esp-12.2.0_20230208/xtensa-esp32-elf/bin/xtensa-esp32-elf-g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Building ESP-IDF components for target esp32
-- Project sdkconfig file /Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world/sdkconfig
Compiler supported targets: xtensa-esp32-elf

-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of time_t
-- Check size of time_t - done
-- Adding linker script /Users/kyson/esp/v5.1.3/esp-idf/components/soc/esp32/ld/esp32.peripherals.ld
-- App "bootloader" version: v5.1.3-dirty
-- Adding linker script /Users/kyson/esp/v5.1.3/esp-idf/components/esp_rom/esp32/ld/esp32.rom.ld
-- Adding linker script /Users/kyson/esp/v5.1.3/esp-idf/components/esp_rom/esp32/ld/esp32.rom.api.ld
-- Adding linker script /Users/kyson/esp/v5.1.3/esp-idf/components/esp_rom/esp32/ld/esp32.rom.libgcc.ld
-- Adding linker script /Users/kyson/esp/v5.1.3/esp-idf/components/esp_rom/esp32/ld/esp32.rom.newlib-funcs.ld
-- Adding linker script /Users/kyson/esp/v5.1.3/esp-idf/components/bootloader/subproject/main/ld/esp32/bootloader.ld
-- Adding linker script /Users/kyson/esp/v5.1.3/esp-idf/components/bootloader/subproject/main/ld/esp32/bootloader.rom.ld
-- Components: bootloader bootloader_support efuse esp_app_format esp_common esp_hw_support esp_rom esp_system esptool_py freertos hal log main micro-ecc newlib partition_table soc spi_flash xtensa
-- Component paths: /Users/kyson/esp/v5.1.3/esp-idf/components/bootloader /Users/kyson/esp/v5.1.3/esp-idf/components/bootloader_support /Users/kyson/esp/v5.1.3/esp-idf/components/efuse /Users/kyson/esp/v5.1.3/esp-idf/components/esp_app_format /Users/kyson/esp/v5.1.3/esp-idf/components/esp_common /Users/kyson/esp/v5.1.3/esp-idf/components/esp_hw_support /Users/kyson/esp/v5.1.3/esp-idf/components/esp_rom /Users/kyson/esp/v5.1.3/esp-idf/components/esp_system /Users/kyson/esp/v5.1.3/esp-idf/components/esptool_py /Users/kyson/esp/v5.1.3/esp-idf/components/freertos /Users/kyson/esp/v5.1.3/esp-idf/components/hal /Users/kyson/esp/v5.1.3/esp-idf/components/log /Users/kyson/esp/v5.1.3/esp-idf/components/bootloader/subproject/main /Users/kyson/esp/v5.1.3/esp-idf/components/bootloader/subproject/components/micro-ecc /Users/kyson/esp/v5.1.3/esp-idf/components/newlib /Users/kyson/esp/v5.1.3/esp-idf/components/partition_table /Users/kyson/esp/v5.1.3/esp-idf/components/soc /Users/kyson/esp/v5.1.3/esp-idf/components/spi_flash /Users/kyson/esp/v5.1.3/esp-idf/components/xtensa
-- Configuring done (8.7s)
-- Generating done (0.1s)
-- Build files have been written to: /Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world/build/bootloader
[103/104] Generating binary image from built executableesptool.py v4.7.0
Creating esp32 image...
Merged 1 ELF section
Successfully created esp32 image.
Generated /Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world/build/bootloader/bootloader.bin
[104/104] cd /Users/kyson/esp/v5.1.3/esp-idf/...d/hello_world/build/bootloader/bootloader.binBootloader binary size 0x6850 bytes. 0x7b0 bytes (7%) free.
[923/924] Generating binary image from built executableesptool.py v4.7.0
Creating esp32 image...
Merged 2 ELF sections
Successfully created esp32 image.
Generated /Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world/build/hello_world.bin
[924/924] cd /Users/kyson/esp/v5.1.3/esp-idf/...get-started/hello_world/build/hello_world.binhello_world.bin binary size 0x2aee0 bytes. Smallest app partition is 0x100000 bytes. 0xd5120 bytes (83%) free.

Project build complete. To flash, run this command:
/Users/kyson/.espressif/python_env/idf4.4_py3.9_env/bin/python ../../../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 2MB --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'

写 flash

idf.py -p /dev/tty.usbserial-0001 flash

其中 /dev/tty.usbserial-0001 是串口的 port,每个人电脑显示不一定一样,但肯定是 tty.usb 开头

一个成功的输出如下:

Executing action: flash
Running ninja in directory /Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world/build
Executing "ninja flash"...
[1/5] cd /Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world/build/esp-idf/esptool_py && /Users/kyson/.espressif/python_env/idf4.4_py3.9_env/bin/python /Users/kyson/esp/v5.1.3/esp-idf/components/partition_table/check_sizes.py --offset 0x8000 partition --type app /Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world/build/partition_table/partition-table.bin /Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world/build/hello_world.bin
hello_world.bin binary size 0x2aee0 bytes. Smallest app partition is 0x100000 bytes. 0xd5120 bytes (83%) free.
[2/5] Performing build step for 'bootloader'
[1/1] cd /Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world/build/bootloader/esp-idf/esptool_py && /Users/kyson/.espressif/python_env/idf4.4_py3.9_env/bin/python /Users/kyson/esp/v5.1.3/esp-idf/components/partition_table/check_sizes.py --offset 0x8000 bootloader 0x1000 /Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world/build/bootloader/bootloader.bin
Bootloader binary size 0x6850 bytes. 0x7b0 bytes (7%) free.
[3/5] No install step for 'bootloader'
[4/5] Completed 'bootloader'
[4/5] cd /Users/kyson/esp/v5.1.3/esp-idf/components/esptool_py && /usr/local/Cellar/cmake/3.29.2/bin/cmake -D IDF_PATH=/Users/kyson/esp/v5.1.3/esp-idf -D "SERIAL_TOOL=/Users/kyson/.espressif/python_env/idf4.4_py3.9_env/bin/python;;/Users/kyson/esp/v5.1.3/esp-idf/components/esptool_py/esptool/esptool.py;--chip;esp32" -D "SERIAL_TOOL_ARGS=--before=default_reset;--after=hard_reset;write_flash;@flash_args" -D WORKING_DIRECTORY=/Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world/build -P /Users/kyson/esp/v5.1.3/esp-idf/components/esptool_py/run_serial_tool.cmake
esptool.py --chip esp32 -p /dev/tty.usbserial-0001 -b 460800 --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 v4.7.0
Serial port /dev/tty.usbserial-0001
Connecting............
Chip is ESP32-D0WD-V3 (revision v3.1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: d0:ef:76:49:4c:1c
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 0x0003afff...
Flash will be erased from 0x00008000 to 0x00008fff...
Compressed 26704 bytes to 16729...
Writing at 0x00001000... (50 %)
Writing at 0x0000769f... (100 %)
Wrote 26704 bytes (16729 compressed) at 0x00001000 in 0.5 seconds (effective 398.3 kbit/s)...
Hash of data verified.
Compressed 175840 bytes to 97990...
Writing at 0x00010000... (16 %)
Writing at 0x0001c0f2... (33 %)
Writing at 0x00021af6... (50 %)
Writing at 0x0002736b... (66 %)
Writing at 0x0002d617... (83 %)
Writing at 0x0003520a... (100 %)
Wrote 175840 bytes (97990 compressed) at 0x00010000 in 2.5 seconds (effective 560.6 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 103...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (103 compressed) at 0x00008000 in 0.0 seconds (effective 1016.6 kbit/s)...
Hash of data verified.

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

监控

通过命令 idf.py monitor监控。

一个成功的监控日志输出:

Executing action: monitor
Serial port /dev/cu.usbserial-0001
Connecting..............
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting.....
Detecting chip type... ESP32
Running idf_monitor in directory /Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world
Executing "/Users/kyson/.espressif/python_env/idf4.4_py3.9_env/bin/python /Users/kyson/esp/v5.1.3/esp-idf/tools/idf_monitor.py -p /dev/cu.usbserial-0001 -b 115200 --toolchain-prefix xtensa-esp32-elf- --target esp32 --revision 0 /Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world/build/hello_world.elf -m '/Users/kyson/.espressif/python_env/idf4.4_py3.9_env/bin/python' '/Users/kyson/esp/v5.1.3/esp-idf/tools/idf.py'"...
--- esp-idf-monitor 1.4.0 on /dev/cu.usbserial-0001 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jul 29 2019 12:21:46

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:7116
ho 0 tail 12 room 4
load:0x40078000,len:15616
load:0x40080400,len:4
0x40080400: _init at ??:?

load:0x40080404,len:3876
entry 0x4008064c
I (31) boot: ESP-IDF v5.1.3-dirty 2nd stage bootloader
I (31) boot: compile time Apr 21 2024 23:02:32
I (31) boot: Multicore bootloader
I (36) boot: chip revision: v3.1
I (40) boot.esp32: SPI Speed      : 40MHz
I (44) boot.esp32: SPI Mode       : DIO
I (49) boot.esp32: SPI Flash Size : 2MB
I (53) boot: Enabling RNG early entropy source...
I (59) boot: Partition Table:
I (62) boot: ## Label            Usage          Type ST Offset   Length
I (70) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (77) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (85) boot:  2 factory          factory app      00 00 00010000 00100000
I (92) boot: End of partition table
I (96) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=09320h ( 37664) map
I (118) esp_image: segment 1: paddr=00019348 vaddr=3ffb0000 size=02120h (  8480) load
I (122) esp_image: segment 2: paddr=0001b470 vaddr=40080000 size=04ba8h ( 19368) load
I (132) esp_image: segment 3: paddr=00020020 vaddr=400d0020 size=139b8h ( 80312) map
I (162) esp_image: segment 4: paddr=000339e0 vaddr=40084ba8 size=074dch ( 29916) load
I (180) boot: Loaded app from partition at offset 0x10000
I (181) boot: Disabling RNG early entropy source...
I (192) cpu_start: Multicore app
I (192) cpu_start: Pro cpu up.
I (193) cpu_start: Starting app cpu, entry point is 0x400810f4
0x400810f4: call_start_cpu1 at /Users/kyson/esp/v5.1.3/esp-idf/components/esp_system/port/cpu_start.c:159

I (0) cpu_start: App cpu up.
I (210) cpu_start: Pro cpu start user code
I (210) cpu_start: cpu freq: 160000000 Hz
I (210) cpu_start: Application information:
I (215) cpu_start: Project name:     hello_world
I (220) cpu_start: App version:      v5.1.3-dirty
I (226) cpu_start: Compile time:     Apr 21 2024 23:14:10
I (232) cpu_start: ELF file SHA256:  a5d2f62722e4ed81...
I (238) cpu_start: ESP-IDF:          v5.1.3-dirty
I (243) cpu_start: Min chip rev:     v0.0
I (248) cpu_start: Max chip rev:     v3.99
I (253) cpu_start: Chip rev:         v3.1
I (258) heap_init: Initializing. RAM available for dynamic allocation:
I (265) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (271) heap_init: At 3FFB2998 len 0002D668 (181 KiB): DRAM
I (277) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (283) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (290) heap_init: At 4008C084 len 00013F7C (79 KiB): IRAM
I (297) spi_flash: detected chip: generic
I (301) spi_flash: flash io: dio
W (305) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (318) app_start: Starting scheduler on CPU0
I (323) app_start: Starting scheduler on CPU1
I (323) main_task: Started on CPU0
I (333) main_task: Calling app_main()
Hello world!
This is esp32 chip with 2 CPU core(s), WiFi/BTBLE, silicon revision v3.1, 2MB external flash
Minimum free heap size: 301188 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...
Restarting in 1 seconds...
Restarting in 0 seconds...
Restarting now.
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_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:7116
ho 0 tail 12 room 4
load:0x40078000,len:15616
load:0x40080400,len:4
0x40080400: _init at ??:?

load:0x40080404,len:3876
entry 0x4008064c
I (31) boot: ESP-IDF v5.1.3-dirty 2nd stage bootloader
I (31) boot: compile time Apr 21 2024 23:02:32
I (31) boot: Multicore bootloader
I (36) boot: chip revision: v3.1
I (40) boot.esp32: SPI Speed      : 40MHz
I (44) boot.esp32: SPI Mode       : DIO
I (49) boot.esp32: SPI Flash Size : 2MB
I (53) boot: Enabling RNG early entropy source...
I (59) boot: Partition Table:
I (62) boot: ## Label            Usage          Type ST Offset   Length
I (70) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (77) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (85) boot:  2 factory          factory app      00 00 00010000 00100000
I (92) boot: End of partition table
I (96) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=09320h ( 37664) map
I (118) esp_image: segment 1: paddr=00019348 vaddr=3ffb0000 size=02120h (  8480) load
I (122) esp_image: segment 2: paddr=0001b470 vaddr=40080000 size=04ba8h ( 19368) load
I (132) esp_image: segment 3: paddr=00020020 vaddr=400d0020 size=139b8h ( 80312) map
I (162) esp_image: segment 4: paddr=000339e0 vaddr=40084ba8 size=074dch ( 29916) load
I (180) boot: Loaded app from partition at offset 0x10000
I (181) boot: Disabling RNG early entropy source...
I (192) cpu_start: Multicore app
I (192) cpu_start: Pro cpu up.
I (192) cpu_start: Starting app cpu, entry point is 0x400810f4
0x400810f4: call_start_cpu1 at /Users/kyson/esp/v5.1.3/esp-idf/components/esp_system/port/cpu_start.c:159

I (180) cpu_start: App cpu up.
I (210) cpu_start: Pro cpu start user code
I (210) cpu_start: cpu freq: 160000000 Hz
I (211) cpu_start: Application information:
I (215) cpu_start: Project name:     hello_world
I (221) cpu_start: App version:      v5.1.3-dirty
I (226) cpu_start: Compile time:     Apr 21 2024 23:14:10
I (232) cpu_start: ELF file SHA256:  a5d2f62722e4ed81...
I (238) cpu_start: ESP-IDF:          v5.1.3-dirty
I (243) cpu_start: Min chip rev:     v0.0
I (248) cpu_start: Max chip rev:     v3.99
I (253) cpu_start: Chip rev:         v3.1
I (258) heap_init: Initializing. RAM available for dynamic allocation:
I (265) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (271) heap_init: At 3FFB2998 len 0002D668 (181 KiB): DRAM
I (277) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (283) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (290) heap_init: At 4008C084 len 00013F7C (79 KiB): IRAM
I (298) spi_flash: detected chip: generic
I (301) spi_flash: flash io: dio
W (305) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (318) app_start: Starting scheduler on CPU0
I (323) app_start: Starting scheduler on CPU1
I (323) main_task: Started on CPU0
I (333) main_task: Calling app_main()

擦除

idf.py -p /dev/tty.usbserial-0001  erase-flash

一个成功的输出大概如下:


Executing action: erase-flash
Running esptool.py in directory /Users/kyson/esp/v5.1.3/esp-idf/examples/get-started/hello_world/build
Executing "/Users/kyson/.espressif/python_env/idf4.4_py3.9_env/bin/python /Users/kyson/esp/v5.1.3/esp-idf/components/esptool_py/esptool/esptool.py -p /dev/tty.usbserial-0001 -b 460800 --before default_reset --after hard_reset --chip esp32 erase_flash"...
esptool.py v4.7.0
Serial port /dev/tty.usbserial-0001
Connecting............
Chip is ESP32-D0WD-V3 (revision v3.1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: d0:ef:76:49:4c:1c
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Erasing flash (this may take a while)...
Chip erase completed successfully in 2.5s
Hard resetting via RTS pin...
Done

参考

ESP32-DevKitC V4 入门指南

esp monitor