Linux’s origins can be traced back to the commercially available Unix operating, but it wasn’t until Finnish student Linus Torvalds announced a free Linux kernel in 1991 that the open source operating system movement really started to gain traction Today, there are several popular Linux distributions that are designed for use with SBCs, an example being the Debian-based Raspbian distribution for use with the Raspberry Pi.
You will also find Linux in use with desktop computers, and in fact, the vast majority of the world’s datacentre servers use Linux. There isn’t a specific version, or distribution, of Linux for use on embedded single board computers – it’s just that some distributions contain less in the way of specific resources or peripheral support, although it may be easy it add them later. Another reason for this is that some SBCs are headless, that is to say they do not have a video output, such as HDMI or composite video. Video controller ICs and associated memory add component cost, size and power consumption, so applications such as an IoT sensor can be kept extremely compact, power efficient and low cost without such capabilities.
Most embedded developers will interact with Linux through the command line interface (CLI) rather than using any desktop graphical environment. But before we embark on some of the basic Linux commands, let’s first of all consider why we are using an operating system.
The alternative to using an operating system is usually nesting your application program into a single ‘round-robin’ or ‘super-loop’ structure and programming using a ‘bare metal’ approach. While this can yield an efficient design, it can become rather cumbersome if the application starts to add features, particularly those requiring networking and connectivity. For those familiar with using an Arduino, this is the approach you experience, where additional libraries and associated drivers need to be added in order to facilitate, for example, an internet connection.
While any operating system adds a degree of memory and resource overhead to any design, the capabilities it provides without any additional effort makes it highly desirable.
Some of the standard Linux functionality includes the provision of a file system, network connectivity and task scheduling, to mention just a few of the many essential features. Linux is an extremely scalable and efficient operating system that has hundreds of commands available, a few of which we’ll cover in this article. Linux also makes it extremely easy to interface to the real world, using GPIO, ADC/DAC and serial interfaces such as I2C.
It is worth noting that, however, Linux is not a real-time operating system since its kernel is not pre-emptive; that is to say that once the kernel is executing a command it cannot be interrupted. So, for those applications that demand high levels of deterministic behaviour, where the operating system can respond in a predictable time, a standard Linux distribution might not be suitable. That said, there are a number of industry initiatives currently underway that aim to provide a pre-emptive-based real-time Linux OS distribution. Some SBCs do cater for real-time applications through the use of dedicated hardware, of which the BeagleBone Black is a good example. Readers interested in this capability are recommended to investigate the BeagleBone Black’s programmable real-time units (PRUs).
The command line interface
Your initial experience of Linux will probably be via its command line. As an administrative user you will enter the operating system’s hierarchy at a ‘root’ level. This structure is the same regardless of the distribution you might be using – see Figure 3.
For this part of the article Linux commands are written in green, and <ret> refers to pressing the return or enter key. Using a combination of the control key and another key, such as ‘D’ in this example, is signified by Ctrl-D. When using a headless board, you will have no option but to connect to the SBC via a remote terminal. In the following examples we are using an Apple MacBook Pro running OS X connecting to a BeagleBone Black that has an IP address of 192.168.1.102 and a default user of ‘root’. Making a connection to your SBC will be down to using a simple terminal program such as PuTTY when running under Microsoft Windows, or Apple’s Terminal, which is already included within OS X.
Within Terminal you can use the SSH (Secure Shell) command to securely connect remotely to another computer. Once connected you will be able to run any command as if you were directly at that computer. The most common format for the SSH command is ssh user@host<ret>, as shown in Figure 4. In this example SSH flags the fact that this host has not been communicated with previously. On accepting the communication, this new connection is stored in the Mac’s known hosts file.
Readers are encouraged to try out some of the basic commands at the Linux command line. ls<ret> lists out files in the current directory, the ls –l<ret> creates a tabulated list of files while the ls – -help gives a detailed instruction on all command options – see Figure 4.
Other popular commands include:
df<ret> to provide file system information and a list of mounted devices
rm filename<ret> to delete a file
mkdir majordomo<ret> to make a new directory
cd majordomo<ret> to change to a new directory
rmdir majordomo<ret> to delete a directory
cp setup.py setup.old<ret> to copy the file setup.py into the file setup.old
mv clock.py project/clock.py<ret> to move the file clock.py into the folder project
You can also ‘pipe’ the output of one command as the input of another command. For example the command ls –R | grep .py | cat<ret> searches recursively from the current directory for any files with the ‘py’ in the file name and then passes it to cat, which displays the output into screen readable pages.
When it comes to creating and editing source code or configuration files Linux has a number of editor options for use at the command line. These include vim and nano. Both are popular, with personal preferences dictating the one you end up using on a regular basis. Most users will find nano extremely quick to master while vim takes a lot more practice.
Controlling an SBC’s GPIO from the Linux command line is very straightforward; since everything within Linux is viewed as a file, GPIO pins are also viewed in much the same way. Each pin will typically be accessible from the /sys/class/gpio directory. You set the direction of the pin through sending an ‘out’ or ‘in’ command by using, for example for gpio44, echo out > direction<ret>, and you can then set a pin either high or low with the following commands: echo 1 > value<ret> or echo 0 > value<ret>.
In this article we have introduced the Linux operating system and highlighted just a few commands to get you started. There are many, many more, some of which, such as ic2detect, are specifically for debugging I2C communications between, for example, an accelerometer sensor and the SBC.
Linux provides an extremely resilient, scalable and powerful operating system for use with your embedded SBC.
About the author:
Manuel Gschwend is Head of Product Group at Conrad Business Supplies – www.conrad.com