Programming your favourite Arduino via Eclipse — Part 2

Akalanka De Silva
7 min readJan 9, 2022

--

This is the continuation of my previous article https://medium.com/@akalankadesilva/programming-your-favourite-arduino-via-eclipse-part-1-2ac99b1220f0 where I described the basic steps of the build process and how the firmware gets loaded to the microcontroller. Now it is time to put that knowledge to test!

To make things clear, the objective of this article is NOT to show an easy method to program our micro. It is to:

  • Gain a practical understanding of how the build process (compiling + linking) and programming works
  • Ability to use your favourite Arduino libraries to program your SAMD21 micro using a proper IDE such as Eclipse

This will be helpful, say, if you decide to ditch Microchip and go with ST’s STM32 series since STM32Cube IDE is basically Eclipse in a suit. Same goes for TI’s products since TI’s Code Composer Studio is also a built on Eclipse. Remeber these IDEs from chip vendors have very powerful features which will drastically reduce your development time in a professional development environment.

Install Eclipse for C/C++ and GNUWin32 Make

First and foremost we will need to download eclipse for C/C++from here https://www.eclipse.org/downloads/packages/release/kepler/sr2/eclipse-ide-cc-developers. I am using the Eclipse IDE 2021‑12 version for this demo.

Next is to install GNUWin32 Make https://ixpeering.dl.sourceforge.net/project/gnuwin32/make/3.81/make-3.81.exe if you are building on Windows. If you are building on Linux you can just install make from your repo manager yum or apt. Note down this installation folder of the make.exe (for Windows) as we will need it later.

Install Arduino and SAMD boards

If you remember from my previous article, the toolchain consists of the compiler, linker and other utilities needed to build your project. Since Eclipse doesn’t come with an inbuilt toolchain for our micro, we need to get the two acquainted. Having just the build tools is not sufficient. We need the following items as well:

  • Bunch of header files for the ARM core
  • Headers and source files for the Arduino abstraction layer
  • Board definition and other related files for the Arduino SAMD21 board

Easiest way to find them is to install the Arduino IDE and pinch them from there. Therefore follow the next steps to install Arduino and the board definitions.

  • Install Arduino IDE https://www.arduino.cc/en/software
  • Go to Tools > Board > Board Manager and install the following: “Arduino SAMD Boards(32-bits ARM Cortex-M0+)” and “Adafruit SAMD Boards”. Note I am installing the Adafruit SAMD Boards because I am using an Adafruit Feather M0 for this demo.

Identifying the build tools and sources

At this point we are ready to pinch the toolchain, sources and libraries from the Arduino installation. Take note of the below very carefully as you will need them in the next steps. (Just take note, we will add them to our project later)

  • The toolchain and other header files needed for the ARM core can be found at somewhere like

C:\Users\<username>\AppData\Local\Arduino15\packages\arduino\tools\arm-none-eabi-gcc\<version>

In this folder, the following will be important to us:

bin → This is where the build tools such as the compiler and linker will sit

arm-none-eabi/include → this is where the header files for the ARM core will sit

  • The vendor independent hardware abstraction layer for ARM processors (a.k.a CMSIS) - Yeah I know it sounds gibberish but if you remember from my previous article ARM licenses its processor cores to multiple vendors like Microchip/Atmel, ST, TI, NXP etc… Think of this as a common API layer that is visible to us regardless of the vendor. This can be found somewhere like

C:\Users\<username>\AppData\Local\Arduino15\packages\arduino\tools\CMSIS\<version>

and

C:\Users\<username>\AppData\Local\Arduino15\packages\arduino\tools\CMSIS-Atmel\<version>\CMSIS\Device\ATMEL

  • Arduino SAMD abstraction layer- This is where the magic happens where your digitalWrites, analogReads and Serial.printlns are defined. Its this ability to use the same exact functions on micros ranging from AVR 8-bits cores to ARM Cortex M4 cores which makes Arduino shine. This will be located somewhere like

C:\Users\<username>\AppData\Local\Arduino15\packages\adafruit\hardware\samd\<version>\cores\arduino

