ESP-IDF 3.2.5 + arduino-esp32 1.0.4 + Visual Studio Community 2019
Posted: Sat Jan 09, 2021 6:38 am
Hi, a few days ago I found that Visual Studio 2019 already has support for CMake and Ninja.
For those who want to use arduino-esp32 library, this is my installation steps for ESP-IDF 3.2.5 + arduino-esp32 1.0.4 + Visual Studio Community 2019:
Note:
Reference:
ESP-IDF 3.2.5 Get Started guide (https://docs.espressif.com/projects/esp ... index.html)
Installation instruction to use arduino-esp32 as a component (https://github.com/espressif/arduino-es ... mponent.md)
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)
For those who want to use arduino-esp32 library, this is my installation steps for ESP-IDF 3.2.5 + arduino-esp32 1.0.4 + 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 Windows all-in-one toolchain & MSYS2 from https://dl.espressif.com/dl/esp32_win32 ... 191220.zip
- Extract the file. I will use C drive here. You can extract in other place but the full directory path should not contain any spaces.
- Run C:\msys32\mingw32.exe and type:
Code: Select all
mkdir -p esp cd esp git clone -b v3.2.5 --recursive https://github.com/espressif/esp-idf.git touch /etc/profile.d/export_idf_path.sh exit
- Open C:\msys32\etc\profile.d\export_idf_path.sh with Notepad or other text editor and type (do not delete the dot on the second line before $IDF_PATH):
Replace [username] with your mingw home directory name (see the folder name under C:\msys32\home). Do it also wherever you see [username] in the next steps. Save the file.
Code: Select all
export IDF_PATH="/c/msys32/home/[username]/esp/esp-idf" . $IDF_PATH/add_path.sh export PATH=$PATH:"/c/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin"
- Now we are going to download arduino-esp32 library version 1.0.4. Run C:\msys32\mingw32.exe and type:
Code: Select all
cd /home/[username]/esp mkdir -p components cd components git clone https://github.com/espressif/arduino-esp32.git arduino cd arduino git submodule update --init --recursive git checkout 4638628873a061c36faffebe4d146d13f960076d git reset --hard exit
- Open file C:\msys32\home\[username]\esp\components\arduino\libraries\WiFi\src\WiFiSTA.cpp
- Go to line 491 (if you use notepad, press CTRL-G, type 491, and click Go To)
- Replace these two lines:
with
Code: Select all
ip_addr_t dns_ip = dns_getserver(dns_no); return IPAddress(dns_ip.u_addr.ip4.addr);
Save the fileCode: Select all
const ip_addr_t * dns_ip = dns_getserver(dns_no); return IPAddress(dns_ip->u_addr.ip4.addr);
- Open file C:\msys32\home\[username]\esp\components\arduino\libraries/WiFi/src/ETH.cpp
- Go to line 196
- Replace these two lines:
with
Code: Select all
ip_addr_t dns_ip = dns_getserver(dns_no); return IPAddress(dns_ip.u_addr.ip4.addr);
Save the fileCode: Select all
const ip_addr_t * dns_ip = dns_getserver(dns_no); return IPAddress(dns_ip->u_addr.ip4.addr);
- Create a new project by copying the hello_world example folder (C:\msys32\home\esp\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).
- We are going to make a symbolic link to the arduino-esp32 component folder to save space. Open command prompt with Run as Administrator and type:
Code: Select all
cd M:\VisualStudio\hello_world mkdir components mklink /J components\arduino C:\msys32\home\[username]\esp\components\arduino
- 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'.
- Copy ninja.exe from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja to C:/msys32/usr/bin. We need to do this to avoid build errors because the Visual Studio path contain spaces. This will also enable us to use ninja.exe from mingw32.
- 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:
Don't forget to replace [username] in the IDF_PATH line. Change the value for FLASH_COM_PORT to the serial port of your ESP32.
Code: Select all
{ "configurations": [ { "name": "ESP-IDF_3_2_5-Debug", "generator": "Ninja", "configurationType": "Debug", "inheritEnvironments": [ "linux_x64" ], "buildRoot": "${projectDir}\\build", "installRoot": "${projectDir}\\install", "cmakeCommandArgs": "--warn-uninitialized", "buildCommandArgs": "", "ctestCommandArgs": "", "environments": [ { "MSYS32_ROOT": "C:\\msys32", "IDF_PATH": "${env.MSYS32_ROOT}\\home\\[username]\\esp\\esp-idf", "PATH": "${env.MSYS32_ROOT}\\mingw32\\bin;${env.MSYS32_ROOT}\\usr\\bin;${env.MSYS32_ROOT}\\opt\\xtensa-esp32-elf\\bin;${env.IDF_PATH};${env.PATH}", "INCLUDE": "${env.MSYS32_ROOT}\\ming32\\include;${env.MSYS32_ROOT}\\usr\\include;${env.MSYS32_ROOT}\\usr\\lib\\gcc\\i686-pc-msys\\7.4.0\\include;${env.IDF_PATH}\\components;${env.INCLUDE}", "FLASH_COM_PORT": "COM4", "environment": "linux_x64" } ], "intelliSenseMode": "windows-clang-x64", "variables": [ { "name": "CMAKE_MAKE_PROGRAM", "value": "${env.MSYS32_ROOT}\\usr\\bin\\ninja.exe", "type": "FILEPATH" } ] } ] }
Save the file. - 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": "${env.MSYS32_ROOT}\\msys2_shell.cmd", "args": [ "-mingw32", "-where ${workspaceRoot}", "-shell bash", "--login", "-c", "'idf.py menuconfig'" ] }, { "taskLabel": "flash", "appliesTo": "*", "type": "launch", "inheritEnvironments": [ "linux_x64" ], "command": "${env.MSYS32_ROOT}\\msys2_shell.cmd", "args": [ "-mingw32", "-where ${workspaceRoot}", "-shell bash", "--login", "-c", "'idf.py -p ${env.FLASH_COM_PORT} flash'" ] }, { "taskLabel": "flash monitor", "appliesTo": "*", "type": "launch", "inheritEnvironments": [ "linux_x64" ], "command": "${env.MSYS32_ROOT}\\msys2_shell.cmd", "args": [ "-mingw32", "-where ${workspaceRoot}", "-shell bash", "--login", "-c", "'idf.py -p ${env.FLASH_COM_PORT} flash monitor'" ] }, { "taskLabel": "monitor", "appliesTo": "*", "type": "launch", "inheritEnvironments": [ "linux_x64" ], "command": "${env.MSYS32_ROOT}\\msys2_shell.cmd", "args": [ "-mingw32", "-where ${workspaceRoot}", "-shell bash", "--login", "-c", "'idf.py monitor'" ] } ] }
- Right click any file in the Solution Explorer and select 'Run menuconfig'. The menu is messed up but still usable. Press the UP arrow button on your keyboard to see the first option (the text was messed up).
- Select Component config - mbedTLS - TLS Key Exchange Methods. Press Space on 'Enable pre-shared-key ciphersuites' and 'Enable PSK based ciphersuite'
- 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.
Reference:
ESP-IDF 3.2.5 Get Started guide (https://docs.espressif.com/projects/esp ... index.html)
Installation instruction to use arduino-esp32 as a component (https://github.com/espressif/arduino-es ... mponent.md)
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)