Linux Driver Development With Raspberry Pi Practical Labs 1st Edition Alberto Liberal de Los Rios
Linux Driver Development With Raspberry Pi Practical Labs 1st Edition Alberto Liberal de Los Rios
https://ebookfinal.com/download/raspberry-pi-hacks-tips-tools-for-
making-things-with-the-inexpensive-linux-computer-1st-edition-ruth-
suehle/
ebookfinal.com
https://ebookfinal.com/download/getting-started-with-python-and-
raspberry-pi-1st-edition-nixon/
ebookfinal.com
https://ebookfinal.com/download/learning-raspberry-pi-1st-edition-
shah/
ebookfinal.com
https://ebookfinal.com/download/raspberry-pi-user-guide-1st-edition-
gareth-halfacree/
ebookfinal.com
Raspberry Pi User Guide 4th Edition Eben Upton
https://ebookfinal.com/download/raspberry-pi-user-guide-4th-edition-
eben-upton/
ebookfinal.com
https://ebookfinal.com/download/raspberry-pi-robotics-projects-2nd-
edition-richard-grimmett/
ebookfinal.com
https://ebookfinal.com/download/raspberry-pi-a-quick-start-guide-2nd-
edition-maik-schmidt/
ebookfinal.com
https://ebookfinal.com/download/the-official-raspberry-pi-beginner-s-
guide-2nd-edition-halfacree/
ebookfinal.com
https://ebookfinal.com/download/raspberry-pi-3-cookbook-for-python-
programmers-third-edition-tim-cox/
ebookfinal.com
Linux Driver Development with Raspberry Pi Practical
Labs 1st Edition Alberto Liberal De Los Rios Digital
Instant Download
Author(s): Alberto Liberal de los Rios
ISBN(s): 9798516120688, 8516120686
Edition: 1
File Details: PDF, 5.95 MB
Year: 2021
Language: english
Linux Driver
Development with
Raspberry Pi®
Practical Labs
Copyright © 2021, Alberto Liberal. All rights reserved.
Linux is a registered trademark of Linus Torvalds. Other trademarks within this book are the
property of their respective owners.
The kernel modules examples provided with this book are released under the GPL license. This
license gives you the right to use, study, modify and share the software freely. However, when
the software is redistributed, modified or unmodified, the GPL requires that you redistribute the
software under the same license, with the source code.
Published by:
Alberto Liberal
[email protected]
[1]
Table of Contents
Preface..................................................................................................................13
Chapter 1: Building the System..........................................................................19
Bootloader..............................................................................................................20
Linux kernel............................................................................................................22
System call interface and C runtime library............................................................25
System shared libraries..........................................................................................26
Root filesystem.......................................................................................................27
Linux boot process..................................................................................................28
Building an embedded Linux system for the Raspberry Pi.....................................29
Raspberry Pi OS.....................................................................................................30
Connect and set up hardware................................................................................32
Setting up an ethernet communication...................................................................36
Copying files to your Raspberry Pi.........................................................................36
Building the Linux kernel........................................................................................37
Chapter 2: The Linux Device and Driver Model.................................................41
Bus core drivers......................................................................................................42
Bus controller drivers..............................................................................................44
Device drivers.........................................................................................................44
Devices...................................................................................................................45
The sysfs filesystem...............................................................................................47
The kobject infrastructure.......................................................................................49
Introduction to the Device Tree...............................................................................53
Chapter 3: The Simplest Drivers.........................................................................61
Licensing.................................................................................................................62
LAB 3.1: "helloworld" module.................................................................................62
Listing 3-1: helloworld_rpi3.c......................................................................63
Listing 3-2: Makefile....................................................................................63
helloworld_rpi3.ko demonstration...............................................................64
LAB 3.2: "helloworld with parameters" module.......................................................65
Listing 3-3: helloworld_rpi3_with_parameters.c..........................................66
Listing 3-4: Makefile....................................................................................66
[3]
Table of Contents
helloworld_rpi3_with_parameters.ko demonstration...................................67
Chapter 4: Character Drivers..............................................................................69
LAB 4.1: "helloworld character" module.................................................................71
Registration and unregistration of character devices.............................................72
Listing 4-1: helloworld_rpi3_char_driver.c...................................................76
Listing 4-2: Makefile....................................................................................78
Listing 4-3: ioctl_test.c................................................................................78
helloworld_rpi3_char_driver.ko demonstration...........................................79
Creating device files with devtmpfs........................................................................79
LAB 4.2: "class character" module.........................................................................80
Listing 4-4: helloworld_rpi3_class_driver.c.................................................82
helloworld_rpi3_class_driver.ko demonstration..........................................84
Miscellaneous character driver...............................................................................85
Registering a minor number...................................................................................85
LAB 4.3: "miscellaneous character" module...........................................................86
Listing 4-5: misc_rpi3_driver.c....................................................................87
misc_rpi3_driver.ko demonstration.............................................................88
Chapter 5: Platform Drivers.................................................................................89
LAB 5.1: "platform device" module.........................................................................91
Listing 5-1: hellokeys_rpi3.c........................................................................93
hellokeys_rpi3.ko demonstration................................................................95
Documentation to interact with the hardware.........................................................96
Hardware naming convention.................................................................................96
Pin control subsystem.............................................................................................97
GPIO controller driver...........................................................................................106
GPIO descriptor consumer interface....................................................................110
Obtaining and disposing GPIOs........................................................................... 110
Using GPIOs......................................................................................................... 110
GPIOs mapped to IRQs........................................................................................ 111
GPIOs in the Device Tree..................................................................................... 112
Exchanging data between kernel and user spaces..............................................112
MMIO (Memory-Mapped I/O) device access........................................................113
LAB 5.2: "RGB LED platform device" module......................................................115
[4]
Table of Contents
[5]
Table of Contents
[6]
Table of Contents
keyled_rpi3_class.ko demonstration.........................................................259
LAB 7.4: "GPIO expander device" module...........................................................262
LAB 7.4 hardware description..............................................................................263
LAB 7.4 Device Tree description..........................................................................264
LAB 7.4 GPIO controller driver description...........................................................265
Listing 7-4: CY8C9520A_rpi3.c ................................................................271
LAB 7.4 GPIO child driver description..................................................................283
Listing 7-5: int_rpi3_gpio.c .......................................................................285
LAB 7.4 GPIO based IRQ application..................................................................286
Listing 7-6: gpio_int.c ...............................................................................287
LAB 7.4 driver demonstration...................................................................289
LAB 7.5: "GPIO-PWM-PINCTRL expander device" module.................................293
LAB 7.5 GPIO irqchip description.........................................................................293
LAB 7.5 pin controller driver description...............................................................295
LAB 7.5 PWM controller driver description...........................................................299
Listing 7-7: CY8C9520A_pwm_pinctrl.c ..................................................302
LAB 7.5 driver demonstration...................................................................323
LAB 7.6: CY8C9520A Device Tree overlay..........................................................325
LAB 7.6 driver demonstration...................................................................332
Chapter 8: Allocating Kernel Memory..............................................................333
Linux address types..............................................................................................333
User process virtual to physical memory mapping...............................................335
Kernel virtual to physical memory mapping..........................................................336
Kernel memory allocators.....................................................................................337
Page allocator.......................................................................................................338
Page allocator API................................................................................................338
SLAB allocator......................................................................................................339
SLAB allocator API...............................................................................................342
Kmalloc allocator..................................................................................................343
LAB 8.1: "linked list memory allocation" module........................................................344
Listing 8-1: linkedlist_rpi3_platform.c..............................................................346
linkedlist_rpi3_platform.ko demonstration......................................................351
Chapter 9: DMA in Device Drivers....................................................................353
Cache coherency..................................................................................................353
Linux DMA Engine API..........................................................................................355
Types of DMA mappings.......................................................................................357
[7]
Table of Contents
[8]
Table of Contents
LAB 10.4 code description of the "SPI accel input device" module......................420
Listing 10-6: adxl345_rpi3.c......................................................................430
adxl345_rpi3.ko demonstration.................................................................442
Chapter 11: Industrial I/O Subsystem...............................................................445
IIO device sysfs interface......................................................................................447
IIO device channels..............................................................................................447
The iio_info structure............................................................................................449
Buffers..................................................................................................................450
IIO buffer sysfs interface.......................................................................................450
IIO buffer setup.....................................................................................................450
Triggers.................................................................................................................452
Triggered buffers..................................................................................................452
Industrial I/O events..............................................................................................454
Delivering IIO events to user space......................................................................456
IIO utils..................................................................................................................458
LAB 11.1: "IIO Mixed-Signal I/O Device" module.................................................458
LAB 11.1 hardware description.............................................................................461
LAB 11.1 Device Tree description........................................................................463
LAB 11.1 driver description...................................................................................468
Listing 11-1: max11300-base.h.................................................................483
Listing 11-2: maxim,max11300.h..............................................................485
Listing 11-3: max11300.c..........................................................................486
Listing 11-4: max11300-base.c.................................................................489
LAB 11.1 driver demonstration..................................................................507
GPIO control through a character device............................................................. 511
Listing 11-5: gpio_device_app.c................................................................512
GPIO control through the gpiolibd library.............................................................513
Listing 11-6: libgpiod_max11300_app.c....................................................514
LAB 11.2: “Nunchuk provider and consumer” modules........................................515
Nunchuck provider module...................................................................................515
Listing 11-7: nunchuk_accel.c...................................................................519
Nunchuck consumer module................................................................................522
Listing 11-8: nunchuk_consumer.c............................................................523
LAB 11.2 Device Tree description........................................................................527
LAB 11.2 driver demonstration..................................................................529
[9]
Table of Contents
[ 10 ]
Table of Contents
[ 11 ]
Preface
Embedded systems have become an integral part of our daily life. They are deployed in mobile
devices, networking infrastructure, home and consumer devices, digital signage, medical imaging,
automotive infotainment and many other industrial applications. The use of embedded systems
is growing exponentially. Many of these embedded systems are powered by an inexpensive yet
powerful system-on-chip (SoC) that is running a Linux operating system. The BCM2837 from
Broadcom is one of these SoCs, running quad ARM Cortex A53 cores at 1.2GHz. This is the SoC
used in the popular Raspberry Pi 3 boards.
This book follows the learning by doing approach, so you will be playing with your Raspberry
Pi since the first chapter. Besides the Raspberry Pi board, you will use several low-cost boards to
develop the hands-on examples. In the labs, it is described what each step means in detail so that
you can use your own hardware components adapting the content of the book to your needs.
You will learn how to develop Linux drivers for the Raspberry Pi boards. You will start with
the simplest ones that do not interact with any external hardware, then you will develop Linux
drivers that manage different kind of devices: Accelerometer, DAC, ADC, RGB LED, Buttons,
Joystick controller, Multi-Display LED controller and I/O expanders controlled via I2C and SPI
buses. You will also develop DMA drivers, USB device drivers, drivers that manage interrupts
and drivers that write and read on the internal registers of the SoC to control its GPIOs. To ease
the development of some of these drivers, you will use different types of Linux kernel subsystems:
Miscellaneous, LED, UIO, USB, Input and Industrial I/O. More than 30 kernel modules have been
written (besides several user applications), which can be downloaded from the book's GitHub
repository.
This book uses the Long Term Support (LTS) Linux kernel 5.4, which was released on November
2019 and will be maintained until December 2025.
This book is a learning tool to start developing drivers without any previous knowledge about
this field, so the intention during its writing has been to develop drivers without a high level of
complexity that both serve to reinforce the main driver development concepts and can be a starting
point to help you to develop your own drivers. And, remember that the best way to develop a
driver is not to write it from scratch. You can reuse free code from similar Linux kernel mainline
drivers. All the drivers written throughout this book are GPL licensed, so you can modify and
redistribute them under the same license.
[ 13 ]
Preface
[ 14 ]
Preface
several I2C client drivers throughout this chapter. You will also see how to add "sysfs" support to
control the hardware from user space via sysfs entries.
Chapter 7, Handling Interrupts in Device Drivers, introduces interrupt hardware and software
operation on embedded processors running Linux. You will learn about the different structures
used to create Linux drivers for interrupt controllers and the different types of GPIO irqchips. You
will develop several drivers that manage interrupts coming from the GPIOs of the Raspberry Pi
SoC. In one of these drivers, you will see how a user application is put to sleep by using a "wait
queue" and woken up later via an interrupt. You will also develop a GPIO controller driver with
interrupt capabilities for an off-chip GPIO expander and a user application that requests interrupts
from the GPIO expander by using GPIOlib APIs. In this chapter, you will see how to introduce
new hardware support by using the Raspberry Pi Device Tree overlay mechanism.
Chapter 8, Allocating Kernel Memory, explains the different types of addresses used in Linux.
This chapter also describes the different kernel memory allocators.
Chapter 9, DMA in Device Drivers, describes the "Linux DMA Engine Subsystem" and the
different types of DMA mappings. You will develop a DMA driver for the Raspberry Pi that
manages memory to memory transactions without CPU intervention.
Chapter 10, Input Subsystem, introduces the use of frameworks to provide a coherent user space
interface for every type of device, regardless of the drivers. The chapter explains in the relationship
between the physical and logical parts of a driver that uses a kernel framework. It focuses on
the Input subsystem, which takes care of the input events coming from the human user. In this
chapter, you will develop a driver for the WII Nunchuk controller and several Python applications
that interact with the events generated by the Nunchuk driver. One of these applications will use
the Pygame Python modules, drawing the Nunchuk Joystick values on a screen connected to the
Raspberry Pi board. This chapter also describes the Linux SPI subsystem, which is based on the
Linux device model. You will develop an SPI client driver for an accelerometer device using the
Input subsystem.
Chapter 11, Industrial I/O Subsystem, describes the IIO subsystem, which provides support for
ADCs, DACs, gyroscopes, accelerometers, magnetometers, proximity sensors, etc. The set up of
IIO triggered buffers and Industrial I/O events will be explained in detail. You will create an IIO
driver to control the Maxim MAX11300 device, which integrates a PIXI™, 12-bit, multichannel,
analog-to-digital converter (ADC) and a 12-bit, multichannel, buffered digital-to-analog converter
(DAC) in a single integrated circuit (IC). In this driver, you will create a GPIO controller, which
configures and controls the MAX11300 ports. You will also develop one application that controls
a GPIO of the MAX11300 device through a character device and another application that controls
the same GPIO using the functions of the gpiolibd library. In this chapter, you will also develop
two drivers using a provider-consumer implementation: the first one is an IIO provider driver
[ 15 ]
Preface
which will read the 3-axis data from the Nunchuk´s accelerometer, and the second one is an Input
subsystem consumer driver which will read the IIO channel values from the IIO provider driver
and report them to the Input subsystem.
Chapter 12, Using the Regmap API in Device Drivers, provides an overview of the regmap API
and explains how it takes care of calling the relevant calls of the SPI or I2C subsystem. You will
transform the SPI Input subsystem driver of the Chapter 10, which uses specific SPI core APIs, into
an SPI IIO subsystem driver which uses the regmap API, keeping the same functionality between
both drivers. Finally, you will dive into the "IIO tools" applications to test the driver.
Chapter 13, USB Device Drivers, describes the Linux USB subsystem, which is based on the Linux
device model. You will learn how to create custom USB HID devices based on the Microchip
PIC32MX microcontroller that will send/receive data to/from a Raspberry Pi Linux USB device
driver. You will learn about main Linux USB data structures and functions and develop several
Linux USB device drivers throughout this chapter.
[ 16 ]
Preface
Questions
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.
[ 17 ]
1
Building the System
The Linux kernel is one of the largest and most successful open source projects that has ever come
about. The huge rate of change and the number of individual contributors shows that it has a
vibrant and active community, constantly stimulating evolution of the kernel. This rate of change
continues to increase, as does the number of developers and companies involved in the process.
The development process has proved that it is able to scale up to higher speeds without trouble.
The Linux kernel together with GNU software and many other open-source components provides
a completely free operating system, GNU/Linux. Embedded Linux is the usage of the Linux kernel
and various open-source components in embedded systems.
Embedded Linux is used in embedded systems such as consumer electronics (e.g., set-top boxes,
smart TVs, PVRs (personal video recorders), IVI (in-vehicle infotainment), networking equipment
(such as routers, switches, WAPs (wireless access points) or wireless routers), machine control,
industrial automation, navigation equipment, spacecraft flight software, and medical instruments
in general).
There are many advantages of using Linux in embedded systems. The following list shows some
of these benefits:
• The main advantage of Linux is the ability to reuse components. Linux provides scalability
due to its modularity and configurability.
• Open source. No royalties or licensing fees.
• Ported to a broad range of hardware architectures, platforms and devices.
• Broad support of applications and communication protocols (e.g., TCP/IP stack, USB stack,
graphical toolkit libraries).
• Large support coming from an active community of developers.
These are the main components of an embedded Linux system: Bootloader, Kernel, System
call interface, C-Runtime library, System shared libraries and Root filesystem. Each of these
components will be described in more detail in the next sections.
[ 19 ]
Building the System Chapter 1
Bootloader
Linux cannot be started on an embedded device without a small amount of machine specific code
to initialize the system. Linux requires the bootloader code to do very little, although several
bootloaders do provide extensive additional functionality. The minimal requirements are:
• Configuration of the memory system.
• Loading of the kernel image and the Device Tree at the correct addresses.
• Optional loading of an initial RAM disk at the correct memory address.
• Setting of the kernel command-line and other parameters (e.g., Device Tree, machine type).
It is also usually expected that the bootloader initializes a serial console for the kernel in addition
to these basic tasks.
There are different bootloader options that come in all shapes and sizes. U-Boot is the standard
bootloader for ARM Linux. U-Boot source code is located at https://source.denx.de/u-boot/u-boot.
These are some of the main U-Boot features:
1. Small: U-Boot is a bootloader, and its primary purpose in the system is to load an
operating system. That means that U-Boot is necessary to perform a certain task, but it is
not worth spending significant resources on. Typically, U-Boot is stored in the relatively
small NOR flash memory, which is expensive compared to the much larger NAND
devices normally used to store the operating system and the application. A usable and
useful configuration of U-Boot, including a basic interactive command interpreter, support
for download over Ethernet and the capability to program the flash should fit in no more
than 128 KB.
2. Fast: The end user is not interested in running U-Boot. In most embedded systems, they
are not even aware that U-Boot exists. The user wants to run some application code, and
they want to do that as soon as possible after switching on the device. Initialize devices
only when they are needed within U-Boot, i.e., don't initialize the Ethernet interface(s)
unless U-Boot performs a download over Ethernet; don't initialize any IDE or USB devices
unless U-Boot actually tries to load files from these, etc.
3. Portable: U-Boot is a bootloader, but it is also a tool used for board bring-up, for
production testing and for other activities that are very closely related to hardware
development. So far, it has been ported to several hundreds of different boards on about
30 different processor families.
4. Configurable: U-Boot is a powerful tool with many, many extremely useful features.
The maintainer or user of each board will have to decide which features are important
[ 20 ]
Chapter 1 Building the System
and what shall be included with their specific board configuration to meet the current
requirements and restrictions.
5. Debuggable: U-Boot is not only a tool in itself, it is often also used for hardware bring-up,
so debugging U-Boot often means that you don't know if you are tracking down a problem
in the U-Boot software or in the hardware you are running on. Code that is clean and easy
to understand and debug is all the more important to everyone. One important feature of
U-Boot is to enable output to the (usually serial) console as soon as possible in the boot
process, even if this causes tradeoffs in other areas like memory footprint. All initialization
steps shall print some "begin doing this" message before they actually start and some
"done" message when they complete. The purpose of this is that you can always see which
initialization step was running if any problems occur. This is important not only during
software development, but also for the service people dealing with broken hardware in the
field. U-Boot should be debuggable with simple JTAG or BDM equipment. It should use a
simple, single-threaded execution model.
[ 21 ]
Building the System Chapter 1
Linux kernel
Linux is a clone of the operating system Unix, written from scratch by Linus Torvalds with
assistance from a loosely-knit team of hackers across the Net. It aims towards POSIX and Single
UNIX Specification compliance.
It has all the features you would expect in a modern, fully-fledged Unix implementation, including
true multitasking, virtual memory, shared libraries, demand loading, shared copy-on-write
executables, proper memory management and multistack networking including IPv4 and IPv6.
Although originally developed for 32-bit x86-based PCs (386 or higher), today Linux also runs on a
multitude of other processor architectures, in both 32-bit and 64-bit variants.
The Linux kernel is the lowest level of software running on a Linux system. It is charged with
managing the hardware, running user programs and maintaining the overall security and integrity
of the whole system. It is this kernel, which, after its initial release by Linus Torvalds in 1991,
jump-started the development of Linux as a whole. The kernel is a relatively small part of the
software on a full Linux system (many other large components come from the GNU project, the
GNOME and KDE desktop projects, the X.org project and many other sources), but the kernel is
the core which determines how well the system will work and is the piece which is truly unique to
Linux.
The kernel, which forms the core of the Linux system, is the result of one of the largest cooperative
software projects ever attempted. Regular two-to-three month releases deliver stable updates to
Linux users, each with significant new features, added device support and improved performance.
The rate of change in the kernel is high and increasing, with over 10,000 patches going into each
recent kernel release. Each of these releases contains the work of more than 1,600 developers
representing over 200 corporations.
As kernels move from the mainline into the stable category, two things can happen:
1. They can reach End of Life after a few bugfix revisions, which means that kernel
maintainers will release no more bugfixes for this kernel version, or
2. They can be put into longterm maintenance, which means that maintainers will provide
bugfixes for this kernel revision for a much longer period of time.
If the kernel version you are using is marked EOL, you should consider upgrading to the next
major version, as there will be no more bugfixes provided in the kernel version you are using.
Linux kernel is released under GNU GPL version 2 and is therefore Free Software as defined by
the Free Software Foundation. You may read the entire copy of the license in the COPYING file
distributed with each release of the Linux kernel.
[ 22 ]
Chapter 1 Building the System
[ 23 ]
Building the System Chapter 1
In addition to the official versions of the kernel, there are many third-parties (chip-vendors,
sub-communities) that supply and maintain their own version of the kernel sources by forking
from the official kernel tree. The intent is to separately develop support for a particular piece of
hardware or subsystem and to integrate this support to the official kernel at a later point. This
process is called mainlining and describes the task to integrate the new feature or hardware
support to the upstream (official) kernel. These are called Distribution kernels.
It is easy to tell if you are running a Distribution kernel. Unless you downloaded, compiled and
installed your own version of kernel from www.kernel.org, you are running a Distribution kernel. To
find out the version of your kernel, run uname -r after booting the processor.
[ 24 ]
Chapter 1 Building the System
You will work with the longterm kernel 5.4.y releases to develop all the drivers throughout this
book.
The C runtime library (C-standard library) defines macros, type definitions and functions for
string handling, mathematical functions, input/output processing, memory allocation and several
other functions that rely on OS services. The runtime library provides applications with access to
OS resources and functions by abstracting the OS System call interface.
Several C runtime libraries are available: glibc, uClibc, eglibc, dietlibc, newlib. The choice of the C
library must be made at the time of the cross-compiling toolchain generation, as the GCC compiler
is compiled against a specific C library.
The GNU C library, glibc, is the default C library used for example in the Yocto project. The
GNU C Library is primarily designed to be a portable and high performance C library. It follows
all relevant standards including ISO C11 and POSIX.1-2008. It is also internationalized and has
[ 25 ]
Building the System Chapter 1
one of the most complete internationalization interfaces known. You can find the glibc manual at
https://www.gnu.org/software/libc/manual/.
[ 26 ]
Chapter 1 Building the System
Root filesystem
The root filesystem is where all the files contained in the file hierarchy (including device nodes) are
stored. The root filesystem is mounted as /, containing all the libraries, applications and data.
The folder structure of the root filesystem is defined by FHS (Filesystem-Hierarchy-Standard). The
FHS defines the names, locations and permissions for many file types and directories. It thereby
ensures compatibility between different Linux distributions and allows applications to make
assumptions about where to find specific system files and configurations.
An embedded Linux root filesystem, usually includes the following:
• /bin: Commands needed during bootup that might be used by normal users (probably
after bootup).
[ 27 ]
Building the System Chapter 1
• /sbin: Like /bin, but the commands are not intended for normal users, although they may
use them if necessary and allowed; /sbin is not usually in the default path of normal users,
but will be in root's default path.
• /etc: Configuration files specific to the machine.
• /home: Like My Documents in Windows.
• /root: The home directory for user root. This is usually not accessible to other users on the
system.
• /lib: Essential shared libraries and kernel modules.
• /dev: Device files. These are special virtual files that help the user interface with the
various devices on the system.
• /tmp: Temporary files. As the name suggests, programs running often store temporary files
in here.
• /boot: Files used by the bootstrap loader. Kernel images are often kept here instead of in
the root directory. If there are many kernel images, the directory can easily grow too large,
and it might be better to keep it in a separate filesystem.
• /mnt: Mount point for mounting a filesystem temporarily.
• /opt: Add-on application software packages.
• /usr: Secondary hierarchy.
• /var: Variable data.
• /sys: Exports information about devices and drivers from the kernel device model to user
space, and it is also used for configuration.
• /proc: Represent the current state of the kernel.
[ 28 ]
Chapter 1 Building the System
4. The kernel runs low level kernel initialization, enabling MMU, creating the initial table of
memory pages and setting up caches. This is done in arch/arm/kernel/head.s. The file head.s
contains CPU architecture specific, but platform independent initialization code. Then, the
system switches to the non architecture specific kernel startup function start_kernel().
5. The kernel runs start_kernel() located in init/main.c that:
• Initializes the kernel core (e.g., memory, scheduling, interrupts).
• Initializes statically compiled drivers.
• Mounts the root filesystem based on bootargs passed to the kernel from U-Boot.
• Executes the first user process, init. The init process, by default, is /init for initramfs
and /sbin/init for a regular filesystem. The three init programs that you usually find on
embedded Linux devices are BusyBox init, System V init and systemd.
[ 29 ]
Building the System Chapter 1
Raspberry Pi OS
Raspberry Pi OS is the recommended operating system for normal use on a Raspberry Pi. Raspberry
Pi OS is a free operating system based on Debian, optimised for the Raspberry Pi hardware.
Raspberry Pi OS comes with over 35,000 packages: precompiled software bundled in a nice format
for easy installation on your Raspberry Pi. Raspberry Pi OS is a community project under active
development, with an emphasis on improving the stability and performance of as many Debian
packages as possible.
You will install on a Micro SD a Raspberry Pi OS image based on kernel 5.4.y. Go to https://
downloads.raspberrypi.org/raspios_full_armhf/images/raspios_full_armhf-2020-12-04/ and download the
2020-12-02-raspios-buster-armhf-full.zip image.
To write the compressed image on the Micro SD card, you will download and install Etcher. This
tool, which is an Open Source software, is useful since it allows to get a compressed image as
input. More information and extra help is available on the Etcher website at https://etcher.io/.
Once the image is installed on the Micro SD card, you will insert the Micro SD into the SD card
reader on your host PC, then you will enable UART, SPI and I2C peripherals in the programmed
Micro SD. Open a terminal application on your host PC, and type the following commands:
~$ lsblk
~$ mkdir ~/mnt
~$ mkdir ~/mnt/fat32
~$ mkdir ~/mnt/ext4
~$ sudo mount /dev/mmcblk0p1 ~/mnt/fat32
See the files in the fat32 partition. Check that config.txt is included:
~$ ls -l ~/mnt/fat32/
[ 30 ]
Chapter 1 Building the System
You can also update previous settings (after booting the Raspberry Pi board) through the
Raspberry Pi Configuration application found in Preferences on the menu.
The Interfaces tab is where you turn these different connections on or off so that the Pi recognizes
that you’ve linked something to it via a particular type of connection.
[ 31 ]
Building the System Chapter 1
[ 32 ]
Chapter 1 Building the System
2. Connect your screen to the single Raspberry Pi’s HDMI port. You can also connect a
mouse to a USB port and keyboard in the same way.
3. Connect the Ethernet port on Raspberry Pi to an Ethernet socket on your host PC.
[ 33 ]
Building the System Chapter 1
4. The serial console is a helpful tool for debugging your board and reviewing system log
information. To access the serial console, connect a USB to TTL Serial Cable to the device
UART pins as shown below.
5. Plug the USB power supply into a socket and connect it to your Raspberry Pi’s power port.
[ 34 ]
Chapter 1 Building the System
You should see a red LED light up, which indicates that the Raspberry Pi board is connected to
power. As it starts up, you will see raspberries appear in the top left-hand corner of your screen.
After a few seconds the Raspberry Pi OS Desktop will appear.
For the official Raspberry Pi OS, the default user name is pi, with password raspberry.
To find out the version of the booted kernel, run uname -r on the Raspberry Pi terminal:
pi@raspberrypi:~$ uname -r
5.4.79-v7+
Reset the board. You can disconnect your screen from the Raspberry Pi’s HDMI port during the
development of the labs (you will only need to connect a screen in the LAB 10.3).
pi@raspberrypi:~$ sudo reboot
To see Linux boot messages on the console, add loglevel=8 in the file cmdline.txt under /boot.
pi@raspberrypi:~$ sudo nano /boot/cmdline.txt
For example:
pi@raspberrypi:~$ echo 8 > /proc/sys/kernel/printk
Now, every kernel message will appear on your console, as all priority higher than 8 (lower loglevel
values) will be displayed. Please note that after reboot, this configuration is reset. To keep the
[ 35 ]
Building the System Chapter 1
configuration permanently, just append the following kernel.printk value to the /etc/sysctl.conf file,
then reboot the processor:
kernel.printk = 8 4 1 3
pi@raspberrypi:~$ sudo nano /etc/sysctl.conf
pi@raspberrypi:~$ sudo reboot
Raspbian has the SSH server disabled by default. You have to start the service on the Pi:
pi@raspberrypi:~# sudo /etc/init.d/ssh restart
Now, verify that you can ping your Linux host machine from the Raspberry Pi. Exit the ping
command by typing "Ctrl-c":
pi@raspberrypi:~# ping 10.0.0.1
You can also ping from Linux host machine to the target. Exit the ping command by typing
"Ctrl-c".
~$ ping 10.0.0.10
[ 36 ]
Chapter 1 Building the System
By default, the root account is disabled, but you can enable it by using this command and giving it
a password, for example "pi":
pi@raspberrypi:~$ sudo passwd root
Now, you can log into your pi as the root user. Open the sshd_config file, and change
PermitRootLogin to yes (also comment the line out). After editing the file, type "Ctrl+x", then type
"yes", and press "enter" to exit:
pi@raspberrypi:~$ sudo nano /etc/ssh/sshd_config
On your host PC, create the linux_rpi3 folder, where you are going to download the kernel sources:
~$ mkdir linux_rpi3
~$ cd linux_rpi3/
Get the kernel sources. The git clone command below will download the current active branch
without any history. Omitting the --depth=1 will download the entire repository, including the full
history of all branches, but this takes much longer and occupies much more storage.
~/linux_rpi3$ git clone --depth=1 -b rpi-5.4.y https://github.com/raspberrypi/linux
Compile the kernel, modules and Device Tree files. First, apply the default configuration:
~/linux_rpi3/linux$ KERNEL=kernel7
~/linux_rpi3/linux$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig
Configure the following kernel settings that will be needed during the development of the labs:
~/linux_rpi3/linux$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig
Device drivers >
[*] SPI support --->
<*> User mode SPI device driver support
[ 37 ]
Building the System Chapter 1
For the LAB 12.1, you will need the functions that enable the triggered buffer support. If they are
not defined accidentally by another driver, there's an error thrown out while linking. To solve
this problem, you can select, for example, the HTS221 driver, which includes this triggered buffer
support:
Device drivers >
<*> Industrial I/O support > Humidity sensors --->
<*> STMicroelectronics HTS221 sensor Driver
Having built the kernel, you need to copy it onto your Raspberry Pi device and also install the
modules. Insert the previously programmed Micro SD into the SD card reader on your host PC,
and execute the following commands:
~$ lsblk
~$ mkdir ~/mnt
~$ mkdir ~/mnt/fat32
~$ mkdir ~/mnt/ext4
~$ sudo mount /dev/mmcblk0p1 ~/mnt/fat32
~$ sudo mount /dev/mmcblk0p2 ~/mnt/ext4/
~$ cd linux_rpi3/linux
~/linux_rpi3/linux$ sudo env PATH=$PATH make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
INSTALL_MOD_PATH=~/mnt/ext4 modules_install
[ 38 ]
Exploring the Variety of Random
Documents with Different Content
Walter, Thomas, cited, 17, 21, 28.
Ward-Stephens, 355.
Z
Zerrahn, Carl, 189;
conductor of music festivals, 222.
Ziegfeld, Florens, 252.
*** END OF THE PROJECT GUTENBERG EBOOK THE ART OF
MUSIC, VOL. 04 (OF 14) ***
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must,
at no additional cost, fee or expense to the user, provide a copy,
a means of exporting a copy, or a means of obtaining a copy
upon request, of the work in its original “Plain Vanilla ASCII” or
other form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookfinal.com