Introduction: ESP32 With Eclipse IDE

Picture of ESP32 With Eclipse IDE

Hello world! In this instructable, we will be looking at how we could develop applications for the ESP32 devkit using the Eclipse IDE (the latest version is Eclipse Oxygen as of December 2017), and the supporting toolchains from Espressif - the makers of ESP32. We will be installing the toolchains, the ESP-IDF from Espressif and finally Eclipse itself. We would then go through some tweaks that would enable us to build and flash applications from Eclipse without the need for external commands. Let's get started.

The ESP32 integrates Wi-Fi and Bluetooth on a single chip, along with dual high-performance cores, ultra-low power co-processor and several peripherals that make it a very robust chip as compared to the ESP8266. You can develop applications for the ESP32 using the Arduino IDE or the Eclipse IDE. This instructable is concerned with the later. I am on a Windows host and some of the steps might be different if you are on Linux or Mac operating system. To get started, let's install the necessary toolchains.

Step 1: Dowload and Install the Toolchain

Picture of Dowload and Install the Toolchain

Click on this link to download the toolchain for Windows. After you have downloaded the zip file, unzip the file to C:\ and it will create a msys32 directory with a pre-prepared environment. Make sure the folder you have extracted is msys32 and not the unzipped zip file (which contains the msys32 folder). If you are on Windows and clicked on "Extract All..." after right-clicking, open up the folder and you will find the msys32 folder. Move this folder to the C:\ directory.

Now check if you have successfully downloaded and installed the toolchain by running C:\msys32\mingw32.exe. This opens an MSYS2 MINGW32 terminal window. The environment in this window is a bash shell that we can use to issue commands. Now we need to get ESP-IDF from GitHub.

Step 2: Get ESP-IDF

Picture of Get ESP-IDF

Now that we have installed the toolchain (that contains programs to compile and build the application), we also need ESP32 specific API/libraries. They are provided by Espressif in the ESP-IDF GitHub repository. To get it, open the MSYS2 MINGW32 terminal by running C:\msys32\mingw32.exe. Then create a folder named esp by typing the following command.

mkdir esp

Then go into the esp directory and clone the ESP-IDF repository,

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

ESP-IDF will be downloaded into ~/esp/esp-idf after you have issued this command. Now, we need to setup path to ESP-IDF so that our toolchain programs can access it.

Step 3: Setup Path to ESP-IDF

The toolchain programs that we downloaded and installed access ESP-IDF using the IDF_PATH environment variable. This variable should be set up on your PC, otherwise, projects will not build. The setting may be done manually, each time PC is rebooted or permanently by defining IDF_PATH in the user profile. The user profile scripts are contained in C:/msys32/etc/profile.d/ folder. They are executed every time you open an MSYS2 window. We first create a new script file in the C:/msys32/etc/profile.d/ directory and name it export_idf_path.sh. To do this, open the MSYS2 MINGW32 terminal by running C:\msys32\mingw32.exe and issue the following commands.

cd esp
cd esp-idf
cd /etc/profile.d/

To create a file and edit it, we use Vim.

vim export_idf_path.sh

Once inside Vim in the terminal, type "i" (without the quotation marks) to enter WRITE/INSERT mode. Now insert the path to ESP-IDF to the IDF_PATH environment variable by typing the following in the terminal text editor.

export IDF_PATH="C:/msys32/home/{your-user-name}/esp/esp-idf"

replace {your-user-name} with your username. To save and exit the in-terminal editor, press the ESC (escape) key and type ":wq" (without the quotation marks). Now close and reopen the MSYS2 terminal and issue the following command.

printenv IDF_PATH

Issuing the command prints out C:/msys32/home/{your-user-name}/esp/esp-idf on the terminal.

Step 4: Establish Serial Connection With the ESP32

Picture of Establish Serial Connection With the ESP32

To establish a serial connection with the ESP, connect the ESP32 devkit to your computer using a USB to micro-USB cable. Then open device manager by right-clicking the start button on the Windows taskbar. Scroll down and search for the label named Ports. You should see the ESP connected to your host. Note down the port the ESP is connected to. If you have a hard time figuring out which port the ESP is connected to, you may unplug devices that are connected to your host 's USB port and note the last one standing, that should be your ESP.

Now open PuTTY, and click on Serial, which is under the Connection label. Type in the port the ESP is connected to and change the baud rate to 115200. Now click on the Session label (first label from the top) and change the connection type to Serial. Hit Open and you should see log display by the ESP32. The log contents depend on application loaded to ESP32. As long as you see anything legible on the log display you should be good to go.

