Book Image

Raspberry Pi Computer Architecture Essentials

By : Andrew K. Dennis, Teemu O Pohjanlehto
Book Image

Raspberry Pi Computer Architecture Essentials

By: Andrew K. Dennis, Teemu O Pohjanlehto

Overview of this book

With the release of the Raspberry Pi 2, a new series of the popular compact computer is available for you to build cheap, exciting projects and learn about programming. In this book, we explore Raspberry Pi 2’s hardware through a number of projects in a variety of programming languages. We will start by exploring the various hardware components in detail, which will provide a base for the programming projects and guide you through setting up the tools for Assembler, C/C++, and Python. We will then learn how to write multi-threaded applications and Raspberry Pi 2’s multi-core processor. Moving on, you’ll get hands on by expanding the storage options of the Raspberry Pi beyond the SD card and interacting with the graphics hardware. Furthermore, you will be introduced to the basics of sound programming while expanding upon your knowledge of Python to build a web server. Finally, you will learn to interact with the third-party microcontrollers. From writing your first Assembly Language application to programming graphics, this title guides you through the essentials.
Table of Contents (18 chapters)
Raspberry Pi Computer Architecture Essentials
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

The microSD card – the main storage and boot device of the Raspberry Pi 2


A micro secure digital (microSD) card is a portable high performance storage medium used in a variety of electronic devices including cameras, phones and computers. You may already be familiar with them if you use one of the devices we have just listed.

Our Raspberry Pi 2 comes equipped with a microSD slot, which lets us use a microSD card as our main storage and boot mechanism. The card is therefore used in a similar manner to a hard drive on a traditional computer or portable device.

The previous Raspberry Pi models used a standard SD card, which was much larger. Therefore, the microSD card saves space on the circuit board and does not poke out as far, reducing the risk of it being broken.

When choosing a microSD card for your projects, there are a variety of brands on the market, and they come in a range of storage sizes running into the tens of gigabytes.

For the projects in this book we recommend using a card with a large amount of storage and you should look at choosing a card that is at least 8 GB in size. The NOOBS application, for example, requires a card of at least this size.

The official Raspberry Pi website provides a guide to microSD cards at http://www.raspberrypi.org/documentation/installation/sd-cards.md and is a good place to start.

We will now discuss the option of purchasing a microSD card preinstalled with the Raspbian operating system or New Out Of the Box Software(NOOBS) versus formatting and installing the operating system ourselves.

For those who really wish to understand the Raspberry Pi 2 in detail, installing the operating system from scratch may be a more rewarding experience.

Preinstalled microSD card versus creating your own

A number of websites offer microSD cards preloaded with one of the operating systems that are available for the Raspberry Pi 2. An example can be found at the Allied Electronics website at http://www.alliedelec.com/raspberry-pi-8gb-sd-card-raspberry-pi-noobs-1-4/70470344/.

These are a good solution for anybody looking to get up and running quickly or who are not comfortable installing an operating system by themselves from scratch. They are also useful for those who do not have second computer to work with in order to format a new microSD card.

The official Raspberry Pi distributions Element 14 also offer a preinstalled microSD card equipped with NOOBS, a Raspberry Pi 2 operating system boot loader. It can be found at https://www.element14.com/community/community/raspberry-pi.

The second option is to purchase a new blank microSD card and follow the instructions contained in this chapter.

It should be noted that if you do not have a home Mac or PC accessible to format a new blank microSD card, then we would recommend acquiring a preformatted card. This should come loaded with either Debian Jessie Raspbian, or the NOOBS boot loader application.

The NOOBS operating system installation manager

This book assumes that the reader will be installing the Raspbian operating system himself or herself. The simplest method for doing this is to install the NOOBS operating system installation manager onto your microSD card.

NOOBS makes the setup of your Raspberry Pi 2 easy and also provides you with a mechanism for choosing other operating systems that are compatible with the Raspberry Pi.

The official Raspberry Pi website contains an introduction and guide to NOOBS and can be found at http://www.raspberrypi.org/help/noobs-setup/.

If you already have a blank microSD card, you can download NOOBS from https://www.raspberrypi.org/downloads/noobs/.

