Showing posts with label Eclipse. Show all posts
Showing posts with label Eclipse. Show all posts

Monday, July 23, 2018

Espressif ESP32 Tutorial - Programming (Eclipse)

Eclipse (Photon)





The Eclipse IDE is one of the most popular desktop development environments. Being free and open source helps! It also supports most of the common programming languages through Language Server-based plugins. The current release is called Photon.

The C/C++ Development Toolkit (CDT) is a collection of Eclipse-based features that provides the capability to create, edit, navigate, build, and debug projects that use C and/or C++ as a programming language.

The CDT does not include the necessary compilers and debuggers to convert C/C++ code into executable programs and to debug those programs, but it does provide the frameworks that allow such tools to be integrated in a consistent fashion. We will be using the CDT version of Eclipse to program the ESP-32.


Installing the Eclipse IDE





The Eclipse IDE gives you a graphical integrated development environment (IDE) for writing, compiling and debugging ESP-IDF projects. It is quite a bit easier to use than the command line ESP-IDF provided by Espressif. It is also more sophisticated and better suited to larger projects than the Arduino IDE.

As Eclipse doesn't provide an ESP-32 compiler or the ability to flash the DevKit, you will need to have already installed the ESP-IDF tool chain for your operating system. If you haven't done this, refer to our earlier tutorial on the ESP-IDF.

Using ESP-IDF with Eclipse on Windows requires different configuration steps. See the Eclipse IDE on Windows guide if you are using Windows. The following is an overview of the steps for those using a Mac. This content is based on the material from the Espressif ESP-IDF Programming Guide but I have noted any areas where I had difficulty and included screenshots to make some of the explanations clearer.

1. Download the Eclipse Installer for your platform from eclipse.org.

2. Create a working directory, uncompress the download and run the installer app.

mkdir -p ~/eclipse
cd ~/eclipse
tar -xzf ~/Downloads/eclipse-inst-mac64.tar.gz 
open -a 'Eclipse Installer.app'

3. When running the Eclipse Installer, choose “Eclipse for C/C++ Development” (in the documentation you’ll see this referred to as CDT.)

4. When running the installer, you may get the following error.


Note that this is NOT referring to the Java Runtime Environment which you would update via System Preferences.


If you get the JVM error, you need to update the Java Development Kit

After that you should be rewarded with the following installation screen when you run the provided install app.



Setting up Eclipse





After the new Eclipse installation launches, follow these steps:

1. Import a New Project

Eclipse uses the Makefile capability in ESP-IDF. This means you need to start by creating an ESP-IDF project. You can use the skeleton project from github or one of the example projects in the esp-idf examples subdirectory. We will use our old mate Blink again. Refer to our previous tutorial on the ESP-IDF if you don't know how to copy an example project.

Once Eclipse is running, choose File -> Import...

In the dialog that pops up, choose “C/C++” -> “Existing Code as Makefile Project” and click Next.



On the next page, in the “Existing Code Location” field, use the directory of your IDF project. Don’t specify the path to the ESP-IDF directory itself (we will do that in Project Properties). The directory you specify should contain a file named “Makefile” (the project Makefile).



On the same page, under “Toolchain for Indexer Settings” choose “Cross GCC”. Then click Finish.

2. Project Properties



The new project will appear in the left hand Project Explorer column. Right-click the project and choose Properties from the context menu.

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. The value should be the full path where ESP-IDF is installed. If you completed our previous ESP-IDF tutorial, you shouldn't have to do this step as the path will already be updated from the .profile preferences file.



Edit the PATH environment variable. Keep the current value, and append the path to the Xtensa toolchain installed as part of IDF setup, if this is not already listed on the PATH. 

A typical path to the toolchain looks like /home/user-name/esp/xtensa-esp32-elf/bin. Note that you need to add a colon : before the appended path.

On macOS, add a PYTHONPATH environment variable and set it to:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages. 

This is so that the system Python, which has pyserial installed as part of the setup steps, overrides any built-in Eclipse Python. I didn't need to do this on my Mac.

Navigate to “C/C++ General” -> “Preprocessor Include Paths” property page:

Click the “Providers” tab

In the list of providers, click “CDT Cross GCC Built-in Compiler Settings”. Under “Command to get compiler specs”, replace the text ${COMMAND} at the beginning of the line with xtensa-esp32-elf-gcc. This means the full “Command to get compiler specs” should be xtensa-esp32-elf-gcc ${FLAGS} -E -P -v -dD "${INPUTS}".



In the list of providers, click “CDT GCC Build Output Parser” and type xtensa-esp32-elf- at the beginning of the Compiler command pattern. This means the full Compiler command pattern should be xtensa-esp32-elf-(g?cc)|([gc]\+\+)|(clang)



Navigate to “C/C++ General” -> “Indexer” property page:

