The First Practical, Hands-On Guide to Embedded System Programming for Android Today, embedded systems programming is a more valuable discipline than ever, driven by fast-growing, new fields such as wearable technology and the Internet of Things. In this concise guide, Roger Ye teaches all the skills you’ll need to write the efficient embedded code necessary to make tomorrow’s Android devices work. The first title in Addison-Wesley’s new AndroidTM Deep Dive series for intermediate and expert Android developers,Embedded Programming with AndroidTMdraws on Roger Ye’s extensive experience with advanced projects in telecommunications and mobile devices. Step by step, he guides you through building a system with all the key components Android hardware developers must deliver to manufacturing. By the time you’re done, you’ll have the key programming, compiler, and debugging skills you’ll need for real-world projects. First, Ye introduces the essentials of bare-metal programming: creating assembly language code that runs directly on hardware. Then, building on this knowledge, he shows how to use C to create hardware interfaces for booting a Linux kernel with the popular U-Boot bootloader. Finally, he walks you through using filesystem images to boot Android and learning to build customized ROMs to support any new Android device. Throughout, Ye provides extensive downloadable code you can run, explore, and adapt. You will * Build a complete virtualized environment for embedded development * Understand the workflow of a modern embedded systems project * Develop assembly programs, create binary images, and load and run them in the Android emulator * Learn what it takes to bring up a bootloader and operating system * Move from assembler to C, and explore Android’s goldfish hardware interfaces * Program serial ports, interrupt controllers, real time clocks, and NAND flash controllers * Integrate C runtime libraries * Support exception handling and timing * Use U-Boot to boot the kernel via NOR or NAND flash processes * Gain in-depth knowledge for porting U-Boot to new environments * Integrate U-Boot and a Linux kernel into an AOSP and CyanogenMod source tree * Create your own Android ROM on a virtual Android device Contents 10 Preface 16 Acknowledgments 22 About the Author 24 I: Bare Metal Programming 26 1 Introduction to Embedded System Programming 28 What Is an Embedded System? 28 Bare Metal Programming 28 Learning Embedded System Programming 30 Software Layers in an Embedded System 32 Tools and Hardware Platform 36 The Difference between Virtual Hardware and Real Hardware 36 Summary 37 2 Inside Android Emulator 38 Overview of the Virtual Hardware 38 Configuring Android Virtual Devices 39 Hardware Interfaces 42 Serial 43 Timer 43 Summary 49 3 Setting Up the Development Environment 50 The Host and Client Environments 50 Development Environment Setup 51 Downloading and Installing Android SDK 52 Downloading and Installing the GNU Toolchain for ARM 52 Integrated Development Environment 54 Your First ARM Program 54 Building the Binary 55 Running in the Android Emulator 57 makefile for the Example Projects 61 Summary 63 4 Linker Script and Memory Map 64 Memory Map 64 Linker 66 Linker Script 76 Initializing Data in RAM 81 Summary 86 5 Using the C Language 88 C Startup in a Bare Metal Environment 88 Calling Convention 103 Goldfish Serial Port Support 106 Summary 117 6 Using the C Library 118 C Library Variants 118 Newlib C Library 121 Common Startup Code Sequence 122 CS3 Linker Scripts 122 Customized CS3 Startup Code for the Goldfish Platform 128 System Call Implementations 129 Running and Debugging the Library 137 Using Newlib with QEMU ARM Semihosting 141 Summary 147 7 Exception Handling and Timer 150 Goldfish Interrupt Controller 150 The Simplest Interrupt Handler 153 Nested Interrupt Handler 165 Testing System Calls/Software Interrupts 188 Timer 189 Real-Time Clock 197 Summary 206 8 NAND Flash Support in Goldfish 208 Android File System 208 NAND Flash Properties 210 NAND Flash Programming in the Goldfish Platform 212 Memory Technology Device Support 213 MTD API 214 NAND Flash Programming Interface Test Program 231 Summary 241 II: U-Boot 242 9 U-Boot Porting 244 Introducing U-Boot 244 Downloading and Compiling U-Boot 245 Debugging U-Boot with GDB 249 Porting U-Boot to the Goldfish Platform 252 Summary 271 10 Using U-Boot to Boot the Goldfish Kernel 274 Building the Goldfish Kernel 274 Prebuilt Toolchain and Kernel Source Code 275 Running and Debugging the Kernel in the Emulator 277 Booting Android from NOR Flash 279 Booting Android from NAND Flash 295 Summary 305 III: Android System Integration 306 11 Building Your Own AOSP and CyanogenMod 308 Introducing AOSP and CyanogenMod 308 Setting Up an Android Virtual Device 309 AOSP Android Emulator Build 313 CyanogenMod Android Emulator Build 322 Summary 332 12 Customizing Android and Creating Your Own Android ROM 334 Supporting New Hardware in AOSP 334 Supporting New Hardware in CyanogenMod 357 Summary 363 IV: Appendixes 364 A: Building the Source Code for This Book 366 Setting Up the Build Environment 366 Setting Up a Virtual Machine 369 Organization of Source Code 369 Source Code for Part I 370 Source Code for Part II 375 Source Code for Part III 377 B: Using Repo in This Book 380 Resources for Repo 380 Syncing a New Source Tree In Minutes 380 Downloading Git Repositories Using Local Manifest 381 Index 384 A 384 B 385 C 386 D 387 E 387 F 388 G 389 H 390 I 390 K 391 L 391 M 391 N 392 O 393 P 393 Q 393 R 393 S 394 T 395 U 396 V 396 W 396 X 396 Y 396 The First Practical, Hands-On Guide to Embedded System Programming for Android Today, embedded systems programming is a more valuable discipline than ever, driven by fast-growing, new fields such as wearable technology and the Internet of Things. In this concise guide, Roger Ye teaches all the skills you'll need to write the efficient embedded code necessary to make tomorrow's Android devices work. The first title in Addison-Wesley's new AndroidTM Deep Dive series for intermediate and expert Android developers, Embedded Programming with AndroidTM draws on Roger Ye's extensive experience with advanced projects in telecommunications and mobile devices. Step by step, he guides you through building a system with all the key components Android hardware developers must deliver to manufacturing. By the time you're done, you'll have the key programming, compiler, and debugging skills you'll need for real-world projects. First, Ye introduces the essentials of bare-metal programming: creating assembly language code that runs directly on hardware. Then, building on this knowledge, he shows how to use C to create hardware interfaces for booting a Linux kernel with the popular U-Boot bootloader. Finally, he walks you through using filesystem images to boot Android and learning to build customized ROMs to support any new Android device. Throughout, Ye provides extensive downloadable code you can run, explore, and adapt. You will Build a complete virtualized environment for embedded development Understand the workflow of a modern embedded systems project Develop assembly programs, create binary images, and load and run them in the Android emulator Learn what it takes to bring up a bootloader and operating system Move from assembler to C, and explore Android's goldfish hardware interfaces Program serial ports, interrupt controllers, real time clocks, and NAND flash controllers Integrate C runtime libraries Support exception handling and timing Use U-Boot to boot the kernel via NOR or NAND flash processes Gain in-depth knowledge for porting U-Boot to new environments Integrate U-Boot and a Linux kernel into an AOSP and CyanogenMod source tree Create your own Android ROM on a virtual Android device