Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

GNU/Linux Rapid Embedded Programming
GNU/Linux Rapid Embedded Programming
GNU/Linux Rapid Embedded Programming
Ebook1,333 pages6 hours

GNU/Linux Rapid Embedded Programming

Rating: 0 out of 5 stars

()

Read preview

About this ebook

About This Book
  • Rapidly design and build powerful prototypes for GNU/Linux Embedded systems
  • Become familiar with the workings of GNU/Linux Embedded systems and how to manage its peripherals
  • Write, monitor, and configure applications quickly and effectively, manage an external micro-controller, and use it as co-processor for real-time tasks
Who This Book Is For

This book targets Embedded System developers and GNU/Linux programmers who would like to program Embedded Systems and perform Embedded development. The book focuses on quick and efficient prototype building. Some experience with hardware and Embedded Systems is assumed, as is having done some previous work on GNU/Linux systems. Knowledge of scripting on GNU/Linux is expected as well.

LanguageEnglish
Release dateMar 29, 2017
ISBN9781786463289
GNU/Linux Rapid Embedded Programming

Read more from Giometti Rodolfo

Related to GNU/Linux Rapid Embedded Programming

Related ebooks

Hardware For You

View More

Related articles

Reviews for GNU/Linux Rapid Embedded Programming

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    GNU/Linux Rapid Embedded Programming - Giometti Rodolfo

    Table of Contents

    GNU/Linux Rapid Embedded Programming

    Credits

    About the Author

    About the Reviewer

    www.PacktPub.com

    Why subscribe?

    Customer Feedback

    Preface

    What this book covers

    What you need for this book

    Software prerequisite

    Hardware prerequisite

    Who this book is for

    Conventions

    Codes and command lines

    Kernel and logging messages

    File modifications

    Serial & network connections

    Other conventions

    Reader feedback

    Customer support

    Downloading the example code

    Downloading the color images of this book

    Errata

    Piracy

    Questions

    1. Installing the Developing System

    Embedded world terms

    Systems' overview

    The BeagleBone Black

    The SAMA5D3 Xplained

    The Wandboard

    Installing a development system

    Setting up the host machine

    Basic tools

    The cross-compiler

    Setting up the BeagleBone Black

    Serial console for the BeagleBone Black

    U-Boot (with MLO)

    Linux kernel for the BeagleBone Black

    Debian 8 (jessie) for the BeagleBone Black

    Setting up the SAMA5D3 Xplained

    Serial console for SAMA5D3 Xplained

    U-Boot (with boot.bin)

    Linux kernel for SAMA5D3 Xplained

    Debian 8 (jessie) for SAMA5D3 Xplained

    Setting up the Wandboard

    Serial console for the Wandboard

    U-Boot (with SPL)

    Linux kernel for the Wandboard

    Debian 8 (jessie) for the Wandboard

    Setting up the developing system

    BeagleBone Black - USB, networking, and overlays

    SAMA5D3 Xplained - USB and networking

    Wandboard - USB and networking (wired and wireless)

    Common settings

    Summary

    2. Managing the System Console

    Basic OS management

    File manipulation and Co

    echo and cat

    dd

    grep and egrep

    tr and sed

    head and tail

    od and hexdump

    file

    strings

    strace

    Package management

    Searching a software package

    Installing a package

    apt-get and friends versus aptitude

    The deb files

    Managing the kernel messages

    A quick tour into the bootloader

    The environment

    Managing the storage devices

    MMC

    Managing the flash

    GPIO management

    Accessing an I2C device

    Loading files from the network

    The kernel command line

    Summary

    3. C Compiler, Device Drivers, and Useful Developing Techniques

    The C compiler

    Native and foreign machine architecture

    Compiling a C program

    The native compilation

    The cross-compilation

    Compiling a kernel module

    The Kernel and DTS files

    Recompiling the kernel

    The device tree

    What is a device driver?

    Char, block, and net devices

    Modules versus built-in devices

    The modutils

    Writing our own device driver

    The root filesystem (rootfs)

    The /dev directory

    The tmpfs

    The procfs

    The sysfs

    The Network FileSystem (NFS)

    Exporting an NFS on the host

    Setting up the kernel to mount an NFS

    U-Boot and the kernel command line to use a NFS

    Developing into an NFS

    Using an emulator

    Executing a program

    Entering into an ARM rootfs tree

    Summary

    4. Quick Programming with Scripts and System Daemons

    Setting up the system

    System daemons

    Useful and ready-to-use daemons

    System daemons management

    syslogd

    syslogd in Bash

    syslogd in C

    syslogd in PHP

    syslogd in Python

    cron

    xinetd

    sshd

    Apache

    MySQL

    MySQL in Bash

    MySQL in C

    MySQL in PHP

    MySQL in Python

    Scripting languages

    Managing a LED in PHP

    The LAMP solution

    The built-in server solution

    Managing a LED in Python

    Managing a LED in Bash

    Writing a custom daemon

    A daemon in C

    A daemon in PHP

    A daemon in Python

    A daemon in Bash

    Summary

    5. Setting Up an Embedded OS

    MTD versus block devices

    What is an MTD device?

    Managing an MTD device

    Filesystems for flash memories

    JFFS2 versus UBIFS

    Building a JFFS2 filesystem

    Building a UBIFS filesystem

    OpenWrt

    Using the default configuration

    Adding a (quasi) LAMP system

    Adding a custom package

    Yocto

    Using the default recipe

    Adding the graphic support

    Adding a custom recipe

    Summary

    6. General Purposes Input Output signals – GPIO

    What is a GPIO line?

    GPIOs lines on the BeagleBone Black

    GPIOs on the SAMA5D3 Xplained

    GPIOs on the Wandboard

    GPIOs in Linux

    Getting access to GPIOs

    Bash

    C

    Using GPIOs with scripting languages

    PHP

    Python

    Managing GPIO into the kernel

    An input device using GPIOs

    LEDs and triggers

    Summary

    7. Serial Ports and TTY Devices - TTY

    What are TTY, serial, and UART lines?

    The electrical lines

    TTYs on the BeagleBone Black

    TTYs on the SAMA5D3 Xplained

    TTYs on the Wandboard

    Implementations of serial ports

    The serial ports in Linux

    The communication parameters

    Getting access to TTYs

    Distance sensor

    RFID LF reader

    Managing TTY in the kernel with SLIP

    Summary

    8. Universal Serial Bus - USB

    What is the universal serial bus?

    The electrical lines

    USB ports on the BeagleBone Black

    USB ports on the SAMA5D3 Xplained

    USB ports on the Wandboard

    The USB bus in Linux

    Acting as a host

    Acting as a device

    The Multi gadget

    The configfs gadget

    The USB tools

    The raw USB bus

    Accessing as a host

    Summary

    9. Inter-Integrated Circuits - I2C

    What is the Inter-Integrated Circuit bus?

    The electrical lines

    I2C ports on the SAMA5D3 Xplained

    I2C ports on the Wandboard

    The I2C bus in Linux

    The I2C tools

    Getting access to I2C devices

    EEPROM, ADC and IO Expander

    EEPROM

    ADC

    IO Expander

    The temperature/humidity and pressure sensors

    Serial port

    The Raw I2C Bus

    Writing data in C

    Reading data in Python

    Summary

    10. Serial Peripheral Interface - SPI

    What is the Serial Peripheral Interface bus?

    The electrical lines

    SPI ports on the BeagleBone Black

    SPI ports on the SAMA5D3 Xplained

    SPI ports on the Wandboard

    The SPI bus in Linux

    The SPI tools

    Getting access to SPI devices

    LCD display

    Serial port

    The raw SPI bus

    Exchanging data in C

    Exchanging data in Python

    Summary

    11. 1-Wire - W1

    What is the 1-Wire Bus?

    The electrical lines

    1-Wire ports on the BeagleBone Black

    1-Wire ports on the SAMA5D3 Xplained

    1-Wire ports on the Wandboard

    The 1-Wire bus in Linux

    Getting access to 1-Wire devices

    Using the GPIO interface

    Using an external controller

    Summary

    12. Ethernet Network Device - ETH

    What is an Ethernet network device?

    Electrical lines

    Ethernet port on the BeagleBone Black

    Ethernet ports on the SAMA5D3 Xplained

    Ethernet port on the Wandboard

    The Ethernet devices in Linux

    The net tools

    Communicating with a remote device

    A simple TCP client/server application

    Using ready-to-use networking tools

    The raw Ethernet bus

    Simple Ethernet bridging

    Summary

    13. Wireless Network Device - WLAN

    What is a wireless network device?

    The electrical lines

    WLAN device on the BeagleBone Black

    WLAN device on the SAMA5D3 Xplained

    WLAN device on the Wandboard

    WLAN devices in Linux

    Pluggable external WLAN devices

    The Wi-Fi operation modes

    The wireless tools

    The WPA supplicant

    The Hostapd daemon

    Summary

    14. Controller Area Network - CAN

    What is the CAN bus?

    The electrical lines

    CAN ports on the BeagleBone Black

    CAN ports on the SAMA5D3 Xplained

    CAN ports on the Wandboard

    The CAN bus in Linux

    The can-utils package

    The raw CAN bus

    Exchanging data via the CAN bus

    Using the on-board controller

    Using an external controller

    Summary

    15. Sound Devices - SND

    What is a sound device?

    The electrical lines

    Sound on the BeagleBone Black

    Sound on the SAMA5D3 Xplained

    Sound on the Wandboard

    Sound in Linux

    The audio tools

    The ALSA utils

    Madplay

    Mplayer

    Sox

    The USB audio device class

    Managing sound devices

    Adding an audio codec

    A simple oscilloscope

    Summary

    16. Video devices - V4L

    What is a video device?

    The electrical lines

    Video on the BeagleBone Black

    Video on the SAMA5D3 Xplained

    Video on the Wandboard

    Video in Linux

    The video tools

    The USB video class device 

    Managing video devices

    Streaming video over the Web

    Capturing motion

    Summary

    17. Analog-to-Digital Converters - ADC

    What is an analog-to-digital converter device?

    The electrical lines

    ADCs on the BeagleBone Black

    ADCs on the SAMA5D3 Xplained

    ADCs on the Wandboard

    ADCs in Linux

    Detecting a gas

    Summary

    18. Pulse-Width Modulation - PWM

    What is a PWM device?

    The electrical lines

    PWMs on the BeagleBone Black

    PWMs on the SAMA5D3 Xplained

    PWMs on the Wandboard

    PWM devices in Linux

    Managing a servo motor

    Summary

    19. Miscellaneous Devices

    Digital sensors

    Water sensor

    Infrared sensor

    Analog sensors

    Moisture sensor

    Pressure sensor

    Light sensor

    GSM/GPRS modem

    Smart card reader

    RFID reader

    Z-Wave

    Z-Wave controllers

    The Z-Wave wall plug sensor

    The Z-Wave multi sensor

    Summary

    GNU/Linux Rapid Embedded Programming


    GNU/Linux Rapid Embedded Programming

    Copyright © 2017 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 2017

    Production reference: 1240317

    Published by Packt Publishing Ltd.

    Livery Place

    35 Livery Street

    Birmingham 

    B3 2PB, UK.

    ISBN 978-1-78646-180-3

    www.packtpub.com

    Credits

    About the Author

    Rodolfo Giometti is an engineer, IT specialist, GNU/Linux expert and software libre evangelist.

    Author of the books BeagleBone Essentials and BeagleBone Home Automation Blueprints by Packt Publishing and maintainer of the LinuxPPS projects (the Linux's Pulse Per Second subsystem) he still actively contributes to the Linux source code with several patches and new device drivers for industrial applications devices.

    During his twenty-year+ experience, he, worked with x86, ARM, MIPS, & PowerPC-based platform.

    Now, Rodolfo is the Co-Chief at HCE Engineering S.r.l. and he is the Co-Founder of the Cosino Project, which involves new hardware and software systems for the quick prototyping in industry environment, control automation, and remote monitoring.

    I would like to thank my wife, Valentina, and my children, Romina and Raffaele, for their patience during the writing of this book. I also give deep thanks and gratitude to the Packt's guys Vivek Anantharaman, who gave to me the opportunity to write this book, and Rashmi Suvarna and Juliana Nair, who supported me in finishing this book (specially to Juliana Nair who spent her nights in editing the book's layout). Many thanks to Luca Zulberti, and Chris Simmonds for their support and effort in reviewing this book so carefully (specially to Luca Zulberti who so carefully re-checked my English, the code, and all circuitries). Also, a big thanks to Mohd Riyan Khan for his patience in adding all my modifications to the final review of this book.

    Finally, I cannot forget to thank my parents. Giving me my first computer when I was a child, allowed me to be doing what I do today.

    About the Reviewer

    Luca Zulberti has obtained a diploma in 2015 in Electronics at ITIS G. Galilei in Livorno, Italy. He is continuing his studies at the University of Pisa in Electronic Engineering class.

    In his spare time, he studies several programming techniques and languages. He has fun with several embedded systems and he’s interested in Embedded OS development and GNU/Linux programming.

    He’s member of the Cosino Project on which he writes some articles and projects.

    www.PacktPub.com

    For support files and downloads related to your book, please visit www.PacktPub.com.

    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 service@packtpub.com 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://www.packtpub.com/mapt

    Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.

    Why subscribe?

    Fully searchable across every book published by Packt

    Copy and paste, print, and bookmark content

    On demand and accessible via a web browser

    Customer Feedback

    Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at: www.amazon.com/dp/ASIN/1786461803.

    If you'd like to join our team of regular reviewers, you can e-mail us at customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!

    Preface

    Embedded computers have become very complex in the last few years and developers need to easily manage them by focusing on how to solve a problem without wasting time in finding good peripherals or learning how to manage them. The main challenge with experienced embedded programmers and engineers is really how long it takes to turn an idea into reality, and we show you exactly how to do it.

    This book shows how to interact with external environments through specific peripherals used in the industry. We will use the latest Linux kernel release 4.x and Debian/Ubuntu distributions (with embedded distributions of OpenWrt and Yocto).

    The book will present popular boards based on widely used and easily available CPUs on the components market and widely available from different professional boards makers. After a brief introduction of each boards, the book will show how to set up them in order to run a complete GNU/Linux distribution and then getting access to the system console. After that the book will present how to install a complete developing system on each board in order to be able to add developer's programs.

    Readers will be able to take their first steps in programming the embedded platforms, using C, Bash and Python/PHP languages in order to get access to the external peripherals. More about using and programming device driver and accessing the peripherals will be covered to lay a strong foundation. The readers will learn how to read/write data from/to the external environment by using both C programs and a scripting language (Bash/Python) and how to configure a device driver for a specific hardware.

    The hardware devices used in the book has been chosen in order to cover all possible connection type we can encounter working with an embedded board so you can find I²C, SPI, USB, 1-Wire, serial, digital and analog devices, etc.

    The programming languages used in the book has been chosen according to the rule to find the quickest and easiest solution to solve the current problem, in particular you can find example codes in Bash, C, PHP, Python.

    In such chapters were will need to use a daemon, a kernel module or to recompile the whole kernel I've added a short description about what the reader should do and where he/she can get more information regarding the used tools, however some basic skills in managing a GNU/Linux system or the kernel's modules or the kernel itself are required.

    What this book covers

    This book can be divided into two major parts: the first one, which is introductory to the second one, where you will see how to install the developing systems and the host system and how to get access to the serial console. You'll take a look at some basic bootloader's commands as far as to the C compiler and the cross-compiler and then I'll introduce kernel modules, device drivers and some filesystem internals with a note on the Network File System. Also the machine emulator usage will be presented in order to execute a complete target machine's Debian distribution on a host PC, you'll see the system daemons and script programming in Bash, PHP and Python and then you'll take a look at the flash memories and Linux's Memory Technology Device (MTD) where I'll introduce the JFFS2 and the UBIFS filesystem and two of the most famous embedded distribution used in these days, Yocto and OpenWrt.

    Below is a brief introduction of each chapters related to this first part:

    Chapter 1 , Installing the Developing System, will present three of the most used development kits for industrial applications: the BeagleBone Black, the SAMA5D3 Xplained and the WandBoard. After a brief introduction of each boards, we'll see how we can set up them in order to run a complete GNU/Linux distribution. We'll see how to install a complete developing system on each board as far as the host system (even on a virtual machine).

    Chapter 2 , Managing the System Console, will show to setup our developer kits and (in part) the host PC, then we'll go further in exploring the serial console and the shell running in it. In the end a special section will introduce the bootloader commands.

    Chapter 3 , C Compiler, Device Drivers, and Useful Developing Techniques, will focus the readers' attention to the C compiler (with its counter part: the cross-compiler). Then we'll take a look at kernel modules, device drivers and some filesystem internals with a note on the Network File System. In the end we'll see how developers can use an emulator in order to execute a complete target machine's Debian distribution on a host PC.

    Chapter 4 , Quick Programming with scripts and system daemons, will take a look at system daemons (what they are and how to use the most famous and useful ones). Then we'll take a look at script programming by using Bash, PHP and Python languages.

    Chapter 5 , Setting up an embedded OS, will start taking a look at flash memories and the software used to manage them, then we'll present the Linux's Memory Technology Device (MTD) and the two major filesystems that can run over them, that is the JFFS2 and the UBIFS. The we'll present two of the most famous embedded distribution used in these days, Yocto and OpenWrt, and how an embedded developer can write an his/her own application and how he/she can add it to them.

    The second part then starts in going deeper in presenting all of such peripheral devices that you, as embedded developer, may encounter into your professional life. For each peripheral I'll present where it is available on each embedded kit supported by this book and then how you can get access and use it. For each device kind I'll show to you how you can easily manage it with practical examples.

    Below is a brief introduction of each chapters related to the second part:

    Chapter 6 , General Purposes Input Output signals - GPIO, will introduce GPIO lines with a short description and then we'll see where they are physically located in our embedded machines. Then we're going to see in detail how we can get access to these lines in a very simple (but poor efficient) manner and then in a smarter (but a bit more complex) way. In the end we'll cover a rapid introduction of the GPIOs management inside the kernel with IRQ management and LED devices support.

    Chapter 7 , Serial Ports and TTY Devices - TTY, will introduce serial ports, that is one of the most important peripherals class a computer can have (at least a computer used in the control automation industry). After a brief description about what a serial port or a serial device is, we'll see how we can manage them into a GNU/Linux system in order to use a real serial device. Then we'll take a look at a kernel trick useful to communicate between two embedded systems by using a serial line as they were connected by an Ethernet cable.

    Chapter 8 , Universal Serial Bus - USB, will introduce the USB bus, that is a versatile bus, widely used in modern PCs, that allow people to connect an electronic device to a computer: for instance an hard disk, a keyboard or a serial device can be all connected to a computer through the same USB port.

    Chapter 9 , Inter-Integrated Circuits - I2C, will introduce the I²C bus which is typically used to connect on-board devices, that is the main computer with devices whose are all placed on the same board. Several devices use the I²C bus to communicate with the CPU and in this chapter will give to you a panoramic as wide as possible of them: we'll see several kinds of different devices with different configurations in order to cover as much as possible the combinations offered by this bus.

    Chapter 10 , Serial Peripheral Interface - SPI, will introduce the SPI bus which is another bus kind typically used to connect on-board devices as I²C does. However, and opposed to the I²C bus, this bus can transfer data at higher rates than I²C and, since it's full-duplex, data transfer can take place bidirectionally at the same time. Due these features the SPI bus is normally used to implement an efficient data stream for multimedia applications or digital signal processing and/or telecommunications devices and SD cards.

    Chapter 11, 1-Wire - W1, will introduce Ethernet devices whose add the possibility to any equipped device to communicate with other devices even on very long distances. The GNU/Linux based systems offer a really good support of Ethernet devices and their relative networking protocols that's why most of networking devices around the world are based on this technology.

    Chapter 12, Ethernet network device - ETH, will introduce the one wire bus which is interesting because it permits to communicate with a remote device using only one wire even if at slower rates. This allows to simplify connections between the CPU and its peripherals giving the designer the possibility to have the most economical and simply way to add electronic devices for identification, authentication and delivery of calibration data or manufacturing information to a computer board.

    Chapter 13 , Wireless Network Device - WLAN, will introduce Wireless network devices whose allow the communication between several computers but doing it without using wires. What is really interesting in using these networking interfaces is that a large part of communication protocols used on Ethernet interfaces can be used with these devices too!

    Chapter 14 , Controller Area Network - CAN, will introduce the CAN bus that has been specifically designed to allow microcontrollers, computers and devices to communicate with each other in applications without a host computer by having a message-based protocol. The CAN bus is not so famous as Ethernet or WiFi but in the embedded world it is used and is not rare finding SoCs which support it by default.

    Chapter 15 , Sound devices - SND, will introduce sound devices with some possible usages of them till to show to the reader how they can use them in order to generate audio how to implement a simple and low cost oscilloscope.

    Chapter 16 , Video devices - V4L, will introduce common video acquisition devices with some possible usages of them till to show to the reader how they can turn our embedded kits into a surveillance camera or a remote image recorder.

    Chapter 17 , Analog-to-Digital Converters - ADC, will introduce ADCs that can be used to get analogical signals from the environment. The chapter will show how to use them and how to use special software and hardware triggers in order to start conversions at specific timing or when some events occur.

    Chapter 18 , Pulse-Width Modulation - PWM, will introduce PWMs that are able to encode a message into a pulsing signal (usually a square waveform) to generate an analog signal by using a digital source, then these messages can be used to control the power supplied to electrical motors or other electronics devices or, as we're going to show into this chapter, to control the position of a servo motor.

    Chapter 19 , Miscellaneous devices, will introduce peripherals that can fit in one of the above chapters but that has not reported there for better readability. In this last chapter we're going to present a list of additional peripherals we can encounter into a monitoring or controlling system such as RFID and smart card readers, some digital and analog sensors, GSM/GPRS modem, Z-Wave, etc.

    What you need for this book

    Following are the requisites for efficient learning.

    Software prerequisite

    Regarding the software you should have a little knowledge of a non graphical text editor as vi, emacs or nano. Even if you can connect an LCD display, a keyboard and a mouse directly to embedded kits and then use the graphical interface, in this book we assume that you is able to do little modifications to text files by using a text only editor.

    The host computer, that is the computer you will use to cross-compile the code and/or to manage your embedded systems, is assumed running a GNU/Linux based distribution. My host PC is running an Ubuntu 15.10 but you can use also a newer Ubuntu Long Term Support (LTS) or a Debian based system too with little modifications or you may use another GNU/Linux distribution but with a little effort from you mainly regarding the cross-compiling tools installation, libraries dependencies and packages management. Foreign systems such as Windows, MacOS or similar are not covered by this book due the fact you should not use low technology systems to develop code for high technology system!

    Knowing how a C compiler works and how to manage a Makefile is required.

    This book will present some kernel programming techniques but these must cannot be taken as a kernel programming course. You need a proper book for such topic! However each example is well documented and you will find several suggested resources. Regarding the kernel I'd like to state that the version used into this book is 4.4.x.

    As a final note I suppose that you known how to connect a GNU/Linux based board on the Internet in order to download a package or a generic file.

    Hardware prerequisite

    In this book all code is developed for BeagleBone Black board revision C, for SAMA5D3 Xplained revision A or for the WandBoard revision C1 (depending on the board used) but you can use an older revision without any issues, in fact the code is portable and it should work on other systems too (but the DTS files whose must be considered apart)!

    Regarding the computer peripherals used in this book I reported in each chapter where I got the hardware and where you can buy it but, of course, you can decide to surf the Internet in order to find a better and cheaper offer. A note where to find the datasheet is also present.

    You should not have any difficulties in order to connect the hardware presented in this book with the embedded kits since the connections are very simple and well documented. They don't require any particular hardware skills to be performed from you (apart knowing how to use a solder), however having a minor knowledge in electronics may help.

    Who this book is for

    If you want to learn how to use embedded machine learning capabilities and get access to a GNU/Linux device driver to collect data from a peripheral or to control a device, this book is for you.

    If you are interested in knowing how to easily and quickly get access to different computer peripherals in order to realize a functional control or monitor system based on GNU/Linux for industrial applications, this book is for you.

    If you have some hardware or electrical engineering experience and know the basics of C, Bash, and Python and PHP programming in a UNIX environment and want using them into an embedded system, this book is for you.

    Conventions

    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.

    Codes and command lines

    Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs and user input are shown as follows: To get the preceding kernel messages, we can use both the dmesg and tail -f /var/log/kern.log commands.

    A block of code is set as follows:

    #include

    int main(int argc, char *argv[])

    {

        printf(Hello World!\n);

        return 0;

    }

    You should note that most code in this book has 4 spaces indentation while the example code you can find into the files provided with this book on Github or Packt site, has 8 spaces indentation. So the above code will look like as follow:

    #include

    int main(int argc, char *argv[])

    {

            printf(Hello World!\n);

            return 0;

    }

    Obviously they are perfectly equivalent!

    Any command line input or output given on one of the embedded kits used in this book is written as follows:

    root@bbb:~# make CFLAGS=-Wall -O2 helloworld cc -Wall -O2 helloworld.c -o helloworld

    Then by looking into the prompt string we can deduce which board we're currently using. I use the string bbb for the BeagleBone Black, a5d3 for the SAMA5D3 Xplained and wb for the WandBoard. However I use the generic string arm when I'm referring a generic embedded kit.

    Note also that due space reasons into the book you may read very long commands lines as follows:

    $ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-                 sama5d3_xplained_nandflash_defconfig

    Otherwise I have had to break the command line. However in some special cases you can find broken output lines (specially on kernel messages) as follow:

    cdc_ether 2-1.1:1.0 usb0: register 'cdc_ether' at usb-0000:00:1d.0-1.1

    , CDC Ethernet Device, 62:1e:f6:88:9b:42

    Unluckily these lines cannot be easily reported into a printed book so you should consider them as a single line.

    Any command line input or output given on my host computer as a non- privileged user is written as follows:

    $ tail -f /var/log/kern.log

    When I need to give a command as a privileged user (root) on my host computer the command line input or output is then written as follows:

    # /etc/init.d/apache2 restart

    You should notice that all privileged commands can be executed by a normal user too by using the sudo command with the form:

    $ sudo

    So the preceding command can be executed by a normal user as:

    $ sudo /etc/init.d/apache2 restart

    Kernel and logging messages

    On several GNU/Linux distribution a kernel message has the usual form:

    Oct 27 10:41:56 hulk kernel: [46692.664196] usb 2-1.1: new high-speed USB device number 12 using ehci-pci

    Which is a quite long line for this book, that's why the characters from the start of the lines till the point where the real information begin are dropped. So, in the preceding example, the lines output will be reported as follow:

    usb 2-1.1: new high-speed USB device number 12 using ehci-pci

    However, as just said above, if the line is still too long it will be broken anyway.

    Long outputs, repeated or less important lines in a terminal are dropped by replacing them with three dots ... shown as follows:

    output begin output line 1 output line 2 ... output line 10 output end

    When the three dots are at the end they mean that the output continues but I decided cut it for space reasons.

    File modifications

    When you should modify a text file, I'm going to use the unified context diff format since this is a very efficient and compact way to represent a text modification. This format can be obtained by using the diff command with the -u option argument.

    As a simple example, let's consider the following text into file1.old:

    This is first line

    This is the second line

    This is the third line

    ...

    ...

    This is the last line

    Suppose we have to modify the third line as highlighted in the following snippet:

    This is first line

    This is the second line

    This is the new third line modified by me

    ...

    ...

    This is the last line

    You can easily understand that reporting each time the whole file for a simple modification it's quite obscure and space consuming, however by using the unified context diff format the preceding modification can be written as follow:

    $ diff -u file1.old file1.new

    --- file1.old 2015-03-23 14:49:04.354377460 +0100

    +++ file1.new 2015-03-23 14:51:57.450373836 +0100

    @@ -1,6 +1,6 @@

    This is first line

    This is the second line

    -This is the third line

    +This is the new third line modified by me

    ...

    ...

    This is the last line

    Now the modification is very clear and written in a compact form! It starts with a two-line header where the original file is preceded by --- and the new file is preceded by +++, then follows one or more change hunks that contain the line differences in the file. The preceding example has just one hunk where the unchanged lines are preceded by a space character, while the lines to be added are preceded by a + character and the lines to be removed are preceded by a - character.

    Still for space reasons, most patches reported into this book has reduced indentation in order to fit printed pages width, however they are still perfectly readable in a correct form. For the real patch you should refer to the provided files on Github or Packt site.

    Serial & network connections

    In this book I'm going to mainly use two different kind of connections to interact with the the embedded boards: the serial console, an SSH terminal and an Ethernet connection.

    The serial console, that is implemented over the same USB connection used to power up the board, is mainly used to manage the system from the command line. It's largely used to monitoring the system especially to take under control the kernel messages.

    An SSH terminal is quite similar to the serial console even if is not exactly the same (for example kernel messages do not automatically appear on a terminal) but it can be used in the same manner as a serial console to give commands and to edit files from the command line.

    In the next chapters I'm going to use a terminal on the serial console or over an SSH connection indifferently to give the most of the commands and configuration settings needed to implement all the prototypes explained in this book.

    To get access to the serial console from your host PC you can use the minicon command as follow:

    $ minicom -o -D /dev/ttyACM0

    Or the next one according to the board and/or the USB-to-Serial adapter used:

    $ minicom -o -D /dev/ttyUSB0

    However in Chapter 1 , Installing the Developing System, this aspects are explained and you should not worry about them. Note also that on some system you may needs the root privileges to get access to the /dev/ttyACM0 device. In this case you can fix this issue or by using the sudo command or, better, by properly add you system's user to the right group by using the command below:

    $ sudo adduser $LOGNAME dialout

    Then log out and log in again and you should access the serial devices without any problem.

    To get access to the SSH terminal you can use the emulated Ethernet connection over the same USB cable used for the serial console. In fact, if your host PC is well configured, when you plug in the USB cable, after a while, you should see a new cable connection with a proper IP address (in case of the BeagleBone Black you should get the address 192.168.7.1, for the SAMA5D3 Xplained the address 192.168.8.1, while address 192.168.9.1 for the WandBoard. See Chapter 1 , Installing the Developing System ). Then, for example, I can use this new connection to get access to the BeagleBone Black by using the following command:

    $ ssh root@192.168.7.2

    The last available communication channel is the Ethernet connection. It is used mainly to download files from the host PC or the Internet and it can be established by connecting an Ethernet cable to the each embedded kit's Ethernet port and then configuring the port accordingly to the reader's LAN settings.

    But it's quite important to point out the you can get connected with the Internet by using also the emulated Ethernet connection over USB presented above. In fact, by using the commands below on the host PC (obviously GNU/Linux based), you'll be able to use it as a router allowing your embedded boards to surf the net as it was connected with its real Ethernet port:

    # iptables --table nat --append POSTROUTING --out-interface eth1

                          -j MASQUERADE

    # iptables --append FORWARD --in-interface eth4 -j ACCEPT # echo 1 >> /proc/sys/net/ipv4/ip_forward

    Then, for instance, on the BeagleBone Black I should set the gateway through the USB cable using the following command:

    root@bbb:~# route add default gw 192.168.7.1

    Note that the eth1 device is the preferred Internet connection on my host system, while the eth4 device is the BeagleBone Black's device as viewed on my system.

    Other conventions

    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: Clicking the Next button moves you to the next screen.

    Note

    Warnings or important notes appear in a box like this.

    Tip

    Tips and tricks appear like this.

    Reader feedback

    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 feedback@packtpub.com , 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 .

    Customer support

    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.

    Downloading the example code

    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:

    Log in or register to our website using your e-mail address and password.

    Hover the mouse pointer on the SUPPORT tab at the top.

    Click on Code Downloads & Errata.

    Enter the name of the book in the Search box.

    Select the book for which you're looking to download the code files.

    Choose from the drop-down menu where you purchased this book from.

    Click on Code Download.

    Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

    WinRAR / 7-Zip for Windows

    Zipeg / iZip / UnRarX for Mac

    7-Zip / PeaZip for Linux

    The complete set of code can also be downloaded from the following GitHub repository: https://github.com/PacktPublishing/GNU-Linux-Rapid-Embedded-Programming/. We also have other code bundles from our rich catalog of books and videos available at: https://github.com/PacktPublishing/. Check them out!

    For this book the example code files can also be downloaded from the author's Github repository at URL https://github.com/giometti/gnu_linux_rapid_embedded_programming .

    Just use the following command to get it at once:

    $ git clone

        https://github.com/giometti/gnu_linux_rapid_embedded_programming.git

    The examples are grouped according to the chapters name so you can easily find the code during the reading of the book.

    Downloading the color images of this book

    We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/GNULinuxRapidEmbeddedProgramming.pdf.

    Errata

    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

    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 copyright@packtpub.com with a link to the suspected pirated material.

    We appreciate your help in protecting our authors and our ability to bring you valuable content.

    Questions

    If you have a problem with any aspect of this book, you can contact us at questions@packtpub.com , and we will do our best to address the problem.

    Chapter 1. Installing the Developing System

    In this chapter, we will present three of the most used development kits for industrial applications. Respect to the most famous Raspberry Pi, these boards are based on widely used CPUs on custom boards in an industrial environment. In fact, while Raspberry Pi's CPU is not easily available on the components market, the CPUs of the following boards are widely available with different professional board makers.

    In the upcoming sections, after a brief introduction of each board, we'll see how we can set them up to run a complete GNU/Linux distribution and then get access to the system console. After that, we will install a complete developing system on each board in order to be able to add our own programs.

    A little tutorial about how to set up the host system is also present, and you can use it to set up a GNU/Linux-based working machine or a dedicated virtual one.

    This chapter can be skipped if you already have a running embedded system with the relative host PC. However, you should consider reading it anyway due the fact that we'll present an overview of the embedded devices we're going to use in this book. In this chapter, we'll fix some common terms used in this book, and you may learn a different way to install a running system on your boards. Also, last but not least, the system that is already running on your boards may be different from the ones presented here. This means that you may need to change some commands presented in this book accordingly in order to have functional examples.

    Embedded world terms

    Before putting our hands on our new boards, it is recommended that you acquaint yourselves with some terms that a user should know in order to avoid misunderstandings. People who have already worked with some GNU/Linux and/or embedded systems may skip this part. The developer kits shown here are tiny single-board computers that can be embedded into a device, so the user should be familiar with some terms used in the wonderful world of embedded programming:

    Now that some important terms have been pointed out, we are ready to step into the next section and discover our developer kits!

    Systems' overview

    Here is a brief introduction of the developer kits we will use into this book.

    The first kit is the BeagleBone Black, which is a low-cost, community-supported development platform for developers and hobbyists. It's able to boot Linux in under 10 seconds and get started on development in few minutes with just a single USB cable. This board is widely used on several prototypes on the Internet, so it's a board that every embedded programmer should know.

    The second kit is SAMA5D3 Xplained , which is a fast prototyping and evaluation platform that comes with a rich set of ready-to-use connectivity, storage peripherals, and expansion headers for easy customization. A USB device connector can be used to power the board as well as for programming and debugging it. This board is very interesting due the fact it uses a very low power-consuming CPU with good performances and with a lot of industrial-oriented peripherals.

    The last (but not least) kit is Wandboard, which is a complete computer with high-performance multimedia capabilities, a good peripheral equipment and, in contrast with the other boards, it's composed by a core module and an easy interface board to customize or modify. The board is very interesting because it can be equipped with a multicore CPU and because it comes as a CPU module connected to a carrier board, which allows embedded developers to have a highly hardware-customizable device.

    The BeagleBone Black

    In the following image, there is a picture of the BeagleBone Black, with a credit card, so that you can have an idea about the real dimensions of the whole system:

    Here are some interesting URLs where you can read some useful information regarding BeagleBone Black:

    https://beagleboard.org/black

    http://beagleboard.org/static/beaglebone/latest/Docs/Hardware/BONE_SRM.pdf

    http://beagleboard.org/getting-started

    https://eewiki.net/display/linuxonarm/BeagleBone+Black

    The main hardware key features of my BeagleBone Black (revision C) are reported in the following table:

    Then, the following image shows a top view of the BeagleBone Black, where we can see some interesting things:

    The connector J1 used to access the serial console.

    The Ethernet connector.

    The power connector.

    The two expansion connectors P8 and P9, where we can connect the dedicated extension boards and/or custom peripherals (these connectors will be explained in detail in the upcoming chapters).

    The microSD slot.

    The USB host port.

    The reset button does what it says while the power button can be used to turn on/off the board, and the user button, which is user controllable, can be used to do an alternate boot on the microSD card instead of the on-board eMMC.

    From the preceding image, we can see that the BeagleBone Black doesn't look like a PC, but it can act as a PC! The BeagleBone Black is a fully functional single-board computer and can be readily used as a PC if required by connecting a monitor to the HDMI port and attaching a USB keyboard and mouse through a USB hub. However, it is more suited to embedded applications, where it can act as more than a PC due its expansion connectors, and we can stack up to four expansion boards (named capes) that are useful for several purposes.

    In this book, we'll see how we

    Enjoying the preview?
    Page 1 of 1