Apress Mastering the Raspberry Pi 2014 Retail eBook-BitBook -E-book

E-Book description:

In the making of a book, there are so many people involved. I first want to thank Michelle Lowman, acquisitions
editor, for her enthusiasm for the initial manuscript and pulling this project together. Enthusiasm goes a long way in
an undertaking like this.
I’d also like to thank Kevin Walter, coordinating editor, for handling all my email questions and correspondence,
and coordinating things. I greatly appreciated the technical review performed by Stewart Watkiss, checking the facts
presented, the formulas, the circuits, and the software. Independent review produces a much better end product.
Thanks also to Sharon Wilkey for patiently wading through the copy edit for me. Judging from the amount of
editing, I left her plenty to do. Thanks to Douglas Pundick, development editor, for his oversight and believing in
this book. Finally, my thanks to all the other unseen people at Apress who worked behind the scenes to bring this
text to print.
I would be remiss if I didn’t thank my friends for helping me with the initial manuscript. My guitar teacher,
Mark Steiger, and my brother-in-law’s brother, Erwin Bendicks, both volunteered their time to help me with the
first manuscript. Mark has no programming or electronics background and probably deserves an award for reading
through “all that stuff.” I am indebted also to my daughter Laura and her fiancé Michael Burton, for taking the time to
take my photograph and the Raspberry Pi cover image, while they plan their wedding.
There are so many others I could list who helped me personally to reach a point in my life where I could write a
book like this. To all of you, please accept my humble thanks, and may God bless.

E-Book Author:

Warren W. Gay

mastering raspberry pi.jpg
E-Book Table of Contents:

