34,79 €
Explore Raspberry Pi's architecture through innovative and fun projects
Raspberry Pi Computer Architecture Essentials is for those who are new and those who are familiar with the Raspberry Pi. Each topic builds upon earlier ones to provide you with a guide to Raspberry Pi's architecture. From the novice to the expert, there is something for everyone. A basic knowledge of programming and Linux would be helpful but is not required.
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.
This book takes a step-by-step approach to exploring Raspberry Pi's architecture through projects that build upon each other. Each project provides you with new information on how to interact with an aspect of the Raspberry Pi and Raspbian operating system, providing a well-rounded guide.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 291
Veröffentlichungsjahr: 2016
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: March 2016
Production reference: 1170316
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78439-797-5
www.packtpub.com
Author
Andrew K. Dennis
Reviewer
Ed Snajder
Commissioning Editor
Amarabha Banerjee
Acquisition Editor
Divya Poojari
Content Development Editor
Trusha Shriyan
Technical Editor
Shivani Kiran Mistry
Copy Editor
Safis Editing
Project Coordinator
Kinjal Bari
Proofreader
Safis Editing
Indexer
Rekha Nair
Production Coordinator
Melwyn Dsa
Cover Work
Melwyn Dsa
Andrew K. Dennis is the manager of professional services software development at Prometheus Research. Prometheus Research is a leading provider of integrated data management for research and is the home of HTSQL, an open source navigational query language for RDBMS.
Andrew has a diploma in computing, a BSc in software engineering, and is currently studying for a second BSc in creative computing in his spare time.
He has over 12 years of experience working in the software industry in the UK, Canada, and the USA. This experience includes e-learning courseware development, custom CMS and LMS development, SCORM consultancy, web development in a variety of languages, open source application development, blogging about the integration of web technologies with electronics for home automation, and punching lots of Cat5 cables.
His interests include web development, e-learning, 3D printing, Linux, the Raspberry Pi and Arduino, open source projects, home automation and the use of web technology in this sphere, amateur electronics, home networking, and software engineering.
Ed Snajder is a database engineer and hardware hacker working at Jive Software. When not breaking databases and distributed data systems, Ed spends a lot of time in the community evangelizing Raspberry Pi, Arduino, and open source 3D printing. His belief is that if every child could have a Raspberry Pi, we will soon have the flying cars we've always dreamed of. He lives in Portland, Oregon, with his partner, Lindsay and his Shih-Tzus, Obi-wan and Gizmo.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Are you interested in the myriad features of your Raspberry Pi 2? From the hardware to the software, do you wish to understand how you can interact with these features?
Then this is the book for you!
The Raspberry Pi 2 is one of the latest hardware offerings in the Raspberry Pi family. With many new and improved features than previous versions, there is so much more an enthusiast can do.
This book will walk you through how you can get the most out of your device.
You will learn about how to program on the Raspberry Pi using the Assembly language, Python, and C/C++. This will include building a web server in Python and saving data to an SQLite database. Ever wondered what threads are? These are covered here too.
In addition to this, you will explore the various types of GPIO pins and how these can be used to interact with third party microcontrollers and electronic circuits.
The sound and graphics capabilities of the Raspberry Pi 2 are also experimented with through a number of projects. And to expand the Raspberry Pi's storage option, we will also set up an external HDD via USB.
Finally, the book concludes with a project that brings together many of the technologies explained throughout the chapters.
By the time you finish reading this book, you'll have a firm knowledge of the Raspberry Pi 2 and how you can devise your own projects that use its capabilities.
Chapter 1, Introduction to the Raspberry Pi's Architecture and Setup, provides an introduction to the Raspberry Pi and its hardware architecture. We will explore the various hardware components in detail, and this will provide a basis for the programming projects in future chapters. A quick guide to getting Raspbian installed and SSH enabled is also provided.
Chapter 2, Programming on Raspbian, provides an introduction to the programming languages used in this book. An explanation will be provided of which language is used and why. This chapter will also guide you through setting up the tools for Assembler, C/C++, and Python. Three introduction programs will then be walked through to give you the opportunity to test that your setup works.
Chapter 3, Low-Level Development with Assembly Language, explores programming in the Raspbian operating system using the Assembler programming language.
Chapter 4, Multithreaded Applications with C/C++, having looked at Assembler, we move up the programming hierarchy to C/C++. We learn how to write multithreaded applications and understand their usefulness. Through these applications, we learn more about the multi-core CPU of the Raspberry Pi 2.
Chapter 5, Expanding on Storage Options, offers a guide to expanding the storage options of the Raspberry Pi beyond the SD card.
Chapter 6, Low-Level Graphics Programming, shows you how to interact with the graphics hardware on the Raspberry Pi 2. Here you will learn how to draw to the screen via the frame buffer.
Chapter 7, Exploring the Raspberry Pi's GPIO Pins, shows you how to interact with electronic components using the Raspberry PI's GPIO pins. Here we look at how Python libraries can be used to simplify the process.
Chapter 8, Exploring Sound with the Raspberry Pi 2, gives an introduction to the basics of sound programming using the Raspberry Pi's hardware. Learn about live coding via the Sonic-Pi IDE to generate your own algorithmic music.
Chapter 9, Building a Web Server, expands upon your knowledge of Python to build a web server via Flask. This chapter explores the Ethernet and Wi-Fi capabilities of the Raspberry Pi for delivering web-based applications. In this chapter, you will also learn about using SQLite to store data and display it via a web page. Topics covered also include Apache and NGINX.
Chapter 10, Integrating with Third-Party Microcontrollers, in this chapter we learn how to interact with third-party microcontrollers such as the Arduino. These devices can form the basis of robotics projects and augment the abilities of the Raspberry Pi.
Chapter 11, Final Project, will conclude the book with a final project that brings together many of the topics explored throughout previous chapters.
The following list provides an overview of the recommended and optional hardware needed for the projects in this book. Where hardware is needed for a specific chapter, the relevant chapter is listed:
Are you interested in the architecture that forms the Raspberry Pi 2? Would you like to learn how its components work through interactive projects?
This book provides a hands-on guide to the Raspberry Pi 2's hardware and software. Each chapter builds upon the last to develop applications and electronics that leverage many of the features of the Raspberry Pi 2. From programming sound to integrating with third party microcontrollers, it's all covered here.
Aimed at the Raspberry Pi enthusiast, this is a perfect introductory text on how to get the most out of your new device.
While understanding programming concepts is helpful, no prior knowledge of the programming languages covered in this book is required.
Some simple electronics projects are included but no soldering is required.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The cd command allows you to change directories."
A block of code is set as follows:
Any command-line input or output is written as follows:
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Click on the Generate button."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
This chapter provides a brief introduction to the Raspberry Pi 2 Model B including both its history and its hardware architecture.
As well as discussing its system architecture, we will also look at some time saving methods for installing the Raspbian operating system.
Finally, we will wrap up with a number of tips and tricks, including how to monitor the voltage, overclock the CPU, and check the device's temperature. These quick tips should get you started exploring the operating system, installing software, and investigating the hardware.
We will mainly focus on the following topics:
The Raspberry Pi is a credit card-sized computer designed and manufactured in the UK with the initial intention of providing a cheap computing device for education. Since its release, however, it has grown far beyond the sphere of academia.
Its origins can be found in the University of Cambridge's Computer Laboratory in 2006. Computer scientist Eben Upton, along with Rob Mullins, Jack Lang and Alan Mycroft, were concerned that incoming computing undergraduate students had grown divorced from the technical aspects of computing. This was largely due to school syllabuses that placed an emphasis on using computers rather than understanding them.
Off the back of this initial concern, the Raspberry Pi foundation was formed. Over the next six years the team worked on developing a cheap and accessible device that would help schools to teach concepts such as programming, thus bringing students closer to understanding how computing works.
The Raspberry Pi's initial commercial release was in February 2012. Since then, the board has gone through a number of revisions and has been available in two models, those being Model A and Model B.
The Model A device is the cheaper and simpler of the two computers and the Model B the more powerful, including support for Ethernet connectivity.
In February 2015, the Raspberry Pi 2 Model B was released, and this is the device discussed in this book.
The new Raspberry Pi 2 is significantly more powerful than previous versions, opening us up to many new possibilities.
We will now look at the hardware of the device to get a basic understanding of what it is capable of doing. Future chapters will build upon the basics presented here.
The new Raspberry Pi is built on the back of the Broadcom BCM2836. The BCM2836 is a system-on-a-chip processor containing four ARM cores and Broadcom's VideoCore® IV graphics stack.
In contrast to this, previous Raspberry Pi A and B models only contained a single core.
On top of this, several other components make up the device, including USB, RCA, and microSD card storage. The previous Raspberry Pi Model B only contained two USB drives and a microUSB compared to the four USB drives and microUSB of the second version.
You can read a good breakdown of how the two boards standup to each other by visiting the following website: http://www.alphr.com/raspberry-pi-2/1000353/raspberry-pi-2-vs-raspberry-pi-b-a-raspberry-pi-comparison.
So, compared to earlier models, version 2 is a far more capable computer, yet still remains at the same price. The added benefit of having multiple cores allows us to explore different programming techniques for utilizing them.
Next, we shall cover the core components of the Raspberry Pi board in more detail. The following is an image of the board with a description of each component:
Image courtesy of Wikipedia
The Raspberry Pi 2 is a small machine measuring only 85.60 mm x 56 mm x 21 mm and weighing approximately 45g. This small size makes it suitable for embedded projects, home automation devices, arcade machines, or building small multi-device clusters.
The System on Chip (SoC) architecture that the Raspberry Pi 2 implements is the Broadcom BCM2836, which we touched upon earlier in this chapter. This contains a CPU, GPU, SDRAM, and single USB port. Each of these items is discussed in more detail under the appropriate heading.
A central processing unit is the brain of your Raspberry Pi. It is responsible for processing machine instructions, which are the result of your compiled programs.
The BCM2836 implements a 900 MHz quad-core ARM Cortex-A7 processor. This runs on the ARMv7 instruction set.
The ARM architecture reference manual can be downloaded from ARM's website at http://infocenter.arm.com/help/topic/com.arm.doc.ddi0406c/index.html.
The graphics processing unit (GPU) is a specialist chip designed to handle the complex mathematics required to render graphics.
The Broadcom VideoCore Iv 250 MHz supports OpenGL ES 2.0 (24 GFLOPS) Mpeg-2 and VC-1 (with license). It also includes a 1080p30 H.264/MPEG-4 AVC decoded/encoder.
The documentation for the GPU can be found on Broadcom's website at https://www.broadcom.com/docs/support/videocore/VideoCoreIV-AG100-R.pdf.
The Raspberry Pi 2 comes equipped with 1 GB of SDRAM, which is shared between the GPU and CPU.
The previous version of the Raspberry Pi Model B contained only a single microUSB port and a two standard USB ports. The Raspberry Pi 2 has been expanded to include an onboard 5-port USB hub.
This allows you to connect four standard USB cables to the device and a single microUSB cable. The micro USB port can be used to power your Raspberry Pi 2.
The microSD card is the main boot and storage mechanism of the Raspberry Pi. It is upon the microSD card that you will load your operating system and store data. Later in this book we will look at using the microSD purely for booting the Raspberry Pi, and then using a USB hard drive as a storage mechanism. In this chapter, we will delve into how we can setup the SD card with the Raspbian operating system.
One of the benefits of the Raspberry Pi 2 Model B is that it contains an Ethernet port. Many Raspberry Pi packages available on Amazon and similar stores include a wireless USB dongle; however, this results in you having to use up a USB port. If you plan to place your Raspberry Pi near a router or switch or have enough Ethernet cable, then you can connect your Raspberry Pi directly with the Ethernet jack.
The Raspberry Pi 2 supports 10/100 Mbps Ethernet, and the USB adapter in the third/fourth port of USB hub can also be used for Ethernet via a USB to Ethernet adapter.
Ethernet to USB adapters can be purchased from most good electronics stores and you can read more about the technology at https://en.wikipedia.org/wiki/Ethernet_over_USB.
The Raspberry Pi 2 implements the Inter-IC Sound (I2S) serial bus for audio input and output. This allows the device to connect multiple digital audio devices together. A 3.5mm TRRS jack is available and shared with the analog video output. The HDMI component also provides digital audio output.
Further information on configuring the audio output of the Raspberry Pi can be found on the official Raspberry Pi website: https://www.raspberrypi.org/documentation/configuration/audio-config.md.
The main method for interacting with electronic components and expansion boards is through the general purpose input/output (GPIO) pins on the Raspberry Pi.
The Raspberry Pi 2 Model B contains 40 pins in total. Future chapters will also look at how we can program these to control electronic devices.
As the acronym suggests the GPIO pins can accept both input and output commands and can be controlled by programs in a variety of languages running on the Raspberry Pi.
The input for example could be readings from a temperature sensor, and the output a command to another device to switch an LED on or off.
The Raspberry Pinout project provides an interactive guide to each GPIO pin and can be found at http://pinout.xyz/.
As well as providing a digital method for hooking up to a TV or monitor, the Raspberry Pi 2 also comes with analog support. The method of connection is commonly known as a composite or RCA port and earlier models of the Raspberry Pi came specifically with an RCA jack. RCA cables typically come with three connectors, two for audio and one (often yellow) for video.
With the release of the Raspberry Pi 2 the composite video (RCA) and 3.5 mm audio jacks functionality has been merged into a single TRRS hardware component. Therefore, if you wish to use video through this port, you may need to get a 3.5mm Mini AV TRRS to RCA cable instead. These can be found at any good electronics stores or on Amazon.
The Raspberry Pi 2 supports both PAL and NTSC standards.
Also included is a High-Definition Multimedia Interface (HDMI) port. This allows the Raspberry Pi 2 to be hooked up to high definition devices such as televisions and monitors. This port provides a digital alternative to the TRRS jack.
The HDMI port is ideal for streaming video and audio to your TV or monitor.
In order to get up and running with your Raspberry Pi 2 you will need the following additional hardware components:
Many websites offer starter kits that include some of these components, and an existing monitor can be reused.
The eLinux website also provides a good guide to peripherals at http://elinux.org/RPi_VerifiedPeripherals.
Before we can power up and start using our Raspberry Pi, however, we need to install an operating system on a microSD card.
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.
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.
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:
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.
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.
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.
The Raspbian installation process involves two steps:
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.