Step 5: Install Eclipse

Picture of Install Eclipse

Download Eclipse and install it. While the Eclipse installer is running, choose the "Eclipse IDE for C/C++ Developers" option and proceed.

Step 6: Create a Workspace and Import a Project

Picture of Create a Workspace and Import a Project

All projects that are developed for ESP32 using Eclipse need to follow the ESP-IDF template. Before we can use Eclipse, open up the MSYS2 terminal and type the following commands.

cd "C:\Users\{your-user-name}\Desktop"
mkdir workspace
cd workspace
git clone https://github.com/espressif/esp-idf-template.git HelloWorld

Now, we need to configure the SDK settings using the following command.

cd HelloWorld
make menuconfig

You will see a menu where you can configure your SDK settings. Select Serial flasher config and then select the Default serial port label. Then enter the name of the port your ESP is connected to. Mine is COM5. Save and exit from the menu.

Step 7: Setting Up Eclipse

Picture of Setting Up Eclipse

Now launch Eclipse. Select the workspace directory that we created earlier as our Eclipse workspace. Then choose File -> Import. In the dialog that pops-up, choose “C/C++” -> “Existing Code as Makefile Project” and click Next. On the next page, enter “Existing Code Location” to be the directory of your project that you cloned from GitHub. The project name should be the same as the directory name of the template that we cloned. On the same page, under “Toolchain for Indexer Settings” uncheck “Show only available toolchains that support this platform”. Then in the extended list that appears, choose “Cygwin GCC”. Then click Finish. You might get warnings but it is completely normal.

The new project will appear under Project Explorer. Right-click the project and choose Properties from the context menu. Click on the “C/C++ Build” properties page (top-level option), uncheck “Use default build command” and enter the following custom build command:

python ${IDF_PATH}/tools/windows/eclipse_make.py

Click on the “Environment” properties page under “C/C++ Build”: Click “Add” and enter, name: BATCH_BUILD and value: 1. Click “Add” again, and enter, name: IDF_PATH. To obtain the value of IDF_PATH, open the MSYS2 terminal and enter the following command:

echo $IDF_PATH

Copy the path and paste it into the value field. Edit the PATH environment variable. Delete the existing value and replace it with,

C:\msys32\usr\bin;C:\msys32\mingw32\bin;C:\msys32\opt\xtensa-esp32-elf\bin

Now click Apply. Click on “C/C++ General” -> “Preprocessor Include Paths, Macros, etc.” property page. Click the “Providers” tab, then in the list of providers, click “CDT GCC Built-in Compiler Settings Cygwin”. Under “Command to get compiler specs”, replace the text with the following,

xtensa-esp32-elf-gcc ${FLAGS} -E -P -v -dD "${INPUTS}"

In the list of providers, click “CDT GCC Build Output Parser” and replace the text in compiler command pattern with the following,

xtensa-esp32-elf-((g?cc)|([gc]\+\+)|(clang))

Now the Eclipse IDE is all set and we can now code and build from the IDE.

Step 8: Building and Flashing From Eclipse

Picture of Building and Flashing From Eclipse

Before we build and flash our program from eclipse, copy and paste this "Hello World!" code into the main.c file.

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"

void app_main()
{
    printf("Hello world!\n");
    /* Print chip information */
    esp_chip_info_t chip_info;
    esp_chip_info(&chip_info);
    printf("This is ESP32 chip with %d CPU cores, WiFi%s%s, ",
            chip_info.cores,
            (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
            (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
    printf("silicon revision %d, ", chip_info.revision);
    printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
            (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
    for (int i = 10; i >= 0; i--) {
        printf("Restarting in %d seconds...\n", i);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
    printf("Restarting now.\n");
    fflush(stdout);
    esp_restart();
}

Hit Ctrl + S to save. Now right-click the project in the project explorer and click on "Build Targets" -> "Create". In the pop-up window type "flash" as the Target name and click ok. Now click on "Build Targets" in the project explorer and right-click "flash". Select "Build Target".

Hold the RESET button first followed by the BOOT button. No release the RESET button followed by the BOOT button.

Step 9: Results

Picture of Results

Now to view the results, open the MSYS2 terminal and issue the following commands.

cd "C:/Users/{user-name}/Desktop"
cd workspace
cd HelloWorld
make monitor

And *boom* you should see the output as shown in the image. Good luck building something awesome!

Comments

About This Instructable

949views

28favorites

License:

Bio: I work on electronics and machine learning
More by nikhilraghava:NBA Game AlerterESP32 With Eclipse IDEGitHub Build Status Server
Add instructable to: