Getting Started with ChibiOS

Today we will tackle the problem of OS on our microcontroller(MCU), the STM32F7 Discovery.

ChibiOS/RT is an RTOS(Real Time Operating System) used for development on embedded platforms. Its licence allows free as well as commercial use. It is very well documented, with lots of examples.

chibios_rt_logo
ChibiOS Mascot

Some features of ChibiOS:

  • Efficient and portable preemptive kernel.
  • Many supported architectures and platforms.
  • Static architecture, everything is statically allocated at compile time.
  • Dynamic extensions, dynamic objects are supported by an optional layer built on top of the static core.
  • Rich set of primitives: threads, virtual timers, semaphores, mutexes, condition variables, messages, mailboxes, event flags, queues.
  • Support for priority inheritance algorithm on mutexes.
  • Hardware Abstraction Layer (HAL) component supporting a variety of abstract device drivers: Port, Serial, ADC, CAN, EXT, GPT, I2C, ICU, MAC, MMC, PWM, RTC, SDC, SPI, UART, USB, USB-CDC.
  • Support for external components uIP, lwIP, FatFs.
  • Extensive test suite with benchmarks.
  • Support for C++ applications.

Our objective is to load the RTOS on the MCU and learn to write a thread for the simple Hello World example using it. First of all, to save a lot of steps in setting up the development environment, we will use ChibiStudio.

Please understand that this abstracts a lot of details from the first time embedded developer about cross development and building ChibiOS using toolchains, but that can be explained in another post. Right now, the focus is on getting up and running for development with ChibiOS+ STM32F7 as soon as possible. Let’s hit the dirt!

ChibiStudio: ChibiOS developers have provided an excellent development environment called ChibiStudio, which simplifies setting up IDE considerably. It comprises of the following tools prebaked in a single Eclipse based IDE:

Eclipse Luna 4.4.2 with the following optional components installed already:
Eclipse CDT
– C/C++ GDB Hardware Debugging
– Eclipse XML Editors and Tools
– Target Management Terminal
Serial Connector plugin.
– ChibiOS/RT debug plugin
– ChibiOS/RT configuration plugin
Embedded Systems Register View plugin
GCC ARM toolchain gcc-arm-none-eabi-4_7-2013q2.
YAGARTO Tools.
OpenOCD
ChibiOS/RT

To begin with the project, let’s execute a very simple program.

The objective is to “Make the onboard LED flash in a heartbeat pattern“.

We will be working in C, so the project must begin with two files:

  1. main.c : File containing main(). This file controls execution of all functions.
  2. makefile: Used to build the project with toolchain.

A ChibiOS based project must also contain three additional files:

  • chconf.h: Containing configuration related to kernel. For example, here it is possible to configure system timing, enable/disable kernel features and enable/disable debug options.
  • halconf.h: Containing configuration related to HAL. Here it is possible to enable/disable whole HAL modules or configure them.
  • mcuconf.h: Containing specific configuration of used MCU. For example, if we enable a certain module in halconf.h, we can choose how many drivers to assign to/enable for that module.

 

Open up ChibiStudio by extracting the archive you downloaded using the link above. Remember to place the final extracted archive in C: drive only(to ease things as of now). The path to executing ChibiStudio(eclipse) should look like

C:\ChibiStudio\

Open up the file ChibiStudio GCC 4.9 and wait for eclipse to start. You should be greeted with a screen similar to the one below.

chibistudio
ChibiStudio

On the left side, the main tree of ChibiOS is displayed in the project explorer. Do not worry about other parts of the window, we will come to those later. We will start by finding our board from the available list, expanding its directory and open the main.c file. You will be greeted by the source code of the main file.

chibibegin2.PNG

Now, the main file has a very simple thread(function) to flash on LED on board while demonstrating ChibiOS. We will extend this a small bit and try to flash the LED in a heartbeat fashion, akin to the BeagleBone Black.

Go to line 30 of the main.c and replace the code given inside while statement as follows:

  while (true) {
palSetLine(LINE_ARD_D13); //First Beat ON
chThdSleepMilliseconds(75);
palClearLine(LINE_ARD_D13); //First Beat OFF
chThdSleepMilliseconds(150);
palSetLine(LINE_ARD_D13); //Second Beat ON
chThdSleepMilliseconds(75);
palClearLine(LINE_ARD_D13); //Second Beat OFF, Wait
chThdSleepMilliseconds(1000);
}

Save your edits(Ctrl-S or click the familiar floppy icon near top left). Now click the hammer icon just below the Navigate menu on top bar. This will build the project and generate the hex and binary files needed for execution on board. These executables will be located in the project’s directory in a separate folder named ‘build’. In my case, the folder is located at

C:\ChibiStudio\chibios161\demos\STM32\RT-STM32F746G-DISCOVERY\build

Go to this folder and find the hex and bin files waiting to be uploaded to the board. Fire up ST-Link Utility and upload the bin file to the board! For any confusion regarding how to upload file using ST-Link Utility, refer to this post.

IMG_20160602_130244_1464852848458

You should now see the LED blinking in a heartbeat fashion. Enjoy this success!

Fiddle with this code and try to make the LED flash in your own amazing way if you want to. You can even try writing morse code!

For any suggestions/corrections, please comment below!

Feel free to contact me to correct any mistakes or send a friendly pointer to better sources for the same knowledge. I shall be grateful to you for the same.

Advertisements