Note this contains the header files and source files in the same folder. Usual practice is to have the header files in an include folder and the source files in the root folder or a src folder. Really don’t know why this folder is not organized in such a way.

  • Adafruit Feather board definitions and related configs- This includes the pin definitions, linker scripts etc. and can be found in

C:\Users\<username>\AppData\Local\Arduino15\packages\adafruit\hardware\samd\<version>\variants\feather_m0

Creating a Hello World project in Eclipse

Now we have a clear view of what we are going for, lets create our first project in Eclipse!

  • Go to File > New > C/C++ Project

Select “C++ Managed Build”

Next >

Next >

Click Finish

Setting up our HelloWorld project

Now we need to add the toolchain and other resources to our project. In Project Explorer you will be able to see the HelloWorld project as shown below. Right click and go to Properties

First we will setup the compiler path pointing to the toolchain. We will set a environment variable so that our path names are not 3 feet long.

Now we set the compiler Prefix and path

Next we need to set the include paths for the GCC and G++ compilers. Remember we have a bunch of include files from the ARM core, CMSIS abstraction layer and Arduino. Add them as shown below:

Next we need to set the compiler flags. This is very interesting since, the GCC compiler has a large number of flags and switches. Add the following under “Miscellaneous” for the GCC and G++ compilers

-c -fmessage-length=0 -mcpu=cortex-m0plus -mthumb   -w -std=gnu++11 -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD -DF_CPU=48000000L -DARDUINO=10810 -DARDUINO_SAMD_ZERO -DARDUINO_ARCH_SAMD -DARDUINO_SAMD_ZERO -DARM_MATH_CM0PLUS -DADAFRUIT_FEATHER_M0 -D__SAMD21G18A__ -DUSB_VID=0x239A -DUSB_PID=0x800B -DUSBCON "-DUSB_MANUFACTURER=\"Adafruit\"" "-DUSB_PRODUCT=\"Feather M0\""

Now we will add the flags to the linker. Go to “Miscellaneous” under “Cross G++ Linker” and add the following:

--specs=nano.specs --specs=nosys.specs -mcpu=cortex-m0plus -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -TC:\Users\akala\AppData\Local\Arduino15\packages\adafruit\hardware\samd\1.7.2\variants\feather_m0\linker_scripts\gcc\flash_with_bootloader.ld

Important: Set the actual path to the feather_m0 folder above.

Last but not least we have to add the source files for the Arduino libraries and feather_m0 resources as Linked Resources as shown below:

Phew! At this point we have done all the configurations that are needed to compile our project.

Writing our Hello World code

Create a source file in the project (Right click > New > Source) called main.cpp and enter the following simple code.

#include "Arduino.h"#include "wiring_constants.h"#include "Uart.h"void setup(){   Serial.begin(115200);   pinMode(11,OUTPUT);}void loop(){   digitalWrite(11,HIGH);   delay(100);   digitalWrite(11,LOW);   delay(1000);}

Right click the project and click ‘Build Project’ while standing on one foot and worshipping the Eclipse gods and you will see the following message in the console!

If you look at the project folder in Project Explorer in the Debug folder you will see the HelloWorld ELF file as shown below.

If you remember the explanation I had on Part 1 of this article, we need a .bin file to program the flash memory of the microcontroller. Therefore, we need to configure Eclipse to automatically generate the .bin file for us. Fortunately this is pretty simple.

Right click the project > Properties > C/C++ Build > Settings > Build Steps tab as shown below and set the Post Build step to instruct Eclipse to generate the bin file after building the project.

${CROSS_TOOL_PATH}\arm-none-eabi-objcopy -O binary ${ProjName} ${ProjName}.bin

If you build the project now, you will see that the HelloWorld.bin file is also generated alongside the ELF file.

I will explain in Part 3 on how we can use this .bin file and program our SAMD21 microcontroller from Eclipse. Till then ciao!

--

--

Akalanka De Silva

Engineer, tech enthusiat, father of two living in Melbourne