In this article, we are going to set-up a ready-to-use Eclipse-based toolchain named ChibiStudio for the development on STM32. We will also explore this development toolchain explaining some basic operations necessary for daily development. So we will introduce the ChibiOS project, we will setup ChibiStudio taking a tour of some parts of it, we will import some ready to use projects and perform our first flash and run. Note that to proceed with this tutorial is necessary the installation of ST-Link drivers: this procedure has been shown in the previous article From 0 to STM32.
Notes about programming language
We are going to use C.
The ChibiOS project
We said that ChibiOS is an Embedded Library but this is the most simplistic and general way to qualify it. To be more specific, ChibiOS stands for Small Operating System (Chibi is a Japanese word which means small) nonetheless it has evolved over the year to become something more than a Real-Time OS (or RTOS).
History in brief
ChibiOS (formerly ChibiOS/RT) is a project born in the 80s and published on Sourceforge in 2007 by my mentor and friend Giovanni Di Sirio. In the beginning, the project was named ChibiOS/RT because it was basically an RTOS. After some months of development, the project starts to include a set of device drivers for testing purposes.
Growing up, the device drivers branch became self-consistent. It was split from RT and named as Hardware Abstraction Layer aka ChibiOS/HAL. Prior to the version 3.0.0, the HAL and the RT were joint together and interdependent. Starting from 3.0.0, the two parts have been separated and interconnected through an Operating System Abstraction Layer (also known as OSAL) in this way HAL became able to run independently from the underlying Operating System.
The ChibiOS project has now a portfolio of four software products which could be used separately or joint-together. It now counts two different kernels: the first is RT aka ChibiOS/RT; the second could be considered a subset of RT designed to be the smallest RTOS ever and is know as NIL or ChibiOS/NIL.
Starting from the latest version, ChibiOS include also a new set of complex drivers: its name is ChibiOS/EX and stands for external peripherals. We will properly describe all these products as we start to use them. In these tutorials, we are going to use some demos based on ChibiOS/RT + ChibiOS/HAL.
Here a collection of useful links related to ChibiOS. We will deepen them later but right now is good to have all the quick references grouped in the same place:
- The ChibiOS project has an Official Website (chibios.org) and a support forum (forum.chibios.org): I suggest you explore the website because it provides interesting technical information, documentation, and news. Another good idea is to subscribe to the forum and get in touch with the community: it could be an irreplaceable source of knowledge.
- This project has an Official Facebook page: the suggestion here is to like and to follow it to receive news about releases or events related to the project.
- The code comes with detailed documentation (there is a document for each sub-project): this is very useful while developing. Anyway, the documentation is auto-generated from code using a tool named Doxygen: exploring the code, you will be able to find all the information needed. We will deepen this point later during hands-on.
- The project is distributed through OSDN. On the project page, you will find a link to the SVN repository and a directory with all the downloadable files.
Concluding, PLAY Embedded has a lot of resources to learn to work with ChibiOS. We often offer support to people who comment on our articles or subscribes our forum (forum.playembedded.org).
When you ask for help the most important think is to give a precise information about the version of ChibiOS you are using.
The project is released as package versioned using the method yy.m.p “codename” where:
- yy indicates the release year (less significant two digits) and changes only on major releases;
- m indicates the release month number and changes only on major releases as well as year;
- p indicates the patch, a number incremented in case of small bug fixes on minor releases;
- codename is an identifier for the current patch.
Each product (for example RT or HAL) has its own version number according to the method M.m.p where:
- p indicates the patch, a number incremented in case of small bug fixes;
- m indicates the minor, a number incremented in case of small changes or even addition of features which in any case do not impact on APIs;
- M indicates the major, a number incremented in case APIs change or addition of big features which requires a change of the design.
At the time of writing the latest ChibiOS package was also indicated as ChibiOS 17.6.2 “Allegro” while ChibiOS/RT was 4.0.0, ChibiOS/HAL 5.0.0 and ChibiOS/NIL 2.0.0. ChibiOS EX instead is a collection of drivers individually versioned. Now I am reviewing this article ChibiOS package is marked as ChibiOS 19.1.2 “Minori” but concept explained in the articles here presented are still valid.
Each ChibiOS sub-project has its own version number identified by three numbers M.m.p. The whole package is versioned with a three number plus one word system yy.m.p “codename”.
There is full compatibility between two different patch numbers. For example, the code that runs on ChibiOS 17.6 “Adagio” runs also on ChibiOS 17.6 “Allegro” but maybe does not compiles on ChibiOS 19.1 “Minori”. Note that usually, it is quite easy to port your application from an older version of ChibiOS to another but the complexity depends on the dimension and the design of your code.
How to use it
Basically ChibiOS is a software library: it is a folder containing source code and other plain text files. There is no way to use ChibiOS itself without a toolchain!
Dealing with programming, more so with embedded programming, you will often hear about “toolchains”.
A toolchain is a set of distinct software development tools linked (chained) together. When you develop code for embedded systems you are compiling code which will run on a machine different from your Computer.
So, we need
- specific embedded libraries able to address our MCU
- a compiler able to generate code which will run most likely on a different architecture: this is known as a cross-compiler,
- a flasher to flash the binary into the STM32,
- in a decent development system, a debugger to test code at run-time.
All those things are provided all together in ChibiStudio, a ready to use toolchain named ChibiStudio which we are now going to install. The following figure schematise the toolchain:
On the PC side, we have the toolchain which contains the ChibiOS Embedded Software + the user application. It is compiled through the ARM GCC Cross Compiler generating a firmware.
On the Development kit side, we have the STM32 connected through the JTAG to the ST-Link debugger.
The two sides are interconnected by the debugger which is composed of a hardware layer (STLink + Cable) plus a software layer (OpenOCD and GDB Server). Using this interconnection is possible to flash the firmware on the STM32, to execute the code step by step or to explore the MCU memories and CPU registers.
Setup of ChibiStudio
In the following, we will setup ChibiStudio on Microsoft Windows. Note that, it is possible to re-assemble the same toolchain under Linux or Mac OS and you can find some guides about that on the ChibiOS website (I have seen ChibiStudio running under Mac OS X Sierra and under Linux Mint).
I am not used to Linux but I have planned to write an article about how to assemble ChibiStudio under a Linux. Right now, in case you are following this tutorial, my suggestion is to temporarily install a virtual machine. If someone wants to contribute to the Linux setup article just comment on this post: help is always welcome.
What follow is a “Learn by doing” section: from here and out is highly suggested to repeat the same steps on your own. Practice is the best way to understand!
Requirement for Windows
There are some requirements to install ChibiStudio:
- Windows 7 or Higher with Administration Rights;
- Java 32-bit since ChibiStudio is based on Eclipse 32-bit. Note that, Java 32-bit and Java 64-bit can co-exist on the same machine;
- The latest version of 7-zip: ChibiStudio is compressed as .7z and older version of the file manager can introduce issues during the extraction.
- Uninstall all the antiviruses and enable Windows Defender.
Let me do some clarifications about the latest point. The toolchain contains a lot of executables like make.exe from GCC or openocd.exe. These executables are so anonymous that common antiviruses detect them like viruses. The antivirus could kill or quarantine them when they are called to execute their normal functions. This generates unexpected behavior of your toolchain. Some antiviruses can be also temporarily disabled but, in my experience, they continue to work in the background even when disabled.
A funny remark, in 2014 Brian Dye, a Symantec’s senior vice president for Info-Sec said that antivirus “is dead”: Dye told the Wall Street Journal that hackers increasingly use novel methods and bugs in the software of computers to perform attacks, resulting in about 55% cyberattacks going unnoticed by commercial antivirus software. My suggestion remains the same: uninstall all the antiviruses and enable Windows Defender; the best antivirus is your finger.
Step by step guide
Starting from 2019, the ChibiOS project has been moved from SourceForge to OSDN.
To get the latest version of ChibiStudio it is highly suggested to visit the Official page of ChibiStudio on chibios.org where references are always updated. Another way to get the direct link to the latest version of ChibiStudio is to visit the link getchibistudio.chibiforge.org.
The setup of ChibiStudio could be performed following these simple steps:
- Download the latest version of ChibiStudio.
- Extract it under C:\ using 7zip.
- Copy the link “Chibi Studio GCC 7.0” or “Chibi Studio GCC 5.4” on the desktop and use it to launch the toolchain.
The two links are quite the same except for the compiler version used by ChibiStudio: the first uses gcc-arm-eabi 7.0 and the second gcc-arm-eabi 5.4.
It is very important that toolchain is extracted under C:\ because batch file and Eclipse are preconfigured: the toolchain will work if the its files are placed at C:\ChibiStudio!
To make thinks simplest, this short video will show you how to execute the previous enumerated steps.
If you are not able to run ChibiStudio and an error windows prompts most likely you haven’t installed Java 32-bit correctly or something went wrong during extraction.
Some necessary concepts
Startup with ChibiStudio could be thorny, especially if you are new to Eclipse. We are going to introduce some general concept related to Eclipse pointing out which are the ChibiStudio specific customization: this would improve your experience. Feel free to jump to paragraph 4.2 if you are already experienced with ChibiStudio.
First of all, Eclipse is an Integrated Development Environment (often abbreviated IDE). An IDE is a tool which aims to simplify the development process with visual aids like code highlighting, autocomplete content assist, advanced code browsing, debugging windows and so on.
Eclipse is completely open source and is widely used but not strictly necessary: it is possible also to write code in a text editor and compile it using a shell if you prefer. Anyway, a well configured IDE is always a good help to develop code quickly, especially if you are not used to shell and scripting languages.
Workbench and workspace
Eclipse is organized in Workbenches: a workbench is practically the Eclipse main window. It is the bench where you can organize your programming instruments, hence the different views and editors. Views and editors are your instruments to manipulate sources and data. and you can arrange them at hand for specific tasks (exploring, debugging, coding, etc.).
All the configurations related to Workbench (settings, IDE related variables, perspectives, history, imported projects, etc.) are saved as metadata in a specific location which is named Workspace. The workspace folder is also the default location where new projects are saved.
ChibiStudio comes with more than a release of ChibiOS for a matter of back compatibility. For example, ChibiStudio Preview 21 comes with three different versions of ChibiOS:
- ChibiOS 19.1.1 (located at C:\ChibiStudio\chibios191), which was the latest stable version of ChibiOS when ChibiStudio has been released.
- ChibiOS 18.2.2 (located at C:\ChibiStudio\chibios182), which is the previous version of ChibiOS with the latest patch available when the toolchain has been released.
- ChibiOS Trunk (located at C:\ChibiStudio\chibios_trunk). The Trunk represents the development version of ChibiOS which by definition is not stable updated at the latest revision number available when ChibiStudio has been released.
The presence of the oldest version of ChibiOS would be a kind of bridge between the old version and the new one: having the previous and the latest version of ChibiOS in the same toolchain could be the chance to port projects from the previous ChibiOS release to the latest one using the same toolchain. The trunk instead could be used to test new features and is also used from us to continue the development. Note that is possible to work on trunk updating it to the latest revision following this article (we are not going to use trunk right now so you don’t need to do this now).
For each version of ChibiOS integrated into ChibiStudio, there is a related Workspace. So, in ChibiStudio Preview 20, there are three different workspaces:
- Workspace 191 (located at C:\ChibiStudio\workspace191), which is configured with all the available demos for ChibiOS 191 already pre-imported;
- Workspace 182 (located at C:\ChibiStudio\workspace182), which is configured with all the available demos for ChibiOS 182 already pre-imported;
- Workspace Trunk (located at C:\ChibiStudio\workspace_trunk), which could be to try trunk new features.
Additionally, there is another workspace:
- Workspace User (located at C:\ChibiStudio\workspace_user, which is an empty workspace dedicated to the user.
In Eclipse, Workspace and workbench are different terms for different concepts: The workbench is the Eclipse main window, The workspace is a directory that holds information about workbench like the projects you work on, as well as the Eclipse preferences (from layout to behaviour). For more information you could refer to Eclipse documentation.
It is possible to switch workspace in use through the menu File->Switch Workspace and choosing a workspace from the history or selecting “Other…” to browse a new workspace like is shown in the next figure.
Taking a look at this menu you can also figure out which is the workspace in use because it would not be in the list. Refers to the previous figure, we are currently using Workspace User which is not in the history list.
Choosing another Workspace Eclipse will restart loading a new Workbench.
The organization of your Workbench could be saved to be quickly recalled at the proper moment: these preset take the name of perspectives.
Perspectives could be switched using the proper button in the top right corner of your Workbench. It is also possible to add other perspectives, save or reset the current one to default through the drop-down menu “Window” located in the menu bar.
Note that switching perspective is a matter of views organization, this will not impact your code or your files. You can switch perspective as you want without any concern.
ChibiStudio comes with two separate perspectives: C/C++ and Debug. If the on first run your do not have C/C++ and Debug perspectives on the top right corner most likely you haven’t uninstalled the antivirus or you haven’t updated 7zip: the extraction has been affected by this.
The two perspectives reflect the way which is split the code development process. Code editing is only a phase of the whole development process: having a compiling code it does not mean it is working as expected. We are talking about functional behavior here. So, the whole development process could be split into two specific parts that are two specific moments of the development process:
- The software development, in other words, that process related to new project creation, code editing, compiling and resolution of errors and warnings. The perspective C/C++ is oriented to this part and you can figure out this because the largest amount of workbench area is dedicated to the text editor area.
- The debugging, in other words, that moment related to testing the functionality of our code, detection of unexpected behavior or evaluation of software performances and consistency. The perspective Debug is oriented to this phase and it offers a lot of additional windows to explore code, memory, and variables or to evaluate an expression, connect terminal and so on.
At first launch, ChibiStudio usually shows a welcome window: just close it. You should be now in the C/C++ perspective. The editor window is the largest windows in the middle of the workbench and should be empty. On the right, there is the Project Explorer which usually contains all the imported projects.
If you have downloaded ChibiStudio Preview 20, you will start in the User Workspace and the project explore should contain only two dummy projects representing the documentation and the tools of our toolchain as shown in Fig. 5.
The best way to proceed at this point is to import a ready to use the demo. The demos we need to import are under the folder C:\ChibiStudio\chibios182\demos\STM32. This is true because we are going to use ChibiOS 182 but there are similar folders also under ChibiOS 176 and ChibiOS Trunk directories.
In the demos\STM32 directory, there are a lot of folders each of which represents a standalone project. Each project is named following the same pattern KERNEL-MCU-BOARD. For example, RT-STM32F207ZG-NUCLEO144 is a demo based on ChibiOS/RT for STM32 Nucleo F207ZG, a Nucleo-144 equipped with STM32F207ZG and it works on this specific development kit.
Importing an existing project
The next step is to import a demo for the development board you are going to use. Almost every official STM32 Discovery and STM32 Nucleo boards are supported. Since I will use both STM32 Nucleo F401RE and STM32F3 Discovery kit, I will import both RT-STM32F401RE-NUCLEO64 and RT-STM32F303-DISCOVERY.
The steps required to import an existing demo are:
- Select File->Import from the menu;
- In the newly prompted window select Existing project into workspace and press Next;
- Select Root directory: “C:\ChibiStudio\chibios182\demos\STM32″;
- Check all the project you want to import and press Finish.
The following video has been done with ChibiStudio Preview 19. If you are using ChibiStudio Preview 20 things are almost the same. The difference is that the latest version of ChibiOS is 182 thus we have to select a different demo root (“C:\ChibiStudio\chibios182\demos\STM32” instead of “C:\ChibiStudio\chibios182\demos\STM32”).
This argumentation will be more valid when a newer version of ChibiStudio will be available. It is always a good idea to use demos from the latest stable version of ChibiOS and many users are even working with Trunk. Note also that in the following video we are importing a demo named RT-STM32F303-DISCOVERY-REVC which in ChibiOS182 have been merged with RT-STM32F303-DISCOVERY (thus in case you are using F3 Discovery import this one).
Handle imported projects
At this point, the Project Explorer should contain one or more additional projects according to how many projects you have imported.
In Eclipse, a project is a blue folder containing files, links, and subfolders and it actually is mirrored to a real folder. As example, the project RT-STM32F401RE-NUCLEO64 is mirrored to the directory “[chibios folder]\demos\STM32\RT-STM32F401RE-NUCLEO64\”. To be able to explore a project in Eclipse it should be open. You can open a project right clicking on it and selecting Open Project.
In Eclipse a project could be open or closed right clicking on it and choosing the proper entry from the menu. A project shows its resources only when is open. To avoid confusion, it is a good idea to work keeping all the projects closed except the one you are working with. It is possible to close all the project except one right clicking on the one you want keep open and choosing Close Unrelated Projects.
Build and clean
The projects we are going to use are based on makefile: the makefile is a script file which contains a series of directives used by make to build our code. All the projects in ChibiOS come with a complete and a ready-to-use makefile.
It could happen that the makefile has to be edited to customize the build process or include new sources and we will face this problem if necessary. Right now we do not have to edit the makefile. Anyway, is interesting to know that if build procedure ends successfully the makefile is designed to create a subfolder named build under the project main folder.
As shown in the figure, the build folder contains the compiled firmware as binary (ch.bin) and as hexadecimal (ch.hex) but also the Executable and Linkable Format file (ch.elf). The ELF contains the executable but also information about how the code is organized in memory and the list of all symbols: this file is required by debugging activities.
To build a project right click on it and select Build Project. Another way is to click on it and press the hammer button in the tool bar. If the procedure ends successfully a new folder into the project will appear: it is named build and contains the output of make process as bin, hex, elf and other formats.
Note that a project could be also cleaned, the clean procedure removes the build. If we do not clean a project, in case of small edits, next build time will be shorter. This because the build folder which contains also some object files.
The object files are a kind of partial output of the making process. These files are not rebuilt if Eclipse “thinks” that related sources have not been modified. This reduces the built time in case of small edits.
Rarely could happen that this mechanism fails: in this case, there could be some misalignment between code and binary. In such a particular condition, a clean and rebuild will solve any issue. A good idea is to clean and rebuild the project when we do major edits to our code, especially when dealing with makefile or pre-processor directives.
There could be mutiple causes of failure during build of default demos and most common are: your antivirus, an error occured during extraction of ChibiStudio or a misplacement of ChibiStudio main folder.
Flash and run
An important part of software development is the flash and run. This procedure copies the binary into the microcontroller flash memory and starts its execution. The flash and run is automatized in ChibiStudio and involves OpenOCD on the PC side and ST-Link on the Development board side as shown in Fig.1.
OpenOCD is an open source GDB server which offers a programming interface to a hardware debugger. The debugger allows common operations like write/erase flash memories, write/clear RAM memories, set register values, set breakpoints or watchpoints, set program counter, execute the code step by step and so on. From our point of view we can say that:
The debugger is composed by an hardware part (in this case ST-Link Embed Debugger) and a software part (in this case OpenOCD). The debugger could be considered like a bridge between our IDE and the MCU. To perform any debugging operation the first step is to “create this bridge”
There are two buttons which we will use to execute the flash and run: these two buttons are shown in the figure and are the External Tools Menu (Orange) and the Debug Menu (Azure). Actually, we should not press on the button but on the small black arrow beside them: this will open a drop-down menu.
To execute the “flash and run” we have to launch OpenOCD: this could be done from the External Tool Menu (Orange).
In this menu, there are many launch configuration. When using the ST-Link as the interface you should pick up the script: “OpenOCD on ST-Link (prompts for .cfg target configuration)“. Note that, many OpenOCD configurations specify the interface internally and thus the script “OpenOCD no interface (prompts for .cfg target configuration)” will work as well.
If the external tool menu is empty, check that you have imported and opened the project ChibiStudio Tools: this project contains the launch configurations of all the external tools including OpenOCD.
After selecting a voice from that menu, a window will prompt: in here, we have to pick-up a configuration script for OpenOCD which indicates which board we are going to target. All the scripts are contained under the folder C:\ChibiStudio\tools\openocd\scripts\board and usually there is a script related to each STM32 development kit.
By choosing the configuration, OpenOCD attempts to start-up and, if this operation goes successful we will see the target information in the Console window, like the MCU voltage, the number of breakpoints and watchpoints as shown in Fig.9.
From the Debug Menu (Azure in fig. 8), we can now launch the flash and run procedure selecting the proper Launch Configuration. Each project has its own Launch Configuration. It is easy to associate the configuration to the related project because they have the same name.
Once we have selected it, Eclipse will build the project (if this has not been already done) and will start the procedure switching automatically to Debug: in this perspective, we can press the Resume button to run the code execution as shown in Fig.10.
- Launch OpenOCD from the External Tool Menu choosing the proper interface (ST-Link in this case);
- In the newly open window, select the proper configuration script related to the board in use;
- Check that OpenOCD has been started properly looking to the console windows: there should be information about the MCU voltage, the number of breakpoints and watchpoints.
- From the Debug Menu, launch the flash and run selecting the proper Launch Configuration;
- In the Debug perspective press the Resume button to run the code execution.
In the following video, we are showing the procedure of flash and run on two different STM32 development boards
The first “flash and run” procedure is very important to check if you have set up the toolchain correctly. As example, if you are not able to launch OpenOCD maybe there is a problem with ST-Link drivers: in this case check that drivers have been installed correctly and check also that the USB cable has been connected properly using to the proper port: certain Discovery kits have more than a USB port so you have to check that you are using the port marked as ST-Link USB.
If you receive an error while switching on Debug perspective it could be that OpenOCD has not been started properly or that it has lost connection with the target so you have to relaunch it (in debug context the MCU targeted by the debugger is usually called target).
If OpenOCD starts properly, you will be able to read the target voltage, the number of breakpoints and watchpoints
Information about the target will appear in the console windows and this will look like Fig.9. While OpenOCD is running, in the Debug perspective, you will see a related entry as shown in Fig.10. Note that,
You cannot launch OpenOCD twice and you cannot launch the flash and run procedure if OpenOCD is not running. To restart the OpenOCD you have to terminate the first instance.
Resuming the code execution, one or more onboard LEDs on will start to blink. On STM32 Nucleo-64 there is a green LED, in case of STM32 Nucleo-144 there are three colored LEDs which execute a nice light game, in case of STM32F3 Discovery it is even showier since this board has 8 LEDs.
If LEDs blinks you have successfully completed this tutorial and you can jump to the next article. In case of problems, just comment on this article or subscribe to our forum.
In this article, we focused more on the IDE and the basic procedures required by software development. In the next article, we will take a deeper look to default demos focusing more on programming.
Previous and next
This article is part of a series of articles which are meant to be tutorials. I have composed them to be read in sequence. Here the previous and next article of this series:
- The ChibiOS project
- Setup of ChibiStudio
- Exploring ChibiStudio
- Previous and next