Hands-On Embedded Programming with C++17
上QQ阅读APP看书,第一时间看更新

Embedded operating systems

An OS is typically used with an embedded system when you're writing your application directly for the system's hardware, which is an unrealistic proposal. What an OS provides to the application is a number of APIs that abstract away the hardware and functionality implemented using this hardware, such as network communications or video output.

The trade-off here is between convenience and both code size and complexity.

Whereas a bare metal implementation ideally implements only those features it needs, an operating system comes with a task scheduler, along with functionality that the application being run may not ever need. For this reason, it's important to know when to use an OS instead of developing directly for the hardware, understanding the complications that come with either.

Good reasons to use an OS are if you have to be able to run different tasks simultaneously (multitasking, or multithreading). Implementing your own scheduler from scratch is generally not worth the effort. Having the need to run a non-fixed number of applications, and being able to remove and add them at will, is also made significantly easier by using an OS.

Finally, features such as advanced graphics output, graphics acceleration (such as OpenGL), touch screens, and advanced network functionality (for example, SSH and encryption) can be made much easier to implement when you have access to an OS and readily accessible drivers, and the APIs related to them.

Commonly used embedded operating systems include the following:

What all of these OSes have in common is that they handle basic functionality such as memory and task management, while offering access to hardware and OS functionality using programming interfaces (APIs).

In this chapter, we will specifically focus on SoC and SBC-based systems, which reflects in the preceding list of operating systems. Each of these OSes is meant to be used on a system with at least a few megabytes of RAM and in the order of megabytes to gigabytes of storage.

If the target SoC or SBC is not yet targeted by an existing Linux distribution, or one wishes to heavily customize the system, one can use the tools from the Yocto Project (http://www.yoctoproject.org/).

Linux-based embedded OSes are quite prevalent, with Android being a well-known example. It is mostly used on smartphones, tablets, and similar devices, which heavily rely on graphical user interaction, while relying on the Android application infrastructure and related APIs. Due to this level of specialization, it is not well-suited to other use cases.

Raspbian is based on the very common Debian Linux distribution, aimed at basically just the Raspberry Pi series of SBCs. Armbian is similar, but covers a far wider range of SBCs. Both of these are community efforts. This is similar to the Debian project, which can also be used directly for embedded systems. The main advantage of the Raspbian, Armbian, and other similar projects is that they provide ready-made images to be used with the target SBC.

Like Linux-based OSes, NetBSD has the advantage of being open source, meaning that you have full access to the source code and can heavily customize any aspect of the operating system, including support for custom hardware. One big advantage NetBSD and similar BSD-based OSes have is that the OS is built from a single codebase, and managed by a single group of developers. This often simplifies the development and maintenance of an embedded project.

The BSD license (three- or two-clause) offers a major benefit for commercial projects, as this license only requires one to provide attribution instead of requiring the manufacturer to provide the full source code of the OS on request. This can be very relevant if one makes certain modifications to the source code, adding code modules that one wants to keep closed source.

Recent PlayStation gaming consoles, for example, use a modified version of FreeBSD, allowing Sony to heavily optimize the OS for the hardware and its use as a gaming console without having to release this code together with the rest of the OS's source code.

Proprietary options also exist, such as the offerings from BlackBerry (QNX) and Microsoft (Windows IoT, formerly Windows Embedded, formerly Windows CE). These tend to require a license fee per device and require the assistance of the manufacturer for any customization.