📜 ⬆️ ⬇️

ChibiOS: Lightweight RTOS


In this article I want to introduce and briefly describe a member of the real-time OS family - ChibiOS.

License


First of all about licensing. ChibiOS is free RTOS and has several licensing options - GPL 3.0, GPL 3.0, with some linking exceptions and a commercial license.
Driver code in most cases comes under the Apache-2.0 license.
All licensing options are available on the project website. The presented options must fully satisfy the fans, as well as companies that do not want to pay for the use of this operating system.

Structure


The project is logically divided into several subsystems:

In addition to everything there are a few add-ons on this whole thing.
ChibiOS itself is implemented in C and assembler, but there are also wrappers for C ++.

Core


ChibiOS, like any other self-respecting RTOS, has a displacement support task scheduler and currently has two options for its operation:

At the moment, the scheduler options are set globally at compile time.
For context switching, the kernel scheduler uses a system counter that is also used for virtual timers.
Naturally, any interruption can lead to a context switch if necessary.
')
In the current version of ChibiOS (2.x), the period of the system timer is fixed (at the assembly stage).
In the future version (3.0), the developers plan to switch to the functioning of the kernel without periodic interruptions (tickless scheduler).

The kernel itself provides basic synchronization elements (mutex, semaphore), memory management (heap, mempool), various queue management primitives (including mbox and events), and of course task management.
ChibiOS has the ability to create tasks both statically and dynamically.

Devices and architectures


A remarkable feature of ChibiOS is support for a fairly wide range of architectures.
Officially supported architectures: ARMv5-7 (LPC1x, LPC2x, STM32), STM8, PPC, MSP430, MegaAVR.
From the informal can be noted Mips32 (qemu, pic32mx), AVR32, Coldfire and NiosII.
Of course, this list cannot be with FreeRTOS, but from personal experience I will say that adding a new architecture is not as difficult as it may seem.

The basic set of HAL drivers includes a fairly wide range of peripherals: ADC, CAN, DMA, TMR, I2C, I2S, ICU, GPIO, PWM, RTC, SPI, UART, USB. In general, almost all common components of modern SoC.
In addition, HAL has several add-ons above the above components (USB ACM, MMC / SPI and others), which I would have taken out of this layer. But so, apparently, has developed historically.

Little things


From the "buns" we can distinguish the ability to connect uIP, lwIP and FATFS (implementation from a certain Mr. ChaN).
There is also a fairly convenient shell to use - a trifle, but nice.

The API of most subsystems is quite simple and readable (especially the kernel), although in some cases it is impossible to do without an example.
Sometimes it upsets the dependence of the API of some drivers on the platform for which the driver was originally developed, but as far as I understood, drivers are not the main priority of the OS and are developed handicraft for the current task. Mostly come across dense clusters of duplicate code.
It seems like it is planned to bring the entire HAL to a separate repository. There is also a potential danger that in future versions of the ChibiOS HAL will be rewritten in C ++.

Of particular weaknesses, I would point out how to configure the system with macros (a set of drivers, features of the functioning of subsystems, etc.). This makes it quite difficult to support out-of-tree projects when removing or adding options to the mainstream.

There are some design solutions that I do not like, but perhaps this is a personal preference.

Example


And to seed a small example of system initialization and shell creation. The code is presented without comment intentionally.
#include "ch.h" #include "hal.h" #include "shell.h" #include "chprintf.h" static void oNotifySD1(GenericQueue *qp) { msg_t b; b = sdRequestDataI(&SD1); if (b != Q_EMPTY) sd_lld_putc(&SD1, b); } #define SHELL_WA_SIZE THD_WA_SIZE(1024) static void cmd_test(BaseSequentialStream *chp, int argc, char *argv[]) { chprintf(chp, "ChibiOS test suite\n"); TestThread(chp); } static const ShellCommand shCmds[] = { {"test", cmd_test}, {NULL, NULL} }; static const ShellConfig shCfg = { (BaseSequentialStream *)&SD1, shCmds }; int main(void) { Thread *sh = NULL; halInit(); chSysInit(); { const SerialConfig sc = { .sc_baud = SERIAL_DEFAULT_BITRATE, .sc_rxirq = EIC_IRQ_UART1_RX, .sc_port = _UART1_BASE_ADDRESS }; sdObjectInit(&SD1, NULL, oNotifySD1); sdStart(&SD1, &sc); } shellInit(); for (;;) { if (!sh) sh = shellCreate(&shCfg, SHELL_WA_SIZE, NORMALPRIO); else if (chThdTerminated(sh)) { chThdRelease(sh); sh = NULL; } chThdSleepMilliseconds(1000); } 


I almost forgot to mention that the head and dictator of the project is Mr. Giovanni Di Sirio.
And of course the link to the site: ChibiOs .

Thanks for attention.

Source: https://habr.com/ru/post/181912/


All Articles