Hacking Raspberry Pi (2014)
Part I. Hardware Foundations
Chapter 1. What Is the Raspberry Pi?
The Raspberry Pi, with Pi pronounced pie, is a $35 personal computer about the size of a credit card. No kidding—the Raspberry Pi development team literally used a credit card as a template when they designed the Pi’s printed circuit board (PCB).
The Pi, or RasPi, or RPi (users enjoy creating nicknames for the device) is the brainchild of the Raspberry Pi Foundation (http://raspberrypi.org), a charity based in the United Kingdom (UK) and founded by Broadcom hardware architect Eben Upton, along with some of his esteemed associates at Broadcom, Cambridge University, and other corporate and educational organizations.
Historically, computer science curricula both at the K-12 and even collegiate levels—if there is any curricula at all—tend to focus more on theory than on practical application of computing concepts. Eben and the Foundation conceived the Raspberry Pi in 2006 as a way to make computer science more accessible to students.
Eben and I arose from the same generation of programmers, which is to say we came of age during the early 1980s and cut our teeth learning Beginner’s All-Purpose Symbolic Instruction Code (BASIC) programming on microcomputer platforms such as the Amstrad CPC, Commodore 64, Tandy TRS-80, and others.
The Raspberry Pi was intended to be the cultural successor to the Acorn BBC Micro personal computer that was extremely popular in the UK during the 1980s (see Figure 1.1).
FIGURE 1.1 The BBC Micro personal computer of 1981 was the prototype for the Raspberry Pi of 2012.
The BBC Micro shipped with a 2 MHz MOS Technology 6502 central processing unit (CPU). Later models, specifically the Archimedes, introduced the Acorn RISC Machines (ARM) processor. Believe it or not, the ARM processor platform is still alive and well in the 21st century; its application is mainly targeted to mobile phones and tablet computers. The Raspberry Pi is equipped with an ARM1176JZF-S (often abbreviated as ARM11) CPU; we’ll learn much more about Pi hardware in Chapter 2, “Hardware Components Quick Start.”
Note: An ARM and a Leg
For CPU devotees in my readership, allow me to tell you that the ARM11 is a 32-bit microprocessor that uses the Reduced Instruction Set Computing (RISC) processing architecture.
Why the Pi?
Many consider the Raspberry Pi to be an ideal platform for teaching both kids and adults how computer science works because it requires minimal investment. Any interested individual can learn not only how to program computer software, but also to work directly with electronics and computer hardware. If an experiment goes wrong and the Pi becomes inoperable, then the student is out only $35 as opposed to hundreds or thousands.
My use of the term computer science is intentional. What’s so cool about the Raspberry Pi is that we can move beyond surface-level software and interact directly with the internals of what most people consider to be a “black box.” A Raspberry Pi-based education can form the foundation of sought-after hardware and software engineering skills, which are lucrative and extraordinarily valuable in today’s global job marketplace.
To be sure, the Pi’s fan base isn’t entirely academic. There exists a devoted following of do-it-yourselfers and hardware hackers who employ the Pi as an integral part of their hardware and software hacking experiments.
How about a solar-powered weather station? Or Pi-powered night vision goggles? How do you feel about having the ability to control your home’s electronics from anywhere in the world using only your smartphone? All of these project ideas are eminently attainable at reasonable cost, thanks to the Raspberry Pi.
Note: What’s in a Name?
In case you were wondering, the name Raspberry Pi does indeed have a colorful history. Raspberry pays homage to the fruit names that played a part in early-80s microcomputing: Apple Macintosh, Tangerine Microtan 65, Apricot PC—the list goes on.
Pi actually references not the standard number but the Python programming language. Eben and the rest of the Foundation originally thought that Python would be the sole programming language supported by their tiny personal computer. As you learn later, the RasPi allows enthusiasts to write programs using a large variety of programming languages.
It is important to remember that the Raspberry Pi is a full-fledged personal computer and not just a simple microcontroller. A personal computer is a self-contained system that performs the following data processing tasks:
Input: The computer receives instructions and data from the user or application.
Processing: The computer performs preprogrammed actions upon its input.
Output: The computer displays the processing results in one or several ways to the user or application.
In addition, a personal computer typically also includes persistent storage and an operating system that features a user interface. Much more is discussed concerning these topics in Chapters 2 and 3, “A Tour of Raspberry Pi Peripheral Devices.”
Suffice to say that the Raspberry Pi does essentially all the things that your full-sized desktop or laptop computer does, albeit more slowly and on a smaller scale.
By contrast, a microcontroller is a much more specialized piece of hardware. A microcontroller is an integrated circuit that is similar to a personal computer inasmuch as it receives input, performs processing on that input, and finally generates output of some kind or another.
However, the microcontroller is set apart from the personal computer by the following three characteristics:
A microcontroller’s operation depends on precise timing: Because the microcontroller is generally a single-purpose device, there’s no driver or operating system overhead to slow down the system. Therefore, the microcontroller can perform work by using extremely precise clock cycles. This time-dependent operation is difficult to accomplish with the Pi because the Pi must access its hardware through several software layers.
A microcontroller gives the user full and direct access to hardware: As you learn in Chapter 4, “Installing and Configuring an Operating System,” most of the Raspberry Pi hardware (particularly the Broadcom BCM2835 system-on-a-chip) is proprietary. By contrast, most microcontrollers such as the Atmel Reduced Instruction Set Computing (RISC) chip at the heart of the Arduino are open source and are therefore completely accessible to users. With the Pi, we are limited to interacting with the board’s hardware components via software application programming interfaces (APIs).
A microcontroller typically has no user interface: A programmer must use an external system to send data to and receive data from a microcontroller.
A microcontroller is typically designed for a single purpose: A microcontroller is intended to perform a single task—and to do that task precisely and very well. For instance, consider an Arduino-powered weather station that senses the environment and reports on air temperature, relative humidity, barometric pressure, and so forth.
A representative Arduino microcontroller (specifically the Uno) board is shown in Figure 1.2.
FIGURE 1.2 The Uno is the flagship of the Arduino microcontroller line.
You learn in Chapter 19, “Raspberry Pi and Arduino,” how well the Raspberry Pi “plays” with the Arduino platform. In the meantime, here is a list that provides you with some of the most popular Raspberry Pi-compatible microcontrollers in use today:
Now then, let’s get down to business and formally introduce the Raspberry Pi.
As of spring 2013, the Raspberry Pi Foundation has two Raspberry Pi models, Model A and Model B. The differences between the two are shown in Table 1.1.
TABLE 1.1 Comparison of the Two Raspberry Pi Models
Note: History, History, All Around Me
Even the Raspberry Pi nomenclature pays tribute to the British Broadcasting Company (BBC) Micro personal computer. As it happens, the Micro BBC had a Model A and Model B, with Model B offering substantially more processing horsepower than the modest Model A.
So aside from the price difference, what are the key points of distinction between Model A and Model B? In the simplest terms:
Model B has twice the RAM as Model A.
Model B has an onboard Ethernet RJ-45 jack.
Model B has an extra USB port.
Model A uses 30% as much power as Model B.
If you spend time analyzing the two models (and I certainly hope you invest in one of each and do so), you’ll notice that the printed circuit boards are indeed identical.
Instead of a redesign, the Foundation simply stripped components off Model B to make Model A. Look at the image of Model A in Figure 1.3, focusing on the lower right—see that empty socket? That’s where the Ethernet port is soldered on the Model B board.
FIGURE 1.3 Raspberry Pi board, Model A.
Also notice in this image the unpopulated pad just above the Ethernet area; this is where the Foundation soldered an SMSC LAN9512 integrated circuit (IC) that controls the Ethernet jack in Model B.
Because the extra $10 buys you so much more computer, I focus on Model B exclusively in this book. The good news for Model A owners, however, is that all of the software, hardware, and programming we undertake here can be performed on both models.
Note: Why Model A?
The question probably came to your mind, “Why would someone purchase Model A when you get so much more ‘bang for your buck’ with Model B?” The answer probably has something to do with power consumption. Because the power footprint of Model A is so tiny, Pi hackers can leverage the Model A platform for low-energy or solar-powered projects much easier than they can with Model B.
One more thing about the Model B boards specifically: As of spring 2013, the Raspberry Pi Foundation released two revisions to the Model B PCBs. You can tell at a glance which board revision you have in front of you by inspecting the light-emitting diode (LED) bank to the right of the USB port(s). Check it out:
If the first status LED is labeled OK, you have a Revision 1 board.
If the first status LED is labeled ACT, you have a Revision 2 board.
You learn more about the Pi’s status LEDs in Chapter 2.
Is the Raspberry Pi Open Source?
A more important question to have answered before asking whether or not the Raspberry Pi is open source is “What does open source mean, and why should I care?”
Open source refers to hardware and/or software that is manufactured and given away free of charge with all intellectual property rights intact. For instance, the open source Linux operating system allows the general public to download, modify, improve, and release the underlying source code.
The term open source applies to hardware as well. For example, the schematics for the Arduino microcontrollers are freely available at the Arduino website (http://is.gd/VDVQfF); therefore, anybody in the world is allowed to analyze and understand the PCBs at a fundamental level.
Why do people invest time and money in developing open source hardware and software, only to release it to the public for free? Essentially, open source proponents are big fans of free information interchange.
Its open architecture is one important reason why Linux is considered to be one of the most secure operating systems in the world. When security vulnerabilities are identified, the Linux community can delve into the source code to identify and resolve the problem for the benefit of all Linux users across the world.
For comparison purposes, think of the Microsoft Windows and Apple OS X desktop operating systems. As you probably know, these OS platforms are proprietary, which means the general public cannot reverse-engineer the software to examine its underlying source code. The closed nature of proprietary software presents information security problems because only the software vendors themselves can resolve vulnerabilities that crop up in their products.
In conclusion, open source hardware and software offers increased security because the community can identify and correct vulnerabilities quickly. Open source architecture also lends itself to education because there are no proprietary, hidden components that bar learning. Finally, you can’t beat the price of open source components—much of it is free, as previously discussed.
Open Source Licenses
Open source software is typically released under a license agreement called the GNU General Public License (http://is.gd/7s17wU), also called the GPL. The gist of the license is that anybody can download and use GPL software to their heart’s content and for free. Users are also welcome to modify the software in any way that they see fit, as long as they release their modified version under the GPL.
Note: GNU Who?
GNU, commonly pronounced guh-NU, is a recursive acronym that stands for “GNU’s Not Unix.” This is a super-geeky reference to the Unix operating system, the proprietary precursor to Linux. Incidentally, a gnu (pronounced nu) is a large, dark antelope that is also known as a wildebeest.
Open Source and the Raspberry Pi
The answer to the kernel question, “Is the Raspberry Pi open source?” is...well...complicated.
The Raspberry Pi runs variants of the Linux (also called GNU/Linux) operating system, which we’ve already established are free and open source. However, the “guts” of the RasPi’s hardware—its Broadcom BCM2835 system on a chip (SoC)—is proprietary and closed source.
Remember that Eben and some other members of the Raspberry Pi Foundation have close ties to Broadcom. It’s awfully nice of Broadcom to license its SoC for use in the Pi. The only downside, as I said, is that the intellectual property behind the Broadcom SoC is confidential.
If there is a silver lining to the Raspberry Pi closed source hardware situation, it is that in late 2012 Broadcom open sourced all of the ARM11 code for the Pi. What this means, especially with reference to the VideoCore graphics processing unit (GPU), is that the community can build their own device drivers that offer much more speed and optimization as compared to the default Broadcom drivers.
Upon closer inspection, however, the situation is much more complicated. The long story short is that Broadcom offers a GPL-licensed “shim” driver that cooperates input and output between the user and the CPU/GPU. For instance, the VideoCore IV driver itself consists of a proprietary, Broadcom-supplied binary large object (BLOB) driver that is not user-modifiable.
Just to be clear: The driver code for the Broadcom SoC is software and is at least somewhat open source. The hardware itself and its accompanying schematics, remain a mystery to all but Broadcom.
Why do Raspberry Pi enthusiasts care about this? Well, for starters, hardware hackers who want to access the full power of the VideoCore GPU need access to its source code to access its complete capability. Imagine if you bought a jigsaw puzzle that only gave you half of the pieces—would you feel somewhat limited in what you could do with that puzzle?
How Can I Purchase a Raspberry Pi?
The Raspberry Pi Foundation and its original equipment manufacturer (OEM) partners have worked hard to provide a supply chain for the Pi. According to Eben, the original plan was to manufacture Raspberry Pi boards strictly in the UK. However, scheduling and cost problems led the Foundation to initially seek Far East fabrication partners.
However, as of spring 2013, courtesy of the Sony factory in Wales, all Raspberry Pi production now occurs in the UK.
The finished units are sold exclusively by the following organizations:
Premier Farnell/Element 14 (http://www.farnell.com/)
RS Components (http://uk.rs-online.com/web/generalDisplay.html?id=raspberrypi)
Both Farnell and RS have distribution partners spread throughout the world; you shouldn’t have trouble finding an official distribution source regardless of where you live.
The problem isn’t so much finding a source for the Pi, but actually receiving a unit! Because demand has historically exceeded supply, during late 2012 and early 2013, I have observed backorders and long wait times from both Farnell and RS for both the Model B and the Model A boards. However, enough “critical mass” should develop in the Raspberry Pi supply chain that by the time you read this, availability should be reasonable.
One alternative you might want to consider is purchasing your Pi from a reputable eBay seller. I myself have had great luck in that regard. You know the rules with supply and demand—you’ll typically pay a premium over the $35 list price for a Model B board. On the other hand, you can get productive with the Pi months before you can receive one from Farnell or RS.
The bottom line, friends, is that if you want to get your hands on a Raspberry Pi, you’ll most likely need to do so by purchasing the device from an online reseller. And then perhaps the day will come (hopefully soon) when consumer electronics stores such as RadioShack and Best Buy stock these fascinating devices.