Uncheck “Allow heuristic resolution of includes”. When this option is enabled Eclipse sometimes fails to find correct header directories.



3. Building in Eclipse

Before your project is first built, Eclipse may show a lot of errors and warnings about undefined values. This is because some source files are automatically generated as part of the esp-idf build process. These errors and warnings will go away after you build the project.

Click OK to close the Properties dialog in Eclipse.

Outside Eclipse, open a command line prompt. Navigate to your project directory, and run make menuconfig to configure your project’s esp-idf settings. This step currently has to be run outside Eclipse.

If you try to build without running a configuration step first, esp-idf will prompt for configuration on the command line - but Eclipse is not able to deal with this, so the build will hang or fail.

Back in Eclipse, choose Project -> Build All to build your project.

TIP: If your project had already been built outside Eclipse, you may need to do a Project -> Clean before choosing Project -> Build. This is so Eclipse can see the compiler arguments for all source files. It uses these to determine the header include paths.

4. Flash from Eclipse

You can integrate the “make flash” target into your Eclipse project to flash using esptool.py from the Eclipse UI:

Right-click your project in Project Explorer (important to make sure you select the project, not a directory in the project, or Eclipse may find the wrong Makefile.)

Select Build Targets -> Create… from the context menu.
Type “flash” as the target name. Leave the other options as their defaults.



Make sure that the ESP-32 is connected and the correct serial port  has been selected using menuconfig then use Project -> Build Target -> Build (Shift+F9) to build the custom flash target, which will compile and flash the project.





Your program will be compiled and flashed to the ESP-32 and the results shown in the CDT build console.

Note that you will need to use “make menuconfig” to set the serial port and other config options for flashing. “make menuconfig” still requires the command line terminal (see the instructions for your platform on the Espressif site or refer to our ESP-IDF tutorial.)



Follow the same steps to add bootloader and partition_table targets, if necessary.

Wednesday, December 23, 2015

Moving to embedXcode...

The Problem


We have been using the Arduino IDE to develop on. Which is fine (and free) but it does have some limitations. In particular, we ran into the problem of having too many tabs (files) in our main controller sketch. This came about as a result of another issue, which is not unrelated.

As with most people we started developing our robot code organically using mostly straight c with c++ functionality reserved for included libraries. By the time you add 13 sensors, speech synthesis, speech recognition, 2 motor controllers, serial comms for the Bluetooth remote (which will be described in a subsequent post) and an I2C bus connecting the real time clock, pressure / altitude module and 2 auxiliary Arduino's used for display and logging, the code gets messy and hard to maintain. A simplified schematic of the robot so far is shown below. The schematic was produced using Fritzing which I have mentioned previously and strongly recommend.



As an aside, we are using SPI for the logging Arduino to communicate with its OLED shield, so we have all the available communication options covered (with the possible exceptions of WiFi and RF - stay tuned)!

To fix the messy, unmaintainable code issue, we started to translate our c code into c++ classes where it made sense. This made the code much more modular but each class requires a new tab which brings us back to the first issue mentioned above. Once the number of tabs fill up the top row of the IDE, it becomes a bit of a pain to move to the tabs not displayed. There is a keyboard shortcut to go to the next and previous tab, but that is a bit clunky and was the straw that sent me on a search for a better IDE to develop Arduino code on.


The other short comings of the Arduino IDE are:
  1. No code completion; and
  2. The syntax highlighting is a bit hit and miss. You could fix a particular issue by editing the keywords.txt file in the appropriate library but that is pretty fragile.

The Solution


A quick google search will turn up LOTS of different options for programming the Arduino. Our preferred solution would incorporate the following:
  1. Minimal learning curve;
  2. Preferably free;
  3. Works on a Mac;
  4. Can upload a sketch from within the IDE;
  5. Includes a serial terminal similar to the serial monitor provided by the Arduino IDE; 
  6. Has code completion and syntax highlighting; and
  7. Handles multiple files well; and
  8. Doesn't create any new issues.
With this shopping list in mind, the field quickly narrows to a plug in for Xcode called embedXcode. This ticks all our requirements and has (almost) no learning curve since we use Xcode for our App Development work.



The other IDE we considered was Eclipse which is the basis of Android Studio that we use for Android Development. What swayed or decision was that the setup looked a lot easier for Xcode and we were more familiar with this IDE as well. Have a look at this blog by a chap in the US, he had the same issues but decided on Eclipse. Going with what you are already familiar with makes a lot of sense.

Setting up embedXcode


This is straight forward and described in detail in the (large) user manual. In summary:
  1. Install Xcode if you don't have it already.
  2. Download embedXcode. We went for the free version to evaluate how good the plug in is. Should it work out we will either upgrade to embedXcode+ or pay a donation. We want to encourage ongoing development of the plugin. We know that Xcode changes frequently and with each new release normally breaks legacy code.
  3. Install the plug in and open up Xcode. That's it! You should now have the option to open a new embedXcode AVR project (see below).