About the Author…..xxvii
About the Technical Reviewer…..xxix
Acknowledgments…..xxxi
Chapter 1: Why This Book?…..1
Who Needs This Book?…..1
This Book Is Primarily About…..2
Learning Approach…..2
Organization of This Book…..3
Software in This Book…..3
Final Words…..4
Chapter 2: The Raspberry Pi…..5
Models…..5
Hardware in Common…..7
Which Model?…..7
Chapter 3: Preparation…..9
Static IP Address…..9
Using SSH…..10
VNC…..10
Display Number…..11
Geometry…..11
Depth…..11
Pixel Format….. 11
Password Setup…..11
Server Startup……12
VNC Viewers…..12
Stopping VNC Server…..12
Prototype Station…..12
Adafruit Pi Cobbler…..14
Gertboard…..14
Bare Metal…..15
Chapter 4: Power…..17
Calculating Power…..18
Current Requirement…..18
Peripheral Power…..19
Model B Input Power…..19
Model A Input Power…..20
3 3 Volt Power…..20
Powered USB Hubs…..20
Power Adapters…..21
An Unsuitable Supply…..21
E-book Adapters…..22
Best Power Source…..22
Voltage Test…..22
Battery Power…..23
Requirements…..23
Headroom……24
LM7805 Regulation…..24
DC-DC Buck Converter…..25
Signs of Insufficient Power…..27
No Power…..27
Status LEDs…..29
OK or ACT LED…..29
PWR LED…..29
FDX LED……30
LNK LED……30
10M or 10/100 LED…..30
Header P1…..31
Safe Mode……32
Logic Levels…..33
GPIO Configuration at Reset…..33
1-Wire Driver…..33
Header P5…..34
Reset…..34
Chapter 6: SDRAM…..37
/proc/meminfo…..37
MemTotal…..39
MemFree…..39
Buffers……39
Cached……40
SwapCached…..40
Active…..40
Inactive……40
Active(anon)…..40
Inactive(anon)…..41
Active(file)……41
Inactive(file)…..41
Unevictable……41
Mlocked……41
SwapTotal……42
SwapFree…..42
Dirty……42
Writeback……42
AnonPages……42
Mapped……42
Shmem……43
Slab…..  43
SReclaimable…..43
SUnreclaim……43
KernelStack…..43
PageTables……43
NFS_Unstable…..44
Bounce……44
WritebackTmp……44
CommitLimit…..44
Committed_AS……46
VmallocTotal…..46
VmallocUsed…..46
VmallocChunk…..46
Physical Memory…..47
Memory Mapping…..48
Address……49
Length…..49
Protection……49
Flags…..49
File Descriptor……50
Offset…..50
Return Value…..50
Volatile……50
Virtual Memory…..51
Final Thoughts on SDRAM…..51
Identification…..53
Overclocking…..54
Execution…..56
Threads…..57
pthread Headers…..57
pthread Error Handling…..57
pthread_create(3)…..57
pthread_attr_t……59
pthread_join(3)…..60
pthread_detach(3)……61
pthread_self(3)…..62
pthread_kill(3)……62
Mutexes…..63
pthread_mutex_create(3)…..63
pthread_mutex_destroy(3)…..64
pthread_mutex_lock(3)…..64
pthread_mutex_unlock(3)…..65
Condition Variables…..65
pthread_cond_init(3)…..66
pthread_cond_destroy(3)…..67
pthread_cond_wait(3)…..67
pthread_cond_signal(3)…..68
pthread_cond_broadcast(3)…..68
Chapter 8: USB…..69
Power……70
Powered Hubs…..70
USB API Support…..71
libusb…..71
Include Files…..72
Wired Ethernet…..75
/etc/network/interfaces…..76
Changing to Static IP…..76
Test Static IP Address……77
USB Adapters…..77
/etc/hosts File…..78
Wireless Ethernet…..78
Configuration…..79
Chapter 10: SD Card Storage…..83
SD Card Media…..83
SD Card Interface…..83
SD Card Basics…..84
Raspbian Block Size…..85
Disk Cache……85
Capacities and Performance…..86
Transfer Modes…..86
SPI Bus Mode…..86
1-bit SD Mode……87
4-bit SD Mode……88
Wear Leveling…..89
Chapter 11: UART…..91
RS-232 Converter…..91
DTE or DCE……92
RS-232…..93
Start Bit…..94
Data Bits…..94
Parity Bit…..94
Stop Bit(s)……94
Baud Rate……95
Break…..96
Flow Control….. 96
Raspberry Pi UARTs…..98
ARM PL011 UART…..98
RTS/CTS Access…..99
PL011 UART Features……99
Exclusive Serial Line Use…..100
Procedure……101
Verification…..101
Serial API…..101
Header Files…..102
open(2)…..  102
struct termios…..102
tcgetattr(3)…..105
tcsetattr(3)…..105
tcsendbreak(3)…..106
tcdrain(3)……106
tcflush(3)…..   107
tcflow(3)….. 107
cfmakeraw(3)…..108
cfgetispeed(3)…..110
cfgetospeed(3)…..110
cfsetispeed(3)…..111
cfsetospeed(3)…..111
cfsetspeed(3)……111
read(2)……112
write(2)…..113
readv(2) and writev(2)…..113
Error EINTR…..115

