ESP-IDF 3.2.5 + arduino-esp32 1.0.4 + Visual Studio Community 2019

antwdj
Posts: 7
Joined: Thu Jan 07, 2021 1:40 am

ESP-IDF 3.2.5 + arduino-esp32 1.0.4 + Visual Studio Community 2019

Postby antwdj » 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:
  • Ninja binary available only for Windows 64 bit
  • I use Visual Studio Community 2019 Version 16.8.3
Steps:
  1. Download the Windows all-in-one toolchain & MSYS2 from https://dl.espressif.com/dl/esp32_win32 ... 191220.zip
  2. 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.
  3. 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
    
  4. 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):

    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"
    
    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.
  5. 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
    
  6. Open file C:\msys32\home\[username]\esp\components\arduino\libraries\WiFi\src\WiFiSTA.cpp
  7. Go to line 491 (if you use notepad, press CTRL-G, type 491, and click Go To)
  8. Replace these two lines:

    Code: Select all

    ip_addr_t dns_ip = dns_getserver(dns_no);
    return IPAddress(dns_ip.u_addr.ip4.addr);
    
    with

    Code: Select all

    const ip_addr_t * dns_ip = dns_getserver(dns_no);
    return IPAddress(dns_ip->u_addr.ip4.addr);
    
    Save the file
  9. Open file C:\msys32\home\[username]\esp\components\arduino\libraries/WiFi/src/ETH.cpp
  10. Go to line 196
  11. Replace these two lines:

    Code: Select all

    ip_addr_t dns_ip = dns_getserver(dns_no);
    return IPAddress(dns_ip.u_addr.ip4.addr);
    
    with

    Code: Select all

    const ip_addr_t * dns_ip = dns_getserver(dns_no);
    return IPAddress(dns_ip->u_addr.ip4.addr);
    
    Save the file
  12. 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).
  13. 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
    
  14. 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'.
  15. 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.
  16. 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.
  17. Click 'Open the CMake Settings Editor' in the 'CMake Overview Pages' or right click CMakeList.txt in the Solution Explorer and choose 'CMake Settings'
  18. Click 'Edit JSON'
  19. Replace all with this:

    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"
            }
          ]
        }
      ]
    }
    
    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.
    Save the file.
  20. Right click any file in the Solution Explorer and choose Configure Tasks. Replace all with this:

    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'"
          ]
        }
      ]
    }
    
    Save the file.
  21. 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).
  22. Select Component config - mbedTLS - TLS Key Exchange Methods. Press Space on 'Enable pre-shared-key ciphersuites' and 'Enable PSK based ciphersuite'
  23. Save and exit
  24. 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.
  25. Build using the regular Visual Studio build menu (Build - Build All)
  26. 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.
  27. When the monitor is opened, the build menu will be disabled. So close the monitor before build again.
  28. 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.
If your settings does not become effective or you got strange error, try:
  • 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.
The intellisensemode that I use is windows-clang-64.
  • 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.
To build arduino style code with setup() and loop(), see the menuconfig section in 'Installation instruction to use arduino-esp32 as a component' in the reference below.

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)

Who is online

Users browsing this forum: No registered users and 38 guests