When installing Raspbian for the first time via NOOBS you will also be presented with the raspi-config screen. This provides some handy shortcuts that allow you to do the following:

  • Expand the file system

  • Change the user password

  • Enable boot to desktop

  • Change language

  • Enable the camera if you have purchased the peripheral

  • Add to Rastrack Raspberry Pi Map

  • Overclock your Raspberry Pi

  • Explore some advanced configuration options

If you choose not to install Raspbian via NOOBS, then the following section will guide you through the process. If you are using NOOBS you can skip to the Raspbian installation wrap-up section.

Downloading the latest version of Raspbian

Your first task will be to download the Raspbian operating system from the official Raspberry Pi website at https://www.raspberrypi.org/downloads/raspbian/.

There are several options for downloading Raspbian including an older version of the OS based on Debian Wheezy. We recommend grabbing the latest version, and it can be obtained over either BitTorrent or via a ZIP file.

Note

The latest version as of September 2015 is Raspbian Jessie

Once you have obtained a copy of the operating system you can move onto formatting your microSD card and installing the image.

Setting up your microSD card and installing the Raspbian operating system

The Raspbian installation process involves two steps:

  • Formatting the microSD card to the FAT file system

  • Copying the Raspbian image to the card

It is important that we quickly look at what File Allocation Table (FAT) is and why we need it.

FAT is a method for defining which sectors of a disk or microSD card files are stored in and which sectors on the disk are free to have new data written to them.

The standard has its origins in the 1970s for use on floppy disks and was developed by Bill Gates and Marc McDonald.

You can read more about FAT here: https://en.wikipedia.org/wiki/File_Allocation_Table.

Due to its simplicity of implementation and robustness, this standard is still used on SD and microSD cards today. Therefore, it is the format you will need in order to install the Raspberry Pi's operating system onto your microSD card.

Due to its widespread adoption you may find and microSD card you purchase is already formatted to FAT.

We recommend, however, formatting any new cards you purchase to ensure you do not encounter any problems.

The official Raspberry Pi website provides handy how-to guides for the three major operating systems on how to format and install the Raspbian image.

You can read an up-to-date overview of the installation procedure at https://www.raspberrypi.org/documentation/installation/installing-images/README.md.

The following are guides to formatting an SD card for your particular operating system:

Having completed installing the operating system we can now look at some final configuration before exploring some interesting features of Raspbian.

Raspbian installation wrap-up

The following section assumes you have your Raspberry Pi connected to a monitor and with a keyboard and mouse available. It also assumes you have your configuration set to boot to desktop and have powered up and logged into your device.

You should at this point connect your device to your home router. If you are planning on using Wi-Fi, read on.

Now that you have successfully installed Raspbian you should see the Linux desktop.

Tip

If you do not see the desktop, but the command line instead, you can type startx to start the GUI.

This desktop contains icons in the top menu linking to a number of programs installed by default with the operating system.

One important icon is the link to LXTerminal. This icon launches the Linux terminal window. Click on this icon and you should see the command line load.

The following tasks in this section can all be performed in this window.

As a handy shortcut you can also load the raspi-config application at any time by typing the following command:

sudo raspi-config

Tip

You can read about the sudo command here: https://www.sudo.ws/.

If you update settings in this manner you may need to reboot the Raspberry Pi for them to take affect.

Check SSH is running

In order to connect to our Raspberry Pi 2 from another device via a terminal window we need to ensure that the Secure Shell(SSH) server is up and running. SSH is the default mechanism for secure communication between our Linux machines. If you used NOOBS to install the OS you may have configured the SSH server at this point via the advanced options. We can check that the SSH service is running successfully as follows.

Open up a terminal window from the Raspbian desktop and type the following command:

ps aux | grep sshd

The following sshd process should be displayed. This tells us the services are up and running:

root    2017  0.0  0.3   6228  2892 ?        Ss   15:13   0:00 /usr/sbin/sshd

If the SSH process does not appear, it is simple to start it. Enter the following command into the terminal:

sudo /etc/init.d/ssh start

After you have executed this command try running the following again and check that the sshd process is now running:

ps aux | grep sshd

By default, to login to the Raspberry Pi 2 over SSH you will be prompted for a username and password. If you have not changed this the username is pi and the password is raspberry.

In addition to the username and password method, we can also use an RSA key to authenticate and gain access to the Raspberry Pi over a network. We discuss this process next.

