VScode+ESP32-IDF的使用

1.简介

Visual Studio Code (简称 VSCode / VSC) 是一款免费开源的现代化轻量级代码编辑器,支持几乎所有主流的开发语言的语法高亮、智能代码补全、自定义热键、括号匹配、代码片段、代码对比 Diff、Git 等特性,支持插件扩展,并针对网页开发和云端应用开发做了优化。软件跨平台支持 Win、Mac 以及 Linux,运行流畅,可谓是微软的良心之作……

话说微软 VSCode 的发展势头简直超猛!!经过多年发展后,目前已成为全球开发者最喜爱也是最热门的主流代码编辑器之一了!在很多年前,大家都没想过有一天能在不用虚拟机的情况下,直接能在 Mac、Linux 上运行使用到微软的 Visual Studio 或像 VSCode 一样的免费却又强大的开发工具吧。VSCode非常好用,乐鑫芯片公司为了让用户使用VScode IDE工具,开发了对应的ESP-IDF插件。

ESP-IDF 是乐鑫官方的物联网开发框架,也是最基础的开发框架,其他的框架,例如音频开发框架 ESP-ADF 也是基于 ESP-IDF 的,所以说 ESP-IDF 框架是学习乐鑫 ESP32 系列芯片开发绕不过去的一道坎。

2.安装VScode和Esp32-IDF插件

2.1 VScode 安装较简单,下载完安装即可。

VScode+ESP32-IDF的使用_第1张图片

2.2 在插件管理界面 搜索esp32 ,安装Espressif IPF

VScode+ESP32-IDF的使用_第2张图片

2.3 选择EXPRESS 安装IDF和IPF TOOLS、 Python环境

VScode+ESP32-IDF的使用_第3张图片

2.4 选择安装的路径和版本,选择国内Espressif git服务器

VScode+ESP32-IDF的使用_第4张图片

2.5 等待安装

VScode+ESP32-IDF的使用_第5张图片

注意:如果下载中断,需要你手动删除你安装目录下的.espressif 和 esp。然后重新安装

报错: EPERM: operation not permitted, rename 'F:\Code\vsCode_env\esp32_IDF\esp-idf-v5.0.1' -> 'F:\Code

EBUSY: resource busy or locked, rename 'F:\Code\vsCode_env\esp32\esp-idf-v5.0.1' -> 'F:\Code\vsCode_env\esp32\esp-idf'

工作区的问题,工作区设置到了F:\Code\vsCode_env\esp32_IDF ,应修改为其他区域。



VScode+ESP32-IDF的使用_第6张图片

VScode+ESP32-IDF的使用_第7张图片

3.测试安装的IPF-TOOL

3.1 创建一个自己的目录,将hello word工程拷贝过去

VScode+ESP32-IDF的使用_第8张图片

VScode+ESP32-IDF的使用_第9张图片

3.2 打开helloword并编译链接

使用的串口

选择目标芯片

Menuconfig

清理工程

编译工程

下载程序的方式

下载

 Executing task: ninja  

