CHAPTER 1

image

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 I’ve 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.

I’ve 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

Chapter 2 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.

Chapter 3 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 don’t want to construct this project, you can install the htop utility instead.

Chapters 4 and 9 cover an exciting tool that is also included in the book’s 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 1–2 MHz. The GtkWave command is then optionally invoked automatically to display the captured data.

Chapter 5 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.

Chapter 6 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.

Chapter 7 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.

Chapter 8 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.

Chapter 9 provides a detailed look at the PiSpy logic analyzer and describes the various features of its application.

Chapter 10 looks at GPIO input processing, focusing particularly on the problem of debouncing input signals from buttons and switches.

Chapter 11 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.

Chapter 12 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. Appendix F describes classes that have to do with gathering system performance metrics as used by the mtop utility.

Appendix G 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:

  1. It provides you with several utilties: gp, piclk, pipwm, mtop, and PiSpy.

  2. It includes the piweb C++ web server project.

  3. 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.

The piclk utility allows you display and control the general purpose clock (and PWM clock). The piclk command and PiSpy are used together for some experiments.

The pipwm utility allows you to display and control the PWM peripherals. The pipwm command and the PiSpy utility are also used together to allow you to explore the PWM peripherals.

The PiSpy utility gives you a GPIO logic analyzer. Using a Linux driver for DMA and the PiSpy user land utility, GPIO data can be captured at nearly 2 MHz or less. This utility automatically assigns and releases the DMA resources it needs for ease of use and system integrity.

If you choose to build the Matrix in Chapter 3, you will be able to visualize the system utilization in real time using the provided mtop command.

Because the Raspberry Pi 2 has four CPU cores, the piweb project demonstrates a high-performance C++ web server using libevent. This thin server will put all four CPU cores to work with sufficient web traffic. Yet, due to its C++ design, it is simple to program and enhance to meet your own needs.

Software Installation

After downloading your source code and unpacking it to a directory (directory ~/pi2 is suggested), then the process of building and installing can be started. In these directions, I assume that the software directory is in subdirectory ~/pi2.

  1. Quickly check Chapter 11 to see if you need to download a new GNU compiler. It is my hope that support for C++11 will be already included by the time you read this. If not, it is a simple matter to download and install a newer GNU compiler that does include C++11 support.

  2. $ sudo apt-get install make

  3. $ sudo apt-get install libevent-dev (needed for the piweb project)

  4. $ sudo apt-get install gtkwave (needed for the PiSpy utility)

  5. $ sudo apt-get install htop (recommended)

  6. $ cd ~/pi2

  7. Optional: When rebuilding do: $ make clobber

  8. $ make

  9. $ sudo make install

This procedure takes care of all software except for the PiSpy utility. PiSpy requires the use of a Linux kernel loadable module. For this reason, the install of PiSpy has a separate procedure:

  1. See Chapter 4 before building PiSpy.

  2. Optional: If rebuilding PiSpy, do: $ make pispy_clobber

  3. $ make pispy

  4. $ sudo make pispy_install

Installation Notes

By default, the top-level Makefile uses the default install PREFIX=/usr/local. This causes the librpi2 to include files to be installed in /usr/local/include/librpi2. The library is installed in /usr/local/lib/librpi2.a. The utilities such as gp and pipwm will be installed in /usr/local/bin.

If you need to change this, export PREFIX before performing the make and install steps. For example, if you want the software installed in /opt/local instead, you can use $ export PREFIX=/opt/local.

Don’t forget do do the same, when you need to use sudo:

$ sudo -i
# export PREFIX=/opt/local
# make install

Thank You

Thank you for buying this book and allowing me to guide you on your exploration. This book and its software will provide many hours of fun on your Raspberry Pi 2.

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

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