RSA key generation for SSH

RSA keys are a useful method to login to the Raspberry Pi. They remove the need to enter a username and password and lock down access to a handful of hardware devices.

A RSA key consists of two parts: a public and private key. A public key can be shared with anyone and any machine with that key on and can in theory let you have access. Therefore, if you purchase more Raspberry Pis, you can place your public key on each, and negate the need to remember multiple passwords.

The private key portion should be kept secret and is located on the machine you will use to access your Raspberry Pi from. For security reasons it is best to keep the private key on a single device.

You can read more about RSA keys and the cryptographic theory behind them here: https://en.wikipedia.org/wiki/RSA_(cryptosystem).

One important piece of information you will need is the IP address or hostname of your Raspberry Pi.

Tip

If Wi-Fi is disabled/not connected, you can enable it via the Menu | Preferences | WiFi Configuration link on the desktop

You can obtain this by looking at your local home router, or by running the following commands in the terminal window of Raspbian.

For the IP address, run this command:

sudo ip addr show

Where to look depends on whether you are using a wireless or wired connection. Ethernet can be found usually at eth0 and wireless at wlan0.

If you would prefer to see the hostname you can run this command:

sudo hostname

Make a note of this information, as you will need it to connect to the Raspberry Pi 2 from your second device.

If you are using a Mac or Linux to SSH into the Raspberry Pi you can generate the RSA key via the terminal window using the following steps. If you are using Windows, skip to the relevant section further on in this chapter.

Linux and Mac RSA key generation

Start by opening up your Mac or Linux terminal. From the command line run the following command:

ssh-keygen -t rsa -b 4096 -C "username"

You should replace the username with your own. A message similar to the following will be displayed:

Generating public/private rsa key pair.

Following this you will see a prompt:

Enter file in which to save the key (/Users/username/.ssh/id_rsa):

You can press enter here and the key will be saved to the path listed in the prompt. Note that it may look slightly different to the preceding example depending on your username and operating system.

Following this, you have the option of adding a password to the RSA private key. These prevent unauthorized users of your second computer from accessing the Raspberry Pi.

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Once you have added a passphrase, the key generation process is complete. You should now see your key's fingerprint:

Your identification has been saved in /Users/username/.ssh/id_rsa.
Your public key has been saved in /Users/username/.ssh/id_rsa.pub.
The key fingerprint is:

Now we have our key, we need to load it into ssh-agent and then copy it onto the Raspberry Pi 2.

Adding the key to the agent can be done with the following command:

ssh-add ~/.ssh/id_rsa

This now allows the SSH command to use your key when trying to authenticate.

Tip

If your ssh-agent isn't running, you can use the following command to start it:

eval "$(ssh-agent –s)"

Before we can SSH into the Raspberry Pi we need to add the public key you created to it.

Thankfully, we can do this in a single command using the Raspberry Pi's default username and password, or if you changed it, that username and password. Run the following command from your terminal. Remember to swap the IP address in the command below with the IP address or hostname you recorded earlier:

cat ~/.ssh/id_rsa.pub | ssh user@ip'cat >> .ssh/authorized_keys'

With the public key now located on the Raspberry Pi you can attempt to SSH in.

Tip

If the .ssh directory and authorized_keys file do not exist on your Raspberry Pi you can create them under the pi user. To create the directory type mkdir .ssh and to create an empty file in this directory type touch authorized_keys.

This can be achieved using the SSH command along with your username and IP address:

ssh –A username@ip

You will notice that you can now login to the computer remotely and will be presented with a terminal window prompt.

Windows RSA key generation

In order to connect to the Raspberry Pi from a Windows device you will need three pieces of software, Pageant, PuTTY, and PuTTYgen.

Start by downloading PuTTYgen from the following URL: http://the.earth.li/~sgtatham/putty/latest/x86/puttygen.exe.

Next download PuTTY from this URL: http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe. Like PuTTYgen, this is also an executable you can run from your desktop.

Then finally grab Pageant from this URL: http://the.earth.li/~sgtatham/putty/latest/x86/pageant.exe.

We will start by generating our public and private key pair. Open up the PuTTYgen executable.

