1. Introduction
The introduction of the Raspberry Pi 2 has ushered in exciting new opportunities. No longer is the software limited to one CPU core, but now executes on four. Memory has been doubled to 1 GB, providing a larger disk cache and resulting in better SD card performance. This also leaves room for larger applications. Having four built-in USB ports is also helpful in avoiding the need for a USB hub.
Some things have changed underneath the hood as well, like the physical address for peripherals. The address BCM2708_PERI_BASE has changed from 0x20000000 to 0x3F000000 for the Raspberry Pi 2. This means that a lot of software written for the earlier Raspberry Pi models will not work on the new Pi 2 without changes. With device tree (DT) support built into Raspbian Linux, it is now a simple matter to automatically detect this. Consequently, the included librpi2 GPIO class detects this automatically.
The main purpose in this book is to exploit the Raspberry Pi 2. To that end, this book comes with software libraries and tools to make doing things on your Pi easier. The provided C++ libraries will make it possible to access the GPIO and other peripherals with only a few lines of code. Command line tools like the gp command will give you simple and ready access to list your GPIO state and to make changes. A useful feature of gp includes the ability to blink a pin, so that you can verify wiring with an LED attached. The gp command also allows a pin to be monitored for changes, making testing inputs easier. All installed tools include their own self-help display (command option -h).
A Raspberry Pi designer is handicapped without a logic analyzer for tracing events on GPIO pins. For this reason, I felt strongly that the reader should have one to use. This book provides you with a software-based logic analyzer for free (it requires no extra hardware). The PiSpy logic analyzer is able to see all GPIO inputs and outputs and record events to a file. The GtkWave utility is used to display the captured events graphically.
The C++ programming language was chosen for this book to make your life easier. The C programmer need not fear this because Ive kept to the basics without using hairy language elements. Many Arduino students use C++ every day without knowing it. The C++ standardization efforts of the last decade have had a very positive effect on g++ and glibc. This and the availability of the Standard Template Library (STL) in glibc make it a very productive environment in which to program.
Ive used a hybrid approach to the C++ code presented. I continue to use the well-understood standard I/O (stdio) routines like printf() so that the code is easy to read and familiar. I know that C++ purists will tsk tsk about this, but this is a practical book rather than a computer science text.
Of particular interest, are the included software utilities and the C++ library librpi2. Tools and libraries are most useful when you have the documentation in your hands.
Overview
presents a simple Raspberry Pi 2 workstation construction project. Using a short plank of wood, you can mount your Raspberry Pi 2 and a breadboard on it. I find that being organized this way helps considerably.
presents The Matrix construction project. It uses an LED matrix to produce a display that can provide instant display of CPU utilization for each of its cores. It also displays bar graphs for memory and disk I/O utilization. If you dont want to construct this project, you can install the htop utility instead.
cover an exciting tool that is also included in the books software, the PiSpy logic analyzer tool. This provides a simple-to-use logic analyzer for your Pi, requiring no additional hardware. It allows you to inspect and capture GPIO signal events, whether the GPIO is an input or output. Through the use of the direct memory access (DMA) peripheral, PiSpy can capture signal changes up to about 12 MHz. The GtkWave command is then optionally invoked automatically to display the captured data.
is dedicated to the GPIO gp command. All utilities provided with this book use proper command line option processing as used by the standard Unix commands. The chapter begins with a brief discussion of what Unix command line conventions are and then works through the gp command use cases with explanations.
introduces the piclk utility, which can display or alter the configuration of the general purpose clock (or PWM clock). Experiments with the clock are included.
expands on the clock concept by examining the Pulse Width Modulation (PWM) peripheral. The pipwm utility and its options are explored in combination with PiSpy. PiSpy illustrates the effect of different configuration settings.
takes a light look at what GPIO is and the Complementary MOS (CMOS) technology that it is made from. Electronics students will find the examination helpful in explaining what CMOS is and avoiding some pitfalls in interfacing. Additionally CMOS inputs are examined with a brief discussion of chip static protection schemes.
provides a detailed look at the PiSpy logic analyzer and describes the various features of its application.
looks at GPIO input processing, focusing particularly on the problem of debouncing input signals from buttons and switches.
is a software-focused chapter. It provides a fast track to using C++ and the STL in a very easy and pragmatic way. The STL provides the programmer great benefits in programming time and reliability. In this chapter we look at the most popular containers and example code fragments are provided throughout.
is the multithreaded software designed to take advantage of all four CPU cores within an efficient web server. The server is based on the libevent library behind the scenes, but C++ classes are used to provide a very simple interface. This allows you to build and customize your own web server with very little mainline code. The provided piweb project can be used and extended.
Appendixes A through C document the librpi2 C++ classes for programming GPIO. With the provided library, it is a simple matter to directly control GPIO from your own code. The appendixes provide you with easy-to-follow documentation and examples for the various class methods supported.
Appendixes D and E describe some other C++ classes that might be useful for people wanting to work with Matrix or 7-segment displays. describes classes that have to do with gathering system performance metrics as used by the mtop utility.
provides some additional information about some of the remaining advanced classes found in librpi2.
Software for This Book
You will want to download the open source software written for this book. It is available from two sources:
The open source software is important because:
It provides you with several utilties: gp, piclk, pipwm, mtop, and PiSpy.
It includes the piweb C++ web server project.
It provides library librpi2, which can be used in your own C++ projects.
If you are using git to download the source code use the command
$ git clone https://github.com/ve3wwg/raspberry_pi2.git ./pi2
The second argument tells git to install the source code into the new pi2 subdirectory.
The gp command provides you with a first-class Unix utility to display and control the GPIO resources. This utility also includes some useful options to blink a GPIO pin, which is great for checking wiring to LEDs. Another option allows you to monitor an input to the terminal session, so you can check input wiring and so on.