[226/416] Performing configure step for 'bootloader'
-- Found Git: G:/Code/esp32/.espressif/tools/idf-git/2.30.1/cmd/git.exe (found version "2.30.1.windows.1")
-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- The ASM compiler identification is GNU
-- Found assembler: G:/Code/esp32/.espressif/tools/xtensa-esp32-elf/esp-2022r1-11.2.0/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc.exe
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: G:/Code/esp32/.espressif/tools/xtensa-esp32-elf/esp-2022r1-11.2.0/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc.exe - 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: G:/Code/esp32/.espressif/tools/xtensa-esp32-elf/esp-2022r1-11.2.0/xtensa-esp32-elf/bin/xtensa-esp32-elf-g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Building ESP-IDF components for target esp32
-- Project sdkconfig file G:/Code/esp32/my/helloword/sdkconfig
-- 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 G:/Code/esp32/esp/esp-idf/components/soc/esp32/ld/esp32.peripherals.ld
-- App "bootloader" version: v5.0.1
-- Adding linker script G:/Code/esp32/esp/esp-idf/components/esp_rom/esp32/ld/esp32.rom.ld
-- Adding linker script G:/Code/esp32/esp/esp-idf/components/esp_rom/esp32/ld/esp32.rom.api.ld
-- Adding linker script G:/Code/esp32/esp/esp-idf/components/esp_rom/esp32/ld/esp32.rom.libgcc.ld
-- Adding linker script G:/Code/esp32/esp/esp-idf/components/esp_rom/esp32/ld/esp32.rom.newlib-funcs.ld
-- Adding linker script G:/Code/esp32/esp/esp-idf/components/bootloader/subproject/main/ld/esp32/bootloader.ld
-- Adding linker script G:/Code/esp32/esp/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: G:/Code/esp32/esp/esp-idf/components/bootloader G:/Code/esp32/esp/esp-idf/components/bootloader_support G:/Code/esp32/esp/esp-idf/components/efuse G:/Code/esp32/esp/esp-idf/components/esp_app_format G:/Code/esp32/esp/esp-idf/components/esp_common G:/Code/esp32/esp/esp-idf/components/esp_hw_support G:/Code/esp32/esp/esp-idf/components/esp_rom G:/Code/esp32/esp/esp-idf/components/esp_system G:/Code/esp32/esp/esp-idf/components/esptool_py G:/Code/esp32/esp/esp-idf/components/freertos G:/Code/esp32/esp/esp-idf/components/hal G:/Code/esp32/esp/esp-idf/components/log G:/Code/esp32/esp/esp-idf/components/bootloader/subproject/main G:/Code/esp32/esp/esp-idf/components/bootloader/subproject/components/micro-ecc G:/Code/esp32/esp/esp-idf/components/newlib G:/Code/esp32/esp/esp-idf/components/partition_table G:/Code/esp32/esp/esp-idf/components/soc G:/Code/esp32/esp/esp-idf/components/spi_flash G:/Code/esp32/esp/esp-idf/components/xtensa
-- Configuring done
-- Generating done
-- Build files have been written to: G:/Code/esp32/my/helloword/build/bootloader
[409/416] Performing build step for 'bootloader'
[1/101] Generating project_elf_src_esp32.c
[2/101] Building C object esp-idf/xtensa/CMakeFiles/__idf_xtensa.dir/eri.c.obj
[3/101] Building C object esp-idf/xtensa/CMakeFiles/__idf_xtensa.dir/xt_trax.c.obj
[4/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/lldesc.c.obj
[5/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/adc_periph.c.obj
[6/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/dport_access_common.c.obj
[7/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/dac_periph.c.obj
[8/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/gpio_periph.c.obj
[9/101] Building C object CMakeFiles/bootloader.elf.dir/project_elf_src_esp32.c.obj
[10/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/dport_access.c.obj
[11/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/interrupts.c.obj
[12/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/i2c_periph.c.obj
[13/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/sdm_periph.c.obj
[14/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/i2s_periph.c.obj
[15/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/lcd_periph.c.obj
[16/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/pcnt_periph.c.obj
[17/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/rmt_periph.c.obj
[18/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/rtc_io_periph.c.obj
[19/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/mcpwm_periph.c.obj
[20/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/ledc_periph.c.obj
[21/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/sdio_slave_periph.c.obj
[22/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/uart_periph.c.obj
[23/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/spi_periph.c.obj
[24/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/sdmmc_periph.c.obj
[25/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/touch_sensor_periph.c.obj
[26/101] Building C object esp-idf/soc/CMakeFiles/__idf_soc.dir/esp32/timer_periph.c.obj
[27/101] Building C object esp-idf/micro-ecc/CMakeFiles/__idf_micro-ecc.dir/uECC_verify_antifault.c.obj
[28/101] Building C object esp-idf/esp_app_format/CMakeFiles/__idf_esp_app_format.dir/esp_app_desc.c.obj
[29/101] Building C object esp-idf/hal/CMakeFiles/__idf_hal.dir/esp32/efuse_hal.c.obj
[30/101] Building C object esp-idf/hal/CMakeFiles/__idf_hal.dir/efuse_hal.c.obj
[31/101] Building C object esp-idf/hal/CMakeFiles/__idf_hal.dir/mpu_hal.c.obj
[32/101] Building C object esp-idf/hal/CMakeFiles/__idf_hal.dir/wdt_hal_iram.c.obj
[33/101] Building C object esp-idf/hal/CMakeFiles/__idf_hal.dir/mmu_hal.c.obj
[34/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/bootloader_random_esp32.c.obj
[35/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/esp_image_format.c.obj
[36/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/secure_boot.c.obj
[37/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/flash_encrypt.c.obj
[38/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/bootloader_utility.c.obj
[39/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/bootloader_common.c.obj
[40/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/bootloader_common_loader.c.obj
[41/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/bootloader_flash/src/bootloader_flash.c.obj
[42/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/bootloader_mem.c.obj
[43/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/bootloader_clock_init.c.obj
[44/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/bootloader_random.c.obj
[45/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/esp32/bootloader_sha.c.obj
[46/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/bootloader_console.c.obj
[47/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/esp32/bootloader_soc.c.obj
[48/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/bootloader_console_loader.c.obj
[49/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/bootloader_panic.c.obj
[50/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/bootloader_flash/src/flash_qio_mode.c.obj
[51/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/bootloader_flash/src/bootloader_flash_config_esp32.c.obj
[52/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/flash_partitions.c.obj
[53/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/bootloader_efuse.c.obj
[54/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/bootloader_init.c.obj
[55/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/bootloader_clock_loader.c.obj
[56/101] Building C object esp-idf/bootloader_support/CMakeFiles/__idf_bootloader_support.dir/src/esp32/bootloader_esp32.c.obj
[57/101] Building C object esp-idf/efuse/CMakeFiles/__idf_efuse.dir/esp32/esp_efuse_table.c.obj
[58/101] Building C object esp-idf/efuse/CMakeFiles/__idf_efuse.dir/src/esp_efuse_utility.c.obj
[59/101] Building C object esp-idf/efuse/CMakeFiles/__idf_efuse.dir/esp32/esp_efuse_utility.c.obj
[60/101] Building C object esp-idf/efuse/CMakeFiles/__idf_efuse.dir/src/esp_efuse_api.c.obj
[61/101] Building C object esp-idf/efuse/CMakeFiles/__idf_efuse.dir/esp32/esp_efuse_fields.c.obj
[62/101] Building C object esp-idf/efuse/CMakeFiles/__idf_efuse.dir/src/esp_efuse_fields.c.obj
[63/101] Building C object esp-idf/efuse/CMakeFiles/__idf_efuse.dir/src/efuse_controller/keys/without_key_purposes/three_key_blocks/esp_efuse_api_key.c.obj
[64/101] Building C object esp-idf/esp_common/CMakeFiles/__idf_esp_common.dir/src/esp_err_to_name.c.obj
[65/101] Building C object esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/esp_memory_utils.c.obj
[66/101] Building C object esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/cpu.c.obj
[67/101] Building C object esp-idf/esp_system/CMakeFiles/__idf_esp_system.dir/esp_err.c.obj
[68/101] Building C object esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/port/esp32/rtc_sleep.c.obj
[69/101] Building C object esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/port/esp32/rtc_pm.c.obj
[70/101] Building C object esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/port/esp32/rtc_clk_init.c.obj
[71/101] Building C object esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/port/esp32/rtc_init.c.obj
[72/101] Building C object esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/port/esp32/rtc_time.c.obj
[73/101] Building C object esp-idf/esp_rom/CMakeFiles/__idf_esp_rom.dir/patches/esp_rom_spiflash.c.obj
[74/101] Building C object esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/port/esp32/rtc_clk.c.obj
[75/101] Building C object esp-idf/esp_hw_support/CMakeFiles/__idf_esp_hw_support.dir/port/esp32/chip_info.c.obj
[76/101] Building C object esp-idf/esp_rom/CMakeFiles/__idf_esp_rom.dir/patches/esp_rom_sys.c.obj
[77/101] Building C object esp-idf/esp_rom/CMakeFiles/__idf_esp_rom.dir/patches/esp_rom_crc.c.obj
[78/101] Building C object esp-idf/main/CMakeFiles/__idf_main.dir/bootloader_start.c.obj
[79/101] Building C object esp-idf/esp_rom/CMakeFiles/__idf_esp_rom.dir/patches/esp_rom_uart.c.obj
[80/101] Building ASM object esp-idf/esp_rom/CMakeFiles/__idf_esp_rom.dir/patches/esp_rom_longjmp.S.obj
[81/101] Building C object esp-idf/esp_rom/CMakeFiles/__idf_esp_rom.dir/patches/esp_rom_regi2c.c.obj
[82/101] Building C object esp-idf/esp_rom/CMakeFiles/__idf_esp_rom.dir/patches/esp_rom_efuse.c.obj
[83/101] Building C object esp-idf/log/CMakeFiles/__idf_log.dir/log_buffers.c.obj
[84/101] Building C object esp-idf/log/CMakeFiles/__idf_log.dir/log_noos.c.obj
[85/101] Building C object esp-idf/log/CMakeFiles/__idf_log.dir/log.c.obj
[86/101] Linking C static library esp-idf\log\liblog.a
[87/101] Linking C static library esp-idf\esp_rom\libesp_rom.a
[88/101] Linking C static library esp-idf\esp_common\libesp_common.a
[89/101] Linking C static library esp-idf\esp_hw_support\libesp_hw_support.a
[90/101] Linking C static library esp-idf\esp_system\libesp_system.a
[91/101] Linking C static library esp-idf\efuse\libefuse.a
[92/101] Linking C static library esp-idf\bootloader_support\libbootloader_support.a
[93/101] Linking C static library esp-idf\esp_app_format\libesp_app_format.a
[94/101] Linking C static library esp-idf\hal\libhal.a
[95/101] Linking C static library esp-idf\micro-ecc\libmicro-ecc.a
[96/101] Linking C static library esp-idf\soc\libsoc.a
[97/101] Linking C static library esp-idf\xtensa\libxtensa.a
[98/101] Linking C static library esp-idf\main\libmain.a
[99/101] Linking C executable bootloader.elf
[100/101] Generating binary image from built executable
esptool.py v4.5.1
Creating esp32 image...
Merged 1 ELF section
Successfully created esp32 image.
Generated G:/Code/esp32/my/helloword/build/bootloader/bootloader.bin
[101/101] cmd.exe /C "cd /D G:\Code\esp32\my\helloword\build\bootloader\esp-idf\esptool_py && G:\Code\esp32\.espressif\python_env\idf5.0_py3.8_env\Scripts\python.exe G:/Code/esp32/esp/esp-idf/components/partition_table/check_sizes.py --offset 0x8000 bootloader 0x1000 G:/Code/esp32/my/helloword/build/bootloader/bootloader.bin"
Bootloader binary size 0x6700 bytes. 0x900 bytes (8%) free.
[415/416] Generating binary image from built executable
esptool.py v4.5.1
Creating esp32 image...
Merged 2 ELF sections
Successfully created esp32 image.
Generated G:/Code/esp32/my/helloword/build/hello_world.bin
[416/416] cmd.exe /C "cd /D G:\Code\esp32\my\helloword\build\esp-idf\esptool_py && G:\Code\esp32\.espr.../helloword/build/partition_table/partition-table.bin G:/Code/esp32/my/helloword/build/hello_world.bin"
hello_world.bin binary size 0x2b570 bytes. Smallest app partition is 0x100000 bytes. 0xd4a90 bytes (83%) free.
 *  Executing task: G:\Code\esp32\.espressif\python_env\idf5.0_py3.8_env\Scripts\python.exe G:\Code\esp32\esp\esp-idf\tools\idf_size.py g:\Code\esp32\my\helloword\build\hello_world.map 

Total sizes:
Used static DRAM:   10092 bytes ( 170644 remain, 5.6% used)
      .data size:    7804 bytes
      .bss  size:    2288 bytes
Used static IRAM:   47122 bytes (  83950 remain, 36.0% used)
      .text size:   46095 bytes
   .vectors size:    1027 bytes
Used Flash size :  122479 bytes
      .text     :   88979 bytes
      .rodata   :   33244 bytes
Total image size:  177405 bytes (.bin may be padded larger)

4.综述

VSCode+ESP32 编译过程,其实是调用了开源的cmake ninja。建议多学习下cmake,ninja ,这对学习操作系统非常有帮助(鸿蒙也是用的ninja+cmake)

出现乱码问题:

  1. 工具的默认编码和程序输出的“汉字”编码不一致导致。

=======以下方法仅供参考,会导致KEIL MDK 中汉字乱码======

Powershell 使用GBK

CMD 使用

VScode+ESP32-IDF的使用_第10张图片

VScode+ESP32-IDF的使用_第11张图片

你可能感兴趣的:(单片机)