Pins and Designations…..117
Configuration After Reset…..119
Pull-up Resistors…..120
Configuring Pull-up Resistors…..120
Testing Pull-up State…..124
Logic Levels…..125
Drive Strength…..125
Input Pins……126
Output Pins…..127
Driving Bi-color LEDs…..130
Testing Drive Strength…..131
GPIO Current Budget…..133
Configuration…..133
Alternate Function Select…..133
Output Pins…..134
Input Pins……134
Alternate Function…..134
Sysfs GPIO Access…..134
export……135
unexport……136
gpioX…..136
Chip Level……138
GPIO Tester…..138
GPIO Input Test…..139
Floating Potentials…..141
Reading Events…..141
Test Run…..142
Direct Register Access…..146
gpio_init( )…..146
gpio_config( )……147
gpio_write( )…..147
gpio_read( )…..147
gpio_io c……147
GPIO Transistor Driver…..149
Driver Design……150
Substitution…..152
Inductive Loads…..152
Driver Summary…..153
Utility gpio…..153
Displaying GPIO Settings…..154
Reading GPIO……155
Writing GPIO…..155
Modify Drive Levels…..155
Chapter 13: 1-Wire Driver…..157
1-Wire Line and Power…..157
Line Driving…..157
Master and Slave…..158
Protocol…..158
Reset…..158
Data I/O…..159
Slave Support…..160
Reading Temperature…..160
Bus Master…..162
Slave Devices…..162
Reading the Temperature…..164
1-Wire GPIO Pin…..165
I2C Overview…..167
SDA and SCL…..168
Multimaster and Arbitration…..168
Bus Signaling…..169
Data Bits…..169
Message Formats…..170
Which I2C Bus?…..171
I2C Bus Speed…..171
Tools……172
I2C API…..172
Kernel Module Support…..172
Header Files…..172
open(2)…..173
ioctl(2,I2C_FUNC)……173
ioctl(2,I2C_RDWR)…..174
Chapter 15: SPI Bus…..177
SPI Basics…..177
SPI Mode…..178
Signaling…..178
Slave Selection…..180
Driver Support…..180
SPI API…..181
Header Files…..181
Open Device…..182
SPI Mode Macros……183
Bits per Word……184
Clock Rate…..184
Data I/O…..185
Close…..187
Write…..187
Read…..187
SPI Testing…..187
Chapter 16: Boot…..191
Booting ARM Linux…..191
Boot Sequence…..191
Boot Files…..192
config txt…..192
Composite Video Settings…..193
High-Definition Video…..194
Overscan Video…..200
Frame Buffer Settings…..201
General Video Options…..202
Licensed Codecs…..202
Testing……202
Memory…..203
Boot Options…..204
Overclocking…..207
Warranty and Overclocking…..208
Voltage Settings…..212
cmdline txt…..214
Serial console=…..215
Virtual console=…..215
kgdboc=……216
root=…..216
rootfstype=…..217
elevator=……217
rootwait=……217
nfsroot=…..218
ip=……219
Emergency Kernel…..219
Chapter 17: Initialization…..221
Run Levels…..221
/etc/inittab…..222
inittab Action initdefault…..222
General Startup Sequence…..223
inittab Action wait…..226
inittab Action once…..226
inittab Action respawn…..226
Changing Run Levels…..226
telinit…..226
Change of Run Level……227
Single-User Mode…..227
Halt and Reboot…..227
Creating a New Service…..228
Chapter 18: vcgencmd…..229
vcgencmd Commands…..229
Option measure_clock…..230
Option measure_volts…..232
Option measure_temp…..232
Option codec_enabled…..232
Option version…..233
Option get_lcd_info…..233
Option get_config…..233
Available Consoles…..235
Serial Console…..236
Chapter 20: Cross-Compiling…..237
Terminology…..237
Operating System…..238
Host, Guest, Build, and Target…..238
Platform Limitations……238
Without VirtualBox (Native)…..239
Using VirtualBox (Ubuntu/Linux)…..239
Planning Your Cross-Development Environment…..239
Building the Cross-Compiler…..240
Download crosstool-NG…..240
Staging Directory…..240
Unpack the Tarball…..241
Create /opt/x-tools…..241
Install Package Dependencies…..241
Configure crosstools-NG…..241
make crosstool-ng…..242
make install…..242
PATH……242
Cross-Compiler Configuration…..242
Paths and Misc Options…..243
Target Options…..245
Operating System…..246
Binary Utilities…..246
C Compiler…..247
Save Configuration…..248
Build Cross-Compiler…..248
Troubleshooting…..250
Image Tools…..253
Download Kernel…..254
Edit Makefile…..255
make mrproper…..256
Kernel Config…..256
make menuconfig…..257
make…..257
Prepare Kernel Image…..257
Install Kernel Image…..258
Boot Failure…..259
Modules…..259
Firmware…..260
VirtualBox Mount of SD Card…..260
Chapter 22: DHT11 Sensor…..263
Characteristics…..263
Circuit…..264
Protocol…..264
Overall Protocol…..264
Data Bits……265
Data Format…..266
Software……266
Chosen Approach…..267
Example Run…..268
Source Code…..269
Chapter 23: MCP23017 GPIO Extender…..275
DC Characteristics…..275
GPIO Output Current……276
GPIO Inputs…..276
Standby Current…..277
Input Logic Levels…..277
Output Logic Levels…..277
Reset Timing…..278
Circuit…..278
I2C Bus…..279
Wiring and Testing…..279
Software Configuration…..280
General Configuration…..281
Main Program…..288
Module i2c_funcs c…..292
Module sysgpio c…..296
Example Run…..298
Response Times…..300
Chapter 24: Nunchuk-Mouse…..303
Project Overview…..303
Nunchuk Features…..303
Connector Pinout…..304
Testing the Connection…..305
Nunchuk I2C Protocol…..306
Encryption…..306
Read Sensor Data…..307
Linux uinput Interface…..307
Working with Header Files…..308
Opening the Device Node…..308
Configuring Events…..309
Creating the Node…..312
Posting EV_KEY Events…..313
Posting EV_REL Events…..314
Posting EV_SYN Events…..314
Closing uinput…..315
X-Window…..315
Input Utilities…..316
Testing the Nunchuk…..316
Testing  /nunchuk…..317
Utility lsinputs…..318
Utility input-events…..318
The Program…..319
Chapter 25: Real-Time Clock…..329
DS1307 Overview…..329
Pins X1 and X2…..330
Pin SQW/OUT……330
Power…..331
3-Volt Compatibility…..331
Logic Levels…..332
Tiny RTC Modifications…..332
Checking for Pull-up Resistors…..332
DS1307 Bus Speed…..334
RTC and RAM Address Map…..335
Reading Date and Time…..336
I2C Communication…..336
Wiring…..337
Running the Examples…..337
The Ultimate Test…..338
The Startup Script…..338
Operating Parameters…..349
Pinout……350
VS1838B Circuit…..350
The IR Receiver…..351
Software…..351
Signal Components…..352
Code Organization…..353
Command-Line Options…..353
Chapter 27: Stepper Motor…..365
Floppy-Disk Stepper Motor…..365
Your Junk-Box Motor?…..366
Driver Circuit…..368
Darlington Pair…..369
Driving the Driver…..370
Input Levels…..370
Power-on Reset/Boot…..371
Modes of Operation…..372
Wave Drive (Mode 0)……372
Full-Step Drive (Mode 1)…..373
Half-Step Drive (Mode 2)…..373
Software…..374
Testing…..376
Chapter 28: The H-Bridge Driver…..383
The L298 Driver…..383
Sensing Resistor…..384
Enable A and B…..384
Inputs In1 and In2…..384
Protection Diodes…..384
L298 PCB…..385
Driving from GPIO…..387
The DMM Check…..388
Bipolar Stepper Modes…..388
One-Phase-On Mode…..388
Two-Phase-On Mode…..389
Half-Step Mode…..390
Choosing Driving GPIOs…..390
Project Schematic…..391
Junk-Box Motors……391
Program Operation…..392
Program Internals…..393
Chapter 29: Remote-Control Panel…..401
Switched Inputs…..401
The CD4013…..402
Testing the Flip-Flop…..403
The LED…..404
ØMQ…..404
Performing Installation…..405
Compiling and Linking…..405
Sensing Station Design…..406
Sensing Station Program…. 406
Console Program…..406
Console Commands…..407
Sensor Source Code…..407
Console Source Code…..415
Introduction to PWM…..421
PWM Parameters…..421
PWM Hardware Peripheral…..422
PWM Software…..422
Meter Circuit…..422
pwm Program…..423
Hardware PWM Set Command…..425
Hardware Based CPU Percent-Busy Display…..426
Software PWM Program…..432
How Many PWMs?…..434
Running the Software PWM Command…. 434
Software Based CPU Percent-Busy Display…..434
Appendix A: Glossary…..439
Appendix B: Power Standards…..445
Appendix C: Electronics Reference…..447
Ohm’s Law…..447
Power…..447
Units……447
Appendix D: Raspbian apt Commands…..449
List Available Packages…..449
List Installed Packages…..449
List Files for Package…..450
Perform Package Search…..450
Install a Package…..450
Remove a Package…..451
Install Updates…..451
Upgrade…..451
Obtain Kernel Sources…..452
Appendix F: Mac OS X Tips…..455
Bibliography…..457
Index…..463

Link to Purchase E-Book:

Apress Mastering the Raspberry Pi 2014 Retail eBook-BitBook -E-book


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top