Once you have this open, run through the following steps:

  1. Set the key type as SSh-2 RSA.

  2. Click on the Generate button.

  3. You'll now be asked to move your mouse around to generate some random data.

  4. Give your key a passphrase.

  5. Next, use the Save private key button to save the generated private key.

  6. Finally, click the Save public key button.

Our next task is going to be to add the key to the Raspberry Pi 2's authorized_keys file.

Note

You will need the Raspberry Pi's login details and IP for the next steps.

Open up the PuTTY executable.

From the Category list on the left, select Session if this is not already open. Now add the following details:

  1. In the Host Name field, enter the Raspberry Pi's IP address.

  2. Set the Port to 22.

  3. Select the SSH radio button.

  4. You can now optionally save these details for future connections.

  5. Click the Open button.

If this is your first connection, you should now see a popup appear called PuTTY Security Alert. Click the Yes button to move on.

If you entered the connection details successfully, the PuTTY terminal window will now present you with a login prompt for the Raspberry Pi.

You will need to enter in the login name here. By default, this is set as pi; however, you may have changed it if you setup the device via NOOBS.

Following this you will be prompted for the password. This will be raspberry by default, or whichever password you set if you configured Raspbian via NOOBS.

If your login was successful, you should now see the Raspbian command line prompt.

We now want to edit the authorized_key file. We can use the default text editor installed by Raspbian to edit this file. If the file does not exist, you can create it and the .ssh directory.

It is located under the pi user account:

.ssh/authorized_keys'

Copy and paste the public key you saved from PuTTYgen into this file.

Save the file and exit it.

You can now logout of the Raspberry Pi.

The final tool we need to test is the Pageant application. This is our windows SSH authentication agent.

Open up the Pageant application. It should be available in the system tray in Windows.

Next, follow these steps:

  1. Right click on the Pageant icon.

  2. Select Add Key from the menu.

  3. A pop-up will display listing any keys you have.

  4. Select the Add Key button.

  5. From the pop-up window, select the private key you generated with PuTTYgen.

  6. Click the Open button.

  7. Next, you should be prompted to enter the passphrase for your key.

  8. Fill this in and click OK.

  9. You should now see it listed in the Pageant Key List window.

  10. You can now close the key list.

Whenever you try and access the Raspberry Pi via PuTTY, all you have to enter is the username and host.

Open up PuTTY and connect to the Raspberry Pi again. You should now see you are logged in without a password prompt appearing. This is because you have authenticated your private key against your public key, which was added to the authorized_keys list.

Note

On the first time logging in over SSH you may see a security alert/prompt. You can select Yes to this.

This completes setting up access to the Raspberry Pi remotely. We can now add a static IP address and run some diagnostic tests on our device.

We shall now move onto running some tests on the Raspberry Pi via Raspbian. These can be performed either directly on the Pi via the terminal window, or over the SSH connection you just created.

Assign a static IP to your Raspberry Pi 2

Assigning a static IP address to your Raspberry Pi means that when you switch it on or off a new IP will not be assigned. Instead, it will always contain the same IP address, meaning you do not have to hunt down the value assigned to it by the DHCP server each time you reboot.

To start with, check the IP address range on your router and find a free IP address. You will also need the subnet mask and the default gateway.

Next, we need to assign the free address to the Raspberry Pi. Editing the interfaces file can do this:

sudo nano /etc/network/interfaces

In the open file you will need to locate the line that specifies eth0 or wlan0 depending on whether you are wired or wireless. For example:

iface eth0 inet dhcp

Change the value dhcp to static:

iface eth0 inet static

Once this is done we need to add three lines directly below it specifying the IP address we wish to assign, netmask, and gateway. You should have these values from checking your router earlier.

Note

You can always check the gateway and netmask address directly on the Raspberry Pi by typing: netstat –rn.

Paste these in below the interface. An example is show here:

address 192.168.1.132
netmask 255.255.255.0
gateway 192.168.1.1

Save the file using Ctrl + X and press Y to save.

We do not need to reboot the Raspberry Pi to apply these changes, but can stop and start the network interface using the following commands:

sudo /etc/init.d/networking stop
sudo /etc/init.d/networking start

If you now run the command from earlier to check the IP address, you should see it is the new value you assigned:

sudo ip addr show

In the preceding example we used the nano text editing tool. We shall now look at some other options for editing files.