Importing Sketches


Unfortunately you can't just import sketches that you have done with the Arduino IDE. There is a process that you need to follow:
  1. Open a new project in Xcode. You can call it the same name as your sketch. Make sure you select the correct board type (e.g. Uno) in the new project options. The scope options are sketch or library and this also sets the extension of the main file (.ino for sketches and .cpp for libraries).
  2. Open your sketch in the Arduino IDE and copy the contents into the ino file in your Xcode project. There is some pre-processor definitions used for code sensing at the top of the ino template file in Xcode. Don't overwrite these unless you know what you are doing (e.g. if you are using a recent Arduino you can replace the lot with #include "Arduino.h".
  3. The next step is to find your Arduino libraries folder in Finder and drag it across to the Sketchbook group. When the choose options for adding these files dialog comes up, don't select "copy items if needed" (unless you want multiple copies of your library files), select index in "add to targets" (see below). You also need to change the type from plain text to "c++ source". This option is found in the right hand pane of Xcode after selecting the main sketch (shown in the second screenshot below).
  4. The thing I stuffed up initially was not also listing the libraries used in the makefile. If you get an error saying a library can't be found, this may be the answer To fix it, select the makefile in the left hand pane and add the names of the libraries used.  There are two types of libraries you will use most often, application libraries and user defined libraries. Generally, the application libraries are the ones defined using <> (e.g.  #define <SoftwareSerial>) and the user libraries are defined using"" (e.g. #define "blunoAccessory"). However the <TimerOne> library was an exception to this rule and needed to be defined as a user library (e.g. "APP_LIBS_LIST = SoftwareSerial" and "USER_LIBS_LIST = U8glib blunoAccessory TimerOne". Different libraries are separated by a space and don't show the file extension. Read the user manual if you are still confused, it covers this topic in some detail.



Once you have completed the four steps above, you should be able to build (compile) your project, but only if your board is connected.

New Issues?


The only issues which have arisen so far are fairly minor apart from the speed of compilation and uploading. This is significantly slower than the Arduino IDE. If you purchased  embedXcode+ then you have two options:

  1. Build -> All targets which builds and links everything, and is slow.
  2. Make -> Fast -> additional targets only, which builds the main sketch and the libraries in the local folder, so it is much faster (up to 10x according to the developer).

If you only downloaded the free version then you are stuck with option 1. Other areas to be aware of are:
  1. You can't Build (compile) a project unless you are connected to your board via USB. This is a bit of a pain as you could just compile using the Arduino IDE without being connected to the board. Solved this. From the Product menu item select Scheme -> Build.
  2. Instead of Serial Monitor in the Arduino IDE you use Terminal on the Mac. The default baud rate for terminal is 9600. Which is ok if that is the baud rate you want to use on the serial port but I have a sketch which communicates with a BlueTooth LE shield on the same comm port that needs 115200 baud. Theoretically you can change the baud rate in Terminal using stty (e.g. stty 115200), but this doesn't seem to work. I have also tried "screen /dev/tty.usbmodemfd121 115200", but no cigar. I will keep working on this but the work around for now is to use 9600 baud if you can or use the old Arduino Serial Monitor if all else fails. Other solutions are welcome, so feel free to comment below. I have found the answer to this as well. There is an option in the makefile that you can use to set the baud rate, namely:  "SERIAL_BAUDRATE = 115200".
  3. Getting code sense to work for me took a bit of tweaking. First make sure that the sketch (ino file) has index selected for Target Membership (click on this file to check). Save the file and close the project. A small update is required to the user manual instructions. In the current version of Xcode (v7.2), projects is now a separate dialogue to Organiser but it is still found under the Windows menu in Xcode. Delete your derived data for your project and then reopen your project. After indexing, code sense for the Arduino specific definitions should work. You can change the syntax colouring in preferences, fonts and color.

If anything else comes up, I will add it here. All in all I am pretty happy with this solution. We will still use the Arduino IDE for simple sketches but for something as complicated as a robot, an IDE like Xcode is essential. Well done embedXcode!

Update 27th December 2015


I was so impressed with embedXcode that I upgraded to embedXcode+. This is worth doing if you use the plugin regularly. It also encourages the developer to keep supporting the product which is important given the frequent updates to Xcode.

Error: Serial port not available (Step2.mk)




If you come across this error (I did), there is a simple fix. First open up terminal and find out the name of your serial port. Type the following command: ls /dev/tty.usb*


This will provide the name of your serial port. Copy this and then open the main Makefile in Xcode uncomment the line:

#BOARD_PORT = /dev/tty.usbmodem*

and specify either a more general name:  e.g. BOARD_PORT = /dev/tty.usbmodem*

or the specific USB port name of the board that you got from Terminal e.g. in my case:

BOARD_PORT = /dev/tty.usbmodemfd121