ESP-IDF 4.1 + Visual Studio Community 2019
Posted: Sat Jan 09, 2021 6:33 am
Hi, a few days ago I found that Visual Studio 2019 already has support for CMake and Ninja.
This is my installation steps for ESP-IDF 4.1 + Visual Studio Community 2019:
Note:
ESP-IDF 4.1 Get Started guide (https://docs.espressif.com/projects/esp ... index.html)
Using MinGW and Cygwin with Visual C++ and Open Folder (https://devblogs.microsoft.com/cppblog/ ... en-folder/)
Open Folder support for C++ build systems in Visual Studio (https://docs.microsoft.com/en-us/cpp/bu ... w=msvc-160)
Customize CMake build settings (https://docs.microsoft.com/en-us/cpp/bu ... w=msvc-160)
CMakeSettings.json schema reference (https://docs.microsoft.com/en-us/cpp/bu ... w=msvc-160)
This is my installation steps for ESP-IDF 4.1 + Visual Studio Community 2019:
Note:
- Ninja binary available only for Windows 64 bit
- I use Visual Studio Community 2019 Version 16.8.3
- Download the ESP-IDF Tools installer from https://dl.espressif.com/dl/esp-idf-tools-setup-2.3.exe
- Run the installer. I use the default installation folder. My computer did not have Python and Git, so the installer also download and run installers for Python and Git.
- Download Visual Studio Community 2019 Installer from Microsoft website. Open the installer. Check 'Desktop development with C++' and 'Linux development with C++' and make sure 'C++ CMake tools for Linux' and 'JSON Editor' are checked. If you want to use Visual Studio only for ESP-IDF and want to minimize download, you can unchecked everything in the Installation details except for 'C++ CMake tools for Linux' and 'JSON Editor'.
- Create a new project by copying the hello_world example folder (C:\Users\[your username here]\Desktop\esp-idf\examples\get-started\hello_world) to the place you want (the full directory path should not contain any spaces). I put mine in M:\VisualStudio so the full path is M:\VisualStudio\hello_world (adjust to your project's path).
- Right click your project folder in Windows Explorer and select 'Open with Visual Studio' or you can open Visual Studio 2019 from Start menu, choose File - Open - Folder or File - Open - CMake, and select your project folder. Wait until 'CMake Overview Pages' is shown.
- Click 'Open the CMake Settings Editor' in the 'CMake Overview Pages' or right click CMakeList.txt in the Solution Explorer and choose 'CMake Settings'
- Click 'Edit JSON'
- Replace all with this:
Replace [username] in the IDF_PATH, IDF_TOOLS_PATH, and IDF_PYTHON_DIR line with your username. Change the value for FLASH_COM_PORT to the serial port of your ESP32. Save the file.
Code: Select all
{ "configurations": [ { "name": "ESP-IDF_4_1-Debug", "generator": "Ninja", "configurationType": "Debug", "inheritEnvironments": [ "linux_x64" ], "buildRoot": "${projectDir}\\build", "installRoot": "${projectDir}\\install", "cmakeCommandArgs": "--warn-uninitialized", "buildCommandArgs": "", "ctestCommandArgs": "", "environments": [ { "IDF_GIT_DIR": "C:\\Program Files\\Git\\cmd", "IDF_PATH": "C:\\Users\\[username]\\Desktop\\esp-idf", "IDF_TOOLS_PATH": "C:\\Users\\[username]\\.espressif", "IDF_PYTHON_DIR": "C:\\Users\\[username]\\AppData\\Local\\Programs\\Python\\Python37", "IDF_PYTHON_ENV_PATH": "${env.IDF_TOOLS_PATH}\\python_env\\idf4.1_py3.7_env", "OPENOCD_SCRIPTS": "${env.IDF_TOOLS_PATH}\\tools\\openocd-esp32\\v0.10.0-esp32-20191114\\openocd-esp32\\share\\openocd\\scripts", "PATH": "${env.IDF_TOOLS_PATH}\\tools\\xtensa-esp32-elf\\esp-2020r2-8.2.0\\xtensa-esp32-elf\\bin;${env.IDF_TOOLS_PATH}\\tools\\xtensa-esp32s2-elf\\esp-2020r2-8.2.0\\xtensa-esp32s2-elf\\bin;${env.IDF_TOOLS_PATH}\\tools\\esp32ulp-elf\\2.28.51-esp-20191205\\esp32ulp-elf-binutils\\bin;${env.IDF_TOOLS_PATH}\\tools\\esp32s2ulp-elf\\2.28.51-esp-20191205\\esp32s2ulp-elf-binutils\\bin;${env.IDF_TOOLS_PATH}\\tools\\ninja\\1.9.0\\;${env.IDF_TOOLS_PATH}\\tools\\idf-exe\\1.0.1\\;${env.IDF_TOOLS_PATH}\\tools\\ccache\\3.7\\;${env.IDF_TOOLS_PATH}\\python_env\\idf4.1_py3.7_env\\Scripts;${env.IDF_PATH}\\tools;${env.IDF_PATH};${env.PATH}", "INCLUDE": "${env.IDF_TOOLS_PATH}\\tools\\xtensa-esp32-elf\\esp-2020r2-8.2.0\\xtensa-esp32-elf\\xtensa-esp32-elf\\include;${env.IDF_TOOLS_PATH}\\tools\\xtensa-esp32s2-elf\\esp-2020r2-8.2.0\\xtensa-esp32s2-elf\\xtensa-esp32s2-elf\\include;${env.IDF_PATH}\\components;${env.INCLUDE}", "FLASH_COM_PORT": "COM4", "environment": "linux_x64" } ], "intelliSenseMode": "windows-clang-x64", "variables": [ { "name": "IDF_TARGET", "value": "esp32", "type": "STRING" }, { "name": "CMAKE_MAKE_PROGRAM", "value": "${env.IDF_TOOLS_PATH}\\tools\\ninja\\1.9.0\\ninja.exe", "type": "FILEPATH" }, { "name": "CCACHE_ENABLE", "value": "1", "type": "STRING" }, { "name": "ESP_PLATFORM", "value": "1", "type": "STRING" } ] } ] }
- Right click any file in the Solution Explorer and choose Configure Tasks. Replace all with this:
Save the file.
Code: Select all
{ "version": "0.2.1", "tasks": [ { "taskLabel": "menuconfig", "appliesTo": "*", "type": "launch", "inheritEnvironments": [ "linux_x64" ], "command": "idf_py_menuconfig.cmd" }, { "taskLabel": "flash", "appliesTo": "*", "type": "launch", "inheritEnvironments": [ "linux_x64" ], "command": "idf.py", "args": [ "-p ${env.FLASH_COM_PORT} flash" ] }, { "taskLabel": "flash monitor", "appliesTo": "*", "type": "launch", "inheritEnvironments": [ "linux_x64" ], "command": "idf_py_flash_monitor.cmd", "args": [ "${env.FLASH_COM_PORT}" ] }, { "taskLabel": "monitor", "appliesTo": "*", "type": "launch", "inheritEnvironments": [ "linux_x64" ], "command": "idf_py_monitor.cmd" } ] }
- Create a text file named idf_py_menuconfig.cmd in C:\Users\[username]\Desktop\esp-idf\tools. Open with a text editor and type:
Save the file
Code: Select all
start idf.py menuconfig
- Create a text file named idf_py_monitor.cmd in C:\Users\[username]\Desktop\esp-idf\tools. Open with a text editor and type:
Save the file
Code: Select all
start idf.py monitor
- Create a text file named idf_py_flash_monitor.cmd in C:\Users\[username]\Desktop\esp-idf\tools. Open with a text editor and type:
Save the file
Code: Select all
start idf.py -p %1 flash monitor
- In Visual Studio, right click any file in the Solution Explorer and select 'Run menuconfig'. Change the settings you want.
- Save and exit
- Select Project - 'Generate Cache for [your project folder name]' from the Visual Studio menu to regenerate CMake cache. I suggest you always regenerate the CMake cache when you are done with menuconfig.
- Build using the regular Visual Studio build menu (Build - Build All)
- To flash the firmware to ESP32, right click any file in the Solution Explorer and choose 'Run flash'. Choose 'Run monitor' to open monitor. Choose 'Run flash monitor' to flash and open monitor.
- When the monitor is opened, the build menu will be disabled. So close the monitor before build again.
- In the CMakeSetting.json, I set the build folder not in the 'out' folder. If you follow this setting, you can delete the 'out' folder if it was created by Visual Studio.
- Delete and generate CMake cache again. Sometimes the CMake cache is not updating. Just be patient with this.
- Or if you want to force it, close Visual Studio, then delete the build folder and everything in the .vs folder except tasks.vs.json.
- If you open C file, the Intellisense does not show undefined method or variable.
- If you open Cpp file, the Intellisense show undefined method or variable.
ESP-IDF 4.1 Get Started guide (https://docs.espressif.com/projects/esp ... index.html)
Using MinGW and Cygwin with Visual C++ and Open Folder (https://devblogs.microsoft.com/cppblog/ ... en-folder/)
Open Folder support for C++ build systems in Visual Studio (https://docs.microsoft.com/en-us/cpp/bu ... w=msvc-160)
Customize CMake build settings (https://docs.microsoft.com/en-us/cpp/bu ... w=msvc-160)
CMakeSettings.json schema reference (https://docs.microsoft.com/en-us/cpp/bu ... w=msvc-160)