Chapter 1

The embedded and real-time space

Abstract

This chapter introduces concepts of embedded and real-time programming and shows how Linux fits in. We point out that embedded systems are often resource constrained, and use unusual peripheral devices not often found in PCs. Real-time means that the system must respond to its environment in a predictable time, not necessarily fast, but predictable.

Keywords

Embedded computing; real-time computing; open source; Linux; general public license

If you want to travel around the world and be invited to speak at a lot of different places, just write a Unix operating system.

Linus Torvalds

What is Embedded?

When I wrote the first edition of this book back in 2002, the notion of embedded computing devices was not widely appreciated. Back then many homes had at least a VCR and/or a DVD player and maybe a Tivo, all of which have at least one embedded processor. Most other appliances of the era were still basically electromechanical.

These days, virtually every appliance has an embedded computer in it. People are much more used to digital user interfaces, and while they may still not fully appreciate the nature of embedded computing, they are at least vaguely aware that they are surrounded by computers 24/7.

When I have to explain what I do for a living, I usually start by saying that an embedded system is a device that has a computer inside it, but the user of the device doesn’t necessarily know, or care, that the computer is there. It is hidden. The example I usually give is the engine control computer in your car. You don’t drive the car any differently because the engine happens to be controlled by a computer. Oh, and there’s a computer that controls the antilock brakes, another to decide when to deploy the airbags, and any number of additional computers that keep you entertained and informed as you sit in the morning’s bumper-to-bumper traffic.

I go on to point out that there are a lot more embedded computers out in the world than there are PCs. In fact, recent market data shows that PCs account for only about 2% of the microprocessor chips sold every year. The average house contains at least a couple dozen computers, even if it does not have a PC.

Is an Android smartphone an embedded system? It is small, self-contained, and has limited input/output capabilities. Nevertheless, you can personalize it and download “apps” to your heart’s content. In that sense, it’s really more of a general purpose computing appliance. So I would say no, an Android phone is not an embedded system.

From the viewpoint of programming, embedded systems show a number of significant differences from conventional “desktop” applications. For example, most desktop applications deal with a fairly predictable set of I/O devices—a disk, graphic display, a keyboard, mouse, sound card, and network interface. And these devices are generally well supported by the operating system. The application programmer doesn’t need to pay much attention to them.

Embedded systems, on the other hand, often incorporate a much wider variety of input/output (I/O) devices than typical desktop computers. A typical system may include user I/O in the form of switches, pushbuttons, and various types of displays often augmented with touchscreens. It may have one or more communication channels, either asynchronous serial, USB, and/or network ports. It may implement data acquisition and control in the form of analog-to-digital (A/D) and digital-to-analog (D/A) converters. These devices seldom have the kind of operating system support that application programmers are accustomed to. Therefore, the embedded systems programmer often has to deal directly with the hardware.

Embedded devices are often severely resource-constrained. Whereas a typical PC now has eight or more GB of RAM, and maybe a terabyte of disk, embedded devices often get by with a few MB of RAM and nonvolatile storage. This too requires creativity on the part of the programmer.

What is Real-Time?

Real-time is even harder to explain. The basic idea behind real-time is that we expect the computer to respond to its environment in time. But what does “in time” mean? Many people assume that real-time means really fast. Not true. Real-time simply means fast enough in the context in which the system is operating. If we’re talking about the computer that runs your car’s engine, that’s fast! That guy has to make decisions—about fuel flow, spark timing—every time the engine makes a revolution.

On the other hand, consider a chemical refinery controlled by one or more computers. The computer system is responsible for controlling the process and detecting potentially destructive malfunctions. But chemical processes have a time constant in the range of seconds-to-minutes at the very least. So we would assume that the computer system should be able to respond to any malfunction in sufficient time to avoid a catastrophe.

But suppose the computer were in the midst of printing an extensive report about last week’s production, or running payroll when the malfunction occurred. How soon would it be able to respond to the potential emergency?

The essence of real-time computing is not only that the computer responds to its environment fast enough, but that it responds reliably fast enough. The engine control computer must be able to adjust fuel flow and spark timing every time the engine turns over. If it’s late, the engine doesn’t perform right. The controller of a chemical plant must be able to detect and respond to abnormal conditions in sufficient time to avoid a catastrophe. If it doesn’t, it has failed.

I think this quote says it best:

A real-time system is one in which the correctness of the computations not only depends upon the logical correctness of the computation, but also upon the time at which the result is produced. If the timing constraints of the system are not met, system failure is said to have occurred.

Donald Gillies in the Real-time Computing FAQ

So, the art of real-time programming is designing systems that reliably meet timing constraints in the midst of random asynchronous events. Not surprisingly, this is easier said than done and there is an extensive body of literature and development work devoted to the theory of real-time systems.

How and Why Does Linux Fit in?

Linux developed as a general-purpose operating system in the model of Unix, whose basic architecture it emulates. No one would suggest that Unix is suitable as an embedded or real-time operating system. It’s big, it’s a resource hog, and its scheduler is based on “fairness” rather than priority. In short, it’s the exact antithesis of an embedded operating system.

But Linux has several things going for it that earlier versions of Unix lack. It’s “free,”1 and you get the source code. There is a large and enthusiastic community of Linux developers and users. There’s a good chance that someone else either is working or has worked on the same problem you’re facing. It’s all out there on the web. The trick is finding it.

Open Source

Linux has been developed under the philosophy of Open Source software, which grew out of the Free Software Movement pioneered by Richard Stallman who founded the Free Software Foundation (FSF) in 1985. Then in 1997, Eric Raymond published a paper, and later a book, titled The Cathedral and the Bazaar2, in which he argued that there are two models of software development: the cathedral—essentially top-down, and the bazaar—bottom up. In the cathedral model, source code is released, if at all, only when the software itself is released. Code developed between releases is restricted to an exclusive group of software developers, the “master craftsmen” if you will. The bazaar model has code developed over the Internet in full view of the public. As Raymond puts it, “given enough eyeballs, all bugs are shallow.” That is, the more widely available the source code is for public testing, scrutiny, and experimentation, the more rapidly all forms of bugs will be discovered.

Quite simply, Open Source is based on the notion that software should be freely available: to use, to modify, to copy. The idea has been around for some 20 years in the technical culture that built the Internet and the World Wide Web, and in recent years has spread to the commercial world.

There are a number of misconceptions about the nature of Open Source software. Perhaps the best way to explain what it is, is to start by talking about what it is not.

• Open Source is not shareware. A precondition for the use of shareware is that you pay the copyright holder a fee. Shareware is often distributed in a free form that is either time- or feature-limited. To get the full package, you have to pay. By contrast, Open Source code is freely available, and there is no obligation to pay for it.

• Open Source is not Public Domain. Public domain code, by definition, is not copyrighted. Open Source code is copyrighted by its author, who has released it under the terms of an Open Source software license. The copyright owner thus gives you the right to use the code provided you adhere to the terms of the license. But if you don’t comply with the terms of the license, the copyright owner can demand that you stop using the code.

• Open Source is not necessarily free of charge. Having said that there’s no obligation to pay for Open Source software doesn’t preclude you from charging a fee to package and distribute it. A number of companies are in the specific business of selling packaged “distributions” of Linux.

Why would you pay someone for something you can get for free? Presumably because everything is in one place and you can get some support from the vendor. Of course, the quality of support greatly depends on the vendor.

So “free” refers to freedom to use the code and not necessarily zero cost. Think “free speech,” not “free beer.”

Open Source code is:

• Subject to the terms of an Open Source license, in many cases the GNU Public License (see below).

• Subject to critical peer review. As an Open Source programmer, your code is out there for everyone to see, and the Open Source community tends to be a very critical group. Open Source code is subject to extensive testing and peer review. It’s a Darwinian process in which only the best code survives. “Best” of course is a subjective term. It may be the best technical solution, but it may also be completely unreadable.

• Highly subversive. The Open Source movement subverts the dominant paradigm, which says that intellectual property such as software must be jealously guarded so you can make a lot of money out of it. In contrast, the Open Source philosophy is that software should be freely available to everyone for the maximum benefit of society. And if you can make some money out of it, that’s great, but it’s not the primary motivation. Richard Stallman, founder of the FSF, is particularly vocal in advocating that software should not have owners (see Appendix B).

Many leading vendors of Open Source software give their programmers and engineers company time to contribute to the Open Source community. And it’s not just charity, it’s good business! Even Microsoft is jumping on the Open Source bandwagon, if perhaps reluctantly. Among other initiatives, the company hosts a number of projects at github.com, although I suspect most of these are designed to work with their commercial, proprietary products.

Portable and Scalable

Linux was originally developed for the Intel x86 family of processors, and most of the ongoing kernel development work continues to be on x86s. Nevertheless, the design of the Linux kernel makes a clear distinction between processor-dependent code, which must be modified for each different architecture, and code that can be ported to a new processor simply by recompiling it. Consequently, Linux has been ported to a wide range of 32-bit, and more recently 64-bit, processor architectures including:

• Motorola 68k and its many variants

• Alpha

• Power PC

• ARM

• Sparc

• MIPS

to name a few of the more popular. So whatever 32-bit, or even 64-bit, architecture you’re considering for your embedded project, chances are there’s a Linux port available for it, and a community of developers supporting it.

A typical desktop Linux installation runs into 10 to 20 gigabytes of disk space, and requires a gigabyte of RAM to execute decently. By contrast, embedded targets are often limited to 64 Mbytes or less of RAM, and perhaps 256 Mbytes of flash ROM for storage. Fortunately, Linux is highly modular. Much of that 10 gigabytes represents documentation, desktop utilities, and options like games that simply aren’t necessary in an embedded target. It is not difficult to produce a fully functional, if limited, Linux system occupying no more than 2 Mbytes of flash memory.

The kernel itself is highly configurable, and includes reasonably user-friendly tools that allow you to remove kernel functionality not required in your application.

Where is Linux Embedded?

Just about everywhere. A 2015 report from VDC Research claimed that in 2012 Linux powered 56.2% of embedded device unit shipments worldwide. That is expected to grow to 64.7% by 2017. Products incorporating Linux range from cell phones, personal digital assistants, and other handheld devices through to routers and gateways, thin clients, multimedia devices, and TV set-top boxes, to robots and even ruggedized VME chassis suitable for military command and control applications.

One of the first, and perhaps best known, home entertainment devices to embed Linux is the TiVo Personal Video Recorder that created a revolution in television viewing when it was first introduced in 2000. The TiVo is based on a Power PC processor and runs a “home grown” embedded Linux port that uses a graphics rendering chip for generating video.

Half the fun of having a device that runs Linux is making it do something more, or different, than the original manufacturer intended. There are a number of websites and books devoted to hacking the TiVo. Increasing the storage capacity is perhaps the most obvious hack. Other popular hacks include displaying weather, sports scores, or stock quotes, and setting up a web server.

Applications for embedded Linux are not limited to consumer products. It is found in point of sale terminals, video surveillance systems, robots, and even in outer space. NASA’s Goddard Space Flight Center developed a version of Linux called FlightLinux to address the unique problems of spacecraft onboard computers. On the International Space Station, Linux-based devices control the rendezvous and docking operations for unmanned servicing spacecraft called Automatic Transfer Vehicles.

Historically, telecommunications carriers and service providers have relied on specialized, proprietary platforms to meet the availability, reliability, performance, and service response time requirements of telecommunication networks. Today, carriers and service providers are embracing “open architecture” and moving to COTS (commercial off-the-shelf) hardware and software in an effort to drive down costs while still maintaining carrier class performance.

Linux plays a major role in the move to open, standards-based network infrastructure. In 2002, the Open Source Development Lab (OSDL) set up a working group to define “Carrier Grade Linux” (CGL) in an effort to meet the higher availability, serviceability, and scalability requirements of the telecom industry. The objective of CGL is to achieve a level of reliability known as “five nines,” meaning the system is operational 99.999% of the time. That translates into no more than about 5 minutes of downtime in a year.

Open Source Licensing

Most End User License Agreements for software are specifically designed to restrict what you are allowed to do with the software covered by the license. Typical restrictions prevent you from making copies or otherwise redistributing it. You are often admonished not to attempt to “reverse-engineer” the software.

By contrast, an Open Source license is intended to guarantee your rights to use, modify, and copy the subject software as much as you like. Along with the rights comes an obligation. If you modify and subsequently distribute software covered by an Open Source license, you are obligated to make available the modified source code under the same terms. The changes become a “derivative work,” which is also subject to the terms of the license. This allows other users to understand the software better, and to make further changes if they wish.

Open Source licenses are called “copyleft” licenses, a play on the word copyright intended to convey the idea of using copyright law as a way of enhancing access to intellectual property like software, rather than restricting it. Whereas copyright is normally used by an author to prevent others from reproducing, adapting, or distributing a work, copyleft explicitly allows such adaption and redistribution provided the resulting work is released under the same license terms. Copyleft thus allows you to benefit from the work of others, but any modifications you make must be released under similar terms.

Arguably, the best-known, and most widely used, Open Source license is the Gnu general public license (GPL) first released by the FSF in 1989. The Linux kernel is licensed under the GPL. But the GPL has a problem that makes it unworkable in many commercial situations. Software that makes use of, or relies upon, other software released under the GPL, even just linking to a library, is considered a “derivative work,” and is therefore subject to the terms of the GPL and must be made available in source code form.

To get around this, and thus promote the development of Open Source libraries, the FSF came up with the “Library GPL.” The distinction is that a program linked to a library covered by the LGPL is not considered a derivative work, and so there is no requirement to distribute the source, although you must still make the source available to the library itself.

Subsequently, the LGPL became known as the “Lesser GPL,” because it offers less freedom to the user. So while the LGPL makes it possible to develop proprietary products using Open Source software, the FSF encourages developers to place their libraries under the GPL in the interest of maximizing openness.

At the other end of the scale is the Berkeley Software Distribution (BSD) license, which predates the GPL by some 12 years. It “suggests,” but does not require, that source code modifications be returned to the developer community, and it specifically allows derived products to use other licenses, including proprietary ones.

Other licenses—and there are quite a few—fall somewhere between these two poles. The Mozilla Public License e.g., developed in 1998 when Netscape made its browser open-source, contains more requirements for derivative works than the BSD license, but fewer than the GPL or LGPL. The Open Source Initiative (OSI), a nonprofit group that certifies licenses meeting its definition of Open Source, lists 89 certified licenses on its website as of October 2016.

Most software released under the GPL, including the Linux kernel, is covered by version 2 of the license, which was released in 1991, coincidentally the same year that Linux was born. FSF released version 3 of the GPL in June of 2007. One of the motivations for version 3 was to address the problem of “tivoization,” a term coined by Richard Stallman. It turns out that Tivo will only run code with an authorized digital signature. So, even though Tivo makes the source code available in compliance with the GPL, modifications to that code won’t run.

Stallman considers this circumventing the spirit of the GPL. Other developers, including Linus Torvalds, see digital signatures as a useful security tool, and wouldn’t want to ban them outright. The debate continues. In any case, the kernel itself is unlikely to move up to version 3 any time soon, as it would require the agreement of literally thousands of developers.

Legal Issues

Considerable FUD3 has been generated about the legal implications of Open Source, particularly in light of SCO’s claims that the Linux kernel is “contaminated” with its proprietary Unix code. The SCO Group, formerly known as Santa Cruz Operations, acquired the rights to the Unix System V source code from Novell in 1996, although there is some dispute as to exactly what SCO bought from Novell. In any case, SCO asserted that IBM introduced pieces of SCO’s copyrighted, proprietary Unix code into the Linux kernel, and is demanding license fees from Linux users as a consequence.

Ultimately, SCO’s case collapsed and the company filed for Chapter 11 bankruptcy in 2007. But all of a sudden there is serious money to be made by fighting over Open Source licensing issues. The upshot is that embedded developers need to be aware of license issues surrounding both Open Source and proprietary software. Of necessity, embedded software is often intimately tied to the operating system, and includes elements derived or acquired from other sources. While no one expects embedded engineers to be intellectual property attorneys, it is nevertheless essential to understand the license terms of the software you use and create, to be sure that all the elements “play nicely” together.

And the issue cuts both ways. There are also efforts to identify violations of the GPL. The intent here is not the make money, but to defend the integrity of the GPL by putting pressure on violators to clean up their act. In particular, the GPL Violations Project has “outed” a dozen or so embedded Linux vendors who appear to have played fast and loose with the GPL terms. According to Harald Welte, founder of the GPL Violations Project, the most common offenders are networking devices such as routers, followed by set-top boxes and vehicle navigation systems.

Open source licensing expert Bruce Perens has observed that embedded developers seem to have a mindset that “this is embedded, no one can change the source—so the GPL must not really apply to us.” It does.

Alternatives to Linux

Linux isn’t the solution to every problem. There are many devices that are simply too resource constrained to run Linux. A majority of the 50,000,000,000 devices expected to connect to the Internet of Things will likely be too small to run Linux. Nevertheless, these devices can benefit from running an operating system. Whereas Linux is a full-blown operating system with a kernel, device driver model, file systems, network stack, and a large collection of utilities, many alternatives are really just the kernel that handles scheduling, synchronization, and interrupts, and perhaps manages memory.

There are a great many operating systems out there, both open source and proprietary, but two stand out in my mind as being worth discussing.

FreeRTOS

FreeRTOS is an open source project designed to be small and simple. It was originally written by Richard Barry, who subsequently formed Real Time Engineers Ltd. to provide commercial support. It has been ported to 35 microcontrollers. FreeRTOS is distributed under the GPL version 2 with an exception and a restriction. The exception allows you to retain your application code as proprietary, even when it is linked to the FreeRTOS kernel. The restriction is that “FreeRTOS may not be used for any competitive or comparative purpose, including the publication of any form of run time or compile time metric, without the express permission of Real Time Engineers Ltd.” The claim is that “this is the norm within the industry and is intended to ensure information accuracy.”

FreeRTOS is also available under a commercial license, called OpenRTOS, with additional features:

• A warranty. The open source version has no warranty.

• Professional technical support. The open source version is supported by a volunteer online community.

• IP infringement protection. Real Time Engineers Ltd. will defend commercial license holders against charges of patent or copyright infringement.

• No requirement to open source your changes to the RTOS kernel.

Another commercial product, SafeRTOS, extends FreeRTOS into the realm of safety critical systems, having been certified under a number of international safety standards. SafeRTOS is qualified for use in medical devices. Both OpenRTOS and SafeRTOS were developed by WITTENSTEIN High Integrity Systems in collaboration with Real Time Engineers Ltd.

FreeRTOS itself is just the kernel. On top of that is a whole ecosystem of packages called FreeRTOS+. These packages, some of which are open source while others are proprietary, support features such as:

• FAT file system

• UDP/IP

• TCP/IP

• SSL and TLS

• File and device I/O

• Board support packages

The kernel itself consists of only 3 or 4 C files, and can take as little as 6 Kbytes of storage. It supports both preemptive and cooperative multitasking. Synchronization mechanisms include semaphores, mutexes, and message queues. A “tickless” mode of operation is available for low power applications.

A number of chip vendors supply their own Windows-based development environments for FreeRTOS, mostly derived from Eclipse. The one I happen to be familiar with is LPCExpresso from NXP Semiconductor.

MicroC/OS

MicroC/OS is a contraction of Micro-Controller Operating System. It was initially developed in 19914 by Jean Labrosse, who subsequently wrote a series of articles in Embedded Systems Programming magazine about it, as well as a book, µC/OS The Real-Time Kernel. MicroC/OS is not an open source project, although it is free to use for noncommercial projects. Labrosse subsequently started a company, Micrium, Inc., to commercialize MicroC/OS.

Like FreeRTOS, MicroC/OS itself is just the kernel, which ranges in size from 6 KB to 24 KB depending on what features are enabled. Additional packages support various forms of networking, file systems, profiling, and tracing. Synchronization mechanisms include semaphores, mutexes, message queues, and event flags.

Two versions are currently supported: MicroC/OS II and MicroC/OS III. They differ primarily in the scheduler implementation. MicroC/OS II uses a clever, very efficient scheduling algorithm that limits the number of tasks to 255, each of which has a unique priority in the range of 0 to 255. Because each task has a unique priority, there is no notion of round robin scheduling.

MicroC/OS III, by contrast, allows an unlimited number of tasks, limited only by the memory of the target system, with an unlimited range of priorities. It also supports round robin scheduling among tasks of equal priority.

A personal note. I first encountered MicroC/OS, at that time called µCOS, in Jean’s three-part series in Embedded Systems Programming. After downloading and playing around with it, I was so impressed I decided to use it as the basis for a class on real-time programming. I found the source code to be the most complete and readable C code I have ever come across. Every function has an extensive header comment that describes what the function does, what the arguments are, and what the return value is. Symbol names are sufficiently long, and contain enough vowels, to be easily understandable even if the words are not fully spelled out. This is the way code should be written!

Now that we have some idea of the embedded and real-time space, how Linux fits into it, and what the alternatives might be, Chapter 2, Installing linux describes the process of installing Linux on a workstation.

Resources

Linux resources on the web are extensive. This is a list of some sites that are of particular interest to embedded developers.

linuxfoundation.org – Founded in 2000, the Linux Foundation is a nonprofit consortium dedicated to fostering the growth of Linux. Among its activities are a number of conferences around the world, and a full range of training courses.

embedded.com – The website for what used to be Embedded Systems Design magazine. This site is not specifically oriented to Linux, but is quite useful as a more general information tool for embedded system issues.

fsf.org − The Free Software Foundation. While it wasn’t called “open source” in the beginning, this is where it all really began.

kernel.org − The Linux kernel archive. This is where you can download the latest kernel versions, as well as virtually any previous version.

opensource.org − The Open Source Initiative (OSI), a non-profit corporation “dedicated to managing and promoting the Open Source Definition for the good of the community.” OSI certifies software licenses that meet its definition of Open Source.

osdl.org − Open Source Development Lab, a nonprofit consortium focused on accelerating the growth and adoption of Linux in both the enterprise and, more recently, embedded spaces. In September of 2005, OSDL took over the work of the Embedded Linux Consortium, which had developed a “platform specification” for embedded Linux.

gpl-violations.org − the GPL Violations Project was started to “raise the awareness about past and present violations” of the General Public License. According the website it is “still almost a one-man effort.”

sourceforge.net − “World’s largest Open Source development website.” Provides free services to open source developers including project hosting and management, version control, bug and issue tracking, backups and archives, and communication and collaboration resources.

uclinux.org − The Linux/Microcontroller project is a port of Linux to systems without a Memory Management Unit (MMU).

free-electrons.com – An embedded Linux consulting and training company with a large collection of free training materials.

elinux.org – This is a very extensive wiki with a large number of links to other resources.

slashdot.org − “News for nerds, stuff that matters.” A very popular news and forum site focused on open source software in general and Linux in particular.

Alternatives to Linux

freertos.org – the site for FreeRTOS

micrium.com – the site for MicroC/OS


1Linus Torvalds once said “Software is like sex. It’s better when it’s free.” Guys, we all know sex is never free and neither is software. One way or another, you’re going to pay for it.

2http://www.catb.org/~esr/writings/cathedral-bazaar/

3Fear, Uncertainty, and Doubt.

4Interestingly, the same year Linus released the first Linux kernel.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.185.199